cms_smime.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 "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 "asn1_locl.h"
  62. static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
  63. {
  64. unsigned char buf[4096];
  65. int r = 0, i;
  66. BIO *tmpout = NULL;
  67. if (out == NULL)
  68. tmpout = BIO_new(BIO_s_null());
  69. else if (flags & CMS_TEXT) {
  70. tmpout = BIO_new(BIO_s_mem());
  71. BIO_set_mem_eof_return(tmpout, 0);
  72. } else
  73. tmpout = out;
  74. if (!tmpout) {
  75. CMSerr(CMS_F_CMS_COPY_CONTENT, ERR_R_MALLOC_FAILURE);
  76. goto err;
  77. }
  78. /* Read all content through chain to process digest, decrypt etc */
  79. for (;;) {
  80. i = BIO_read(in, buf, sizeof(buf));
  81. if (i <= 0) {
  82. if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
  83. if (!BIO_get_cipher_status(in))
  84. goto err;
  85. }
  86. if (i < 0)
  87. goto err;
  88. break;
  89. }
  90. if (tmpout && (BIO_write(tmpout, buf, i) != i))
  91. goto err;
  92. }
  93. if (flags & CMS_TEXT) {
  94. if (!SMIME_text(tmpout, out)) {
  95. CMSerr(CMS_F_CMS_COPY_CONTENT, CMS_R_SMIME_TEXT_ERROR);
  96. goto err;
  97. }
  98. }
  99. r = 1;
  100. err:
  101. if (tmpout && (tmpout != out))
  102. BIO_free(tmpout);
  103. return r;
  104. }
  105. static int check_content(CMS_ContentInfo *cms)
  106. {
  107. ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
  108. if (!pos || !*pos) {
  109. CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
  110. return 0;
  111. }
  112. return 1;
  113. }
  114. static void do_free_upto(BIO *f, BIO *upto)
  115. {
  116. if (upto) {
  117. BIO *tbio;
  118. do {
  119. tbio = BIO_pop(f);
  120. BIO_free(f);
  121. f = tbio;
  122. }
  123. while (f && f != upto);
  124. } else
  125. BIO_free_all(f);
  126. }
  127. int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
  128. {
  129. BIO *cont;
  130. int r;
  131. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
  132. CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
  133. return 0;
  134. }
  135. cont = CMS_dataInit(cms, NULL);
  136. if (!cont)
  137. return 0;
  138. r = cms_copy_content(out, cont, flags);
  139. BIO_free_all(cont);
  140. return r;
  141. }
  142. CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
  143. {
  144. CMS_ContentInfo *cms;
  145. cms = cms_Data_create();
  146. if (!cms)
  147. return NULL;
  148. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  149. return cms;
  150. CMS_ContentInfo_free(cms);
  151. return NULL;
  152. }
  153. int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  154. unsigned int flags)
  155. {
  156. BIO *cont;
  157. int r;
  158. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
  159. CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
  160. return 0;
  161. }
  162. if (!dcont && !check_content(cms))
  163. return 0;
  164. cont = CMS_dataInit(cms, dcont);
  165. if (!cont)
  166. return 0;
  167. r = cms_copy_content(out, cont, flags);
  168. if (r)
  169. r = cms_DigestedData_do_final(cms, cont, 1);
  170. do_free_upto(cont, dcont);
  171. return r;
  172. }
  173. CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
  174. unsigned int flags)
  175. {
  176. CMS_ContentInfo *cms;
  177. if (!md)
  178. md = EVP_sha1();
  179. cms = cms_DigestedData_create(md);
  180. if (!cms)
  181. return NULL;
  182. if (!(flags & CMS_DETACHED))
  183. CMS_set_detached(cms, 0);
  184. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  185. return cms;
  186. CMS_ContentInfo_free(cms);
  187. return NULL;
  188. }
  189. int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
  190. const unsigned char *key, size_t keylen,
  191. BIO *dcont, BIO *out, unsigned int flags)
  192. {
  193. BIO *cont;
  194. int r;
  195. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
  196. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
  197. CMS_R_TYPE_NOT_ENCRYPTED_DATA);
  198. return 0;
  199. }
  200. if (!dcont && !check_content(cms))
  201. return 0;
  202. if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
  203. return 0;
  204. cont = CMS_dataInit(cms, dcont);
  205. if (!cont)
  206. return 0;
  207. r = cms_copy_content(out, cont, flags);
  208. do_free_upto(cont, dcont);
  209. return r;
  210. }
  211. CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
  212. const unsigned char *key,
  213. size_t keylen, unsigned int flags)
  214. {
  215. CMS_ContentInfo *cms;
  216. if (!cipher) {
  217. CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
  218. return NULL;
  219. }
  220. cms = CMS_ContentInfo_new();
  221. if (!cms)
  222. return NULL;
  223. if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
  224. return NULL;
  225. if (!(flags & CMS_DETACHED))
  226. CMS_set_detached(cms, 0);
  227. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  228. || CMS_final(cms, in, NULL, flags))
  229. return cms;
  230. CMS_ContentInfo_free(cms);
  231. return NULL;
  232. }
  233. static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
  234. X509_STORE *store,
  235. STACK_OF(X509) *certs,
  236. STACK_OF(X509_CRL) *crls,
  237. unsigned int flags)
  238. {
  239. X509_STORE_CTX ctx;
  240. X509 *signer;
  241. int i, j, r = 0;
  242. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  243. if (!X509_STORE_CTX_init(&ctx, store, signer, certs)) {
  244. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, CMS_R_STORE_INIT_ERROR);
  245. goto err;
  246. }
  247. X509_STORE_CTX_set_default(&ctx, "smime_sign");
  248. if (crls)
  249. X509_STORE_CTX_set0_crls(&ctx, crls);
  250. i = X509_verify_cert(&ctx);
  251. if (i <= 0) {
  252. j = X509_STORE_CTX_get_error(&ctx);
  253. CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
  254. CMS_R_CERTIFICATE_VERIFY_ERROR);
  255. ERR_add_error_data(2, "Verify error:",
  256. X509_verify_cert_error_string(j));
  257. goto err;
  258. }
  259. r = 1;
  260. err:
  261. X509_STORE_CTX_cleanup(&ctx);
  262. return r;
  263. }
  264. int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
  265. X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
  266. {
  267. CMS_SignerInfo *si;
  268. STACK_OF(CMS_SignerInfo) *sinfos;
  269. STACK_OF(X509) *cms_certs = NULL;
  270. STACK_OF(X509_CRL) *crls = NULL;
  271. X509 *signer;
  272. int i, scount = 0, ret = 0;
  273. BIO *cmsbio = NULL, *tmpin = NULL;
  274. if (!dcont && !check_content(cms))
  275. return 0;
  276. /* Attempt to find all signer certificates */
  277. sinfos = CMS_get0_SignerInfos(cms);
  278. if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
  279. CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
  280. goto err;
  281. }
  282. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  283. si = sk_CMS_SignerInfo_value(sinfos, i);
  284. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  285. if (signer)
  286. scount++;
  287. }
  288. if (scount != sk_CMS_SignerInfo_num(sinfos))
  289. scount += CMS_set1_signers_certs(cms, certs, flags);
  290. if (scount != sk_CMS_SignerInfo_num(sinfos)) {
  291. CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
  292. goto err;
  293. }
  294. /* Attempt to verify all signers certs */
  295. if (!(flags & CMS_NO_SIGNER_CERT_VERIFY)) {
  296. cms_certs = CMS_get1_certs(cms);
  297. if (!(flags & CMS_NOCRL))
  298. crls = CMS_get1_crls(cms);
  299. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  300. si = sk_CMS_SignerInfo_value(sinfos, i);
  301. if (!cms_signerinfo_verify_cert(si, store,
  302. cms_certs, crls, flags))
  303. goto err;
  304. }
  305. }
  306. /* Attempt to verify all SignerInfo signed attribute signatures */
  307. if (!(flags & CMS_NO_ATTR_VERIFY)) {
  308. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  309. si = sk_CMS_SignerInfo_value(sinfos, i);
  310. if (CMS_signed_get_attr_count(si) < 0)
  311. continue;
  312. if (CMS_SignerInfo_verify(si) <= 0)
  313. goto err;
  314. }
  315. }
  316. /*
  317. * Performance optimization: if the content is a memory BIO then store
  318. * its contents in a temporary read only memory BIO. This avoids
  319. * potentially large numbers of slow copies of data which will occur when
  320. * reading from a read write memory BIO when signatures are calculated.
  321. */
  322. if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
  323. char *ptr;
  324. long len;
  325. len = BIO_get_mem_data(dcont, &ptr);
  326. tmpin = BIO_new_mem_buf(ptr, len);
  327. if (tmpin == NULL) {
  328. CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
  329. goto err2;
  330. }
  331. } else
  332. tmpin = dcont;
  333. cmsbio = CMS_dataInit(cms, tmpin);
  334. if (!cmsbio)
  335. goto err;
  336. if (!cms_copy_content(out, cmsbio, flags))
  337. goto err;
  338. if (!(flags & CMS_NO_CONTENT_VERIFY)) {
  339. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  340. si = sk_CMS_SignerInfo_value(sinfos, i);
  341. if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
  342. CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR);
  343. goto err;
  344. }
  345. }
  346. }
  347. ret = 1;
  348. err:
  349. if (dcont && (tmpin == dcont))
  350. do_free_upto(cmsbio, dcont);
  351. else
  352. BIO_free_all(cmsbio);
  353. err2:
  354. if (cms_certs)
  355. sk_X509_pop_free(cms_certs, X509_free);
  356. if (crls)
  357. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  358. return ret;
  359. }
  360. int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
  361. STACK_OF(X509) *certs,
  362. X509_STORE *store, unsigned int flags)
  363. {
  364. int r;
  365. flags &= ~(CMS_DETACHED | CMS_TEXT);
  366. r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
  367. if (r <= 0)
  368. return r;
  369. return cms_Receipt_verify(rcms, ocms);
  370. }
  371. CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey,
  372. STACK_OF(X509) *certs, BIO *data,
  373. unsigned int flags)
  374. {
  375. CMS_ContentInfo *cms;
  376. int i;
  377. cms = CMS_ContentInfo_new();
  378. if (!cms || !CMS_SignedData_init(cms))
  379. goto merr;
  380. if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
  381. CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
  382. goto err;
  383. }
  384. for (i = 0; i < sk_X509_num(certs); i++) {
  385. X509 *x = sk_X509_value(certs, i);
  386. if (!CMS_add1_cert(cms, x))
  387. goto merr;
  388. }
  389. if (!(flags & CMS_DETACHED))
  390. CMS_set_detached(cms, 0);
  391. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  392. || CMS_final(cms, data, NULL, flags))
  393. return cms;
  394. else
  395. goto err;
  396. merr:
  397. CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
  398. err:
  399. if (cms)
  400. CMS_ContentInfo_free(cms);
  401. return NULL;
  402. }
  403. CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
  404. X509 *signcert, EVP_PKEY *pkey,
  405. STACK_OF(X509) *certs, unsigned int flags)
  406. {
  407. CMS_SignerInfo *rct_si;
  408. CMS_ContentInfo *cms = NULL;
  409. ASN1_OCTET_STRING **pos, *os;
  410. BIO *rct_cont = NULL;
  411. int r = 0;
  412. flags &= ~(CMS_STREAM | CMS_TEXT);
  413. /* Not really detached but avoids content being allocated */
  414. flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
  415. if (!pkey || !signcert) {
  416. CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
  417. return NULL;
  418. }
  419. /* Initialize signed data */
  420. cms = CMS_sign(NULL, NULL, certs, NULL, flags);
  421. if (!cms)
  422. goto err;
  423. /* Set inner content type to signed receipt */
  424. if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
  425. goto err;
  426. rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
  427. if (!rct_si) {
  428. CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
  429. goto err;
  430. }
  431. os = cms_encode_Receipt(si);
  432. if (!os)
  433. goto err;
  434. /* Set content to digest */
  435. rct_cont = BIO_new_mem_buf(os->data, os->length);
  436. if (!rct_cont)
  437. goto err;
  438. /* Add msgSigDigest attribute */
  439. if (!cms_msgSigDigest_add1(rct_si, si))
  440. goto err;
  441. /* Finalize structure */
  442. if (!CMS_final(cms, rct_cont, NULL, flags))
  443. goto err;
  444. /* Set embedded content */
  445. pos = CMS_get0_content(cms);
  446. *pos = os;
  447. r = 1;
  448. err:
  449. if (rct_cont)
  450. BIO_free(rct_cont);
  451. if (r)
  452. return cms;
  453. CMS_ContentInfo_free(cms);
  454. return NULL;
  455. }
  456. CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
  457. const EVP_CIPHER *cipher, unsigned int flags)
  458. {
  459. CMS_ContentInfo *cms;
  460. int i;
  461. X509 *recip;
  462. cms = CMS_EnvelopedData_create(cipher);
  463. if (!cms)
  464. goto merr;
  465. for (i = 0; i < sk_X509_num(certs); i++) {
  466. recip = sk_X509_value(certs, i);
  467. if (!CMS_add1_recipient_cert(cms, recip, flags)) {
  468. CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
  469. goto err;
  470. }
  471. }
  472. if (!(flags & CMS_DETACHED))
  473. CMS_set_detached(cms, 0);
  474. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  475. || CMS_final(cms, data, NULL, flags))
  476. return cms;
  477. else
  478. goto err;
  479. merr:
  480. CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
  481. err:
  482. if (cms)
  483. CMS_ContentInfo_free(cms);
  484. return NULL;
  485. }
  486. static int cms_kari_set1_pkey(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
  487. EVP_PKEY *pk, X509 *cert)
  488. {
  489. int i;
  490. STACK_OF(CMS_RecipientEncryptedKey) *reks;
  491. CMS_RecipientEncryptedKey *rek;
  492. reks = CMS_RecipientInfo_kari_get0_reks(ri);
  493. if (!cert)
  494. return 0;
  495. for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
  496. int rv;
  497. rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
  498. if (CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
  499. continue;
  500. CMS_RecipientInfo_kari_set0_pkey(ri, pk);
  501. rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
  502. CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
  503. if (rv > 0)
  504. return 1;
  505. return -1;
  506. }
  507. return 0;
  508. }
  509. int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
  510. {
  511. STACK_OF(CMS_RecipientInfo) *ris;
  512. CMS_RecipientInfo *ri;
  513. int i, r, ri_type;
  514. int debug = 0, match_ri = 0;
  515. ris = CMS_get0_RecipientInfos(cms);
  516. if (ris)
  517. debug = cms->d.envelopedData->encryptedContentInfo->debug;
  518. ri_type = cms_pkey_get_ri_type(pk);
  519. if (ri_type == CMS_RECIPINFO_NONE) {
  520. CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,
  521. CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  522. return 0;
  523. }
  524. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  525. ri = sk_CMS_RecipientInfo_value(ris, i);
  526. if (CMS_RecipientInfo_type(ri) != ri_type)
  527. continue;
  528. match_ri = 1;
  529. if (ri_type == CMS_RECIPINFO_AGREE) {
  530. r = cms_kari_set1_pkey(cms, ri, pk, cert);
  531. if (r > 0)
  532. return 1;
  533. if (r < 0)
  534. return 0;
  535. }
  536. /*
  537. * If we have a cert try matching RecipientInfo otherwise try them
  538. * all.
  539. */
  540. else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
  541. CMS_RecipientInfo_set0_pkey(ri, pk);
  542. r = CMS_RecipientInfo_decrypt(cms, ri);
  543. CMS_RecipientInfo_set0_pkey(ri, NULL);
  544. if (cert) {
  545. /*
  546. * If not debugging clear any error and return success to
  547. * avoid leaking of information useful to MMA
  548. */
  549. if (!debug) {
  550. ERR_clear_error();
  551. return 1;
  552. }
  553. if (r > 0)
  554. return 1;
  555. CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_DECRYPT_ERROR);
  556. return 0;
  557. }
  558. /*
  559. * If no cert and not debugging don't leave loop after first
  560. * successful decrypt. Always attempt to decrypt all recipients
  561. * to avoid leaking timing of a successful decrypt.
  562. */
  563. else if (r > 0 && debug)
  564. return 1;
  565. }
  566. }
  567. /* If no cert and not debugging always return success */
  568. if (match_ri && !cert && !debug) {
  569. ERR_clear_error();
  570. return 1;
  571. }
  572. CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
  573. return 0;
  574. }
  575. int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
  576. unsigned char *key, size_t keylen,
  577. unsigned char *id, size_t idlen)
  578. {
  579. STACK_OF(CMS_RecipientInfo) *ris;
  580. CMS_RecipientInfo *ri;
  581. int i, r;
  582. ris = CMS_get0_RecipientInfos(cms);
  583. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  584. ri = sk_CMS_RecipientInfo_value(ris, i);
  585. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
  586. continue;
  587. /*
  588. * If we have an id try matching RecipientInfo otherwise try them
  589. * all.
  590. */
  591. if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
  592. CMS_RecipientInfo_set0_key(ri, key, keylen);
  593. r = CMS_RecipientInfo_decrypt(cms, ri);
  594. CMS_RecipientInfo_set0_key(ri, NULL, 0);
  595. if (r > 0)
  596. return 1;
  597. if (id) {
  598. CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR);
  599. return 0;
  600. }
  601. ERR_clear_error();
  602. }
  603. }
  604. CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
  605. return 0;
  606. }
  607. int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
  608. unsigned char *pass, ossl_ssize_t passlen)
  609. {
  610. STACK_OF(CMS_RecipientInfo) *ris;
  611. CMS_RecipientInfo *ri;
  612. int i, r;
  613. ris = CMS_get0_RecipientInfos(cms);
  614. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  615. ri = sk_CMS_RecipientInfo_value(ris, i);
  616. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
  617. continue;
  618. CMS_RecipientInfo_set0_password(ri, pass, passlen);
  619. r = CMS_RecipientInfo_decrypt(cms, ri);
  620. CMS_RecipientInfo_set0_password(ri, NULL, 0);
  621. if (r > 0)
  622. return 1;
  623. }
  624. CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD, CMS_R_NO_MATCHING_RECIPIENT);
  625. return 0;
  626. }
  627. int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
  628. BIO *dcont, BIO *out, unsigned int flags)
  629. {
  630. int r;
  631. BIO *cont;
  632. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {
  633. CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
  634. return 0;
  635. }
  636. if (!dcont && !check_content(cms))
  637. return 0;
  638. if (flags & CMS_DEBUG_DECRYPT)
  639. cms->d.envelopedData->encryptedContentInfo->debug = 1;
  640. else
  641. cms->d.envelopedData->encryptedContentInfo->debug = 0;
  642. if (!pk && !cert && !dcont && !out)
  643. return 1;
  644. if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
  645. return 0;
  646. cont = CMS_dataInit(cms, dcont);
  647. if (!cont)
  648. return 0;
  649. r = cms_copy_content(out, cont, flags);
  650. do_free_upto(cont, dcont);
  651. return r;
  652. }
  653. int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
  654. {
  655. BIO *cmsbio;
  656. int ret = 0;
  657. if (!(cmsbio = CMS_dataInit(cms, dcont))) {
  658. CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_LIB);
  659. return 0;
  660. }
  661. SMIME_crlf_copy(data, cmsbio, flags);
  662. (void)BIO_flush(cmsbio);
  663. if (!CMS_dataFinal(cms, cmsbio)) {
  664. CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
  665. goto err;
  666. }
  667. ret = 1;
  668. err:
  669. do_free_upto(cmsbio, dcont);
  670. return ret;
  671. }
  672. #ifdef ZLIB
  673. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  674. unsigned int flags)
  675. {
  676. BIO *cont;
  677. int r;
  678. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
  679. CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
  680. return 0;
  681. }
  682. if (!dcont && !check_content(cms))
  683. return 0;
  684. cont = CMS_dataInit(cms, dcont);
  685. if (!cont)
  686. return 0;
  687. r = cms_copy_content(out, cont, flags);
  688. do_free_upto(cont, dcont);
  689. return r;
  690. }
  691. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  692. {
  693. CMS_ContentInfo *cms;
  694. if (comp_nid <= 0)
  695. comp_nid = NID_zlib_compression;
  696. cms = cms_CompressedData_create(comp_nid);
  697. if (!cms)
  698. return NULL;
  699. if (!(flags & CMS_DETACHED))
  700. CMS_set_detached(cms, 0);
  701. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  702. return cms;
  703. CMS_ContentInfo_free(cms);
  704. return NULL;
  705. }
  706. #else
  707. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  708. unsigned int flags)
  709. {
  710. CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  711. return 0;
  712. }
  713. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  714. {
  715. CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  716. return NULL;
  717. }
  718. #endif