cms_smime.c 23 KB

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