pk7_smime.c 16 KB

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