ssl_cert.c 36 KB

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