@@ -66,7 +66,9 @@ private PivEccPrivateKey()
66
66
/// <exception cref="ArgumentException">
67
67
/// The size of the private value is not supported by the YubiKey.
68
68
/// </exception>
69
- public PivEccPrivateKey ( ReadOnlySpan < byte > privateValue , PivAlgorithm ? algorithm = null )
69
+ public PivEccPrivateKey (
70
+ ReadOnlySpan < byte > privateValue ,
71
+ PivAlgorithm ? algorithm = null )
70
72
{
71
73
if ( algorithm . HasValue )
72
74
{
@@ -83,8 +85,15 @@ public PivEccPrivateKey(ReadOnlySpan<byte> privateValue, PivAlgorithm? algorithm
83
85
} ;
84
86
}
85
87
88
+ int tag = Algorithm switch
89
+ {
90
+ PivAlgorithm . EccEd25519 => PivConstants . PrivateECEd25519Tag ,
91
+ PivAlgorithm . EccX25519 => PivConstants . PrivateECX25519Tag ,
92
+ _ => PivConstants . PrivateECDsaTag
93
+ } ;
94
+
86
95
var tlvWriter = new TlvWriter ( ) ;
87
- tlvWriter . WriteValue ( PivConstants . PrivateECDsaTag , privateValue ) ;
96
+ tlvWriter . WriteValue ( tag , privateValue ) ;
88
97
EncodedKey = tlvWriter . Encode ( ) ;
89
98
_privateValue = new Memory < byte > ( privateValue . ToArray ( ) ) ;
90
99
}
@@ -94,29 +103,31 @@ public PivEccPrivateKey(ReadOnlySpan<byte> privateValue, PivAlgorithm? algorithm
94
103
/// encoding.
95
104
/// </summary>
96
105
/// <param name="encodedPrivateKey">
97
- /// The PIV TLV encoding.
106
+ /// The PIV TLV encoding.
98
107
/// </param>
108
+ /// <param name="pivAlgorithm"></param>
99
109
/// <returns>
100
110
/// A new instance of a PivEccPrivateKey object based on the encoding.
101
111
/// </returns>
102
112
/// <exception cref="ArgumentException">
103
113
/// The encoding of the private key is not supported.
104
114
/// </exception>
105
- public static PivEccPrivateKey CreateEccPrivateKey ( ReadOnlyMemory < byte > encodedPrivateKey )
115
+ public static PivEccPrivateKey CreateEccPrivateKey (
116
+ ReadOnlyMemory < byte > encodedPrivateKey ,
117
+ PivAlgorithm ? pivAlgorithm )
106
118
{
107
119
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 ) )
110
122
{
111
123
throw new ArgumentException (
112
124
string . Format (
113
125
CultureInfo . CurrentCulture ,
114
126
ExceptionMessages . InvalidPrivateKeyData ) ) ;
115
127
}
116
128
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 ) ;
120
131
}
121
132
122
133
/// <inheritdoc />
0 commit comments