Skip to content

Commit

Permalink
cleanups and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Jan 31, 2025
1 parent 2d65a27 commit 4134f4b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/rust/cryptography-crypto/src/pbkdf1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub fn openssl_kdf(
) -> Result<Vec<u8>, openssl::error::ErrorStack> {
let mut key = Vec::with_capacity(length);

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

if !key.is_empty() {
Expand Down
12 changes: 7 additions & 5 deletions src/rust/cryptography-key-parsing/src/pkcs8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ pub fn parse_encrypted_private_key(

let plaintext = match epki.encryption_algorithm.params {
AlgorithmParameters::Pbes1WithShaAnd3KeyTripleDesCbc(params) => {
// XXX:
// - handle invalid utf8
let password = std::str::from_utf8(password).unwrap();
let Ok(password) = std::str::from_utf8(password) else {
return Err(KeyParsingError::IncorrectPassword);
};
let key = cryptography_crypto::pkcs12::kdf(
password,
&params.salt,
Expand Down Expand Up @@ -191,8 +191,10 @@ pub fn parse_encrypted_private_key(
openssl::pkcs5::pbkdf2_hmac(
password,
pbkdf2_params.salt,
// XXX
pbkdf2_params.iteration_count.try_into().expect("XXX"),
pbkdf2_params
.iteration_count
.try_into()
.map_err(|_| KeyParsingError::InvalidKey)?,
md,
&mut key,
)
Expand Down
9 changes: 8 additions & 1 deletion src/rust/src/backend/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,14 @@ fn load_pem_private_key<'p>(
let key = cryptography_crypto::pbkdf1::openssl_kdf(
openssl::hash::MessageDigest::md5(),
password,
&iv,
iv.get(..8)
.ok_or_else(|| {
pyo3::exceptions::PyValueError::new_err(
"DEK-Info IV must be at least 8 bytes",
)
})?
.try_into()
.unwrap(),
cipher.key_len(),
)
.map_err(|_| {
Expand Down

0 comments on commit 4134f4b

Please sign in to comment.