cms_smime.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. * Copyright 2008-2021 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. ERR_raise(ERR_LIB_CMS, 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 != NULL && (BIO_write(tmpout, buf, i) != i))
  52. goto err;
  53. }
  54. if (flags & CMS_TEXT) {
  55. if (!SMIME_text(tmpout, out)) {
  56. ERR_raise(ERR_LIB_CMS, 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. ERR_raise(ERR_LIB_CMS, 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. }
  88. int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
  89. {
  90. BIO *cont;
  91. int r;
  92. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
  93. ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DATA);
  94. return 0;
  95. }
  96. cont = CMS_dataInit(cms, NULL);
  97. if (cont == NULL)
  98. return 0;
  99. r = cms_copy_content(out, cont, flags);
  100. BIO_free_all(cont);
  101. return r;
  102. }
  103. CMS_ContentInfo *CMS_data_create_ex(BIO *in, unsigned int flags,
  104. OSSL_LIB_CTX *libctx, const char *propq)
  105. {
  106. CMS_ContentInfo *cms = ossl_cms_Data_create(libctx, propq);
  107. if (cms == NULL)
  108. return NULL;
  109. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  110. return cms;
  111. CMS_ContentInfo_free(cms);
  112. return NULL;
  113. }
  114. CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
  115. {
  116. return CMS_data_create_ex(in, flags, NULL, NULL);
  117. }
  118. int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  119. unsigned int flags)
  120. {
  121. BIO *cont;
  122. int r;
  123. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
  124. ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_DIGESTED_DATA);
  125. return 0;
  126. }
  127. if (dcont == NULL && !check_content(cms))
  128. return 0;
  129. cont = CMS_dataInit(cms, dcont);
  130. if (cont == NULL)
  131. return 0;
  132. r = cms_copy_content(out, cont, flags);
  133. if (r)
  134. r = ossl_cms_DigestedData_do_final(cms, cont, 1);
  135. do_free_upto(cont, dcont);
  136. return r;
  137. }
  138. CMS_ContentInfo *CMS_digest_create_ex(BIO *in, const EVP_MD *md,
  139. unsigned int flags, OSSL_LIB_CTX *ctx,
  140. const char *propq)
  141. {
  142. CMS_ContentInfo *cms;
  143. /*
  144. * Because the EVP_MD is cached and can be a legacy algorithm, we
  145. * cannot fetch the algorithm if it isn't supplied.
  146. */
  147. if (md == NULL)
  148. md = EVP_sha1();
  149. cms = ossl_cms_DigestedData_create(md, ctx, propq);
  150. if (cms == NULL)
  151. return NULL;
  152. if (!(flags & CMS_DETACHED))
  153. CMS_set_detached(cms, 0);
  154. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  155. return cms;
  156. CMS_ContentInfo_free(cms);
  157. return NULL;
  158. }
  159. CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
  160. unsigned int flags)
  161. {
  162. return CMS_digest_create_ex(in, md, flags, NULL, NULL);
  163. }
  164. int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
  165. const unsigned char *key, size_t keylen,
  166. BIO *dcont, BIO *out, unsigned int flags)
  167. {
  168. BIO *cont;
  169. int r;
  170. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
  171. ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENCRYPTED_DATA);
  172. return 0;
  173. }
  174. if (dcont == NULL && !check_content(cms))
  175. return 0;
  176. if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
  177. return 0;
  178. cont = CMS_dataInit(cms, dcont);
  179. if (cont == NULL)
  180. return 0;
  181. r = cms_copy_content(out, cont, flags);
  182. do_free_upto(cont, dcont);
  183. return r;
  184. }
  185. CMS_ContentInfo *CMS_EncryptedData_encrypt_ex(BIO *in, const EVP_CIPHER *cipher,
  186. const unsigned char *key,
  187. size_t keylen, unsigned int flags,
  188. OSSL_LIB_CTX *libctx,
  189. const char *propq)
  190. {
  191. CMS_ContentInfo *cms;
  192. if (cipher == NULL) {
  193. ERR_raise(ERR_LIB_CMS, CMS_R_NO_CIPHER);
  194. return NULL;
  195. }
  196. cms = CMS_ContentInfo_new_ex(libctx, propq);
  197. if (cms == NULL)
  198. return NULL;
  199. if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
  200. return NULL;
  201. if (!(flags & CMS_DETACHED))
  202. CMS_set_detached(cms, 0);
  203. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  204. || CMS_final(cms, in, NULL, flags))
  205. return cms;
  206. CMS_ContentInfo_free(cms);
  207. return NULL;
  208. }
  209. CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
  210. const unsigned char *key,
  211. size_t keylen, unsigned int flags)
  212. {
  213. return CMS_EncryptedData_encrypt_ex(in, cipher, key, keylen, flags, NULL,
  214. NULL);
  215. }
  216. static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
  217. X509_STORE *store,
  218. STACK_OF(X509) *certs,
  219. STACK_OF(X509_CRL) *crls,
  220. STACK_OF(X509) **chain,
  221. const CMS_CTX *cms_ctx)
  222. {
  223. X509_STORE_CTX *ctx;
  224. X509 *signer;
  225. int i, j, r = 0;
  226. ctx = X509_STORE_CTX_new_ex(ossl_cms_ctx_get0_libctx(cms_ctx),
  227. ossl_cms_ctx_get0_propq(cms_ctx));
  228. if (ctx == NULL) {
  229. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  230. goto err;
  231. }
  232. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  233. if (!X509_STORE_CTX_init(ctx, store, signer, certs)) {
  234. ERR_raise(ERR_LIB_CMS, CMS_R_STORE_INIT_ERROR);
  235. goto err;
  236. }
  237. X509_STORE_CTX_set_default(ctx, "smime_sign");
  238. if (crls != NULL)
  239. X509_STORE_CTX_set0_crls(ctx, crls);
  240. i = X509_verify_cert(ctx);
  241. if (i <= 0) {
  242. j = X509_STORE_CTX_get_error(ctx);
  243. ERR_raise_data(ERR_LIB_CMS, CMS_R_CERTIFICATE_VERIFY_ERROR,
  244. "Verify error: %s", X509_verify_cert_error_string(j));
  245. goto err;
  246. }
  247. r = 1;
  248. /* also send back the trust chain when required */
  249. if (chain != NULL)
  250. *chain = X509_STORE_CTX_get1_chain(ctx);
  251. err:
  252. X509_STORE_CTX_free(ctx);
  253. return r;
  254. }
  255. int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
  256. X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
  257. {
  258. CMS_SignerInfo *si;
  259. STACK_OF(CMS_SignerInfo) *sinfos;
  260. STACK_OF(X509) *cms_certs = NULL;
  261. STACK_OF(X509_CRL) *crls = NULL;
  262. STACK_OF(X509) **si_chains = NULL;
  263. X509 *signer;
  264. int i, scount = 0, ret = 0;
  265. BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
  266. int cadesVerify = (flags & CMS_CADES) != 0;
  267. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  268. if (dcont == NULL && !check_content(cms))
  269. return 0;
  270. if (dcont != NULL && !(flags & CMS_BINARY)) {
  271. const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
  272. if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
  273. flags |= CMS_ASCIICRLF;
  274. }
  275. /* Attempt to find all signer certificates */
  276. sinfos = CMS_get0_SignerInfos(cms);
  277. if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
  278. ERR_raise(ERR_LIB_CMS, CMS_R_NO_SIGNERS);
  279. goto err;
  280. }
  281. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  282. si = sk_CMS_SignerInfo_value(sinfos, i);
  283. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  284. if (signer)
  285. scount++;
  286. }
  287. if (scount != sk_CMS_SignerInfo_num(sinfos))
  288. scount += CMS_set1_signers_certs(cms, certs, flags);
  289. if (scount != sk_CMS_SignerInfo_num(sinfos)) {
  290. ERR_raise(ERR_LIB_CMS, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
  291. goto err;
  292. }
  293. /* Attempt to verify all signers certs */
  294. /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
  295. if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
  296. if (cadesVerify) {
  297. /* Certificate trust chain is required to check CAdES signature */
  298. si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
  299. if (si_chains == NULL) {
  300. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  301. goto err;
  302. }
  303. }
  304. cms_certs = CMS_get1_certs(cms);
  305. if (!(flags & CMS_NOCRL))
  306. crls = CMS_get1_crls(cms);
  307. for (i = 0; i < scount; i++) {
  308. si = sk_CMS_SignerInfo_value(sinfos, i);
  309. if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls,
  310. si_chains ? &si_chains[i] : NULL,
  311. ctx))
  312. goto err;
  313. }
  314. }
  315. /* Attempt to verify all SignerInfo signed attribute signatures */
  316. if ((flags & CMS_NO_ATTR_VERIFY) == 0 || cadesVerify) {
  317. for (i = 0; i < scount; i++) {
  318. si = sk_CMS_SignerInfo_value(sinfos, i);
  319. if (CMS_signed_get_attr_count(si) < 0)
  320. continue;
  321. if (CMS_SignerInfo_verify(si) <= 0)
  322. goto err;
  323. if (cadesVerify) {
  324. STACK_OF(X509) *si_chain = si_chains ? si_chains[i] : NULL;
  325. if (ossl_cms_check_signing_certs(si, si_chain) <= 0)
  326. goto err;
  327. }
  328. }
  329. }
  330. /*
  331. * Performance optimization: if the content is a memory BIO then store
  332. * its contents in a temporary read only memory BIO. This avoids
  333. * potentially large numbers of slow copies of data which will occur when
  334. * reading from a read write memory BIO when signatures are calculated.
  335. */
  336. if (dcont != NULL && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
  337. char *ptr;
  338. long len;
  339. len = BIO_get_mem_data(dcont, &ptr);
  340. tmpin = (len == 0) ? dcont : BIO_new_mem_buf(ptr, len);
  341. if (tmpin == NULL) {
  342. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  343. goto err2;
  344. }
  345. } else {
  346. tmpin = dcont;
  347. }
  348. /*
  349. * If not binary mode and detached generate digests by *writing* through
  350. * the BIO. That makes it possible to canonicalise the input.
  351. */
  352. if (!(flags & SMIME_BINARY) && dcont) {
  353. /*
  354. * Create output BIO so we can either handle text or to ensure
  355. * included content doesn't override detached content.
  356. */
  357. tmpout = cms_get_text_bio(out, flags);
  358. if (tmpout == NULL) {
  359. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  360. goto err;
  361. }
  362. cmsbio = CMS_dataInit(cms, tmpout);
  363. if (cmsbio == NULL)
  364. goto err;
  365. /*
  366. * Don't use SMIME_TEXT for verify: it adds headers and we want to
  367. * remove them.
  368. */
  369. SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
  370. if (flags & CMS_TEXT) {
  371. if (!SMIME_text(tmpout, out)) {
  372. ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
  373. goto err;
  374. }
  375. }
  376. } else {
  377. cmsbio = CMS_dataInit(cms, tmpin);
  378. if (cmsbio == NULL)
  379. goto err;
  380. if (!cms_copy_content(out, cmsbio, flags))
  381. goto err;
  382. }
  383. if (!(flags & CMS_NO_CONTENT_VERIFY)) {
  384. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  385. si = sk_CMS_SignerInfo_value(sinfos, i);
  386. if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
  387. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_VERIFY_ERROR);
  388. goto err;
  389. }
  390. }
  391. }
  392. ret = 1;
  393. err:
  394. if (!(flags & SMIME_BINARY) && dcont) {
  395. do_free_upto(cmsbio, tmpout);
  396. if (tmpin != dcont)
  397. BIO_free(tmpin);
  398. } else {
  399. if (dcont && (tmpin == dcont))
  400. do_free_upto(cmsbio, dcont);
  401. else
  402. BIO_free_all(cmsbio);
  403. }
  404. if (out != tmpout)
  405. BIO_free_all(tmpout);
  406. err2:
  407. if (si_chains != NULL) {
  408. for (i = 0; i < scount; ++i)
  409. sk_X509_pop_free(si_chains[i], X509_free);
  410. OPENSSL_free(si_chains);
  411. }
  412. sk_X509_pop_free(cms_certs, X509_free);
  413. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  414. return ret;
  415. }
  416. int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
  417. STACK_OF(X509) *certs,
  418. X509_STORE *store, unsigned int flags)
  419. {
  420. int r;
  421. flags &= ~(CMS_DETACHED | CMS_TEXT);
  422. r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
  423. if (r <= 0)
  424. return r;
  425. return ossl_cms_Receipt_verify(rcms, ocms);
  426. }
  427. CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
  428. STACK_OF(X509) *certs, BIO *data,
  429. unsigned int flags, OSSL_LIB_CTX *libctx,
  430. const char *propq)
  431. {
  432. CMS_ContentInfo *cms;
  433. int i;
  434. cms = CMS_ContentInfo_new_ex(libctx, propq);
  435. if (cms == NULL || !CMS_SignedData_init(cms))
  436. goto merr;
  437. if (flags & CMS_ASCIICRLF
  438. && !CMS_set1_eContentType(cms,
  439. OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
  440. goto err;
  441. if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
  442. ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
  443. goto err;
  444. }
  445. for (i = 0; i < sk_X509_num(certs); i++) {
  446. X509 *x = sk_X509_value(certs, i);
  447. if (!CMS_add1_cert(cms, x))
  448. goto merr;
  449. }
  450. if (!(flags & CMS_DETACHED))
  451. CMS_set_detached(cms, 0);
  452. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  453. || CMS_final(cms, data, NULL, flags))
  454. return cms;
  455. else
  456. goto err;
  457. merr:
  458. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  459. err:
  460. CMS_ContentInfo_free(cms);
  461. return NULL;
  462. }
  463. CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
  464. BIO *data, unsigned int flags)
  465. {
  466. return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
  467. }
  468. CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
  469. X509 *signcert, EVP_PKEY *pkey,
  470. STACK_OF(X509) *certs, unsigned int flags)
  471. {
  472. CMS_SignerInfo *rct_si;
  473. CMS_ContentInfo *cms = NULL;
  474. ASN1_OCTET_STRING **pos, *os;
  475. BIO *rct_cont = NULL;
  476. int r = 0;
  477. const CMS_CTX *ctx = si->cms_ctx;
  478. flags &= ~(CMS_STREAM | CMS_TEXT);
  479. /* Not really detached but avoids content being allocated */
  480. flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
  481. if (pkey == NULL || signcert == NULL) {
  482. ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY_OR_CERT);
  483. return NULL;
  484. }
  485. /* Initialize signed data */
  486. cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags,
  487. ossl_cms_ctx_get0_libctx(ctx),
  488. ossl_cms_ctx_get0_propq(ctx));
  489. if (cms == NULL)
  490. goto err;
  491. /* Set inner content type to signed receipt */
  492. if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
  493. goto err;
  494. rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
  495. if (!rct_si) {
  496. ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
  497. goto err;
  498. }
  499. os = ossl_cms_encode_Receipt(si);
  500. if (os == NULL)
  501. goto err;
  502. /* Set content to digest */
  503. rct_cont = BIO_new_mem_buf(os->data, os->length);
  504. if (rct_cont == NULL)
  505. goto err;
  506. /* Add msgSigDigest attribute */
  507. if (!ossl_cms_msgSigDigest_add1(rct_si, si))
  508. goto err;
  509. /* Finalize structure */
  510. if (!CMS_final(cms, rct_cont, NULL, flags))
  511. goto err;
  512. /* Set embedded content */
  513. pos = CMS_get0_content(cms);
  514. *pos = os;
  515. r = 1;
  516. err:
  517. BIO_free(rct_cont);
  518. if (r)
  519. return cms;
  520. CMS_ContentInfo_free(cms);
  521. return NULL;
  522. }
  523. CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
  524. const EVP_CIPHER *cipher, unsigned int flags,
  525. OSSL_LIB_CTX *libctx, const char *propq)
  526. {
  527. CMS_ContentInfo *cms;
  528. int i;
  529. X509 *recip;
  530. cms = (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
  531. ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
  532. : CMS_EnvelopedData_create_ex(cipher, libctx, propq);
  533. if (cms == NULL)
  534. goto merr;
  535. for (i = 0; i < sk_X509_num(certs); i++) {
  536. recip = sk_X509_value(certs, i);
  537. if (!CMS_add1_recipient_cert(cms, recip, flags)) {
  538. ERR_raise(ERR_LIB_CMS, CMS_R_RECIPIENT_ERROR);
  539. goto err;
  540. }
  541. }
  542. if (!(flags & CMS_DETACHED))
  543. CMS_set_detached(cms, 0);
  544. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  545. || CMS_final(cms, data, NULL, flags))
  546. return cms;
  547. else
  548. goto err;
  549. merr:
  550. ERR_raise(ERR_LIB_CMS, ERR_R_MALLOC_FAILURE);
  551. err:
  552. CMS_ContentInfo_free(cms);
  553. return NULL;
  554. }
  555. CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
  556. const EVP_CIPHER *cipher, unsigned int flags)
  557. {
  558. return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL);
  559. }
  560. static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms,
  561. CMS_RecipientInfo *ri,
  562. EVP_PKEY *pk, X509 *cert, X509 *peer)
  563. {
  564. int i;
  565. STACK_OF(CMS_RecipientEncryptedKey) *reks;
  566. CMS_RecipientEncryptedKey *rek;
  567. reks = CMS_RecipientInfo_kari_get0_reks(ri);
  568. for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
  569. int rv;
  570. rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
  571. if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
  572. continue;
  573. CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
  574. rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
  575. CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
  576. if (rv > 0)
  577. return 1;
  578. return cert == NULL ? 0 : -1;
  579. }
  580. return 0;
  581. }
  582. int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
  583. {
  584. return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
  585. }
  586. int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
  587. X509 *cert, X509 *peer)
  588. {
  589. STACK_OF(CMS_RecipientInfo) *ris;
  590. CMS_RecipientInfo *ri;
  591. int i, r, cms_pkey_ri_type;
  592. int debug = 0, match_ri = 0;
  593. ris = CMS_get0_RecipientInfos(cms);
  594. if (ris != NULL)
  595. debug = ossl_cms_get0_env_enc_content(cms)->debug;
  596. cms_pkey_ri_type = ossl_cms_pkey_get_ri_type(pk);
  597. if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
  598. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  599. return 0;
  600. }
  601. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  602. int ri_type;
  603. ri = sk_CMS_RecipientInfo_value(ris, i);
  604. ri_type = CMS_RecipientInfo_type(ri);
  605. if (!ossl_cms_pkey_is_ri_type_supported(pk, ri_type))
  606. continue;
  607. match_ri = 1;
  608. if (ri_type == CMS_RECIPINFO_AGREE) {
  609. r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
  610. if (r > 0)
  611. return 1;
  612. if (r < 0)
  613. return 0;
  614. }
  615. /*
  616. * If we have a cert try matching RecipientInfo otherwise try them
  617. * all.
  618. */
  619. else if (cert == NULL|| !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
  620. EVP_PKEY_up_ref(pk);
  621. CMS_RecipientInfo_set0_pkey(ri, pk);
  622. r = CMS_RecipientInfo_decrypt(cms, ri);
  623. CMS_RecipientInfo_set0_pkey(ri, NULL);
  624. if (cert != NULL) {
  625. /*
  626. * If not debugging clear any error and return success to
  627. * avoid leaking of information useful to MMA
  628. */
  629. if (!debug) {
  630. ERR_clear_error();
  631. return 1;
  632. }
  633. if (r > 0)
  634. return 1;
  635. ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
  636. return 0;
  637. }
  638. /*
  639. * If no cert and not debugging don't leave loop after first
  640. * successful decrypt. Always attempt to decrypt all recipients
  641. * to avoid leaking timing of a successful decrypt.
  642. */
  643. else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
  644. return 1;
  645. }
  646. }
  647. /* If no cert, key transport and not debugging always return success */
  648. if (cert == NULL
  649. && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
  650. && match_ri
  651. && !debug) {
  652. ERR_clear_error();
  653. return 1;
  654. }
  655. ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
  656. return 0;
  657. }
  658. int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
  659. unsigned char *key, size_t keylen,
  660. const unsigned char *id, size_t idlen)
  661. {
  662. STACK_OF(CMS_RecipientInfo) *ris;
  663. CMS_RecipientInfo *ri;
  664. int i, r;
  665. ris = CMS_get0_RecipientInfos(cms);
  666. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  667. ri = sk_CMS_RecipientInfo_value(ris, i);
  668. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
  669. continue;
  670. /*
  671. * If we have an id try matching RecipientInfo otherwise try them
  672. * all.
  673. */
  674. if (id == NULL || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
  675. CMS_RecipientInfo_set0_key(ri, key, keylen);
  676. r = CMS_RecipientInfo_decrypt(cms, ri);
  677. CMS_RecipientInfo_set0_key(ri, NULL, 0);
  678. if (r > 0)
  679. return 1;
  680. if (id != NULL) {
  681. ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
  682. return 0;
  683. }
  684. ERR_clear_error();
  685. }
  686. }
  687. ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
  688. return 0;
  689. }
  690. int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
  691. unsigned char *pass, ossl_ssize_t passlen)
  692. {
  693. STACK_OF(CMS_RecipientInfo) *ris;
  694. CMS_RecipientInfo *ri;
  695. int i, r;
  696. ris = CMS_get0_RecipientInfos(cms);
  697. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  698. ri = sk_CMS_RecipientInfo_value(ris, i);
  699. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
  700. continue;
  701. CMS_RecipientInfo_set0_password(ri, pass, passlen);
  702. r = CMS_RecipientInfo_decrypt(cms, ri);
  703. CMS_RecipientInfo_set0_password(ri, NULL, 0);
  704. if (r > 0)
  705. return 1;
  706. }
  707. ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
  708. return 0;
  709. }
  710. int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
  711. BIO *dcont, BIO *out, unsigned int flags)
  712. {
  713. int r;
  714. BIO *cont;
  715. int nid = OBJ_obj2nid(CMS_get0_type(cms));
  716. if (nid != NID_pkcs7_enveloped
  717. && nid != NID_id_smime_ct_authEnvelopedData) {
  718. ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENVELOPED_DATA);
  719. return 0;
  720. }
  721. if (dcont == NULL && !check_content(cms))
  722. return 0;
  723. if (flags & CMS_DEBUG_DECRYPT)
  724. ossl_cms_get0_env_enc_content(cms)->debug = 1;
  725. else
  726. ossl_cms_get0_env_enc_content(cms)->debug = 0;
  727. if (cert == NULL)
  728. ossl_cms_get0_env_enc_content(cms)->havenocert = 1;
  729. else
  730. ossl_cms_get0_env_enc_content(cms)->havenocert = 0;
  731. if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
  732. return 1;
  733. if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
  734. return 0;
  735. cont = CMS_dataInit(cms, dcont);
  736. if (cont == NULL)
  737. return 0;
  738. r = cms_copy_content(out, cont, flags);
  739. do_free_upto(cont, dcont);
  740. return r;
  741. }
  742. int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
  743. {
  744. BIO *cmsbio;
  745. int ret = 0;
  746. if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
  747. ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
  748. return 0;
  749. }
  750. ret = SMIME_crlf_copy(data, cmsbio, flags);
  751. (void)BIO_flush(cmsbio);
  752. if (!CMS_dataFinal(cms, cmsbio)) {
  753. ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
  754. goto err;
  755. }
  756. err:
  757. do_free_upto(cmsbio, dcont);
  758. return ret;
  759. }
  760. #ifdef ZLIB
  761. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  762. unsigned int flags)
  763. {
  764. BIO *cont;
  765. int r;
  766. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
  767. ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
  768. return 0;
  769. }
  770. if (dcont == NULL && !check_content(cms))
  771. return 0;
  772. cont = CMS_dataInit(cms, dcont);
  773. if (cont == NULL)
  774. return 0;
  775. r = cms_copy_content(out, cont, flags);
  776. do_free_upto(cont, dcont);
  777. return r;
  778. }
  779. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  780. {
  781. CMS_ContentInfo *cms;
  782. if (comp_nid <= 0)
  783. comp_nid = NID_zlib_compression;
  784. cms = ossl_cms_CompressedData_create(comp_nid, NULL, NULL);
  785. if (cms == NULL)
  786. return NULL;
  787. if (!(flags & CMS_DETACHED))
  788. CMS_set_detached(cms, 0);
  789. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  790. return cms;
  791. CMS_ContentInfo_free(cms);
  792. return NULL;
  793. }
  794. #else
  795. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  796. unsigned int flags)
  797. {
  798. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  799. return 0;
  800. }
  801. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  802. {
  803. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  804. return NULL;
  805. }
  806. #endif