1 /*
2  * Copyright 2008-2023 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 #include "internal/cryptlib.h"
11 #include <openssl/asn1t.h>
12 #include <openssl/x509.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/err.h>
15 #include <openssl/cms.h>
16 #include "cms_local.h"
17 #include "crypto/asn1.h"
18 
cms_get_text_bio(BIO *out, unsigned int flags)19 static BIO *cms_get_text_bio(BIO *out, unsigned int flags)
20 {
21     BIO *rbio;
22 
23     if (out == NULL)
24         rbio = BIO_new(BIO_s_null());
25     else if (flags & CMS_TEXT) {
26         rbio = BIO_new(BIO_s_mem());
27         BIO_set_mem_eof_return(rbio, 0);
28     } else
29         rbio = out;
30     return rbio;
31 }
32 
cms_copy_content(BIO *out, BIO *in, unsigned int flags)33 static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
34 {
35     unsigned char buf[4096];
36     int r = 0, i;
37     BIO *tmpout;
38 
39     tmpout = cms_get_text_bio(out, flags);
40 
41     if (tmpout == NULL) {
42         ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
43         goto err;
44     }
45 
46     /* Read all content through chain to process digest, decrypt etc */
47     for (;;) {
48         i = BIO_read(in, buf, sizeof(buf));
49         if (i <= 0) {
50             if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
51                 if (BIO_get_cipher_status(in) <= 0)
52                     goto err;
53             }
54             if (i < 0)
55                 goto err;
56             break;
57         }
58 
59         if (tmpout != NULL && (BIO_write(tmpout, buf, i) != i))
60             goto err;
61     }
62 
63     if (flags & CMS_TEXT) {
64         if (!SMIME_text(tmpout, out)) {
65             ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
66             goto err;
67         }
68     }
69 
70     r = 1;
71  err:
72     if (tmpout != out)
73         BIO_free(tmpout);
74     return r;
75 
76 }
77 
check_content(CMS_ContentInfo *cms)78 static int check_content(CMS_ContentInfo *cms)
79 {
80     ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
81 
82     if (pos == NULL || *pos == NULL) {
83         ERR_raise(ERR_LIB_CMS, CMS_R_NO_CONTENT);
84         return 0;
85     }
86     return 1;
87 }
88 
do_free_upto(BIO *f, BIO *upto)89 static void do_free_upto(BIO *f, BIO *upto)
90 {
91     if (upto != NULL) {
92         BIO *tbio;
93 
94         do {
95             tbio = BIO_pop(f);
96             BIO_free(f);
97             f = tbio;
98         } while (f != NULL && f != upto);
99     } else {
100         BIO_free_all(f);
101     }
102 }
103 
CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)104 int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
105 {
106     BIO *cont;
107     int r;
108 
109     if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
110         ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DATA);
111         return 0;
112     }
113     cont = CMS_dataInit(cms, NULL);
114     if (cont == NULL)
115         return 0;
116     r = cms_copy_content(out, cont, flags);
117     BIO_free_all(cont);
118     return r;
119 }
120 
CMS_data_create_ex(BIO *in, unsigned int flags, OSSL_LIB_CTX *libctx, const char *propq)121 CMS_ContentInfo *CMS_data_create_ex(BIO *in, unsigned int flags,
122                                     OSSL_LIB_CTX *libctx, const char *propq)
123 {
124     CMS_ContentInfo *cms = ossl_cms_Data_create(libctx, propq);
125 
126     if (cms == NULL)
127         return NULL;
128 
129     if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
130         return cms;
131 
132     CMS_ContentInfo_free(cms);
133     return NULL;
134 }
135 
CMS_data_create(BIO *in, unsigned int flags)136 CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
137 {
138     return CMS_data_create_ex(in, flags, NULL, NULL);
139 }
140 
CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out, unsigned int flags)141 int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
142                       unsigned int flags)
143 {
144     BIO *cont;
145     int r;
146 
147     if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
148         ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DIGESTED_DATA);
149         return 0;
150     }
151 
152     if (dcont == NULL && !check_content(cms))
153         return 0;
154 
155     cont = CMS_dataInit(cms, dcont);
156     if (cont == NULL)
157         return 0;
158 
159     r = cms_copy_content(out, cont, flags);
160     if (r)
161         r = ossl_cms_DigestedData_do_final(cms, cont, 1);
162     do_free_upto(cont, dcont);
163     return r;
164 }
165 
CMS_digest_create_ex(BIO *in, const EVP_MD *md, unsigned int flags, OSSL_LIB_CTX *ctx, const char *propq)166 CMS_ContentInfo *CMS_digest_create_ex(BIO *in, const EVP_MD *md,
167                                       unsigned int flags, OSSL_LIB_CTX *ctx,
168                                       const char *propq)
169 {
170     CMS_ContentInfo *cms;
171 
172     /*
173      * Because the EVP_MD is cached and can be a legacy algorithm, we
174      * cannot fetch the algorithm if it isn't supplied.
175      */
176     if (md == NULL)
177         md = EVP_sha1();
178     cms = ossl_cms_DigestedData_create(md, ctx, propq);
179     if (cms == NULL)
180         return NULL;
181 
182     if (!(flags & CMS_DETACHED))
183         CMS_set_detached(cms, 0);
184 
185     if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
186         return cms;
187 
188     CMS_ContentInfo_free(cms);
189     return NULL;
190 }
191 
CMS_digest_create(BIO *in, const EVP_MD *md, unsigned int flags)192 CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
193                                    unsigned int flags)
194 {
195     return CMS_digest_create_ex(in, md, flags, NULL, NULL);
196 }
197 
CMS_EncryptedData_decrypt(CMS_ContentInfo *cms, const unsigned char *key, size_t keylen, BIO *dcont, BIO *out, unsigned int flags)198 int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
199                               const unsigned char *key, size_t keylen,
200                               BIO *dcont, BIO *out, unsigned int flags)
201 {
202     BIO *cont;
203     int r;
204 
205     if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
206         ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENCRYPTED_DATA);
207         return 0;
208     }
209 
210     if (dcont == NULL && !check_content(cms))
211         return 0;
212 
213     if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
214         return 0;
215     cont = CMS_dataInit(cms, dcont);
216     if (cont == NULL)
217         return 0;
218     r = cms_copy_content(out, cont, flags);
219     do_free_upto(cont, dcont);
220     return r;
221 }
222 
CMS_EncryptedData_encrypt_ex(BIO *in, const EVP_CIPHER *cipher, const unsigned char *key, size_t keylen, unsigned int flags, OSSL_LIB_CTX *libctx, const char *propq)223 CMS_ContentInfo *CMS_EncryptedData_encrypt_ex(BIO *in, const EVP_CIPHER *cipher,
224                                               const unsigned char *key,
225                                               size_t keylen, unsigned int flags,
226                                               OSSL_LIB_CTX *libctx,
227                                               const char *propq)
228 {
229     CMS_ContentInfo *cms;
230 
231     if (cipher == NULL) {
232         ERR_raise(ERR_LIB_CMS, CMS_R_NO_CIPHER);
233         return NULL;
234     }
235     cms = CMS_ContentInfo_new_ex(libctx, propq);
236     if (cms == NULL)
237         return NULL;
238     if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
239         goto err;
240 
241     if (!(flags & CMS_DETACHED))
242         CMS_set_detached(cms, 0);
243 
244     if ((flags & (CMS_STREAM | CMS_PARTIAL))
245         || CMS_final(cms, in, NULL, flags))
246         return cms;
247 
248  err:
249     CMS_ContentInfo_free(cms);
250     return NULL;
251 }
252 
CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher, const unsigned char *key, size_t keylen, unsigned int flags)253 CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
254                                            const unsigned char *key,
255                                            size_t keylen, unsigned int flags)
256 {
257     return CMS_EncryptedData_encrypt_ex(in, cipher, key, keylen, flags, NULL,
258                                         NULL);
259 }
260 
cms_signerinfo_verify_cert(CMS_SignerInfo *si, X509_STORE *store, STACK_OF(X509) *certs, STACK_OF(X509_CRL) *crls, STACK_OF(X509) **chain, const CMS_CTX *cms_ctx)261 static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
262                                       X509_STORE *store,
263                                       STACK_OF(X509) *certs,
264                                       STACK_OF(X509_CRL) *crls,
265                                       STACK_OF(X509) **chain,
266                                       const CMS_CTX *cms_ctx)
267 {
268     X509_STORE_CTX *ctx;
269     X509 *signer;
270     int i, j, r = 0;
271 
272     ctx = X509_STORE_CTX_new_ex(ossl_cms_ctx_get0_libctx(cms_ctx),
273                                 ossl_cms_ctx_get0_propq(cms_ctx));
274     if (ctx == NULL) {
275         ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
276         goto err;
277     }
278     CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
279     if (!X509_STORE_CTX_init(ctx, store, signer, certs)) {
280         ERR_raise(ERR_LIB_CMS, CMS_R_STORE_INIT_ERROR);
281         goto err;
282     }
283     X509_STORE_CTX_set_default(ctx, "smime_sign");
284     if (crls != NULL)
285         X509_STORE_CTX_set0_crls(ctx, crls);
286 
287     i = X509_verify_cert(ctx);
288     if (i <= 0) {
289         j = X509_STORE_CTX_get_error(ctx);
290         ERR_raise_data(ERR_LIB_CMS, CMS_R_CERTIFICATE_VERIFY_ERROR,
291                        "Verify error: %s", X509_verify_cert_error_string(j));
292         goto err;
293     }
294     r = 1;
295 
296     /* also send back the trust chain when required */
297     if (chain != NULL)
298         *chain = X509_STORE_CTX_get1_chain(ctx);
299  err:
300     X509_STORE_CTX_free(ctx);
301     return r;
302 
303 }
304 
CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs, X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)305 int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
306                X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
307 {
308     CMS_SignerInfo *si;
309     STACK_OF(CMS_SignerInfo) *sinfos;
310     STACK_OF(X509) *cms_certs = NULL;
311     STACK_OF(X509_CRL) *crls = NULL;
312     STACK_OF(X509) **si_chains = NULL;
313     X509 *signer;
314     int i, scount = 0, ret = 0;
315     BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
316     int cadesVerify = (flags & CMS_CADES) != 0;
317     const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
318 
319     if (dcont == NULL && !check_content(cms))
320         return 0;
321     if (dcont != NULL && !(flags & CMS_BINARY)) {
322         const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
323 
324         if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
325             flags |= CMS_ASCIICRLF;
326     }
327 
328     /* Attempt to find all signer certificates */
329 
330     sinfos = CMS_get0_SignerInfos(cms);
331 
332     if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
333         ERR_raise(ERR_LIB_CMS, CMS_R_NO_SIGNERS);
334         goto err;
335     }
336 
337     for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
338         si = sk_CMS_SignerInfo_value(sinfos, i);
339         CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
340         if (signer)
341             scount++;
342     }
343 
344     if (scount != sk_CMS_SignerInfo_num(sinfos))
345         scount += CMS_set1_signers_certs(cms, certs, flags);
346 
347     if (scount != sk_CMS_SignerInfo_num(sinfos)) {
348         ERR_raise(ERR_LIB_CMS, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
349         goto err;
350     }
351 
352     /* Attempt to verify all signers certs */
353     /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
354 
355     if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
356         if (cadesVerify) {
357             /* Certificate trust chain is required to check CAdES signature */
358             si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
359             if (si_chains == NULL) {
360                 ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
361                 goto err;
362             }
363         }
364         cms_certs = CMS_get1_certs(cms);
365         if (!(flags & CMS_NOCRL))
366             crls = CMS_get1_crls(cms);
367         for (i = 0; i < scount; i++) {
368             si = sk_CMS_SignerInfo_value(sinfos, i);
369 
370             if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
371                                             si_chains ? &si_chains[i] : NULL,
372                                             ctx))
373                 goto err;
374         }
375     }
376 
377     /* Attempt to verify all SignerInfo signed attribute signatures */
378 
379     if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
380         for (i = 0; i < scount; i++) {
381             si = sk_CMS_SignerInfo_value(sinfos, i);
382             if (CMS_signed_get_attr_count(si) < 0)
383                 continue;
384             if (CMS_SignerInfo_verify(si) <= 0)
385                 goto err;
386             if (cadesVerify) {
387                 STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
388 
389                 if (ossl_cms_check_signing_certs(si, si_chain) <= 0)
390                     goto err;
391             }
392         }
393     }
394 
395     /*
396      * Performance optimization: if the content is a memory BIO then store
397      * its contents in a temporary read only memory BIO. This avoids
398      * potentially large numbers of slow copies of data which will occur when
399      * reading from a read write memory BIO when signatures are calculated.
400      */
401 
402     if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
403         char *ptr;
404         long len;
405 
406         len = BIO_get_mem_data(dcont, &ptr);
407         tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len);
408         if (tmpin == NULL) {
409             ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
410             goto err2;
411         }
412     } else {
413         tmpin = dcont;
414     }
415     /*
416      * If not binary mode and detached generate digests by *writing* through
417      * the BIO. That makes it possible to canonicalise the input.
418      */
419     if (!(flags & SMIME_BINARY) && dcont) {
420         /*
421          * Create output BIO so we can either handle text or to ensure
422          * included content doesn't override detached content.
423          */
424         tmpout = cms_get_text_bio(out, flags);
425         if (tmpout == NULL) {
426             ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
427             goto err;
428         }
429         cmsbio = CMS_dataInit(cms, tmpout);
430         if (cmsbio == NULL)
431             goto err;
432         /*
433          * Don't use SMIME_TEXT for verify: it adds headers and we want to
434          * remove them.
435          */
436         if (!SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT))
437             goto err;
438 
439         if (flags & CMS_TEXT) {
440             if (!SMIME_text(tmpout, out)) {
441                 ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
442                 goto err;
443             }
444         }
445     } else {
446         cmsbio = CMS_dataInit(cms, tmpin);
447         if (cmsbio == NULL)
448             goto err;
449 
450         if (!cms_copy_content(out, cmsbio, flags))
451             goto err;
452 
453     }
454     if (!(flags & CMS_NO_CONTENT_VERIFY)) {
455         for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
456             si = sk_CMS_SignerInfo_value(sinfos, i);
457             if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
458                 ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_VERIFY_ERROR);
459                 goto err;
460             }
461         }
462     }
463 
464     ret = 1;
465  err:
466     if (!(flags & SMIME_BINARY) && dcont) {
467         do_free_upto(cmsbio, tmpout);
468         if (tmpin != dcont)
469             BIO_free(tmpin);
470     } else {
471         if (dcont && (tmpin == dcont))
472             do_free_upto(cmsbio, dcont);
473         else
474             BIO_free_all(cmsbio);
475     }
476 
477     if (out != tmpout)
478         BIO_free_all(tmpout);
479 
480  err2:
481     if (si_chains != NULL) {
482         for (i = 0; i < scount; ++i)
483             sk_X509_pop_free(si_chains[i], X509_free);
484         OPENSSL_free(si_chains);
485     }
486     sk_X509_pop_free(cms_certs, X509_free);
487     sk_X509_CRL_pop_free(crls, X509_CRL_free);
488 
489     return ret;
490 }
491 
CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms, STACK_OF(X509) *certs, X509_STORE *store, unsigned int flags)492 int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
493                        STACK_OF(X509) *certs,
494                        X509_STORE *store, unsigned int flags)
495 {
496     int r;
497 
498     flags &= ~(CMS_DETACHED | CMS_TEXT);
499     r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
500     if (r <= 0)
501         return r;
502     return ossl_cms_Receipt_verify(rcms, ocms);
503 }
504 
CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, BIO *data, unsigned int flags, OSSL_LIB_CTX *libctx, const char *propq)505 CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
506                              STACK_OF(X509) *certs, BIO *data,
507                              unsigned int flags, OSSL_LIB_CTX *libctx,
508                              const char *propq)
509 {
510     CMS_ContentInfo *cms;
511     int i;
512 
513     cms = CMS_ContentInfo_new_ex(libctx, propq);
514     if (cms == NULL || !CMS_SignedData_init(cms))
515         goto merr;
516     if (flags & CMS_ASCIICRLF
517         && !CMS_set1_eContentType(cms,
518                                   OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
519         goto err;
520 
521     if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
522         ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
523         goto err;
524     }
525 
526     for (i = 0; i < sk_X509_num(certs); i++) {
527         X509 *x = sk_X509_value(certs, i);
528 
529         if (!CMS_add1_cert(cms, x))
530             goto merr;
531     }
532 
533     if (!(flags & CMS_DETACHED))
534         CMS_set_detached(cms, 0);
535 
536     if ((flags & (CMS_STREAM | CMS_PARTIAL))
537         || CMS_final(cms, data, NULL, flags))
538         return cms;
539     else
540         goto err;
541 
542  merr:
543     ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
544 
545  err:
546     CMS_ContentInfo_free(cms);
547     return NULL;
548 }
549 
CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, BIO *data, unsigned int flags)550 CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
551                           BIO *data, unsigned int flags)
552 {
553     return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
554 }
555 
CMS_sign_receipt(CMS_SignerInfo *si, X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs, unsigned int flags)556 CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
557                                   X509 *signcert, EVP_PKEY *pkey,
558                                   STACK_OF(X509) *certs, unsigned int flags)
559 {
560     CMS_SignerInfo *rct_si;
561     CMS_ContentInfo *cms = NULL;
562     ASN1_OCTET_STRING **pos, *os;
563     BIO *rct_cont = NULL;
564     int r = 0;
565     const CMS_CTX *ctx = si->cms_ctx;
566 
567     flags &= ~(CMS_STREAM | CMS_TEXT);
568     /* Not really detached but avoids content being allocated */
569     flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
570     if (pkey == NULL || signcert == NULL) {
571         ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY_OR_CERT);
572         return NULL;
573     }
574 
575     /* Initialize signed data */
576 
577     cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags,
578                       ossl_cms_ctx_get0_libctx(ctx),
579                       ossl_cms_ctx_get0_propq(ctx));
580     if (cms == NULL)
581         goto err;
582 
583     /* Set inner content type to signed receipt */
584     if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
585         goto err;
586 
587     rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
588     if (!rct_si) {
589         ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
590         goto err;
591     }
592 
593     os = ossl_cms_encode_Receipt(si);
594     if (os == NULL)
595         goto err;
596 
597     /* Set content to digest */
598     rct_cont = BIO_new_mem_buf(os->data, os->length);
599     if (rct_cont == NULL)
600         goto err;
601 
602     /* Add msgSigDigest attribute */
603 
604     if (!ossl_cms_msgSigDigest_add1(rct_si, si))
605         goto err;
606 
607     /* Finalize structure */
608     if (!CMS_final(cms, rct_cont, NULL, flags))
609         goto err;
610 
611     /* Set embedded content */
612     pos = CMS_get0_content(cms);
613     if (pos == NULL)
614         goto err;
615     *pos = os;
616 
617     r = 1;
618 
619  err:
620     BIO_free(rct_cont);
621     if (r)
622         return cms;
623     CMS_ContentInfo_free(cms);
624     return NULL;
625 
626 }
627 
STACK_OFnull628 CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
629                                 const EVP_CIPHER *cipher, unsigned int flags,
630                                 OSSL_LIB_CTX *libctx, const char *propq)
631 {
632     CMS_ContentInfo *cms;
633     int i;
634     X509 *recip;
635 
636 
637     cms = (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
638           ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
639           : CMS_EnvelopedData_create_ex(cipher, libctx, propq);
640     if (cms == NULL)
641         goto merr;
642     for (i = 0; i < sk_X509_num(certs); i++) {
643         recip = sk_X509_value(certs, i);
644         if (!CMS_add1_recipient_cert(cms, recip, flags)) {
645             ERR_raise(ERR_LIB_CMS, CMS_R_RECIPIENT_ERROR);
646             goto err;
647         }
648     }
649 
650     if (!(flags & CMS_DETACHED))
651         CMS_set_detached(cms, 0);
652 
653     if ((flags & (CMS_STREAM | CMS_PARTIAL))
654         || CMS_final(cms, data, NULL, flags))
655         return cms;
656     else
657         goto err;
658 
659  merr:
660     ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
661  err:
662     CMS_ContentInfo_free(cms);
663     return NULL;
664 }
665 
STACK_OFnull666 CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
667                              const EVP_CIPHER *cipher, unsigned int flags)
668 {
669     return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL);
670 }
671 
cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms, CMS_RecipientInfo *ri, EVP_PKEY *pk, X509 *cert, X509 *peer)672 static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms,
673                                        CMS_RecipientInfo *ri,
674                                        EVP_PKEY *pk, X509 *cert, X509 *peer)
675 {
676     int i;
677     STACK_OF(CMS_RecipientEncryptedKey) *reks;
678     CMS_RecipientEncryptedKey *rek;
679 
680     reks = CMS_RecipientInfo_kari_get0_reks(ri);
681     for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
682         int rv;
683 
684         rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
685         if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
686             continue;
687         CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
688         rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
689         CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
690         if (rv > 0)
691             return 1;
692         return cert == NULL ? 0 : -1;
693     }
694     return 0;
695 }
696 
CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)697 int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
698 {
699      return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
700 }
701 
CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert, X509 *peer)702 int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
703                                    X509 *cert, X509 *peer)
704 {
705     STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
706     CMS_RecipientInfo *ri;
707     int i, r, cms_pkey_ri_type;
708     int debug = 0, match_ri = 0;
709     CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
710 
711     /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
712     if (ec != NULL) {
713         OPENSSL_clear_free(ec->key, ec->keylen);
714         ec->key = NULL;
715         ec->keylen = 0;
716     }
717 
718     if (ris != NULL && ec != NULL)
719         debug = ec->debug;
720 
721     cms_pkey_ri_type = ossl_cms_pkey_get_ri_type(pk);
722     if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
723          ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
724          return 0;
725     }
726 
727     for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
728         int ri_type;
729 
730         ri = sk_CMS_RecipientInfo_value(ris, i);
731         ri_type = CMS_RecipientInfo_type(ri);
732         if (!ossl_cms_pkey_is_ri_type_supported(pk, ri_type))
733             continue;
734         match_ri = 1;
735         if (ri_type == CMS_RECIPINFO_AGREE) {
736             r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
737             if (r > 0)
738                 return 1;
739             if (r < 0)
740                 return 0;
741         }
742         /* If we have a cert, try matching RecipientInfo, else try them all */
743         else if (cert == NULL || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
744             EVP_PKEY_up_ref(pk);
745             CMS_RecipientInfo_set0_pkey(ri, pk);
746             r = CMS_RecipientInfo_decrypt(cms, ri);
747             CMS_RecipientInfo_set0_pkey(ri, NULL);
748             if (cert != NULL) {
749                 /*
750                  * If not debugging clear any error and return success to
751                  * avoid leaking of information useful to MMA
752                  */
753                 if (!debug) {
754                     ERR_clear_error();
755                     return 1;
756                 }
757                 if (r > 0)
758                     return 1;
759                 ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
760                 return 0;
761             }
762             /*
763              * If no cert and not debugging don't leave loop after first
764              * successful decrypt. Always attempt to decrypt all recipients
765              * to avoid leaking timing of a successful decrypt.
766              */
767             else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
768                 return 1;
769         }
770     }
771     /* If no cert, key transport and not debugging always return success */
772     if (cert == NULL
773         && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
774         && match_ri
775         && !debug) {
776         ERR_clear_error();
777         return 1;
778     }
779 
780     if (!match_ri)
781         ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
782     return 0;
783 
784 }
785 
CMS_decrypt_set1_key(CMS_ContentInfo *cms, unsigned char *key, size_t keylen, const unsigned char *id, size_t idlen)786 int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
787                          unsigned char *key, size_t keylen,
788                          const unsigned char *id, size_t idlen)
789 {
790     STACK_OF(CMS_RecipientInfo) *ris;
791     CMS_RecipientInfo *ri;
792     int i, r, match_ri = 0;
793 
794     ris = CMS_get0_RecipientInfos(cms);
795     for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
796         ri = sk_CMS_RecipientInfo_value(ris, i);
797         if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
798             continue;
799 
800         /* If we have an id, try matching RecipientInfo, else try them all */
801         if (id == NULL
802                 || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
803             match_ri = 1;
804             CMS_RecipientInfo_set0_key(ri, key, keylen);
805             r = CMS_RecipientInfo_decrypt(cms, ri);
806             CMS_RecipientInfo_set0_key(ri, NULL, 0);
807             if (r > 0)
808                 return 1;
809             if (id != NULL) {
810                 ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
811                 return 0;
812             }
813             ERR_clear_error();
814         }
815     }
816 
817     if (!match_ri)
818         ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
819     return 0;
820 
821 }
822 
CMS_decrypt_set1_password(CMS_ContentInfo *cms, unsigned char *pass, ossl_ssize_t passlen)823 int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
824                               unsigned char *pass, ossl_ssize_t passlen)
825 {
826     STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
827     CMS_RecipientInfo *ri;
828     int i, r, match_ri = 0;
829     CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
830 
831     /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
832     if (ec != NULL) {
833         OPENSSL_clear_free(ec->key, ec->keylen);
834         ec->key = NULL;
835         ec->keylen = 0;
836     }
837 
838     for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
839         ri = sk_CMS_RecipientInfo_value(ris, i);
840         if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
841             continue;
842 
843         /* Must try each PasswordRecipientInfo */
844         match_ri = 1;
845         CMS_RecipientInfo_set0_password(ri, pass, passlen);
846         r = CMS_RecipientInfo_decrypt(cms, ri);
847         CMS_RecipientInfo_set0_password(ri, NULL, 0);
848         if (r > 0)
849             return 1;
850     }
851 
852     if (!match_ri)
853         ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
854     return 0;
855 
856 }
857 
CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert, BIO *dcont, BIO *out, unsigned int flags)858 int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
859                 BIO *dcont, BIO *out, unsigned int flags)
860 {
861     int r;
862     BIO *cont;
863     CMS_EncryptedContentInfo *ec;
864     int nid = OBJ_obj2nid(CMS_get0_type(cms));
865 
866     if (nid != NID_pkcs7_enveloped
867             && nid != NID_id_smime_ct_authEnvelopedData) {
868         ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENVELOPED_DATA);
869         return 0;
870     }
871     if (dcont == NULL && !check_content(cms))
872         return 0;
873     ec = ossl_cms_get0_env_enc_content(cms);
874     ec->debug = (flags & CMS_DEBUG_DECRYPT) != 0;
875     ec->havenocert = cert == NULL;
876     if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
877         return 1;
878     if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
879         return 0;
880     cont = CMS_dataInit(cms, dcont);
881     if (cont == NULL)
882         return 0;
883     r = cms_copy_content(out, cont, flags);
884     do_free_upto(cont, dcont);
885     return r;
886 }
887 
CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)888 int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
889 {
890     BIO *cmsbio;
891     int ret = 0;
892 
893     if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
894         ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
895         return 0;
896     }
897 
898     if (!SMIME_crlf_copy(data, cmsbio, flags)) {
899         goto err;
900     }
901 
902     (void)BIO_flush(cmsbio);
903 
904     if (!CMS_dataFinal(cms, cmsbio)) {
905         ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
906         goto err;
907     }
908 
909     ret = 1;
910 
911 err:
912     do_free_upto(cmsbio, dcont);
913 
914     return ret;
915 
916 }
917 
918 #ifdef ZLIB
919 
CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, unsigned int flags)920 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
921                    unsigned int flags)
922 {
923     BIO *cont;
924     int r;
925 
926     if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
927         ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
928         return 0;
929     }
930 
931     if (dcont == NULL && !check_content(cms))
932         return 0;
933 
934     cont = CMS_dataInit(cms, dcont);
935     if (cont == NULL)
936         return 0;
937     r = cms_copy_content(out, cont, flags);
938     do_free_upto(cont, dcont);
939     return r;
940 }
941 
CMS_compress(BIO *in, int comp_nid, unsigned int flags)942 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
943 {
944     CMS_ContentInfo *cms;
945 
946     if (comp_nid <= 0)
947         comp_nid = NID_zlib_compression;
948     cms = ossl_cms_CompressedData_create(comp_nid, NULL, NULL);
949     if (cms == NULL)
950         return NULL;
951 
952     if (!(flags & CMS_DETACHED))
953         CMS_set_detached(cms, 0);
954 
955     if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
956         return cms;
957 
958     CMS_ContentInfo_free(cms);
959     return NULL;
960 }
961 
962 #else
963 
CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, unsigned int flags)964 int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
965                    unsigned int flags)
966 {
967     ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
968     return 0;
969 }
970 
CMS_compress(BIO *in, int comp_nid, unsigned int flags)971 CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
972 {
973     ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
974     return NULL;
975 }
976 
977 #endif
978