2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* Simple PKCS#7 processing functions */
13 #include "internal/cryptlib.h"
14 #include <openssl/x509.h>
15 #include <openssl/x509v3.h>
16 #include "pk7_local.h"
18 #define BUFFERSIZE 4096
20 DEFINE_STACK_OF(X509_ALGOR)
21 DEFINE_STACK_OF(PKCS7_SIGNER_INFO)
23 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
25 PKCS7 *PKCS7_sign_with_libctx(X509 *signcert, EVP_PKEY *pkey,
26 STACK_OF(X509) *certs, BIO *data, int flags,
27 OPENSSL_CTX *libctx, const char *propq)
32 if ((p7 = PKCS7_new_with_libctx(libctx, propq)) == NULL) {
33 PKCS7err(0, ERR_R_MALLOC_FAILURE);
37 if (!PKCS7_set_type(p7, NID_pkcs7_signed))
40 if (!PKCS7_content_new(p7, NID_pkcs7_data))
43 if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) {
44 PKCS7err(0, PKCS7_R_PKCS7_ADD_SIGNER_ERROR);
48 if (!(flags & PKCS7_NOCERTS)) {
49 for (i = 0; i < sk_X509_num(certs); i++) {
50 if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i)))
55 if (flags & PKCS7_DETACHED)
56 PKCS7_set_detached(p7, 1);
58 if (flags & (PKCS7_STREAM | PKCS7_PARTIAL))
61 if (PKCS7_final(p7, data, flags))
69 PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
72 return PKCS7_sign_with_libctx(signcert, pkey, certs, data, flags, NULL, NULL);
76 int PKCS7_final(PKCS7 *p7, BIO *data, int flags)
81 if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
82 PKCS7err(PKCS7_F_PKCS7_FINAL, ERR_R_MALLOC_FAILURE);
86 SMIME_crlf_copy(data, p7bio, flags);
88 (void)BIO_flush(p7bio);
90 if (!PKCS7_dataFinal(p7, p7bio)) {
91 PKCS7err(PKCS7_F_PKCS7_FINAL, PKCS7_R_PKCS7_DATASIGN);
102 /* Check to see if a cipher exists and if so add S/MIME capabilities */
104 static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
106 if (EVP_get_cipherbynid(nid))
107 return PKCS7_simple_smimecap(sk, nid, arg);
111 static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
113 if (EVP_get_digestbynid(nid))
114 return PKCS7_simple_smimecap(sk, nid, arg);
118 PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
119 EVP_PKEY *pkey, const EVP_MD *md,
122 PKCS7_SIGNER_INFO *si = NULL;
123 STACK_OF(X509_ALGOR) *smcap = NULL;
125 if (!X509_check_private_key(signcert, pkey)) {
126 PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
127 PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
131 if ((si = PKCS7_add_signature(p7, signcert, pkey, md)) == NULL) {
132 PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
133 PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
137 si->ctx = pkcs7_get0_ctx(p7);
138 if (!(flags & PKCS7_NOCERTS)) {
139 if (!PKCS7_add_certificate(p7, signcert))
143 if (!(flags & PKCS7_NOATTR)) {
144 if (!PKCS7_add_attrib_content_type(si, NULL))
146 /* Add SMIMECapabilities */
147 if (!(flags & PKCS7_NOSMIMECAP)) {
148 if ((smcap = sk_X509_ALGOR_new_null()) == NULL) {
149 PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, ERR_R_MALLOC_FAILURE);
152 if (!add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
153 || !add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
154 || !add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
155 || !add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
156 || !add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
157 || !add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
158 || !add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
159 || !add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
160 || !add_cipher_smcap(smcap, NID_rc2_cbc, 128)
161 || !add_cipher_smcap(smcap, NID_rc2_cbc, 64)
162 || !add_cipher_smcap(smcap, NID_des_cbc, -1)
163 || !add_cipher_smcap(smcap, NID_rc2_cbc, 40)
164 || !PKCS7_add_attrib_smimecap(si, smcap))
166 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
169 if (flags & PKCS7_REUSE_DIGEST) {
170 if (!pkcs7_copy_existing_digest(p7, si))
172 if (!(flags & PKCS7_PARTIAL)
173 && !PKCS7_SIGNER_INFO_sign(si))
179 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
184 * Search for a digest matching SignerInfo digest type and if found copy
188 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
191 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
192 PKCS7_SIGNER_INFO *sitmp;
193 ASN1_OCTET_STRING *osdig = NULL;
194 sinfos = PKCS7_get_signer_info(p7);
195 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
196 sitmp = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
199 if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0)
201 if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) {
202 osdig = PKCS7_digest_from_attributes(sitmp->auth_attr);
209 return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length);
211 PKCS7err(PKCS7_F_PKCS7_COPY_EXISTING_DIGEST,
212 PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND);
216 int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
217 BIO *indata, BIO *out, int flags)
219 STACK_OF(X509) *signers;
221 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
222 PKCS7_SIGNER_INFO *si;
223 X509_STORE_CTX *cert_ctx = NULL;
225 int i, j = 0, k, ret = 0;
227 BIO *tmpin = NULL, *tmpout = NULL;
228 const PKCS7_CTX *p7_ctx;
231 PKCS7err(0, PKCS7_R_INVALID_NULL_POINTER);
235 if (!PKCS7_type_is_signed(p7)) {
236 PKCS7err(0, PKCS7_R_WRONG_CONTENT_TYPE);
240 /* Check for no data and no content: no data to verify signature */
241 if (PKCS7_get_detached(p7) && !indata) {
242 PKCS7err(0, PKCS7_R_NO_CONTENT);
246 if (flags & PKCS7_NO_DUAL_CONTENT) {
248 * This was originally "#if 0" because we thought that only old broken
249 * Netscape did this. It turns out that Authenticode uses this kind
250 * of "extended" PKCS7 format, and things like UEFI secure boot and
251 * tools like osslsigncode need it. In Authenticode the verification
252 * process is different, but the existing PKCs7 verification works.
254 if (!PKCS7_get_detached(p7) && indata) {
255 PKCS7err(0, PKCS7_R_CONTENT_AND_DATA_PRESENT);
260 sinfos = PKCS7_get_signer_info(p7);
262 if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
263 PKCS7err(0, PKCS7_R_NO_SIGNATURES_ON_DATA);
267 signers = PKCS7_get0_signers(p7, certs, flags);
271 /* Now verify the certificates */
272 p7_ctx = pkcs7_get0_ctx(p7);
273 cert_ctx = X509_STORE_CTX_new_with_libctx(p7_ctx->libctx, p7_ctx->propq);
274 if (cert_ctx == NULL)
276 if (!(flags & PKCS7_NOVERIFY))
277 for (k = 0; k < sk_X509_num(signers); k++) {
278 signer = sk_X509_value(signers, k);
279 if (!(flags & PKCS7_NOCHAIN)) {
280 if (!X509_STORE_CTX_init(cert_ctx, store, signer,
282 PKCS7err(0, ERR_R_X509_LIB);
285 X509_STORE_CTX_set_default(cert_ctx, "smime_sign");
286 } else if (!X509_STORE_CTX_init(cert_ctx, store, signer, NULL)) {
287 PKCS7err(0, ERR_R_X509_LIB);
290 if (!(flags & PKCS7_NOCRL))
291 X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl);
292 i = X509_verify_cert(cert_ctx);
294 j = X509_STORE_CTX_get_error(cert_ctx);
295 X509_STORE_CTX_cleanup(cert_ctx);
297 PKCS7err(0, PKCS7_R_CERTIFICATE_VERIFY_ERROR);
298 ERR_add_error_data(2, "Verify error:",
299 X509_verify_cert_error_string(j));
302 /* Check for revocation status here */
306 * Performance optimization: if the content is a memory BIO then store
307 * its contents in a temporary read only memory BIO. This avoids
308 * potentially large numbers of slow copies of data which will occur when
309 * reading from a read write memory BIO when signatures are calculated.
312 if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) {
315 len = BIO_get_mem_data(indata, &ptr);
316 tmpin = BIO_new_mem_buf(ptr, len);
318 PKCS7err(0, ERR_R_MALLOC_FAILURE);
324 if ((p7bio = PKCS7_dataInit(p7, tmpin)) == NULL)
327 if (flags & PKCS7_TEXT) {
328 if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
329 PKCS7err(0, ERR_R_MALLOC_FAILURE);
332 BIO_set_mem_eof_return(tmpout, 0);
336 /* We now have to 'read' from p7bio to calculate digests etc. */
337 if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
338 PKCS7err(0, ERR_R_MALLOC_FAILURE);
342 i = BIO_read(p7bio, buf, BUFFERSIZE);
346 BIO_write(tmpout, buf, i);
349 if (flags & PKCS7_TEXT) {
350 if (!SMIME_text(tmpout, out)) {
351 PKCS7err(0, PKCS7_R_SMIME_TEXT_ERROR);
358 /* Now Verify All Signatures */
359 if (!(flags & PKCS7_NOSIGS))
360 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
361 si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
362 signer = sk_X509_value(signers, i);
363 j = PKCS7_signatureVerify(p7bio, p7, si, signer);
365 PKCS7err(0, PKCS7_R_SIGNATURE_FAILURE);
373 X509_STORE_CTX_free(cert_ctx);
375 if (tmpin == indata) {
380 sk_X509_free(signers);
384 STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
387 STACK_OF(X509) *signers;
388 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
389 PKCS7_SIGNER_INFO *si;
390 PKCS7_ISSUER_AND_SERIAL *ias;
395 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_INVALID_NULL_POINTER);
399 if (!PKCS7_type_is_signed(p7)) {
400 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_WRONG_CONTENT_TYPE);
404 /* Collect all the signers together */
406 sinfos = PKCS7_get_signer_info(p7);
408 if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
409 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_NO_SIGNERS);
413 if ((signers = sk_X509_new_null()) == NULL) {
414 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, ERR_R_MALLOC_FAILURE);
418 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
419 si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
420 ias = si->issuer_and_serial;
422 /* If any certificates passed they take priority */
424 signer = X509_find_by_issuer_and_serial(certs,
425 ias->issuer, ias->serial);
426 if (!signer && !(flags & PKCS7_NOINTERN)
429 X509_find_by_issuer_and_serial(p7->d.sign->cert,
430 ias->issuer, ias->serial);
432 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,
433 PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
434 sk_X509_free(signers);
438 if (!sk_X509_push(signers, signer)) {
439 sk_X509_free(signers);
446 /* Build a complete PKCS#7 enveloped data */
448 PKCS7 *PKCS7_encrypt_with_libctx(STACK_OF(X509) *certs, BIO *in,
449 const EVP_CIPHER *cipher, int flags,
450 OPENSSL_CTX *libctx, const char *propq)
457 if ((p7 = PKCS7_new_with_libctx(libctx, propq)) == NULL) {
458 PKCS7err(0, ERR_R_MALLOC_FAILURE);
462 if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
464 if (!PKCS7_set_cipher(p7, cipher)) {
465 PKCS7err(0, PKCS7_R_ERROR_SETTING_CIPHER);
469 for (i = 0; i < sk_X509_num(certs); i++) {
470 x509 = sk_X509_value(certs, i);
471 if (!PKCS7_add_recipient(p7, x509)) {
472 PKCS7err(0, PKCS7_R_ERROR_ADDING_RECIPIENT);
477 if (flags & PKCS7_STREAM)
480 if (PKCS7_final(p7, in, flags))
491 PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
494 return PKCS7_encrypt_with_libctx(certs, in, cipher, flags, NULL, NULL);
498 int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
505 PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_INVALID_NULL_POINTER);
509 if (!PKCS7_type_is_enveloped(p7)) {
510 PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_WRONG_CONTENT_TYPE);
514 if (cert && !X509_check_private_key(cert, pkey)) {
515 PKCS7err(PKCS7_F_PKCS7_DECRYPT,
516 PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
520 if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) {
521 PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR);
525 if (flags & PKCS7_TEXT) {
527 /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
528 if ((tmpbuf = BIO_new(BIO_f_buffer())) == NULL) {
529 PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
530 BIO_free_all(tmpmem);
533 if ((bread = BIO_push(tmpbuf, tmpmem)) == NULL) {
534 PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
535 BIO_free_all(tmpbuf);
536 BIO_free_all(tmpmem);
539 ret = SMIME_text(bread, data);
540 if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
541 if (!BIO_get_cipher_status(tmpmem))
547 if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
548 PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
552 i = BIO_read(tmpmem, buf, BUFFERSIZE);
555 if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
556 if (!BIO_get_cipher_status(tmpmem))
562 if (BIO_write(data, buf, i) != i) {
568 BIO_free_all(tmpmem);