cms_smime.c 25 KB

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