ssl_cert.c 30 KB

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