ssl_cert.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*! \file ssl/ssl_cert.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@OpenSSL.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. */
  106. /* ====================================================================
  107. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  108. * ECC cipher suite support in OpenSSL originally developed by
  109. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  110. */
  111. #include <stdio.h>
  112. #include "e_os.h"
  113. #ifndef NO_SYS_TYPES_H
  114. # include <sys/types.h>
  115. #endif
  116. #include "o_dir.h"
  117. #include <openssl/objects.h>
  118. #include <openssl/bio.h>
  119. #include <openssl/pem.h>
  120. #include <openssl/x509v3.h>
  121. #ifndef OPENSSL_NO_DH
  122. #include <openssl/dh.h>
  123. #endif
  124. #include <openssl/bn.h>
  125. #include "ssl_locl.h"
  126. int SSL_get_ex_data_X509_STORE_CTX_idx(void)
  127. {
  128. static volatile int ssl_x509_store_ctx_idx= -1;
  129. if (ssl_x509_store_ctx_idx < 0)
  130. {
  131. /* any write lock will do; usually this branch
  132. * will only be taken once anyway */
  133. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  134. if (ssl_x509_store_ctx_idx < 0)
  135. {
  136. ssl_x509_store_ctx_idx=X509_STORE_CTX_get_ex_new_index(
  137. 0,"SSL for verify callback",NULL,NULL,NULL);
  138. }
  139. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  140. }
  141. return ssl_x509_store_ctx_idx;
  142. }
  143. CERT *ssl_cert_new(void)
  144. {
  145. CERT *ret;
  146. ret=(CERT *)OPENSSL_malloc(sizeof(CERT));
  147. if (ret == NULL)
  148. {
  149. SSLerr(SSL_F_SSL_CERT_NEW,ERR_R_MALLOC_FAILURE);
  150. return(NULL);
  151. }
  152. memset(ret,0,sizeof(CERT));
  153. ret->key= &(ret->pkeys[SSL_PKEY_RSA_ENC]);
  154. ret->references=1;
  155. return(ret);
  156. }
  157. CERT *ssl_cert_dup(CERT *cert)
  158. {
  159. CERT *ret;
  160. int i;
  161. ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
  162. if (ret == NULL)
  163. {
  164. SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
  165. return(NULL);
  166. }
  167. memset(ret, 0, sizeof(CERT));
  168. ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
  169. /* or ret->key = ret->pkeys + (cert->key - cert->pkeys),
  170. * if you find that more readable */
  171. ret->valid = cert->valid;
  172. ret->mask = cert->mask;
  173. ret->export_mask = cert->export_mask;
  174. #ifndef OPENSSL_NO_RSA
  175. if (cert->rsa_tmp != NULL)
  176. {
  177. RSA_up_ref(cert->rsa_tmp);
  178. ret->rsa_tmp = cert->rsa_tmp;
  179. }
  180. ret->rsa_tmp_cb = cert->rsa_tmp_cb;
  181. #endif
  182. #ifndef OPENSSL_NO_DH
  183. if (cert->dh_tmp != NULL)
  184. {
  185. /* DH parameters don't have a reference count */
  186. ret->dh_tmp = DHparams_dup(cert->dh_tmp);
  187. if (ret->dh_tmp == NULL)
  188. {
  189. SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB);
  190. goto err;
  191. }
  192. if (cert->dh_tmp->priv_key)
  193. {
  194. BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
  195. if (!b)
  196. {
  197. SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
  198. goto err;
  199. }
  200. ret->dh_tmp->priv_key = b;
  201. }
  202. if (cert->dh_tmp->pub_key)
  203. {
  204. BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
  205. if (!b)
  206. {
  207. SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
  208. goto err;
  209. }
  210. ret->dh_tmp->pub_key = b;
  211. }
  212. }
  213. ret->dh_tmp_cb = cert->dh_tmp_cb;
  214. #endif
  215. #ifndef OPENSSL_NO_ECDH
  216. if (cert->ecdh_tmp)
  217. {
  218. EC_KEY_up_ref(cert->ecdh_tmp);
  219. ret->ecdh_tmp = cert->ecdh_tmp;
  220. }
  221. ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
  222. #endif
  223. for (i = 0; i < SSL_PKEY_NUM; i++)
  224. {
  225. if (cert->pkeys[i].x509 != NULL)
  226. {
  227. ret->pkeys[i].x509 = cert->pkeys[i].x509;
  228. CRYPTO_add(&ret->pkeys[i].x509->references, 1,
  229. CRYPTO_LOCK_X509);
  230. }
  231. if (cert->pkeys[i].privatekey != NULL)
  232. {
  233. ret->pkeys[i].privatekey = cert->pkeys[i].privatekey;
  234. CRYPTO_add(&ret->pkeys[i].privatekey->references, 1,
  235. CRYPTO_LOCK_EVP_PKEY);
  236. switch(i)
  237. {
  238. /* If there was anything special to do for
  239. * certain types of keys, we'd do it here.
  240. * (Nothing at the moment, I think.) */
  241. case SSL_PKEY_RSA_ENC:
  242. case SSL_PKEY_RSA_SIGN:
  243. /* We have an RSA key. */
  244. break;
  245. case SSL_PKEY_DSA_SIGN:
  246. /* We have a DSA key. */
  247. break;
  248. case SSL_PKEY_DH_RSA:
  249. case SSL_PKEY_DH_DSA:
  250. /* We have a DH key. */
  251. break;
  252. case SSL_PKEY_ECC:
  253. /* We have an ECC key */
  254. break;
  255. default:
  256. /* Can't happen. */
  257. SSLerr(SSL_F_SSL_CERT_DUP, SSL_R_LIBRARY_BUG);
  258. }
  259. }
  260. }
  261. /* ret->extra_certs *should* exist, but currently the own certificate
  262. * chain is held inside SSL_CTX */
  263. ret->references=1;
  264. return(ret);
  265. #ifndef OPENSSL_NO_DH /* avoid 'unreferenced label' warning if OPENSSL_NO_DH is defined */
  266. err:
  267. #endif
  268. #ifndef OPENSSL_NO_RSA
  269. if (ret->rsa_tmp != NULL)
  270. RSA_free(ret->rsa_tmp);
  271. #endif
  272. #ifndef OPENSSL_NO_DH
  273. if (ret->dh_tmp != NULL)
  274. DH_free(ret->dh_tmp);
  275. #endif
  276. #ifndef OPENSSL_NO_ECDH
  277. if (ret->ecdh_tmp != NULL)
  278. EC_KEY_free(ret->ecdh_tmp);
  279. #endif
  280. for (i = 0; i < SSL_PKEY_NUM; i++)
  281. {
  282. if (ret->pkeys[i].x509 != NULL)
  283. X509_free(ret->pkeys[i].x509);
  284. if (ret->pkeys[i].privatekey != NULL)
  285. EVP_PKEY_free(ret->pkeys[i].privatekey);
  286. }
  287. return NULL;
  288. }
  289. void ssl_cert_free(CERT *c)
  290. {
  291. int i;
  292. if(c == NULL)
  293. return;
  294. i=CRYPTO_add(&c->references,-1,CRYPTO_LOCK_SSL_CERT);
  295. #ifdef REF_PRINT
  296. REF_PRINT("CERT",c);
  297. #endif
  298. if (i > 0) return;
  299. #ifdef REF_CHECK
  300. if (i < 0)
  301. {
  302. fprintf(stderr,"ssl_cert_free, bad reference count\n");
  303. abort(); /* ok */
  304. }
  305. #endif
  306. #ifndef OPENSSL_NO_RSA
  307. if (c->rsa_tmp) RSA_free(c->rsa_tmp);
  308. #endif
  309. #ifndef OPENSSL_NO_DH
  310. if (c->dh_tmp) DH_free(c->dh_tmp);
  311. #endif
  312. #ifndef OPENSSL_NO_ECDH
  313. if (c->ecdh_tmp) EC_KEY_free(c->ecdh_tmp);
  314. #endif
  315. for (i=0; i<SSL_PKEY_NUM; i++)
  316. {
  317. if (c->pkeys[i].x509 != NULL)
  318. X509_free(c->pkeys[i].x509);
  319. if (c->pkeys[i].privatekey != NULL)
  320. EVP_PKEY_free(c->pkeys[i].privatekey);
  321. #if 0
  322. if (c->pkeys[i].publickey != NULL)
  323. EVP_PKEY_free(c->pkeys[i].publickey);
  324. #endif
  325. }
  326. OPENSSL_free(c);
  327. }
  328. int ssl_cert_inst(CERT **o)
  329. {
  330. /* Create a CERT if there isn't already one
  331. * (which cannot really happen, as it is initially created in
  332. * SSL_CTX_new; but the earlier code usually allows for that one
  333. * being non-existant, so we follow that behaviour, as it might
  334. * turn out that there actually is a reason for it -- but I'm
  335. * not sure that *all* of the existing code could cope with
  336. * s->cert being NULL, otherwise we could do without the
  337. * initialization in SSL_CTX_new).
  338. */
  339. if (o == NULL)
  340. {
  341. SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER);
  342. return(0);
  343. }
  344. if (*o == NULL)
  345. {
  346. if ((*o = ssl_cert_new()) == NULL)
  347. {
  348. SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE);
  349. return(0);
  350. }
  351. }
  352. return(1);
  353. }
  354. SESS_CERT *ssl_sess_cert_new(void)
  355. {
  356. SESS_CERT *ret;
  357. ret = OPENSSL_malloc(sizeof *ret);
  358. if (ret == NULL)
  359. {
  360. SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE);
  361. return NULL;
  362. }
  363. memset(ret, 0 ,sizeof *ret);
  364. ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
  365. ret->references = 1;
  366. return ret;
  367. }
  368. void ssl_sess_cert_free(SESS_CERT *sc)
  369. {
  370. int i;
  371. if (sc == NULL)
  372. return;
  373. i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT);
  374. #ifdef REF_PRINT
  375. REF_PRINT("SESS_CERT", sc);
  376. #endif
  377. if (i > 0)
  378. return;
  379. #ifdef REF_CHECK
  380. if (i < 0)
  381. {
  382. fprintf(stderr,"ssl_sess_cert_free, bad reference count\n");
  383. abort(); /* ok */
  384. }
  385. #endif
  386. /* i == 0 */
  387. if (sc->cert_chain != NULL)
  388. sk_X509_pop_free(sc->cert_chain, X509_free);
  389. for (i = 0; i < SSL_PKEY_NUM; i++)
  390. {
  391. if (sc->peer_pkeys[i].x509 != NULL)
  392. X509_free(sc->peer_pkeys[i].x509);
  393. #if 0 /* We don't have the peer's private key. These lines are just
  394. * here as a reminder that we're still using a not-quite-appropriate
  395. * data structure. */
  396. if (sc->peer_pkeys[i].privatekey != NULL)
  397. EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
  398. #endif
  399. }
  400. #ifndef OPENSSL_NO_RSA
  401. if (sc->peer_rsa_tmp != NULL)
  402. RSA_free(sc->peer_rsa_tmp);
  403. #endif
  404. #ifndef OPENSSL_NO_DH
  405. if (sc->peer_dh_tmp != NULL)
  406. DH_free(sc->peer_dh_tmp);
  407. #endif
  408. #ifndef OPENSSL_NO_ECDH
  409. if (sc->peer_ecdh_tmp != NULL)
  410. EC_KEY_free(sc->peer_ecdh_tmp);
  411. #endif
  412. OPENSSL_free(sc);
  413. }
  414. int ssl_set_peer_cert_type(SESS_CERT *sc,int type)
  415. {
  416. sc->peer_cert_type = type;
  417. return(1);
  418. }
  419. int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk)
  420. {
  421. X509 *x;
  422. int i;
  423. X509_STORE_CTX ctx;
  424. if ((sk == NULL) || (sk_X509_num(sk) == 0))
  425. return(0);
  426. x=sk_X509_value(sk,0);
  427. if(!X509_STORE_CTX_init(&ctx,s->ctx->cert_store,x,sk))
  428. {
  429. SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,ERR_R_X509_LIB);
  430. return(0);
  431. }
  432. if (s->param)
  433. X509_VERIFY_PARAM_inherit(X509_STORE_CTX_get0_param(&ctx),
  434. s->param);
  435. #if 0
  436. if (SSL_get_verify_depth(s) >= 0)
  437. X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
  438. #endif
  439. X509_STORE_CTX_set_ex_data(&ctx,SSL_get_ex_data_X509_STORE_CTX_idx(),s);
  440. /* We need to inherit the verify parameters. These can be determined by
  441. * the context: if its a server it will verify SSL client certificates
  442. * or vice versa.
  443. */
  444. X509_STORE_CTX_set_default(&ctx,
  445. s->server ? "ssl_client" : "ssl_server");
  446. if (s->verify_callback)
  447. X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
  448. if (s->ctx->app_verify_callback != NULL)
  449. #if 1 /* new with OpenSSL 0.9.7 */
  450. i=s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
  451. #else
  452. i=s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
  453. #endif
  454. else
  455. {
  456. #ifndef OPENSSL_NO_X509_VERIFY
  457. i=X509_verify_cert(&ctx);
  458. #else
  459. i=0;
  460. ctx.error=X509_V_ERR_APPLICATION_VERIFICATION;
  461. SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN,SSL_R_NO_VERIFY_CALLBACK);
  462. #endif
  463. }
  464. s->verify_result=ctx.error;
  465. X509_STORE_CTX_cleanup(&ctx);
  466. return(i);
  467. }
  468. static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,STACK_OF(X509_NAME) *name_list)
  469. {
  470. if (*ca_list != NULL)
  471. sk_X509_NAME_pop_free(*ca_list,X509_NAME_free);
  472. *ca_list=name_list;
  473. }
  474. STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
  475. {
  476. int i;
  477. STACK_OF(X509_NAME) *ret;
  478. X509_NAME *name;
  479. ret=sk_X509_NAME_new_null();
  480. for (i=0; i<sk_X509_NAME_num(sk); i++)
  481. {
  482. name=X509_NAME_dup(sk_X509_NAME_value(sk,i));
  483. if ((name == NULL) || !sk_X509_NAME_push(ret,name))
  484. {
  485. sk_X509_NAME_pop_free(ret,X509_NAME_free);
  486. return(NULL);
  487. }
  488. }
  489. return(ret);
  490. }
  491. void SSL_set_client_CA_list(SSL *s,STACK_OF(X509_NAME) *name_list)
  492. {
  493. set_client_CA_list(&(s->client_CA),name_list);
  494. }
  495. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx,STACK_OF(X509_NAME) *name_list)
  496. {
  497. set_client_CA_list(&(ctx->client_CA),name_list);
  498. }
  499. STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
  500. {
  501. return(ctx->client_CA);
  502. }
  503. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
  504. {
  505. if (s->type == SSL_ST_CONNECT)
  506. { /* we are in the client */
  507. if (((s->version>>8) == SSL3_VERSION_MAJOR) &&
  508. (s->s3 != NULL))
  509. return(s->s3->tmp.ca_names);
  510. else
  511. return(NULL);
  512. }
  513. else
  514. {
  515. if (s->client_CA != NULL)
  516. return(s->client_CA);
  517. else
  518. return(s->ctx->client_CA);
  519. }
  520. }
  521. static int add_client_CA(STACK_OF(X509_NAME) **sk,X509 *x)
  522. {
  523. X509_NAME *name;
  524. if (x == NULL) return(0);
  525. if ((*sk == NULL) && ((*sk=sk_X509_NAME_new_null()) == NULL))
  526. return(0);
  527. if ((name=X509_NAME_dup(X509_get_subject_name(x))) == NULL)
  528. return(0);
  529. if (!sk_X509_NAME_push(*sk,name))
  530. {
  531. X509_NAME_free(name);
  532. return(0);
  533. }
  534. return(1);
  535. }
  536. int SSL_add_client_CA(SSL *ssl,X509 *x)
  537. {
  538. return(add_client_CA(&(ssl->client_CA),x));
  539. }
  540. int SSL_CTX_add_client_CA(SSL_CTX *ctx,X509 *x)
  541. {
  542. return(add_client_CA(&(ctx->client_CA),x));
  543. }
  544. static int xname_cmp(const X509_NAME * const *a, const X509_NAME * const *b)
  545. {
  546. return(X509_NAME_cmp(*a,*b));
  547. }
  548. #ifndef OPENSSL_NO_STDIO
  549. /*!
  550. * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
  551. * it doesn't really have anything to do with clients (except that a common use
  552. * for a stack of CAs is to send it to the client). Actually, it doesn't have
  553. * much to do with CAs, either, since it will load any old cert.
  554. * \param file the file containing one or more certs.
  555. * \return a ::STACK containing the certs.
  556. */
  557. STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
  558. {
  559. BIO *in;
  560. X509 *x=NULL;
  561. X509_NAME *xn=NULL;
  562. STACK_OF(X509_NAME) *ret = NULL,*sk;
  563. sk=sk_X509_NAME_new(xname_cmp);
  564. in=BIO_new(BIO_s_file_internal());
  565. if ((sk == NULL) || (in == NULL))
  566. {
  567. SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE);
  568. goto err;
  569. }
  570. if (!BIO_read_filename(in,file))
  571. goto err;
  572. for (;;)
  573. {
  574. if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
  575. break;
  576. if (ret == NULL)
  577. {
  578. ret = sk_X509_NAME_new_null();
  579. if (ret == NULL)
  580. {
  581. SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE,ERR_R_MALLOC_FAILURE);
  582. goto err;
  583. }
  584. }
  585. if ((xn=X509_get_subject_name(x)) == NULL) goto err;
  586. /* check for duplicates */
  587. xn=X509_NAME_dup(xn);
  588. if (xn == NULL) goto err;
  589. if (sk_X509_NAME_find(sk,xn) >= 0)
  590. X509_NAME_free(xn);
  591. else
  592. {
  593. sk_X509_NAME_push(sk,xn);
  594. sk_X509_NAME_push(ret,xn);
  595. }
  596. }
  597. if (0)
  598. {
  599. err:
  600. if (ret != NULL) sk_X509_NAME_pop_free(ret,X509_NAME_free);
  601. ret=NULL;
  602. }
  603. if (sk != NULL) sk_X509_NAME_free(sk);
  604. if (in != NULL) BIO_free(in);
  605. if (x != NULL) X509_free(x);
  606. if (ret != NULL)
  607. ERR_clear_error();
  608. return(ret);
  609. }
  610. #endif
  611. /*!
  612. * Add a file of certs to a stack.
  613. * \param stack the stack to add to.
  614. * \param file the file to add from. All certs in this file that are not
  615. * already in the stack will be added.
  616. * \return 1 for success, 0 for failure. Note that in the case of failure some
  617. * certs may have been added to \c stack.
  618. */
  619. int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  620. const char *file)
  621. {
  622. BIO *in;
  623. X509 *x=NULL;
  624. X509_NAME *xn=NULL;
  625. int ret=1;
  626. int (*oldcmp)(const X509_NAME * const *a, const X509_NAME * const *b);
  627. oldcmp=sk_X509_NAME_set_cmp_func(stack,xname_cmp);
  628. in=BIO_new(BIO_s_file_internal());
  629. if (in == NULL)
  630. {
  631. SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,ERR_R_MALLOC_FAILURE);
  632. goto err;
  633. }
  634. if (!BIO_read_filename(in,file))
  635. goto err;
  636. for (;;)
  637. {
  638. if (PEM_read_bio_X509(in,&x,NULL,NULL) == NULL)
  639. break;
  640. if ((xn=X509_get_subject_name(x)) == NULL) goto err;
  641. xn=X509_NAME_dup(xn);
  642. if (xn == NULL) goto err;
  643. if (sk_X509_NAME_find(stack,xn) >= 0)
  644. X509_NAME_free(xn);
  645. else
  646. sk_X509_NAME_push(stack,xn);
  647. }
  648. if (0)
  649. {
  650. err:
  651. ret=0;
  652. }
  653. if(in != NULL)
  654. BIO_free(in);
  655. if(x != NULL)
  656. X509_free(x);
  657. sk_X509_NAME_set_cmp_func(stack,oldcmp);
  658. return ret;
  659. }
  660. /*!
  661. * Add a directory of certs to a stack.
  662. * \param stack the stack to append to.
  663. * \param dir the directory to append from. All files in this directory will be
  664. * examined as potential certs. Any that are acceptable to
  665. * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
  666. * included.
  667. * \return 1 for success, 0 for failure. Note that in the case of failure some
  668. * certs may have been added to \c stack.
  669. */
  670. int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  671. const char *dir)
  672. {
  673. OPENSSL_DIR_CTX *d = NULL;
  674. const char *filename;
  675. int ret = 0;
  676. CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
  677. /* Note that a side effect is that the CAs will be sorted by name */
  678. while((filename = OPENSSL_DIR_read(&d, dir)))
  679. {
  680. char buf[1024];
  681. int r;
  682. if(strlen(dir)+strlen(filename)+2 > sizeof buf)
  683. {
  684. SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,SSL_R_PATH_TOO_LONG);
  685. goto err;
  686. }
  687. #ifdef OPENSSL_SYS_VMS
  688. r = BIO_snprintf(buf,sizeof buf,"%s%s",dir,filename);
  689. #else
  690. r = BIO_snprintf(buf,sizeof buf,"%s/%s",dir,filename);
  691. #endif
  692. if (r <= 0 || r >= (int)sizeof(buf))
  693. goto err;
  694. if(!SSL_add_file_cert_subjects_to_stack(stack,buf))
  695. goto err;
  696. }
  697. if (errno)
  698. {
  699. SYSerr(SYS_F_OPENDIR, get_last_sys_error());
  700. ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
  701. SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
  702. goto err;
  703. }
  704. ret = 1;
  705. err:
  706. if (d) OPENSSL_DIR_end(&d);
  707. CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
  708. return ret;
  709. }