2
0

ssl_cert.c 31 KB

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