2
0

ssl_cert.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /*
  2. * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include "internal/nelem.h"
  13. #include "internal/o_dir.h"
  14. #include <openssl/bio.h>
  15. #include <openssl/pem.h>
  16. #include <openssl/store.h>
  17. #include <openssl/x509v3.h>
  18. #include <openssl/dh.h>
  19. #include <openssl/bn.h>
  20. #include <openssl/crypto.h>
  21. #include "internal/refcount.h"
  22. #include "ssl_local.h"
  23. #include "ssl_cert_table.h"
  24. #include "internal/thread_once.h"
  25. #ifndef OPENSSL_NO_POSIX_IO
  26. # include <sys/stat.h>
  27. # ifdef _WIN32
  28. # define stat _stat
  29. # endif
  30. # ifndef S_ISDIR
  31. # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
  32. # endif
  33. #endif
  34. static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
  35. int op, int bits, int nid, void *other,
  36. void *ex);
  37. static CRYPTO_ONCE ssl_x509_store_ctx_once = CRYPTO_ONCE_STATIC_INIT;
  38. static volatile int ssl_x509_store_ctx_idx = -1;
  39. DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init)
  40. {
  41. ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0,
  42. "SSL for verify callback",
  43. NULL, NULL, NULL);
  44. return ssl_x509_store_ctx_idx >= 0;
  45. }
  46. int SSL_get_ex_data_X509_STORE_CTX_idx(void)
  47. {
  48. if (!RUN_ONCE(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init))
  49. return -1;
  50. return ssl_x509_store_ctx_idx;
  51. }
  52. CERT *ssl_cert_new(size_t ssl_pkey_num)
  53. {
  54. CERT *ret = NULL;
  55. /* Should never happen */
  56. if (!ossl_assert(ssl_pkey_num >= SSL_PKEY_NUM))
  57. return NULL;
  58. ret = OPENSSL_zalloc(sizeof(*ret));
  59. if (ret == NULL)
  60. return NULL;
  61. ret->ssl_pkey_num = ssl_pkey_num;
  62. ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY));
  63. if (ret->pkeys == NULL) {
  64. OPENSSL_free(ret);
  65. return NULL;
  66. }
  67. ret->key = &(ret->pkeys[SSL_PKEY_RSA]);
  68. ret->sec_cb = ssl_security_default_callback;
  69. ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL;
  70. ret->sec_ex = NULL;
  71. if (!CRYPTO_NEW_REF(&ret->references, 1)) {
  72. OPENSSL_free(ret->pkeys);
  73. OPENSSL_free(ret);
  74. return NULL;
  75. }
  76. return ret;
  77. }
  78. CERT *ssl_cert_dup(CERT *cert)
  79. {
  80. CERT *ret = OPENSSL_zalloc(sizeof(*ret));
  81. size_t i;
  82. #ifndef OPENSSL_NO_COMP_ALG
  83. int j;
  84. #endif
  85. if (ret == NULL)
  86. return NULL;
  87. ret->ssl_pkey_num = cert->ssl_pkey_num;
  88. ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY));
  89. if (ret->pkeys == NULL) {
  90. OPENSSL_free(ret);
  91. return NULL;
  92. }
  93. ret->key = &ret->pkeys[cert->key - cert->pkeys];
  94. if (!CRYPTO_NEW_REF(&ret->references, 1)) {
  95. OPENSSL_free(ret->pkeys);
  96. OPENSSL_free(ret);
  97. return NULL;
  98. }
  99. if (cert->dh_tmp != NULL) {
  100. ret->dh_tmp = cert->dh_tmp;
  101. EVP_PKEY_up_ref(ret->dh_tmp);
  102. }
  103. ret->dh_tmp_cb = cert->dh_tmp_cb;
  104. ret->dh_tmp_auto = cert->dh_tmp_auto;
  105. for (i = 0; i < ret->ssl_pkey_num; i++) {
  106. CERT_PKEY *cpk = cert->pkeys + i;
  107. CERT_PKEY *rpk = ret->pkeys + i;
  108. if (cpk->x509 != NULL) {
  109. rpk->x509 = cpk->x509;
  110. X509_up_ref(rpk->x509);
  111. }
  112. if (cpk->privatekey != NULL) {
  113. rpk->privatekey = cpk->privatekey;
  114. EVP_PKEY_up_ref(cpk->privatekey);
  115. }
  116. if (cpk->chain) {
  117. rpk->chain = X509_chain_up_ref(cpk->chain);
  118. if (!rpk->chain) {
  119. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  120. goto err;
  121. }
  122. }
  123. if (cpk->serverinfo != NULL) {
  124. /* Just copy everything. */
  125. rpk->serverinfo = OPENSSL_memdup(cpk->serverinfo, cpk->serverinfo_length);
  126. if (rpk->serverinfo == NULL)
  127. goto err;
  128. rpk->serverinfo_length = cpk->serverinfo_length;
  129. }
  130. #ifndef OPENSSL_NO_COMP_ALG
  131. for (j = TLSEXT_comp_cert_none; j < TLSEXT_comp_cert_limit; j++) {
  132. if (cpk->comp_cert[j] != NULL) {
  133. if (!OSSL_COMP_CERT_up_ref(cpk->comp_cert[j]))
  134. goto err;
  135. rpk->comp_cert[j] = cpk->comp_cert[j];
  136. }
  137. }
  138. #endif
  139. }
  140. /* Configured sigalgs copied across */
  141. if (cert->conf_sigalgs) {
  142. ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen
  143. * sizeof(*cert->conf_sigalgs));
  144. if (ret->conf_sigalgs == NULL)
  145. goto err;
  146. memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
  147. cert->conf_sigalgslen * sizeof(*cert->conf_sigalgs));
  148. ret->conf_sigalgslen = cert->conf_sigalgslen;
  149. } else
  150. ret->conf_sigalgs = NULL;
  151. if (cert->client_sigalgs) {
  152. ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen
  153. * sizeof(*cert->client_sigalgs));
  154. if (ret->client_sigalgs == NULL)
  155. goto err;
  156. memcpy(ret->client_sigalgs, cert->client_sigalgs,
  157. cert->client_sigalgslen * sizeof(*cert->client_sigalgs));
  158. ret->client_sigalgslen = cert->client_sigalgslen;
  159. } else
  160. ret->client_sigalgs = NULL;
  161. /* Copy any custom client certificate types */
  162. if (cert->ctype) {
  163. ret->ctype = OPENSSL_memdup(cert->ctype, cert->ctype_len);
  164. if (ret->ctype == NULL)
  165. goto err;
  166. ret->ctype_len = cert->ctype_len;
  167. }
  168. ret->cert_flags = cert->cert_flags;
  169. ret->cert_cb = cert->cert_cb;
  170. ret->cert_cb_arg = cert->cert_cb_arg;
  171. if (cert->verify_store) {
  172. X509_STORE_up_ref(cert->verify_store);
  173. ret->verify_store = cert->verify_store;
  174. }
  175. if (cert->chain_store) {
  176. X509_STORE_up_ref(cert->chain_store);
  177. ret->chain_store = cert->chain_store;
  178. }
  179. ret->sec_cb = cert->sec_cb;
  180. ret->sec_level = cert->sec_level;
  181. ret->sec_ex = cert->sec_ex;
  182. if (!custom_exts_copy(&ret->custext, &cert->custext))
  183. goto err;
  184. #ifndef OPENSSL_NO_PSK
  185. if (cert->psk_identity_hint) {
  186. ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint);
  187. if (ret->psk_identity_hint == NULL)
  188. goto err;
  189. }
  190. #endif
  191. return ret;
  192. err:
  193. ssl_cert_free(ret);
  194. return NULL;
  195. }
  196. /* Free up and clear all certificates and chains */
  197. void ssl_cert_clear_certs(CERT *c)
  198. {
  199. size_t i;
  200. #ifndef OPENSSL_NO_COMP_ALG
  201. int j;
  202. #endif
  203. if (c == NULL)
  204. return;
  205. for (i = 0; i < c->ssl_pkey_num; i++) {
  206. CERT_PKEY *cpk = c->pkeys + i;
  207. X509_free(cpk->x509);
  208. cpk->x509 = NULL;
  209. EVP_PKEY_free(cpk->privatekey);
  210. cpk->privatekey = NULL;
  211. OSSL_STACK_OF_X509_free(cpk->chain);
  212. cpk->chain = NULL;
  213. OPENSSL_free(cpk->serverinfo);
  214. cpk->serverinfo = NULL;
  215. cpk->serverinfo_length = 0;
  216. #ifndef OPENSSL_NO_COMP_ALG
  217. for (j = 0; j < TLSEXT_comp_cert_limit; j++) {
  218. OSSL_COMP_CERT_free(cpk->comp_cert[j]);
  219. cpk->comp_cert[j] = NULL;
  220. cpk->cert_comp_used = 0;
  221. }
  222. #endif
  223. }
  224. }
  225. void ssl_cert_free(CERT *c)
  226. {
  227. int i;
  228. if (c == NULL)
  229. return;
  230. CRYPTO_DOWN_REF(&c->references, &i);
  231. REF_PRINT_COUNT("CERT", c);
  232. if (i > 0)
  233. return;
  234. REF_ASSERT_ISNT(i < 0);
  235. EVP_PKEY_free(c->dh_tmp);
  236. ssl_cert_clear_certs(c);
  237. OPENSSL_free(c->conf_sigalgs);
  238. OPENSSL_free(c->client_sigalgs);
  239. OPENSSL_free(c->ctype);
  240. X509_STORE_free(c->verify_store);
  241. X509_STORE_free(c->chain_store);
  242. custom_exts_free(&c->custext);
  243. #ifndef OPENSSL_NO_PSK
  244. OPENSSL_free(c->psk_identity_hint);
  245. #endif
  246. OPENSSL_free(c->pkeys);
  247. CRYPTO_FREE_REF(&c->references);
  248. OPENSSL_free(c);
  249. }
  250. int ssl_cert_set0_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
  251. {
  252. int i, r;
  253. CERT_PKEY *cpk = s != NULL ? s->cert->key : ctx->cert->key;
  254. if (!cpk)
  255. return 0;
  256. for (i = 0; i < sk_X509_num(chain); i++) {
  257. X509 *x = sk_X509_value(chain, i);
  258. r = ssl_security_cert(s, ctx, x, 0, 0);
  259. if (r != 1) {
  260. ERR_raise(ERR_LIB_SSL, r);
  261. return 0;
  262. }
  263. }
  264. OSSL_STACK_OF_X509_free(cpk->chain);
  265. cpk->chain = chain;
  266. return 1;
  267. }
  268. int ssl_cert_set1_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
  269. {
  270. STACK_OF(X509) *dchain;
  271. if (!chain)
  272. return ssl_cert_set0_chain(s, ctx, NULL);
  273. dchain = X509_chain_up_ref(chain);
  274. if (!dchain)
  275. return 0;
  276. if (!ssl_cert_set0_chain(s, ctx, dchain)) {
  277. OSSL_STACK_OF_X509_free(dchain);
  278. return 0;
  279. }
  280. return 1;
  281. }
  282. int ssl_cert_add0_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x)
  283. {
  284. int r;
  285. CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
  286. if (!cpk)
  287. return 0;
  288. r = ssl_security_cert(s, ctx, x, 0, 0);
  289. if (r != 1) {
  290. ERR_raise(ERR_LIB_SSL, r);
  291. return 0;
  292. }
  293. if (!cpk->chain)
  294. cpk->chain = sk_X509_new_null();
  295. if (!cpk->chain || !sk_X509_push(cpk->chain, x))
  296. return 0;
  297. return 1;
  298. }
  299. int ssl_cert_add1_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x)
  300. {
  301. if (!ssl_cert_add0_chain_cert(s, ctx, x))
  302. return 0;
  303. X509_up_ref(x);
  304. return 1;
  305. }
  306. int ssl_cert_select_current(CERT *c, X509 *x)
  307. {
  308. size_t i;
  309. if (x == NULL)
  310. return 0;
  311. for (i = 0; i < c->ssl_pkey_num; i++) {
  312. CERT_PKEY *cpk = c->pkeys + i;
  313. if (cpk->x509 == x && cpk->privatekey) {
  314. c->key = cpk;
  315. return 1;
  316. }
  317. }
  318. for (i = 0; i < c->ssl_pkey_num; i++) {
  319. CERT_PKEY *cpk = c->pkeys + i;
  320. if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
  321. c->key = cpk;
  322. return 1;
  323. }
  324. }
  325. return 0;
  326. }
  327. int ssl_cert_set_current(CERT *c, long op)
  328. {
  329. size_t i, idx;
  330. if (!c)
  331. return 0;
  332. if (op == SSL_CERT_SET_FIRST)
  333. idx = 0;
  334. else if (op == SSL_CERT_SET_NEXT) {
  335. idx = (size_t)(c->key - c->pkeys + 1);
  336. if (idx >= c->ssl_pkey_num)
  337. return 0;
  338. } else
  339. return 0;
  340. for (i = idx; i < c->ssl_pkey_num; i++) {
  341. CERT_PKEY *cpk = c->pkeys + i;
  342. if (cpk->x509 && cpk->privatekey) {
  343. c->key = cpk;
  344. return 1;
  345. }
  346. }
  347. return 0;
  348. }
  349. void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
  350. {
  351. c->cert_cb = cb;
  352. c->cert_cb_arg = arg;
  353. }
  354. /*
  355. * Verify a certificate chain/raw public key
  356. * Return codes:
  357. * 1: Verify success
  358. * 0: Verify failure or error
  359. * -1: Retry required
  360. */
  361. static int ssl_verify_internal(SSL_CONNECTION *s, STACK_OF(X509) *sk, EVP_PKEY *rpk)
  362. {
  363. X509 *x;
  364. int i = 0;
  365. X509_STORE *verify_store;
  366. X509_STORE_CTX *ctx = NULL;
  367. X509_VERIFY_PARAM *param;
  368. SSL_CTX *sctx;
  369. /* Something must be passed in */
  370. if ((sk == NULL || sk_X509_num(sk) == 0) && rpk == NULL)
  371. return 0;
  372. /* Only one can be set */
  373. if (sk != NULL && rpk != NULL)
  374. return 0;
  375. sctx = SSL_CONNECTION_GET_CTX(s);
  376. if (s->cert->verify_store)
  377. verify_store = s->cert->verify_store;
  378. else
  379. verify_store = sctx->cert_store;
  380. ctx = X509_STORE_CTX_new_ex(sctx->libctx, sctx->propq);
  381. if (ctx == NULL) {
  382. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  383. return 0;
  384. }
  385. if (sk != NULL) {
  386. x = sk_X509_value(sk, 0);
  387. if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) {
  388. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  389. goto end;
  390. }
  391. } else {
  392. if (!X509_STORE_CTX_init_rpk(ctx, verify_store, rpk)) {
  393. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  394. goto end;
  395. }
  396. }
  397. param = X509_STORE_CTX_get0_param(ctx);
  398. /*
  399. * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
  400. * point, for now a single @SECLEVEL sets the same policy for TLS crypto
  401. * and PKI authentication.
  402. */
  403. X509_VERIFY_PARAM_set_auth_level(param,
  404. SSL_get_security_level(SSL_CONNECTION_GET_SSL(s)));
  405. /* Set suite B flags if needed */
  406. X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));
  407. if (!X509_STORE_CTX_set_ex_data(ctx,
  408. SSL_get_ex_data_X509_STORE_CTX_idx(), s)) {
  409. goto end;
  410. }
  411. /* Verify via DANE if enabled */
  412. if (DANETLS_ENABLED(&s->dane))
  413. X509_STORE_CTX_set0_dane(ctx, &s->dane);
  414. /*
  415. * We need to inherit the verify parameters. These can be determined by
  416. * the context: if its a server it will verify SSL client certificates or
  417. * vice versa.
  418. */
  419. X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server");
  420. /*
  421. * Anything non-default in "s->param" should overwrite anything in the ctx.
  422. */
  423. X509_VERIFY_PARAM_set1(param, s->param);
  424. if (s->verify_callback)
  425. X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);
  426. if (sctx->app_verify_callback != NULL) {
  427. i = sctx->app_verify_callback(ctx, sctx->app_verify_arg);
  428. } else {
  429. i = X509_verify_cert(ctx);
  430. /* We treat an error in the same way as a failure to verify */
  431. if (i < 0)
  432. i = 0;
  433. }
  434. s->verify_result = X509_STORE_CTX_get_error(ctx);
  435. OSSL_STACK_OF_X509_free(s->verified_chain);
  436. s->verified_chain = NULL;
  437. if (sk != NULL && X509_STORE_CTX_get0_chain(ctx) != NULL) {
  438. s->verified_chain = X509_STORE_CTX_get1_chain(ctx);
  439. if (s->verified_chain == NULL) {
  440. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  441. i = 0;
  442. }
  443. }
  444. /* Move peername from the store context params to the SSL handle's */
  445. X509_VERIFY_PARAM_move_peername(s->param, param);
  446. end:
  447. X509_STORE_CTX_free(ctx);
  448. return i;
  449. }
  450. /*
  451. * Verify a raw public key
  452. * Return codes:
  453. * 1: Verify success
  454. * 0: Verify failure or error
  455. * -1: Retry required
  456. */
  457. int ssl_verify_rpk(SSL_CONNECTION *s, EVP_PKEY *rpk)
  458. {
  459. return ssl_verify_internal(s, NULL, rpk);
  460. }
  461. /*
  462. * Verify a certificate chain
  463. * Return codes:
  464. * 1: Verify success
  465. * 0: Verify failure or error
  466. * -1: Retry required
  467. */
  468. int ssl_verify_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk)
  469. {
  470. return ssl_verify_internal(s, sk, NULL);
  471. }
  472. static void set0_CA_list(STACK_OF(X509_NAME) **ca_list,
  473. STACK_OF(X509_NAME) *name_list)
  474. {
  475. sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
  476. *ca_list = name_list;
  477. }
  478. STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk)
  479. {
  480. int i;
  481. const int num = sk_X509_NAME_num(sk);
  482. STACK_OF(X509_NAME) *ret;
  483. X509_NAME *name;
  484. ret = sk_X509_NAME_new_reserve(NULL, num);
  485. if (ret == NULL) {
  486. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  487. return NULL;
  488. }
  489. for (i = 0; i < num; i++) {
  490. name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
  491. if (name == NULL) {
  492. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  493. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  494. return NULL;
  495. }
  496. sk_X509_NAME_push(ret, name); /* Cannot fail after reserve call */
  497. }
  498. return ret;
  499. }
  500. void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
  501. {
  502. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  503. if (sc == NULL)
  504. return;
  505. set0_CA_list(&sc->ca_names, name_list);
  506. }
  507. void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
  508. {
  509. set0_CA_list(&ctx->ca_names, name_list);
  510. }
  511. const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx)
  512. {
  513. return ctx->ca_names;
  514. }
  515. const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s)
  516. {
  517. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  518. if (sc == NULL)
  519. return NULL;
  520. return sc->ca_names != NULL ? sc->ca_names : s->ctx->ca_names;
  521. }
  522. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
  523. {
  524. set0_CA_list(&ctx->client_ca_names, name_list);
  525. }
  526. STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
  527. {
  528. return ctx->client_ca_names;
  529. }
  530. void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
  531. {
  532. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  533. if (sc == NULL)
  534. return;
  535. set0_CA_list(&sc->client_ca_names, name_list);
  536. }
  537. const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s)
  538. {
  539. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  540. if (sc == NULL)
  541. return NULL;
  542. return sc->s3.tmp.peer_ca_names;
  543. }
  544. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
  545. {
  546. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  547. if (sc == NULL)
  548. return NULL;
  549. if (!sc->server)
  550. return sc->s3.tmp.peer_ca_names;
  551. return sc->client_ca_names != NULL ? sc->client_ca_names
  552. : s->ctx->client_ca_names;
  553. }
  554. static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x)
  555. {
  556. X509_NAME *name;
  557. if (x == NULL)
  558. return 0;
  559. if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL))
  560. return 0;
  561. if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
  562. return 0;
  563. if (!sk_X509_NAME_push(*sk, name)) {
  564. X509_NAME_free(name);
  565. return 0;
  566. }
  567. return 1;
  568. }
  569. int SSL_add1_to_CA_list(SSL *ssl, const X509 *x)
  570. {
  571. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
  572. if (sc == NULL)
  573. return 0;
  574. return add_ca_name(&sc->ca_names, x);
  575. }
  576. int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x)
  577. {
  578. return add_ca_name(&ctx->ca_names, x);
  579. }
  580. /*
  581. * The following two are older names are to be replaced with
  582. * SSL(_CTX)_add1_to_CA_list
  583. */
  584. int SSL_add_client_CA(SSL *ssl, X509 *x)
  585. {
  586. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
  587. if (sc == NULL)
  588. return 0;
  589. return add_ca_name(&sc->client_ca_names, x);
  590. }
  591. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
  592. {
  593. return add_ca_name(&ctx->client_ca_names, x);
  594. }
  595. static int xname_cmp(const X509_NAME *a, const X509_NAME *b)
  596. {
  597. unsigned char *abuf = NULL, *bbuf = NULL;
  598. int alen, blen, ret;
  599. /* X509_NAME_cmp() itself casts away constness in this way, so
  600. * assume it's safe:
  601. */
  602. alen = i2d_X509_NAME((X509_NAME *)a, &abuf);
  603. blen = i2d_X509_NAME((X509_NAME *)b, &bbuf);
  604. if (alen < 0 || blen < 0)
  605. ret = -2;
  606. else if (alen != blen)
  607. ret = alen - blen;
  608. else /* alen == blen */
  609. ret = memcmp(abuf, bbuf, alen);
  610. OPENSSL_free(abuf);
  611. OPENSSL_free(bbuf);
  612. return ret;
  613. }
  614. static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
  615. {
  616. return xname_cmp(*a, *b);
  617. }
  618. static unsigned long xname_hash(const X509_NAME *a)
  619. {
  620. /* This returns 0 also if SHA1 is not available */
  621. return X509_NAME_hash_ex((X509_NAME *)a, NULL, NULL, NULL);
  622. }
  623. STACK_OF(X509_NAME) *SSL_load_client_CA_file_ex(const char *file,
  624. OSSL_LIB_CTX *libctx,
  625. const char *propq)
  626. {
  627. BIO *in = BIO_new(BIO_s_file());
  628. X509 *x = NULL;
  629. X509_NAME *xn = NULL;
  630. STACK_OF(X509_NAME) *ret = NULL;
  631. LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
  632. OSSL_LIB_CTX *prev_libctx = NULL;
  633. if (name_hash == NULL) {
  634. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  635. goto err;
  636. }
  637. if (in == NULL) {
  638. ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB);
  639. goto err;
  640. }
  641. x = X509_new_ex(libctx, propq);
  642. if (x == NULL) {
  643. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  644. goto err;
  645. }
  646. if (BIO_read_filename(in, file) <= 0)
  647. goto err;
  648. /* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */
  649. prev_libctx = OSSL_LIB_CTX_set0_default(libctx);
  650. for (;;) {
  651. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
  652. break;
  653. if (ret == NULL) {
  654. ret = sk_X509_NAME_new_null();
  655. if (ret == NULL) {
  656. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  657. goto err;
  658. }
  659. }
  660. if ((xn = X509_get_subject_name(x)) == NULL)
  661. goto err;
  662. /* check for duplicates */
  663. xn = X509_NAME_dup(xn);
  664. if (xn == NULL)
  665. goto err;
  666. if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
  667. /* Duplicate. */
  668. X509_NAME_free(xn);
  669. xn = NULL;
  670. } else {
  671. lh_X509_NAME_insert(name_hash, xn);
  672. if (!sk_X509_NAME_push(ret, xn))
  673. goto err;
  674. }
  675. }
  676. goto done;
  677. err:
  678. X509_NAME_free(xn);
  679. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  680. ret = NULL;
  681. done:
  682. /* restore the old libctx */
  683. OSSL_LIB_CTX_set0_default(prev_libctx);
  684. BIO_free(in);
  685. X509_free(x);
  686. lh_X509_NAME_free(name_hash);
  687. if (ret != NULL)
  688. ERR_clear_error();
  689. return ret;
  690. }
  691. STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
  692. {
  693. return SSL_load_client_CA_file_ex(file, NULL, NULL);
  694. }
  695. int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  696. const char *file)
  697. {
  698. BIO *in;
  699. X509 *x = NULL;
  700. X509_NAME *xn = NULL;
  701. int ret = 1;
  702. int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
  703. oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
  704. in = BIO_new(BIO_s_file());
  705. if (in == NULL) {
  706. ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB);
  707. goto err;
  708. }
  709. if (BIO_read_filename(in, file) <= 0)
  710. goto err;
  711. for (;;) {
  712. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
  713. break;
  714. if ((xn = X509_get_subject_name(x)) == NULL)
  715. goto err;
  716. xn = X509_NAME_dup(xn);
  717. if (xn == NULL)
  718. goto err;
  719. if (sk_X509_NAME_find(stack, xn) >= 0) {
  720. /* Duplicate. */
  721. X509_NAME_free(xn);
  722. } else if (!sk_X509_NAME_push(stack, xn)) {
  723. X509_NAME_free(xn);
  724. goto err;
  725. }
  726. }
  727. ERR_clear_error();
  728. goto done;
  729. err:
  730. ret = 0;
  731. done:
  732. BIO_free(in);
  733. X509_free(x);
  734. (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
  735. return ret;
  736. }
  737. int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  738. const char *dir)
  739. {
  740. OPENSSL_DIR_CTX *d = NULL;
  741. const char *filename;
  742. int ret = 0;
  743. /* Note that a side effect is that the CAs will be sorted by name */
  744. while ((filename = OPENSSL_DIR_read(&d, dir))) {
  745. char buf[1024];
  746. int r;
  747. #ifndef OPENSSL_NO_POSIX_IO
  748. struct stat st;
  749. #else
  750. /* Cannot use stat so just skip current and parent directories */
  751. if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
  752. continue;
  753. #endif
  754. if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
  755. ERR_raise(ERR_LIB_SSL, SSL_R_PATH_TOO_LONG);
  756. goto err;
  757. }
  758. #ifdef OPENSSL_SYS_VMS
  759. r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
  760. #else
  761. r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
  762. #endif
  763. #ifndef OPENSSL_NO_POSIX_IO
  764. /* Skip subdirectories */
  765. if (!stat(buf, &st) && S_ISDIR(st.st_mode))
  766. continue;
  767. #endif
  768. if (r <= 0 || r >= (int)sizeof(buf))
  769. goto err;
  770. if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
  771. goto err;
  772. }
  773. if (errno) {
  774. ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
  775. "calling OPENSSL_dir_read(%s)", dir);
  776. ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
  777. goto err;
  778. }
  779. ret = 1;
  780. err:
  781. if (d)
  782. OPENSSL_DIR_end(&d);
  783. return ret;
  784. }
  785. static int add_uris_recursive(STACK_OF(X509_NAME) *stack,
  786. const char *uri, int depth)
  787. {
  788. int ok = 1;
  789. OSSL_STORE_CTX *ctx = NULL;
  790. X509 *x = NULL;
  791. X509_NAME *xn = NULL;
  792. if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)
  793. goto err;
  794. while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) {
  795. OSSL_STORE_INFO *info = OSSL_STORE_load(ctx);
  796. int infotype = info == 0 ? 0 : OSSL_STORE_INFO_get_type(info);
  797. if (info == NULL)
  798. continue;
  799. if (infotype == OSSL_STORE_INFO_NAME) {
  800. /*
  801. * This is an entry in the "directory" represented by the current
  802. * uri. if |depth| allows, dive into it.
  803. */
  804. if (depth > 0)
  805. ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info),
  806. depth - 1);
  807. } else if (infotype == OSSL_STORE_INFO_CERT) {
  808. if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL
  809. || (xn = X509_get_subject_name(x)) == NULL
  810. || (xn = X509_NAME_dup(xn)) == NULL)
  811. goto err;
  812. if (sk_X509_NAME_find(stack, xn) >= 0) {
  813. /* Duplicate. */
  814. X509_NAME_free(xn);
  815. } else if (!sk_X509_NAME_push(stack, xn)) {
  816. X509_NAME_free(xn);
  817. goto err;
  818. }
  819. }
  820. OSSL_STORE_INFO_free(info);
  821. }
  822. ERR_clear_error();
  823. goto done;
  824. err:
  825. ok = 0;
  826. done:
  827. OSSL_STORE_close(ctx);
  828. return ok;
  829. }
  830. int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  831. const char *store)
  832. {
  833. int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b)
  834. = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
  835. int ret = add_uris_recursive(stack, store, 1);
  836. (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
  837. return ret;
  838. }
  839. /* Build a certificate chain for current certificate */
  840. int ssl_build_cert_chain(SSL_CONNECTION *s, SSL_CTX *ctx, int flags)
  841. {
  842. CERT *c = s != NULL ? s->cert : ctx->cert;
  843. CERT_PKEY *cpk = c->key;
  844. X509_STORE *chain_store = NULL;
  845. X509_STORE_CTX *xs_ctx = NULL;
  846. STACK_OF(X509) *chain = NULL, *untrusted = NULL;
  847. X509 *x;
  848. SSL_CTX *real_ctx = (s == NULL) ? ctx : SSL_CONNECTION_GET_CTX(s);
  849. int i, rv = 0;
  850. if (cpk->x509 == NULL) {
  851. ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET);
  852. goto err;
  853. }
  854. /* Rearranging and check the chain: add everything to a store */
  855. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
  856. chain_store = X509_STORE_new();
  857. if (chain_store == NULL)
  858. goto err;
  859. for (i = 0; i < sk_X509_num(cpk->chain); i++) {
  860. x = sk_X509_value(cpk->chain, i);
  861. if (!X509_STORE_add_cert(chain_store, x))
  862. goto err;
  863. }
  864. /* Add EE cert too: it might be self signed */
  865. if (!X509_STORE_add_cert(chain_store, cpk->x509))
  866. goto err;
  867. } else {
  868. if (c->chain_store != NULL)
  869. chain_store = c->chain_store;
  870. else
  871. chain_store = real_ctx->cert_store;
  872. if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
  873. untrusted = cpk->chain;
  874. }
  875. xs_ctx = X509_STORE_CTX_new_ex(real_ctx->libctx, real_ctx->propq);
  876. if (xs_ctx == NULL) {
  877. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  878. goto err;
  879. }
  880. if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
  881. ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
  882. goto err;
  883. }
  884. /* Set suite B flags if needed */
  885. X509_STORE_CTX_set_flags(xs_ctx,
  886. c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
  887. i = X509_verify_cert(xs_ctx);
  888. if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
  889. if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
  890. ERR_clear_error();
  891. i = 1;
  892. rv = 2;
  893. }
  894. if (i > 0)
  895. chain = X509_STORE_CTX_get1_chain(xs_ctx);
  896. if (i <= 0) {
  897. i = X509_STORE_CTX_get_error(xs_ctx);
  898. ERR_raise_data(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED,
  899. "Verify error:%s", X509_verify_cert_error_string(i));
  900. goto err;
  901. }
  902. /* Remove EE certificate from chain */
  903. x = sk_X509_shift(chain);
  904. X509_free(x);
  905. if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
  906. if (sk_X509_num(chain) > 0) {
  907. /* See if last cert is self signed */
  908. x = sk_X509_value(chain, sk_X509_num(chain) - 1);
  909. if (X509_get_extension_flags(x) & EXFLAG_SS) {
  910. x = sk_X509_pop(chain);
  911. X509_free(x);
  912. }
  913. }
  914. }
  915. /*
  916. * Check security level of all CA certificates: EE will have been checked
  917. * already.
  918. */
  919. for (i = 0; i < sk_X509_num(chain); i++) {
  920. x = sk_X509_value(chain, i);
  921. rv = ssl_security_cert(s, ctx, x, 0, 0);
  922. if (rv != 1) {
  923. ERR_raise(ERR_LIB_SSL, rv);
  924. OSSL_STACK_OF_X509_free(chain);
  925. rv = 0;
  926. goto err;
  927. }
  928. }
  929. OSSL_STACK_OF_X509_free(cpk->chain);
  930. cpk->chain = chain;
  931. if (rv == 0)
  932. rv = 1;
  933. err:
  934. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
  935. X509_STORE_free(chain_store);
  936. X509_STORE_CTX_free(xs_ctx);
  937. return rv;
  938. }
  939. int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
  940. {
  941. X509_STORE **pstore;
  942. if (chain)
  943. pstore = &c->chain_store;
  944. else
  945. pstore = &c->verify_store;
  946. X509_STORE_free(*pstore);
  947. *pstore = store;
  948. if (ref && store)
  949. X509_STORE_up_ref(store);
  950. return 1;
  951. }
  952. int ssl_cert_get_cert_store(CERT *c, X509_STORE **pstore, int chain)
  953. {
  954. *pstore = (chain ? c->chain_store : c->verify_store);
  955. return 1;
  956. }
  957. int ssl_get_security_level_bits(const SSL *s, const SSL_CTX *ctx, int *levelp)
  958. {
  959. int level;
  960. /*
  961. * note that there's a corresponding minbits_table
  962. * in crypto/x509/x509_vfy.c that's used for checking the security level
  963. * of RSA and DSA keys
  964. */
  965. static const int minbits_table[5 + 1] = { 0, 80, 112, 128, 192, 256 };
  966. if (ctx != NULL)
  967. level = SSL_CTX_get_security_level(ctx);
  968. else
  969. level = SSL_get_security_level(s);
  970. if (level > 5)
  971. level = 5;
  972. else if (level < 0)
  973. level = 0;
  974. if (levelp != NULL)
  975. *levelp = level;
  976. return minbits_table[level];
  977. }
  978. static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
  979. int op, int bits, int nid, void *other,
  980. void *ex)
  981. {
  982. int level, minbits, pfs_mask;
  983. const SSL_CONNECTION *sc;
  984. minbits = ssl_get_security_level_bits(s, ctx, &level);
  985. if (level == 0) {
  986. /*
  987. * No EDH keys weaker than 1024-bits even at level 0, otherwise,
  988. * anything goes.
  989. */
  990. if (op == SSL_SECOP_TMP_DH && bits < 80)
  991. return 0;
  992. return 1;
  993. }
  994. switch (op) {
  995. case SSL_SECOP_CIPHER_SUPPORTED:
  996. case SSL_SECOP_CIPHER_SHARED:
  997. case SSL_SECOP_CIPHER_CHECK:
  998. {
  999. const SSL_CIPHER *c = other;
  1000. /* No ciphers below security level */
  1001. if (bits < minbits)
  1002. return 0;
  1003. /* No unauthenticated ciphersuites */
  1004. if (c->algorithm_auth & SSL_aNULL)
  1005. return 0;
  1006. /* No MD5 mac ciphersuites */
  1007. if (c->algorithm_mac & SSL_MD5)
  1008. return 0;
  1009. /* SHA1 HMAC is 160 bits of security */
  1010. if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
  1011. return 0;
  1012. /* Level 3: forward secure ciphersuites only */
  1013. pfs_mask = SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK;
  1014. if (level >= 3 && c->min_tls != TLS1_3_VERSION &&
  1015. !(c->algorithm_mkey & pfs_mask))
  1016. return 0;
  1017. break;
  1018. }
  1019. case SSL_SECOP_VERSION:
  1020. if ((sc = SSL_CONNECTION_FROM_CONST_SSL(s)) == NULL)
  1021. return 0;
  1022. if (!SSL_CONNECTION_IS_DTLS(sc)) {
  1023. /* SSLv3, TLS v1.0 and TLS v1.1 only allowed at level 0 */
  1024. if (nid <= TLS1_1_VERSION && level > 0)
  1025. return 0;
  1026. } else {
  1027. /* DTLS v1.0 only allowed at level 0 */
  1028. if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level > 0)
  1029. return 0;
  1030. }
  1031. break;
  1032. case SSL_SECOP_COMPRESSION:
  1033. if (level >= 2)
  1034. return 0;
  1035. break;
  1036. case SSL_SECOP_TICKET:
  1037. if (level >= 3)
  1038. return 0;
  1039. break;
  1040. default:
  1041. if (bits < minbits)
  1042. return 0;
  1043. }
  1044. return 1;
  1045. }
  1046. int ssl_security(const SSL_CONNECTION *s, int op, int bits, int nid, void *other)
  1047. {
  1048. return s->cert->sec_cb(SSL_CONNECTION_GET_SSL(s), NULL, op, bits, nid,
  1049. other, s->cert->sec_ex);
  1050. }
  1051. int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
  1052. {
  1053. return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
  1054. ctx->cert->sec_ex);
  1055. }
  1056. int ssl_cert_lookup_by_nid(int nid, size_t *pidx, SSL_CTX *ctx)
  1057. {
  1058. size_t i;
  1059. for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
  1060. if (ssl_cert_info[i].nid == nid) {
  1061. *pidx = i;
  1062. return 1;
  1063. }
  1064. }
  1065. for (i = 0; i < ctx->sigalg_list_len; i++) {
  1066. if (ctx->ssl_cert_info[i].nid == nid) {
  1067. *pidx = SSL_PKEY_NUM + i;
  1068. return 1;
  1069. }
  1070. }
  1071. return 0;
  1072. }
  1073. const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx, SSL_CTX *ctx)
  1074. {
  1075. size_t i;
  1076. /* check classic pk types */
  1077. for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
  1078. const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i];
  1079. if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
  1080. || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
  1081. if (pidx != NULL)
  1082. *pidx = i;
  1083. return tmp_lu;
  1084. }
  1085. }
  1086. /* check provider-loaded pk types */
  1087. for (i = 0; ctx->sigalg_list_len; i++) {
  1088. SSL_CERT_LOOKUP *tmp_lu = &(ctx->ssl_cert_info[i]);
  1089. if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->nid))
  1090. || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->nid))) {
  1091. if (pidx != NULL)
  1092. *pidx = SSL_PKEY_NUM + i;
  1093. return &ctx->ssl_cert_info[i];
  1094. }
  1095. }
  1096. return NULL;
  1097. }
  1098. const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx, SSL_CTX *ctx)
  1099. {
  1100. if (idx >= (OSSL_NELEM(ssl_cert_info) + ctx->sigalg_list_len))
  1101. return NULL;
  1102. else if (idx >= (OSSL_NELEM(ssl_cert_info)))
  1103. return &(ctx->ssl_cert_info[idx - SSL_PKEY_NUM]);
  1104. return &ssl_cert_info[idx];
  1105. }