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 16, 2024
1 parent 637b5d2 commit 3e89698
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 7 deletions.
22 changes: 15 additions & 7 deletions crypto/pkcs7/pkcs7.c
Original file line number Diff line number Diff line change
Expand Up @@ -1219,7 +1219,7 @@ static BIO *pkcs7_data_decode(PKCS7 *p7, EVP_PKEY *pkey, X509 *pcert) {
goto err;
}

// Detached content must be supplied via in_bio instead
// envelopedData must have data content to decrypt
if (data_body == NULL) {
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_CONTENT);
goto err;
Expand Down Expand Up @@ -1250,7 +1250,10 @@ 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)
// For fuzz testing, we do not want to bail out early.
ri = NULL;
#endif
}
if (ri == NULL) {
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE);
Expand Down Expand Up @@ -1290,14 +1293,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,
expected_iv_len);
if (!EVP_CipherInit_ex(evp_ctx, NULL, NULL, NULL, iv, 0)) {
goto err;
}
Expand All @@ -1323,9 +1328,12 @@ 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) {
// We verify data_body != NULL above
if (data_body->length > 0) {
data_bio = BIO_new_mem_buf(data_body->data, data_body->length);
} else {
data_bio = BIO_new(BIO_s_mem());
Expand Down Expand Up @@ -1360,7 +1368,7 @@ PKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509) {
return ri;
}

int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags) {
int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int _flags) {
GUARD_PTR(p7);
GUARD_PTR(pkey);
GUARD_PTR(data);
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
157 changes: 157 additions & 0 deletions fuzz/pkcs7_decrypt.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// 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
//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/G37I1WjEJwSh6YrthQJrSmfnVxUgXwZqW3Ou5UJpaWwReEMV
xXwznJTwoUwD9pzh/+2QeNYV+6CPmGPD8znhh9QzOoo1mIaUNdDGN7K6dVBBBr7O
Dm67qWXf8AoWLUO5XTiha5V19Jsyxszo2Ntfef9mK4LIszuRbMrpfMlUEvuO3oLa
SLScdPonEPF7CGuyJcyVVJ4T+mqTzmQ9AKzujjXk3sqw5rFbPIC+ei5Fdf0SXsgS
jEX83uZPZRIMjkyiVjDsWwDDumaTJ/KCUhaqGzJ6/ZKLEvKCgtgDQg2QetmRzS5j
c9CC6I6Kfx01cJ6wOlJY22DDjWJAviEP40/TVk1xoEWyiErEiYq3vdCRRoxSR55N
/tjiQM0XbiGfKfx2KrAHFIsRQsqHF0bL4HDjCh29cz3jleWdLGNPwrdfLtPMcXf8
Eu6/5vlz8BUuaR08fjv59tfdedLDRlbpZFERMio6PINFoRuVtbcgVw5xHucHjl3p
shp0eY7uylv5j8j1X7m5fp+BZKILGHLKVrE2ihJgyS9je2Jf40Fb+Hu+1dcsAKd+
jbgtnMeOs3SWELGeAG2TXsKmNOb0OwzGeL5jJpe6tsEUiQQQxhfarBIlxoTPizI=
-----END CERTIFICATE-----
)";

class SharedData {
public:
EVP_PKEY *key = nullptr;
X509 *cert = nullptr;

SharedData() {
{
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 *cert_bio = BIO_new_mem_buf(const_cast<char *>(kCert), sizeof(kCert) - 1);
cert = PEM_read_bio_X509(cert_bio, nullptr, nullptr, nullptr);
BIO_free(cert_bio);
}
}

~SharedData() {
X509_free(cert);
EVP_PKEY_free(key);
}
};

static SharedData sharedData;


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

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

{
BIO *data_bio = BIO_new(BIO_s_mem());
OPENSSL_BEGIN_ALLOW_DEPRECATED
PKCS7_decrypt(pkcs7, sharedData.key, NULL, data_bio, 0);
OPENSSL_END_ALLOW_DEPRECATED
BIO_free(data_bio);
}

{
BIO *data_bio = BIO_new(BIO_s_mem());
OPENSSL_BEGIN_ALLOW_DEPRECATED
PKCS7_decrypt(pkcs7, sharedData.key, sharedData.cert, data_bio, 0);
OPENSSL_END_ALLOW_DEPRECATED
BIO_free(data_bio);
}

end:
PKCS7_free(pkcs7);

return 0;
}

0 comments on commit 3e89698

Please sign in to comment.