2 * Copyright 1999-2021 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 static int pkcs7_copy_existing_digest(PKCS7
*p7
, PKCS7_SIGNER_INFO
*si
);
23 PKCS7
*PKCS7_sign_ex(X509
*signcert
, EVP_PKEY
*pkey
, STACK_OF(X509
) *certs
,
24 BIO
*data
, int flags
, OSSL_LIB_CTX
*libctx
,
30 if ((p7
= PKCS7_new_ex(libctx
, propq
)) == NULL
) {
31 ERR_raise(ERR_LIB_PKCS7
, ERR_R_MALLOC_FAILURE
);
35 if (!PKCS7_set_type(p7
, NID_pkcs7_signed
))
38 if (!PKCS7_content_new(p7
, NID_pkcs7_data
))
41 if (pkey
&& !PKCS7_sign_add_signer(p7
, signcert
, pkey
, NULL
, flags
)) {
42 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_PKCS7_ADD_SIGNER_ERROR
);
46 if (!(flags
& PKCS7_NOCERTS
)) {
47 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
48 if (!PKCS7_add_certificate(p7
, sk_X509_value(certs
, i
)))
53 if (flags
& PKCS7_DETACHED
)
54 PKCS7_set_detached(p7
, 1);
56 if (flags
& (PKCS7_STREAM
| PKCS7_PARTIAL
))
59 if (PKCS7_final(p7
, data
, flags
))
67 PKCS7
*PKCS7_sign(X509
*signcert
, EVP_PKEY
*pkey
, STACK_OF(X509
) *certs
,
70 return PKCS7_sign_ex(signcert
, pkey
, certs
, data
, flags
, NULL
, NULL
);
74 int PKCS7_final(PKCS7
*p7
, BIO
*data
, int flags
)
79 if ((p7bio
= PKCS7_dataInit(p7
, NULL
)) == NULL
) {
80 ERR_raise(ERR_LIB_PKCS7
, ERR_R_MALLOC_FAILURE
);
84 SMIME_crlf_copy(data
, p7bio
, flags
);
86 (void)BIO_flush(p7bio
);
88 if (!PKCS7_dataFinal(p7
, p7bio
)) {
89 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_PKCS7_DATASIGN
);
100 /* Check to see if a cipher exists and if so add S/MIME capabilities */
102 static int add_cipher_smcap(STACK_OF(X509_ALGOR
) *sk
, int nid
, int arg
)
104 if (EVP_get_cipherbynid(nid
))
105 return PKCS7_simple_smimecap(sk
, nid
, arg
);
109 static int add_digest_smcap(STACK_OF(X509_ALGOR
) *sk
, int nid
, int arg
)
111 if (EVP_get_digestbynid(nid
))
112 return PKCS7_simple_smimecap(sk
, nid
, arg
);
116 PKCS7_SIGNER_INFO
*PKCS7_sign_add_signer(PKCS7
*p7
, X509
*signcert
,
117 EVP_PKEY
*pkey
, const EVP_MD
*md
,
120 PKCS7_SIGNER_INFO
*si
= NULL
;
121 STACK_OF(X509_ALGOR
) *smcap
= NULL
;
123 if (!X509_check_private_key(signcert
, pkey
)) {
124 ERR_raise(ERR_LIB_PKCS7
,
125 PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE
);
129 if ((si
= PKCS7_add_signature(p7
, signcert
, pkey
, md
)) == NULL
) {
130 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR
);
134 si
->ctx
= ossl_pkcs7_get0_ctx(p7
);
135 if (!(flags
& PKCS7_NOCERTS
)) {
136 if (!PKCS7_add_certificate(p7
, signcert
))
140 if (!(flags
& PKCS7_NOATTR
)) {
141 if (!PKCS7_add_attrib_content_type(si
, NULL
))
143 /* Add SMIMECapabilities */
144 if (!(flags
& PKCS7_NOSMIMECAP
)) {
145 if ((smcap
= sk_X509_ALGOR_new_null()) == NULL
) {
146 ERR_raise(ERR_LIB_PKCS7
, ERR_R_MALLOC_FAILURE
);
149 if (!add_cipher_smcap(smcap
, NID_aes_256_cbc
, -1)
150 || !add_digest_smcap(smcap
, NID_id_GostR3411_2012_256
, -1)
151 || !add_digest_smcap(smcap
, NID_id_GostR3411_2012_512
, -1)
152 || !add_digest_smcap(smcap
, NID_id_GostR3411_94
, -1)
153 || !add_cipher_smcap(smcap
, NID_id_Gost28147_89
, -1)
154 || !add_cipher_smcap(smcap
, NID_aes_192_cbc
, -1)
155 || !add_cipher_smcap(smcap
, NID_aes_128_cbc
, -1)
156 || !add_cipher_smcap(smcap
, NID_des_ede3_cbc
, -1)
157 || !add_cipher_smcap(smcap
, NID_rc2_cbc
, 128)
158 || !add_cipher_smcap(smcap
, NID_rc2_cbc
, 64)
159 || !add_cipher_smcap(smcap
, NID_des_cbc
, -1)
160 || !add_cipher_smcap(smcap
, NID_rc2_cbc
, 40)
161 || !PKCS7_add_attrib_smimecap(si
, smcap
))
163 sk_X509_ALGOR_pop_free(smcap
, X509_ALGOR_free
);
166 if (flags
& PKCS7_REUSE_DIGEST
) {
167 if (!pkcs7_copy_existing_digest(p7
, si
))
169 if (!(flags
& PKCS7_PARTIAL
)
170 && !PKCS7_SIGNER_INFO_sign(si
))
176 sk_X509_ALGOR_pop_free(smcap
, X509_ALGOR_free
);
181 * Search for a digest matching SignerInfo digest type and if found copy
185 static int pkcs7_copy_existing_digest(PKCS7
*p7
, PKCS7_SIGNER_INFO
*si
)
188 STACK_OF(PKCS7_SIGNER_INFO
) *sinfos
;
189 PKCS7_SIGNER_INFO
*sitmp
;
190 ASN1_OCTET_STRING
*osdig
= NULL
;
191 sinfos
= PKCS7_get_signer_info(p7
);
192 for (i
= 0; i
< sk_PKCS7_SIGNER_INFO_num(sinfos
); i
++) {
193 sitmp
= sk_PKCS7_SIGNER_INFO_value(sinfos
, i
);
196 if (sk_X509_ATTRIBUTE_num(sitmp
->auth_attr
) <= 0)
198 if (!OBJ_cmp(si
->digest_alg
->algorithm
, sitmp
->digest_alg
->algorithm
)) {
199 osdig
= PKCS7_digest_from_attributes(sitmp
->auth_attr
);
206 return PKCS7_add1_attrib_digest(si
, osdig
->data
, osdig
->length
);
208 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND
);
212 int PKCS7_verify(PKCS7
*p7
, STACK_OF(X509
) *certs
, X509_STORE
*store
,
213 BIO
*indata
, BIO
*out
, int flags
)
215 STACK_OF(X509
) *signers
;
217 STACK_OF(PKCS7_SIGNER_INFO
) *sinfos
;
218 PKCS7_SIGNER_INFO
*si
;
219 X509_STORE_CTX
*cert_ctx
= NULL
;
221 int i
, j
= 0, k
, ret
= 0;
224 const PKCS7_CTX
*p7_ctx
;
227 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_INVALID_NULL_POINTER
);
231 if (!PKCS7_type_is_signed(p7
)) {
232 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_WRONG_CONTENT_TYPE
);
236 /* Check for no data and no content: no data to verify signature */
237 if (PKCS7_get_detached(p7
) && !indata
) {
238 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_NO_CONTENT
);
242 if (flags
& PKCS7_NO_DUAL_CONTENT
) {
244 * This was originally "#if 0" because we thought that only old broken
245 * Netscape did this. It turns out that Authenticode uses this kind
246 * of "extended" PKCS7 format, and things like UEFI secure boot and
247 * tools like osslsigncode need it. In Authenticode the verification
248 * process is different, but the existing PKCs7 verification works.
250 if (!PKCS7_get_detached(p7
) && indata
) {
251 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_CONTENT_AND_DATA_PRESENT
);
256 sinfos
= PKCS7_get_signer_info(p7
);
258 if (!sinfos
|| !sk_PKCS7_SIGNER_INFO_num(sinfos
)) {
259 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_NO_SIGNATURES_ON_DATA
);
263 signers
= PKCS7_get0_signers(p7
, certs
, flags
);
267 /* Now verify the certificates */
268 p7_ctx
= ossl_pkcs7_get0_ctx(p7
);
269 cert_ctx
= X509_STORE_CTX_new_ex(ossl_pkcs7_ctx_get0_libctx(p7_ctx
),
270 ossl_pkcs7_ctx_get0_propq(p7_ctx
));
271 if (cert_ctx
== NULL
)
273 if (!(flags
& PKCS7_NOVERIFY
))
274 for (k
= 0; k
< sk_X509_num(signers
); k
++) {
275 signer
= sk_X509_value(signers
, k
);
276 if (!(flags
& PKCS7_NOCHAIN
)) {
277 if (!X509_STORE_CTX_init(cert_ctx
, store
, signer
,
279 ERR_raise(ERR_LIB_PKCS7
, ERR_R_X509_LIB
);
282 X509_STORE_CTX_set_default(cert_ctx
, "smime_sign");
283 } else if (!X509_STORE_CTX_init(cert_ctx
, store
, signer
, NULL
)) {
284 ERR_raise(ERR_LIB_PKCS7
, ERR_R_X509_LIB
);
287 if (!(flags
& PKCS7_NOCRL
))
288 X509_STORE_CTX_set0_crls(cert_ctx
, p7
->d
.sign
->crl
);
289 i
= X509_verify_cert(cert_ctx
);
291 j
= X509_STORE_CTX_get_error(cert_ctx
);
293 ERR_raise_data(ERR_LIB_PKCS7
, PKCS7_R_CERTIFICATE_VERIFY_ERROR
,
295 X509_verify_cert_error_string(j
));
298 /* Check for revocation status here */
301 if ((p7bio
= PKCS7_dataInit(p7
, indata
)) == NULL
)
304 if (flags
& PKCS7_TEXT
) {
305 if ((tmpout
= BIO_new(BIO_s_mem())) == NULL
) {
306 ERR_raise(ERR_LIB_PKCS7
, ERR_R_MALLOC_FAILURE
);
309 BIO_set_mem_eof_return(tmpout
, 0);
313 /* We now have to 'read' from p7bio to calculate digests etc. */
314 if ((buf
= OPENSSL_malloc(BUFFERSIZE
)) == NULL
) {
315 ERR_raise(ERR_LIB_PKCS7
, ERR_R_MALLOC_FAILURE
);
319 i
= BIO_read(p7bio
, buf
, BUFFERSIZE
);
323 BIO_write(tmpout
, buf
, i
);
326 if (flags
& PKCS7_TEXT
) {
327 if (!SMIME_text(tmpout
, out
)) {
328 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_SMIME_TEXT_ERROR
);
335 /* Now Verify All Signatures */
336 if (!(flags
& PKCS7_NOSIGS
))
337 for (i
= 0; i
< sk_PKCS7_SIGNER_INFO_num(sinfos
); i
++) {
338 si
= sk_PKCS7_SIGNER_INFO_value(sinfos
, i
);
339 signer
= sk_X509_value(signers
, i
);
340 j
= PKCS7_signatureVerify(p7bio
, p7
, si
, signer
);
342 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_SIGNATURE_FAILURE
);
350 X509_STORE_CTX_free(cert_ctx
);
355 sk_X509_free(signers
);
359 STACK_OF(X509
) *PKCS7_get0_signers(PKCS7
*p7
, STACK_OF(X509
) *certs
,
362 STACK_OF(X509
) *signers
;
363 STACK_OF(PKCS7_SIGNER_INFO
) *sinfos
;
364 PKCS7_SIGNER_INFO
*si
;
365 PKCS7_ISSUER_AND_SERIAL
*ias
;
370 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_INVALID_NULL_POINTER
);
374 if (!PKCS7_type_is_signed(p7
)) {
375 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_WRONG_CONTENT_TYPE
);
379 /* Collect all the signers together */
381 sinfos
= PKCS7_get_signer_info(p7
);
383 if (sk_PKCS7_SIGNER_INFO_num(sinfos
) <= 0) {
384 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_NO_SIGNERS
);
388 if ((signers
= sk_X509_new_null()) == NULL
) {
389 ERR_raise(ERR_LIB_PKCS7
, ERR_R_MALLOC_FAILURE
);
393 for (i
= 0; i
< sk_PKCS7_SIGNER_INFO_num(sinfos
); i
++) {
394 si
= sk_PKCS7_SIGNER_INFO_value(sinfos
, i
);
395 ias
= si
->issuer_and_serial
;
397 /* If any certificates passed they take priority */
399 signer
= X509_find_by_issuer_and_serial(certs
,
400 ias
->issuer
, ias
->serial
);
401 if (!signer
&& !(flags
& PKCS7_NOINTERN
)
404 X509_find_by_issuer_and_serial(p7
->d
.sign
->cert
,
405 ias
->issuer
, ias
->serial
);
407 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND
);
408 sk_X509_free(signers
);
412 if (!sk_X509_push(signers
, signer
)) {
413 sk_X509_free(signers
);
420 /* Build a complete PKCS#7 enveloped data */
422 PKCS7
*PKCS7_encrypt_ex(STACK_OF(X509
) *certs
, BIO
*in
,
423 const EVP_CIPHER
*cipher
, int flags
,
424 OSSL_LIB_CTX
*libctx
, const char *propq
)
431 if ((p7
= PKCS7_new_ex(libctx
, propq
)) == NULL
) {
432 ERR_raise(ERR_LIB_PKCS7
, ERR_R_MALLOC_FAILURE
);
436 if (!PKCS7_set_type(p7
, NID_pkcs7_enveloped
))
438 if (!PKCS7_set_cipher(p7
, cipher
)) {
439 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_ERROR_SETTING_CIPHER
);
443 for (i
= 0; i
< sk_X509_num(certs
); i
++) {
444 x509
= sk_X509_value(certs
, i
);
445 if (!PKCS7_add_recipient(p7
, x509
)) {
446 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_ERROR_ADDING_RECIPIENT
);
451 if (flags
& PKCS7_STREAM
)
454 if (PKCS7_final(p7
, in
, flags
))
465 PKCS7
*PKCS7_encrypt(STACK_OF(X509
) *certs
, BIO
*in
, const EVP_CIPHER
*cipher
,
468 return PKCS7_encrypt_ex(certs
, in
, cipher
, flags
, NULL
, NULL
);
472 int PKCS7_decrypt(PKCS7
*p7
, EVP_PKEY
*pkey
, X509
*cert
, BIO
*data
, int flags
)
479 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_INVALID_NULL_POINTER
);
483 if (!PKCS7_type_is_enveloped(p7
)) {
484 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_WRONG_CONTENT_TYPE
);
488 if (cert
&& !X509_check_private_key(cert
, pkey
)) {
489 ERR_raise(ERR_LIB_PKCS7
,
490 PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE
);
494 if ((tmpmem
= PKCS7_dataDecode(p7
, pkey
, NULL
, cert
)) == NULL
) {
495 ERR_raise(ERR_LIB_PKCS7
, PKCS7_R_DECRYPT_ERROR
);
499 if (flags
& PKCS7_TEXT
) {
501 /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
502 if ((tmpbuf
= BIO_new(BIO_f_buffer())) == NULL
) {
503 ERR_raise(ERR_LIB_PKCS7
, ERR_R_MALLOC_FAILURE
);
504 BIO_free_all(tmpmem
);
507 if ((bread
= BIO_push(tmpbuf
, tmpmem
)) == NULL
) {
508 ERR_raise(ERR_LIB_PKCS7
, ERR_R_MALLOC_FAILURE
);
509 BIO_free_all(tmpbuf
);
510 BIO_free_all(tmpmem
);
513 ret
= SMIME_text(bread
, data
);
514 if (ret
> 0 && BIO_method_type(tmpmem
) == BIO_TYPE_CIPHER
) {
515 if (BIO_get_cipher_status(tmpmem
) <= 0)
521 if ((buf
= OPENSSL_malloc(BUFFERSIZE
)) == NULL
) {
522 ERR_raise(ERR_LIB_PKCS7
, ERR_R_MALLOC_FAILURE
);
526 i
= BIO_read(tmpmem
, buf
, BUFFERSIZE
);
529 if (BIO_method_type(tmpmem
) == BIO_TYPE_CIPHER
) {
530 if (BIO_get_cipher_status(tmpmem
) <= 0)
536 if (BIO_write(data
, buf
, i
) != i
) {
542 BIO_free_all(tmpmem
);