@@ -9,7 +9,7 @@ use der::{
9
9
use pkcs5:: EncryptionScheme ;
10
10
11
11
#[ cfg( feature = "alloc" ) ]
12
- use der:: SecretDocument ;
12
+ use { alloc :: boxed :: Box , der:: SecretDocument } ;
13
13
14
14
#[ cfg( feature = "encryption" ) ]
15
15
use {
@@ -40,23 +40,26 @@ use der::pem::PemLabel;
40
40
///
41
41
/// [RFC 5208 Section 6]: https://tools.ietf.org/html/rfc5208#section-6
42
42
#[ derive( Clone , Eq , PartialEq ) ]
43
- pub struct EncryptedPrivateKeyInfo < ' a > {
43
+ pub struct EncryptedPrivateKeyInfo < Data > {
44
44
/// Algorithm identifier describing a password-based symmetric encryption
45
45
/// scheme used to encrypt the `encrypted_data` field.
46
46
pub encryption_algorithm : EncryptionScheme ,
47
47
48
48
/// Private key data
49
- pub encrypted_data : & ' a [ u8 ] ,
49
+ pub encrypted_data : Data ,
50
50
}
51
51
52
- impl < ' a > EncryptedPrivateKeyInfo < ' a > {
52
+ impl < ' a , Data > EncryptedPrivateKeyInfo < Data >
53
+ where
54
+ Data : AsRef < [ u8 ] > + From < & ' a [ u8 ] > ,
55
+ {
53
56
/// Attempt to decrypt this encrypted private key using the provided
54
57
/// password to derive an encryption key.
55
58
#[ cfg( feature = "encryption" ) ]
56
59
pub fn decrypt ( & self , password : impl AsRef < [ u8 ] > ) -> Result < SecretDocument > {
57
60
Ok ( self
58
61
. encryption_algorithm
59
- . decrypt ( password, self . encrypted_data ) ?
62
+ . decrypt ( password, self . encrypted_data . as_ref ( ) ) ?
60
63
. try_into ( ) ?)
61
64
}
62
65
@@ -75,7 +78,7 @@ impl<'a> EncryptedPrivateKeyInfo<'a> {
75
78
rng. fill_bytes ( & mut iv) ;
76
79
77
80
let pbes2_params = pbes2:: Parameters :: scrypt_aes256cbc ( Default :: default ( ) , & salt, iv) ?;
78
- EncryptedPrivateKeyInfo :: encrypt_with ( pbes2_params, password, doc)
81
+ Self :: encrypt_with ( pbes2_params, password, doc)
79
82
}
80
83
81
84
/// Encrypt this private key using a symmetric encryption key derived
@@ -90,50 +93,59 @@ impl<'a> EncryptedPrivateKeyInfo<'a> {
90
93
91
94
EncryptedPrivateKeyInfo {
92
95
encryption_algorithm : pbes2_params. into ( ) ,
93
- encrypted_data : & encrypted_data ,
96
+ encrypted_data,
94
97
}
95
98
. try_into ( )
96
99
}
97
100
}
98
101
99
- impl < ' a > DecodeValue < ' a > for EncryptedPrivateKeyInfo < ' a > {
100
- fn decode_value < R : Reader < ' a > > (
101
- reader : & mut R ,
102
- header : Header ,
103
- ) -> der:: Result < EncryptedPrivateKeyInfo < ' a > > {
102
+ impl < ' a , Data > DecodeValue < ' a > for EncryptedPrivateKeyInfo < Data >
103
+ where
104
+ Data : From < & ' a [ u8 ] > ,
105
+ {
106
+ fn decode_value < R : Reader < ' a > > ( reader : & mut R , header : Header ) -> der:: Result < Self > {
104
107
reader. read_nested ( header. length , |reader| {
105
108
Ok ( Self {
106
109
encryption_algorithm : reader. decode ( ) ?,
107
- encrypted_data : OctetStringRef :: decode ( reader) ?. as_bytes ( ) ,
110
+ encrypted_data : OctetStringRef :: decode ( reader) ?. as_bytes ( ) . into ( ) ,
108
111
} )
109
112
} )
110
113
}
111
114
}
112
115
113
- impl EncodeValue for EncryptedPrivateKeyInfo < ' _ > {
116
+ impl < Data > EncodeValue for EncryptedPrivateKeyInfo < Data >
117
+ where
118
+ Data : AsRef < [ u8 ] > ,
119
+ {
114
120
fn value_len ( & self ) -> der:: Result < Length > {
115
121
self . encryption_algorithm . encoded_len ( ) ?
116
- + OctetStringRef :: new ( self . encrypted_data ) ?. encoded_len ( ) ?
122
+ + OctetStringRef :: new ( self . encrypted_data . as_ref ( ) ) ?. encoded_len ( ) ?
117
123
}
118
124
119
125
fn encode_value ( & self , writer : & mut impl Writer ) -> der:: Result < ( ) > {
120
126
self . encryption_algorithm . encode ( writer) ?;
121
- OctetStringRef :: new ( self . encrypted_data ) ?. encode ( writer) ?;
127
+ OctetStringRef :: new ( self . encrypted_data . as_ref ( ) ) ?. encode ( writer) ?;
122
128
Ok ( ( ) )
123
129
}
124
130
}
125
131
126
- impl < ' a > Sequence < ' a > for EncryptedPrivateKeyInfo < ' a > { }
132
+ impl < ' a , Data > Sequence < ' a > for EncryptedPrivateKeyInfo < Data > where
133
+ Data : AsRef < [ u8 ] > + From < & ' a [ u8 ] >
134
+ {
135
+ }
127
136
128
- impl < ' a > TryFrom < & ' a [ u8 ] > for EncryptedPrivateKeyInfo < ' a > {
137
+ impl < ' a , Data > TryFrom < & ' a [ u8 ] > for EncryptedPrivateKeyInfo < Data >
138
+ where
139
+ Data : AsRef < [ u8 ] > + From < & ' a [ u8 ] > ,
140
+ {
129
141
type Error = Error ;
130
142
131
143
fn try_from ( bytes : & ' a [ u8 ] ) -> Result < Self > {
132
144
Ok ( Self :: from_der ( bytes) ?)
133
145
}
134
146
}
135
147
136
- impl < ' a > fmt:: Debug for EncryptedPrivateKeyInfo < ' a > {
148
+ impl < Data > fmt:: Debug for EncryptedPrivateKeyInfo < Data > {
137
149
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
138
150
f. debug_struct ( "EncryptedPrivateKeyInfo" )
139
151
. field ( "encryption_algorithm" , & self . encryption_algorithm )
@@ -142,24 +154,37 @@ impl<'a> fmt::Debug for EncryptedPrivateKeyInfo<'a> {
142
154
}
143
155
144
156
#[ cfg( feature = "alloc" ) ]
145
- impl TryFrom < EncryptedPrivateKeyInfo < ' _ > > for SecretDocument {
157
+ impl < ' a , Data > TryFrom < EncryptedPrivateKeyInfo < Data > > for SecretDocument
158
+ where
159
+ Data : AsRef < [ u8 ] > + From < & ' a [ u8 ] > ,
160
+ {
146
161
type Error = Error ;
147
162
148
- fn try_from ( encrypted_private_key : EncryptedPrivateKeyInfo < ' _ > ) -> Result < SecretDocument > {
163
+ fn try_from ( encrypted_private_key : EncryptedPrivateKeyInfo < Data > ) -> Result < SecretDocument > {
149
164
SecretDocument :: try_from ( & encrypted_private_key)
150
165
}
151
166
}
152
167
153
168
#[ cfg( feature = "alloc" ) ]
154
- impl TryFrom < & EncryptedPrivateKeyInfo < ' _ > > for SecretDocument {
169
+ impl < ' a , Data > TryFrom < & EncryptedPrivateKeyInfo < Data > > for SecretDocument
170
+ where
171
+ Data : AsRef < [ u8 ] > + From < & ' a [ u8 ] > ,
172
+ {
155
173
type Error = Error ;
156
174
157
- fn try_from ( encrypted_private_key : & EncryptedPrivateKeyInfo < ' _ > ) -> Result < SecretDocument > {
175
+ fn try_from ( encrypted_private_key : & EncryptedPrivateKeyInfo < Data > ) -> Result < SecretDocument > {
158
176
Ok ( Self :: encode_msg ( encrypted_private_key) ?)
159
177
}
160
178
}
161
179
162
180
#[ cfg( feature = "pem" ) ]
163
- impl PemLabel for EncryptedPrivateKeyInfo < ' _ > {
181
+ impl < Data > PemLabel for EncryptedPrivateKeyInfo < Data > {
164
182
const PEM_LABEL : & ' static str = "ENCRYPTED PRIVATE KEY" ;
165
183
}
184
+
185
+ /// [`EncryptedPrivateKeyInfo`] with `&[u8]` encrypted data.
186
+ pub type EncryptedPrivateKeyInfoRef < ' a > = EncryptedPrivateKeyInfo < & ' a [ u8 ] > ;
187
+
188
+ #[ cfg( feature = "alloc" ) ]
189
+ /// [`EncryptedPrivateKeyInfo`] with `Box<[u8]>` encrypted data.
190
+ pub type EncryptedPrivateKeyInfoOwned = EncryptedPrivateKeyInfo < Box < [ u8 ] > > ;
0 commit comments