Skip to content

Commit 2ceed71

Browse files
authored
pkcs5: don't panic on a short DES/3DES IV in PBES2 params (#2383)
The DES-CBC and 3DES-EDE3-CBC arms of the EncryptionScheme parser slice iv[0..DES_BLOCK_SIZE] before the try_into length check, so an AlgorithmIdentifier carrying one of those OIDs with an IV octet string shorter than 8 bytes panics with a slice range error instead of returning an error. The AES arms already run try_into on the whole slice, which length-checks gracefully; mirror that in the DES arms. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
1 parent e330a32 commit 2ceed71

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

pkcs5/src/pbes2.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,15 +543,11 @@ impl TryFrom<AlgorithmIdentifierRef<'_>> for EncryptionScheme {
543543
}),
544544
#[cfg(feature = "des-insecure")]
545545
DES_CBC_OID => Ok(Self::DesCbc {
546-
iv: iv[0..DES_BLOCK_SIZE]
547-
.try_into()
548-
.map_err(|_| Tag::OctetString.value_error())?,
546+
iv: iv.try_into().map_err(|_| Tag::OctetString.value_error())?,
549547
}),
550548
#[cfg(feature = "3des")]
551549
DES_EDE3_CBC_OID => Ok(Self::DesEde3Cbc {
552-
iv: iv[0..DES_BLOCK_SIZE]
553-
.try_into()
554-
.map_err(|_| Tag::OctetString.value_error())?,
550+
iv: iv.try_into().map_err(|_| Tag::OctetString.value_error())?,
555551
}),
556552
oid => Err(ErrorKind::OidUnknown { oid }.into()),
557553
}

pkcs5/tests/pbes2.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,47 @@ fn decode_pbes2_pbkdf2_sha256_descbc() {
194194
}
195195
}
196196

197+
/// A bare DES-EDE3-CBC `AlgorithmIdentifier` whose IV octet string is only 3
198+
/// bytes long (shorter than the 8-byte DES block). This must decode to an
199+
/// error rather than panicking.
200+
#[cfg(feature = "3des")]
201+
const DESEDE3CBC_SHORT_IV_ALG_ID: &[u8] = &hex!("300f06082a864886f70d03070403010203");
202+
203+
/// A bare DES-CBC `AlgorithmIdentifier` with a 3-byte IV octet string.
204+
#[cfg(feature = "des-insecure")]
205+
const DESCBC_SHORT_IV_ALG_ID: &[u8] = &hex!("300c06052b0e0302070403010203");
206+
207+
/// A well-formed DES-EDE3-CBC `AlgorithmIdentifier` with a full 8-byte IV.
208+
#[cfg(feature = "3des")]
209+
const DESEDE3CBC_VALID_IV_ALG_ID: &[u8] = &hex!("301406082a864886f70d030704080102030405060708");
210+
211+
#[cfg(feature = "3des")]
212+
#[test]
213+
fn desede3cbc_short_iv_is_error_not_panic() {
214+
use der::Decode;
215+
assert!(pbes2::EncryptionScheme::from_der(DESEDE3CBC_SHORT_IV_ALG_ID).is_err());
216+
}
217+
218+
#[cfg(feature = "des-insecure")]
219+
#[test]
220+
fn descbc_short_iv_is_error_not_panic() {
221+
use der::Decode;
222+
assert!(pbes2::EncryptionScheme::from_der(DESCBC_SHORT_IV_ALG_ID).is_err());
223+
}
224+
225+
#[cfg(feature = "3des")]
226+
#[test]
227+
fn desede3cbc_valid_iv_still_decodes() {
228+
use der::Decode;
229+
let scheme = pbes2::EncryptionScheme::from_der(DESEDE3CBC_VALID_IV_ALG_ID).unwrap();
230+
match scheme {
231+
pbes2::EncryptionScheme::DesEde3Cbc { iv } => {
232+
assert_eq!(iv, hex!("0102030405060708"));
233+
}
234+
other => panic!("unexpected encryption scheme: {other:?}"),
235+
}
236+
}
237+
197238
/// Encoding test for PBES2 + PBKDF2-SHA1 + AES-128-CBC `AlgorithmIdentifier`
198239
#[test]
199240
fn encode_pbes2_pbkdf2_sha1_aes128cbc() {

0 commit comments

Comments
 (0)