22// 2.0, and the BSD License. See the LICENSE file in the root of this repository
33// for complete details.
44
5- use cryptography_x509:: common:: { AlgorithmIdentifier , AlgorithmParameters , Pkcs12PbeParams } ;
5+ use cryptography_x509:: common:: {
6+ AlgorithmIdentifier , AlgorithmParameters , PbeParams , Pkcs12PbeParams ,
7+ } ;
68use cryptography_x509:: csr:: Attributes ;
79use cryptography_x509:: pkcs8:: EncryptedPrivateKeyInfo ;
810
@@ -153,6 +155,31 @@ fn pkcs12_pbe_decrypt(
153155 . map_err ( |_| KeyParsingError :: IncorrectPassword )
154156}
155157
158+ fn pkcs5_pbe_decrypt (
159+ data : & [ u8 ] ,
160+ password : & [ u8 ] ,
161+ cipher : openssl:: symm:: Cipher ,
162+ hash : openssl:: hash:: MessageDigest ,
163+ params : & PbeParams ,
164+ ) -> KeyParsingResult < Vec < u8 > > {
165+ // PKCS#5 v1.5 uses PBKDF1 with iteration count
166+ // For PKCS#5 PBE, we need key + IV length
167+ let key_iv_len = cipher. key_len ( ) + cipher. iv_len ( ) . unwrap ( ) ;
168+ let key_iv = cryptography_crypto:: pbkdf1:: pbkdf1 (
169+ hash,
170+ password,
171+ params. salt ,
172+ params. iterations ,
173+ key_iv_len,
174+ ) ?;
175+
176+ let key = & key_iv[ ..cipher. key_len ( ) ] ;
177+ let iv = & key_iv[ cipher. key_len ( ) ..] ;
178+
179+ openssl:: symm:: decrypt ( cipher, key, Some ( iv) , data)
180+ . map_err ( |_| KeyParsingError :: IncorrectPassword )
181+ }
182+
156183pub fn parse_encrypted_private_key (
157184 data : & [ u8 ] ,
158185 password : Option < & [ u8 ] > ,
@@ -164,6 +191,13 @@ pub fn parse_encrypted_private_key(
164191 } ;
165192
166193 let plaintext = match epki. encryption_algorithm . params {
194+ AlgorithmParameters :: PbeWithMd5AndDesCbc ( params) => pkcs5_pbe_decrypt (
195+ epki. encrypted_data ,
196+ password,
197+ openssl:: symm:: Cipher :: des_cbc ( ) ,
198+ openssl:: hash:: MessageDigest :: md5 ( ) ,
199+ & params,
200+ ) ?,
167201 AlgorithmParameters :: PbeWithShaAnd3KeyTripleDesCbc ( params) => pkcs12_pbe_decrypt (
168202 epki. encrypted_data ,
169203 password,
0 commit comments