diff --git a/crypto/pkcs7/pkcs7.c b/crypto/pkcs7/pkcs7.c index 8232af62d2..305e052a48 100644 --- a/crypto/pkcs7/pkcs7.c +++ b/crypto/pkcs7/pkcs7.c @@ -17,10 +17,13 @@ #include #include #include +#include +#include #include #include #include "internal.h" +#include "../internal.h" #include "../bytestring/internal.h" @@ -191,3 +194,431 @@ int pkcs7_add_signed_data(CBB *out, return CBB_flush(out); } + +int PKCS7_set_type(PKCS7 *p7, int type) { + if (p7 == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } + ASN1_OBJECT *obj = OBJ_nid2obj(type); + if (obj == NULL) { + OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); + return 0; + } + + switch (type) { + case NID_pkcs7_signed: + p7->type = obj; + PKCS7_SIGNED_free(p7->d.sign); + p7->d.sign = PKCS7_SIGNED_new(); + if (p7->d.sign == NULL) { + return 0; + } + if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) { + PKCS7_SIGNED_free(p7->d.sign); + p7->d.sign = NULL; + return 0; + } + break; + case NID_pkcs7_digest: + p7->type = obj; + PKCS7_DIGEST_free(p7->d.digest); + p7->d.digest = PKCS7_DIGEST_new(); + if (p7->d.digest == NULL) { + return 0; + } + if (!ASN1_INTEGER_set(p7->d.digest->version, 0)) { + PKCS7_DIGEST_free(p7->d.digest); + p7->d.digest = NULL; + return 0; + } + break; + case NID_pkcs7_data: + p7->type = obj; + ASN1_OCTET_STRING_free(p7->d.data); + p7->d.data = ASN1_OCTET_STRING_new(); + if (p7->d.data == NULL) { + return 0; + } + break; + case NID_pkcs7_signedAndEnveloped: + p7->type = obj; + PKCS7_SIGN_ENVELOPE_free(p7->d.signed_and_enveloped); + p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new(); + if (p7->d.signed_and_enveloped == NULL) { + return 0; + } + if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1)) { + PKCS7_SIGN_ENVELOPE_free(p7->d.signed_and_enveloped); + p7->d.signed_and_enveloped = NULL; + return 0; + } + p7->d.signed_and_enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data); + break; + case NID_pkcs7_enveloped: + p7->type = obj; + PKCS7_ENVELOPE_free(p7->d.enveloped); + p7->d.enveloped = PKCS7_ENVELOPE_new(); + if (p7->d.enveloped == NULL) { + return 0; + } + if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0)) { + PKCS7_ENVELOPE_free(p7->d.enveloped); + p7->d.enveloped = NULL; + return 0; + } + p7->d.enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data); + break; + case NID_pkcs7_encrypted: + p7->type = obj; + PKCS7_ENCRYPT_free(p7->d.encrypted); + p7->d.encrypted = PKCS7_ENCRYPT_new(); + if (p7->d.encrypted == NULL) { + return 0; + } + if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0)) { + PKCS7_ENCRYPT_free(p7->d.encrypted); + p7->d.encrypted = NULL; + return 0; + } + p7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data); + break; + default: + OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); + return 0; + } + return 1; +} + +int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher) +{ + if (p7 == NULL || cipher == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } + if (EVP_get_cipherbynid(EVP_CIPHER_nid(cipher)) == NULL) { + OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); + return 0; + } + + PKCS7_ENC_CONTENT *ec; + switch (OBJ_obj2nid(p7->type)) { + case NID_pkcs7_signedAndEnveloped: + ec = p7->d.signed_and_enveloped->enc_data; + break; + case NID_pkcs7_enveloped: + ec = p7->d.enveloped->enc_data; + break; + default: + OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); + return 0; + } + + ec->cipher = cipher; + return 1; +} + +int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data) +{ + if (p7 == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } + + switch (OBJ_obj2nid(p7->type)) { + case NID_pkcs7_signed: + PKCS7_free(p7->d.sign->contents); + p7->d.sign->contents = p7_data; + break; + case NID_pkcs7_digest: + PKCS7_free(p7->d.digest->contents); + p7->d.digest->contents = p7_data; + break; + default: + OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); + return 0; + } + return 1; +} + +int PKCS7_content_new(PKCS7 *p7, int type) +{ + PKCS7 *ret = PKCS7_new(); + if (ret == NULL) { + goto err; + } + if (!PKCS7_set_type(ret, type)) { + goto err; + } + if (!PKCS7_set_content(p7, ret)) { + goto err; + } + return 1; + err: + PKCS7_free(ret); + return 0; +} + +int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri) { + STACK_OF(PKCS7_RECIP_INFO) *sk; + + if (p7 == NULL || ri == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } + + switch (OBJ_obj2nid(p7->type)) { + case NID_pkcs7_signedAndEnveloped: + sk = p7->d.signed_and_enveloped->recipientinfo; + break; + case NID_pkcs7_enveloped: + sk = p7->d.enveloped->recipientinfo; + break; + default: + OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); + return 0; + } + + if (!sk_PKCS7_RECIP_INFO_push(sk, ri)) { + return 0; + } + return 1; +} + +int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i) { + ASN1_OBJECT *obj; + X509_ALGOR *alg; + STACK_OF(PKCS7_SIGNER_INFO) *signer_sk; + STACK_OF(X509_ALGOR) *md_sk; + + if (p7 == NULL || p7i == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } + + switch (OBJ_obj2nid(p7->type)) { + case NID_pkcs7_signed: + signer_sk = p7->d.sign->signer_info; + md_sk = p7->d.sign->md_algs; + break; + case NID_pkcs7_signedAndEnveloped: + signer_sk = p7->d.signed_and_enveloped->signer_info; + md_sk = p7->d.signed_and_enveloped->md_algs; + break; + default: + OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); + return 0; + } + + + obj = p7i->digest_alg->algorithm; + /* If the digest is not currently listed, add it */ + int alg_found = 0; + for (size_t i = 0; i < sk_X509_ALGOR_num(md_sk); i++) { + alg = sk_X509_ALGOR_value(md_sk, i); + if (OBJ_cmp(obj, alg->algorithm) == 0) { + alg_found = 1; + break; + } + } + if (!alg_found) { + if ((alg = X509_ALGOR_new()) == NULL + || (alg->parameter = ASN1_TYPE_new()) == NULL) { + X509_ALGOR_free(alg); + OPENSSL_PUT_ERROR(PKCS7, ERR_R_ASN1_LIB); + return 0; + } + /* + * If there is a constant copy of the ASN1 OBJECT in libcrypto, then + * use that. Otherwise, use a dynamically duplicated copy + */ + int nid = OBJ_obj2nid(obj); + if (nid != NID_undef) { + alg->algorithm = OBJ_nid2obj(nid); + } else { + alg->algorithm = OBJ_dup(obj); + } + alg->parameter->type = V_ASN1_NULL; + if (alg->algorithm == NULL || !sk_X509_ALGOR_push(md_sk, alg)) { + X509_ALGOR_free(alg); + return 0; + } + } + + if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, p7i)) { + return 0; + } + return 1; +} + +ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, int nid) { + if (si == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); + return NULL; + } + for (size_t i = 0; i < sk_X509_ATTRIBUTE_num(si->auth_attr); i++) { + X509_ATTRIBUTE *attr = sk_X509_ATTRIBUTE_value(si->auth_attr, i); + ASN1_OBJECT *obj = X509_ATTRIBUTE_get0_object(attr); + if (OBJ_obj2nid(obj) == nid) { + return X509_ATTRIBUTE_get0_type(attr, 0); + } + } + return NULL; +} + +STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7) { + if (p7 == NULL || p7->d.ptr == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); + return NULL; + } + + switch (OBJ_obj2nid(p7->type)) { + case NID_pkcs7_signed: + return p7->d.sign->signer_info; + case NID_pkcs7_signedAndEnveloped: + return p7->d.signed_and_enveloped->signer_info; + default: + return NULL; + } +} + +int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, + const EVP_MD *dgst) { + /* We now need to add another PKCS7_SIGNER_INFO entry */ + if (!p7i || !x509 || !pkey || !dgst) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } else if (!ASN1_INTEGER_set(p7i->version, 1)) { + return 0; + } else if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, + X509_get_issuer_name(x509))) { + return 0; + } + + /* + * because ASN1_INTEGER_set is used to set a 'long' we will do things the + * ugly way. + */ + ASN1_INTEGER_free(p7i->issuer_and_serial->serial); + if (!(p7i->issuer_and_serial->serial = + ASN1_INTEGER_dup(X509_get0_serialNumber(x509)))) { + return 0; + } + + // NOTE: OpenSSL does not free |p7i->pkey| before setting it. we do so here + // to avoid potential memory leaks. + EVP_PKEY_free(p7i->pkey); + EVP_PKEY_up_ref(pkey); + p7i->pkey = pkey; + + /* Set the algorithms */ + + if (!X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)), + V_ASN1_NULL, NULL)) { + return 0; + } + + switch(EVP_PKEY_id(pkey)) { + case EVP_PKEY_EC: + case EVP_PKEY_DH: { + int snid, hnid; + X509_ALGOR *alg1, *alg2; + PKCS7_SIGNER_INFO_get0_algs(p7i, NULL, &alg1, &alg2); + if (alg1 == NULL || alg1->algorithm == NULL) { + return 0; + } + hnid = OBJ_obj2nid(alg1->algorithm); + if (hnid == NID_undef + || !OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)) + || !X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, NULL)) { + return 0; + } + break; + } + case EVP_PKEY_RSA: + case EVP_PKEY_RSA_PSS: { + X509_ALGOR *alg = NULL; + PKCS7_SIGNER_INFO_get0_algs(p7i, NULL, NULL, &alg); + if (alg != NULL) { + return X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), + V_ASN1_NULL, NULL); + } + break; + } + default: + OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); + return 0; + } + + return 1; +} + +int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509) { + if (!p7i || !x509) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } + if (!ASN1_INTEGER_set(p7i->version, 0)) { + return 0; + } else if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, + X509_get_issuer_name(x509))) { + return 0; + } + + ASN1_INTEGER_free(p7i->issuer_and_serial->serial); + if (!(p7i->issuer_and_serial->serial = + ASN1_INTEGER_dup(X509_get0_serialNumber(x509)))) { + return 0; + } + + EVP_PKEY *pkey = X509_get0_pubkey(x509); + if (pkey == NULL) { + return 0; + } + + if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA_PSS) { + return 0; + } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { + X509_ALGOR *alg; + PKCS7_RECIP_INFO_get0_alg(p7i, &alg); + if (!X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption), + V_ASN1_NULL, NULL)) { + return 0; + } + } + + // NOTE: OpenSSL does not free |p7i->cert| before setting it. we do so here + // to avoid potential memory leaks. + X509_free(p7i->cert); + X509_up_ref(x509); + p7i->cert = x509; + + return 1; +} + +void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk, + X509_ALGOR **pdig, X509_ALGOR **psig) +{ + if (!si) { + return; + } + if (pk) { + *pk = si->pkey; + } + if (pdig) { + *pdig = si->digest_alg; + } + if (psig) { + *psig = si->digest_enc_alg; + } +} + +void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc) +{ + if (!ri) { + return; + } + if (penc) { + *penc = ri->key_enc_algor; + } +} diff --git a/crypto/pkcs7/pkcs7_test.cc b/crypto/pkcs7/pkcs7_test.cc index 8957aa4374..459e065efc 100644 --- a/crypto/pkcs7/pkcs7_test.cc +++ b/crypto/pkcs7/pkcs7_test.cc @@ -336,6 +336,84 @@ static const uint8_t kPKCS7Windows[] = { 0xcd, 0x5a, 0x2a, 0x82, 0xb2, 0x37, 0x79, 0x31, 0x00, }; + +// kPKCS7SignedWithSignerInfo has content SignedData, but unlike other test +// objects, it contains SignerInfos as well. +static const uint8_t kPKCS7SignedWithSignerInfo[] { + 0x30, 0x82, 0x03, 0x54, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, + 0x01, 0x07, 0x02, 0xa0, 0x82, 0x03, 0x45, 0x30, 0x82, 0x03, 0x41, 0x02, + 0x01, 0x01, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, + 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x30, 0x82, 0x00, 0x25, 0x06, + 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x82, + 0x00, 0x16, 0x04, 0x82, 0x00, 0x12, 0x49, 0x6e, 0x69, 0x7a, 0x69, 0x6f, + 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x75, 0x74, 0x6f, 0x2e, 0x0a, + 0xa0, 0x82, 0x01, 0x72, 0x30, 0x82, 0x01, 0x6e, 0x30, 0x82, 0x01, 0x14, + 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x10, 0x60, 0x5c, 0xd2, 0xd5, 0x26, + 0x75, 0xb3, 0x92, 0xa5, 0xaa, 0x9b, 0x02, 0xb2, 0x6a, 0x55, 0x66, 0x30, + 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, + 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x04, + 0x54, 0x65, 0x73, 0x74, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x38, 0x30, 0x37, + 0x31, 0x36, 0x31, 0x34, 0x35, 0x36, 0x33, 0x35, 0x5a, 0x17, 0x0d, 0x31, + 0x39, 0x30, 0x37, 0x31, 0x36, 0x31, 0x34, 0x35, 0x36, 0x33, 0x35, 0x5a, + 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, + 0x04, 0x54, 0x65, 0x73, 0x74, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, + 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, + 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x24, 0x4c, 0xb6, 0xcc, + 0x1c, 0x69, 0x21, 0xff, 0x49, 0xd8, 0xbe, 0x26, 0x3e, 0x0b, 0x7c, 0xd0, + 0x5a, 0x28, 0x65, 0x5a, 0x84, 0x6c, 0x82, 0x5e, 0xca, 0xe6, 0xec, 0xe6, + 0x9a, 0x6a, 0x21, 0xc4, 0xe4, 0xf2, 0x20, 0x24, 0xc0, 0xe9, 0xf4, 0xe4, + 0x74, 0x9c, 0x98, 0xa1, 0xad, 0xf2, 0x5f, 0x90, 0xde, 0x6e, 0xf9, 0x48, + 0x2b, 0x67, 0x18, 0x83, 0xa7, 0x0e, 0xb4, 0xb7, 0xab, 0x9f, 0x06, 0x43, + 0xa3, 0x52, 0x30, 0x50, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x01, + 0x01, 0xff, 0x04, 0x04, 0x03, 0x02, 0x04, 0xf0, 0x30, 0x1d, 0x06, 0x03, + 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xc3, 0xc0, 0x84, 0xdf, 0x7b, + 0x04, 0x0d, 0xb0, 0x38, 0xaf, 0x51, 0x8c, 0xe3, 0x97, 0xf6, 0xec, 0x20, + 0xd6, 0x26, 0xe6, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, + 0x30, 0x16, 0x80, 0x14, 0xc3, 0xc0, 0x84, 0xdf, 0x7b, 0x04, 0x0d, 0xb0, + 0x38, 0xaf, 0x51, 0x8c, 0xe3, 0x97, 0xf6, 0xec, 0x20, 0xd6, 0x26, 0xe6, + 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, + 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x21, 0x00, 0xde, 0x60, 0x1e, 0x57, + 0x3d, 0xaf, 0xb5, 0x9b, 0xc5, 0x51, 0xd5, 0x8e, 0x3e, 0x7b, 0x9e, 0xda, + 0x06, 0x12, 0xdd, 0x01, 0x12, 0x80, 0x5a, 0x22, 0x17, 0xb7, 0x34, 0x75, + 0x9b, 0x88, 0x44, 0x17, 0x02, 0x20, 0x67, 0xc3, 0xfd, 0xe6, 0x07, 0x80, + 0xd4, 0x1c, 0x1d, 0x7a, 0x3b, 0x90, 0x29, 0x1f, 0x3d, 0x39, 0xc4, 0xdc, + 0x2f, 0x20, 0x6d, 0xcc, 0xba, 0x2f, 0x98, 0x2c, 0x06, 0xb6, 0x7c, 0x09, + 0xb2, 0x32, 0x31, 0x82, 0x01, 0x8a, 0x30, 0x82, 0x01, 0x86, 0x02, 0x01, + 0x01, 0x30, 0x23, 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, + 0x04, 0x03, 0x0c, 0x04, 0x54, 0x65, 0x73, 0x74, 0x02, 0x10, 0x60, 0x5c, + 0xd2, 0xd5, 0x26, 0x75, 0xb3, 0x92, 0xa5, 0xaa, 0x9b, 0x02, 0xb2, 0x6a, + 0x55, 0x66, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, + 0x04, 0x02, 0x01, 0x05, 0x00, 0xa0, 0x81, 0xf7, 0x30, 0x18, 0x06, 0x09, + 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x03, 0x31, 0x0b, 0x06, + 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 0x1c, + 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05, 0x31, + 0x0f, 0x17, 0x0d, 0x31, 0x38, 0x30, 0x37, 0x31, 0x36, 0x31, 0x34, 0x35, + 0x36, 0x33, 0x35, 0x5a, 0x30, 0x2a, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, + 0xf7, 0x0d, 0x01, 0x09, 0x34, 0x31, 0x1d, 0x30, 0x1b, 0x30, 0x0d, 0x06, + 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, + 0xa1, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, + 0x30, 0x2f, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, + 0x04, 0x31, 0x22, 0x04, 0x20, 0x72, 0x4c, 0x51, 0xbb, 0xe7, 0x6d, 0xa0, + 0x5a, 0xfb, 0x20, 0xcb, 0xe8, 0xeb, 0x03, 0x7c, 0xda, 0xe1, 0xaf, 0xd7, + 0x13, 0x12, 0x5d, 0x2d, 0xc1, 0x3d, 0x55, 0x2d, 0xa9, 0xf4, 0x42, 0xd2, + 0x4d, 0x30, 0x60, 0x06, 0x0b, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, + 0x09, 0x10, 0x02, 0x2f, 0x31, 0x51, 0x30, 0x4f, 0x30, 0x4d, 0x30, 0x4b, + 0x04, 0x20, 0xba, 0xbc, 0x08, 0x43, 0x4c, 0x58, 0x26, 0x73, 0x01, 0xe0, + 0x06, 0x86, 0x1d, 0x27, 0xec, 0xa1, 0x25, 0x67, 0x0e, 0x79, 0x27, 0x97, + 0x24, 0x8e, 0x76, 0xa5, 0x71, 0x7a, 0x5b, 0xf9, 0x93, 0xc2, 0x30, 0x27, + 0x30, 0x13, 0xa4, 0x11, 0x30, 0x0f, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x03, + 0x55, 0x04, 0x03, 0x0c, 0x04, 0x54, 0x65, 0x73, 0x74, 0x02, 0x10, 0x60, + 0x5c, 0xd2, 0xd5, 0x26, 0x75, 0xb3, 0x92, 0xa5, 0xaa, 0x9b, 0x02, 0xb2, + 0x6a, 0x55, 0x66, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, + 0x04, 0x03, 0x02, 0x04, 0x47, 0x30, 0x45, 0x02, 0x21, 0x00, 0xf1, 0x04, + 0x30, 0x2e, 0xab, 0xb4, 0x28, 0xec, 0xc8, 0xe7, 0x1c, 0xe2, 0xe3, 0x8f, + 0x6c, 0xbe, 0xd8, 0x48, 0xa8, 0xe9, 0xa2, 0xb5, 0xd5, 0xda, 0x19, 0x93, + 0x3e, 0x48, 0x5c, 0x83, 0xcc, 0x71, 0x02, 0x20, 0x0c, 0xbb, 0x40, 0xa0, + 0xb7, 0xb9, 0xb2, 0xe0, 0x48, 0x92, 0x06, 0x22, 0xfb, 0x28, 0x6f, 0xd1, + 0x46, 0x95, 0xf2, 0x4c, 0xf6, 0x50, 0xbd, 0xaa, 0x40, 0xb8, 0x85, 0xf8, + 0x4c, 0x34, 0xbb, 0xb9, +}; + // kOpenSSLCRL is the Equifax CRL, converted to PKCS#7 form by: // openssl crl2pkcs7 -inform DER -in secureca.crl static const uint8_t kOpenSSLCRL[] = { @@ -528,6 +606,9 @@ static void TestCertReparse(const uint8_t *der_bytes, size_t der_len) { } else { EXPECT_EQ(ptr, der_bytes + der_len); } + bssl::UniquePtr pkcs7_dup(PKCS7_dup(pkcs7_obj.get())); + ASSERT_TRUE(pkcs7_dup); + EXPECT_EQ(OBJ_obj2nid(pkcs7_obj.get()->type), OBJ_obj2nid(pkcs7_dup.get()->type)); ASSERT_TRUE(PKCS7_type_is_signed(pkcs7_obj.get())); const STACK_OF(X509) *certs3 = pkcs7_obj->d.sign->cert; @@ -673,6 +754,10 @@ TEST(PKCS7Test, CertReparseWindows) { TestCertReparse(kPKCS7Windows, sizeof(kPKCS7Windows)); } +TEST(PKCS7Test, CertSignedWithSignerInfos) { + TestCertReparse(kPKCS7SignedWithSignerInfo, sizeof(kPKCS7SignedWithSignerInfo)); +} + TEST(PKCS7Test, CrlReparse) { TestCRLReparse(kOpenSSLCRL, sizeof(kOpenSSLCRL)); } @@ -1044,3 +1129,232 @@ hJTbHtjEDJ7BHLC/CNUhXbpyyu1y ERR_clear_error(); } + +TEST(PKCS7Test, GettersSetters) { + bssl::UniquePtr p7; + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7); + EXPECT_FALSE(PKCS7_set_type(p7.get(), NID_undef)); + EXPECT_FALSE(PKCS7_content_new(p7.get(), NID_undef)); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7); + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signed)); + // set type redundantly to ensure we're properly freeing up existing + // resources on subsequent set. + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signed)); + EXPECT_TRUE(PKCS7_type_is_signed(p7.get())); + EXPECT_TRUE(PKCS7_content_new(p7.get(), NID_pkcs7_signed)); + EXPECT_FALSE(PKCS7_set_cipher(p7.get(), EVP_aes_128_gcm())); + EXPECT_FALSE(PKCS7_add_recipient_info(p7.get(), nullptr)); + EXPECT_FALSE(PKCS7_get_signer_info(nullptr)); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7); + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_digest)); + // set type redundantly to ensure we're properly freeing up existing + // resources on subsequent set. + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_digest)); + EXPECT_TRUE(PKCS7_type_is_digest(p7.get())); + EXPECT_TRUE(PKCS7_content_new(p7.get(), NID_pkcs7_digest)); + EXPECT_FALSE(PKCS7_add_certificate(p7.get(), nullptr)); + EXPECT_FALSE(PKCS7_add_crl(p7.get(), nullptr)); + EXPECT_FALSE(PKCS7_add_signer(p7.get(), nullptr)); + EXPECT_FALSE(PKCS7_get_signer_info(p7.get())); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7.get()); + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_data)); + // set type redundantly to ensure we're properly freeing up existing + // resources on subsequent set. + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_data)); + EXPECT_TRUE(PKCS7_type_is_data(p7.get())); + EXPECT_FALSE(PKCS7_set_content(p7.get(), p7.get())); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7.get()); + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signedAndEnveloped)); + // set type redundantly to ensure we're properly freeing up existing + // resources on subsequent set. + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signedAndEnveloped)); + EXPECT_TRUE(PKCS7_type_is_signedAndEnveloped(p7.get())); + EXPECT_TRUE(PKCS7_set_cipher(p7.get(), EVP_aes_128_gcm())); + EXPECT_FALSE(PKCS7_set_content(p7.get(), p7.get())); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7.get()); + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_enveloped)); + // set type redundantly to ensure we're properly freeing up existing + // resources on subsequent set. + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_enveloped)); + EXPECT_TRUE(PKCS7_type_is_enveloped(p7.get())); + EXPECT_TRUE(PKCS7_set_cipher(p7.get(), EVP_aes_128_gcm())); + EXPECT_FALSE(PKCS7_set_content(p7.get(), p7.get())); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7.get()); + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_encrypted)); + // set type redundantly to ensure we're properly freeing up existing + // resources on subsequent set. + EXPECT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_encrypted)); + EXPECT_TRUE(PKCS7_type_is_encrypted(p7.get())); + EXPECT_FALSE(PKCS7_set_content(p7.get(), p7.get())); + + // |d2i_*| functions advance the input reference by number of bytes parsed, + // so save off a local reference and reset it for each test case. + const uint8_t *p7_der = kPKCS7SignedWithSignerInfo; + const size_t p7_der_len = sizeof(kPKCS7SignedWithSignerInfo); + p7.reset(d2i_PKCS7(nullptr, &p7_der, p7_der_len)); + ASSERT_TRUE(p7); + ASSERT_TRUE(PKCS7_type_is_signed(p7.get())); + STACK_OF(PKCS7_SIGNER_INFO) *sk_p7si_signed = PKCS7_get_signer_info(p7.get()); + ASSERT_TRUE(sk_p7si_signed); + ASSERT_GT(sk_PKCS7_SIGNER_INFO_num(sk_p7si_signed), 0UL); + PKCS7_SIGNER_INFO *p7si = sk_PKCS7_SIGNER_INFO_value(sk_p7si_signed, 0); + ASSERT_TRUE(p7si); + EXPECT_FALSE(PKCS7_get_signed_attribute(p7si, NID_md5)); // hash nid not valid x509 attr + ASN1_TYPE *signing_time = PKCS7_get_signed_attribute(p7si, NID_pkcs9_signingTime); + ASSERT_TRUE(signing_time); + EVP_PKEY *pkey; + X509_ALGOR *pdig; + X509_ALGOR *psig; + PKCS7_SIGNER_INFO_get0_algs(p7si, &pkey, &pdig, &psig); + ASSERT_FALSE(pkey); // no attached pkey + ASSERT_TRUE(psig); + ASSERT_TRUE(pdig); + + bssl::UniquePtr p7_dup(PKCS7_dup(p7.get())); + ASSERT_TRUE(p7_dup); + EXPECT_TRUE(PKCS7_type_is_signed(p7_dup.get())); + + p7_der = kPKCS7SignedWithSignerInfo; + PKCS7 *p7_ptr = nullptr; + bssl::UniquePtr bio(BIO_new_mem_buf(p7_der, p7_der_len)); + ASSERT_FALSE(d2i_PKCS7_bio(bio.get(), nullptr)); + p7.reset(d2i_PKCS7_bio(bio.get(), &p7_ptr)); + ASSERT_TRUE(p7); + ASSERT_TRUE(PKCS7_type_is_signed(p7.get())); + bio.reset(BIO_new(BIO_s_mem())); + ASSERT_TRUE(i2d_PKCS7_bio(bio.get(), p7.get())); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7); + ASSERT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signed)); + bio.reset(BIO_new_mem_buf(kPEMCert, strlen(kPEMCert))); + ASSERT_TRUE(bio); + bssl::UniquePtr certs(sk_X509_new_null()); + ASSERT_TRUE(certs); + ASSERT_TRUE(PKCS7_get_PEM_certificates(certs.get(), bio.get())); + ASSERT_EQ(1U, sk_X509_num(certs.get())); + EXPECT_TRUE(PKCS7_add_certificate(p7.get(), sk_X509_value(certs.get(), 0U))); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7); + ASSERT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signed)); + bio.reset(BIO_new_mem_buf(kPEMCRL, strlen(kPEMCRL))); + ASSERT_TRUE(bio); + bssl::UniquePtr crls(sk_X509_CRL_new_null()); + ASSERT_TRUE(crls); + ASSERT_TRUE(PKCS7_get_PEM_CRLs(crls.get(), bio.get())); + ASSERT_EQ(1U, sk_X509_CRL_num(crls.get())); + EXPECT_TRUE(PKCS7_add_crl(p7.get(), sk_X509_CRL_value(crls.get(), 0U))); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7); + ASSERT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signedAndEnveloped)); + bio.reset(BIO_new_mem_buf(kPEMCert, strlen(kPEMCert))); + ASSERT_TRUE(bio); + certs.reset(sk_X509_new_null()); + ASSERT_TRUE(certs); + ASSERT_TRUE(PKCS7_get_PEM_certificates(certs.get(), bio.get())); + ASSERT_EQ(1U, sk_X509_num(certs.get())); + EXPECT_TRUE(PKCS7_add_certificate(p7.get(), sk_X509_value(certs.get(), 0U))); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7); + ASSERT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signedAndEnveloped)); + bio.reset(BIO_new_mem_buf(kPEMCRL, strlen(kPEMCRL))); + ASSERT_TRUE(bio); + crls.reset(sk_X509_CRL_new_null()); + ASSERT_TRUE(crls); + ASSERT_TRUE(PKCS7_get_PEM_CRLs(crls.get(), bio.get())); + ASSERT_EQ(1U, sk_X509_CRL_num(crls.get())); + EXPECT_TRUE(PKCS7_add_crl(p7.get(), sk_X509_CRL_value(crls.get(), 0U))); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7); + ASSERT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signed)); + bssl::UniquePtr rsa(RSA_new()); + ASSERT_TRUE(rsa); + ASSERT_TRUE(RSA_generate_key_fips(rsa.get(), 2048, nullptr)); + bssl::UniquePtr rsa_pkey(EVP_PKEY_new()); + ASSERT_TRUE(rsa_pkey); + ASSERT_TRUE(EVP_PKEY_set1_RSA(rsa_pkey.get(), rsa.get())); + bssl::UniquePtr rsa_x509(sk_X509_pop(certs.get())); + ASSERT_EQ(0U, sk_X509_num(certs.get())); + ASSERT_TRUE(rsa_x509); + p7si = PKCS7_SIGNER_INFO_new(); + ASSERT_TRUE(p7si); + EXPECT_TRUE(PKCS7_SIGNER_INFO_set(p7si, rsa_x509.get(), rsa_pkey.get(), EVP_sha256())); + EXPECT_FALSE(PKCS7_SIGNER_INFO_set(p7si, nullptr, nullptr, nullptr)); + EXPECT_TRUE(PKCS7_add_signer(p7.get(), p7si)); + EXPECT_TRUE(PKCS7_get_signer_info(p7.get())); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7.get()); + ASSERT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signedAndEnveloped)); + p7si = PKCS7_SIGNER_INFO_new(); + ASSERT_TRUE(p7si); + bssl::UniquePtr ecdsa_x509(X509_new()); + ASSERT_TRUE(ecdsa_x509); + bssl::UniquePtr ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr)); + ASSERT_TRUE(ctx); + ASSERT_TRUE(EVP_PKEY_keygen_init(ctx.get())); + ASSERT_TRUE( + EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(), NID_X9_62_prime256v1)); + bssl::UniquePtr ecdsa_pkey(EVP_PKEY_new()); + EVP_PKEY *ecdsa_pkey_ptr = ecdsa_pkey.get(); + ASSERT_TRUE(EVP_PKEY_keygen(ctx.get(), &ecdsa_pkey_ptr)); + EXPECT_TRUE(PKCS7_SIGNER_INFO_set(p7si, ecdsa_x509.get(), ecdsa_pkey.get(), EVP_sha256())); + EXPECT_TRUE(PKCS7_add_signer(p7.get(), p7si)); + EXPECT_TRUE(PKCS7_get_signer_info(p7.get())); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7.get()); + ASSERT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signed)); + p7si = PKCS7_SIGNER_INFO_new(); + ASSERT_TRUE(p7si); + ecdsa_x509.reset(X509_new()); + ASSERT_TRUE(ecdsa_x509); + ctx.reset(EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, nullptr)); + ASSERT_TRUE(ctx); + ASSERT_TRUE(EVP_PKEY_keygen_init(ctx.get())); + ecdsa_pkey.reset(EVP_PKEY_new()); + ecdsa_pkey_ptr = ecdsa_pkey.get(); + ASSERT_TRUE(EVP_PKEY_keygen(ctx.get(), &ecdsa_pkey_ptr)); + EXPECT_FALSE(PKCS7_SIGNER_INFO_set(p7si, ecdsa_x509.get(), ecdsa_pkey.get(), EVP_sha256())); + PKCS7_SIGNER_INFO_free(p7si); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7); + ASSERT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_signedAndEnveloped)); + ASSERT_TRUE(X509_set_pubkey(rsa_x509.get(), rsa_pkey.get())); + PKCS7_RECIP_INFO *p7ri = PKCS7_RECIP_INFO_new(); + EXPECT_TRUE(PKCS7_RECIP_INFO_set(p7ri, rsa_x509.get())); + EXPECT_FALSE(PKCS7_RECIP_INFO_set(p7ri, nullptr)); + X509_ALGOR *penc = NULL; + PKCS7_RECIP_INFO_get0_alg(p7ri, &penc); + ASSERT_TRUE(penc); + EXPECT_TRUE(PKCS7_add_recipient_info(p7.get(), p7ri)); + + p7.reset(PKCS7_new()); + ASSERT_TRUE(p7); + ASSERT_TRUE(PKCS7_set_type(p7.get(), NID_pkcs7_enveloped)); + ASSERT_TRUE(X509_set_pubkey(rsa_x509.get(), rsa_pkey.get())); + p7ri = PKCS7_RECIP_INFO_new(); + EXPECT_TRUE(PKCS7_RECIP_INFO_set(p7ri, rsa_x509.get())); + PKCS7_RECIP_INFO_get0_alg(p7ri, &penc); + ASSERT_TRUE(penc); + EXPECT_TRUE(PKCS7_add_recipient_info(p7.get(), p7ri)); +} diff --git a/crypto/pkcs7/pkcs7_x509.c b/crypto/pkcs7/pkcs7_x509.c index 7a3f6c9752..adaaa265fa 100644 --- a/crypto/pkcs7/pkcs7_x509.c +++ b/crypto/pkcs7/pkcs7_x509.c @@ -238,6 +238,7 @@ int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls) { PKCS7 *d2i_PKCS7_bio(BIO *bio, PKCS7 **out) { if (out == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); return NULL; } @@ -428,3 +429,76 @@ PKCS7 *PKCS7_sign(X509 *sign_cert, EVP_PKEY *pkey, STACK_OF(X509) *certs, OPENSSL_free(der); return ret; } + +int PKCS7_add_certificate(PKCS7 *p7, X509 *x509) +{ + STACK_OF(X509) **sk; + + if (p7 == NULL || x509 == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } + + switch (OBJ_obj2nid(p7->type)) { + case NID_pkcs7_signed: + sk = &(p7->d.sign->cert); + break; + case NID_pkcs7_signedAndEnveloped: + sk = &(p7->d.signed_and_enveloped->cert); + break; + default: + OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); + return 0; + } + + if (*sk == NULL) { + *sk = sk_X509_new_null(); + } + if (*sk == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_CRYPTO_LIB); + return 0; + } + + if (!sk_X509_push(*sk, x509)) { + return 0; + } + X509_up_ref(x509); + return 1; + +} + +int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl) +{ + STACK_OF(X509_CRL) **sk; + + if (p7 == NULL || crl == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); + return 0; + } + + switch (OBJ_obj2nid(p7->type)) { + case NID_pkcs7_signed: + sk = &(p7->d.sign->crl); + break; + case NID_pkcs7_signedAndEnveloped: + sk = &(p7->d.signed_and_enveloped->crl); + break; + default: + OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); + return 0; + } + + if (*sk == NULL) { + *sk = sk_X509_CRL_new_null(); + } + if (*sk == NULL) { + OPENSSL_PUT_ERROR(PKCS7, ERR_R_CRYPTO_LIB); + return 0; + } + + if (!sk_X509_CRL_push(*sk, crl)) { + return 0; + } + X509_CRL_up_ref(crl); + return 1; +} diff --git a/include/openssl/pkcs7.h b/include/openssl/pkcs7.h index 1f046f634a..e3b3f3793b 100644 --- a/include/openssl/pkcs7.h +++ b/include/openssl/pkcs7.h @@ -183,6 +183,77 @@ OPENSSL_EXPORT PKCS7 *d2i_PKCS7_bio(BIO *bio, PKCS7 **out); // error. OPENSSL_EXPORT int i2d_PKCS7_bio(BIO *bio, const PKCS7 *p7); +// PKCS7_get_signed_attribute returns a pointer to the first signed attribute +// from |si| with NID |nid| if one is present, else NULL. +OPENSSL_EXPORT ASN1_TYPE *PKCS7_get_signed_attribute(const PKCS7_SIGNER_INFO *si, + int nid); + +// PKCS7_get_signer_info returns |p7|'s attached PKCS7_SIGNER_INFO if present +// and |p7| is of a relevant type, else NULL. This function only pertains to +// signedData and signedAndEnvelopedData. +OPENSSL_EXPORT STACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7); + +// PKCS7_RECIP_INFO_set attaches |x509| to |p7i| and increments |x509|'s +// reference count. It returns 1 on success and 0 on failure or if |x509|'s +// public key not usable for encryption. +OPENSSL_EXPORT int PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509); + +// PKCS7_SIGNER_INFO_set attaches the other parameters to |p7i|, returning 1 on +// success and 0 on error or if specified parameters are inapplicable to +// signing. Only EC, DH, and RSA |pkey|s are supported. |pkey| is assigned to +// |p7i| and its reference count is incremented. +OPENSSL_EXPORT int PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, + EVP_PKEY *pkey, const EVP_MD *dgst); + +// PKCS7_add_certificate adds |x509| to |p7|'s certificate stack, incrementing +// |x509|'s reference count. It returns 1 on success and 0 on failure or if +// |p7| isn't of an applicable type: signedData and signedAndEnvelopedData. +OPENSSL_EXPORT int PKCS7_add_certificate(PKCS7 *p7, X509 * x509); + +// PKCS7_add_crl adds |x509| to |p7|'s CRL stack, incrementing |x509|'s +// reference count. It returns 1 on success and 0 on failure or if |p7| isn't +// of an applicable type: signedData and signedAndEnvelopedData. +OPENSSL_EXPORT int PKCS7_add_crl(PKCS7 *p7, X509_CRL * x509); + +// PKCS7_add_recipient_info adds |ri| to |p7|, returning 1 on succes or 0 if +// |p7| is of an inapplicable type: envelopedData and signedAndEnvelopedData. +OPENSSL_EXPORT int PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri); + +// PKCS7_add_signer adds |p7i| to |p7|, returning 1 on succes or 0 if +// |p7| is of an inapplicable type: signedData and signedAndEnvelopedData. +OPENSSL_EXPORT int PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *p7i); + +// PKCS7_content_new allocates a new PKCS7 and adds it to |p7| as content. It +// returns 1 on success and 0 on failure. +OPENSSL_EXPORT int PKCS7_content_new(PKCS7 *p7, int nid); + +// PKCS7_set_cipher sets |cipher| on |p7| for applicable types of |p7|. It +// returns 1 on success and 0 on failure or if |p7| is not an applicable type: +// envelopedData and signedAndEnvelopedData. +OPENSSL_EXPORT int PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher); + +// PKCS7_set_content sets |p7_data| as content on |p7| for applicable types of +// |p7|: signedData and digestData. |p7_data| may be NULL. It frees any +// existing content on |p7|, returning 1 on success and 0 on failure. +OPENSSL_EXPORT int PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data); + +// PKCS7_set_type instantiates |p7| as type |type|. It returns 1 on success and +// 0 on failure or if |type| is not a valid PKCS7 content type. +OPENSSL_EXPORT int PKCS7_set_type(PKCS7 *p7, int type); + +// PKCS7_RECIP_INFO_get0_alg sets |*penc| to |ri|'s key encryption algorithm, +// if present. Ownership of |*penc| is retained by |ri|. +OPENSSL_EXPORT void PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, + X509_ALGOR **penc); + +// PKCS7_SIGNER_INFO_get0_algs sets all of, if present: |*pk| to |si|'s key, +// |*pdig| to |si|'s digest angorithm, and |*psig| to |si|'s signature +// algorithm. Ownership of |*pk|, |*pdig|, and |*psig) is retained by |si|. +OPENSSL_EXPORT void PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, + EVP_PKEY **pk, + X509_ALGOR **pdig, + X509_ALGOR **psig); + // PKCS7_type_is_data returns 1 if |p7| is of type data OPENSSL_EXPORT int PKCS7_type_is_data(const PKCS7 *p7); @@ -286,5 +357,9 @@ BSSL_NAMESPACE_END #define PKCS7_R_NOT_PKCS7_SIGNED_DATA 101 #define PKCS7_R_NO_CERTIFICATES_INCLUDED 102 #define PKCS7_R_NO_CRLS_INCLUDED 103 +#define PKCS7_R_UNSUPPORTED_CONTENT_TYPE 104 +#define PKCS7_R_WRONG_CONTENT_TYPE 105 +#define PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER 106 +#define PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE 107 #endif // OPENSSL_HEADER_PKCS7_H