2 * Copyright 1995-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
11 #include "internal/cryptlib.h"
12 #include <openssl/rand.h>
13 #include <openssl/objects.h>
14 #include <openssl/x509.h>
15 #include <openssl/x509v3.h>
16 #include <openssl/err.h>
18 DEFINE_STACK_OF(X509_ALGOR
)
19 DEFINE_STACK_OF(X509_ATTRIBUTE
)
20 DEFINE_STACK_OF(PKCS7_RECIP_INFO
)
21 DEFINE_STACK_OF(PKCS7_SIGNER_INFO
)
23 static int add_attribute(STACK_OF(X509_ATTRIBUTE
) **sk
, int nid
, int atrtype
,
25 static ASN1_TYPE
*get_attribute(STACK_OF(X509_ATTRIBUTE
) *sk
, int nid
);
27 static int PKCS7_type_is_other(PKCS7
*p7
)
31 int nid
= OBJ_obj2nid(p7
->type
);
35 case NID_pkcs7_signed
:
36 case NID_pkcs7_enveloped
:
37 case NID_pkcs7_signedAndEnveloped
:
38 case NID_pkcs7_digest
:
39 case NID_pkcs7_encrypted
:
50 static ASN1_OCTET_STRING
*PKCS7_get_octet_string(PKCS7
*p7
)
52 if (PKCS7_type_is_data(p7
))
54 if (PKCS7_type_is_other(p7
) && p7
->d
.other
55 && (p7
->d
.other
->type
== V_ASN1_OCTET_STRING
))
56 return p7
->d
.other
->value
.octet_string
;
60 static int PKCS7_bio_add_digest(BIO
**pbio
, X509_ALGOR
*alg
)
64 if ((btmp
= BIO_new(BIO_f_md())) == NULL
) {
65 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST
, ERR_R_BIO_LIB
);
69 md
= EVP_get_digestbyobj(alg
->algorithm
);
71 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST
, PKCS7_R_UNKNOWN_DIGEST_TYPE
);
78 else if (!BIO_push(*pbio
, btmp
)) {
79 PKCS7err(PKCS7_F_PKCS7_BIO_ADD_DIGEST
, ERR_R_BIO_LIB
);
92 static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO
*ri
,
93 unsigned char *key
, int keylen
)
95 EVP_PKEY_CTX
*pctx
= NULL
;
96 EVP_PKEY
*pkey
= NULL
;
97 unsigned char *ek
= NULL
;
101 pkey
= X509_get0_pubkey(ri
->cert
);
105 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
109 if (EVP_PKEY_encrypt_init(pctx
) <= 0)
112 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_ENCRYPT
,
113 EVP_PKEY_CTRL_PKCS7_ENCRYPT
, 0, ri
) <= 0) {
114 PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO
, PKCS7_R_CTRL_ERROR
);
118 if (EVP_PKEY_encrypt(pctx
, NULL
, &eklen
, key
, keylen
) <= 0)
121 ek
= OPENSSL_malloc(eklen
);
124 PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO
, ERR_R_MALLOC_FAILURE
);
128 if (EVP_PKEY_encrypt(pctx
, ek
, &eklen
, key
, keylen
) <= 0)
131 ASN1_STRING_set0(ri
->enc_key
, ek
, eklen
);
137 EVP_PKEY_CTX_free(pctx
);
143 static int pkcs7_decrypt_rinfo(unsigned char **pek
, int *peklen
,
144 PKCS7_RECIP_INFO
*ri
, EVP_PKEY
*pkey
,
147 EVP_PKEY_CTX
*pctx
= NULL
;
148 unsigned char *ek
= NULL
;
152 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
156 if (EVP_PKEY_decrypt_init(pctx
) <= 0)
159 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_DECRYPT
,
160 EVP_PKEY_CTRL_PKCS7_DECRYPT
, 0, ri
) <= 0) {
161 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO
, PKCS7_R_CTRL_ERROR
);
165 if (EVP_PKEY_decrypt(pctx
, NULL
, &eklen
,
166 ri
->enc_key
->data
, ri
->enc_key
->length
) <= 0)
169 ek
= OPENSSL_malloc(eklen
);
172 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO
, ERR_R_MALLOC_FAILURE
);
176 if (EVP_PKEY_decrypt(pctx
, ek
, &eklen
,
177 ri
->enc_key
->data
, ri
->enc_key
->length
) <= 0
179 || (fixlen
!= 0 && eklen
!= fixlen
)) {
181 PKCS7err(PKCS7_F_PKCS7_DECRYPT_RINFO
, ERR_R_EVP_LIB
);
187 OPENSSL_clear_free(*pek
, *peklen
);
192 EVP_PKEY_CTX_free(pctx
);
199 BIO
*PKCS7_dataInit(PKCS7
*p7
, BIO
*bio
)
202 BIO
*out
= NULL
, *btmp
= NULL
;
203 X509_ALGOR
*xa
= NULL
;
204 const EVP_CIPHER
*evp_cipher
= NULL
;
205 STACK_OF(X509_ALGOR
) *md_sk
= NULL
;
206 STACK_OF(PKCS7_RECIP_INFO
) *rsk
= NULL
;
207 X509_ALGOR
*xalg
= NULL
;
208 PKCS7_RECIP_INFO
*ri
= NULL
;
209 ASN1_OCTET_STRING
*os
= NULL
;
212 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, PKCS7_R_INVALID_NULL_POINTER
);
216 * The content field in the PKCS7 ContentInfo is optional, but that really
217 * only applies to inner content (precisely, detached signatures).
219 * When reading content, missing outer content is therefore treated as an
222 * When creating content, PKCS7_content_new() must be called before
223 * calling this method, so a NULL p7->d is always an error.
225 if (p7
->d
.ptr
== NULL
) {
226 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, PKCS7_R_NO_CONTENT
);
230 i
= OBJ_obj2nid(p7
->type
);
231 p7
->state
= PKCS7_S_HEADER
;
234 case NID_pkcs7_signed
:
235 md_sk
= p7
->d
.sign
->md_algs
;
236 os
= PKCS7_get_octet_string(p7
->d
.sign
->contents
);
238 case NID_pkcs7_signedAndEnveloped
:
239 rsk
= p7
->d
.signed_and_enveloped
->recipientinfo
;
240 md_sk
= p7
->d
.signed_and_enveloped
->md_algs
;
241 xalg
= p7
->d
.signed_and_enveloped
->enc_data
->algorithm
;
242 evp_cipher
= p7
->d
.signed_and_enveloped
->enc_data
->cipher
;
243 if (evp_cipher
== NULL
) {
244 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, PKCS7_R_CIPHER_NOT_INITIALIZED
);
248 case NID_pkcs7_enveloped
:
249 rsk
= p7
->d
.enveloped
->recipientinfo
;
250 xalg
= p7
->d
.enveloped
->enc_data
->algorithm
;
251 evp_cipher
= p7
->d
.enveloped
->enc_data
->cipher
;
252 if (evp_cipher
== NULL
) {
253 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, PKCS7_R_CIPHER_NOT_INITIALIZED
);
257 case NID_pkcs7_digest
:
258 xa
= p7
->d
.digest
->md
;
259 os
= PKCS7_get_octet_string(p7
->d
.digest
->contents
);
264 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, PKCS7_R_UNSUPPORTED_CONTENT_TYPE
);
268 for (i
= 0; i
< sk_X509_ALGOR_num(md_sk
); i
++)
269 if (!PKCS7_bio_add_digest(&out
, sk_X509_ALGOR_value(md_sk
, i
)))
272 if (xa
&& !PKCS7_bio_add_digest(&out
, xa
))
275 if (evp_cipher
!= NULL
) {
276 unsigned char key
[EVP_MAX_KEY_LENGTH
];
277 unsigned char iv
[EVP_MAX_IV_LENGTH
];
281 if ((btmp
= BIO_new(BIO_f_cipher())) == NULL
) {
282 PKCS7err(PKCS7_F_PKCS7_DATAINIT
, ERR_R_BIO_LIB
);
285 BIO_get_cipher_ctx(btmp
, &ctx
);
286 keylen
= EVP_CIPHER_key_length(evp_cipher
);
287 ivlen
= EVP_CIPHER_iv_length(evp_cipher
);
288 xalg
->algorithm
= OBJ_nid2obj(EVP_CIPHER_type(evp_cipher
));
290 if (RAND_bytes(iv
, ivlen
) <= 0)
292 if (EVP_CipherInit_ex(ctx
, evp_cipher
, NULL
, NULL
, NULL
, 1) <= 0)
294 if (EVP_CIPHER_CTX_rand_key(ctx
, key
) <= 0)
296 if (EVP_CipherInit_ex(ctx
, NULL
, NULL
, key
, iv
, 1) <= 0)
300 if (xalg
->parameter
== NULL
) {
301 xalg
->parameter
= ASN1_TYPE_new();
302 if (xalg
->parameter
== NULL
)
305 if (EVP_CIPHER_param_to_asn1(ctx
, xalg
->parameter
) < 0)
309 /* Lets do the pub key stuff :-) */
310 for (i
= 0; i
< sk_PKCS7_RECIP_INFO_num(rsk
); i
++) {
311 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, i
);
312 if (pkcs7_encode_rinfo(ri
, key
, keylen
) <= 0)
315 OPENSSL_cleanse(key
, keylen
);
325 if (PKCS7_is_detached(p7
)) {
326 bio
= BIO_new(BIO_s_null());
327 } else if (os
&& os
->length
> 0) {
328 bio
= BIO_new_mem_buf(os
->data
, os
->length
);
330 bio
= BIO_new(BIO_s_mem());
333 BIO_set_mem_eof_return(bio
, 0);
350 static int pkcs7_cmp_ri(PKCS7_RECIP_INFO
*ri
, X509
*pcert
)
353 ret
= X509_NAME_cmp(ri
->issuer_and_serial
->issuer
,
354 X509_get_issuer_name(pcert
));
357 return ASN1_INTEGER_cmp(X509_get_serialNumber(pcert
),
358 ri
->issuer_and_serial
->serial
);
362 BIO
*PKCS7_dataDecode(PKCS7
*p7
, EVP_PKEY
*pkey
, BIO
*in_bio
, X509
*pcert
)
365 BIO
*out
= NULL
, *btmp
= NULL
, *etmp
= NULL
, *bio
= NULL
;
367 ASN1_OCTET_STRING
*data_body
= NULL
;
368 const EVP_MD
*evp_md
;
369 const EVP_CIPHER
*evp_cipher
= NULL
;
370 EVP_CIPHER_CTX
*evp_ctx
= NULL
;
371 X509_ALGOR
*enc_alg
= NULL
;
372 STACK_OF(X509_ALGOR
) *md_sk
= NULL
;
373 STACK_OF(PKCS7_RECIP_INFO
) *rsk
= NULL
;
374 PKCS7_RECIP_INFO
*ri
= NULL
;
375 unsigned char *ek
= NULL
, *tkey
= NULL
;
376 int eklen
= 0, tkeylen
= 0;
379 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, PKCS7_R_INVALID_NULL_POINTER
);
383 if (p7
->d
.ptr
== NULL
) {
384 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, PKCS7_R_NO_CONTENT
);
388 i
= OBJ_obj2nid(p7
->type
);
389 p7
->state
= PKCS7_S_HEADER
;
392 case NID_pkcs7_signed
:
394 * p7->d.sign->contents is a PKCS7 structure consisting of a contentType
395 * field and optional content.
396 * data_body is NULL if that structure has no (=detached) content
397 * or if the contentType is wrong (i.e., not "data").
399 data_body
= PKCS7_get_octet_string(p7
->d
.sign
->contents
);
400 if (!PKCS7_is_detached(p7
) && data_body
== NULL
) {
401 PKCS7err(PKCS7_F_PKCS7_DATADECODE
,
402 PKCS7_R_INVALID_SIGNED_DATA_TYPE
);
405 md_sk
= p7
->d
.sign
->md_algs
;
407 case NID_pkcs7_signedAndEnveloped
:
408 rsk
= p7
->d
.signed_and_enveloped
->recipientinfo
;
409 md_sk
= p7
->d
.signed_and_enveloped
->md_algs
;
410 /* data_body is NULL if the optional EncryptedContent is missing. */
411 data_body
= p7
->d
.signed_and_enveloped
->enc_data
->enc_data
;
412 enc_alg
= p7
->d
.signed_and_enveloped
->enc_data
->algorithm
;
413 evp_cipher
= EVP_get_cipherbyobj(enc_alg
->algorithm
);
414 if (evp_cipher
== NULL
) {
415 PKCS7err(PKCS7_F_PKCS7_DATADECODE
,
416 PKCS7_R_UNSUPPORTED_CIPHER_TYPE
);
420 case NID_pkcs7_enveloped
:
421 rsk
= p7
->d
.enveloped
->recipientinfo
;
422 enc_alg
= p7
->d
.enveloped
->enc_data
->algorithm
;
423 /* data_body is NULL if the optional EncryptedContent is missing. */
424 data_body
= p7
->d
.enveloped
->enc_data
->enc_data
;
425 evp_cipher
= EVP_get_cipherbyobj(enc_alg
->algorithm
);
426 if (evp_cipher
== NULL
) {
427 PKCS7err(PKCS7_F_PKCS7_DATADECODE
,
428 PKCS7_R_UNSUPPORTED_CIPHER_TYPE
);
433 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, PKCS7_R_UNSUPPORTED_CONTENT_TYPE
);
437 /* Detached content must be supplied via in_bio instead. */
438 if (data_body
== NULL
&& in_bio
== NULL
) {
439 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, PKCS7_R_NO_CONTENT
);
443 /* We will be checking the signature */
445 for (i
= 0; i
< sk_X509_ALGOR_num(md_sk
); i
++) {
446 xa
= sk_X509_ALGOR_value(md_sk
, i
);
447 if ((btmp
= BIO_new(BIO_f_md())) == NULL
) {
448 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, ERR_R_BIO_LIB
);
452 j
= OBJ_obj2nid(xa
->algorithm
);
453 evp_md
= EVP_get_digestbynid(j
);
454 if (evp_md
== NULL
) {
455 PKCS7err(PKCS7_F_PKCS7_DATADECODE
,
456 PKCS7_R_UNKNOWN_DIGEST_TYPE
);
460 BIO_set_md(btmp
, evp_md
);
469 if (evp_cipher
!= NULL
) {
470 if ((etmp
= BIO_new(BIO_f_cipher())) == NULL
) {
471 PKCS7err(PKCS7_F_PKCS7_DATADECODE
, ERR_R_BIO_LIB
);
476 * It was encrypted, we need to decrypt the secret key with the
481 * Find the recipientInfo which matches the passed certificate (if
486 for (i
= 0; i
< sk_PKCS7_RECIP_INFO_num(rsk
); i
++) {
487 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, i
);
488 if (!pkcs7_cmp_ri(ri
, pcert
))
493 PKCS7err(PKCS7_F_PKCS7_DATADECODE
,
494 PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE
);
499 /* If we haven't got a certificate try each ri in turn */
502 * Always attempt to decrypt all rinfo even after success as a
503 * defence against MMA timing attacks.
505 for (i
= 0; i
< sk_PKCS7_RECIP_INFO_num(rsk
); i
++) {
506 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, i
);
508 if (pkcs7_decrypt_rinfo(&ek
, &eklen
, ri
, pkey
,
509 EVP_CIPHER_key_length(evp_cipher
)) < 0)
514 /* Only exit on fatal errors, not decrypt failure */
515 if (pkcs7_decrypt_rinfo(&ek
, &eklen
, ri
, pkey
, 0) < 0)
521 BIO_get_cipher_ctx(etmp
, &evp_ctx
);
522 if (EVP_CipherInit_ex(evp_ctx
, evp_cipher
, NULL
, NULL
, NULL
, 0) <= 0)
524 if (EVP_CIPHER_asn1_to_param(evp_ctx
, enc_alg
->parameter
) < 0)
526 /* Generate random key as MMA defence */
527 tkeylen
= EVP_CIPHER_CTX_key_length(evp_ctx
);
528 tkey
= OPENSSL_malloc(tkeylen
);
531 if (EVP_CIPHER_CTX_rand_key(evp_ctx
, tkey
) <= 0)
539 if (eklen
!= EVP_CIPHER_CTX_key_length(evp_ctx
)) {
541 * Some S/MIME clients don't use the same key and effective key
542 * length. The key length is determined by the size of the
545 if (!EVP_CIPHER_CTX_set_key_length(evp_ctx
, eklen
)) {
546 /* Use random key as MMA defence */
547 OPENSSL_clear_free(ek
, eklen
);
553 /* Clear errors so we don't leak information useful in MMA */
555 if (EVP_CipherInit_ex(evp_ctx
, NULL
, NULL
, ek
, NULL
, 0) <= 0)
558 OPENSSL_clear_free(ek
, eklen
);
560 OPENSSL_clear_free(tkey
, tkeylen
);
569 if (in_bio
!= NULL
) {
572 if (data_body
->length
> 0)
573 bio
= BIO_new_mem_buf(data_body
->data
, data_body
->length
);
575 bio
= BIO_new(BIO_s_mem());
578 BIO_set_mem_eof_return(bio
, 0);
588 OPENSSL_clear_free(ek
, eklen
);
589 OPENSSL_clear_free(tkey
, tkeylen
);
597 static BIO
*PKCS7_find_digest(EVP_MD_CTX
**pmd
, BIO
*bio
, int nid
)
600 bio
= BIO_find_type(bio
, BIO_TYPE_MD
);
602 PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST
,
603 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST
);
606 BIO_get_md_ctx(bio
, pmd
);
608 PKCS7err(PKCS7_F_PKCS7_FIND_DIGEST
, ERR_R_INTERNAL_ERROR
);
611 if (EVP_MD_CTX_type(*pmd
) == nid
)
618 static int do_pkcs7_signed_attrib(PKCS7_SIGNER_INFO
*si
, EVP_MD_CTX
*mctx
)
620 unsigned char md_data
[EVP_MAX_MD_SIZE
];
623 /* Add signing time if not already present */
624 if (!PKCS7_get_signed_attribute(si
, NID_pkcs9_signingTime
)) {
625 if (!PKCS7_add0_attrib_signing_time(si
, NULL
)) {
626 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
, ERR_R_MALLOC_FAILURE
);
632 if (!EVP_DigestFinal_ex(mctx
, md_data
, &md_len
)) {
633 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
, ERR_R_EVP_LIB
);
636 if (!PKCS7_add1_attrib_digest(si
, md_data
, md_len
)) {
637 PKCS7err(PKCS7_F_DO_PKCS7_SIGNED_ATTRIB
, ERR_R_MALLOC_FAILURE
);
641 /* Now sign the attributes */
642 if (!PKCS7_SIGNER_INFO_sign(si
))
648 int PKCS7_dataFinal(PKCS7
*p7
, BIO
*bio
)
653 PKCS7_SIGNER_INFO
*si
;
654 EVP_MD_CTX
*mdc
, *ctx_tmp
;
655 STACK_OF(X509_ATTRIBUTE
) *sk
;
656 STACK_OF(PKCS7_SIGNER_INFO
) *si_sk
= NULL
;
657 ASN1_OCTET_STRING
*os
= NULL
;
660 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, PKCS7_R_INVALID_NULL_POINTER
);
664 if (p7
->d
.ptr
== NULL
) {
665 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, PKCS7_R_NO_CONTENT
);
669 ctx_tmp
= EVP_MD_CTX_new();
670 if (ctx_tmp
== NULL
) {
671 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, ERR_R_MALLOC_FAILURE
);
675 i
= OBJ_obj2nid(p7
->type
);
676 p7
->state
= PKCS7_S_HEADER
;
682 case NID_pkcs7_signedAndEnveloped
:
683 /* XXXXXXXXXXXXXXXX */
684 si_sk
= p7
->d
.signed_and_enveloped
->signer_info
;
685 os
= p7
->d
.signed_and_enveloped
->enc_data
->enc_data
;
687 os
= ASN1_OCTET_STRING_new();
689 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, ERR_R_MALLOC_FAILURE
);
692 p7
->d
.signed_and_enveloped
->enc_data
->enc_data
= os
;
695 case NID_pkcs7_enveloped
:
696 /* XXXXXXXXXXXXXXXX */
697 os
= p7
->d
.enveloped
->enc_data
->enc_data
;
699 os
= ASN1_OCTET_STRING_new();
701 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, ERR_R_MALLOC_FAILURE
);
704 p7
->d
.enveloped
->enc_data
->enc_data
= os
;
707 case NID_pkcs7_signed
:
708 si_sk
= p7
->d
.sign
->signer_info
;
709 os
= PKCS7_get_octet_string(p7
->d
.sign
->contents
);
710 /* If detached data then the content is excluded */
711 if (PKCS7_type_is_data(p7
->d
.sign
->contents
) && p7
->detached
) {
712 ASN1_OCTET_STRING_free(os
);
714 p7
->d
.sign
->contents
->d
.data
= NULL
;
718 case NID_pkcs7_digest
:
719 os
= PKCS7_get_octet_string(p7
->d
.digest
->contents
);
720 /* If detached data then the content is excluded */
721 if (PKCS7_type_is_data(p7
->d
.digest
->contents
) && p7
->detached
) {
722 ASN1_OCTET_STRING_free(os
);
724 p7
->d
.digest
->contents
->d
.data
= NULL
;
729 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, PKCS7_R_UNSUPPORTED_CONTENT_TYPE
);
734 for (i
= 0; i
< sk_PKCS7_SIGNER_INFO_num(si_sk
); i
++) {
735 si
= sk_PKCS7_SIGNER_INFO_value(si_sk
, i
);
736 if (si
->pkey
== NULL
)
739 j
= OBJ_obj2nid(si
->digest_alg
->algorithm
);
743 btmp
= PKCS7_find_digest(&mdc
, btmp
, j
);
749 * We now have the EVP_MD_CTX, lets do the signing.
751 if (!EVP_MD_CTX_copy_ex(ctx_tmp
, mdc
))
757 * If there are attributes, we add the digest attribute and only
758 * sign the attributes
760 if (sk_X509_ATTRIBUTE_num(sk
) > 0) {
761 if (!do_pkcs7_signed_attrib(si
, ctx_tmp
))
764 unsigned char *abuf
= NULL
;
765 unsigned int abuflen
;
766 abuflen
= EVP_PKEY_size(si
->pkey
);
767 abuf
= OPENSSL_malloc(abuflen
);
771 if (!EVP_SignFinal(ctx_tmp
, abuf
, &abuflen
, si
->pkey
)) {
773 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, ERR_R_EVP_LIB
);
776 ASN1_STRING_set0(si
->enc_digest
, abuf
, abuflen
);
779 } else if (i
== NID_pkcs7_digest
) {
780 unsigned char md_data
[EVP_MAX_MD_SIZE
];
782 if (!PKCS7_find_digest(&mdc
, bio
,
783 OBJ_obj2nid(p7
->d
.digest
->md
->algorithm
)))
785 if (!EVP_DigestFinal_ex(mdc
, md_data
, &md_len
))
787 if (!ASN1_OCTET_STRING_set(p7
->d
.digest
->digest
, md_data
, md_len
))
791 if (!PKCS7_is_detached(p7
)) {
793 * NOTE(emilia): I think we only reach os == NULL here because detached
794 * digested data support is broken.
798 if (!(os
->flags
& ASN1_STRING_FLAG_NDEF
)) {
801 btmp
= BIO_find_type(bio
, BIO_TYPE_MEM
);
803 PKCS7err(PKCS7_F_PKCS7_DATAFINAL
, PKCS7_R_UNABLE_TO_FIND_MEM_BIO
);
806 contlen
= BIO_get_mem_data(btmp
, &cont
);
808 * Mark the BIO read only then we can use its copy of the data
809 * instead of making an extra copy.
811 BIO_set_flags(btmp
, BIO_FLAGS_MEM_RDONLY
);
812 BIO_set_mem_eof_return(btmp
, 0);
813 ASN1_STRING_set0(os
, (unsigned char *)cont
, contlen
);
818 EVP_MD_CTX_free(ctx_tmp
);
822 int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO
*si
)
825 EVP_PKEY_CTX
*pctx
= NULL
;
826 unsigned char *abuf
= NULL
;
829 const EVP_MD
*md
= NULL
;
831 md
= EVP_get_digestbyobj(si
->digest_alg
->algorithm
);
835 mctx
= EVP_MD_CTX_new();
837 PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN
, ERR_R_MALLOC_FAILURE
);
841 if (EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, si
->pkey
) <= 0)
845 * TODO(3.0): This causes problems when providers are in use, so disabled
846 * for now. Can we get rid of this completely? AFAICT this ctrl has never
847 * been used since it was first put in. All internal implementations just
848 * return 1 and ignore this ctrl and have always done so by the looks of
849 * things. To fix this we could convert this ctrl into a param, which would
850 * require us to send all the signer info data as a set of params...but that
851 * is non-trivial and since this isn't used by anything it may be better
852 * just to remove it. The original commit that added it had this
853 * justification in CHANGES:
855 * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
856 * EVP_PKEY_METHOD before and after signing via the
857 * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
858 * before and/or after signing if necessary."
861 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
862 EVP_PKEY_CTRL_PKCS7_SIGN
, 0, si
) <= 0) {
863 PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN
, PKCS7_R_CTRL_ERROR
);
868 alen
= ASN1_item_i2d((ASN1_VALUE
*)si
->auth_attr
, &abuf
,
869 ASN1_ITEM_rptr(PKCS7_ATTR_SIGN
));
872 if (EVP_DigestSignUpdate(mctx
, abuf
, alen
) <= 0)
876 if (EVP_DigestSignFinal(mctx
, NULL
, &siglen
) <= 0)
878 abuf
= OPENSSL_malloc(siglen
);
881 if (EVP_DigestSignFinal(mctx
, abuf
, &siglen
) <= 0)
885 * TODO(3.0): This causes problems when providers are in use, so disabled
886 * for now. Can we get rid of this completely? AFAICT this ctrl has never
887 * been used since it was first put in. All internal implementations just
888 * return 1 and ignore this ctrl and have always done so by the looks of
889 * things. To fix this we could convert this ctrl into a param, which would
890 * require us to send all the signer info data as a set of params...but that
891 * is non-trivial and since this isn't used by anything it may be better
892 * just to remove it. The original commit that added it had this
893 * justification in CHANGES:
895 * "During PKCS7 signing pass the PKCS7 SignerInfo structure to the
896 * EVP_PKEY_METHOD before and after signing via the
897 * EVP_PKEY_CTRL_PKCS7_SIGN ctrl. It can then customise the structure
898 * before and/or after signing if necessary."
901 if (EVP_PKEY_CTX_ctrl(pctx
, -1, EVP_PKEY_OP_SIGN
,
902 EVP_PKEY_CTRL_PKCS7_SIGN
, 1, si
) <= 0) {
903 PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN
, PKCS7_R_CTRL_ERROR
);
908 EVP_MD_CTX_free(mctx
);
910 ASN1_STRING_set0(si
->enc_digest
, abuf
, siglen
);
916 EVP_MD_CTX_free(mctx
);
921 int PKCS7_dataVerify(X509_STORE
*cert_store
, X509_STORE_CTX
*ctx
, BIO
*bio
,
922 PKCS7
*p7
, PKCS7_SIGNER_INFO
*si
)
924 PKCS7_ISSUER_AND_SERIAL
*ias
;
926 STACK_OF(X509
) *cert
;
930 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
, PKCS7_R_INVALID_NULL_POINTER
);
934 if (p7
->d
.ptr
== NULL
) {
935 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
, PKCS7_R_NO_CONTENT
);
939 if (PKCS7_type_is_signed(p7
)) {
940 cert
= p7
->d
.sign
->cert
;
941 } else if (PKCS7_type_is_signedAndEnveloped(p7
)) {
942 cert
= p7
->d
.signed_and_enveloped
->cert
;
944 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
, PKCS7_R_WRONG_PKCS7_TYPE
);
947 /* XXXXXXXXXXXXXXXXXXXXXXX */
948 ias
= si
->issuer_and_serial
;
950 x509
= X509_find_by_issuer_and_serial(cert
, ias
->issuer
, ias
->serial
);
952 /* were we able to find the cert in passed to us */
954 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
,
955 PKCS7_R_UNABLE_TO_FIND_CERTIFICATE
);
960 if (!X509_STORE_CTX_init(ctx
, cert_store
, x509
, cert
)) {
961 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
, ERR_R_X509_LIB
);
964 X509_STORE_CTX_set_purpose(ctx
, X509_PURPOSE_SMIME_SIGN
);
965 i
= X509_verify_cert(ctx
);
967 PKCS7err(PKCS7_F_PKCS7_DATAVERIFY
, ERR_R_X509_LIB
);
968 X509_STORE_CTX_cleanup(ctx
);
971 X509_STORE_CTX_cleanup(ctx
);
973 return PKCS7_signatureVerify(bio
, p7
, si
, x509
);
978 int PKCS7_signatureVerify(BIO
*bio
, PKCS7
*p7
, PKCS7_SIGNER_INFO
*si
,
981 ASN1_OCTET_STRING
*os
;
982 EVP_MD_CTX
*mdc_tmp
, *mdc
;
985 STACK_OF(X509_ATTRIBUTE
) *sk
;
989 mdc_tmp
= EVP_MD_CTX_new();
990 if (mdc_tmp
== NULL
) {
991 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, ERR_R_MALLOC_FAILURE
);
995 if (!PKCS7_type_is_signed(p7
) && !PKCS7_type_is_signedAndEnveloped(p7
)) {
996 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, PKCS7_R_WRONG_PKCS7_TYPE
);
1000 md_type
= OBJ_obj2nid(si
->digest_alg
->algorithm
);
1004 if ((btmp
== NULL
) ||
1005 ((btmp
= BIO_find_type(btmp
, BIO_TYPE_MD
)) == NULL
)) {
1006 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
,
1007 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST
);
1010 BIO_get_md_ctx(btmp
, &mdc
);
1012 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, ERR_R_INTERNAL_ERROR
);
1015 if (EVP_MD_CTX_type(mdc
) == md_type
)
1018 * Workaround for some broken clients that put the signature OID
1019 * instead of the digest OID in digest_alg->algorithm
1021 if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc
)) == md_type
)
1023 btmp
= BIO_next(btmp
);
1027 * mdc is the digest ctx that we want, unless there are attributes, in
1028 * which case the digest is the signed attributes
1030 if (!EVP_MD_CTX_copy_ex(mdc_tmp
, mdc
))
1034 if ((sk
!= NULL
) && (sk_X509_ATTRIBUTE_num(sk
) != 0)) {
1035 unsigned char md_dat
[EVP_MAX_MD_SIZE
], *abuf
= NULL
;
1036 unsigned int md_len
;
1038 ASN1_OCTET_STRING
*message_digest
;
1040 if (!EVP_DigestFinal_ex(mdc_tmp
, md_dat
, &md_len
))
1042 message_digest
= PKCS7_digest_from_attributes(sk
);
1043 if (!message_digest
) {
1044 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
,
1045 PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST
);
1048 if ((message_digest
->length
!= (int)md_len
) ||
1049 (memcmp(message_digest
->data
, md_dat
, md_len
))) {
1050 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, PKCS7_R_DIGEST_FAILURE
);
1055 if (!EVP_VerifyInit_ex(mdc_tmp
, EVP_get_digestbynid(md_type
), NULL
))
1058 alen
= ASN1_item_i2d((ASN1_VALUE
*)sk
, &abuf
,
1059 ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY
));
1061 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, ERR_R_ASN1_LIB
);
1065 if (!EVP_VerifyUpdate(mdc_tmp
, abuf
, alen
))
1071 os
= si
->enc_digest
;
1072 pkey
= X509_get0_pubkey(x509
);
1078 i
= EVP_VerifyFinal(mdc_tmp
, os
->data
, os
->length
, pkey
);
1080 PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY
, PKCS7_R_SIGNATURE_FAILURE
);
1086 EVP_MD_CTX_free(mdc_tmp
);
1090 PKCS7_ISSUER_AND_SERIAL
*PKCS7_get_issuer_and_serial(PKCS7
*p7
, int idx
)
1092 STACK_OF(PKCS7_RECIP_INFO
) *rsk
;
1093 PKCS7_RECIP_INFO
*ri
;
1096 i
= OBJ_obj2nid(p7
->type
);
1097 if (i
!= NID_pkcs7_signedAndEnveloped
)
1099 if (p7
->d
.signed_and_enveloped
== NULL
)
1101 rsk
= p7
->d
.signed_and_enveloped
->recipientinfo
;
1104 if (sk_PKCS7_RECIP_INFO_num(rsk
) <= idx
)
1106 ri
= sk_PKCS7_RECIP_INFO_value(rsk
, idx
);
1107 return ri
->issuer_and_serial
;
1110 ASN1_TYPE
*PKCS7_get_signed_attribute(PKCS7_SIGNER_INFO
*si
, int nid
)
1112 return get_attribute(si
->auth_attr
, nid
);
1115 ASN1_TYPE
*PKCS7_get_attribute(PKCS7_SIGNER_INFO
*si
, int nid
)
1117 return get_attribute(si
->unauth_attr
, nid
);
1120 static ASN1_TYPE
*get_attribute(STACK_OF(X509_ATTRIBUTE
) *sk
, int nid
)
1124 idx
= X509at_get_attr_by_NID(sk
, nid
, -1);
1125 xa
= X509at_get_attr(sk
, idx
);
1126 return X509_ATTRIBUTE_get0_type(xa
, 0);
1129 ASN1_OCTET_STRING
*PKCS7_digest_from_attributes(STACK_OF(X509_ATTRIBUTE
) *sk
)
1132 if ((astype
= get_attribute(sk
, NID_pkcs9_messageDigest
)) == NULL
)
1134 return astype
->value
.octet_string
;
1137 int PKCS7_set_signed_attributes(PKCS7_SIGNER_INFO
*p7si
,
1138 STACK_OF(X509_ATTRIBUTE
) *sk
)
1142 sk_X509_ATTRIBUTE_pop_free(p7si
->auth_attr
, X509_ATTRIBUTE_free
);
1143 p7si
->auth_attr
= sk_X509_ATTRIBUTE_dup(sk
);
1144 if (p7si
->auth_attr
== NULL
)
1146 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(sk
); i
++) {
1147 if ((sk_X509_ATTRIBUTE_set(p7si
->auth_attr
, i
,
1148 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1156 int PKCS7_set_attributes(PKCS7_SIGNER_INFO
*p7si
,
1157 STACK_OF(X509_ATTRIBUTE
) *sk
)
1161 sk_X509_ATTRIBUTE_pop_free(p7si
->unauth_attr
, X509_ATTRIBUTE_free
);
1162 p7si
->unauth_attr
= sk_X509_ATTRIBUTE_dup(sk
);
1163 if (p7si
->unauth_attr
== NULL
)
1165 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(sk
); i
++) {
1166 if ((sk_X509_ATTRIBUTE_set(p7si
->unauth_attr
, i
,
1167 X509_ATTRIBUTE_dup(sk_X509_ATTRIBUTE_value
1175 int PKCS7_add_signed_attribute(PKCS7_SIGNER_INFO
*p7si
, int nid
, int atrtype
,
1178 return add_attribute(&(p7si
->auth_attr
), nid
, atrtype
, value
);
1181 int PKCS7_add_attribute(PKCS7_SIGNER_INFO
*p7si
, int nid
, int atrtype
,
1184 return add_attribute(&(p7si
->unauth_attr
), nid
, atrtype
, value
);
1187 static int add_attribute(STACK_OF(X509_ATTRIBUTE
) **sk
, int nid
, int atrtype
,
1190 X509_ATTRIBUTE
*attr
= NULL
;
1193 if ((*sk
= sk_X509_ATTRIBUTE_new_null()) == NULL
)
1196 if ((attr
= X509_ATTRIBUTE_create(nid
, atrtype
, value
)) == NULL
)
1198 if (!sk_X509_ATTRIBUTE_push(*sk
, attr
)) {
1199 X509_ATTRIBUTE_free(attr
);
1205 for (i
= 0; i
< sk_X509_ATTRIBUTE_num(*sk
); i
++) {
1206 attr
= sk_X509_ATTRIBUTE_value(*sk
, i
);
1207 if (OBJ_obj2nid(X509_ATTRIBUTE_get0_object(attr
)) == nid
) {
1208 X509_ATTRIBUTE_free(attr
);
1209 attr
= X509_ATTRIBUTE_create(nid
, atrtype
, value
);
1212 if (!sk_X509_ATTRIBUTE_set(*sk
, i
, attr
)) {
1213 X509_ATTRIBUTE_free(attr
);