File tree Expand file tree Collapse file tree 3 files changed +16
-6
lines changed
cryptography-key-parsing/src Expand file tree Collapse file tree 3 files changed +16
-6
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ pub fn openssl_kdf(
12
12
) -> Result < Vec < u8 > , openssl:: error:: ErrorStack > {
13
13
let mut key = Vec :: with_capacity ( length) ;
14
14
15
+ while key. len ( ) < length {
15
16
let mut h = openssl:: hash:: Hasher :: new ( hash_alg) ?;
16
17
17
18
if !key. is_empty ( ) {
Original file line number Diff line number Diff line change @@ -126,9 +126,9 @@ pub fn parse_encrypted_private_key(
126
126
127
127
let plaintext = match epki. encryption_algorithm . params {
128
128
AlgorithmParameters :: Pbes1WithShaAnd3KeyTripleDesCbc ( params) => {
129
- // XXX:
130
- // - handle invalid utf8
131
- let password = std :: str :: from_utf8 ( password ) . unwrap ( ) ;
129
+ let Ok ( password ) = std :: str :: from_utf8 ( password ) else {
130
+ return Err ( KeyParsingError :: IncorrectPassword ) ;
131
+ } ;
132
132
let key = cryptography_crypto:: pkcs12:: kdf (
133
133
password,
134
134
& params. salt ,
@@ -191,8 +191,10 @@ pub fn parse_encrypted_private_key(
191
191
openssl:: pkcs5:: pbkdf2_hmac (
192
192
password,
193
193
pbkdf2_params. salt ,
194
- // XXX
195
- pbkdf2_params. iteration_count . try_into ( ) . expect ( "XXX" ) ,
194
+ pbkdf2_params
195
+ . iteration_count
196
+ . try_into ( )
197
+ . map_err ( |_| KeyParsingError :: InvalidKey ) ?,
196
198
md,
197
199
& mut key,
198
200
)
Original file line number Diff line number Diff line change @@ -129,7 +129,14 @@ fn load_pem_private_key<'p>(
129
129
let key = cryptography_crypto:: pbkdf1:: openssl_kdf (
130
130
openssl:: hash:: MessageDigest :: md5 ( ) ,
131
131
password,
132
- & iv,
132
+ iv. get ( ..8 )
133
+ . ok_or_else ( || {
134
+ pyo3:: exceptions:: PyValueError :: new_err (
135
+ "DEK-Info IV must be at least 8 bytes" ,
136
+ )
137
+ } ) ?
138
+ . try_into ( )
139
+ . unwrap ( ) ,
133
140
cipher. key_len ( ) ,
134
141
)
135
142
. map_err ( |_| {
You can’t perform that action at this time.
0 commit comments