13
13
// limitations under the License.
14
14
15
15
using System ;
16
- using System . Collections . Generic ;
17
16
using System . Globalization ;
18
17
using System . Linq ;
19
- using System . Numerics ;
20
18
using System . Security . Cryptography ;
21
19
using Yubico . Core . Tlv ;
22
20
using Yubico . YubiKey . Cryptography ;
23
21
24
22
namespace Yubico . YubiKey . Piv ;
25
23
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>
26
28
public static class KeyParametersPivHelper
27
29
{
28
30
public static IPublicKeyParameters CreatePublicKeyParameters ( ReadOnlyMemory < byte > pivEncodedKey , KeyType keyType ) =>
@@ -40,32 +42,16 @@ public static IPublicKeyParameters CreatePublicKeyParameters(ReadOnlyMemory<byte
40
42
ExceptionMessages . InvalidApduResponseData ) )
41
43
} ;
42
44
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 )
60
46
{
61
- var ( modulus , exponent ) = PivEncodingReader . GetPublicRSAValues ( pivEncodingBytes ) ;
47
+ var ( modulus , exponent ) = PivEncodingReader . GetPublicRSAValues ( pivEncodedKey ) ;
62
48
var rsaParameters = new RSAParameters { Modulus = modulus . ToArray ( ) , Exponent = exponent . ToArray ( ) } ;
63
49
return RSAPublicKeyParameters . CreateFromParameters ( rsaParameters ) ;
64
50
}
65
51
66
- public static ECPublicKeyParameters CreatePublicEcFromPivEncoding ( ReadOnlyMemory < byte > pivEncodingBytes )
52
+ public static ECPublicKeyParameters CreatePublicEcFromPivEncoding ( ReadOnlyMemory < byte > pivEncodedKey )
67
53
{
68
- var publicPointData = PivEncodingReader . GetECPublicPointValues ( pivEncodingBytes ) ;
54
+ var publicPointData = PivEncodingReader . GetECPublicPointValues ( pivEncodedKey ) ;
69
55
if ( publicPointData . Span [ 0 ] != 0x4 )
70
56
{
71
57
throw new ArgumentException (
@@ -93,17 +79,52 @@ public static ECPublicKeyParameters CreatePublicEcFromPivEncoding(ReadOnlyMemory
93
79
return ECPublicKeyParameters . CreateFromParameters ( parameters ) ;
94
80
}
95
81
96
- public static Curve25519PublicKeyParameters CreatePublicCurve25519FromPivEncoding ( ReadOnlyMemory < byte > pivEncodingBytes , KeyType keyType )
82
+ public static Curve25519PublicKeyParameters CreatePublicCurve25519FromPivEncoding ( ReadOnlyMemory < byte > pivEncodedKey , KeyType keyType )
97
83
{
98
- var publicPoint = PivEncodingReader . GetECPublicPointValues ( pivEncodingBytes ) ;
84
+ var publicPoint = PivEncodingReader . GetECPublicPointValues ( pivEncodedKey ) ;
99
85
return Curve25519PublicKeyParameters . CreateFromValue ( publicPoint , keyType ) ;
100
86
}
101
87
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
+
102
123
public static Curve25519PrivateKeyParameters CreatePrivateCurve25519FromPivEncoding (
103
- ReadOnlyMemory < byte > pivEncodingBytes ,
124
+ ReadOnlyMemory < byte > pivEncodedKey ,
104
125
KeyType keyType )
105
126
{
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 ) )
107
128
{
108
129
throw new ArgumentException (
109
130
string . Format (
@@ -128,9 +149,9 @@ public static Curve25519PrivateKeyParameters CreatePrivateCurve25519FromPivEncod
128
149
} ;
129
150
}
130
151
131
- public static ECPrivateKeyParameters CreatePrivateEcFromPivEncoding ( ReadOnlyMemory < byte > pivEncodingBytes )
152
+ public static ECPrivateKeyParameters CreatePrivateEcFromPivEncoding ( ReadOnlyMemory < byte > pivEncodedKey )
132
153
{
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 )
134
155
{
135
156
throw new ArgumentException (
136
157
string . Format (
@@ -157,11 +178,11 @@ public static ECPrivateKeyParameters CreatePrivateEcFromPivEncoding(ReadOnlyMemo
157
178
}
158
179
}
159
180
160
- public static RSAPrivateKeyParameters CreatePrivateRsaFromPivEncoding ( ReadOnlyMemory < byte > pivEncodingBytes )
181
+ public static RSAPrivateKeyParameters CreatePrivateRsaFromPivEncoding ( ReadOnlyMemory < byte > pivEncodedKey )
161
182
{
162
183
const int CrtComponentCount = 5 ;
163
184
164
- var tlvReader = new TlvReader ( pivEncodingBytes ) ;
185
+ var tlvReader = new TlvReader ( pivEncodedKey ) ;
165
186
var valueArray = new ReadOnlyMemory < byte > [ CrtComponentCount ] ;
166
187
167
188
int index = 0 ;
0 commit comments