Skip to content

Commit 0dea78d

Browse files
committed
fix: removed test_support pkcs7_encrypt
added vector for aes_256_cbc encrypted pkcs7
1 parent f5fa888 commit 0dea78d

File tree

4 files changed

+30
-77
lines changed

4 files changed

+30
-77
lines changed

src/cryptography/hazmat/bindings/_rust/test_support.pyi

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ class TestCertificate:
1313
subject_value_tags: list[int]
1414

1515
def test_parse_certificate(data: bytes) -> TestCertificate: ...
16-
def pkcs7_encrypt(
17-
cert_recipients: list[x509.Certificate],
18-
msg: bytes,
19-
cipher: bytes,
20-
options: list[pkcs7.PKCS7Options],
21-
encoding: serialization.Encoding,
22-
) -> bytes: ...
2316
def pkcs7_decrypt(
2417
encoding: serialization.Encoding,
2518
msg: bytes,

src/rust/src/test_support.rs

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -103,57 +103,6 @@ fn pkcs7_verify(
103103
Ok(())
104104
}
105105

106-
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
107-
#[pyo3::pyfunction]
108-
#[pyo3(signature = (cert_recipients, msg, cipher, options, encoding))]
109-
fn pkcs7_encrypt<'p>(
110-
py: pyo3::Python<'p>,
111-
cert_recipients: Vec<pyo3::Bound<'p, PyCertificate>>,
112-
msg: CffiBuf<'p>,
113-
cipher: CffiBuf<'p>,
114-
options: pyo3::Bound<'p, pyo3::types::PyList>,
115-
encoding: pyo3::Bound<'p, pyo3::PyAny>,
116-
) -> CryptographyResult<pyo3::Bound<'p, pyo3::types::PyBytes>> {
117-
// Prepare the certificates
118-
let mut certs_stack = openssl::stack::Stack::new()?;
119-
for cert in &cert_recipients {
120-
let der = asn1::write_single(cert.get().raw.borrow_dependent())?;
121-
certs_stack.push(openssl::x509::X509::from_der(&der)?)?;
122-
}
123-
124-
// Prepare the cipher
125-
// SAFETY: No pre-conditions
126-
let cipher = unsafe {
127-
let ptr = openssl_sys::EVP_get_cipherbyname(cipher.as_bytes().as_ptr() as *const _);
128-
openssl::symm::Cipher::from_ptr(ptr as *mut _)
129-
};
130-
131-
// Prepare the options
132-
let mut flags = openssl::pkcs7::Pkcs7Flags::empty();
133-
if options.contains(types::PKCS7_TEXT.get(py)?)? {
134-
flags |= openssl::pkcs7::Pkcs7Flags::TEXT;
135-
}
136-
if options.contains(types::PKCS7_BINARY.get(py)?)? {
137-
flags |= openssl::pkcs7::Pkcs7Flags::BINARY;
138-
}
139-
140-
// Encrypt the message
141-
let p7 = openssl::pkcs7::Pkcs7::encrypt(&certs_stack, msg.as_bytes(), cipher, flags).unwrap();
142-
143-
// Return the result in the correct format
144-
if encoding.is(&types::ENCODING_DER.get(py)?) {
145-
Ok(pyo3::types::PyBytes::new_bound(py, &p7.to_der().unwrap()))
146-
} else if encoding.is(&types::ENCODING_PEM.get(py)?) {
147-
Ok(pyo3::types::PyBytes::new_bound(py, &p7.to_pem().unwrap()))
148-
} else {
149-
Ok(pyo3::types::PyBytes::new_bound(
150-
py,
151-
&p7.to_smime(&[], openssl::pkcs7::Pkcs7Flags::empty())
152-
.unwrap(),
153-
))
154-
}
155-
}
156-
157106
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
158107
#[pyo3::pyfunction]
159108
#[pyo3(signature = (encoding, msg, pkey, cert_recipient, options))]
@@ -205,9 +154,6 @@ pub(crate) mod test_support {
205154
use super::pkcs7_decrypt;
206155
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
207156
#[pymodule_export]
208-
use super::pkcs7_encrypt;
209-
#[cfg(not(CRYPTOGRAPHY_IS_BORINGSSL))]
210-
#[pymodule_export]
211157
use super::pkcs7_verify;
212158
#[pymodule_export]
213159
use super::test_parse_certificate;

tests/hazmat/primitives/test_pkcs7.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,15 @@ def _load_another_rsa_cert_key():
867867
return cert, key
868868

869869

870+
def _load_aes_256_cbc_pkcs7_der():
871+
enveloped = load_vectors_from_file(
872+
os.path.join("pkcs7", "aes_256_cbc.der"),
873+
loader=lambda pemfile: pemfile.read(),
874+
mode="rb",
875+
)
876+
return enveloped
877+
878+
870879
@pytest.mark.supported(
871880
only_if=lambda backend: backend.pkcs7_supported()
872881
and backend.rsa_encryption_supported(padding.PKCS1v15()),
@@ -1147,10 +1156,12 @@ def test_pkcs7_decrypt_der(
11471156
self, backend, data, certificate, private_key, options
11481157
):
11491158
# Encryption
1150-
encoding = serialization.Encoding.DER
1151-
enveloped = test_support.pkcs7_encrypt(
1152-
[certificate], data, b"aes-128-cbc", options, encoding
1159+
builder = (
1160+
pkcs7.PKCS7EnvelopeBuilder()
1161+
.set_data(data)
1162+
.add_recipient(certificate)
11531163
)
1164+
enveloped = builder.encrypt(serialization.Encoding.DER, options)
11541165

11551166
# Test decryption
11561167
decrypted = pkcs7.pkcs7_decrypt_der(
@@ -1165,10 +1176,12 @@ def test_pkcs7_decrypt_pem(
11651176
self, backend, data, certificate, private_key, options
11661177
):
11671178
# Encryption
1168-
encoding = serialization.Encoding.PEM
1169-
enveloped = test_support.pkcs7_encrypt(
1170-
[certificate], data, b"aes-128-cbc", options, encoding
1179+
builder = (
1180+
pkcs7.PKCS7EnvelopeBuilder()
1181+
.set_data(data)
1182+
.add_recipient(certificate)
11711183
)
1184+
enveloped = builder.encrypt(serialization.Encoding.PEM, options)
11721185

11731186
# Test decryption
11741187
decrypted = pkcs7.pkcs7_decrypt_pem(
@@ -1183,10 +1196,12 @@ def test_pkcs7_decrypt_smime(
11831196
self, backend, data, certificate, private_key, options
11841197
):
11851198
# Encryption
1186-
encoding = serialization.Encoding.SMIME
1187-
enveloped = test_support.pkcs7_encrypt(
1188-
[certificate], data, b"aes-128-cbc", options, encoding
1199+
builder = (
1200+
pkcs7.PKCS7EnvelopeBuilder()
1201+
.set_data(data)
1202+
.add_recipient(certificate)
11891203
)
1204+
enveloped = builder.encrypt(serialization.Encoding.SMIME, options)
11901205

11911206
# Test decryption
11921207
decrypted = pkcs7.pkcs7_decrypt_smime(
@@ -1198,10 +1213,12 @@ def test_smime_decrypt_no_recipient_match(
11981213
self, backend, data, certificate
11991214
):
12001215
# Encrypt some data with one RSA chain
1201-
encoding = serialization.Encoding.DER
1202-
enveloped = test_support.pkcs7_encrypt(
1203-
[certificate], data, b"aes-128-cbc", [], encoding
1216+
builder = (
1217+
pkcs7.PKCS7EnvelopeBuilder()
1218+
.set_data(data)
1219+
.add_recipient(certificate)
12041220
)
1221+
enveloped = builder.encrypt(serialization.Encoding.DER, [])
12051222

12061223
# Test decryption with another RSA chain
12071224
another_cert, another_private_key = _load_another_rsa_cert_key()
@@ -1213,10 +1230,7 @@ def test_smime_decrypt_no_recipient_match(
12131230
def test_smime_decrypt_algorithm_not_supported(
12141231
self, backend, data, certificate, private_key
12151232
):
1216-
encoding = serialization.Encoding.DER
1217-
enveloped = test_support.pkcs7_encrypt(
1218-
[certificate], data, b"aes-256-cbc", [], encoding
1219-
)
1233+
enveloped = _load_aes_256_cbc_pkcs7_der()
12201234

12211235
with pytest.raises(exceptions.UnsupportedAlgorithm):
12221236
pkcs7.pkcs7_decrypt_der(enveloped, certificate, private_key, [])
671 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)