ssl_cert.c 27 KB

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