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