Skip to content

Commit df48ce0

Browse files
authored
Rename PBES1 types to better match their origins in the spec (#12976)
PBEParams is defined in RFC 8018 as going with PBES1, but then its also used by PKCS#12 ciphers in RFC 7292.
1 parent f1a2597 commit df48ce0

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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, PBES1Params};
5+
use cryptography_x509::common::{AlgorithmIdentifier, AlgorithmParameters, Pkcs12PbeParams};
66
use cryptography_x509::csr::Attributes;
77
use cryptography_x509::pkcs8::EncryptedPrivateKeyInfo;
88

@@ -122,27 +122,27 @@ pub fn parse_private_key(
122122
}
123123
}
124124

125-
fn pbes1_decrypt(
125+
fn pkcs12_pbe_decrypt(
126126
data: &[u8],
127127
password: &[u8],
128128
cipher: openssl::symm::Cipher,
129129
hash: openssl::hash::MessageDigest,
130-
params: &PBES1Params,
130+
params: &Pkcs12PbeParams<'_>,
131131
) -> KeyParsingResult<Vec<u8>> {
132132
let Ok(password) = std::str::from_utf8(password) else {
133133
return Err(KeyParsingError::IncorrectPassword);
134134
};
135135
let key = cryptography_crypto::pkcs12::kdf(
136136
password,
137-
&params.salt,
137+
params.salt,
138138
cryptography_crypto::pkcs12::KDF_ENCRYPTION_KEY_ID,
139139
params.iterations,
140140
cipher.key_len(),
141141
hash,
142142
)?;
143143
let iv = cryptography_crypto::pkcs12::kdf(
144144
password,
145-
&params.salt,
145+
params.salt,
146146
cryptography_crypto::pkcs12::KDF_IV_ID,
147147
params.iterations,
148148
cipher.block_size(),
@@ -164,15 +164,15 @@ pub fn parse_encrypted_private_key(
164164
};
165165

166166
let plaintext = match epki.encryption_algorithm.params {
167-
AlgorithmParameters::Pbes1WithShaAnd3KeyTripleDesCbc(params) => pbes1_decrypt(
167+
AlgorithmParameters::PbeWithShaAnd3KeyTripleDesCbc(params) => pkcs12_pbe_decrypt(
168168
epki.encrypted_data,
169169
password,
170170
openssl::symm::Cipher::des_ede3_cbc(),
171171
openssl::hash::MessageDigest::sha1(),
172172
&params,
173173
)?,
174174
#[cfg(not(CRYPTOGRAPHY_OSSLCONF = "OPENSSL_NO_RC2"))]
175-
AlgorithmParameters::Pbe1WithShaAnd40BitRc2Cbc(params) => pbes1_decrypt(
175+
AlgorithmParameters::PbeWithShaAnd40BitRc2Cbc(params) => pkcs12_pbe_decrypt(
176176
epki.encrypted_data,
177177
password,
178178
openssl::symm::Cipher::rc2_40_cbc(),

src/rust/cryptography-x509/src/common.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ pub enum AlgorithmParameters<'a> {
165165
#[defined_by(oid::RC2_CBC)]
166166
Rc2Cbc(Rc2CbcParams),
167167

168-
#[defined_by(oid::PBES1_WITH_SHA_AND_3KEY_TRIPLEDES_CBC)]
169-
Pbes1WithShaAnd3KeyTripleDesCbc(PBES1Params),
170-
#[defined_by(oid::PBES1_WITH_SHA_AND_40_BIT_RC2_CBC)]
171-
Pbe1WithShaAnd40BitRc2Cbc(PBES1Params),
168+
#[defined_by(oid::PBE_WITH_SHA_AND_3KEY_TRIPLEDES_CBC)]
169+
PbeWithShaAnd3KeyTripleDesCbc(Pkcs12PbeParams<'a>),
170+
#[defined_by(oid::PBE_WITH_SHA_AND_40_BIT_RC2_CBC)]
171+
PbeWithShaAnd40BitRc2Cbc(Pkcs12PbeParams<'a>),
172172

173173
#[default]
174174
Other(asn1::ObjectIdentifier, Option<asn1::Tlv<'a>>),
@@ -529,9 +529,10 @@ pub struct ScryptParams<'a> {
529529
pub key_length: Option<u32>,
530530
}
531531

532+
// From RFC 7202 Appendix C
532533
#[derive(asn1::Asn1Read, asn1::Asn1Write, PartialEq, Eq, Hash, Clone, Debug)]
533-
pub struct PBES1Params {
534-
pub salt: [u8; 8],
534+
pub struct Pkcs12PbeParams<'a> {
535+
pub salt: &'a [u8],
535536
pub iterations: u64,
536537
}
537538

src/rust/cryptography-x509/src/oid.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ pub const PBES2_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1, 5
154154
pub const PBKDF2_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1, 5, 12);
155155
pub const SCRYPT_OID: asn1::ObjectIdentifier = asn1::oid!(1, 3, 6, 1, 4, 1, 11591, 4, 11);
156156

157-
pub const PBES1_WITH_SHA_AND_3KEY_TRIPLEDES_CBC: asn1::ObjectIdentifier =
157+
pub const PBE_WITH_SHA_AND_3KEY_TRIPLEDES_CBC: asn1::ObjectIdentifier =
158158
asn1::oid!(1, 2, 840, 113549, 1, 12, 1, 3);
159-
pub const PBES1_WITH_SHA_AND_40_BIT_RC2_CBC: asn1::ObjectIdentifier =
159+
pub const PBE_WITH_SHA_AND_40_BIT_RC2_CBC: asn1::ObjectIdentifier =
160160
asn1::oid!(1, 2, 840, 113549, 1, 12, 1, 6);
161161

162162
pub const AES_128_CBC_OID: asn1::ObjectIdentifier = asn1::oid!(2, 16, 840, 1, 101, 3, 4, 1, 2);

src/rust/src/pkcs12.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ pub(crate) fn symmetric_encrypt(
112112
}
113113

114114
enum EncryptionAlgorithm {
115-
PBESv1SHA1And3KeyTripleDESCBC,
115+
PBESHA1And3KeyTripleDESCBC,
116116
PBESv2SHA256AndAES256CBC,
117117
}
118118

119119
impl EncryptionAlgorithm {
120120
fn salt_length(&self) -> usize {
121121
match self {
122-
EncryptionAlgorithm::PBESv1SHA1And3KeyTripleDESCBC => 8,
122+
EncryptionAlgorithm::PBESHA1And3KeyTripleDESCBC => 8,
123123
EncryptionAlgorithm::PBESv2SHA256AndAES256CBC => 16,
124124
}
125125
}
@@ -131,11 +131,11 @@ impl EncryptionAlgorithm {
131131
iv: &'a [u8],
132132
) -> cryptography_x509::common::AlgorithmIdentifier<'a> {
133133
match self {
134-
EncryptionAlgorithm::PBESv1SHA1And3KeyTripleDESCBC => {
134+
EncryptionAlgorithm::PBESHA1And3KeyTripleDESCBC => {
135135
cryptography_x509::common::AlgorithmIdentifier {
136136
oid: asn1::DefinedByMarker::marker(),
137-
params: cryptography_x509::common::AlgorithmParameters::Pbes1WithShaAnd3KeyTripleDesCbc(cryptography_x509::common::PBES1Params{
138-
salt: salt[..8].try_into().unwrap(),
137+
params: cryptography_x509::common::AlgorithmParameters::PbeWithShaAnd3KeyTripleDesCbc(cryptography_x509::common::Pkcs12PbeParams{
138+
salt,
139139
iterations: cipher_kdf_iter,
140140
}),
141141
}
@@ -189,7 +189,7 @@ impl EncryptionAlgorithm {
189189
data: &[u8],
190190
) -> CryptographyResult<Vec<u8>> {
191191
match self {
192-
EncryptionAlgorithm::PBESv1SHA1And3KeyTripleDESCBC => {
192+
EncryptionAlgorithm::PBESHA1And3KeyTripleDESCBC => {
193193
let key = cryptography_crypto::pkcs12::kdf(
194194
password,
195195
salt,
@@ -341,7 +341,7 @@ fn decode_encryption_algorithm<'a>(
341341
let key_cert_alg =
342342
encryption_algorithm.getattr(pyo3::intern!(py, "_key_cert_algorithm"))?;
343343
let cipher = if key_cert_alg.is(&types::PBES_PBESV1SHA1AND3KEYTRIPLEDESCBC.get(py)?) {
344-
EncryptionAlgorithm::PBESv1SHA1And3KeyTripleDESCBC
344+
EncryptionAlgorithm::PBESHA1And3KeyTripleDESCBC
345345
} else if key_cert_alg.is(&types::PBES_PBESV2SHA256ANDAES256CBC.get(py)?) {
346346
EncryptionAlgorithm::PBESv2SHA256AndAES256CBC
347347
} else {

0 commit comments

Comments
 (0)