pk7_smime.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * Copyright 1999-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. /* 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. if (i <= 0) {
  250. ERR_raise_data(ERR_LIB_PKCS7, PKCS7_R_CERTIFICATE_VERIFY_ERROR,
  251. "Verify error: %s",
  252. X509_verify_cert_error_string(j));
  253. goto err;
  254. }
  255. /* Check for revocation status here */
  256. }
  257. if ((p7bio = PKCS7_dataInit(p7, indata)) == NULL)
  258. goto err;
  259. if (flags & PKCS7_TEXT) {
  260. if ((tmpout = BIO_new(BIO_s_mem())) == NULL) {
  261. ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB);
  262. goto err;
  263. }
  264. BIO_set_mem_eof_return(tmpout, 0);
  265. } else
  266. tmpout = out;
  267. /* We now have to 'read' from p7bio to calculate digests etc. */
  268. if ((buf = OPENSSL_malloc(BUFFERSIZE)) == NULL)
  269. goto err;
  270. for (;;) {
  271. i = BIO_read(p7bio, buf, BUFFERSIZE);
  272. if (i <= 0)
  273. break;
  274. if (tmpout)
  275. BIO_write(tmpout, buf, i);
  276. }
  277. if (flags & PKCS7_TEXT) {
  278. if (!SMIME_text(tmpout, out)) {
  279. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SMIME_TEXT_ERROR);
  280. BIO_free(tmpout);
  281. goto err;
  282. }
  283. BIO_free(tmpout);
  284. }
  285. /* Now Verify All Signatures */
  286. if (!(flags & PKCS7_NOSIGS))
  287. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
  288. si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
  289. signer = sk_X509_value(signers, i);
  290. j = PKCS7_signatureVerify(p7bio, p7, si, signer);
  291. if (j <= 0) {
  292. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNATURE_FAILURE);
  293. goto err;
  294. }
  295. }
  296. ret = 1;
  297. err:
  298. X509_STORE_CTX_free(cert_ctx);
  299. OPENSSL_free(buf);
  300. if (indata != NULL)
  301. BIO_pop(p7bio);
  302. BIO_free_all(p7bio);
  303. sk_X509_free(signers);
  304. return ret;
  305. }
  306. STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
  307. int flags)
  308. {
  309. STACK_OF(X509) *signers;
  310. STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
  311. PKCS7_SIGNER_INFO *si;
  312. PKCS7_ISSUER_AND_SERIAL *ias;
  313. X509 *signer;
  314. int i;
  315. if (p7 == NULL) {
  316. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
  317. return NULL;
  318. }
  319. if (!PKCS7_type_is_signed(p7)) {
  320. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_WRONG_CONTENT_TYPE);
  321. return NULL;
  322. }
  323. /* Collect all the signers together */
  324. sinfos = PKCS7_get_signer_info(p7);
  325. if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
  326. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_NO_SIGNERS);
  327. return 0;
  328. }
  329. if ((signers = sk_X509_new_null()) == NULL) {
  330. ERR_raise(ERR_LIB_PKCS7, ERR_R_CRYPTO_LIB);
  331. return NULL;
  332. }
  333. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
  334. si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
  335. ias = si->issuer_and_serial;
  336. signer = NULL;
  337. /* If any certificates passed they take priority */
  338. if (certs != NULL)
  339. signer = X509_find_by_issuer_and_serial(certs,
  340. ias->issuer, ias->serial);
  341. if (signer == NULL && !(flags & PKCS7_NOINTERN)
  342. && p7->d.sign->cert)
  343. signer =
  344. X509_find_by_issuer_and_serial(p7->d.sign->cert,
  345. ias->issuer, ias->serial);
  346. if (signer == NULL) {
  347. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
  348. sk_X509_free(signers);
  349. return 0;
  350. }
  351. if (!sk_X509_push(signers, signer)) {
  352. sk_X509_free(signers);
  353. return NULL;
  354. }
  355. }
  356. return signers;
  357. }
  358. /* Build a complete PKCS#7 enveloped data */
  359. PKCS7 *PKCS7_encrypt_ex(STACK_OF(X509) *certs, BIO *in,
  360. const EVP_CIPHER *cipher, int flags,
  361. OSSL_LIB_CTX *libctx, const char *propq)
  362. {
  363. PKCS7 *p7;
  364. BIO *p7bio = NULL;
  365. int i;
  366. X509 *x509;
  367. if ((p7 = PKCS7_new_ex(libctx, propq)) == NULL) {
  368. ERR_raise(ERR_LIB_PKCS7, ERR_R_PKCS7_LIB);
  369. return NULL;
  370. }
  371. if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
  372. goto err;
  373. if (!PKCS7_set_cipher(p7, cipher)) {
  374. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_SETTING_CIPHER);
  375. goto err;
  376. }
  377. for (i = 0; i < sk_X509_num(certs); i++) {
  378. x509 = sk_X509_value(certs, i);
  379. if (!PKCS7_add_recipient(p7, x509)) {
  380. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_ERROR_ADDING_RECIPIENT);
  381. goto err;
  382. }
  383. }
  384. if (flags & PKCS7_STREAM)
  385. return p7;
  386. if (PKCS7_final(p7, in, flags))
  387. return p7;
  388. err:
  389. BIO_free_all(p7bio);
  390. PKCS7_free(p7);
  391. return NULL;
  392. }
  393. PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
  394. int flags)
  395. {
  396. return PKCS7_encrypt_ex(certs, in, cipher, flags, NULL, NULL);
  397. }
  398. int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
  399. {
  400. BIO *tmpmem;
  401. int ret = 0, i;
  402. char *buf = NULL;
  403. if (p7 == NULL) {
  404. ERR_raise(ERR_LIB_PKCS7, PKCS7_R_INVALID_NULL_POINTER);
  405. return 0;
  406. }
  407. if (!PKCS7_type_is_enveloped(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. }