Skip to content

Commit 1dbc5e0

Browse files
authored
fixes #13056 -- added support for SHA+RC4 PKCS#8 encryption (#13057)
1 parent 7e83196 commit 1dbc5e0

File tree

7 files changed

+37
-2
lines changed

7 files changed

+37
-2
lines changed

docs/development/test-vectors.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ Asymmetric ciphers
6767
* ``asymmetric/PKCS8/rsa-rc2-cbc-effective-key-length.pem`` a PKCS8 encoded key
6868
encrypted with ``RC2-CBC`` with the ``effectiveKeyLength`` parameter set to
6969
258. This is an invalid key.
70+
* ``asymmetric/PKCS8/enc-ec-sha1-128-rc4.pem`` a PKCS8 encoded ECDSA P-256 key
71+
encrypted with ``pbeWithSHA1And128BitRC4``. The password is ``password``.
7072

7173

7274
Custom asymmetric vectors

src/rust/cryptography-key-parsing/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ cryptography-crypto = { path = "../cryptography-crypto" }
1616
cryptography-x509 = { path = "../cryptography-x509" }
1717

1818
[lints.rust]
19-
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(CRYPTOGRAPHY_IS_LIBRESSL)', 'cfg(CRYPTOGRAPHY_IS_BORINGSSL)', 'cfg(CRYPTOGRAPHY_OSSLCONF, values("OPENSSL_NO_RC2"))', 'cfg(CRYPTOGRAPHY_IS_AWSLC)'] }
19+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(CRYPTOGRAPHY_IS_LIBRESSL)', 'cfg(CRYPTOGRAPHY_IS_BORINGSSL)', 'cfg(CRYPTOGRAPHY_OSSLCONF, values("OPENSSL_NO_RC2", "OPENSSL_NO_RC4"))', 'cfg(CRYPTOGRAPHY_IS_AWSLC)'] }

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ pub fn parse_encrypted_private_key(
213213
openssl::hash::MessageDigest::sha1(),
214214
&params,
215215
)?,
216+
#[cfg(not(CRYPTOGRAPHY_OSSLCONF = "OPENSSL_NO_RC4"))]
217+
AlgorithmParameters::PbeWithShaAnd128BitRc4(params) => pkcs12_pbe_decrypt(
218+
epki.encrypted_data,
219+
password,
220+
openssl::symm::Cipher::rc4(),
221+
openssl::hash::MessageDigest::sha1(),
222+
&params,
223+
)?,
216224
AlgorithmParameters::Pbes2(params) => {
217225
let (cipher, iv) = match params.encryption_scheme.params {
218226
AlgorithmParameters::DesEde3Cbc(ref iv) => {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ pub enum AlgorithmParameters<'a> {
167167

168168
#[defined_by(oid::PBE_WITH_MD5_AND_DES_CBC)]
169169
PbeWithMd5AndDesCbc(PbeParams),
170+
#[defined_by(oid::PBE_WITH_SHA_AND_128_BIT_RC4)]
171+
PbeWithShaAnd128BitRc4(Pkcs12PbeParams<'a>),
170172
#[defined_by(oid::PBE_WITH_SHA_AND_3KEY_TRIPLEDES_CBC)]
171173
PbeWithShaAnd3KeyTripleDesCbc(Pkcs12PbeParams<'a>),
172174
#[defined_by(oid::PBE_WITH_SHA_AND_40_BIT_RC2_CBC)]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ pub const PBKDF2_OID: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1,
155155
pub const PBE_WITH_MD5_AND_DES_CBC: asn1::ObjectIdentifier = asn1::oid!(1, 2, 840, 113549, 1, 5, 3);
156156
pub const SCRYPT_OID: asn1::ObjectIdentifier = asn1::oid!(1, 3, 6, 1, 4, 1, 11591, 4, 11);
157157

158+
pub const PBE_WITH_SHA_AND_128_BIT_RC4: asn1::ObjectIdentifier =
159+
asn1::oid!(1, 2, 840, 113549, 1, 12, 1, 1);
158160
pub const PBE_WITH_SHA_AND_3KEY_TRIPLEDES_CBC: asn1::ObjectIdentifier =
159161
asn1::oid!(1, 2, 840, 113549, 1, 12, 1, 3);
160162
pub const PBE_WITH_SHA_AND_40_BIT_RC2_CBC: asn1::ObjectIdentifier =

tests/hazmat/primitives/test_serialization.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pytest
1212

1313
from cryptography.hazmat.bindings._rust import openssl as rust_openssl
14-
from cryptography.hazmat.decrepit.ciphers.algorithms import _DES, RC2
14+
from cryptography.hazmat.decrepit.ciphers.algorithms import _DES, ARC4, RC2
1515
from cryptography.hazmat.primitives.asymmetric import (
1616
dsa,
1717
ec,
@@ -552,6 +552,21 @@ def test_load_pkcs8_rc2_cbc_effective_key_length(self):
552552
with pytest.raises(ValueError):
553553
load_pem_private_key(data, password=b"password")
554554

555+
@pytest.mark.supported(
556+
only_if=lambda backend: backend.cipher_supported(
557+
ARC4(b"\x00" * 16), None
558+
),
559+
skip_message="Does not support RC4",
560+
)
561+
def test_load_pkcs8_rc4_sha1_128bit(self):
562+
key = load_vectors_from_file(
563+
os.path.join("asymmetric", "PKCS8", "enc-ec-sha1-128-rc4.pem"),
564+
lambda f: load_pem_private_key(f.read(), password=b"password"),
565+
mode="rb",
566+
)
567+
assert isinstance(key, ec.EllipticCurvePrivateKey)
568+
assert isinstance(key.curve, ec.SECP256R1)
569+
555570
def test_load_pkcs8_aes_192_cbc(self):
556571
key = load_vectors_from_file(
557572
os.path.join("asymmetric", "PKCS8", "rsa-aes-192-cbc.pem"),
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-----BEGIN ENCRYPTED PRIVATE KEY-----
2+
MIGrMBwGCiqGSIb3DQEMAQEwDgQIprjt98myskECAggABIGKdJJyNgMqiLL0EWI3
3+
ZVto6g9msWT2ovpySiGxZyoUDfFrqfBuHY4IqwL/PYr9La1u/F/VuP5DRLf47YWp
4+
iwfxc6sYedBU85f0c14Ha2Yc6hUEakCbQEzCqEg8RmJ2oDETbTO9STlMyk9ou8XV
5+
7hdRkBqKNj3RIdgf01Aj5t8YmYsrKTx9VUDBpij0
6+
-----END ENCRYPTED PRIVATE KEY-----

0 commit comments

Comments
 (0)