8d89f7fd441b739fef6a52243de9b507a4c0be02
[openssl.git] / crypto / pkcs7 / pk7_smime.c
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
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
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
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     SMIME_crlf_copy(data, p7bio, flags);
85
86     (void)BIO_flush(p7bio);
87
88     if (!PKCS7_dataFinal(p7, p7bio)) {
89         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_DATASIGN);
90         goto err;
91     }
92     ret = 1;
93 err:
94     BIO_free_all(p7bio);
95
96     return ret;
97
98 }
99
100 /* Check to see if a cipher exists and if so add S/MIME capabilities */
101
102 static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
103 {
104     if (EVP_get_cipherbynid(nid))
105         return PKCS7_simple_smimecap(sk, nid, arg);
106     return 1;
107 }
108
109 static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
110 {
111     if (EVP_get_digestbynid(nid))
112         return PKCS7_simple_smimecap(sk, nid, arg);
113     return 1;
114 }
115
116 PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
117                                          EVP_PKEY *pkey, const EVP_MD *md,
118                                          int flags)
119 {
120     PKCS7_SIGNER_INFO *si = NULL;
121     STACK_OF(X509_ALGOR) *smcap = NULL;
122
123     if (!X509_check_private_key(signcert, pkey)) {
124         ERR_raise(ERR_LIB_PKCS7,
125                   PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
126         return NULL;
127     }
128
129     if ((si = PKCS7_add_signature(p7, signcert, pkey, md)) == NULL) {
130         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
131         return NULL;
132     }
133
134     si->ctx = pkcs7_get0_ctx(p7);
135     if (!(flags & PKCS7_NOCERTS)) {
136         if (!PKCS7_add_certificate(p7, signcert))
137             goto err;
138     }
139
140     if (!(flags & PKCS7_NOATTR)) {
141         if (!PKCS7_add_attrib_content_type(si, NULL))
142             goto err;
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);
147                 goto err;
148             }
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))
162                 goto err;
163             sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
164             smcap = NULL;
165         }
166         if (flags & PKCS7_REUSE_DIGEST) {
167             if (!pkcs7_copy_existing_digest(p7, si))
168                 goto err;
169             if (!(flags & PKCS7_PARTIAL)
170                 && !PKCS7_SIGNER_INFO_sign(si))
171                 goto err;
172         }
173     }
174     return si;
175  err:
176     sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
177     return NULL;
178 }
179
180 /*
181  * Search for a digest matching SignerInfo digest type and if found copy
182  * across.
183  */
184
185 static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
186 {
187     int i;
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);
194         if (si == sitmp)
195             break;
196         if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0)
197             continue;
198         if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) {
199             osdig = PKCS7_digest_from_attributes(sitmp->auth_attr);
200             break;
201         }
202
203     }
204
205     if (osdig != NULL)
206         return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length);
207
208     ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND);
209     return 0;
210 }
211
212 int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
213                  BIO *indata, BIO *out, int flags)
214 {
215     STACK_OF(X509) *signers;
216     X509 *signer;
217     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
218     PKCS7_SIGNER_INFO *si;
219     X509_STORE_CTX *cert_ctx = NULL;
220     char *buf = NULL;
221     int i, j = 0, k, ret = 0;
222     BIO *p7bio = NULL;
223     BIO *tmpin = NULL, *tmpout = NULL;
224     const PKCS7_CTX *p7_ctx;
225
226     if (p7 == NULL) {
227         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
228         return 0;
229     }
230
231     if (!PKCS7_type_is_signed(p7)) {
232         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
233         return 0;
234     }
235
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);
239         return 0;
240     }
241
242     if (flags & PKCS7_NO_DUAL_CONTENT) {
243         /*
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.
249          */
250         if (!PKCS7_get_detached(p7) && indata) {
251             ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CONTENT_AND_DATA_PRESENT);
252             return 0;
253         }
254     }
255
256     sinfos = PKCS7_get_signer_info(p7);
257
258     if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
259         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_SIGNATURES_ON_DATA);
260         return 0;
261     }
262
263     signers = PKCS7_get0_signers(p7, certs, flags);
264     if (signers == NULL)
265         return 0;
266
267     /* Now verify the certificates */
268     p7_ctx = pkcs7_get0_ctx(p7);
269     cert_ctx = X509_STORE_CTX_new_ex(pkcs7_ctx_get0_libctx(p7_ctx),
270                                      pkcs7_ctx_get0_propq(p7_ctx));
271     if (cert_ctx == NULL)
272         goto err;
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,
278                                          p7->d.sign->cert)) {
279                     ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
280                     goto err;
281                 }
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);
285                 goto err;
286             }
287             if (!(flags & PKCS7_NOCRL))
288                 X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl);
289             i = X509_verify_cert(cert_ctx);
290             if (i <= 0)
291                 j = X509_STORE_CTX_get_error(cert_ctx);
292             X509_STORE_CTX_cleanup(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     /*
303      * Performance optimization: if the content is a memory BIO then store
304      * its contents in a temporary read only memory BIO. This avoids
305      * potentially large numbers of slow copies of data which will occur when
306      * reading from a read write memory BIO when signatures are calculated.
307      */
308
309     if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) {
310         char *ptr;
311         long len;
312         len = BIO_get_mem_data(indata, &ptr);
313         tmpin = (len == 0) ? indata : BIO_new_mem_buf(ptr, len);
314         if (tmpin == NULL) {
315             ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
316             goto err;
317         }
318     } else
319         tmpin = indata;
320
321     if ((p7bio = PKCS7_dataInit(p7, tmpin)) == NULL)
322         goto err;
323
324     if (flags & PKCS7_TEXT) {
325         if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
326             ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
327             goto err;
328         }
329         BIO_set_mem_eof_return(tmpout, 0);
330     } else
331         tmpout = out;
332
333     /* We now have to 'read' from p7bio to calculate digests etc. */
334     if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
335         ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
336         goto err;
337     }
338     for (;;) {
339         i = BIO_read(p7bio, buf, BUFFERSIZE);
340         if (i <= 0)
341             break;
342         if (tmpout)
343             BIO_write(tmpout, buf, i);
344     }
345
346     if (flags & PKCS7_TEXT) {
347         if (!SMIME_text(tmpout, out)) {
348             ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SMIME_TEXT_ERROR);
349             BIO_free(tmpout);
350             goto err;
351         }
352         BIO_free(tmpout);
353     }
354
355     /* Now Verify All Signatures */
356     if (!(flags & PKCS7_NOSIGS))
357         for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
358             si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
359             signer = sk_X509_value(signers, i);
360             j = PKCS7_signatureVerify(p7bio, p7, si, signer);
361             if (j <= 0) {
362                 ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
363                 goto err;
364             }
365         }
366
367     ret = 1;
368
369  err:
370     X509_STORE_CTX_free(cert_ctx);
371     OPENSSL_free(buf);
372     if (tmpin == indata) {
373         if (indata)
374             BIO_pop(p7bio);
375     }
376     BIO_free_all(p7bio);
377     sk_X509_free(signers);
378     return ret;
379 }
380
381 STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
382                                    int flags)
383 {
384     STACK_OF(X509) *signers;
385     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
386     PKCS7_SIGNER_INFO *si;
387     PKCS7_ISSUER_AND_SERIAL *ias;
388     X509 *signer;
389     int i;
390
391     if (p7 == NULL) {
392         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
393         return NULL;
394     }
395
396     if (!PKCS7_type_is_signed(p7)) {
397         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
398         return NULL;
399     }
400
401     /* Collect all the signers together */
402
403     sinfos = PKCS7_get_signer_info(p7);
404
405     if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
406         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_SIGNERS);
407         return 0;
408     }
409
410     if ((signers = sk_X509_new_null()) == NULL) {
411         ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
412         return NULL;
413     }
414
415     for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
416         si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
417         ias = si->issuer_and_serial;
418         signer = NULL;
419         /* If any certificates passed they take priority */
420         if (certs)
421             signer = X509_find_by_issuer_and_serial(certs,
422                                                     ias->issuer, ias->serial);
423         if (!signer && !(flags & PKCS7_NOINTERN)
424             && p7->d.sign->cert)
425             signer =
426                 X509_find_by_issuer_and_serial(p7->d.sign->cert,
427                                                ias->issuer, ias->serial);
428         if (!signer) {
429             ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
430             sk_X509_free(signers);
431             return 0;
432         }
433
434         if (!sk_X509_push(signers, signer)) {
435             sk_X509_free(signers);
436             return NULL;
437         }
438     }
439     return signers;
440 }
441
442 /* Build a complete PKCS#7 enveloped data */
443
444 PKCS7 *PKCS7_encrypt_ex(STACK_OF(X509) *certs, BIO *in,
445                         const EVP_CIPHER *cipher, int flags,
446                         OSSL_LIB_CTX *libctx, const char *propq)
447 {
448     PKCS7 *p7;
449     BIO *p7bio = NULL;
450     int i;
451     X509 *x509;
452
453     if ((p7 = PKCS7_new_ex(libctx, propq)) == NULL) {
454         ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
455         return NULL;
456     }
457
458     if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
459         goto err;
460     if (!PKCS7_set_cipher(p7, cipher)) {
461         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_SETTING_CIPHER);
462         goto err;
463     }
464
465     for (i = 0; i < sk_X509_num(certs); i++) {
466         x509 = sk_X509_value(certs, i);
467         if (!PKCS7_add_recipient(p7, x509)) {
468             ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_ADDING_RECIPIENT);
469             goto err;
470         }
471     }
472
473     if (flags & PKCS7_STREAM)
474         return p7;
475
476     if (PKCS7_final(p7, in, flags))
477         return p7;
478
479  err:
480
481     BIO_free_all(p7bio);
482     PKCS7_free(p7);
483     return NULL;
484
485 }
486
487 PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
488                      int flags)
489 {
490     return PKCS7_encrypt_ex(certs, in, cipher, flags, NULL, NULL);
491 }
492
493
494 int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
495 {
496     BIO *tmpmem;
497     int ret = 0, i;
498     char *buf = NULL;
499
500     if (p7 == NULL) {
501         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
502         return 0;
503     }
504
505     if (!PKCS7_type_is_enveloped(p7)) {
506         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
507         return 0;
508     }
509
510     if (cert && !X509_check_private_key(cert, pkey)) {
511         ERR_raise(ERR_LIB_PKCS7,
512                   PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
513         return 0;
514     }
515
516     if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) {
517         ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DECRYPT_ERROR);
518         return 0;
519     }
520
521     if (flags & PKCS7_TEXT) {
522         BIO *tmpbuf, *bread;
523         /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
524         if ((tmpbuf = BIO_new(BIO_f_buffer())) == NULL) {
525             ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
526             BIO_free_all(tmpmem);
527             return 0;
528         }
529         if ((bread = BIO_push(tmpbuf, tmpmem)) == NULL) {
530             ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
531             BIO_free_all(tmpbuf);
532             BIO_free_all(tmpmem);
533             return 0;
534         }
535         ret = SMIME_text(bread, data);
536         if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
537             if (!BIO_get_cipher_status(tmpmem))
538                 ret = 0;
539         }
540         BIO_free_all(bread);
541         return ret;
542     }
543     if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
544         ERR_raise(ERR_LIB_PKCS7, ERR_R_MALLOC_FAILURE);
545         goto err;
546     }
547     for (;;) {
548         i = BIO_read(tmpmem, buf, BUFFERSIZE);
549         if (i <= 0) {
550             ret = 1;
551             if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
552                 if (!BIO_get_cipher_status(tmpmem))
553                     ret = 0;
554             }
555
556             break;
557         }
558         if (BIO_write(data, buf, i) != i) {
559             break;
560         }
561     }
562 err:
563     OPENSSL_free(buf);
564     BIO_free_all(tmpmem);
565     return ret;
566 }