cms_smime.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /* crypto/cms/cms_smime.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. */
  54. #include "internal/cryptlib.h"
  55. #include <openssl/asn1t.h>
  56. #include <openssl/x509.h>
  57. #include <openssl/x509v3.h>
  58. #include <openssl/err.h>
  59. #include <openssl/cms.h>
  60. #include "cms_lcl.h"
  61. #include "internal/asn1_int.h"
  62. static BIO *cms_get_text_bio(BIO *out, unsigned int flags)
  63. {
  64. BIO *rbio;
  65. if (out == NULL)
  66. rbio = BIO_new(BIO_s_null());
  67. else if (flags & CMS_TEXT) {
  68. rbio = BIO_new(BIO_s_mem());
  69. BIO_set_mem_eof_return(rbio, 0);
  70. } else
  71. rbio = out;
  72. return rbio;
  73. }
  74. static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
  75. {
  76. unsigned char buf[4096];
  77. int r = 0, i;
  78. BIO *tmpout;
  79. tmpout = cms_get_text_bio(out, flags);
  80. if (tmpout == NULL) {
  81. CMSerr(CMS_F_CMS_COPY_CONTENT, ERR_R_MALLOC_FAILURE);
  82. goto err;
  83. }
  84. /* Read all content through chain to process digest, decrypt etc */
  85. for (;;) {
  86. i = BIO_read(in, buf, sizeof(buf));
  87. if (i <= 0) {
  88. if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
  89. if (!BIO_get_cipher_status(in))
  90. goto err;
  91. }
  92. if (i < 0)
  93. goto err;
  94. break;
  95. }
  96. if (tmpout && (BIO_write(tmpout, buf, i) != i))
  97. goto err;
  98. }
  99. if (flags & CMS_TEXT) {
  100. if (!SMIME_text(tmpout, out)) {
  101. CMSerr(CMS_F_CMS_COPY_CONTENT, CMS_R_SMIME_TEXT_ERROR);
  102. goto err;
  103. }
  104. }
  105. r = 1;
  106. err:
  107. if (tmpout != out)
  108. BIO_free(tmpout);
  109. return r;
  110. }
  111. static int check_content(CMS_ContentInfo *cms)
  112. {
  113. ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
  114. if (!pos || !*pos) {
  115. CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
  116. return 0;
  117. }
  118. return 1;
  119. }
  120. static void do_free_upto(BIO *f, BIO *upto)
  121. {
  122. if (upto) {
  123. BIO *tbio;
  124. do {
  125. tbio = BIO_pop(f);
  126. BIO_free(f);
  127. f = tbio;
  128. }
  129. while (f && f != upto);
  130. } else
  131. BIO_free_all(f);
  132. }
  133. int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
  134. {
  135. BIO *cont;
  136. int r;
  137. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
  138. CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
  139. return 0;
  140. }
  141. cont = CMS_dataInit(cms, NULL);
  142. if (!cont)
  143. return 0;
  144. r = cms_copy_content(out, cont, flags);
  145. BIO_free_all(cont);
  146. return r;
  147. }
  148. CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
  149. {
  150. CMS_ContentInfo *cms;
  151. cms = cms_Data_create();
  152. if (!cms)
  153. return NULL;
  154. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  155. return cms;
  156. CMS_ContentInfo_free(cms);
  157. return NULL;
  158. }
  159. int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  160. unsigned int flags)
  161. {
  162. BIO *cont;
  163. int r;
  164. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
  165. CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
  166. return 0;
  167. }
  168. if (!dcont && !check_content(cms))
  169. return 0;
  170. cont = CMS_dataInit(cms, dcont);
  171. if (!cont)
  172. return 0;
  173. r = cms_copy_content(out, cont, flags);
  174. if (r)
  175. r = cms_DigestedData_do_final(cms, cont, 1);
  176. do_free_upto(cont, dcont);
  177. return r;
  178. }
  179. CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
  180. unsigned int flags)
  181. {
  182. CMS_ContentInfo *cms;
  183. if (!md)
  184. md = EVP_sha1();
  185. cms = cms_DigestedData_create(md);
  186. if (!cms)
  187. return NULL;
  188. if (!(flags & CMS_DETACHED))
  189. CMS_set_detached(cms, 0);
  190. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  191. return cms;
  192. CMS_ContentInfo_free(cms);
  193. return NULL;
  194. }
  195. int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
  196. const unsigned char *key, size_t keylen,
  197. BIO *dcont, BIO *out, unsigned int flags)
  198. {
  199. BIO *cont;
  200. int r;
  201. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
  202. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
  203. CMS_R_TYPE_NOT_ENCRYPTED_DATA);
  204. return 0;
  205. }
  206. if (!dcont && !check_content(cms))
  207. return 0;
  208. if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
  209. return 0;
  210. cont = CMS_dataInit(cms, dcont);
  211. if (!cont)
  212. return 0;
  213. r = cms_copy_content(out, cont, flags);
  214. do_free_upto(cont, dcont);
  215. return r;
  216. }
  217. CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
  218. const unsigned char *key,
  219. size_t keylen, unsigned int flags)
  220. {
  221. CMS_ContentInfo *cms;
  222. if (!cipher) {
  223. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
  224. return NULL;
  225. }
  226. cms = CMS_ContentInfo_new();
  227. if (cms == NULL)
  228. return NULL;
  229. if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
  230. return NULL;
  231. if (!(flags & CMS_DETACHED))
  232. CMS_set_detached(cms, 0);
  233. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  234. || CMS_final(cms, in, NULL, flags))
  235. return cms;
  236. CMS_ContentInfo_free(cms);
  237. return NULL;
  238. }
  239. static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
  240. X509_STORE *store,
  241. STACK_OF(X509) *certs,
  242. STACK_OF(X509_CRL) *crls,
  243. unsigned int flags)
  244. {
  245. X509_STORE_CTX ctx;
  246. X509 *signer;
  247. int i, j, r = 0;
  248. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  249. if (!X509_STORE_CTX_init(&ctx, store, signer, certs)) {
  250. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, CMS_R_STORE_INIT_ERROR);
  251. goto err;
  252. }
  253. X509_STORE_CTX_set_default(&ctx, "smime_sign");
  254. if (crls)
  255. X509_STORE_CTX_set0_crls(&ctx, crls);
  256. i = X509_verify_cert(&ctx);
  257. if (i <= 0) {
  258. j = X509_STORE_CTX_get_error(&ctx);
  259. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
  260. CMS_R_CERTIFICATE_VERIFY_ERROR);
  261. ERR_add_error_data(2, "Verify error:",
  262. X509_verify_cert_error_string(j));
  263. goto err;
  264. }
  265. r = 1;
  266. err:
  267. X509_STORE_CTX_cleanup(&ctx);
  268. return r;
  269. }
  270. int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
  271. X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
  272. {
  273. CMS_SignerInfo *si;
  274. STACK_OF(CMS_SignerInfo) *sinfos;
  275. STACK_OF(X509) *cms_certs = NULL;
  276. STACK_OF(X509_CRL) *crls = NULL;
  277. X509 *signer;
  278. int i, scount = 0, ret = 0;
  279. BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
  280. if (!dcont && !check_content(cms))
  281. return 0;
  282. if (dcont && !(flags & CMS_BINARY)) {
  283. const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
  284. if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
  285. flags |= CMS_ASCIICRLF;
  286. }
  287. /* Attempt to find all signer certificates */
  288. sinfos = CMS_get0_SignerInfos(cms);
  289. if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
  290. CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
  291. goto err;
  292. }
  293. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  294. si = sk_CMS_SignerInfo_value(sinfos, i);
  295. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  296. if (signer)
  297. scount++;
  298. }
  299. if (scount != sk_CMS_SignerInfo_num(sinfos))
  300. scount += CMS_set1_signers_certs(cms, certs, flags);
  301. if (scount != sk_CMS_SignerInfo_num(sinfos)) {
  302. CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
  303. goto err;
  304. }
  305. /* Attempt to verify all signers certs */
  306. if (!(flags & CMS_NO_SIGNER_CERT_VERIFY)) {
  307. cms_certs = CMS_get1_certs(cms);
  308. if (!(flags & CMS_NOCRL))
  309. crls = CMS_get1_crls(cms);
  310. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  311. si = sk_CMS_SignerInfo_value(sinfos, i);
  312. if (!cms_signerinfo_verify_cert(si, store,
  313. cms_certs, crls, flags))
  314. goto err;
  315. }
  316. }
  317. /* Attempt to verify all SignerInfo signed attribute signatures */
  318. if (!(flags & CMS_NO_ATTR_VERIFY)) {
  319. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  320. si = sk_CMS_SignerInfo_value(sinfos, i);
  321. if (CMS_signed_get_attr_count(si) < 0)
  322. continue;
  323. if (CMS_SignerInfo_verify(si) <= 0)
  324. goto err;
  325. }
  326. }
  327. /*
  328. * Performance optimization: if the content is a memory BIO then store
  329. * its contents in a temporary read only memory BIO. This avoids
  330. * potentially large numbers of slow copies of data which will occur when
  331. * reading from a read write memory BIO when signatures are calculated.
  332. */
  333. if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
  334. char *ptr;
  335. long len;
  336. len = BIO_get_mem_data(dcont, &ptr);
  337. tmpin = BIO_new_mem_buf(ptr, len);
  338. if (tmpin == NULL) {
  339. CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
  340. goto err2;
  341. }
  342. } else
  343. tmpin = dcont;
  344. /*
  345. * If not binary mode and detached generate digests by *writing* through
  346. * the BIO. That makes it possible to canonicalise the input.
  347. */
  348. if (!(flags & SMIME_BINARY) && dcont) {
  349. /*
  350. * Create output BIO so we can either handle text or to ensure
  351. * included content doesn't override detached content.
  352. */
  353. tmpout = cms_get_text_bio(out, flags);
  354. if (!tmpout) {
  355. CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
  356. goto err;
  357. }
  358. cmsbio = CMS_dataInit(cms, tmpout);
  359. if (!cmsbio)
  360. goto err;
  361. /*
  362. * Don't use SMIME_TEXT for verify: it adds headers and we want to
  363. * remove them.
  364. */
  365. SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
  366. if (flags & CMS_TEXT) {
  367. if (!SMIME_text(tmpout, out)) {
  368. CMSerr(CMS_F_CMS_VERIFY, CMS_R_SMIME_TEXT_ERROR);
  369. goto err;
  370. }
  371. }
  372. } else {
  373. cmsbio = CMS_dataInit(cms, tmpin);
  374. if (!cmsbio)
  375. goto err;
  376. if (!cms_copy_content(out, cmsbio, flags))
  377. goto err;
  378. }
  379. if (!(flags & CMS_NO_CONTENT_VERIFY)) {
  380. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  381. si = sk_CMS_SignerInfo_value(sinfos, i);
  382. if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
  383. CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR);
  384. goto err;
  385. }
  386. }
  387. }
  388. ret = 1;
  389. err:
  390. if (!(flags & SMIME_BINARY) && dcont) {
  391. do_free_upto(cmsbio, tmpout);
  392. if (tmpin != dcont)
  393. BIO_free(tmpin);
  394. } else {
  395. if (dcont && (tmpin == dcont))
  396. do_free_upto(cmsbio, dcont);
  397. else
  398. BIO_free_all(cmsbio);
  399. }
  400. if (out != tmpout)
  401. BIO_free_all(tmpout);
  402. err2:
  403. sk_X509_pop_free(cms_certs, X509_free);
  404. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  405. return ret;
  406. }
  407. int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
  408. STACK_OF(X509) *certs,
  409. X509_STORE *store, unsigned int flags)
  410. {
  411. int r;
  412. flags &= ~(CMS_DETACHED | CMS_TEXT);
  413. r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
  414. if (r <= 0)
  415. return r;
  416. return cms_Receipt_verify(rcms, ocms);
  417. }
  418. CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey,
  419. STACK_OF(X509) *certs, BIO *data,
  420. unsigned int flags)
  421. {
  422. CMS_ContentInfo *cms;
  423. int i;
  424. cms = CMS_ContentInfo_new();
  425. if (cms == NULL || !CMS_SignedData_init(cms))
  426. goto merr;
  427. if (flags & CMS_ASCIICRLF
  428. && !CMS_set1_eContentType(cms,
  429. OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
  430. goto err;
  431. if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
  432. CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
  433. goto err;
  434. }
  435. for (i = 0; i < sk_X509_num(certs); i++) {
  436. X509 *x = sk_X509_value(certs, i);
  437. if (!CMS_add1_cert(cms, x))
  438. goto merr;
  439. }
  440. if (!(flags & CMS_DETACHED))
  441. CMS_set_detached(cms, 0);
  442. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  443. || CMS_final(cms, data, NULL, flags))
  444. return cms;
  445. else
  446. goto err;
  447. merr:
  448. CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
  449. err:
  450. CMS_ContentInfo_free(cms);
  451. return NULL;
  452. }
  453. CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
  454. X509 *signcert, EVP_PKEY *pkey,
  455. STACK_OF(X509) *certs, unsigned int flags)
  456. {
  457. CMS_SignerInfo *rct_si;
  458. CMS_ContentInfo *cms = NULL;
  459. ASN1_OCTET_STRING **pos, *os;
  460. BIO *rct_cont = NULL;
  461. int r = 0;
  462. flags &= ~(CMS_STREAM | CMS_TEXT);
  463. /* Not really detached but avoids content being allocated */
  464. flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
  465. if (!pkey || !signcert) {
  466. CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
  467. return NULL;
  468. }
  469. /* Initialize signed data */
  470. cms = CMS_sign(NULL, NULL, certs, NULL, flags);
  471. if (!cms)
  472. goto err;
  473. /* Set inner content type to signed receipt */
  474. if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
  475. goto err;
  476. rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
  477. if (!rct_si) {
  478. CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
  479. goto err;
  480. }
  481. os = cms_encode_Receipt(si);
  482. if (!os)
  483. goto err;
  484. /* Set content to digest */
  485. rct_cont = BIO_new_mem_buf(os->data, os->length);
  486. if (!rct_cont)
  487. goto err;
  488. /* Add msgSigDigest attribute */
  489. if (!cms_msgSigDigest_add1(rct_si, si))
  490. goto err;
  491. /* Finalize structure */
  492. if (!CMS_final(cms, rct_cont, NULL, flags))
  493. goto err;
  494. /* Set embedded content */
  495. pos = CMS_get0_content(cms);
  496. *pos = os;
  497. r = 1;
  498. err:
  499. BIO_free(rct_cont);
  500. if (r)
  501. return cms;
  502. CMS_ContentInfo_free(cms);
  503. return NULL;
  504. }
  505. CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
  506. const EVP_CIPHER *cipher, unsigned int flags)
  507. {
  508. CMS_ContentInfo *cms;
  509. int i;
  510. X509 *recip;
  511. cms = CMS_EnvelopedData_create(cipher);
  512. if (!cms)
  513. goto merr;
  514. for (i = 0; i < sk_X509_num(certs); i++) {
  515. recip = sk_X509_value(certs, i);
  516. if (!CMS_add1_recipient_cert(cms, recip, flags)) {
  517. CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
  518. goto err;
  519. }
  520. }
  521. if (!(flags & CMS_DETACHED))
  522. CMS_set_detached(cms, 0);
  523. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  524. || CMS_final(cms, data, NULL, flags))
  525. return cms;
  526. else
  527. goto err;
  528. merr:
  529. CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
  530. err:
  531. CMS_ContentInfo_free(cms);
  532. return NULL;
  533. }
  534. static int cms_kari_set1_pkey(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
  535. EVP_PKEY *pk, X509 *cert)
  536. {
  537. int i;
  538. STACK_OF(CMS_RecipientEncryptedKey) *reks;
  539. CMS_RecipientEncryptedKey *rek;
  540. reks = CMS_RecipientInfo_kari_get0_reks(ri);
  541. if (!cert)
  542. return 0;
  543. for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
  544. int rv;
  545. rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
  546. if (CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
  547. continue;
  548. CMS_RecipientInfo_kari_set0_pkey(ri, pk);
  549. rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
  550. CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
  551. if (rv > 0)
  552. return 1;
  553. return -1;
  554. }
  555. return 0;
  556. }
  557. int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
  558. {
  559. STACK_OF(CMS_RecipientInfo) *ris;
  560. CMS_RecipientInfo *ri;
  561. int i, r, ri_type;
  562. int debug = 0, match_ri = 0;
  563. ris = CMS_get0_RecipientInfos(cms);
  564. if (ris)
  565. debug = cms->d.envelopedData->encryptedContentInfo->debug;
  566. ri_type = cms_pkey_get_ri_type(pk);
  567. if (ri_type == CMS_RECIPINFO_NONE) {
  568. CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,
  569. CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  570. return 0;
  571. }
  572. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  573. ri = sk_CMS_RecipientInfo_value(ris, i);
  574. if (CMS_RecipientInfo_type(ri) != ri_type)
  575. continue;
  576. match_ri = 1;
  577. if (ri_type == CMS_RECIPINFO_AGREE) {
  578. r = cms_kari_set1_pkey(cms, ri, pk, cert);
  579. if (r > 0)
  580. return 1;
  581. if (r < 0)
  582. return 0;
  583. }
  584. /*
  585. * If we have a cert try matching RecipientInfo otherwise try them
  586. * all.
  587. */
  588. else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
  589. CMS_RecipientInfo_set0_pkey(ri, pk);
  590. r = CMS_RecipientInfo_decrypt(cms, ri);
  591. CMS_RecipientInfo_set0_pkey(ri, NULL);
  592. if (cert) {
  593. /*
  594. * If not debugging clear any error and return success to
  595. * avoid leaking of information useful to MMA
  596. */
  597. if (!debug) {
  598. ERR_clear_error();
  599. return 1;
  600. }
  601. if (r > 0)
  602. return 1;
  603. CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_DECRYPT_ERROR);
  604. return 0;
  605. }
  606. /*
  607. * If no cert and not debugging don't leave loop after first
  608. * successful decrypt. Always attempt to decrypt all recipients
  609. * to avoid leaking timing of a successful decrypt.
  610. */
  611. else if (r > 0 && debug)
  612. return 1;
  613. }
  614. }
  615. /* If no cert and not debugging always return success */
  616. if (match_ri && !cert && !debug) {
  617. ERR_clear_error();
  618. return 1;
  619. }
  620. CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
  621. return 0;
  622. }
  623. int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
  624. unsigned char *key, size_t keylen,
  625. unsigned char *id, size_t idlen)
  626. {
  627. STACK_OF(CMS_RecipientInfo) *ris;
  628. CMS_RecipientInfo *ri;
  629. int i, r;
  630. ris = CMS_get0_RecipientInfos(cms);
  631. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  632. ri = sk_CMS_RecipientInfo_value(ris, i);
  633. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
  634. continue;
  635. /*
  636. * If we have an id try matching RecipientInfo otherwise try them
  637. * all.
  638. */
  639. if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
  640. CMS_RecipientInfo_set0_key(ri, key, keylen);
  641. r = CMS_RecipientInfo_decrypt(cms, ri);
  642. CMS_RecipientInfo_set0_key(ri, NULL, 0);
  643. if (r > 0)
  644. return 1;
  645. if (id) {
  646. CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR);
  647. return 0;
  648. }
  649. ERR_clear_error();
  650. }
  651. }
  652. CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
  653. return 0;
  654. }
  655. int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
  656. unsigned char *pass, ossl_ssize_t passlen)
  657. {
  658. STACK_OF(CMS_RecipientInfo) *ris;
  659. CMS_RecipientInfo *ri;
  660. int i, r;
  661. ris = CMS_get0_RecipientInfos(cms);
  662. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  663. ri = sk_CMS_RecipientInfo_value(ris, i);
  664. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
  665. continue;
  666. CMS_RecipientInfo_set0_password(ri, pass, passlen);
  667. r = CMS_RecipientInfo_decrypt(cms, ri);
  668. CMS_RecipientInfo_set0_password(ri, NULL, 0);
  669. if (r > 0)
  670. return 1;
  671. }
  672. CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD, CMS_R_NO_MATCHING_RECIPIENT);
  673. return 0;
  674. }
  675. int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
  676. BIO *dcont, BIO *out, unsigned int flags)
  677. {
  678. int r;
  679. BIO *cont;
  680. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {
  681. CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
  682. return 0;
  683. }
  684. if (!dcont && !check_content(cms))
  685. return 0;
  686. if (flags & CMS_DEBUG_DECRYPT)
  687. cms->d.envelopedData->encryptedContentInfo->debug = 1;
  688. else
  689. cms->d.envelopedData->encryptedContentInfo->debug = 0;
  690. if (!pk && !cert && !dcont && !out)
  691. return 1;
  692. if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
  693. return 0;
  694. cont = CMS_dataInit(cms, dcont);
  695. if (!cont)
  696. return 0;
  697. r = cms_copy_content(out, cont, flags);
  698. do_free_upto(cont, dcont);
  699. return r;
  700. }
  701. int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
  702. {
  703. BIO *cmsbio;
  704. int ret = 0;
  705. if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
  706. CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_LIB);
  707. return 0;
  708. }
  709. SMIME_crlf_copy(data, cmsbio, flags);
  710. (void)BIO_flush(cmsbio);
  711. if (!CMS_dataFinal(cms, cmsbio)) {
  712. CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
  713. goto err;
  714. }
  715. ret = 1;
  716. err:
  717. do_free_upto(cmsbio, dcont);
  718. return ret;
  719. }
  720. #ifdef ZLIB
  721. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  722. unsigned int flags)
  723. {
  724. BIO *cont;
  725. int r;
  726. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
  727. CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
  728. return 0;
  729. }
  730. if (!dcont && !check_content(cms))
  731. return 0;
  732. cont = CMS_dataInit(cms, dcont);
  733. if (!cont)
  734. return 0;
  735. r = cms_copy_content(out, cont, flags);
  736. do_free_upto(cont, dcont);
  737. return r;
  738. }
  739. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  740. {
  741. CMS_ContentInfo *cms;
  742. if (comp_nid <= 0)
  743. comp_nid = NID_zlib_compression;
  744. cms = cms_CompressedData_create(comp_nid);
  745. if (!cms)
  746. return NULL;
  747. if (!(flags & CMS_DETACHED))
  748. CMS_set_detached(cms, 0);
  749. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  750. return cms;
  751. CMS_ContentInfo_free(cms);
  752. return NULL;
  753. }
  754. #else
  755. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  756. unsigned int flags)
  757. {
  758. CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  759. return 0;
  760. }
  761. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  762. {
  763. CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  764. return NULL;
  765. }
  766. #endif