ssl_cert.c 34 KB

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