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
21 DEFINE_STACK_OF(X509_ATTRIBUTE)
22 DEFINE_STACK_OF(X509_ALGOR)
23 DEFINE_STACK_OF(PKCS7_SIGNER_INFO)
25 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
27 PKCS7 *PKCS7_sign_with_libctx(X509 *signcert, EVP_PKEY *pkey,
28 STACK_OF(X509) *certs, BIO *data, int flags,
29 OPENSSL_CTX *libctx, const char *propq)
34 if ((p7 = PKCS7_new_with_libctx(libctx, propq)) == NULL) {
35 PKCS7err(0, ERR_R_MALLOC_FAILURE);
39 if (!PKCS7_set_type(p7, NID_pkcs7_signed))
42 if (!PKCS7_content_new(p7, NID_pkcs7_data))
45 if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) {
46 PKCS7err(0, PKCS7_R_PKCS7_ADD_SIGNER_ERROR);
50 if (!(flags & PKCS7_NOCERTS)) {
51 for (i = 0; i < sk_X509_num(certs); i++) {
52 if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i)))
57 if (flags & PKCS7_DETACHED)
58 PKCS7_set_detached(p7, 1);
60 if (flags & (PKCS7_STREAM | PKCS7_PARTIAL))
63 if (PKCS7_final(p7, data, flags))
71 PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
74 return PKCS7_sign_with_libctx(signcert, pkey, certs, data, flags, NULL, NULL);
78 int PKCS7_final(PKCS7 *p7, BIO *data, int flags)
83 if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
84 PKCS7err(PKCS7_F_PKCS7_FINAL, ERR_R_MALLOC_FAILURE);
88 SMIME_crlf_copy(data, p7bio, flags);
90 (void)BIO_flush(p7bio);
92 if (!PKCS7_dataFinal(p7, p7bio)) {
93 PKCS7err(PKCS7_F_PKCS7_FINAL, PKCS7_R_PKCS7_DATASIGN);
104 /* Check to see if a cipher exists and if so add S/MIME capabilities */
106 static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
108 if (EVP_get_cipherbynid(nid))
109 return PKCS7_simple_smimecap(sk, nid, arg);
113 static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
115 if (EVP_get_digestbynid(nid))
116 return PKCS7_simple_smimecap(sk, nid, arg);
120 PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
121 EVP_PKEY *pkey, const EVP_MD *md,
124 PKCS7_SIGNER_INFO *si = NULL;
125 STACK_OF(X509_ALGOR) *smcap = NULL;
127 if (!X509_check_private_key(signcert, pkey)) {
128 PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
129 PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
133 if ((si = PKCS7_add_signature(p7, signcert, pkey, md)) == NULL) {
134 PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
135 PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
139 si->ctx = pkcs7_get0_ctx(p7);
140 if (!(flags & PKCS7_NOCERTS)) {
141 if (!PKCS7_add_certificate(p7, signcert))
145 if (!(flags & PKCS7_NOATTR)) {
146 if (!PKCS7_add_attrib_content_type(si, NULL))
148 /* Add SMIMECapabilities */
149 if (!(flags & PKCS7_NOSMIMECAP)) {
150 if ((smcap = sk_X509_ALGOR_new_null()) == NULL) {
151 PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, ERR_R_MALLOC_FAILURE);
154 if (!add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
155 || !add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
156 || !add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
157 || !add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
158 || !add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
159 || !add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
160 || !add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
161 || !add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
162 || !add_cipher_smcap(smcap, NID_rc2_cbc, 128)
163 || !add_cipher_smcap(smcap, NID_rc2_cbc, 64)
164 || !add_cipher_smcap(smcap, NID_des_cbc, -1)
165 || !add_cipher_smcap(smcap, NID_rc2_cbc, 40)
166 || !PKCS7_add_attrib_smimecap(si, smcap))
168 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
171 if (flags & PKCS7_REUSE_DIGEST) {
172 if (!pkcs7_copy_existing_digest(p7, si))
174 if (!(flags & PKCS7_PARTIAL)
175 && !PKCS7_SIGNER_INFO_sign(si))
181 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
186 * Search for a digest matching SignerInfo digest type and if found copy
190 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
193 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
194 PKCS7_SIGNER_INFO *sitmp;
195 ASN1_OCTET_STRING *osdig = NULL;
196 sinfos = PKCS7_get_signer_info(p7);
197 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
198 sitmp = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
201 if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0)
203 if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) {
204 osdig = PKCS7_digest_from_attributes(sitmp->auth_attr);
211 return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length);
213 PKCS7err(PKCS7_F_PKCS7_COPY_EXISTING_DIGEST,
214 PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND);
218 int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
219 BIO *indata, BIO *out, int flags)
221 STACK_OF(X509) *signers;
223 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
224 PKCS7_SIGNER_INFO *si;
225 X509_STORE_CTX *cert_ctx = NULL;
227 int i, j = 0, k, ret = 0;
229 BIO *tmpin = NULL, *tmpout = NULL;
230 const PKCS7_CTX *p7_ctx;
233 PKCS7err(0, PKCS7_R_INVALID_NULL_POINTER);
237 if (!PKCS7_type_is_signed(p7)) {
238 PKCS7err(0, PKCS7_R_WRONG_CONTENT_TYPE);
242 /* Check for no data and no content: no data to verify signature */
243 if (PKCS7_get_detached(p7) && !indata) {
244 PKCS7err(0, PKCS7_R_NO_CONTENT);
248 if (flags & PKCS7_NO_DUAL_CONTENT) {
250 * This was originally "#if 0" because we thought that only old broken
251 * Netscape did this. It turns out that Authenticode uses this kind
252 * of "extended" PKCS7 format, and things like UEFI secure boot and
253 * tools like osslsigncode need it. In Authenticode the verification
254 * process is different, but the existing PKCs7 verification works.
256 if (!PKCS7_get_detached(p7) && indata) {
257 PKCS7err(0, PKCS7_R_CONTENT_AND_DATA_PRESENT);
262 sinfos = PKCS7_get_signer_info(p7);
264 if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
265 PKCS7err(0, PKCS7_R_NO_SIGNATURES_ON_DATA);
269 signers = PKCS7_get0_signers(p7, certs, flags);
273 /* Now verify the certificates */
274 p7_ctx = pkcs7_get0_ctx(p7);
275 cert_ctx = X509_STORE_CTX_new_with_libctx(p7_ctx->libctx, p7_ctx->propq);
276 if (cert_ctx == NULL)
278 if (!(flags & PKCS7_NOVERIFY))
279 for (k = 0; k < sk_X509_num(signers); k++) {
280 signer = sk_X509_value(signers, k);
281 if (!(flags & PKCS7_NOCHAIN)) {
282 if (!X509_STORE_CTX_init(cert_ctx, store, signer,
284 PKCS7err(0, ERR_R_X509_LIB);
287 X509_STORE_CTX_set_default(cert_ctx, "smime_sign");
288 } else if (!X509_STORE_CTX_init(cert_ctx, store, signer, NULL)) {
289 PKCS7err(0, ERR_R_X509_LIB);
292 if (!(flags & PKCS7_NOCRL))
293 X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl);
294 i = X509_verify_cert(cert_ctx);
296 j = X509_STORE_CTX_get_error(cert_ctx);
297 X509_STORE_CTX_cleanup(cert_ctx);
299 PKCS7err(0, PKCS7_R_CERTIFICATE_VERIFY_ERROR);
300 ERR_add_error_data(2, "Verify error:",
301 X509_verify_cert_error_string(j));
304 /* Check for revocation status here */
308 * Performance optimization: if the content is a memory BIO then store
309 * its contents in a temporary read only memory BIO. This avoids
310 * potentially large numbers of slow copies of data which will occur when
311 * reading from a read write memory BIO when signatures are calculated.
314 if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) {
317 len = BIO_get_mem_data(indata, &ptr);
318 tmpin = BIO_new_mem_buf(ptr, len);
320 PKCS7err(0, ERR_R_MALLOC_FAILURE);
326 if ((p7bio = PKCS7_dataInit(p7, tmpin)) == NULL)
329 if (flags & PKCS7_TEXT) {
330 if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
331 PKCS7err(0, ERR_R_MALLOC_FAILURE);
334 BIO_set_mem_eof_return(tmpout, 0);
338 /* We now have to 'read' from p7bio to calculate digests etc. */
339 if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
340 PKCS7err(0, ERR_R_MALLOC_FAILURE);
344 i = BIO_read(p7bio, buf, BUFFERSIZE);
348 BIO_write(tmpout, buf, i);
351 if (flags & PKCS7_TEXT) {
352 if (!SMIME_text(tmpout, out)) {
353 PKCS7err(0, PKCS7_R_SMIME_TEXT_ERROR);
360 /* Now Verify All Signatures */
361 if (!(flags & PKCS7_NOSIGS))
362 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
363 si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
364 signer = sk_X509_value(signers, i);
365 j = PKCS7_signatureVerify(p7bio, p7, si, signer);
367 PKCS7err(0, PKCS7_R_SIGNATURE_FAILURE);
375 X509_STORE_CTX_free(cert_ctx);
377 if (tmpin == indata) {
382 sk_X509_free(signers);
386 STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
389 STACK_OF(X509) *signers;
390 STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
391 PKCS7_SIGNER_INFO *si;
392 PKCS7_ISSUER_AND_SERIAL *ias;
397 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_INVALID_NULL_POINTER);
401 if (!PKCS7_type_is_signed(p7)) {
402 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_WRONG_CONTENT_TYPE);
406 /* Collect all the signers together */
408 sinfos = PKCS7_get_signer_info(p7);
410 if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
411 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_NO_SIGNERS);
415 if ((signers = sk_X509_new_null()) == NULL) {
416 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, ERR_R_MALLOC_FAILURE);
420 for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
421 si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
422 ias = si->issuer_and_serial;
424 /* If any certificates passed they take priority */
426 signer = X509_find_by_issuer_and_serial(certs,
427 ias->issuer, ias->serial);
428 if (!signer && !(flags & PKCS7_NOINTERN)
431 X509_find_by_issuer_and_serial(p7->d.sign->cert,
432 ias->issuer, ias->serial);
434 PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,
435 PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
436 sk_X509_free(signers);
440 if (!sk_X509_push(signers, signer)) {
441 sk_X509_free(signers);
448 /* Build a complete PKCS#7 enveloped data */
450 PKCS7 *PKCS7_encrypt_with_libctx(STACK_OF(X509) *certs, BIO *in,
451 const EVP_CIPHER *cipher, int flags,
452 OPENSSL_CTX *libctx, const char *propq)
459 if ((p7 = PKCS7_new_with_libctx(libctx, propq)) == NULL) {
460 PKCS7err(0, ERR_R_MALLOC_FAILURE);
464 if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
466 if (!PKCS7_set_cipher(p7, cipher)) {
467 PKCS7err(0, PKCS7_R_ERROR_SETTING_CIPHER);
471 for (i = 0; i < sk_X509_num(certs); i++) {
472 x509 = sk_X509_value(certs, i);
473 if (!PKCS7_add_recipient(p7, x509)) {
474 PKCS7err(0, PKCS7_R_ERROR_ADDING_RECIPIENT);
479 if (flags & PKCS7_STREAM)
482 if (PKCS7_final(p7, in, flags))
493 PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
496 return PKCS7_encrypt_with_libctx(certs, in, cipher, flags, NULL, NULL);
500 int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
507 PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_INVALID_NULL_POINTER);
511 if (!PKCS7_type_is_enveloped(p7)) {
512 PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_WRONG_CONTENT_TYPE);
516 if (cert && !X509_check_private_key(cert, pkey)) {
517 PKCS7err(PKCS7_F_PKCS7_DECRYPT,
518 PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
522 if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) {
523 PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR);
527 if (flags & PKCS7_TEXT) {
529 /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
530 if ((tmpbuf = BIO_new(BIO_f_buffer())) == NULL) {
531 PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
532 BIO_free_all(tmpmem);
535 if ((bread = BIO_push(tmpbuf, tmpmem)) == NULL) {
536 PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
537 BIO_free_all(tmpbuf);
538 BIO_free_all(tmpmem);
541 ret = SMIME_text(bread, data);
542 if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
543 if (!BIO_get_cipher_status(tmpmem))
549 if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
550 PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
554 i = BIO_read(tmpmem, buf, BUFFERSIZE);
557 if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
558 if (!BIO_get_cipher_status(tmpmem))
564 if (BIO_write(data, buf, i) != i) {
570 BIO_free_all(tmpmem);