provider: disable fall-backs if OSSL_PROVIDER_load() fails.
[openssl.git] / crypto / pkcs7 / pk7_smime.c
1 /*
2  * Copyright 1999-2020 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 DEFINE_STACK_OF(X509)
21 DEFINE_STACK_OF(X509_ATTRIBUTE)
22 DEFINE_STACK_OF(X509_ALGOR)
23 DEFINE_STACK_OF(PKCS7_SIGNER_INFO)
24
25 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
26
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)
30 {
31     PKCS7 *p7;
32     int i;
33
34     if ((p7 = PKCS7_new_with_libctx(libctx, propq)) == NULL) {
35         PKCS7err(0, ERR_R_MALLOC_FAILURE);
36         return NULL;
37     }
38
39     if (!PKCS7_set_type(p7, NID_pkcs7_signed))
40         goto err;
41
42     if (!PKCS7_content_new(p7, NID_pkcs7_data))
43         goto err;
44
45     if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) {
46         PKCS7err(0, PKCS7_R_PKCS7_ADD_SIGNER_ERROR);
47         goto err;
48     }
49
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)))
53                 goto err;
54         }
55     }
56
57     if (flags & PKCS7_DETACHED)
58         PKCS7_set_detached(p7, 1);
59
60     if (flags & (PKCS7_STREAM | PKCS7_PARTIAL))
61         return p7;
62
63     if (PKCS7_final(p7, data, flags))
64         return p7;
65
66  err:
67     PKCS7_free(p7);
68     return NULL;
69 }
70
71 PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
72                   BIO *data, int flags)
73 {
74     return PKCS7_sign_with_libctx(signcert, pkey, certs, data, flags, NULL, NULL);
75 }
76
77
78 int PKCS7_final(PKCS7 *p7, BIO *data, int flags)
79 {
80     BIO *p7bio;
81     int ret = 0;
82
83     if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
84         PKCS7err(PKCS7_F_PKCS7_FINAL, ERR_R_MALLOC_FAILURE);
85         return 0;
86     }
87
88     SMIME_crlf_copy(data, p7bio, flags);
89
90     (void)BIO_flush(p7bio);
91
92     if (!PKCS7_dataFinal(p7, p7bio)) {
93         PKCS7err(PKCS7_F_PKCS7_FINAL, PKCS7_R_PKCS7_DATASIGN);
94         goto err;
95     }
96     ret = 1;
97 err:
98     BIO_free_all(p7bio);
99
100     return ret;
101
102 }
103
104 /* Check to see if a cipher exists and if so add S/MIME capabilities */
105
106 static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
107 {
108     if (EVP_get_cipherbynid(nid))
109         return PKCS7_simple_smimecap(sk, nid, arg);
110     return 1;
111 }
112
113 static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
114 {
115     if (EVP_get_digestbynid(nid))
116         return PKCS7_simple_smimecap(sk, nid, arg);
117     return 1;
118 }
119
120 PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
121                                          EVP_PKEY *pkey, const EVP_MD *md,
122                                          int flags)
123 {
124     PKCS7_SIGNER_INFO *si = NULL;
125     STACK_OF(X509_ALGOR) *smcap = NULL;
126
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);
130         return NULL;
131     }
132
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);
136         return NULL;
137     }
138
139     si->ctx = pkcs7_get0_ctx(p7);
140     if (!(flags & PKCS7_NOCERTS)) {
141         if (!PKCS7_add_certificate(p7, signcert))
142             goto err;
143     }
144
145     if (!(flags & PKCS7_NOATTR)) {
146         if (!PKCS7_add_attrib_content_type(si, NULL))
147             goto err;
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);
152                 goto err;
153             }
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))
167                 goto err;
168             sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
169             smcap = NULL;
170         }
171         if (flags & PKCS7_REUSE_DIGEST) {
172             if (!pkcs7_copy_existing_digest(p7, si))
173                 goto err;
174             if (!(flags & PKCS7_PARTIAL)
175                 && !PKCS7_SIGNER_INFO_sign(si))
176                 goto err;
177         }
178     }
179     return si;
180  err:
181     sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
182     return NULL;
183 }
184
185 /*
186  * Search for a digest matching SignerInfo digest type and if found copy
187  * across.
188  */
189
190 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
191 {
192     int i;
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);
199         if (si == sitmp)
200             break;
201         if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0)
202             continue;
203         if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) {
204             osdig = PKCS7_digest_from_attributes(sitmp->auth_attr);
205             break;
206         }
207
208     }
209
210     if (osdig != NULL)
211         return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length);
212
213     PKCS7err(PKCS7_F_PKCS7_COPY_EXISTING_DIGEST,
214              PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND);
215     return 0;
216 }
217
218 int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
219                  BIO *indata, BIO *out, int flags)
220 {
221     STACK_OF(X509) *signers;
222     X509 *signer;
223     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
224     PKCS7_SIGNER_INFO *si;
225     X509_STORE_CTX *cert_ctx = NULL;
226     char *buf = NULL;
227     int i, j = 0, k, ret = 0;
228     BIO *p7bio = NULL;
229     BIO *tmpin = NULL, *tmpout = NULL;
230     const PKCS7_CTX *p7_ctx;
231
232     if (p7 == NULL) {
233         PKCS7err(0, PKCS7_R_INVALID_NULL_POINTER);
234         return 0;
235     }
236
237     if (!PKCS7_type_is_signed(p7)) {
238         PKCS7err(0, PKCS7_R_WRONG_CONTENT_TYPE);
239         return 0;
240     }
241
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);
245         return 0;
246     }
247
248     if (flags & PKCS7_NO_DUAL_CONTENT) {
249         /*
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.
255          */
256         if (!PKCS7_get_detached(p7) && indata) {
257             PKCS7err(0, PKCS7_R_CONTENT_AND_DATA_PRESENT);
258             return 0;
259         }
260     }
261
262     sinfos = PKCS7_get_signer_info(p7);
263
264     if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
265         PKCS7err(0, PKCS7_R_NO_SIGNATURES_ON_DATA);
266         return 0;
267     }
268
269     signers = PKCS7_get0_signers(p7, certs, flags);
270     if (signers == NULL)
271         return 0;
272
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)
277         goto err;
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,
283                                          p7->d.sign->cert)) {
284                     PKCS7err(0, ERR_R_X509_LIB);
285                     goto err;
286                 }
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);
290                 goto err;
291             }
292             if (!(flags & PKCS7_NOCRL))
293                 X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl);
294             i = X509_verify_cert(cert_ctx);
295             if (i <= 0)
296                 j = X509_STORE_CTX_get_error(cert_ctx);
297             X509_STORE_CTX_cleanup(cert_ctx);
298             if (i <= 0) {
299                 PKCS7err(0, PKCS7_R_CERTIFICATE_VERIFY_ERROR);
300                 ERR_add_error_data(2, "Verify error:",
301                                    X509_verify_cert_error_string(j));
302                 goto err;
303             }
304             /* Check for revocation status here */
305         }
306
307     /*
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.
312      */
313
314     if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) {
315         char *ptr;
316         long len;
317         len = BIO_get_mem_data(indata, &ptr);
318         tmpin = BIO_new_mem_buf(ptr, len);
319         if (tmpin == NULL) {
320             PKCS7err(0, ERR_R_MALLOC_FAILURE);
321             goto err;
322         }
323     } else
324         tmpin = indata;
325
326     if ((p7bio = PKCS7_dataInit(p7, tmpin)) == NULL)
327         goto err;
328
329     if (flags & PKCS7_TEXT) {
330         if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
331             PKCS7err(0, ERR_R_MALLOC_FAILURE);
332             goto err;
333         }
334         BIO_set_mem_eof_return(tmpout, 0);
335     } else
336         tmpout = out;
337
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);
341         goto err;
342     }
343     for (;;) {
344         i = BIO_read(p7bio, buf, BUFFERSIZE);
345         if (i <= 0)
346             break;
347         if (tmpout)
348             BIO_write(tmpout, buf, i);
349     }
350
351     if (flags & PKCS7_TEXT) {
352         if (!SMIME_text(tmpout, out)) {
353             PKCS7err(0, PKCS7_R_SMIME_TEXT_ERROR);
354             BIO_free(tmpout);
355             goto err;
356         }
357         BIO_free(tmpout);
358     }
359
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);
366             if (j <= 0) {
367                 PKCS7err(0, PKCS7_R_SIGNATURE_FAILURE);
368                 goto err;
369             }
370         }
371
372     ret = 1;
373
374  err:
375     X509_STORE_CTX_free(cert_ctx);
376     OPENSSL_free(buf);
377     if (tmpin == indata) {
378         if (indata)
379             BIO_pop(p7bio);
380     }
381     BIO_free_all(p7bio);
382     sk_X509_free(signers);
383     return ret;
384 }
385
386 STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
387                                    int flags)
388 {
389     STACK_OF(X509) *signers;
390     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
391     PKCS7_SIGNER_INFO *si;
392     PKCS7_ISSUER_AND_SERIAL *ias;
393     X509 *signer;
394     int i;
395
396     if (p7 == NULL) {
397         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_INVALID_NULL_POINTER);
398         return NULL;
399     }
400
401     if (!PKCS7_type_is_signed(p7)) {
402         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_WRONG_CONTENT_TYPE);
403         return NULL;
404     }
405
406     /* Collect all the signers together */
407
408     sinfos = PKCS7_get_signer_info(p7);
409
410     if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
411         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_NO_SIGNERS);
412         return 0;
413     }
414
415     if ((signers = sk_X509_new_null()) == NULL) {
416         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, ERR_R_MALLOC_FAILURE);
417         return NULL;
418     }
419
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;
423         signer = NULL;
424         /* If any certificates passed they take priority */
425         if (certs)
426             signer = X509_find_by_issuer_and_serial(certs,
427                                                     ias->issuer, ias->serial);
428         if (!signer && !(flags & PKCS7_NOINTERN)
429             && p7->d.sign->cert)
430             signer =
431                 X509_find_by_issuer_and_serial(p7->d.sign->cert,
432                                                ias->issuer, ias->serial);
433         if (!signer) {
434             PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,
435                      PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
436             sk_X509_free(signers);
437             return 0;
438         }
439
440         if (!sk_X509_push(signers, signer)) {
441             sk_X509_free(signers);
442             return NULL;
443         }
444     }
445     return signers;
446 }
447
448 /* Build a complete PKCS#7 enveloped data */
449
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)
453 {
454     PKCS7 *p7;
455     BIO *p7bio = NULL;
456     int i;
457     X509 *x509;
458
459     if ((p7 = PKCS7_new_with_libctx(libctx, propq)) == NULL) {
460         PKCS7err(0, ERR_R_MALLOC_FAILURE);
461         return NULL;
462     }
463
464     if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
465         goto err;
466     if (!PKCS7_set_cipher(p7, cipher)) {
467         PKCS7err(0, PKCS7_R_ERROR_SETTING_CIPHER);
468         goto err;
469     }
470
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);
475             goto err;
476         }
477     }
478
479     if (flags & PKCS7_STREAM)
480         return p7;
481
482     if (PKCS7_final(p7, in, flags))
483         return p7;
484
485  err:
486
487     BIO_free_all(p7bio);
488     PKCS7_free(p7);
489     return NULL;
490
491 }
492
493 PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
494                      int flags)
495 {
496     return PKCS7_encrypt_with_libctx(certs, in, cipher, flags, NULL, NULL);
497 }
498
499
500 int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
501 {
502     BIO *tmpmem;
503     int ret = 0, i;
504     char *buf = NULL;
505
506     if (p7 == NULL) {
507         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_INVALID_NULL_POINTER);
508         return 0;
509     }
510
511     if (!PKCS7_type_is_enveloped(p7)) {
512         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_WRONG_CONTENT_TYPE);
513         return 0;
514     }
515
516     if (cert && !X509_check_private_key(cert, pkey)) {
517         PKCS7err(PKCS7_F_PKCS7_DECRYPT,
518                  PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
519         return 0;
520     }
521
522     if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) {
523         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR);
524         return 0;
525     }
526
527     if (flags & PKCS7_TEXT) {
528         BIO *tmpbuf, *bread;
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);
533             return 0;
534         }
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);
539             return 0;
540         }
541         ret = SMIME_text(bread, data);
542         if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
543             if (!BIO_get_cipher_status(tmpmem))
544                 ret = 0;
545         }
546         BIO_free_all(bread);
547         return ret;
548     }
549     if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
550         PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
551         goto err;
552     }
553     for (;;) {
554         i = BIO_read(tmpmem, buf, BUFFERSIZE);
555         if (i <= 0) {
556             ret = 1;
557             if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
558                 if (!BIO_get_cipher_status(tmpmem))
559                     ret = 0;
560             }
561
562             break;
563         }
564         if (BIO_write(data, buf, i) != i) {
565             break;
566         }
567     }
568 err:
569     OPENSSL_free(buf);
570     BIO_free_all(tmpmem);
571     return ret;
572 }