xref: /openssl/crypto/pkcs7/pk7_smime.c (revision 67c0460b)
1 /*
2  * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9 
10 /* Simple PKCS#7 processing functions */
11 
12 #include <stdio.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/x509.h>
15 #include <openssl/x509v3.h>
16 #include "pk7_local.h"
17 
18 #define BUFFERSIZE 4096
19 
20 
21 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
22 
PKCS7_sign_ex(X509 * signcert,EVP_PKEY * pkey,STACK_OF (X509)* certs,BIO * data,int flags,OSSL_LIB_CTX * libctx,const char * propq)23 PKCS7 *PKCS7_sign_ex(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
24                      BIO *data, int flags, OSSL_LIB_CTX *libctx,
25                      const char *propq)
26 {
27     PKCS7 *p7;
28     int i;
29 
30     if ((p7 = PKCS7_new_ex(libctx, propq)) == NULL) {
31         ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
32         return NULL;
33     }
34 
35     if (!PKCS7_set_type(p7, NID_pkcs7_signed))
36         goto err;
37 
38     if (!PKCS7_content_new(p7, NID_pkcs7_data))
39         goto err;
40 
41     if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) {
42         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_ADD_SIGNER_ERROR);
43         goto err;
44     }
45 
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)))
49                 goto err;
50         }
51     }
52 
53     if (flags & PKCS7_DETACHED)
54         PKCS7_set_detached(p7, 1);
55 
56     if (flags & (PKCS7_STREAM | PKCS7_PARTIAL))
57         return p7;
58 
59     if (PKCS7_final(p7, data, flags))
60         return p7;
61 
62  err:
63     PKCS7_free(p7);
64     return NULL;
65 }
66 
PKCS7_sign(X509 * signcert,EVP_PKEY * pkey,STACK_OF (X509)* certs,BIO * data,int flags)67 PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
68                   BIO *data, int flags)
69 {
70     return PKCS7_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
71 }
72 
73 
PKCS7_final(PKCS7 * p7,BIO * data,int flags)74 int PKCS7_final(PKCS7 *p7, BIO *data, int flags)
75 {
76     BIO *p7bio;
77     int ret = 0;
78 
79     if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
80         ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
81         return 0;
82     }
83 
84     if (!SMIME_crlf_copy(data, p7bio, flags))
85         goto err;
86 
87     (void)BIO_flush(p7bio);
88 
89     if (!PKCS7_dataFinal(p7, p7bio)) {
90         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_DATASIGN);
91         goto err;
92     }
93     ret = 1;
94 err:
95     BIO_free_all(p7bio);
96 
97     return ret;
98 
99 }
100 
101 /* Check to see if a cipher exists and if so add S/MIME capabilities */
102 
add_cipher_smcap(STACK_OF (X509_ALGOR)* sk,int nid,int arg)103 static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
104 {
105     if (EVP_get_cipherbynid(nid))
106         return PKCS7_simple_smimecap(sk, nid, arg);
107     return 1;
108 }
109 
add_digest_smcap(STACK_OF (X509_ALGOR)* sk,int nid,int arg)110 static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
111 {
112     if (EVP_get_digestbynid(nid))
113         return PKCS7_simple_smimecap(sk, nid, arg);
114     return 1;
115 }
116 
PKCS7_sign_add_signer(PKCS7 * p7,X509 * signcert,EVP_PKEY * pkey,const EVP_MD * md,int flags)117 PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
118                                          EVP_PKEY *pkey, const EVP_MD *md,
119                                          int flags)
120 {
121     PKCS7_SIGNER_INFO *si = NULL;
122     STACK_OF(X509_ALGOR) *smcap = NULL;
123 
124     if (!X509_check_private_key(signcert, pkey)) {
125         ERR_raise(ERR_LIB_PKCS7,
126                   PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
127         return NULL;
128     }
129 
130     if ((si = PKCS7_add_signature(p7, signcert, pkey, md)) == NULL) {
131         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
132         return NULL;
133     }
134 
135     si->ctx = ossl_pkcs7_get0_ctx(p7);
136     if (!(flags & PKCS7_NOCERTS)) {
137         if (!PKCS7_add_certificate(p7, signcert))
138             goto err;
139     }
140 
141     if (!(flags & PKCS7_NOATTR)) {
142         if (!PKCS7_add_attrib_content_type(si, NULL))
143             goto err;
144         /* Add SMIMECapabilities */
145         if (!(flags & PKCS7_NOSMIMECAP)) {
146             if ((smcap = sk_X509_ALGOR_new_null()) == NULL) {
147                 ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
148                 goto err;
149             }
150             if (!add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
151                 || !add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
152                 || !add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
153                 || !add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
154                 || !add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
155                 || !add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
156                 || !add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
157                 || !add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
158                 || !add_cipher_smcap(smcap, NID_rc2_cbc, 128)
159                 || !add_cipher_smcap(smcap, NID_rc2_cbc, 64)
160                 || !add_cipher_smcap(smcap, NID_des_cbc, -1)
161                 || !add_cipher_smcap(smcap, NID_rc2_cbc, 40)
162                 || !PKCS7_add_attrib_smimecap(si, smcap))
163                 goto err;
164             sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
165             smcap = NULL;
166         }
167         if (flags & PKCS7_REUSE_DIGEST) {
168             if (!pkcs7_copy_existing_digest(p7, si))
169                 goto err;
170             if (!(flags & PKCS7_PARTIAL)
171                 && !PKCS7_SIGNER_INFO_sign(si))
172                 goto err;
173         }
174     }
175     return si;
176  err:
177     sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
178     return NULL;
179 }
180 
181 /*
182  * Search for a digest matching SignerInfo digest type and if found copy
183  * across.
184  */
185 
pkcs7_copy_existing_digest(PKCS7 * p7,PKCS7_SIGNER_INFO * si)186 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
187 {
188     int i;
189     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
190     PKCS7_SIGNER_INFO *sitmp;
191     ASN1_OCTET_STRING *osdig = NULL;
192     sinfos = PKCS7_get_signer_info(p7);
193     for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
194         sitmp = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
195         if (si == sitmp)
196             break;
197         if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0)
198             continue;
199         if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) {
200             osdig = PKCS7_digest_from_attributes(sitmp->auth_attr);
201             break;
202         }
203 
204     }
205 
206     if (osdig != NULL)
207         return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length);
208 
209     ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND);
210     return 0;
211 }
212 
PKCS7_verify(PKCS7 * p7,STACK_OF (X509)* certs,X509_STORE * store,BIO * indata,BIO * out,int flags)213 int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
214                  BIO *indata, BIO *out, int flags)
215 {
216     STACK_OF(X509) *signers;
217     X509 *signer;
218     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
219     PKCS7_SIGNER_INFO *si;
220     X509_STORE_CTX *cert_ctx = NULL;
221     char *buf = NULL;
222     int i, j = 0, k, ret = 0;
223     BIO *p7bio = NULL;
224     BIO *tmpout = NULL;
225     const PKCS7_CTX *p7_ctx;
226 
227     if (p7 == NULL) {
228         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
229         return 0;
230     }
231 
232     if (!PKCS7_type_is_signed(p7)) {
233         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
234         return 0;
235     }
236 
237     /* Check for no data and no content: no data to verify signature */
238     if (PKCS7_get_detached(p7) && !indata) {
239         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
240         return 0;
241     }
242 
243     if (flags & PKCS7_NO_DUAL_CONTENT) {
244         /*
245          * This was originally "#if 0" because we thought that only old broken
246          * Netscape did this.  It turns out that Authenticode uses this kind
247          * of "extended" PKCS7 format, and things like UEFI secure boot and
248          * tools like osslsigncode need it.  In Authenticode the verification
249          * process is different, but the existing PKCs7 verification works.
250          */
251         if (!PKCS7_get_detached(p7) && indata) {
252             ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CONTENT_AND_DATA_PRESENT);
253             return 0;
254         }
255     }
256 
257     sinfos = PKCS7_get_signer_info(p7);
258 
259     if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
260         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_SIGNATURES_ON_DATA);
261         return 0;
262     }
263 
264     signers = PKCS7_get0_signers(p7, certs, flags);
265     if (signers == NULL)
266         return 0;
267 
268     /* Now verify the certificates */
269     p7_ctx = ossl_pkcs7_get0_ctx(p7);
270     cert_ctx = X509_STORE_CTX_new_ex(ossl_pkcs7_ctx_get0_libctx(p7_ctx),
271                                      ossl_pkcs7_ctx_get0_propq(p7_ctx));
272     if (cert_ctx == NULL)
273         goto err;
274     if (!(flags & PKCS7_NOVERIFY))
275         for (k = 0; k < sk_X509_num(signers); k++) {
276             signer = sk_X509_value(signers, k);
277             if (!(flags & PKCS7_NOCHAIN)) {
278                 if (!X509_STORE_CTX_init(cert_ctx, store, signer,
279                                          p7->d.sign->cert)) {
280                     ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
281                     goto err;
282                 }
283                 X509_STORE_CTX_set_default(cert_ctx, "smime_sign");
284             } else if (!X509_STORE_CTX_init(cert_ctx, store, signer, NULL)) {
285                 ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
286                 goto err;
287             }
288             if (!(flags & PKCS7_NOCRL))
289                 X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl);
290             i = X509_verify_cert(cert_ctx);
291             if (i <= 0)
292                 j = X509_STORE_CTX_get_error(cert_ctx);
293             if (i <= 0) {
294                 ERR_raise_data(ERR_LIB_PKCS7, PKCS7_R_CERTIFICATE_VERIFY_ERROR,
295                                "Verify error: %s",
296                                X509_verify_cert_error_string(j));
297                 goto err;
298             }
299             /* Check for revocation status here */
300         }
301 
302     if ((p7bio = PKCS7_dataInit(p7, indata)) == NULL)
303         goto err;
304 
305     if (flags & PKCS7_TEXT) {
306         if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
307             ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
308             goto err;
309         }
310         BIO_set_mem_eof_return(tmpout, 0);
311     } else
312         tmpout = out;
313 
314     /* We now have to 'read' from p7bio to calculate digests etc. */
315     if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
316         ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
317         goto err;
318     }
319     for (;;) {
320         i = BIO_read(p7bio, buf, BUFFERSIZE);
321         if (i <= 0)
322             break;
323         if (tmpout)
324             BIO_write(tmpout, buf, i);
325     }
326 
327     if (flags & PKCS7_TEXT) {
328         if (!SMIME_text(tmpout, out)) {
329             ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SMIME_TEXT_ERROR);
330             BIO_free(tmpout);
331             goto err;
332         }
333         BIO_free(tmpout);
334     }
335 
336     /* Now Verify All Signatures */
337     if (!(flags & PKCS7_NOSIGS))
338         for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
339             si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
340             signer = sk_X509_value(signers, i);
341             j = PKCS7_signatureVerify(p7bio, p7, si, signer);
342             if (j <= 0) {
343                 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
344                 goto err;
345             }
346         }
347 
348     ret = 1;
349 
350  err:
351     X509_STORE_CTX_free(cert_ctx);
352     OPENSSL_free(buf);
353     if (indata)
354         BIO_pop(p7bio);
355     BIO_free_all(p7bio);
356     sk_X509_free(signers);
357     return ret;
358 }
359 
STACK_OF(X509)360 STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
361                                    int flags)
362 {
363     STACK_OF(X509) *signers;
364     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
365     PKCS7_SIGNER_INFO *si;
366     PKCS7_ISSUER_AND_SERIAL *ias;
367     X509 *signer;
368     int i;
369 
370     if (p7 == NULL) {
371         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
372         return NULL;
373     }
374 
375     if (!PKCS7_type_is_signed(p7)) {
376         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
377         return NULL;
378     }
379 
380     /* Collect all the signers together */
381 
382     sinfos = PKCS7_get_signer_info(p7);
383 
384     if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
385         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_SIGNERS);
386         return 0;
387     }
388 
389     if ((signers = sk_X509_new_null()) == NULL) {
390         ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
391         return NULL;
392     }
393 
394     for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
395         si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
396         ias = si->issuer_and_serial;
397         signer = NULL;
398         /* If any certificates passed they take priority */
399         if (certs)
400             signer = X509_find_by_issuer_and_serial(certs,
401                                                     ias->issuer, ias->serial);
402         if (!signer && !(flags & PKCS7_NOINTERN)
403             && p7->d.sign->cert)
404             signer =
405                 X509_find_by_issuer_and_serial(p7->d.sign->cert,
406                                                ias->issuer, ias->serial);
407         if (!signer) {
408             ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
409             sk_X509_free(signers);
410             return 0;
411         }
412 
413         if (!sk_X509_push(signers, signer)) {
414             sk_X509_free(signers);
415             return NULL;
416         }
417     }
418     return signers;
419 }
420 
421 /* Build a complete PKCS#7 enveloped data */
422 
PKCS7_encrypt_ex(STACK_OF (X509)* certs,BIO * in,const EVP_CIPHER * cipher,int flags,OSSL_LIB_CTX * libctx,const char * propq)423 PKCS7 *PKCS7_encrypt_ex(STACK_OF(X509) *certs, BIO *in,
424                         const EVP_CIPHER *cipher, int flags,
425                         OSSL_LIB_CTX *libctx, const char *propq)
426 {
427     PKCS7 *p7;
428     BIO *p7bio = NULL;
429     int i;
430     X509 *x509;
431 
432     if ((p7 = PKCS7_new_ex(libctx, propq)) == NULL) {
433         ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
434         return NULL;
435     }
436 
437     if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
438         goto err;
439     if (!PKCS7_set_cipher(p7, cipher)) {
440         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_SETTING_CIPHER);
441         goto err;
442     }
443 
444     for (i = 0; i < sk_X509_num(certs); i++) {
445         x509 = sk_X509_value(certs, i);
446         if (!PKCS7_add_recipient(p7, x509)) {
447             ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_ADDING_RECIPIENT);
448             goto err;
449         }
450     }
451 
452     if (flags & PKCS7_STREAM)
453         return p7;
454 
455     if (PKCS7_final(p7, in, flags))
456         return p7;
457 
458  err:
459 
460     BIO_free_all(p7bio);
461     PKCS7_free(p7);
462     return NULL;
463 
464 }
465 
PKCS7_encrypt(STACK_OF (X509)* certs,BIO * in,const EVP_CIPHER * cipher,int flags)466 PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
467                      int flags)
468 {
469     return PKCS7_encrypt_ex(certs, in, cipher, flags, NULL, NULL);
470 }
471 
472 
PKCS7_decrypt(PKCS7 * p7,EVP_PKEY * pkey,X509 * cert,BIO * data,int flags)473 int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
474 {
475     BIO *tmpmem;
476     int ret = 0, i;
477     char *buf = NULL;
478 
479     if (p7 == NULL) {
480         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
481         return 0;
482     }
483 
484     if (!PKCS7_type_is_enveloped(p7)) {
485         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
486         return 0;
487     }
488 
489     if (cert && !X509_check_private_key(cert, pkey)) {
490         ERR_raise(ERR_LIB_PKCS7,
491                   PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
492         return 0;
493     }
494 
495     if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) {
496         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DECRYPT_ERROR);
497         return 0;
498     }
499 
500     if (flags & PKCS7_TEXT) {
501         BIO *tmpbuf, *bread;
502         /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
503         if ((tmpbuf = BIO_new(BIO_f_buffer())) == NULL) {
504             ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
505             BIO_free_all(tmpmem);
506             return 0;
507         }
508         if ((bread = BIO_push(tmpbuf, tmpmem)) == NULL) {
509             ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
510             BIO_free_all(tmpbuf);
511             BIO_free_all(tmpmem);
512             return 0;
513         }
514         ret = SMIME_text(bread, data);
515         if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
516             if (BIO_get_cipher_status(tmpmem) <= 0)
517                 ret = 0;
518         }
519         BIO_free_all(bread);
520         return ret;
521     }
522     if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
523         ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
524         goto err;
525     }
526     for (;;) {
527         i = BIO_read(tmpmem, buf, BUFFERSIZE);
528         if (i <= 0) {
529             ret = 1;
530             if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
531                 if (BIO_get_cipher_status(tmpmem) <= 0)
532                     ret = 0;
533             }
534 
535             break;
536         }
537         if (BIO_write(data, buf, i) != i) {
538             break;
539         }
540     }
541 err:
542     OPENSSL_free(buf);
543     BIO_free_all(tmpmem);
544     return ret;
545 }
546