diff --git a/pkcs5/src/pbes2.rs b/pkcs5/src/pbes2.rs index c6660e739..f896be4a6 100644 --- a/pkcs5/src/pbes2.rs +++ b/pkcs5/src/pbes2.rs @@ -543,15 +543,11 @@ impl TryFrom> for EncryptionScheme { }), #[cfg(feature = "des-insecure")] DES_CBC_OID => Ok(Self::DesCbc { - iv: iv[0..DES_BLOCK_SIZE] - .try_into() - .map_err(|_| Tag::OctetString.value_error())?, + iv: iv.try_into().map_err(|_| Tag::OctetString.value_error())?, }), #[cfg(feature = "3des")] DES_EDE3_CBC_OID => Ok(Self::DesEde3Cbc { - iv: iv[0..DES_BLOCK_SIZE] - .try_into() - .map_err(|_| Tag::OctetString.value_error())?, + iv: iv.try_into().map_err(|_| Tag::OctetString.value_error())?, }), oid => Err(ErrorKind::OidUnknown { oid }.into()), } diff --git a/pkcs5/tests/pbes2.rs b/pkcs5/tests/pbes2.rs index 3f6a20ad6..60f68411b 100644 --- a/pkcs5/tests/pbes2.rs +++ b/pkcs5/tests/pbes2.rs @@ -194,6 +194,47 @@ fn decode_pbes2_pbkdf2_sha256_descbc() { } } +/// A bare DES-EDE3-CBC `AlgorithmIdentifier` whose IV octet string is only 3 +/// bytes long (shorter than the 8-byte DES block). This must decode to an +/// error rather than panicking. +#[cfg(feature = "3des")] +const DESEDE3CBC_SHORT_IV_ALG_ID: &[u8] = &hex!("300f06082a864886f70d03070403010203"); + +/// A bare DES-CBC `AlgorithmIdentifier` with a 3-byte IV octet string. +#[cfg(feature = "des-insecure")] +const DESCBC_SHORT_IV_ALG_ID: &[u8] = &hex!("300c06052b0e0302070403010203"); + +/// A well-formed DES-EDE3-CBC `AlgorithmIdentifier` with a full 8-byte IV. +#[cfg(feature = "3des")] +const DESEDE3CBC_VALID_IV_ALG_ID: &[u8] = &hex!("301406082a864886f70d030704080102030405060708"); + +#[cfg(feature = "3des")] +#[test] +fn desede3cbc_short_iv_is_error_not_panic() { + use der::Decode; + assert!(pbes2::EncryptionScheme::from_der(DESEDE3CBC_SHORT_IV_ALG_ID).is_err()); +} + +#[cfg(feature = "des-insecure")] +#[test] +fn descbc_short_iv_is_error_not_panic() { + use der::Decode; + assert!(pbes2::EncryptionScheme::from_der(DESCBC_SHORT_IV_ALG_ID).is_err()); +} + +#[cfg(feature = "3des")] +#[test] +fn desede3cbc_valid_iv_still_decodes() { + use der::Decode; + let scheme = pbes2::EncryptionScheme::from_der(DESEDE3CBC_VALID_IV_ALG_ID).unwrap(); + match scheme { + pbes2::EncryptionScheme::DesEde3Cbc { iv } => { + assert_eq!(iv, hex!("0102030405060708")); + } + other => panic!("unexpected encryption scheme: {other:?}"), + } +} + /// Encoding test for PBES2 + PBKDF2-SHA1 + AES-128-CBC `AlgorithmIdentifier` #[test] fn encode_pbes2_pbkdf2_sha1_aes128cbc() {