29
29
sec1:: { EncodedPoint , ModulusSize , ValidatePublicKey } ,
30
30
FieldBytesSize ,
31
31
} ,
32
- sec1:: der,
32
+ sec1:: der:: { self , oid :: AssociatedOid } ,
33
33
} ;
34
34
35
35
#[ cfg( all( feature = "alloc" , feature = "arithmetic" , feature = "sec1" ) ) ]
@@ -184,7 +184,7 @@ where
184
184
#[ cfg( feature = "sec1" ) ]
185
185
pub fn from_sec1_der ( der_bytes : & [ u8 ] ) -> Result < Self >
186
186
where
187
- C : Curve + ValidatePublicKey ,
187
+ C : AssociatedOid + Curve + ValidatePublicKey ,
188
188
FieldBytesSize < C > : ModulusSize ,
189
189
{
190
190
sec1:: EcPrivateKey :: try_from ( der_bytes) ?
@@ -196,17 +196,18 @@ where
196
196
#[ cfg( all( feature = "alloc" , feature = "arithmetic" , feature = "sec1" ) ) ]
197
197
pub fn to_sec1_der ( & self ) -> der:: Result < Zeroizing < Vec < u8 > > >
198
198
where
199
- C : CurveArithmetic ,
199
+ C : AssociatedOid + CurveArithmetic ,
200
200
AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
201
201
FieldBytesSize < C > : ModulusSize ,
202
202
{
203
203
let private_key_bytes = Zeroizing :: new ( self . to_bytes ( ) ) ;
204
204
let public_key_bytes = self . public_key ( ) . to_encoded_point ( false ) ;
205
+ let parameters = sec1:: EcParameters :: NamedCurve ( C :: OID ) ;
205
206
206
207
let ec_private_key = Zeroizing :: new (
207
208
sec1:: EcPrivateKey {
208
209
private_key : & private_key_bytes,
209
- parameters : None ,
210
+ parameters : Some ( parameters ) ,
210
211
public_key : Some ( public_key_bytes. as_bytes ( ) ) ,
211
212
}
212
213
. to_der ( ) ?,
@@ -225,7 +226,7 @@ where
225
226
#[ cfg( feature = "pem" ) ]
226
227
pub fn from_sec1_pem ( s : & str ) -> Result < Self >
227
228
where
228
- C : Curve + ValidatePublicKey ,
229
+ C : AssociatedOid + Curve + ValidatePublicKey ,
229
230
FieldBytesSize < C > : ModulusSize ,
230
231
{
231
232
let ( label, der_bytes) = pem:: decode_vec ( s. as_bytes ( ) ) . map_err ( |_| Error ) ?;
@@ -244,7 +245,7 @@ where
244
245
#[ cfg( feature = "pem" ) ]
245
246
pub fn to_sec1_pem ( & self , line_ending : pem:: LineEnding ) -> Result < Zeroizing < String > >
246
247
where
247
- C : CurveArithmetic ,
248
+ C : AssociatedOid + CurveArithmetic ,
248
249
AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
249
250
FieldBytesSize < C > : ModulusSize ,
250
251
{
@@ -344,16 +345,21 @@ where
344
345
#[ cfg( feature = "sec1" ) ]
345
346
impl < C > TryFrom < sec1:: EcPrivateKey < ' _ > > for SecretKey < C >
346
347
where
347
- C : Curve + ValidatePublicKey ,
348
+ C : AssociatedOid + Curve + ValidatePublicKey ,
348
349
FieldBytesSize < C > : ModulusSize ,
349
350
{
350
351
type Error = der:: Error ;
351
352
352
353
fn try_from ( sec1_private_key : sec1:: EcPrivateKey < ' _ > ) -> der:: Result < Self > {
354
+ if let Some ( sec1:: EcParameters :: NamedCurve ( curve_oid) ) = sec1_private_key. parameters {
355
+ if C :: OID != curve_oid {
356
+ return Err ( der:: Tag :: ObjectIdentifier . value_error ( ) ) ;
357
+ }
358
+ }
359
+
353
360
let secret_key = Self :: from_slice ( sec1_private_key. private_key )
354
- . map_err ( |_| der:: Tag :: Sequence . value_error ( ) ) ?;
361
+ . map_err ( |_| der:: Tag :: OctetString . value_error ( ) ) ?;
355
362
356
- // TODO(tarcieri): validate `sec1_private_key.params`?
357
363
if let Some ( pk_bytes) = sec1_private_key. public_key {
358
364
let pk = EncodedPoint :: < C > :: from_bytes ( pk_bytes)
359
365
. map_err ( |_| der:: Tag :: BitString . value_error ( ) ) ?;
0 commit comments