Skip to content

Commit 4134f4b

Browse files
committed
cleanups and fixes
1 parent 2d65a27 commit 4134f4b

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/rust/cryptography-crypto/src/pbkdf1.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub fn openssl_kdf(
1212
) -> Result<Vec<u8>, openssl::error::ErrorStack> {
1313
let mut key = Vec::with_capacity(length);
1414

15+
while key.len() < length {
1516
let mut h = openssl::hash::Hasher::new(hash_alg)?;
1617

1718
if !key.is_empty() {

src/rust/cryptography-key-parsing/src/pkcs8.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ pub fn parse_encrypted_private_key(
126126

127127
let plaintext = match epki.encryption_algorithm.params {
128128
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+
};
132132
let key = cryptography_crypto::pkcs12::kdf(
133133
password,
134134
&params.salt,
@@ -191,8 +191,10 @@ pub fn parse_encrypted_private_key(
191191
openssl::pkcs5::pbkdf2_hmac(
192192
password,
193193
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)?,
196198
md,
197199
&mut key,
198200
)

src/rust/src/backend/keys.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,14 @@ fn load_pem_private_key<'p>(
129129
let key = cryptography_crypto::pbkdf1::openssl_kdf(
130130
openssl::hash::MessageDigest::md5(),
131131
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(),
133140
cipher.key_len(),
134141
)
135142
.map_err(|_| {

0 commit comments

Comments
 (0)