1515//! breaking changes when using this module.
1616
1717pub use pkcs8:: {
18- spki, DecodePrivateKey , DecodePublicKey , Error , ObjectIdentifier , PrivateKeyInfo , Result ,
18+ spki, DecodePrivateKey , DecodePublicKey , Error , ObjectIdentifier , PrivateKeyInfoRef , Result ,
1919} ;
2020
2121#[ cfg( feature = "alloc" ) ]
2222pub use pkcs8:: { spki:: EncodePublicKey , EncodePrivateKey } ;
2323
2424#[ cfg( feature = "alloc" ) ]
25- pub use pkcs8:: der:: { asn1:: BitStringRef , Document , SecretDocument } ;
25+ pub use pkcs8:: der:: {
26+ asn1:: { BitStringRef , OctetStringRef } ,
27+ Document , SecretDocument ,
28+ } ;
2629
2730#[ cfg( feature = "zeroize" ) ]
2831use zeroize:: Zeroize ;
@@ -122,10 +125,14 @@ impl EncodePrivateKey for KeypairBytes {
122125 private_key[ 1 ] = 0x39 ;
123126 private_key[ 2 ..] . copy_from_slice ( & self . secret_key ) ;
124127
125- let private_key_info = PrivateKeyInfo {
128+ let private_key_info = PrivateKeyInfoRef {
126129 algorithm : ALGORITHM_ID ,
127- private_key : & private_key,
128- public_key : self . public_key . as_ref ( ) . map ( |pk| pk. 0 . as_slice ( ) ) ,
130+ private_key : OctetStringRef :: new ( & private_key) ?,
131+ public_key : self
132+ . public_key
133+ . as_ref ( )
134+ . map ( |pk| BitStringRef :: new ( 0 , & pk. 0 ) )
135+ . transpose ( ) ?,
129136 } ;
130137 let result = SecretDocument :: encode_msg ( & private_key_info) ?;
131138
@@ -136,10 +143,10 @@ impl EncodePrivateKey for KeypairBytes {
136143 }
137144}
138145
139- impl TryFrom < PrivateKeyInfo < ' _ > > for KeypairBytes {
146+ impl TryFrom < PrivateKeyInfoRef < ' _ > > for KeypairBytes {
140147 type Error = Error ;
141148
142- fn try_from ( private_key : PrivateKeyInfo < ' _ > ) -> Result < Self > {
149+ fn try_from ( private_key : PrivateKeyInfoRef < ' _ > ) -> Result < Self > {
143150 private_key. algorithm . assert_algorithm_oid ( ALGORITHM_OID ) ?;
144151
145152 if private_key. algorithm . parameters . is_some ( ) {
@@ -154,13 +161,14 @@ impl TryFrom<PrivateKeyInfo<'_>> for KeypairBytes {
154161 //
155162 // - 0x04: OCTET STRING tag
156163 // - 0x39: 57-byte length
157- let secret_key = match private_key. private_key {
164+ let secret_key = match private_key. private_key . as_bytes ( ) {
158165 [ 0x04 , 0x39 , rest @ ..] => rest. try_into ( ) . map_err ( |_| Error :: KeyMalformed ) ,
159166 _ => Err ( Error :: KeyMalformed ) ,
160167 } ?;
161168
162169 let public_key = private_key
163170 . public_key
171+ . and_then ( |bs| bs. as_bytes ( ) )
164172 . map ( |bytes| bytes. try_into ( ) . map_err ( |_| Error :: KeyMalformed ) )
165173 . transpose ( ) ?
166174 . map ( PublicKeyBytes ) ;
0 commit comments