ssl_cert.c 27 KB

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