Skip to content

Commit

Permalink
Add fuzz/pkcs7_decrypt.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth committed Dec 13, 2024
1 parent 850af98 commit 133dc5c
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 4 deletions.
16 changes: 12 additions & 4 deletions crypto/pkcs7/pkcs7.c
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,10 @@ static int pkcs7_decrypt_rinfo(unsigned char **ek_out, PKCS7_RECIP_INFO *ri,
// we still need to set |ek| to NULL to signal decryption failure to callers
// so they can use random bytes as content encryption key for MMA defense.
if (!ok) {
#if !defined(BORINGSSL_UNSAFE_FUZZER_MODE)
OPENSSL_free(ek);
ek = NULL;
#endif
}

ret = 1;
Expand Down Expand Up @@ -1250,7 +1252,9 @@ static BIO *pkcs7_data_decode(PKCS7 *p7, EVP_PKEY *pkey, X509 *pcert) {
if (!pkcs7_cmp_ri(ri, pcert)) {
break;
}
#if !defined(BORINGSSL_UNSAFE_FUZZER_MODE)
ri = NULL;
#endif
}
if (ri == NULL) {
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
Expand Down Expand Up @@ -1290,14 +1294,16 @@ static BIO *pkcs7_data_decode(PKCS7 *p7, EVP_PKEY *pkey, X509 *pcert) {
!EVP_CipherInit_ex(evp_ctx, cipher, NULL, NULL, NULL, 0)) {
goto err;
}
uint8_t iv[EVP_MAX_IV_LENGTH];
OPENSSL_memcpy(iv, enc_alg->parameter->value.octet_string->data,
enc_alg->parameter->value.octet_string->length);
const int expected_iv_len = EVP_CIPHER_CTX_iv_length(evp_ctx);
if (enc_alg->parameter->value.octet_string->length != expected_iv_len) {
if (enc_alg == NULL || enc_alg->parameter == NULL ||
enc_alg->parameter->value.octet_string == NULL ||
enc_alg->parameter->value.octet_string->length != expected_iv_len) {
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PKCS7_LIB);
goto err;
}
uint8_t iv[EVP_MAX_IV_LENGTH];
OPENSSL_memcpy(iv, enc_alg->parameter->value.octet_string->data,
enc_alg->parameter->value.octet_string->length);
if (!EVP_CipherInit_ex(evp_ctx, NULL, NULL, NULL, iv, 0)) {
goto err;
}
Expand All @@ -1323,6 +1329,8 @@ static BIO *pkcs7_data_decode(PKCS7 *p7, EVP_PKEY *pkey, X509 *pcert) {

OPENSSL_free(cek);
OPENSSL_free(dummy_key);
cek = NULL;
dummy_key = NULL;
out = cipher_bio;

if (data_body && data_body->length > 0) {
Expand Down
1 change: 1 addition & 0 deletions fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fuzzer(ocsp)
fuzzer(ocsp_http)
fuzzer(ocsp_parse_url)
fuzzer(pkcs12)
fuzzer(pkcs7_decrypt)
fuzzer(pkcs8)
fuzzer(pkcs8_v2)
fuzzer(privkey)
Expand Down
99 changes: 99 additions & 0 deletions fuzz/pkcs7_decrypt.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0 OR ISC

#include <cstdint>

#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pkcs7.h>
#include <openssl/rsa.h>
#include <openssl/x509.h>
#include <openssl/pem.h>

// The corpus was created using the following key.
// If you change the key, the corpus should be augmented with inputs
// created using (or "seeded" from) PKCS7 values encrypted with
// the new key.
static const char kKey[] = R"(
-----BEGIN PRIVATE KEY-----
MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQC1+MOn+BopcEVR
4QMvjXdAxGkWFllXyQFDToL+qOiPRU1yN7C8KCtkbOAFttJIO4O/i0iZ7KqYbnmB
6YUA/ONAcakocnrdoESgRJcVMeAxDk/11OtMF5yIfeOOO/TUeVNmAUaT63gFbKy/
adpqhzJtOv9BBl5VcYNGGSE+0wtbmjpmNsxunEQR1KLDc97fGYHeRfKoSyrCIEE8
IaAEpKGR2Sku3v9J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Fal7F9uJk9m3JWMOhn3OvudguoPX0ZWEtgll+iP4axDSAFd2DWcXn
tCxzStdQjgHdZOxrL4FNW06xGxm6Nvi4zyuySfsCggEAOuIpC3ATBxRyZYMm/FGZ
tEquyV2omz8FQA1nJFzu7MMCHHPcdzSVH4Pl3GGloQi1gW51H8GuMDxZ/H2NcDWY
WuG49u1GFdKjinRXFKztnKBjNzHEVWRYfOSRuMh8N6SNKbYPnWlNos1k0IypFSGT
pe5uhnF58gK8wgD67bkLce43B6NEWSb+tSMx2qFE8SfqAQSoD6zv//NjA4OrKJNS
1RVFS279vpqMdib/qk+nFn3G2i0Dr1NEcpihHgCyAZff2Hze6pyjeQr+RrNE74VY
MudNiiG8lV2t2+tClZ6ULoaPvpIvAP04+WiYav+uOX0VxwO8tXgqWSQOCzNNxlr7
IwKCAQA7odNjE6Sc2qiecrOu13kEi3gT0heshIyZ0XhePrS1vgHfCouIRvNMw4FT
45ZZUFDSdOxhrew5GuMeLvo2YILBjmkX3UqTojQMbur7FcGH8/P0Sm0f20Vc06oS
sQF5Ji4LSyf6t9oQKePjFIGoIc6pf6BXJZYP4rBnzQzUQjH2yzDYDY3TuV7bFJJU
DcSTGM6nP0fRMmgBtB14o7A6Gsy6X/N2ElgbvWT8YhmUC6H8DIzmZwHRKaG6C6g5
eEjuAYenYNM4jxeteC1neUDIdGxH/BA7JrAqcGaN9GT+R47YIfiS2WrEssD1Pi5h
hJTbHtjEDJ7BHLC/CNUhXbpyyu1y
-----END PRIVATE KEY-----
)";

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
PKCS7 *pkcs7 = nullptr;
EVP_PKEY *key = nullptr;

pkcs7 = d2i_PKCS7(nullptr, &buf, len);
if (pkcs7 == nullptr) {
goto end;
}

{
BIO *key_bio = BIO_new_mem_buf(const_cast<char *>(kKey), sizeof(kKey) - 1);
key = PEM_read_bio_PrivateKey(key_bio, nullptr, nullptr, nullptr);
BIO_free(key_bio);
}
{
BIO *data_bio = BIO_new(BIO_s_mem());
OPENSSL_BEGIN_ALLOW_DEPRECATED
PKCS7_decrypt(pkcs7, key, NULL, data_bio, 0);
OPENSSL_END_ALLOW_DEPRECATED
BIO_free(data_bio);
}

end:
EVP_PKEY_free(key);
PKCS7_free(pkcs7);

return 0;
}

0 comments on commit 133dc5c

Please sign in to comment.