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
@@ -128,10 +131,14 @@ impl EncodePrivateKey for KeypairBytes {
128
131
private_key[ 1 ] = 0x20 ;
129
132
private_key[ 2 ..] . copy_from_slice ( & self . secret_key ) ;
130
133
131
- let private_key_info = PrivateKeyInfo {
134
+ let private_key_info = PrivateKeyInfoRef {
132
135
algorithm : ALGORITHM_ID ,
133
- private_key : & private_key,
134
- public_key : self . public_key . as_ref ( ) . map ( |pk| pk. 0 . as_slice ( ) ) ,
136
+ private_key : OctetStringRef :: new ( & private_key) ?,
137
+ public_key : self
138
+ . public_key
139
+ . as_ref ( )
140
+ . map ( |pk| BitStringRef :: new ( 0 , & pk. 0 ) )
141
+ . transpose ( ) ?,
135
142
} ;
136
143
137
144
let result = SecretDocument :: encode_msg ( & private_key_info) ?;
@@ -143,10 +150,10 @@ impl EncodePrivateKey for KeypairBytes {
143
150
}
144
151
}
145
152
146
- impl TryFrom < PrivateKeyInfo < ' _ > > for KeypairBytes {
153
+ impl TryFrom < PrivateKeyInfoRef < ' _ > > for KeypairBytes {
147
154
type Error = Error ;
148
155
149
- fn try_from ( private_key : PrivateKeyInfo < ' _ > ) -> Result < Self > {
156
+ fn try_from ( private_key : PrivateKeyInfoRef < ' _ > ) -> Result < Self > {
150
157
private_key. algorithm . assert_algorithm_oid ( ALGORITHM_OID ) ?;
151
158
152
159
if private_key. algorithm . parameters . is_some ( ) {
@@ -161,13 +168,14 @@ impl TryFrom<PrivateKeyInfo<'_>> for KeypairBytes {
161
168
//
162
169
// - 0x04: OCTET STRING tag
163
170
// - 0x20: 32-byte length
164
- let secret_key = match private_key. private_key {
171
+ let secret_key = match private_key. private_key . as_bytes ( ) {
165
172
[ 0x04 , 0x20 , rest @ ..] => rest. try_into ( ) . map_err ( |_| Error :: KeyMalformed ) ,
166
173
_ => Err ( Error :: KeyMalformed ) ,
167
174
} ?;
168
175
169
176
let public_key = private_key
170
177
. public_key
178
+ . and_then ( |bs| bs. as_bytes ( ) )
171
179
. map ( |bytes| bytes. try_into ( ) . map_err ( |_| Error :: KeyMalformed ) )
172
180
. transpose ( ) ?
173
181
. map ( PublicKeyBytes ) ;
0 commit comments