ssl_cert.c 28 KB

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