@@ -425,6 +425,28 @@ where
425
425
}
426
426
}
427
427
428
+ #[ cfg( feature = "serde" ) ]
429
+ impl < C > PublicKey < C >
430
+ where
431
+ C : AssociatedOid + CurveArithmetic ,
432
+ AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
433
+ FieldBytesSize < C > : ModulusSize ,
434
+ {
435
+ /// Encode this [`PublicKey`] as der bytes, placing the result in `output`. This function
436
+ /// returns a slice containing the encoded DER bytes.
437
+ fn encode_as_der < ' buf > ( & self , output : & ' buf mut [ u8 ] ) -> der:: Result < & ' buf [ u8 ] > {
438
+ let public_key_bytes = self . to_encoded_point ( false ) ;
439
+ let subject_public_key = der:: asn1:: BitStringRef :: new ( 0 , public_key_bytes. as_bytes ( ) ) ?;
440
+
441
+ let spki = pkcs8:: SubjectPublicKeyInfo {
442
+ algorithm : Self :: ALGORITHM_IDENTIFIER ,
443
+ subject_public_key,
444
+ } ;
445
+
446
+ der:: Encode :: encode_to_slice ( & spki, output)
447
+ }
448
+ }
449
+
428
450
#[ cfg( all( feature = "alloc" , feature = "pkcs8" ) ) ]
429
451
impl < C > EncodePublicKey for PublicKey < C >
430
452
where
@@ -436,6 +458,7 @@ where
436
458
let public_key_bytes = self . to_encoded_point ( false ) ;
437
459
let subject_public_key = der:: asn1:: BitStringRef :: new ( 0 , public_key_bytes. as_bytes ( ) ) ?;
438
460
461
+ // TODO: use `encode_as_der` here?
439
462
pkcs8:: SubjectPublicKeyInfo {
440
463
algorithm : Self :: ALGORITHM_IDENTIFIER ,
441
464
subject_public_key,
@@ -483,7 +506,11 @@ where
483
506
where
484
507
S : ser:: Serializer ,
485
508
{
486
- let der = self . to_public_key_der ( ) . map_err ( ser:: Error :: custom) ?;
509
+ // TODO: can we determine DER encoding length up-front? Using `MockCurve` gives
510
+ // 91 bytes of output, but it feels like that depends on the curve that is being
511
+ // used here.
512
+ let mut buf = [ 0u8 ; 91 ] ;
513
+ let der = self . encode_as_der ( & mut buf) . map_err ( ser:: Error :: custom) ?;
487
514
serdect:: slice:: serialize_hex_upper_or_bin ( & der, serializer)
488
515
}
489
516
}
0 commit comments