cms_smime.c 27 KB

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