pk7_smime.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /* pk7_smime.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  4. * project.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* Simple PKCS#7 processing functions */
  60. #include <stdio.h>
  61. #include "cryptlib.h"
  62. #include <openssl/x509.h>
  63. #include <openssl/x509v3.h>
  64. PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
  65. BIO *data, int flags)
  66. {
  67. PKCS7 *p7 = NULL;
  68. PKCS7_SIGNER_INFO *si;
  69. BIO *p7bio = NULL;
  70. STACK_OF(X509_ALGOR) *smcap = NULL;
  71. int i;
  72. if (!X509_check_private_key(signcert, pkey)) {
  73. PKCS7err(PKCS7_F_PKCS7_SIGN,
  74. PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  75. return NULL;
  76. }
  77. if (!(p7 = PKCS7_new())) {
  78. PKCS7err(PKCS7_F_PKCS7_SIGN, ERR_R_MALLOC_FAILURE);
  79. return NULL;
  80. }
  81. if (!PKCS7_set_type(p7, NID_pkcs7_signed))
  82. goto err;
  83. if (!PKCS7_content_new(p7, NID_pkcs7_data))
  84. goto err;
  85. if (!(si = PKCS7_add_signature(p7, signcert, pkey, EVP_sha1()))) {
  86. PKCS7err(PKCS7_F_PKCS7_SIGN, PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR);
  87. goto err;
  88. }
  89. if (!(flags & PKCS7_NOCERTS)) {
  90. if (!PKCS7_add_certificate(p7, signcert))
  91. goto err;
  92. if (certs)
  93. for (i = 0; i < sk_X509_num(certs); i++)
  94. if (!PKCS7_add_certificate(p7, sk_X509_value(certs, i)))
  95. goto err;
  96. }
  97. if (!(flags & PKCS7_NOATTR)) {
  98. if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
  99. V_ASN1_OBJECT,
  100. OBJ_nid2obj(NID_pkcs7_data)))
  101. goto err;
  102. /* Add SMIMECapabilities */
  103. if (!(flags & PKCS7_NOSMIMECAP)) {
  104. if (!(smcap = sk_X509_ALGOR_new_null())) {
  105. PKCS7err(PKCS7_F_PKCS7_SIGN, ERR_R_MALLOC_FAILURE);
  106. goto err;
  107. }
  108. #ifndef OPENSSL_NO_DES
  109. if (!PKCS7_simple_smimecap(smcap, NID_des_ede3_cbc, -1))
  110. goto err;
  111. #endif
  112. #ifndef OPENSSL_NO_RC2
  113. if (!PKCS7_simple_smimecap(smcap, NID_rc2_cbc, 128))
  114. goto err;
  115. if (!PKCS7_simple_smimecap(smcap, NID_rc2_cbc, 64))
  116. goto err;
  117. #endif
  118. #ifndef OPENSSL_NO_DES
  119. if (!PKCS7_simple_smimecap(smcap, NID_des_cbc, -1))
  120. goto err;
  121. #endif
  122. #ifndef OPENSSL_NO_RC2
  123. if (!PKCS7_simple_smimecap(smcap, NID_rc2_cbc, 40))
  124. goto err;
  125. #endif
  126. if (!PKCS7_add_attrib_smimecap(si, smcap))
  127. goto err;
  128. sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
  129. smcap = NULL;
  130. }
  131. }
  132. if (flags & PKCS7_DETACHED)
  133. PKCS7_set_detached(p7, 1);
  134. if (flags & PKCS7_STREAM)
  135. return p7;
  136. if (!(p7bio = PKCS7_dataInit(p7, NULL))) {
  137. PKCS7err(PKCS7_F_PKCS7_SIGN, ERR_R_MALLOC_FAILURE);
  138. goto err;
  139. }
  140. SMIME_crlf_copy(data, p7bio, flags);
  141. if (!PKCS7_dataFinal(p7, p7bio)) {
  142. PKCS7err(PKCS7_F_PKCS7_SIGN, PKCS7_R_PKCS7_DATASIGN);
  143. goto err;
  144. }
  145. BIO_free_all(p7bio);
  146. return p7;
  147. err:
  148. sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
  149. BIO_free_all(p7bio);
  150. PKCS7_free(p7);
  151. return NULL;
  152. }
  153. int PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
  154. BIO *indata, BIO *out, int flags)
  155. {
  156. STACK_OF(X509) *signers;
  157. X509 *signer;
  158. STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
  159. PKCS7_SIGNER_INFO *si;
  160. X509_STORE_CTX cert_ctx;
  161. char buf[4096];
  162. int i, j = 0, k, ret = 0;
  163. BIO *p7bio;
  164. BIO *tmpin, *tmpout;
  165. if (!p7) {
  166. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_INVALID_NULL_POINTER);
  167. return 0;
  168. }
  169. if (!PKCS7_type_is_signed(p7)) {
  170. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_WRONG_CONTENT_TYPE);
  171. return 0;
  172. }
  173. /* Check for no data and no content: no data to verify signature */
  174. if (PKCS7_get_detached(p7) && !indata) {
  175. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_CONTENT);
  176. return 0;
  177. }
  178. #if 0
  179. /*
  180. * NB: this test commented out because some versions of Netscape
  181. * illegally include zero length content when signing data.
  182. */
  183. /* Check for data and content: two sets of data */
  184. if (!PKCS7_get_detached(p7) && indata) {
  185. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_CONTENT_AND_DATA_PRESENT);
  186. return 0;
  187. }
  188. #endif
  189. sinfos = PKCS7_get_signer_info(p7);
  190. if (!sinfos || !sk_PKCS7_SIGNER_INFO_num(sinfos)) {
  191. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_NO_SIGNATURES_ON_DATA);
  192. return 0;
  193. }
  194. signers = PKCS7_get0_signers(p7, certs, flags);
  195. if (!signers)
  196. return 0;
  197. /* Now verify the certificates */
  198. if (!(flags & PKCS7_NOVERIFY))
  199. for (k = 0; k < sk_X509_num(signers); k++) {
  200. signer = sk_X509_value(signers, k);
  201. if (!(flags & PKCS7_NOCHAIN)) {
  202. if (!X509_STORE_CTX_init(&cert_ctx, store, signer,
  203. p7->d.sign->cert)) {
  204. PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB);
  205. sk_X509_free(signers);
  206. return 0;
  207. }
  208. X509_STORE_CTX_set_default(&cert_ctx, "smime_sign");
  209. } else if (!X509_STORE_CTX_init(&cert_ctx, store, signer, NULL)) {
  210. PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_X509_LIB);
  211. sk_X509_free(signers);
  212. return 0;
  213. }
  214. if (!(flags & PKCS7_NOCRL))
  215. X509_STORE_CTX_set0_crls(&cert_ctx, p7->d.sign->crl);
  216. i = X509_verify_cert(&cert_ctx);
  217. if (i <= 0)
  218. j = X509_STORE_CTX_get_error(&cert_ctx);
  219. X509_STORE_CTX_cleanup(&cert_ctx);
  220. if (i <= 0) {
  221. PKCS7err(PKCS7_F_PKCS7_VERIFY,
  222. PKCS7_R_CERTIFICATE_VERIFY_ERROR);
  223. ERR_add_error_data(2, "Verify error:",
  224. X509_verify_cert_error_string(j));
  225. sk_X509_free(signers);
  226. return 0;
  227. }
  228. /* Check for revocation status here */
  229. }
  230. /*
  231. * Performance optimization: if the content is a memory BIO then store
  232. * its contents in a temporary read only memory BIO. This avoids
  233. * potentially large numbers of slow copies of data which will occur when
  234. * reading from a read write memory BIO when signatures are calculated.
  235. */
  236. if (indata && (BIO_method_type(indata) == BIO_TYPE_MEM)) {
  237. char *ptr;
  238. long len;
  239. len = BIO_get_mem_data(indata, &ptr);
  240. tmpin = BIO_new_mem_buf(ptr, len);
  241. if (tmpin == NULL) {
  242. PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE);
  243. return 0;
  244. }
  245. } else
  246. tmpin = indata;
  247. if (!(p7bio = PKCS7_dataInit(p7, tmpin)))
  248. goto err;
  249. if (flags & PKCS7_TEXT) {
  250. if (!(tmpout = BIO_new(BIO_s_mem()))) {
  251. PKCS7err(PKCS7_F_PKCS7_VERIFY, ERR_R_MALLOC_FAILURE);
  252. goto err;
  253. }
  254. BIO_set_mem_eof_return(tmpout, 0);
  255. } else
  256. tmpout = out;
  257. /* We now have to 'read' from p7bio to calculate digests etc. */
  258. for (;;) {
  259. i = BIO_read(p7bio, buf, sizeof(buf));
  260. if (i <= 0)
  261. break;
  262. if (tmpout)
  263. BIO_write(tmpout, buf, i);
  264. }
  265. if (flags & PKCS7_TEXT) {
  266. if (!SMIME_text(tmpout, out)) {
  267. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SMIME_TEXT_ERROR);
  268. BIO_free(tmpout);
  269. goto err;
  270. }
  271. BIO_free(tmpout);
  272. }
  273. /* Now Verify All Signatures */
  274. if (!(flags & PKCS7_NOSIGS))
  275. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
  276. si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
  277. signer = sk_X509_value(signers, i);
  278. j = PKCS7_signatureVerify(p7bio, p7, si, signer);
  279. if (j <= 0) {
  280. PKCS7err(PKCS7_F_PKCS7_VERIFY, PKCS7_R_SIGNATURE_FAILURE);
  281. goto err;
  282. }
  283. }
  284. ret = 1;
  285. err:
  286. if (tmpin == indata) {
  287. if (indata)
  288. BIO_pop(p7bio);
  289. }
  290. BIO_free_all(p7bio);
  291. sk_X509_free(signers);
  292. return ret;
  293. }
  294. STACK_OF(X509) *PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs,
  295. int flags)
  296. {
  297. STACK_OF(X509) *signers;
  298. STACK_OF(PKCS7_SIGNER_INFO) *sinfos;
  299. PKCS7_SIGNER_INFO *si;
  300. PKCS7_ISSUER_AND_SERIAL *ias;
  301. X509 *signer;
  302. int i;
  303. if (!p7) {
  304. PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_INVALID_NULL_POINTER);
  305. return NULL;
  306. }
  307. if (!PKCS7_type_is_signed(p7)) {
  308. PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_WRONG_CONTENT_TYPE);
  309. return NULL;
  310. }
  311. /* Collect all the signers together */
  312. sinfos = PKCS7_get_signer_info(p7);
  313. if (sk_PKCS7_SIGNER_INFO_num(sinfos) <= 0) {
  314. PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, PKCS7_R_NO_SIGNERS);
  315. return NULL;
  316. }
  317. if (!(signers = sk_X509_new_null())) {
  318. PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS, ERR_R_MALLOC_FAILURE);
  319. return NULL;
  320. }
  321. for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(sinfos); i++) {
  322. si = sk_PKCS7_SIGNER_INFO_value(sinfos, i);
  323. ias = si->issuer_and_serial;
  324. signer = NULL;
  325. /* If any certificates passed they take priority */
  326. if (certs)
  327. signer = X509_find_by_issuer_and_serial(certs,
  328. ias->issuer, ias->serial);
  329. if (!signer && !(flags & PKCS7_NOINTERN)
  330. && p7->d.sign->cert)
  331. signer =
  332. X509_find_by_issuer_and_serial(p7->d.sign->cert,
  333. ias->issuer, ias->serial);
  334. if (!signer) {
  335. PKCS7err(PKCS7_F_PKCS7_GET0_SIGNERS,
  336. PKCS7_R_SIGNER_CERTIFICATE_NOT_FOUND);
  337. sk_X509_free(signers);
  338. return NULL;
  339. }
  340. if (!sk_X509_push(signers, signer)) {
  341. sk_X509_free(signers);
  342. return NULL;
  343. }
  344. }
  345. return signers;
  346. }
  347. /* Build a complete PKCS#7 enveloped data */
  348. PKCS7 *PKCS7_encrypt(STACK_OF(X509) *certs, BIO *in, const EVP_CIPHER *cipher,
  349. int flags)
  350. {
  351. PKCS7 *p7;
  352. BIO *p7bio = NULL;
  353. int i;
  354. X509 *x509;
  355. if (!(p7 = PKCS7_new())) {
  356. PKCS7err(PKCS7_F_PKCS7_ENCRYPT, ERR_R_MALLOC_FAILURE);
  357. return NULL;
  358. }
  359. if (!PKCS7_set_type(p7, NID_pkcs7_enveloped))
  360. goto err;
  361. if (!PKCS7_set_cipher(p7, cipher)) {
  362. PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_SETTING_CIPHER);
  363. goto err;
  364. }
  365. for (i = 0; i < sk_X509_num(certs); i++) {
  366. x509 = sk_X509_value(certs, i);
  367. if (!PKCS7_add_recipient(p7, x509)) {
  368. PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_ERROR_ADDING_RECIPIENT);
  369. goto err;
  370. }
  371. }
  372. if (!(p7bio = PKCS7_dataInit(p7, NULL))) {
  373. PKCS7err(PKCS7_F_PKCS7_ENCRYPT, ERR_R_MALLOC_FAILURE);
  374. goto err;
  375. }
  376. SMIME_crlf_copy(in, p7bio, flags);
  377. (void)BIO_flush(p7bio);
  378. if (!PKCS7_dataFinal(p7, p7bio)) {
  379. PKCS7err(PKCS7_F_PKCS7_ENCRYPT, PKCS7_R_PKCS7_DATAFINAL_ERROR);
  380. goto err;
  381. }
  382. BIO_free_all(p7bio);
  383. return p7;
  384. err:
  385. BIO_free_all(p7bio);
  386. PKCS7_free(p7);
  387. return NULL;
  388. }
  389. int PKCS7_decrypt(PKCS7 *p7, EVP_PKEY *pkey, X509 *cert, BIO *data, int flags)
  390. {
  391. BIO *tmpmem;
  392. int ret, i;
  393. char buf[4096];
  394. if (!p7) {
  395. PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_INVALID_NULL_POINTER);
  396. return 0;
  397. }
  398. if (!PKCS7_type_is_enveloped(p7)) {
  399. PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_WRONG_CONTENT_TYPE);
  400. return 0;
  401. }
  402. if (cert && !X509_check_private_key(cert, pkey)) {
  403. PKCS7err(PKCS7_F_PKCS7_DECRYPT,
  404. PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
  405. return 0;
  406. }
  407. if (!(tmpmem = PKCS7_dataDecode(p7, pkey, NULL, cert))) {
  408. PKCS7err(PKCS7_F_PKCS7_DECRYPT, PKCS7_R_DECRYPT_ERROR);
  409. return 0;
  410. }
  411. if (flags & PKCS7_TEXT) {
  412. BIO *tmpbuf, *bread;
  413. /* Encrypt BIOs can't do BIO_gets() so add a buffer BIO */
  414. if (!(tmpbuf = BIO_new(BIO_f_buffer()))) {
  415. PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
  416. BIO_free_all(tmpmem);
  417. return 0;
  418. }
  419. if (!(bread = BIO_push(tmpbuf, tmpmem))) {
  420. PKCS7err(PKCS7_F_PKCS7_DECRYPT, ERR_R_MALLOC_FAILURE);
  421. BIO_free_all(tmpbuf);
  422. BIO_free_all(tmpmem);
  423. return 0;
  424. }
  425. ret = SMIME_text(bread, data);
  426. if (ret > 0 && BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
  427. if (!BIO_get_cipher_status(tmpmem))
  428. ret = 0;
  429. }
  430. BIO_free_all(bread);
  431. return ret;
  432. } else {
  433. for (;;) {
  434. i = BIO_read(tmpmem, buf, sizeof(buf));
  435. if (i <= 0) {
  436. ret = 1;
  437. if (BIO_method_type(tmpmem) == BIO_TYPE_CIPHER) {
  438. if (!BIO_get_cipher_status(tmpmem))
  439. ret = 0;
  440. }
  441. break;
  442. }
  443. if (BIO_write(data, buf, i) != i) {
  444. ret = 0;
  445. break;
  446. }
  447. }
  448. BIO_free_all(tmpmem);
  449. return ret;
  450. }
  451. }