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