Skip to content

Commit b37d338

Browse files
committed
misc: misc changes in Piv key parameters handling
1 parent d6857c2 commit b37d338

File tree

2 files changed

+51
-31
lines changed

2 files changed

+51
-31
lines changed

Yubico.YubiKey/src/Yubico/YubiKey/Piv/KeyParametersPivExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
// limitations under the License.
1414

1515
using System;
16-
using System.Collections.Generic;
1716
using System.Globalization;
1817
using Yubico.Core.Tlv;
1918
using Yubico.YubiKey.Cryptography;

Yubico.YubiKey/src/Yubico/YubiKey/Piv/KeyParametersPivHelper.cs

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
// limitations under the License.
1414

1515
using System;
16-
using System.Collections.Generic;
1716
using System.Globalization;
1817
using System.Linq;
19-
using System.Numerics;
2018
using System.Security.Cryptography;
2119
using Yubico.Core.Tlv;
2220
using Yubico.YubiKey.Cryptography;
2321

2422
namespace Yubico.YubiKey.Piv;
2523

24+
/// <summary>
25+
/// This class converts from a Piv Encoded Key to either instances of the common IPublicKeyParameters and IPrivateKeyParameters
26+
/// or concrete the concrete types that inherit these interfaces.
27+
/// </summary>
2628
public static class KeyParametersPivHelper
2729
{
2830
public static IPublicKeyParameters CreatePublicKeyParameters(ReadOnlyMemory<byte> pivEncodedKey, KeyType keyType) =>
@@ -40,32 +42,16 @@ public static IPublicKeyParameters CreatePublicKeyParameters(ReadOnlyMemory<byte
4042
ExceptionMessages.InvalidApduResponseData))
4143
};
4244

43-
// TODO Is this needed?
44-
public static IPrivateKeyParameters CreatePrivateKeyParameters(ReadOnlyMemory<byte> value, KeyType keyType) =>
45-
keyType switch
46-
{
47-
KeyType.Ed25519 or KeyType.X25519 => KeyParametersPivHelper
48-
.CreatePrivateCurve25519FromPivEncoding(value, keyType),
49-
KeyType.P256 or KeyType.P384 or KeyType.P521 => KeyParametersPivHelper
50-
.CreatePrivateEcFromPivEncoding(value),
51-
KeyType.RSA1024 or KeyType.RSA2048 or KeyType.RSA3072 or KeyType.RSA4096 => KeyParametersPivHelper
52-
.CreatePrivateRsaFromPivEncoding(value),
53-
_ => throw new InvalidOperationException(
54-
string.Format(
55-
CultureInfo.CurrentCulture,
56-
ExceptionMessages.InvalidApduResponseData))
57-
};
58-
59-
public static RSAPublicKeyParameters CreatePublicRsaFromPivEncoding(ReadOnlyMemory<byte> pivEncodingBytes)
45+
public static RSAPublicKeyParameters CreatePublicRsaFromPivEncoding(ReadOnlyMemory<byte> pivEncodedKey)
6046
{
61-
var (modulus, exponent) = PivEncodingReader.GetPublicRSAValues(pivEncodingBytes);
47+
var (modulus, exponent) = PivEncodingReader.GetPublicRSAValues(pivEncodedKey);
6248
var rsaParameters = new RSAParameters { Modulus = modulus.ToArray(), Exponent = exponent.ToArray() };
6349
return RSAPublicKeyParameters.CreateFromParameters(rsaParameters);
6450
}
6551

66-
public static ECPublicKeyParameters CreatePublicEcFromPivEncoding(ReadOnlyMemory<byte> pivEncodingBytes)
52+
public static ECPublicKeyParameters CreatePublicEcFromPivEncoding(ReadOnlyMemory<byte> pivEncodedKey)
6753
{
68-
var publicPointData = PivEncodingReader.GetECPublicPointValues(pivEncodingBytes);
54+
var publicPointData = PivEncodingReader.GetECPublicPointValues(pivEncodedKey);
6955
if (publicPointData.Span[0] != 0x4)
7056
{
7157
throw new ArgumentException(
@@ -93,17 +79,52 @@ public static ECPublicKeyParameters CreatePublicEcFromPivEncoding(ReadOnlyMemory
9379
return ECPublicKeyParameters.CreateFromParameters(parameters);
9480
}
9581

96-
public static Curve25519PublicKeyParameters CreatePublicCurve25519FromPivEncoding(ReadOnlyMemory<byte> pivEncodingBytes, KeyType keyType)
82+
public static Curve25519PublicKeyParameters CreatePublicCurve25519FromPivEncoding(ReadOnlyMemory<byte> pivEncodedKey, KeyType keyType)
9783
{
98-
var publicPoint = PivEncodingReader.GetECPublicPointValues(pivEncodingBytes);
84+
var publicPoint = PivEncodingReader.GetECPublicPointValues(pivEncodedKey);
9985
return Curve25519PublicKeyParameters.CreateFromValue(publicPoint, keyType);
10086
}
10187

88+
// Private
89+
90+
/// <summary>
91+
/// Creates an instance of <see cref="IPrivateKeyParameters"/> from the
92+
/// given PIV-encoded key.
93+
/// </summary>
94+
/// <remarks>
95+
/// The created instance will be one of the following concrete types:
96+
/// <list type="bullet">
97+
/// <item><see cref="RSAPrivateKeyParameters"/></item>
98+
/// <item><see cref="ECPrivateKeyParameters"/></item>
99+
/// <item><see cref="Curve25519PrivateKeyParameters"/></item>
100+
/// </list>
101+
/// </remarks>
102+
/// <param name="pivEncodedKey">The PIV-encoded key.</param>
103+
/// <param name="keyType">The type of the key.</param>
104+
/// <returns>An instance of <see cref="IPrivateKeyParameters"/>.</returns>
105+
/// <exception cref="InvalidOperationException">
106+
/// The key type is not supported.
107+
/// </exception>
108+
public static IPrivateKeyParameters CreatePrivateKeyParameters(ReadOnlyMemory<byte> pivEncodedKey, KeyType keyType) =>
109+
keyType switch
110+
{
111+
KeyType.Ed25519 or KeyType.X25519 => KeyParametersPivHelper
112+
.CreatePrivateCurve25519FromPivEncoding(pivEncodedKey, keyType),
113+
KeyType.P256 or KeyType.P384 or KeyType.P521 => KeyParametersPivHelper
114+
.CreatePrivateEcFromPivEncoding(pivEncodedKey),
115+
KeyType.RSA1024 or KeyType.RSA2048 or KeyType.RSA3072 or KeyType.RSA4096 => KeyParametersPivHelper
116+
.CreatePrivateRsaFromPivEncoding(pivEncodedKey),
117+
_ => throw new InvalidOperationException(
118+
string.Format(
119+
CultureInfo.CurrentCulture,
120+
ExceptionMessages.InvalidApduResponseData))
121+
};
122+
102123
public static Curve25519PrivateKeyParameters CreatePrivateCurve25519FromPivEncoding(
103-
ReadOnlyMemory<byte> pivEncodingBytes,
124+
ReadOnlyMemory<byte> pivEncodedKey,
104125
KeyType keyType)
105126
{
106-
if (!TlvObject.TryParse(pivEncodingBytes.Span, out var tlv) || !PivConstants.IsValidPrivateECTag(tlv.Tag))
127+
if (!TlvObject.TryParse(pivEncodedKey.Span, out var tlv) || !PivConstants.IsValidPrivateECTag(tlv.Tag))
107128
{
108129
throw new ArgumentException(
109130
string.Format(
@@ -128,9 +149,9 @@ public static Curve25519PrivateKeyParameters CreatePrivateCurve25519FromPivEncod
128149
};
129150
}
130151

131-
public static ECPrivateKeyParameters CreatePrivateEcFromPivEncoding(ReadOnlyMemory<byte> pivEncodingBytes)
152+
public static ECPrivateKeyParameters CreatePrivateEcFromPivEncoding(ReadOnlyMemory<byte> pivEncodedKey)
132153
{
133-
if (!TlvObject.TryParse(pivEncodingBytes.Span, out var tlv) || tlv.Tag != PivConstants.PrivateECDsaTag)
154+
if (!TlvObject.TryParse(pivEncodedKey.Span, out var tlv) || tlv.Tag != PivConstants.PrivateECDsaTag)
134155
{
135156
throw new ArgumentException(
136157
string.Format(
@@ -157,11 +178,11 @@ public static ECPrivateKeyParameters CreatePrivateEcFromPivEncoding(ReadOnlyMemo
157178
}
158179
}
159180

160-
public static RSAPrivateKeyParameters CreatePrivateRsaFromPivEncoding(ReadOnlyMemory<byte> pivEncodingBytes)
181+
public static RSAPrivateKeyParameters CreatePrivateRsaFromPivEncoding(ReadOnlyMemory<byte> pivEncodedKey)
161182
{
162183
const int CrtComponentCount = 5;
163184

164-
var tlvReader = new TlvReader(pivEncodingBytes);
185+
var tlvReader = new TlvReader(pivEncodedKey);
165186
var valueArray = new ReadOnlyMemory<byte>[CrtComponentCount];
166187

167188
int index = 0;

0 commit comments

Comments
 (0)