ssl_cert.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /*
  2. * Copyright 1995-2018 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/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.tmp.peer_ca_names;
  451. }
  452. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
  453. {
  454. if (!s->server)
  455. return s->s3.tmp.peer_ca_names;
  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. /**
  522. * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
  523. * it doesn't really have anything to do with clients (except that a common use
  524. * for a stack of CAs is to send it to the client). Actually, it doesn't have
  525. * much to do with CAs, either, since it will load any old cert.
  526. * \param file the file containing one or more certs.
  527. * \return a ::STACK containing the certs.
  528. */
  529. STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
  530. {
  531. BIO *in = BIO_new(BIO_s_file());
  532. X509 *x = NULL;
  533. X509_NAME *xn = NULL;
  534. STACK_OF(X509_NAME) *ret = NULL;
  535. LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
  536. if ((name_hash == NULL) || (in == NULL)) {
  537. SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
  538. goto err;
  539. }
  540. if (!BIO_read_filename(in, file))
  541. goto err;
  542. for (;;) {
  543. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
  544. break;
  545. if (ret == NULL) {
  546. ret = sk_X509_NAME_new_null();
  547. if (ret == NULL) {
  548. SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
  549. goto err;
  550. }
  551. }
  552. if ((xn = X509_get_subject_name(x)) == NULL)
  553. goto err;
  554. /* check for duplicates */
  555. xn = X509_NAME_dup(xn);
  556. if (xn == NULL)
  557. goto err;
  558. if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
  559. /* Duplicate. */
  560. X509_NAME_free(xn);
  561. xn = NULL;
  562. } else {
  563. lh_X509_NAME_insert(name_hash, xn);
  564. if (!sk_X509_NAME_push(ret, xn))
  565. goto err;
  566. }
  567. }
  568. goto done;
  569. err:
  570. X509_NAME_free(xn);
  571. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  572. ret = NULL;
  573. done:
  574. BIO_free(in);
  575. X509_free(x);
  576. lh_X509_NAME_free(name_hash);
  577. if (ret != NULL)
  578. ERR_clear_error();
  579. return ret;
  580. }
  581. /**
  582. * Add a file of certs to a stack.
  583. * \param stack the stack to add to.
  584. * \param file the file to add from. All certs in this file that are not
  585. * already in the stack will be added.
  586. * \return 1 for success, 0 for failure. Note that in the case of failure some
  587. * certs may have been added to \c stack.
  588. */
  589. int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  590. const char *file)
  591. {
  592. BIO *in;
  593. X509 *x = NULL;
  594. X509_NAME *xn = NULL;
  595. int ret = 1;
  596. int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
  597. oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
  598. in = BIO_new(BIO_s_file());
  599. if (in == NULL) {
  600. SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK, ERR_R_MALLOC_FAILURE);
  601. goto err;
  602. }
  603. if (!BIO_read_filename(in, file))
  604. goto err;
  605. for (;;) {
  606. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
  607. break;
  608. if ((xn = X509_get_subject_name(x)) == NULL)
  609. goto err;
  610. xn = X509_NAME_dup(xn);
  611. if (xn == NULL)
  612. goto err;
  613. if (sk_X509_NAME_find(stack, xn) >= 0) {
  614. /* Duplicate. */
  615. X509_NAME_free(xn);
  616. } else if (!sk_X509_NAME_push(stack, xn)) {
  617. X509_NAME_free(xn);
  618. goto err;
  619. }
  620. }
  621. ERR_clear_error();
  622. goto done;
  623. err:
  624. ret = 0;
  625. done:
  626. BIO_free(in);
  627. X509_free(x);
  628. (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
  629. return ret;
  630. }
  631. /**
  632. * Add a directory of certs to a stack.
  633. * \param stack the stack to append to.
  634. * \param dir the directory to append from. All files in this directory will be
  635. * examined as potential certs. Any that are acceptable to
  636. * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
  637. * included.
  638. * \return 1 for success, 0 for failure. Note that in the case of failure some
  639. * certs may have been added to \c stack.
  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. SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,
  653. SSL_R_PATH_TOO_LONG);
  654. goto err;
  655. }
  656. #ifdef OPENSSL_SYS_VMS
  657. r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
  658. #else
  659. r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
  660. #endif
  661. if (r <= 0 || r >= (int)sizeof(buf))
  662. goto err;
  663. if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
  664. goto err;
  665. }
  666. if (errno) {
  667. ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
  668. "calling OPENSSL_dir_read(%s)",
  669. dir);
  670. SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
  671. goto err;
  672. }
  673. ret = 1;
  674. err:
  675. if (d)
  676. OPENSSL_DIR_end(&d);
  677. return ret;
  678. }
  679. /* Build a certificate chain for current certificate */
  680. int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags)
  681. {
  682. CERT *c = s ? s->cert : ctx->cert;
  683. CERT_PKEY *cpk = c->key;
  684. X509_STORE *chain_store = NULL;
  685. X509_STORE_CTX *xs_ctx = NULL;
  686. STACK_OF(X509) *chain = NULL, *untrusted = NULL;
  687. X509 *x;
  688. int i, rv = 0;
  689. if (!cpk->x509) {
  690. SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET);
  691. goto err;
  692. }
  693. /* Rearranging and check the chain: add everything to a store */
  694. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
  695. chain_store = X509_STORE_new();
  696. if (chain_store == NULL)
  697. goto err;
  698. for (i = 0; i < sk_X509_num(cpk->chain); i++) {
  699. x = sk_X509_value(cpk->chain, i);
  700. if (!X509_STORE_add_cert(chain_store, x))
  701. goto err;
  702. }
  703. /* Add EE cert too: it might be self signed */
  704. if (!X509_STORE_add_cert(chain_store, cpk->x509))
  705. goto err;
  706. } else {
  707. if (c->chain_store)
  708. chain_store = c->chain_store;
  709. else if (s)
  710. chain_store = s->ctx->cert_store;
  711. else
  712. chain_store = ctx->cert_store;
  713. if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
  714. untrusted = cpk->chain;
  715. }
  716. xs_ctx = X509_STORE_CTX_new();
  717. if (xs_ctx == NULL) {
  718. SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_MALLOC_FAILURE);
  719. goto err;
  720. }
  721. if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
  722. SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB);
  723. goto err;
  724. }
  725. /* Set suite B flags if needed */
  726. X509_STORE_CTX_set_flags(xs_ctx,
  727. c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
  728. i = X509_verify_cert(xs_ctx);
  729. if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
  730. if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
  731. ERR_clear_error();
  732. i = 1;
  733. rv = 2;
  734. }
  735. if (i > 0)
  736. chain = X509_STORE_CTX_get1_chain(xs_ctx);
  737. if (i <= 0) {
  738. SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED);
  739. i = X509_STORE_CTX_get_error(xs_ctx);
  740. ERR_add_error_data(2, "Verify error:",
  741. X509_verify_cert_error_string(i));
  742. goto err;
  743. }
  744. /* Remove EE certificate from chain */
  745. x = sk_X509_shift(chain);
  746. X509_free(x);
  747. if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
  748. if (sk_X509_num(chain) > 0) {
  749. /* See if last cert is self signed */
  750. x = sk_X509_value(chain, sk_X509_num(chain) - 1);
  751. if (X509_get_extension_flags(x) & EXFLAG_SS) {
  752. x = sk_X509_pop(chain);
  753. X509_free(x);
  754. }
  755. }
  756. }
  757. /*
  758. * Check security level of all CA certificates: EE will have been checked
  759. * already.
  760. */
  761. for (i = 0; i < sk_X509_num(chain); i++) {
  762. x = sk_X509_value(chain, i);
  763. rv = ssl_security_cert(s, ctx, x, 0, 0);
  764. if (rv != 1) {
  765. SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, rv);
  766. sk_X509_pop_free(chain, X509_free);
  767. rv = 0;
  768. goto err;
  769. }
  770. }
  771. sk_X509_pop_free(cpk->chain, X509_free);
  772. cpk->chain = chain;
  773. if (rv == 0)
  774. rv = 1;
  775. err:
  776. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
  777. X509_STORE_free(chain_store);
  778. X509_STORE_CTX_free(xs_ctx);
  779. return rv;
  780. }
  781. int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
  782. {
  783. X509_STORE **pstore;
  784. if (chain)
  785. pstore = &c->chain_store;
  786. else
  787. pstore = &c->verify_store;
  788. X509_STORE_free(*pstore);
  789. *pstore = store;
  790. if (ref && store)
  791. X509_STORE_up_ref(store);
  792. return 1;
  793. }
  794. static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
  795. int op, int bits, int nid, void *other,
  796. void *ex)
  797. {
  798. int level, minbits;
  799. static const int minbits_table[5] = { 80, 112, 128, 192, 256 };
  800. if (ctx)
  801. level = SSL_CTX_get_security_level(ctx);
  802. else
  803. level = SSL_get_security_level(s);
  804. if (level <= 0) {
  805. /*
  806. * No EDH keys weaker than 1024-bits even at level 0, otherwise,
  807. * anything goes.
  808. */
  809. if (op == SSL_SECOP_TMP_DH && bits < 80)
  810. return 0;
  811. return 1;
  812. }
  813. if (level > 5)
  814. level = 5;
  815. minbits = minbits_table[level - 1];
  816. switch (op) {
  817. case SSL_SECOP_CIPHER_SUPPORTED:
  818. case SSL_SECOP_CIPHER_SHARED:
  819. case SSL_SECOP_CIPHER_CHECK:
  820. {
  821. const SSL_CIPHER *c = other;
  822. /* No ciphers below security level */
  823. if (bits < minbits)
  824. return 0;
  825. /* No unauthenticated ciphersuites */
  826. if (c->algorithm_auth & SSL_aNULL)
  827. return 0;
  828. /* No MD5 mac ciphersuites */
  829. if (c->algorithm_mac & SSL_MD5)
  830. return 0;
  831. /* SHA1 HMAC is 160 bits of security */
  832. if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
  833. return 0;
  834. /* Level 2: no RC4 */
  835. if (level >= 2 && c->algorithm_enc == SSL_RC4)
  836. return 0;
  837. /* Level 3: forward secure ciphersuites only */
  838. if (level >= 3 && c->min_tls != TLS1_3_VERSION &&
  839. !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH)))
  840. return 0;
  841. break;
  842. }
  843. case SSL_SECOP_VERSION:
  844. if (!SSL_IS_DTLS(s)) {
  845. /* SSLv3 not allowed at level 2 */
  846. if (nid <= SSL3_VERSION && level >= 2)
  847. return 0;
  848. /* TLS v1.1 and above only for level 3 */
  849. if (nid <= TLS1_VERSION && level >= 3)
  850. return 0;
  851. /* TLS v1.2 only for level 4 and above */
  852. if (nid <= TLS1_1_VERSION && level >= 4)
  853. return 0;
  854. } else {
  855. /* DTLS v1.2 only for level 4 and above */
  856. if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level >= 4)
  857. return 0;
  858. }
  859. break;
  860. case SSL_SECOP_COMPRESSION:
  861. if (level >= 2)
  862. return 0;
  863. break;
  864. case SSL_SECOP_TICKET:
  865. if (level >= 3)
  866. return 0;
  867. break;
  868. default:
  869. if (bits < minbits)
  870. return 0;
  871. }
  872. return 1;
  873. }
  874. int ssl_security(const SSL *s, int op, int bits, int nid, void *other)
  875. {
  876. return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex);
  877. }
  878. int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
  879. {
  880. return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
  881. ctx->cert->sec_ex);
  882. }
  883. int ssl_cert_lookup_by_nid(int nid, size_t *pidx)
  884. {
  885. size_t i;
  886. for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
  887. if (ssl_cert_info[i].nid == nid) {
  888. *pidx = i;
  889. return 1;
  890. }
  891. }
  892. return 0;
  893. }
  894. const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx)
  895. {
  896. int nid = EVP_PKEY_id(pk);
  897. size_t tmpidx;
  898. if (nid == NID_undef)
  899. return NULL;
  900. if (!ssl_cert_lookup_by_nid(nid, &tmpidx))
  901. return NULL;
  902. if (pidx != NULL)
  903. *pidx = tmpidx;
  904. return &ssl_cert_info[tmpidx];
  905. }
  906. const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx)
  907. {
  908. if (idx >= OSSL_NELEM(ssl_cert_info))
  909. return NULL;
  910. return &ssl_cert_info[idx];
  911. }