pk7_smime.c 13 KB

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