pk7_smime.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * Copyright 1999-2024 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. /* Simple PKCS#7 processing functions */
  10. #include <stdio.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/x509.h>
  13. #include <openssl/x509v3.h>
  14. #include "pk7_local.h"
  15. #define BUFFERSIZE 4096
  16. static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si);
  17. PKCS7 *PKCS7_sign_ex(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
  18. BIO *data, int flags, OSSL_LIB_CTX *libctx,
  19. const char *propq)
  20. {
  21. PKCS7 *p7;
  22. int i;
  23. if ((p7 = PKCS7_new_ex(libctx, propq)) == NULL) {
  24. ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
  25. return NULL;
  26. }
  27. if (!PKCS7_set_type(p7, NID_pkcs7_signed))
  28. goto err;
  29. if (!PKCS7_content_new(p7, NID_pkcs7_data))
  30. goto err;
  31. if (pkey && !PKCS7_sign_add_signer(p7, signcert, pkey, NULL, flags)) {
  32. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_ADD_SIGNER_ERROR);
  33. goto err;
  34. }
  35. if (!(flags & PKCS7_NOCERTS)) {
  36. for (i = 0; i < sk_X509_num(certs); i++) {
  37. if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i)))
  38. goto err;
  39. }
  40. }
  41. if (flags & PKCS7_DETACHED)
  42. PKCS7_set_detached(p7, 1);
  43. if (flags & (PKCS7_STREAM | PKCS7_PARTIAL))
  44. return p7;
  45. if (PKCS7_final(p7, data, flags))
  46. return p7;
  47. err:
  48. PKCS7_free(p7);
  49. return NULL;
  50. }
  51. PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
  52. BIO *data, int flags)
  53. {
  54. return PKCS7_sign_ex(signcert, pkey, certs, data, flags, NULL, NULL);
  55. }
  56. int PKCS7_final(PKCS7 *p7, BIO *data, int flags)
  57. {
  58. BIO *p7bio;
  59. int ret = 0;
  60. if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
  61. ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
  62. return 0;
  63. }
  64. if (!SMIME_crlf_copy(data, p7bio, flags))
  65. goto err;
  66. (void)BIO_flush(p7bio);
  67. if (!PKCS7_dataFinal(p7, p7bio)) {
  68. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_DATASIGN);
  69. goto err;
  70. }
  71. ret = 1;
  72. err:
  73. BIO_free_all(p7bio);
  74. return ret;
  75. }
  76. /* Check to see if a cipher exists and if so add S/MIME capabilities */
  77. static int add_cipher_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
  78. {
  79. if (EVP_get_cipherbynid(nid))
  80. return PKCS7_simple_smimecap(sk, nid, arg);
  81. return 1;
  82. }
  83. static int add_digest_smcap(STACK_OF(X509_ALGOR) *sk, int nid, int arg)
  84. {
  85. if (EVP_get_digestbynid(nid))
  86. return PKCS7_simple_smimecap(sk, nid, arg);
  87. return 1;
  88. }
  89. PKCS7_SIGNER_INFO *PKCS7_sign_add_signer(PKCS7 *p7, X509 *signcert,
  90. EVP_PKEY *pkey, const EVP_MD *md,
  91. int flags)
  92. {
  93. PKCS7_SIGNER_INFO *si = NULL;
  94. STACK_OF(X509_ALGOR) *smcap = NULL;
  95. if (!X509_check_private_key(signcert, pkey)) {
  96. ERR_raise(ERR_LIB_PKCS7,
  97. PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  98. return NULL;
  99. }
  100. if ((si = PKCS7_add_signature(p7, signcert, pkey, md)) == NULL) {
  101. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
  102. return NULL;
  103. }
  104. si->ctx = ossl_pkcs7_get0_ctx(p7);
  105. if (!(flags & PKCS7_NOCERTS)) {
  106. if (!PKCS7_add_certificate(p7, signcert))
  107. goto err;
  108. }
  109. if (!(flags & PKCS7_NOATTR)) {
  110. if (!PKCS7_add_attrib_content_type(si, NULL))
  111. goto err;
  112. /* Add SMIMECapabilities */
  113. if (!(flags & PKCS7_NOSMIMECAP)) {
  114. if ((smcap = sk_X509_ALGOR_new_null()) == NULL) {
  115. ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
  116. goto err;
  117. }
  118. if (!add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
  119. || !add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
  120. || !add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
  121. || !add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
  122. || !add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
  123. || !add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
  124. || !add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
  125. || !add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
  126. || !add_cipher_smcap(smcap, NID_rc2_cbc, 128)
  127. || !add_cipher_smcap(smcap, NID_rc2_cbc, 64)
  128. || !add_cipher_smcap(smcap, NID_des_cbc, -1)
  129. || !add_cipher_smcap(smcap, NID_rc2_cbc, 40)
  130. || !PKCS7_add_attrib_smimecap(si, smcap))
  131. goto err;
  132. sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
  133. smcap = NULL;
  134. }
  135. if (flags & PKCS7_REUSE_DIGEST) {
  136. if (!pkcs7_copy_existing_digest(p7, si))
  137. goto err;
  138. if (!(flags & PKCS7_PARTIAL)
  139. && !PKCS7_SIGNER_INFO_sign(si))
  140. goto err;
  141. }
  142. }
  143. return si;
  144. err:
  145. sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
  146. return NULL;
  147. }
  148. /*
  149. * Search for a digest matching SignerInfo digest type and if found copy
  150. * across.
  151. */
  152. static int pkcs7_copy_existing_digest(PKCS7 *p7, PKCS7_SIGNER_INFO *si)
  153. {
  154. int i;
  155. STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
  156. PKCS7_SIGNER_INFO *sitmp;
  157. ASN1_OCTET_STRING *osdig = NULL;
  158. sinfos = PKCS7_get_signer_info(p7);
  159. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
  160. sitmp = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
  161. if (si == sitmp)
  162. break;
  163. if (sk_X509_ATTRIBUTE_num(sitmp->auth_attr) <= 0)
  164. continue;
  165. if (!OBJ_cmp(si->digest_alg->algorithm, sitmp->digest_alg->algorithm)) {
  166. osdig = PKCS7_digest_from_attributes(sitmp->auth_attr);
  167. break;
  168. }
  169. }
  170. if (osdig != NULL)
  171. return PKCS7_add1_attrib_digest(si, osdig->data, osdig->length);
  172. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_MATCHING_DIGEST_TYPE_FOUND);
  173. return 0;
  174. }
  175. /* This strongly overlaps with CMS_verify(), partly with PKCS7_dataVerify() */
  176. int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
  177. BIO *indata, BIO *out, int flags)
  178. {
  179. STACK_OF(X509) *signers;
  180. X509 *signer;
  181. STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
  182. PKCS7_SIGNER_INFO *si;
  183. X509_STORE_CTX *cert_ctx = NULL;
  184. char *buf = NULL;
  185. int i, j = 0, k, ret = 0;
  186. BIO *p7bio = NULL;
  187. BIO *tmpout = NULL;
  188. const PKCS7_CTX *p7_ctx;
  189. if (p7 == NULL) {
  190. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
  191. return 0;
  192. }
  193. if (!PKCS7_type_is_signed(p7)) {
  194. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
  195. return 0;
  196. }
  197. /* Check for no data and no content: no data to verify signature */
  198. if (PKCS7_get_detached(p7) && indata == NULL) {
  199. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_CONTENT);
  200. return 0;
  201. }
  202. if (flags & PKCS7_NO_DUAL_CONTENT) {
  203. /*
  204. * This was originally "#if 0" because we thought that only old broken
  205. * Netscape did this. It turns out that Authenticode uses this kind
  206. * of "extended" PKCS7 format, and things like UEFI secure boot and
  207. * tools like osslsigncode need it. In Authenticode the verification
  208. * process is different, but the existing PKCs7 verification works.
  209. */
  210. if (!PKCS7_get_detached(p7) && indata != NULL) {
  211. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_CONTENT_AND_DATA_PRESENT);
  212. return 0;
  213. }
  214. }
  215. sinfos = PKCS7_get_signer_info(p7);
  216. if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
  217. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_SIGNATURES_ON_DATA);
  218. return 0;
  219. }
  220. signers = PKCS7_get0_signers(p7, certs, flags);
  221. if (signers == NULL)
  222. return 0;
  223. /* Now verify the certificates */
  224. p7_ctx = ossl_pkcs7_get0_ctx(p7);
  225. cert_ctx = X509_STORE_CTX_new_ex(ossl_pkcs7_ctx_get0_libctx(p7_ctx),
  226. ossl_pkcs7_ctx_get0_propq(p7_ctx));
  227. if (cert_ctx == NULL)
  228. goto err;
  229. if (!(flags & PKCS7_NOVERIFY))
  230. for (k = 0; k < sk_X509_num(signers); k++) {
  231. signer = sk_X509_value(signers, k);
  232. if (!(flags & PKCS7_NOCHAIN)) {
  233. if (!X509_STORE_CTX_init(cert_ctx, store, signer,
  234. p7->d.sign->cert)) {
  235. ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
  236. goto err;
  237. }
  238. if (!X509_STORE_CTX_set_default(cert_ctx, "smime_sign"))
  239. goto err;
  240. } else if (!X509_STORE_CTX_init(cert_ctx, store, signer, NULL)) {
  241. ERR_raise(ERR_LIB_PKCS7, ERR_R_X509_LIB);
  242. goto err;
  243. }
  244. if (!(flags & PKCS7_NOCRL))
  245. X509_STORE_CTX_set0_crls(cert_ctx, p7->d.sign->crl);
  246. i = X509_verify_cert(cert_ctx);
  247. if (i <= 0) {
  248. j = X509_STORE_CTX_get_error(cert_ctx);
  249. ERR_raise_data(ERR_LIB_PKCS7, PKCS7_R_CERTIFICATE_VERIFY_ERROR,
  250. "Verify error: %s",
  251. X509_verify_cert_error_string(j));
  252. goto err;
  253. }
  254. /* Check for revocation status here */
  255. }
  256. if ((p7bio = PKCS7_dataInit(p7, indata)) == NULL)
  257. goto err;
  258. if (flags & PKCS7_TEXT) {
  259. if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
  260. ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
  261. goto err;
  262. }
  263. BIO_set_mem_eof_return(tmpout, 0);
  264. } else
  265. tmpout = out;
  266. /* We now have to 'read' from p7bio to calculate digests etc. */
  267. if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL)
  268. goto err;
  269. for (;;) {
  270. i = BIO_read(p7bio, buf, BUFFERSIZE);
  271. if (i <= 0)
  272. break;
  273. if (tmpout)
  274. BIO_write(tmpout, buf, i);
  275. }
  276. if (flags & PKCS7_TEXT) {
  277. if (!SMIME_text(tmpout, out)) {
  278. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SMIME_TEXT_ERROR);
  279. BIO_free(tmpout);
  280. goto err;
  281. }
  282. BIO_free(tmpout);
  283. }
  284. /* Now Verify All Signatures */
  285. if (!(flags & PKCS7_NOSIGS))
  286. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
  287. si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
  288. signer = sk_X509_value(signers, i);
  289. j = PKCS7_signatureVerify(p7bio, p7, si, signer);
  290. if (j <= 0) {
  291. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
  292. goto err;
  293. }
  294. }
  295. ret = 1;
  296. err:
  297. X509_STORE_CTX_free(cert_ctx);
  298. OPENSSL_free(buf);
  299. if (indata != NULL)
  300. BIO_pop(p7bio);
  301. BIO_free_all(p7bio);
  302. sk_X509_free(signers);
  303. return ret;
  304. }
  305. STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
  306. int flags)
  307. {
  308. STACK_OF(X509) *signers;
  309. STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
  310. PKCS7_SIGNER_INFO *si;
  311. PKCS7_ISSUER_AND_SERIAL *ias;
  312. X509 *signer;
  313. int i;
  314. if (p7 == NULL) {
  315. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
  316. return NULL;
  317. }
  318. if (!PKCS7_type_is_signed(p7)) {
  319. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
  320. return NULL;
  321. }
  322. /* Collect all the signers together */
  323. sinfos = PKCS7_get_signer_info(p7);
  324. if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
  325. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_SIGNERS);
  326. return 0;
  327. }
  328. if ((signers = sk_X509_new_null()) == NULL) {
  329. ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
  330. return NULL;
  331. }
  332. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
  333. si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
  334. ias = si->issuer_and_serial;
  335. signer = NULL;
  336. /* If any certificates passed they take priority */
  337. if (certs != NULL)
  338. signer = X509_find_by_issuer_and_serial(certs,
  339. ias->issuer, ias->serial);
  340. if (signer == NULL && !(flags & PKCS7_NOINTERN)
  341. && p7->d.sign->cert)
  342. signer =
  343. X509_find_by_issuer_and_serial(p7->d.sign->cert,
  344. ias->issuer, ias->serial);
  345. if (signer == NULL) {
  346. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
  347. sk_X509_free(signers);
  348. return 0;
  349. }
  350. if (!sk_X509_push(signers, signer)) {
  351. sk_X509_free(signers);
  352. return NULL;
  353. }
  354. }
  355. return signers;
  356. }
  357. /* Build a complete PKCS#7 enveloped data */
  358. PKCS7 *PKCS7_encrypt_ex(STACK_OF(X509) *certs, BIO *in,
  359. const EVP_CIPHER *cipher, int flags,
  360. OSSL_LIB_CTX *libctx, const char *propq)
  361. {
  362. PKCS7 *p7;
  363. BIO *p7bio = NULL;
  364. int i;
  365. X509 *x509;
  366. if ((p7 = PKCS7_new_ex(libctx, propq)) == NULL) {
  367. ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
  368. return NULL;
  369. }
  370. if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
  371. goto err;
  372. if (!PKCS7_set_cipher(p7, cipher)) {
  373. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_SETTING_CIPHER);
  374. goto err;
  375. }
  376. for (i = 0; i < sk_X509_num(certs); i++) {
  377. x509 = sk_X509_value(certs, i);
  378. if (!PKCS7_add_recipient(p7, x509)) {
  379. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_ADDING_RECIPIENT);
  380. goto err;
  381. }
  382. }
  383. if (flags & PKCS7_STREAM)
  384. return p7;
  385. if (PKCS7_final(p7, in, flags))
  386. return p7;
  387. err:
  388. BIO_free_all(p7bio);
  389. PKCS7_free(p7);
  390. return NULL;
  391. }
  392. PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
  393. int flags)
  394. {
  395. return PKCS7_encrypt_ex(certs, in, cipher, flags, NULL, NULL);
  396. }
  397. int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
  398. {
  399. BIO *tmpmem;
  400. int ret = 0, i;
  401. char *buf = NULL;
  402. if (p7 == NULL) {
  403. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
  404. return 0;
  405. }
  406. if (!PKCS7_type_is_enveloped(p7)
  407. && !PKCS7_type_is_signedAndEnveloped(p7)) {
  408. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
  409. return 0;
  410. }
  411. if (cert && !X509_check_private_key(cert, pkey)) {
  412. ERR_raise(ERR_LIB_PKCS7,
  413. PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  414. return 0;
  415. }
  416. if ((tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert)) == NULL) {
  417. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_DECRYPT_ERROR);
  418. return 0;
  419. }
  420. if (flags & PKCS7_TEXT) {
  421. BIO *tmpbuf, *bread;
  422. /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
  423. if ((tmpbuf = BIO_new(BIO_f_buffer())) == NULL) {
  424. ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
  425. BIO_free_all(tmpmem);
  426. return 0;
  427. }
  428. if ((bread = BIO_push(tmpbuf, tmpmem)) == NULL) {
  429. ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
  430. BIO_free_all(tmpbuf);
  431. BIO_free_all(tmpmem);
  432. return 0;
  433. }
  434. ret = SMIME_text(bread, data);
  435. if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
  436. if (BIO_get_cipher_status(tmpmem) <= 0)
  437. ret = 0;
  438. }
  439. BIO_free_all(bread);
  440. return ret;
  441. }
  442. if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL)
  443. goto err;
  444. for (;;) {
  445. i = BIO_read(tmpmem, buf, BUFFERSIZE);
  446. if (i <= 0) {
  447. ret = 1;
  448. if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
  449. if (BIO_get_cipher_status(tmpmem) <= 0)
  450. ret = 0;
  451. }
  452. break;
  453. }
  454. if (BIO_write(data, buf, i) != i) {
  455. break;
  456. }
  457. }
  458. err:
  459. OPENSSL_free(buf);
  460. BIO_free_all(tmpmem);
  461. return ret;
  462. }