ssl_cert.c 28 KB

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