15
15
//! breaking changes when using this module.
16
16
17
17
pub use pkcs8:: {
18
- spki, DecodePrivateKey , DecodePublicKey , Error , ObjectIdentifier , PrivateKeyInfo , Result ,
18
+ spki, DecodePrivateKey , DecodePublicKey , Error , ObjectIdentifier , PrivateKeyInfoRef , Result ,
19
19
} ;
20
20
21
21
#[ cfg( feature = "alloc" ) ]
22
22
pub use pkcs8:: { spki:: EncodePublicKey , EncodePrivateKey } ;
23
23
24
24
#[ 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
+ } ;
26
29
27
30
use core:: fmt;
28
31
@@ -112,21 +115,26 @@ impl EncodePrivateKey for KeypairBytes {
112
115
private_key[ 0 ] = 0x04 ;
113
116
private_key[ 1 ] = 0x39 ;
114
117
private_key[ 2 ..] . copy_from_slice ( & self . secret_key ) ;
118
+ let private_key = OctetStringRef :: new ( & private_key) ?;
115
119
116
- let private_key_info = PrivateKeyInfo {
120
+ let private_key_info = PrivateKeyInfoRef {
117
121
algorithm : ALGORITHM_ID ,
118
- private_key : & private_key,
119
- public_key : self . public_key . as_ref ( ) . map ( |pk| pk. 0 . as_slice ( ) ) ,
122
+ private_key : private_key,
123
+ public_key : self
124
+ . public_key
125
+ . as_ref ( )
126
+ . map ( |pk| BitStringRef :: new ( 0 , & pk. 0 ) )
127
+ . transpose ( ) ?,
120
128
} ;
121
129
122
130
Ok ( SecretDocument :: encode_msg ( & private_key_info) ?)
123
131
}
124
132
}
125
133
126
- impl TryFrom < PrivateKeyInfo < ' _ > > for KeypairBytes {
134
+ impl TryFrom < PrivateKeyInfoRef < ' _ > > for KeypairBytes {
127
135
type Error = Error ;
128
136
129
- fn try_from ( private_key : PrivateKeyInfo < ' _ > ) -> Result < Self > {
137
+ fn try_from ( private_key : PrivateKeyInfoRef < ' _ > ) -> Result < Self > {
130
138
private_key. algorithm . assert_algorithm_oid ( ALGORITHM_OID ) ?;
131
139
132
140
if private_key. algorithm . parameters . is_some ( ) {
@@ -141,13 +149,14 @@ impl TryFrom<PrivateKeyInfo<'_>> for KeypairBytes {
141
149
//
142
150
// - 0x04: OCTET STRING tag
143
151
// - 0x39: 57-byte length
144
- let secret_key = match private_key. private_key {
152
+ let secret_key = match private_key. private_key . as_bytes ( ) {
145
153
[ 0x04 , 0x39 , rest @ ..] => rest. try_into ( ) . map_err ( |_| Error :: KeyMalformed ) ,
146
154
_ => Err ( Error :: KeyMalformed ) ,
147
155
} ?;
148
156
149
157
let public_key = private_key
150
158
. public_key
159
+ . and_then ( |bs| bs. as_bytes ( ) )
151
160
. map ( |bytes| bytes. try_into ( ) . map_err ( |_| Error :: KeyMalformed ) )
152
161
. transpose ( ) ?
153
162
. map ( PublicKeyBytes ) ;
0 commit comments