-
Notifications
You must be signed in to change notification settings - Fork 60
fix: Fixed bug importing PIV private key in legacy classes #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Test Results: Windows 2 files 2 suites 9s ⏱️ Results for commit 5b1352d. ♻️ This comment has been updated with latest results. |
Test Results: Ubuntu 2 files 2 suites 14s ⏱️ Results for commit 5b1352d. ♻️ This comment has been updated with latest results. |
Test Results: MacOS 2 files 2 suites 8s ⏱️ Results for commit 5b1352d. ♻️ This comment has been updated with latest results. |
| _ => throw new ArgumentException("The type conversion for the specified key type is not supported", nameof(parameters)) | ||
|
|
||
| #pragma warning disable CS0618 // Type or member is obsolete | ||
| PivPublicKey p => p.PivEncodedPublicKey.ToArray(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the bugfix
| _ => throw new ArgumentException("The type conversion for the specified key type is not supported", nameof(parameters)) | ||
|
|
||
| #pragma warning disable CS0618 // Type or member is obsolete | ||
| PivPrivateKey p => p.EncodedPrivateKey.ToArray(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the bugfix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a bug in importing legacy PIV private keys and deprecates old Piv* key types in favor of modern IPublicKey/IPrivateKey implementations, while improving memory handling and code structure.
- Deprecate legacy
PivEcc*/PivRsa*types and update obsolete messages. - Replace raw byte arrays with
Memory<byte>andSpan<byte>in critical classes for better safety. - Add helper constructors and validation, plus code cleanup and new extension methods.
Reviewed Changes
Copilot reviewed 31 out of 31 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| ECPublicKeyTests.cs | Wrap obsolete AsPivPublicKey calls with warning pragmas |
| ECPrivateKeyTests.cs | Wrap obsolete AsPivPrivateKey calls with warning pragmas |
| ImportTests.cs | Add integration test for legacy PivEccPrivateKey import |
| PivSession.KeyPairs.cs | Use ZeroingMemoryHandle for key import and update obsolete message |
| PivSession.Crypto.cs | Update obsolete attribute message |
| PivSession.Attestation.cs | Update obsolete attribute message |
| PivRsaPublicKey.cs | Mark PivRsaPublicKey obsolete |
| PivRsaPrivateKey.cs | Mark PivRsaPrivateKey obsolete |
| PivPublicKey.cs | Refactor Create, add ExportSubjectPublicKeyInfo, update obsolete |
| PivPrivateKey.cs | Mark PivPrivateKey obsolete |
| PivMetadata.cs | Update obsolete attribute message |
| PivEccPublicKey.cs | Mark class obsolete, remove redundant comments |
| PivEccPrivateKey.cs | Add size validation helpers |
| Converters/PivKeyEncoder.cs | Convert to static, deprecate methods |
| Converters/KeyExtensions.cs | Replace PivKeyEncoder calls with extension methods |
| Commands/ImportAsymmetricKeyCommand.cs | Update obsolete attribute message |
| Commands/GenerateKeyPairResponse.cs | Disable obsolete warnings around legacy return types |
| Cryptography/ZeroingMemoryHandle.cs | Rewrite to use Memory<byte>, remove enumerator interfaces |
| Cryptography/EcdsaVerify.cs | Add EcpPublicKey constructor and preserve legacy constructor |
| Cryptography/AsnPrivateKeyEncoder.cs | Switch to .Span for private key octet string writes |
Comments suppressed due to low confidence (5)
Yubico.YubiKey/src/Yubico/YubiKey/Piv/Converters/PivKeyEncoder.cs:82
- The error message says "Unsupported public key type" in the private-key encoder. It should read "Unsupported private key type" to accurately describe the failure.
_ => throw new ArgumentException("Unsupported public key type."),
Yubico.YubiKey/src/Yubico/YubiKey/Piv/PivEccPublicKey.cs:216
- [nitpick] A comment explaining why
YubiKeyEncodedKeyis set using a slice was removed. Reintroduce a brief note (e.g. "// The metadata-encoded key is the nested slice of PivEncodedKey") to aid future readers.
PivEncodedKey = tlvWriter.Encode();
Yubico.YubiKey/src/Yubico/YubiKey/Piv/Converters/KeyExtensions.cs:33
- The XML summary still refers to a "byte array" but the method now returns
Memory<byte>. Update the documentation to reflect the change in return type.
/// <returns>A BER encoded byte array containing the encoded public key.</returns>
Yubico.YubiKey/src/Yubico/YubiKey/Cryptography/EcdsaVerify.cs:176
- A new constructor for
ECPublicKeywas added but no unit tests verify its behavior. Consider adding tests that cover passing valid and invalidECPublicKeyinstances.
public EcdsaVerify(ECPublicKey publicKey)
Yubico.YubiKey/src/Yubico/YubiKey/Piv/PivPublicKey.cs:150
- The new
ExportSubjectPublicKeyInfooverride should be covered by unit tests to ensure it returns a valid SPKI encoding for both RSA and ECC cases.
public override byte[] ExportSubjectPublicKeyInfo()
| #pragma warning restore CS0618 // Type or member is obsolete | ||
| { |
Copilot
AI
May 26, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The warning disable only wraps the class declaration, but the GetData() method still emits CS0618 warnings on the return type. Consider extending the pragma to cover GetData() or annotate it with [Obsolete] to fully suppress warnings.
| #pragma warning restore CS0618 // Type or member is obsolete | |
| { | |
| { | |
| #pragma warning disable CS0618 // Type or member is obsolete |
AdamVe
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This PR fixes #228 where one could where a legacy PivPrivateKey would not be handled properly in the new
ImportPrivateKey(byte slotNumber, IPrivateKey). [1] as well as #227, when creating aPivEccPrivateKey(privateKeyBytes, PivAlgorithm)where the PivAlgorithm would not be set. [2]Additionally, deprecating older key types (
PivEccPublic,PivEccPrivateKey, etc.) in favor of modern implementations (ECPublicKey,ECPrivateKey,RSAPublicKey, etc.) and refining code structure. Below are the most significant changes grouped by theme:Summarized by Copilot:
Deprecation of Legacy Key Types
PivEccPublic,PivEccPrivateKey,PivRsaPublic, andPivRsaPrivateKeyas obsolete, encouraging the use ofECPublicKey,ECPrivateKey,RSAPublicKey, and similar implementations instead. This affects multiple classes, includingPivEccPrivateKey,PivEccPublicKey, andPivPrivateKey. [1] [2] [3] [4]Memory Handling Improvements
ZeroingMemoryHandleto useMemory<byte>instead ofbyte[], simplifying the class and improving memory safety. Removed unnecessary methods likeCopyToandSlice..Spanwhen accessingMemory<byte>inAsnPrivateKeyEncoder.csto improve compatibility and performance. [1] [2]New Constructor and Validation Enhancements
EcdsaVerifyforECPublicKeywith validation for key type and curve.PivEccPrivateKey. [1] [2]Obsolete Method and Class Updates
PivKeyEncoderfor encoding keys, redirecting users toKeyExtensionsfor modern alternatives. [1] [2]KeyExtensionsto handle obsoletePivPublicKeyandPivPrivateKeywith warnings for backward compatibility.Code Cleanup and Refactoring
PivEccPublicKeyandPivMetadata. [1] [2]