cms_smime.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /*
  2. * Copyright 2008-2023 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_CMS_LIB);
  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) <= 0)
  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. goto err;
  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. err:
  207. CMS_ContentInfo_free(cms);
  208. return NULL;
  209. }
  210. CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
  211. const unsigned char *key,
  212. size_t keylen, unsigned int flags)
  213. {
  214. return CMS_EncryptedData_encrypt_ex(in, cipher, key, keylen, flags, NULL,
  215. NULL);
  216. }
  217. static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
  218. X509_STORE *store,
  219. STACK_OF(X509) *untrusted,
  220. STACK_OF(X509_CRL) *crls,
  221. STACK_OF(X509) **chain,
  222. const CMS_CTX *cms_ctx)
  223. {
  224. X509_STORE_CTX *ctx;
  225. X509 *signer;
  226. int i, j, r = 0;
  227. ctx = X509_STORE_CTX_new_ex(ossl_cms_ctx_get0_libctx(cms_ctx),
  228. ossl_cms_ctx_get0_propq(cms_ctx));
  229. if (ctx == NULL) {
  230. ERR_raise(ERR_LIB_CMS, ERR_R_X509_LIB);
  231. goto err;
  232. }
  233. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  234. if (!X509_STORE_CTX_init(ctx, store, signer, untrusted)) {
  235. ERR_raise(ERR_LIB_CMS, CMS_R_STORE_INIT_ERROR);
  236. goto err;
  237. }
  238. X509_STORE_CTX_set_default(ctx, "smime_sign");
  239. if (crls != NULL)
  240. X509_STORE_CTX_set0_crls(ctx, crls);
  241. i = X509_verify_cert(ctx);
  242. if (i <= 0) {
  243. j = X509_STORE_CTX_get_error(ctx);
  244. ERR_raise_data(ERR_LIB_CMS, CMS_R_CERTIFICATE_VERIFY_ERROR,
  245. "Verify error: %s", X509_verify_cert_error_string(j));
  246. goto err;
  247. }
  248. r = 1;
  249. /* also send back the trust chain when required */
  250. if (chain != NULL)
  251. *chain = X509_STORE_CTX_get1_chain(ctx);
  252. err:
  253. X509_STORE_CTX_free(ctx);
  254. return r;
  255. }
  256. /* This strongly overlaps with PKCS7_verify() */
  257. int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
  258. X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
  259. {
  260. CMS_SignerInfo *si;
  261. STACK_OF(CMS_SignerInfo) *sinfos;
  262. STACK_OF(X509) *cms_certs = NULL;
  263. STACK_OF(X509_CRL) *crls = NULL;
  264. STACK_OF(X509) **si_chains = NULL;
  265. X509 *signer;
  266. int i, scount = 0, ret = 0;
  267. BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
  268. int cadesVerify = (flags & CMS_CADES) != 0;
  269. const CMS_CTX *ctx = ossl_cms_get0_cmsctx(cms);
  270. if (dcont == NULL && !check_content(cms))
  271. return 0;
  272. if (dcont != NULL && !(flags & CMS_BINARY)) {
  273. const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
  274. if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
  275. flags |= CMS_ASCIICRLF;
  276. }
  277. /* Attempt to find all signer certificates */
  278. sinfos = CMS_get0_SignerInfos(cms);
  279. if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
  280. ERR_raise(ERR_LIB_CMS, CMS_R_NO_SIGNERS);
  281. goto err;
  282. }
  283. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  284. si = sk_CMS_SignerInfo_value(sinfos, i);
  285. CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
  286. if (signer != NULL)
  287. scount++;
  288. }
  289. if (scount != sk_CMS_SignerInfo_num(sinfos))
  290. scount += CMS_set1_signers_certs(cms, certs, flags);
  291. if (scount != sk_CMS_SignerInfo_num(sinfos)) {
  292. ERR_raise(ERR_LIB_CMS, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
  293. goto err;
  294. }
  295. /* Attempt to verify all signers certs */
  296. /* at this point scount == sk_CMS_SignerInfo_num(sinfos) */
  297. if ((flags & CMS_NO_SIGNER_CERT_VERIFY) == 0 || cadesVerify) {
  298. if (cadesVerify) {
  299. /* Certificate trust chain is required to check CAdES signature */
  300. si_chains = OPENSSL_zalloc(scount * sizeof(si_chains[0]));
  301. if (si_chains == NULL)
  302. goto err;
  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_BIO_LIB);
  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_CMS_LIB);
  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. if (!SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT))
  370. goto err;
  371. if (flags & CMS_TEXT) {
  372. if (!SMIME_text(tmpout, out)) {
  373. ERR_raise(ERR_LIB_CMS, CMS_R_SMIME_TEXT_ERROR);
  374. goto err;
  375. }
  376. }
  377. } else {
  378. cmsbio = CMS_dataInit(cms, tmpin);
  379. if (cmsbio == NULL)
  380. goto err;
  381. if (!cms_copy_content(out, cmsbio, flags))
  382. goto err;
  383. }
  384. if (!(flags & CMS_NO_CONTENT_VERIFY)) {
  385. for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
  386. si = sk_CMS_SignerInfo_value(sinfos, i);
  387. if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
  388. ERR_raise(ERR_LIB_CMS, CMS_R_CONTENT_VERIFY_ERROR);
  389. goto err;
  390. }
  391. }
  392. }
  393. ret = 1;
  394. err:
  395. if (!(flags & SMIME_BINARY) && dcont) {
  396. do_free_upto(cmsbio, tmpout);
  397. if (tmpin != dcont)
  398. BIO_free(tmpin);
  399. } else {
  400. if (dcont && (tmpin == dcont))
  401. do_free_upto(cmsbio, dcont);
  402. else
  403. BIO_free_all(cmsbio);
  404. }
  405. if (out != tmpout)
  406. BIO_free_all(tmpout);
  407. err2:
  408. if (si_chains != NULL) {
  409. for (i = 0; i < scount; ++i)
  410. OSSL_STACK_OF_X509_free(si_chains[i]);
  411. OPENSSL_free(si_chains);
  412. }
  413. OSSL_STACK_OF_X509_free(cms_certs);
  414. sk_X509_CRL_pop_free(crls, X509_CRL_free);
  415. return ret;
  416. }
  417. int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
  418. STACK_OF(X509) *certs,
  419. X509_STORE *store, unsigned int flags)
  420. {
  421. int r;
  422. flags &= ~(CMS_DETACHED | CMS_TEXT);
  423. r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
  424. if (r <= 0)
  425. return r;
  426. return ossl_cms_Receipt_verify(rcms, ocms);
  427. }
  428. CMS_ContentInfo *CMS_sign_ex(X509 *signcert, EVP_PKEY *pkey,
  429. STACK_OF(X509) *certs, BIO *data,
  430. unsigned int flags, OSSL_LIB_CTX *libctx,
  431. const char *propq)
  432. {
  433. CMS_ContentInfo *cms;
  434. int i;
  435. cms = CMS_ContentInfo_new_ex(libctx, propq);
  436. if (cms == NULL || !CMS_SignedData_init(cms)) {
  437. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  438. goto err;
  439. }
  440. if (flags & CMS_ASCIICRLF
  441. && !CMS_set1_eContentType(cms,
  442. OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF))) {
  443. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  444. goto err;
  445. }
  446. if (pkey != NULL && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
  447. ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
  448. goto err;
  449. }
  450. for (i = 0; i < sk_X509_num(certs); i++) {
  451. X509 *x = sk_X509_value(certs, i);
  452. if (!CMS_add1_cert(cms, x)) {
  453. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  454. goto err;
  455. }
  456. }
  457. if (!(flags & CMS_DETACHED))
  458. CMS_set_detached(cms, 0);
  459. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  460. || CMS_final(cms, data, NULL, flags))
  461. return cms;
  462. else
  463. goto err;
  464. err:
  465. CMS_ContentInfo_free(cms);
  466. return NULL;
  467. }
  468. CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
  469. BIO *data, unsigned int flags)
  470. {
  471. return CMS_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
  472. }
  473. CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
  474. X509 *signcert, EVP_PKEY *pkey,
  475. STACK_OF(X509) *certs, unsigned int flags)
  476. {
  477. CMS_SignerInfo *rct_si;
  478. CMS_ContentInfo *cms = NULL;
  479. ASN1_OCTET_STRING **pos, *os = NULL;
  480. BIO *rct_cont = NULL;
  481. int r = 0;
  482. const CMS_CTX *ctx = si->cms_ctx;
  483. flags &= ~(CMS_STREAM | CMS_TEXT);
  484. /* Not really detached but avoids content being allocated */
  485. flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
  486. if (pkey == NULL || signcert == NULL) {
  487. ERR_raise(ERR_LIB_CMS, CMS_R_NO_KEY_OR_CERT);
  488. return NULL;
  489. }
  490. /* Initialize signed data */
  491. cms = CMS_sign_ex(NULL, NULL, certs, NULL, flags,
  492. ossl_cms_ctx_get0_libctx(ctx),
  493. ossl_cms_ctx_get0_propq(ctx));
  494. if (cms == NULL)
  495. goto err;
  496. /* Set inner content type to signed receipt */
  497. if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
  498. goto err;
  499. rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
  500. if (!rct_si) {
  501. ERR_raise(ERR_LIB_CMS, CMS_R_ADD_SIGNER_ERROR);
  502. goto err;
  503. }
  504. os = ossl_cms_encode_Receipt(si);
  505. if (os == NULL)
  506. goto err;
  507. /* Set content to digest */
  508. rct_cont = BIO_new_mem_buf(os->data, os->length);
  509. if (rct_cont == NULL)
  510. goto err;
  511. /* Add msgSigDigest attribute */
  512. if (!ossl_cms_msgSigDigest_add1(rct_si, si))
  513. goto err;
  514. /* Finalize structure */
  515. if (!CMS_final(cms, rct_cont, NULL, flags))
  516. goto err;
  517. /* Set embedded content */
  518. pos = CMS_get0_content(cms);
  519. if (pos == NULL)
  520. goto err;
  521. *pos = os;
  522. r = 1;
  523. err:
  524. BIO_free(rct_cont);
  525. if (r)
  526. return cms;
  527. CMS_ContentInfo_free(cms);
  528. ASN1_OCTET_STRING_free(os);
  529. return NULL;
  530. }
  531. CMS_ContentInfo *CMS_encrypt_ex(STACK_OF(X509) *certs, BIO *data,
  532. const EVP_CIPHER *cipher, unsigned int flags,
  533. OSSL_LIB_CTX *libctx, const char *propq)
  534. {
  535. CMS_ContentInfo *cms;
  536. int i;
  537. X509 *recip;
  538. cms = (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
  539. ? CMS_AuthEnvelopedData_create_ex(cipher, libctx, propq)
  540. : CMS_EnvelopedData_create_ex(cipher, libctx, propq);
  541. if (cms == NULL) {
  542. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  543. goto err;
  544. }
  545. for (i = 0; i < sk_X509_num(certs); i++) {
  546. recip = sk_X509_value(certs, i);
  547. if (!CMS_add1_recipient_cert(cms, recip, flags)) {
  548. ERR_raise(ERR_LIB_CMS, CMS_R_RECIPIENT_ERROR);
  549. goto err;
  550. }
  551. }
  552. if (!(flags & CMS_DETACHED))
  553. CMS_set_detached(cms, 0);
  554. if ((flags & (CMS_STREAM | CMS_PARTIAL))
  555. || CMS_final(cms, data, NULL, flags))
  556. return cms;
  557. else
  558. ERR_raise(ERR_LIB_CMS, ERR_R_CMS_LIB);
  559. err:
  560. CMS_ContentInfo_free(cms);
  561. return NULL;
  562. }
  563. CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
  564. const EVP_CIPHER *cipher, unsigned int flags)
  565. {
  566. return CMS_encrypt_ex(certs, data, cipher, flags, NULL, NULL);
  567. }
  568. static int cms_kari_set1_pkey_and_peer(CMS_ContentInfo *cms,
  569. CMS_RecipientInfo *ri,
  570. EVP_PKEY *pk, X509 *cert, X509 *peer)
  571. {
  572. int i;
  573. STACK_OF(CMS_RecipientEncryptedKey) *reks;
  574. CMS_RecipientEncryptedKey *rek;
  575. reks = CMS_RecipientInfo_kari_get0_reks(ri);
  576. for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
  577. int rv;
  578. rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
  579. if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
  580. continue;
  581. CMS_RecipientInfo_kari_set0_pkey_and_peer(ri, pk, peer);
  582. rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
  583. CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
  584. if (rv > 0)
  585. return 1;
  586. return cert == NULL ? 0 : -1;
  587. }
  588. return 0;
  589. }
  590. int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
  591. {
  592. return CMS_decrypt_set1_pkey_and_peer(cms, pk, cert, NULL);
  593. }
  594. int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms, EVP_PKEY *pk,
  595. X509 *cert, X509 *peer)
  596. {
  597. STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
  598. CMS_RecipientInfo *ri;
  599. int i, r, cms_pkey_ri_type;
  600. int debug = 0, match_ri = 0;
  601. CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
  602. /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
  603. if (ec != NULL) {
  604. OPENSSL_clear_free(ec->key, ec->keylen);
  605. ec->key = NULL;
  606. ec->keylen = 0;
  607. }
  608. if (ris != NULL && ec != NULL)
  609. debug = ec->debug;
  610. cms_pkey_ri_type = ossl_cms_pkey_get_ri_type(pk);
  611. if (cms_pkey_ri_type == CMS_RECIPINFO_NONE) {
  612. ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
  613. return 0;
  614. }
  615. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  616. int ri_type;
  617. ri = sk_CMS_RecipientInfo_value(ris, i);
  618. ri_type = CMS_RecipientInfo_type(ri);
  619. if (!ossl_cms_pkey_is_ri_type_supported(pk, ri_type))
  620. continue;
  621. match_ri = 1;
  622. if (ri_type == CMS_RECIPINFO_AGREE) {
  623. r = cms_kari_set1_pkey_and_peer(cms, ri, pk, cert, peer);
  624. if (r > 0)
  625. return 1;
  626. if (r < 0)
  627. return 0;
  628. }
  629. /* If we have a cert, try matching RecipientInfo, else try them all */
  630. else if (cert == NULL || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
  631. EVP_PKEY_up_ref(pk);
  632. CMS_RecipientInfo_set0_pkey(ri, pk);
  633. r = CMS_RecipientInfo_decrypt(cms, ri);
  634. CMS_RecipientInfo_set0_pkey(ri, NULL);
  635. if (cert != NULL) {
  636. /*
  637. * If not debugging clear any error and return success to
  638. * avoid leaking of information useful to MMA
  639. */
  640. if (!debug) {
  641. ERR_clear_error();
  642. return 1;
  643. }
  644. if (r > 0)
  645. return 1;
  646. ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
  647. return 0;
  648. }
  649. /*
  650. * If no cert and not debugging don't leave loop after first
  651. * successful decrypt. Always attempt to decrypt all recipients
  652. * to avoid leaking timing of a successful decrypt.
  653. */
  654. else if (r > 0 && (debug || cms_pkey_ri_type != CMS_RECIPINFO_TRANS))
  655. return 1;
  656. }
  657. }
  658. /* If no cert, key transport and not debugging always return success */
  659. if (cert == NULL
  660. && cms_pkey_ri_type == CMS_RECIPINFO_TRANS
  661. && match_ri
  662. && !debug) {
  663. ERR_clear_error();
  664. return 1;
  665. }
  666. if (!match_ri)
  667. ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
  668. return 0;
  669. }
  670. int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
  671. unsigned char *key, size_t keylen,
  672. const unsigned char *id, size_t idlen)
  673. {
  674. STACK_OF(CMS_RecipientInfo) *ris;
  675. CMS_RecipientInfo *ri;
  676. int i, r, match_ri = 0;
  677. ris = CMS_get0_RecipientInfos(cms);
  678. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  679. ri = sk_CMS_RecipientInfo_value(ris, i);
  680. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
  681. continue;
  682. /* If we have an id, try matching RecipientInfo, else try them all */
  683. if (id == NULL
  684. || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
  685. match_ri = 1;
  686. CMS_RecipientInfo_set0_key(ri, key, keylen);
  687. r = CMS_RecipientInfo_decrypt(cms, ri);
  688. CMS_RecipientInfo_set0_key(ri, NULL, 0);
  689. if (r > 0)
  690. return 1;
  691. if (id != NULL) {
  692. ERR_raise(ERR_LIB_CMS, CMS_R_DECRYPT_ERROR);
  693. return 0;
  694. }
  695. ERR_clear_error();
  696. }
  697. }
  698. if (!match_ri)
  699. ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
  700. return 0;
  701. }
  702. int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
  703. unsigned char *pass, ossl_ssize_t passlen)
  704. {
  705. STACK_OF(CMS_RecipientInfo) *ris = CMS_get0_RecipientInfos(cms);
  706. CMS_RecipientInfo *ri;
  707. int i, r, match_ri = 0;
  708. CMS_EncryptedContentInfo *ec = ossl_cms_get0_env_enc_content(cms);
  709. /* Prevent mem leak on earlier CMS_decrypt_set1_{pkey_and_peer,password} */
  710. if (ec != NULL) {
  711. OPENSSL_clear_free(ec->key, ec->keylen);
  712. ec->key = NULL;
  713. ec->keylen = 0;
  714. }
  715. for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
  716. ri = sk_CMS_RecipientInfo_value(ris, i);
  717. if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
  718. continue;
  719. /* Must try each PasswordRecipientInfo */
  720. match_ri = 1;
  721. CMS_RecipientInfo_set0_password(ri, pass, passlen);
  722. r = CMS_RecipientInfo_decrypt(cms, ri);
  723. CMS_RecipientInfo_set0_password(ri, NULL, 0);
  724. if (r > 0)
  725. return 1;
  726. }
  727. if (!match_ri)
  728. ERR_raise(ERR_LIB_CMS, CMS_R_NO_MATCHING_RECIPIENT);
  729. return 0;
  730. }
  731. int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
  732. BIO *dcont, BIO *out, unsigned int flags)
  733. {
  734. int r;
  735. BIO *cont;
  736. CMS_EncryptedContentInfo *ec;
  737. int nid = OBJ_obj2nid(CMS_get0_type(cms));
  738. if (nid != NID_pkcs7_enveloped
  739. && nid != NID_id_smime_ct_authEnvelopedData) {
  740. ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_ENVELOPED_DATA);
  741. return 0;
  742. }
  743. if (dcont == NULL && !check_content(cms))
  744. return 0;
  745. ec = ossl_cms_get0_env_enc_content(cms);
  746. ec->debug = (flags & CMS_DEBUG_DECRYPT) != 0;
  747. ec->havenocert = cert == NULL;
  748. if (pk == NULL && cert == NULL && dcont == NULL && out == NULL)
  749. return 1;
  750. if (pk != NULL && !CMS_decrypt_set1_pkey(cms, pk, cert))
  751. return 0;
  752. cont = CMS_dataInit(cms, dcont);
  753. if (cont == NULL)
  754. return 0;
  755. r = cms_copy_content(out, cont, flags);
  756. do_free_upto(cont, dcont);
  757. return r;
  758. }
  759. int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
  760. {
  761. BIO *cmsbio;
  762. int ret = 0;
  763. if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
  764. ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
  765. return 0;
  766. }
  767. if (!SMIME_crlf_copy(data, cmsbio, flags)) {
  768. goto err;
  769. }
  770. (void)BIO_flush(cmsbio);
  771. if (!CMS_dataFinal(cms, cmsbio)) {
  772. ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
  773. goto err;
  774. }
  775. ret = 1;
  776. err:
  777. do_free_upto(cmsbio, dcont);
  778. return ret;
  779. }
  780. int CMS_final_digest(CMS_ContentInfo *cms,
  781. const unsigned char *md, unsigned int mdlen,
  782. BIO *dcont, unsigned int flags)
  783. {
  784. BIO *cmsbio;
  785. int ret = 0;
  786. if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
  787. ERR_raise(ERR_LIB_CMS, CMS_R_CMS_LIB);
  788. return 0;
  789. }
  790. (void)BIO_flush(cmsbio);
  791. if (!ossl_cms_DataFinal(cms, cmsbio, md, mdlen)) {
  792. ERR_raise(ERR_LIB_CMS, CMS_R_CMS_DATAFINAL_ERROR);
  793. goto err;
  794. }
  795. ret = 1;
  796. err:
  797. do_free_upto(cmsbio, dcont);
  798. return ret;
  799. }
  800. #ifndef OPENSSL_NO_ZLIB
  801. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  802. unsigned int flags)
  803. {
  804. BIO *cont;
  805. int r;
  806. if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
  807. ERR_raise(ERR_LIB_CMS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
  808. return 0;
  809. }
  810. if (dcont == NULL && !check_content(cms))
  811. return 0;
  812. cont = CMS_dataInit(cms, dcont);
  813. if (cont == NULL)
  814. return 0;
  815. r = cms_copy_content(out, cont, flags);
  816. do_free_upto(cont, dcont);
  817. return r;
  818. }
  819. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  820. {
  821. CMS_ContentInfo *cms;
  822. if (comp_nid <= 0)
  823. comp_nid = NID_zlib_compression;
  824. cms = ossl_cms_CompressedData_create(comp_nid, NULL, NULL);
  825. if (cms == NULL)
  826. return NULL;
  827. if (!(flags & CMS_DETACHED))
  828. CMS_set_detached(cms, 0);
  829. if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
  830. return cms;
  831. CMS_ContentInfo_free(cms);
  832. return NULL;
  833. }
  834. #else
  835. int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
  836. unsigned int flags)
  837. {
  838. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  839. return 0;
  840. }
  841. CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
  842. {
  843. ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
  844. return NULL;
  845. }
  846. #endif