Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

ssl_cert.c 28 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
  114. #include <stdio.h>
  115. #include <openssl/bio.h>
  116. #include <openssl/bn.h>
  117. #include <openssl/buf.h>
  118. #include <openssl/dh.h>
  119. #include <openssl/err.h>
  120. #include <openssl/mem.h>
  121. #include <openssl/obj.h>
  122. #include <openssl/pem.h>
  123. #include <openssl/x509v3.h>
  124. #include "../crypto/dh/internal.h"
  125. #include "../crypto/directory.h"
  126. #include "ssl_locl.h"
  127. int SSL_get_ex_data_X509_STORE_CTX_idx(void)
  128. {
  129. static volatile int ssl_x509_store_ctx_idx= -1;
  130. int got_write_lock = 0;
  131. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  132. if (ssl_x509_store_ctx_idx < 0)
  133. {
  134. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  135. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  136. got_write_lock = 1;
  137. if (ssl_x509_store_ctx_idx < 0)
  138. {
  139. ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index(
  140. 0,"SSL for verify callback",NULL,NULL,NULL);
  141. }
  142. }
  143. if (got_write_lock)
  144. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  145. else
  146. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  147. return ssl_x509_store_ctx_idx;
  148. }
  149. void ssl_cert_set_default_md(CERT *cert)
  150. {
  151. /* Set digest values to defaults */
  152. cert->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
  153. cert->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
  154. cert->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
  155. }
  156. CERT *ssl_cert_new(void)
  157. {
  158. CERT *ret;
  159. ret=(CERT *)OPENSSL_malloc(sizeof(CERT));
  160. if (ret == NULL)
  161. {
  162. OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
  163. return(NULL);
  164. }
  165. memset(ret,0,sizeof(CERT));
  166. ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]);
  167. ssl_cert_set_default_md(ret);
  168. return(ret);
  169. }
  170. CERT *ssl_cert_dup(CERT *cert)
  171. {
  172. CERT *ret;
  173. int i;
  174. ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
  175. if (ret == NULL)
  176. {
  177. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
  178. return(NULL);
  179. }
  180. memset(ret, 0, sizeof(CERT));
  181. ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
  182. /* or ret->key = ret->pkeys + (cert->key - cert->pkeys),
  183. * if you find that more readable */
  184. ret->valid = cert->valid;
  185. ret->mask_k = cert->mask_k;
  186. ret->mask_a = cert->mask_a;
  187. if (cert->dh_tmp != NULL)
  188. {
  189. ret->dh_tmp = DHparams_dup(cert->dh_tmp);
  190. if (ret->dh_tmp == NULL)
  191. {
  192. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_DH_LIB);
  193. goto err;
  194. }
  195. if (cert->dh_tmp->priv_key)
  196. {
  197. BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
  198. if (!b)
  199. {
  200. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
  201. goto err;
  202. }
  203. ret->dh_tmp->priv_key = b;
  204. }
  205. if (cert->dh_tmp->pub_key)
  206. {
  207. BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
  208. if (!b)
  209. {
  210. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
  211. goto err;
  212. }
  213. ret->dh_tmp->pub_key = b;
  214. }
  215. }
  216. ret->dh_tmp_cb = cert->dh_tmp_cb;
  217. if (cert->ecdh_tmp)
  218. {
  219. ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp);
  220. if (ret->ecdh_tmp == NULL)
  221. {
  222. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_EC_LIB);
  223. goto err;
  224. }
  225. }
  226. ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
  227. ret->ecdh_tmp_auto = cert->ecdh_tmp_auto;
  228. for (i = 0; i < SSL_PKEY_NUM; i++)
  229. {
  230. CERT_PKEY *cpk = cert->pkeys + i;
  231. CERT_PKEY *rpk = ret->pkeys + i;
  232. if (cpk->x509 != NULL)
  233. {
  234. rpk->x509 = X509_up_ref(cpk->x509);
  235. }
  236. if (cpk->privatekey != NULL)
  237. {
  238. rpk->privatekey = cpk->privatekey;
  239. CRYPTO_add(&cpk->privatekey->references, 1,
  240. CRYPTO_LOCK_EVP_PKEY);
  241. switch(i)
  242. {
  243. /* If there was anything special to do for
  244. * certain types of keys, we'd do it here.
  245. * (Nothing at the moment, I think.) */
  246. case SSL_PKEY_RSA_ENC:
  247. case SSL_PKEY_RSA_SIGN:
  248. /* We have an RSA key. */
  249. break;
  250. case SSL_PKEY_ECC:
  251. /* We have an ECC key */
  252. break;
  253. default:
  254. /* Can't happen. */
  255. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, SSL_R_LIBRARY_BUG);
  256. }
  257. }
  258. if (cpk->chain)
  259. {
  260. rpk->chain = X509_chain_up_ref(cpk->chain);
  261. if (!rpk->chain)
  262. {
  263. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
  264. goto err;
  265. }
  266. }
  267. rpk->valid_flags = 0;
  268. }
  269. /* Set digests to defaults. NB: we don't copy existing values as they
  270. * will be set during handshake.
  271. */
  272. ssl_cert_set_default_md(ret);
  273. /* Peer sigalgs set to NULL as we get these from handshake too */
  274. ret->peer_sigalgs = NULL;
  275. ret->peer_sigalgslen = 0;
  276. /* Configured sigalgs however we copy across */
  277. if (cert->conf_sigalgs)
  278. {
  279. ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen);
  280. if (!ret->conf_sigalgs)
  281. goto err;
  282. memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
  283. cert->conf_sigalgslen);
  284. ret->conf_sigalgslen = cert->conf_sigalgslen;
  285. }
  286. else
  287. ret->conf_sigalgs = NULL;
  288. if (cert->client_sigalgs)
  289. {
  290. ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen);
  291. if (!ret->client_sigalgs)
  292. goto err;
  293. memcpy(ret->client_sigalgs, cert->client_sigalgs,
  294. cert->client_sigalgslen);
  295. ret->client_sigalgslen = cert->client_sigalgslen;
  296. }
  297. else
  298. ret->client_sigalgs = NULL;
  299. /* Shared sigalgs also NULL */
  300. ret->shared_sigalgs = NULL;
  301. /* Copy any custom client certificate types */
  302. if (cert->client_certificate_types)
  303. {
  304. ret->client_certificate_types = BUF_memdup(
  305. cert->client_certificate_types,
  306. cert->num_client_certificate_types);
  307. if (!ret->client_certificate_types)
  308. goto err;
  309. ret->num_client_certificate_types = cert->num_client_certificate_types;
  310. }
  311. ret->cert_flags = cert->cert_flags;
  312. ret->cert_cb = cert->cert_cb;
  313. ret->cert_cb_arg = cert->cert_cb_arg;
  314. if (cert->verify_store)
  315. {
  316. CRYPTO_add(&cert->verify_store->references, 1, CRYPTO_LOCK_X509_STORE);
  317. ret->verify_store = cert->verify_store;
  318. }
  319. if (cert->chain_store)
  320. {
  321. CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
  322. ret->chain_store = cert->chain_store;
  323. }
  324. ret->ciphers_raw = NULL;
  325. return(ret);
  326. err:
  327. ssl_cert_free(ret);
  328. return NULL;
  329. }
  330. /* Free up and clear all certificates and chains */
  331. void ssl_cert_clear_certs(CERT *c)
  332. {
  333. int i;
  334. if (c == NULL)
  335. return;
  336. for (i = 0; i<SSL_PKEY_NUM; i++)
  337. {
  338. CERT_PKEY *cpk = c->pkeys + i;
  339. if (cpk->x509)
  340. {
  341. X509_free(cpk->x509);
  342. cpk->x509 = NULL;
  343. }
  344. if (cpk->privatekey)
  345. {
  346. EVP_PKEY_free(cpk->privatekey);
  347. cpk->privatekey = NULL;
  348. }
  349. if (cpk->chain)
  350. {
  351. sk_X509_pop_free(cpk->chain, X509_free);
  352. cpk->chain = NULL;
  353. }
  354. /* Clear all flags apart from explicit sign */
  355. cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
  356. }
  357. }
  358. void ssl_cert_free(CERT *c)
  359. {
  360. if(c == NULL)
  361. return;
  362. if (c->dh_tmp) DH_free(c->dh_tmp);
  363. if (c->ecdh_tmp) EC_KEY_free(c->ecdh_tmp);
  364. ssl_cert_clear_certs(c);
  365. if (c->peer_sigalgs)
  366. OPENSSL_free(c->peer_sigalgs);
  367. if (c->conf_sigalgs)
  368. OPENSSL_free(c->conf_sigalgs);
  369. if (c->client_sigalgs)
  370. OPENSSL_free(c->client_sigalgs);
  371. if (c->shared_sigalgs)
  372. OPENSSL_free(c->shared_sigalgs);
  373. if (c->client_certificate_types)
  374. OPENSSL_free(c->client_certificate_types);
  375. if (c->verify_store)
  376. X509_STORE_free(c->verify_store);
  377. if (c->chain_store)
  378. X509_STORE_free(c->chain_store);
  379. if (c->ciphers_raw)
  380. OPENSSL_free(c->ciphers_raw);
  381. OPENSSL_free(c);
  382. }
  383. int ssl_cert_inst(CERT **o)
  384. {
  385. /* Create a CERT if there isn't already one
  386. * (which cannot really happen, as it is initially created in
  387. * SSL_CTX_new; but the earlier code usually allows for that one
  388. * being non-existant, so we follow that behaviour, as it might
  389. * turn out that there actually is a reason for it -- but I'm
  390. * not sure that *all* of the existing code could cope with
  391. * s->cert being NULL, otherwise we could do without the
  392. * initialization in SSL_CTX_new).
  393. */
  394. if (o == NULL)
  395. {
  396. OPENSSL_PUT_ERROR(SSL, ssl_cert_inst, ERR_R_PASSED_NULL_PARAMETER);
  397. return(0);
  398. }
  399. if (*o == NULL)
  400. {
  401. if ((*o = ssl_cert_new()) == NULL)
  402. {
  403. OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
  404. return(0);
  405. }
  406. }
  407. return(1);
  408. }
  409. int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) *chain)
  410. {
  411. CERT_PKEY *cpk = c->key;
  412. if (!cpk)
  413. return 0;
  414. if (cpk->chain)
  415. sk_X509_pop_free(cpk->chain, X509_free);
  416. cpk->chain = chain;
  417. return 1;
  418. }
  419. int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) *chain)
  420. {
  421. STACK_OF(X509) *dchain;
  422. if (!chain)
  423. return ssl_cert_set0_chain(c, NULL);
  424. dchain = X509_chain_up_ref(chain);
  425. if (!dchain)
  426. return 0;
  427. if (!ssl_cert_set0_chain(c, dchain))
  428. {
  429. sk_X509_pop_free(dchain, X509_free);
  430. return 0;
  431. }
  432. return 1;
  433. }
  434. int ssl_cert_add0_chain_cert(CERT *c, X509 *x)
  435. {
  436. CERT_PKEY *cpk = c->key;
  437. if (!cpk)
  438. return 0;
  439. if (!cpk->chain)
  440. cpk->chain = sk_X509_new_null();
  441. if (!cpk->chain || !sk_X509_push(cpk->chain, x))
  442. return 0;
  443. return 1;
  444. }
  445. int ssl_cert_add1_chain_cert(CERT *c, X509 *x)
  446. {
  447. if (!ssl_cert_add0_chain_cert(c, x))
  448. return 0;
  449. X509_up_ref(x);
  450. return 1;
  451. }
  452. int ssl_cert_select_current(CERT *c, X509 *x)
  453. {
  454. int i;
  455. if (x == NULL)
  456. return 0;
  457. for (i = 0; i < SSL_PKEY_NUM; i++)
  458. {
  459. if (c->pkeys[i].x509 == x)
  460. {
  461. c->key = &c->pkeys[i];
  462. return 1;
  463. }
  464. }
  465. for (i = 0; i < SSL_PKEY_NUM; i++)
  466. {
  467. if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x))
  468. {
  469. c->key = &c->pkeys[i];
  470. return 1;
  471. }
  472. }
  473. return 0;
  474. }
  475. void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg)
  476. {
  477. c->cert_cb = cb;
  478. c->cert_cb_arg = arg;
  479. }
  480. SESS_CERT *ssl_sess_cert_new(void)
  481. {
  482. SESS_CERT *ret;
  483. ret = OPENSSL_malloc(sizeof *ret);
  484. if (ret == NULL)
  485. {
  486. OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE);
  487. return NULL;
  488. }
  489. memset(ret, 0 ,sizeof *ret);
  490. ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
  491. return ret;
  492. }
  493. void ssl_sess_cert_free(SESS_CERT *sc)
  494. {
  495. int i;
  496. if (sc == NULL)
  497. return;
  498. if (sc->cert_chain != NULL)
  499. sk_X509_pop_free(sc->cert_chain, X509_free);
  500. for (i = 0; i < SSL_PKEY_NUM; i++)
  501. {
  502. if (sc->peer_pkeys[i].x509 != NULL)
  503. X509_free(sc->peer_pkeys[i].x509);
  504. #if 0 /* We don't have the peer's private key. These lines are just
  505. * here as a reminder that we're still using a not-quite-appropriate
  506. * data structure. */
  507. if (sc->peer_pkeys[i].privatekey != NULL)
  508. EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
  509. #endif
  510. }
  511. if (sc->peer_dh_tmp != NULL)
  512. DH_free(sc->peer_dh_tmp);
  513. if (sc->peer_ecdh_tmp != NULL)
  514. EC_KEY_free(sc->peer_ecdh_tmp);
  515. OPENSSL_free(sc);
  516. }
  517. int ssl_set_peer_cert_type(SESS_CERT *sc,int type)
  518. {
  519. sc->peer_cert_type = type;
  520. return(1);
  521. }
  522. int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk)
  523. {
  524. X509 *x;
  525. int i;
  526. X509_STORE *verify_store;
  527. X509_STORE_CTX ctx;
  528. if (s->cert->verify_store)
  529. verify_store = s->cert->verify_store;
  530. else
  531. verify_store = s->ctx->cert_store;
  532. if ((sk == NULL) || (sk_X509_num(sk) == 0))
  533. return(0);
  534. x=sk_X509_value(sk,0);
  535. if(!X509_STORE_CTX_init(&ctx,verify_store,x,sk))
  536. {
  537. OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB);
  538. return(0);
  539. }
  540. #if 0
  541. if (SSL_get_verify_depth(s) >= 0)
  542. X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
  543. #endif
  544. X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s);
  545. /* We need to inherit the verify parameters. These can be determined by
  546. * the context: if its a server it will verify SSL client certificates
  547. * or vice versa.
  548. */
  549. X509_STORE_CTX_set_default(&ctx,
  550. s->server ? "ssl_client" : "ssl_server");
  551. /* Anything non-default in "param" should overwrite anything in the
  552. * ctx.
  553. */
  554. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
  555. if (s->verify_callback)
  556. X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
  557. if (s->ctx->app_verify_callback != NULL)
  558. #if 1 /* new with OpenSSL 0.9.7 */
  559. i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
  560. #else
  561. i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
  562. #endif
  563. else
  564. {
  565. #ifndef OPENSSL_NO_X509_VERIFY
  566. i=X509_verify_cert(&ctx);
  567. #else
  568. i=0;
  569. ctx.error=X509_V_ERR_APPLICATION_VERIFICATION;
  570. OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, SSL_R_NO_VERIFY_CALLBACK);
  571. #endif
  572. }
  573. s->verify_result=ctx.error;
  574. X509_STORE_CTX_cleanup(&ctx);
  575. return(i);
  576. }
  577. static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *name_list)
  578. {
  579. if (*ca_list != NULL)
  580. sk_X509_NAME_pop_free(*ca_list,X509_NAME_free);
  581. *ca_list=name_list;
  582. }
  583. STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
  584. {
  585. size_t i;
  586. STACK_OF(X509_NAME) *ret;
  587. X509_NAME *name;
  588. ret=sk_X509_NAME_new_null();
  589. for (i=0; i<sk_X509_NAME_num(sk); i++)
  590. {
  591. name=X509_NAME_dup(sk_X509_NAME_value(sk,i));
  592. if ((name == NULL) || !sk_X509_NAME_push(ret,name))
  593. {
  594. sk_X509_NAME_pop_free(ret,X509_NAME_free);
  595. return(NULL);
  596. }
  597. }
  598. return(ret);
  599. }
  600. void SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *name_list)
  601. {
  602. set_client_CA_list(&(s->client_CA),name_list);
  603. }
  604. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *name_list)
  605. {
  606. set_client_CA_list(&(ctx->client_CA),name_list);
  607. }
  608. STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
  609. {
  610. return(ctx->client_CA);
  611. }
  612. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
  613. {
  614. if (s->type == SSL_ST_CONNECT)
  615. { /* we are in the client */
  616. if (((s->version>>8) == SSL3_VERSION_MAJOR) &&
  617. (s->s3 != NULL))
  618. return(s->s3->tmp.ca_names);
  619. else
  620. return(NULL);
  621. }
  622. else
  623. {
  624. if (s->client_CA != NULL)
  625. return(s->client_CA);
  626. else
  627. return(s->ctx->client_CA);
  628. }
  629. }
  630. static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x)
  631. {
  632. X509_NAME *name;
  633. if (x == NULL) return(0);
  634. if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL))
  635. return(0);
  636. if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL)
  637. return(0);
  638. if (!sk_X509_NAME_push(*sk,name))
  639. {
  640. X509_NAME_free(name);
  641. return(0);
  642. }
  643. return(1);
  644. }
  645. int SSL_add_client_CA(SSL *ssl,X509 *x)
  646. {
  647. return(add_client_CA(&(ssl->client_CA),x));
  648. }
  649. int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x)
  650. {
  651. return(add_client_CA(&(ctx->client_CA),x));
  652. }
  653. static int xname_cmp(const X509_NAME **a, const X509_NAME **b)
  654. {
  655. return(X509_NAME_cmp(*a,*b));
  656. }
  657. #ifndef OPENSSL_NO_STDIO
  658. /*!
  659. * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
  660. * it doesn't really have anything to do with clients (except that a common use
  661. * for a stack of CAs is to send it to the client). Actually, it doesn't have
  662. * much to do with CAs, either, since it will load any old cert.
  663. * \param file the file containing one or more certs.
  664. * \return a ::STACK containing the certs.
  665. */
  666. STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
  667. {
  668. BIO *in;
  669. X509 *x=NULL;
  670. X509_NAME *xn=NULL;
  671. STACK_OF(X509_NAME) *ret = NULL,*sk;
  672. sk=sk_X509_NAME_new(xname_cmp);
  673. in=BIO_new(BIO_s_file());
  674. if ((sk == NULL) || (in == NULL))
  675. {
  676. OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
  677. goto err;
  678. }
  679. if (!BIO_read_filename(in,file))
  680. goto err;
  681. for (;;)
  682. {
  683. if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
  684. break;
  685. if (ret == NULL)
  686. {
  687. ret = sk_X509_NAME_new_null();
  688. if (ret == NULL)
  689. {
  690. OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
  691. goto err;
  692. }
  693. }
  694. if ((xn=X509_get_subject_name(x)) == NULL) goto err;
  695. /* check for duplicates */
  696. xn=X509_NAME_dup(xn);
  697. if (xn == NULL) goto err;
  698. if (sk_X509_NAME_find(sk, NULL, xn))
  699. X509_NAME_free(xn);
  700. else
  701. {
  702. sk_X509_NAME_push(sk,xn);
  703. sk_X509_NAME_push(ret,xn);
  704. }
  705. }
  706. if (0)
  707. {
  708. err:
  709. if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free);
  710. ret=NULL;
  711. }
  712. if (sk != NULL) sk_X509_NAME_free(sk);
  713. if (in != NULL) BIO_free(in);
  714. if (x != NULL) X509_free(x);
  715. if (ret != NULL)
  716. ERR_clear_error();
  717. return(ret);
  718. }
  719. #endif
  720. /*!
  721. * Add a file of certs to a stack.
  722. * \param stack the stack to add to.
  723. * \param file the file to add from. All certs in this file that are not
  724. * already in the stack will be added.
  725. * \return 1 for success, 0 for failure. Note that in the case of failure some
  726. * certs may have been added to \c stack.
  727. */
  728. int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  729. const char *file)
  730. {
  731. BIO *in;
  732. X509 *x=NULL;
  733. X509_NAME *xn=NULL;
  734. int ret=1;
  735. int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
  736. oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp);
  737. in=BIO_new(BIO_s_file());
  738. if (in == NULL)
  739. {
  740. OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, ERR_R_MALLOC_FAILURE);
  741. goto err;
  742. }
  743. if (!BIO_read_filename(in,file))
  744. goto err;
  745. for (;;)
  746. {
  747. if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
  748. break;
  749. if ((xn=X509_get_subject_name(x)) == NULL) goto err;
  750. xn=X509_NAME_dup(xn);
  751. if (xn == NULL) goto err;
  752. if (sk_X509_NAME_find(stack, NULL, xn))
  753. X509_NAME_free(xn);
  754. else
  755. sk_X509_NAME_push(stack,xn);
  756. }
  757. ERR_clear_error();
  758. if (0)
  759. {
  760. err:
  761. ret=0;
  762. }
  763. if(in != NULL)
  764. BIO_free(in);
  765. if(x != NULL)
  766. X509_free(x);
  767. (void)sk_X509_NAME_set_cmp_func(stack,oldcmp);
  768. return ret;
  769. }
  770. /*!
  771. * Add a directory of certs to a stack.
  772. * \param stack the stack to append to.
  773. * \param dir the directory to append from. All files in this directory will be
  774. * examined as potential certs. Any that are acceptable to
  775. * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
  776. * included.
  777. * \return 1 for success, 0 for failure. Note that in the case of failure some
  778. * certs may have been added to \c stack.
  779. */
  780. int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  781. const char *dir)
  782. {
  783. OPENSSL_DIR_CTX *d = NULL;
  784. const char *filename;
  785. int ret = 0;
  786. CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
  787. /* Note that a side effect is that the CAs will be sorted by name */
  788. while((filename = OPENSSL_DIR_read(&d, dir)))
  789. {
  790. char buf[1024];
  791. int r;
  792. if(strlen(dir)+strlen(filename)+2 > sizeof buf)
  793. {
  794. OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, SSL_R_PATH_TOO_LONG);
  795. goto err;
  796. }
  797. #ifdef OPENSSL_SYS_VMS
  798. r = BIO_snprintf(buf,sizeof buf,"%s%s",dir,filename);
  799. #else
  800. r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,filename);
  801. #endif
  802. if (r <= 0 || r >= (int)sizeof(buf))
  803. goto err;
  804. if(!SSL_add_file_cert_subjects_to_stack(stack,buf))
  805. goto err;
  806. }
  807. if (errno)
  808. {
  809. OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack, ERR_R_SYS_LIB);
  810. ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
  811. goto err;
  812. }
  813. ret = 1;
  814. err:
  815. if (d) OPENSSL_DIR_end(&d);
  816. CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
  817. return ret;
  818. }
  819. /* Add a certificate to a BUF_MEM structure */
  820. static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
  821. {
  822. int n;
  823. unsigned char *p;
  824. n=i2d_X509(x,NULL);
  825. if (!BUF_MEM_grow_clean(buf,(int)(n+(*l)+3)))
  826. {
  827. OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB);
  828. return 0;
  829. }
  830. p=(unsigned char *)&(buf->data[*l]);
  831. l2n3(n,p);
  832. i2d_X509(x,&p);
  833. *l+=n+3;
  834. return 1;
  835. }
  836. /* Add certificate chain to internal SSL BUF_MEM strcuture */
  837. int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
  838. {
  839. BUF_MEM *buf = s->init_buf;
  840. int no_chain;
  841. size_t i;
  842. X509 *x;
  843. STACK_OF(X509) *extra_certs;
  844. X509_STORE *chain_store;
  845. if (cpk)
  846. x = cpk->x509;
  847. else
  848. x = NULL;
  849. if (s->cert->chain_store)
  850. chain_store = s->cert->chain_store;
  851. else
  852. chain_store = s->ctx->cert_store;
  853. /* If we have a certificate specific chain use it, else use
  854. * parent ctx.
  855. */
  856. if (cpk && cpk->chain)
  857. extra_certs = cpk->chain;
  858. else
  859. extra_certs = s->ctx->extra_certs;
  860. if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
  861. no_chain = 1;
  862. else
  863. no_chain = 0;
  864. /* TLSv1 sends a chain with nothing in it, instead of an alert */
  865. if (!BUF_MEM_grow_clean(buf,10))
  866. {
  867. OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_BUF_LIB);
  868. return 0;
  869. }
  870. if (x != NULL)
  871. {
  872. if (no_chain)
  873. {
  874. if (!ssl_add_cert_to_buf(buf, l, x))
  875. return 0;
  876. }
  877. else
  878. {
  879. X509_STORE_CTX xs_ctx;
  880. if (!X509_STORE_CTX_init(&xs_ctx,chain_store,x,NULL))
  881. {
  882. OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB);
  883. return(0);
  884. }
  885. X509_verify_cert(&xs_ctx);
  886. /* Don't leave errors in the queue */
  887. ERR_clear_error();
  888. for (i=0; i < sk_X509_num(xs_ctx.chain); i++)
  889. {
  890. x = sk_X509_value(xs_ctx.chain, i);
  891. if (!ssl_add_cert_to_buf(buf, l, x))
  892. {
  893. X509_STORE_CTX_cleanup(&xs_ctx);
  894. return 0;
  895. }
  896. }
  897. X509_STORE_CTX_cleanup(&xs_ctx);
  898. }
  899. }
  900. for (i=0; i<sk_X509_num(extra_certs); i++)
  901. {
  902. x=sk_X509_value(extra_certs,i);
  903. if (!ssl_add_cert_to_buf(buf, l, x))
  904. return 0;
  905. }
  906. return 1;
  907. }
  908. /* Build a certificate chain for current certificate */
  909. int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
  910. {
  911. CERT_PKEY *cpk = c->key;
  912. X509_STORE_CTX xs_ctx;
  913. STACK_OF(X509) *chain = NULL, *untrusted = NULL;
  914. X509 *x;
  915. int i, rv = 0;
  916. unsigned long error;
  917. if (!cpk->x509)
  918. {
  919. OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET);
  920. goto err;
  921. }
  922. /* Rearranging and check the chain: add everything to a store */
  923. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
  924. {
  925. size_t j;
  926. chain_store = X509_STORE_new();
  927. if (!chain_store)
  928. goto err;
  929. for (j = 0; j < sk_X509_num(cpk->chain); j++)
  930. {
  931. x = sk_X509_value(cpk->chain, j);
  932. if (!X509_STORE_add_cert(chain_store, x))
  933. {
  934. error = ERR_peek_last_error();
  935. if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
  936. ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
  937. goto err;
  938. ERR_clear_error();
  939. }
  940. }
  941. /* Add EE cert too: it might be self signed */
  942. if (!X509_STORE_add_cert(chain_store, cpk->x509))
  943. {
  944. error = ERR_peek_last_error();
  945. if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
  946. ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
  947. goto err;
  948. ERR_clear_error();
  949. }
  950. }
  951. else
  952. {
  953. if (c->chain_store)
  954. chain_store = c->chain_store;
  955. if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
  956. untrusted = cpk->chain;
  957. }
  958. if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted))
  959. {
  960. OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB);
  961. goto err;
  962. }
  963. i = X509_verify_cert(&xs_ctx);
  964. if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)
  965. {
  966. if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
  967. ERR_clear_error();
  968. i = 1;
  969. rv = 2;
  970. }
  971. if (i > 0)
  972. chain = X509_STORE_CTX_get1_chain(&xs_ctx);
  973. if (i <= 0)
  974. {
  975. OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_CERTIFICATE_VERIFY_FAILED);
  976. i = X509_STORE_CTX_get_error(&xs_ctx);
  977. ERR_add_error_data(2, "Verify error:",
  978. X509_verify_cert_error_string(i));
  979. X509_STORE_CTX_cleanup(&xs_ctx);
  980. goto err;
  981. }
  982. X509_STORE_CTX_cleanup(&xs_ctx);
  983. if (cpk->chain)
  984. sk_X509_pop_free(cpk->chain, X509_free);
  985. /* Remove EE certificate from chain */
  986. x = sk_X509_shift(chain);
  987. X509_free(x);
  988. if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT)
  989. {
  990. if (sk_X509_num(chain) > 0)
  991. {
  992. /* See if last cert is self signed */
  993. x = sk_X509_value(chain, sk_X509_num(chain) - 1);
  994. X509_check_purpose(x, -1, 0);
  995. if (x->ex_flags & EXFLAG_SS)
  996. {
  997. x = sk_X509_pop(chain);
  998. X509_free(x);
  999. }
  1000. }
  1001. }
  1002. cpk->chain = chain;
  1003. if (rv == 0)
  1004. rv = 1;
  1005. err:
  1006. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
  1007. X509_STORE_free(chain_store);
  1008. return rv;
  1009. }
  1010. int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
  1011. {
  1012. X509_STORE **pstore;
  1013. if (chain)
  1014. pstore = &c->chain_store;
  1015. else
  1016. pstore = &c->verify_store;
  1017. if (*pstore)
  1018. X509_STORE_free(*pstore);
  1019. *pstore = store;
  1020. if (ref && store)
  1021. CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
  1022. return 1;
  1023. }