ssl_cert.c 28 KB

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