ssl_cert.c 30 KB

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