Skip to content

Commit 2593ce5

Browse files
committed
fix: add backwards compatibility for PivEccPrivateKeys
1 parent 87a29b0 commit 2593ce5

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

Yubico.YubiKey/src/Yubico/YubiKey/Piv/PivEccPrivateKey.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ private PivEccPrivateKey()
6666
/// <exception cref="ArgumentException">
6767
/// The size of the private value is not supported by the YubiKey.
6868
/// </exception>
69-
public PivEccPrivateKey(ReadOnlySpan<byte> privateValue, PivAlgorithm? algorithm = null)
69+
public PivEccPrivateKey(
70+
ReadOnlySpan<byte> privateValue,
71+
PivAlgorithm? algorithm = null)
7072
{
7173
if (algorithm.HasValue)
7274
{
@@ -83,8 +85,15 @@ public PivEccPrivateKey(ReadOnlySpan<byte> privateValue, PivAlgorithm? algorithm
8385
};
8486
}
8587

88+
int tag = Algorithm switch
89+
{
90+
PivAlgorithm.EccEd25519 => PivConstants.PrivateECEd25519Tag,
91+
PivAlgorithm.EccX25519 => PivConstants.PrivateECX25519Tag,
92+
_ => PivConstants.PrivateECDsaTag
93+
};
94+
8695
var tlvWriter = new TlvWriter();
87-
tlvWriter.WriteValue(PivConstants.PrivateECDsaTag, privateValue);
96+
tlvWriter.WriteValue(tag, privateValue);
8897
EncodedKey = tlvWriter.Encode();
8998
_privateValue = new Memory<byte>(privateValue.ToArray());
9099
}
@@ -94,29 +103,31 @@ public PivEccPrivateKey(ReadOnlySpan<byte> privateValue, PivAlgorithm? algorithm
94103
/// encoding.
95104
/// </summary>
96105
/// <param name="encodedPrivateKey">
97-
/// The PIV TLV encoding.
106+
/// The PIV TLV encoding.
98107
/// </param>
108+
/// <param name="pivAlgorithm"></param>
99109
/// <returns>
100110
/// A new instance of a PivEccPrivateKey object based on the encoding.
101111
/// </returns>
102112
/// <exception cref="ArgumentException">
103113
/// The encoding of the private key is not supported.
104114
/// </exception>
105-
public static PivEccPrivateKey CreateEccPrivateKey(ReadOnlyMemory<byte> encodedPrivateKey)
115+
public static PivEccPrivateKey CreateEccPrivateKey(
116+
ReadOnlyMemory<byte> encodedPrivateKey,
117+
PivAlgorithm? pivAlgorithm)
106118
{
107119
var tlvReader = new TlvReader(encodedPrivateKey);
108-
109-
if (tlvReader.HasData == false || tlvReader.PeekTag() != PivConstants.PrivateECDsaTag)
120+
int tag = tlvReader.PeekTag();
121+
if (tlvReader.HasData == false || !PivConstants.IsValidPrivateECTag(tag))
110122
{
111123
throw new ArgumentException(
112124
string.Format(
113125
CultureInfo.CurrentCulture,
114126
ExceptionMessages.InvalidPrivateKeyData));
115127
}
116128

117-
var value = tlvReader.ReadValue(PivConstants.PrivateECDsaTag);
118-
119-
return new PivEccPrivateKey(value.Span);
129+
var value = tlvReader.ReadValue(tag);
130+
return new PivEccPrivateKey(value.Span, pivAlgorithm);
120131
}
121132

122133
/// <inheritdoc />

Yubico.YubiKey/src/Yubico/YubiKey/Piv/PivPrivateKey.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,17 @@ public PivPrivateKey()
9494
/// <c>PivEccPrivateKey</c>.
9595
/// </remarks>
9696
/// <param name="encodedPrivateKey">
97-
/// The PIV TLV encoding.
97+
/// The PIV TLV encoding.
9898
/// </param>
99+
/// <param name="pivAlgorithm"></param>
99100
/// <returns>
100101
/// An instance of a subclass of <c>PivPrivateKey</c>, the actual key
101102
/// represented by the encoding.
102103
/// </returns>
103104
/// <exception cref="ArgumentException">
104105
/// The key data supplied is not a supported encoding.
105106
/// </exception>
106-
public static PivPrivateKey Create(ReadOnlyMemory<byte> encodedPrivateKey)
107+
public static PivPrivateKey Create(ReadOnlyMemory<byte> encodedPrivateKey, PivAlgorithm? pivAlgorithm = null)
107108
{
108109
byte tag = 0;
109110
if (encodedPrivateKey.Length > 0)
@@ -119,14 +120,10 @@ public static PivPrivateKey Create(ReadOnlyMemory<byte> encodedPrivateKey)
119120
CultureInfo.CurrentCulture,
120121
ExceptionMessages.InvalidPrivateKeyData));
121122

122-
case PivConstants.PrivateECDsaTag:
123-
return PivEccPrivateKey.CreateEccPrivateKey(encodedPrivateKey);
123+
case var _ when PivConstants.IsValidPrivateECTag(tag):
124+
return PivEccPrivateKey.CreateEccPrivateKey(encodedPrivateKey, pivAlgorithm);
124125

125-
case PivConstants.PrivateRSAPrimePTag:
126-
case PivConstants.PrivateRSAPrimeQTag:
127-
case PivConstants.PrivateRSAExponentPTag:
128-
case PivConstants.PrivateRSAExponentQTag:
129-
case PivConstants.PrivateRSACoefficientTag:
126+
case var _ when PivConstants.IsValidPrivateRSATag(tag):
130127
return PivRsaPrivateKey.CreateRsaPrivateKey(encodedPrivateKey);
131128
}
132129
}

0 commit comments

Comments
 (0)