Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[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
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         PKCS7err(0, 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         PKCS7err(0, 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         PKCS7err(PKCS7_F_PKCS7_FINAL, 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         PKCS7err(PKCS7_F_PKCS7_FINAL, 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         PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
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         PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER,
131                  PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
132         return NULL;
133     }
134
135     si->ctx = 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                 PKCS7err(PKCS7_F_PKCS7_SIGN_ADD_SIGNER, 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
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     PKCS7err(PKCS7_F_PKCS7_COPY_EXISTING_DIGEST,
210              PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND);
211     return 0;
212 }
213
214 int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
215                  BIO *indata, BIO *out, int flags)
216 {
217     STACK_OF(X509) *signers;
218     X509 *signer;
219     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
220     PKCS7_SIGNER_INFO *si;
221     X509_STORE_CTX *cert_ctx = NULL;
222     char *buf = NULL;
223     int i, j = 0, k, ret = 0;
224     BIO *p7bio = NULL;
225     BIO *tmpin = NULL, *tmpout = NULL;
226     const PKCS7_CTX *p7_ctx;
227
228     if (p7 == NULL) {
229         PKCS7err(0, PKCS7_R_INVALID_NULL_POINTER);
230         return 0;
231     }
232
233     if (!PKCS7_type_is_signed(p7)) {
234         PKCS7err(0, PKCS7_R_WRONG_CONTENT_TYPE);
235         return 0;
236     }
237
238     /* Check for no data and no content: no data to verify signature */
239     if (PKCS7_get_detached(p7) && !indata) {
240         PKCS7err(0, PKCS7_R_NO_CONTENT);
241         return 0;
242     }
243
244     if (flags & PKCS7_NO_DUAL_CONTENT) {
245         /*
246          * This was originally "#if 0" because we thought that only old broken
247          * Netscape did this.  It turns out that Authenticode uses this kind
248          * of "extended" PKCS7 format, and things like UEFI secure boot and
249          * tools like osslsigncode need it.  In Authenticode the verification
250          * process is different, but the existing PKCs7 verification works.
251          */
252         if (!PKCS7_get_detached(p7) && indata) {
253             PKCS7err(0, PKCS7_R_CONTENT_AND_DATA_PRESENT);
254             return 0;
255         }
256     }
257
258     sinfos = PKCS7_get_signer_info(p7);
259
260     if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
261         PKCS7err(0, PKCS7_R_NO_SIGNATURES_ON_DATA);
262         return 0;
263     }
264
265     signers = PKCS7_get0_signers(p7, certs, flags);
266     if (signers == NULL)
267         return 0;
268
269     /* Now verify the certificates */
270     p7_ctx = pkcs7_get0_ctx(p7);
271     cert_ctx = X509_STORE_CTX_new_ex(p7_ctx->libctx, p7_ctx->propq);
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                     PKCS7err(0, 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                 PKCS7err(0, 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             X509_STORE_CTX_cleanup(cert_ctx);
294             if (i <= 0) {
295                 PKCS7err(0, PKCS7_R_CERTIFICATE_VERIFY_ERROR);
296                 ERR_add_error_data(2, "Verify error:",
297                                    X509_verify_cert_error_string(j));
298                 goto err;
299             }
300             /* Check for revocation status here */
301         }
302
303     /*
304      * Performance optimization: if the content is a memory BIO then store
305      * its contents in a temporary read only memory BIO. This avoids
306      * potentially large numbers of slow copies of data which will occur when
307      * reading from a read write memory BIO when signatures are calculated.
308      */
309
310     if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) {
311         char *ptr;
312         long len;
313         len = BIO_get_mem_data(indata, &ptr);
314         tmpin = (len == 0) ? indata : BIO_new_mem_buf(ptr, len);
315         if (tmpin == NULL) {
316             PKCS7err(0, ERR_R_MALLOC_FAILURE);
317             goto err;
318         }
319     } else
320         tmpin = indata;
321
322     if ((p7bio = PKCS7_dataInit(p7, tmpin)) == NULL)
323         goto err;
324
325     if (flags & PKCS7_TEXT) {
326         if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
327             PKCS7err(0, ERR_R_MALLOC_FAILURE);
328             goto err;
329         }
330         BIO_set_mem_eof_return(tmpout, 0);
331     } else
332         tmpout = out;
333
334     /* We now have to 'read' from p7bio to calculate digests etc. */
335     if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
336         PKCS7err(0, ERR_R_MALLOC_FAILURE);
337         goto err;
338     }
339     for (;;) {
340         i = BIO_read(p7bio, buf, BUFFERSIZE);
341         if (i <= 0)
342             break;
343         if (tmpout)
344             BIO_write(tmpout, buf, i);
345     }
346
347     if (flags & PKCS7_TEXT) {
348         if (!SMIME_text(tmpout, out)) {
349             PKCS7err(0, PKCS7_R_SMIME_TEXT_ERROR);
350             BIO_free(tmpout);
351             goto err;
352         }
353         BIO_free(tmpout);
354     }
355
356     /* Now Verify All Signatures */
357     if (!(flags & PKCS7_NOSIGS))
358         for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
359             si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
360             signer = sk_X509_value(signers, i);
361             j = PKCS7_signatureVerify(p7bio, p7, si, signer);
362             if (j <= 0) {
363                 PKCS7err(0, PKCS7_R_SIGNATURE_FAILURE);
364                 goto err;
365             }
366         }
367
368     ret = 1;
369
370  err:
371     X509_STORE_CTX_free(cert_ctx);
372     OPENSSL_free(buf);
373     if (tmpin == indata) {
374         if (indata)
375             BIO_pop(p7bio);
376     }
377     BIO_free_all(p7bio);
378     sk_X509_free(signers);
379     return ret;
380 }
381
382 STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
383                                    int flags)
384 {
385     STACK_OF(X509) *signers;
386     STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
387     PKCS7_SIGNER_INFO *si;
388     PKCS7_ISSUER_AND_SERIAL *ias;
389     X509 *signer;
390     int i;
391
392     if (p7 == NULL) {
393         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_INVALID_NULL_POINTER);
394         return NULL;
395     }
396
397     if (!PKCS7_type_is_signed(p7)) {
398         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_WRONG_CONTENT_TYPE);
399         return NULL;
400     }
401
402     /* Collect all the signers together */
403
404     sinfos = PKCS7_get_signer_info(p7);
405
406     if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
407         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_NO_SIGNERS);
408         return 0;
409     }
410
411     if ((signers = sk_X509_new_null()) == NULL) {
412         PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, ERR_R_MALLOC_FAILURE);
413         return NULL;
414     }
415
416     for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
417         si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
418         ias = si->issuer_and_serial;
419         signer = NULL;
420         /* If any certificates passed they take priority */
421         if (certs)
422             signer = X509_find_by_issuer_and_serial(certs,
423                                                     ias->issuer, ias->serial);
424         if (!signer && !(flags & PKCS7_NOINTERN)
425             && p7->d.sign->cert)
426             signer =
427                 X509_find_by_issuer_and_serial(p7->d.sign->cert,
428                                                ias->issuer, ias->serial);
429         if (!signer) {
430             PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,
431                      PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
432             sk_X509_free(signers);
433             return 0;
434         }
435
436         if (!sk_X509_push(signers, signer)) {
437             sk_X509_free(signers);
438             return NULL;
439         }
440     }
441     return signers;
442 }
443
444 /* Build a complete PKCS#7 enveloped data */
445
446 PKCS7 *PKCS7_encrypt_ex(STACK_OF(X509) *certs, BIO *in,
447                         const EVP_CIPHER *cipher, int flags,
448                         OSSL_LIB_CTX *libctx, const char *propq)
449 {
450     PKCS7 *p7;
451     BIO *p7bio = NULL;
452     int i;
453     X509 *x509;
454
455     if ((p7 = PKCS7_new_ex(libctx, propq)) == NULL) {
456         PKCS7err(0, ERR_R_MALLOC_FAILURE);
457         return NULL;
458     }
459
460     if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
461         goto err;
462     if (!PKCS7_set_cipher(p7, cipher)) {
463         PKCS7err(0, PKCS7_R_ERROR_SETTING_CIPHER);
464         goto err;
465     }
466
467     for (i = 0; i < sk_X509_num(certs); i++) {
468         x509 = sk_X509_value(certs, i);
469         if (!PKCS7_add_recipient(p7, x509)) {
470             PKCS7err(0, PKCS7_R_ERROR_ADDING_RECIPIENT);
471             goto err;
472         }
473     }
474
475     if (flags & PKCS7_STREAM)
476         return p7;
477
478     if (PKCS7_final(p7, in, flags))
479         return p7;
480
481  err:
482
483     BIO_free_all(p7bio);
484     PKCS7_free(p7);
485     return NULL;
486
487 }
488
489 PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
490                      int flags)
491 {
492     return PKCS7_encrypt_ex(certs, in, cipher, flags, NULL, NULL);
493 }
494
495
496 int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
497 {
498     BIO *tmpmem;
499     int ret = 0, i;
500     char *buf = NULL;
501
502     if (p7 == NULL) {
503         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_INVALID_NULL_POINTER);
504         return 0;
505     }
506
507     if (!PKCS7_type_is_enveloped(p7)) {
508         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_WRONG_CONTENT_TYPE);
509         return 0;
510     }
511
512     if (cert && !X509_check_private_key(cert, pkey)) {
513         PKCS7err(PKCS7_F_PKCS7_DECRYPT,
514                  PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
515         return 0;
516     }
517
518     if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) {
519         PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR);
520         return 0;
521     }
522
523     if (flags & PKCS7_TEXT) {
524         BIO *tmpbuf, *bread;
525         /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
526         if ((tmpbuf = BIO_new(BIO_f_buffer())) == NULL) {
527             PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
528             BIO_free_all(tmpmem);
529             return 0;
530         }
531         if ((bread = BIO_push(tmpbuf, tmpmem)) == NULL) {
532             PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
533             BIO_free_all(tmpbuf);
534             BIO_free_all(tmpmem);
535             return 0;
536         }
537         ret = SMIME_text(bread, data);
538         if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
539             if (!BIO_get_cipher_status(tmpmem))
540                 ret = 0;
541         }
542         BIO_free_all(bread);
543         return ret;
544     }
545     if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL) {
546         PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
547         goto err;
548     }
549     for (;;) {
550         i = BIO_read(tmpmem, buf, BUFFERSIZE);
551         if (i <= 0) {
552             ret = 1;
553             if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
554                 if (!BIO_get_cipher_status(tmpmem))
555                     ret = 0;
556             }
557
558             break;
559         }
560         if (BIO_write(data, buf, i) != i) {
561             break;
562         }
563     }
564 err:
565     OPENSSL_free(buf);
566     BIO_free_all(tmpmem);
567     return ret;
568 }