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