ssl_conf.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. * Copyright 2012-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "ssl_local.h"
  11. #include <openssl/conf.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/dh.h>
  14. #include "internal/nelem.h"
  15. /*
  16. * structure holding name tables. This is used for permitted elements in lists
  17. * such as TLSv1.
  18. */
  19. typedef struct {
  20. const char *name;
  21. int namelen;
  22. unsigned int name_flags;
  23. unsigned long option_value;
  24. } ssl_flag_tbl;
  25. /* Switch table: use for single command line switches like no_tls2 */
  26. typedef struct {
  27. unsigned long option_value;
  28. unsigned int name_flags;
  29. } ssl_switch_tbl;
  30. /* Sense of name is inverted e.g. "TLSv1" will clear SSL_OP_NO_TLSv1 */
  31. #define SSL_TFLAG_INV 0x1
  32. /* Mask for type of flag referred to */
  33. #define SSL_TFLAG_TYPE_MASK 0xf00
  34. /* Flag is for options */
  35. #define SSL_TFLAG_OPTION 0x000
  36. /* Flag is for cert_flags */
  37. #define SSL_TFLAG_CERT 0x100
  38. /* Flag is for verify mode */
  39. #define SSL_TFLAG_VFY 0x200
  40. /* Option can only be used for clients */
  41. #define SSL_TFLAG_CLIENT SSL_CONF_FLAG_CLIENT
  42. /* Option can only be used for servers */
  43. #define SSL_TFLAG_SERVER SSL_CONF_FLAG_SERVER
  44. #define SSL_TFLAG_BOTH (SSL_TFLAG_CLIENT|SSL_TFLAG_SERVER)
  45. #define SSL_FLAG_TBL(str, flag) \
  46. {str, (int)(sizeof(str) - 1), SSL_TFLAG_BOTH, flag}
  47. #define SSL_FLAG_TBL_SRV(str, flag) \
  48. {str, (int)(sizeof(str) - 1), SSL_TFLAG_SERVER, flag}
  49. #define SSL_FLAG_TBL_CLI(str, flag) \
  50. {str, (int)(sizeof(str) - 1), SSL_TFLAG_CLIENT, flag}
  51. #define SSL_FLAG_TBL_INV(str, flag) \
  52. {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_BOTH, flag}
  53. #define SSL_FLAG_TBL_SRV_INV(str, flag) \
  54. {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_SERVER, flag}
  55. #define SSL_FLAG_TBL_CERT(str, flag) \
  56. {str, (int)(sizeof(str) - 1), SSL_TFLAG_CERT|SSL_TFLAG_BOTH, flag}
  57. #define SSL_FLAG_VFY_CLI(str, flag) \
  58. {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_CLIENT, flag}
  59. #define SSL_FLAG_VFY_SRV(str, flag) \
  60. {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_SERVER, flag}
  61. /*
  62. * Opaque structure containing SSL configuration context.
  63. */
  64. struct ssl_conf_ctx_st {
  65. /*
  66. * Various flags indicating (among other things) which options we will
  67. * recognise.
  68. */
  69. unsigned int flags;
  70. /* Prefix and length of commands */
  71. char *prefix;
  72. size_t prefixlen;
  73. /* SSL_CTX or SSL structure to perform operations on */
  74. SSL_CTX *ctx;
  75. SSL *ssl;
  76. /* Pointer to SSL or SSL_CTX options field or NULL if none */
  77. uint32_t *poptions;
  78. /* Certificate filenames for each type */
  79. char *cert_filename[SSL_PKEY_NUM];
  80. /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */
  81. uint32_t *pcert_flags;
  82. /* Pointer to SSL or SSL_CTX verify_mode or NULL if none */
  83. uint32_t *pvfy_flags;
  84. /* Pointer to SSL or SSL_CTX min_version field or NULL if none */
  85. int *min_version;
  86. /* Pointer to SSL or SSL_CTX max_version field or NULL if none */
  87. int *max_version;
  88. /* Current flag table being worked on */
  89. const ssl_flag_tbl *tbl;
  90. /* Size of table */
  91. size_t ntbl;
  92. /* Client CA names */
  93. STACK_OF(X509_NAME) *canames;
  94. };
  95. static void ssl_set_option(SSL_CONF_CTX *cctx, unsigned int name_flags,
  96. unsigned long option_value, int onoff)
  97. {
  98. uint32_t *pflags;
  99. if (cctx->poptions == NULL)
  100. return;
  101. if (name_flags & SSL_TFLAG_INV)
  102. onoff ^= 1;
  103. switch (name_flags & SSL_TFLAG_TYPE_MASK) {
  104. case SSL_TFLAG_CERT:
  105. pflags = cctx->pcert_flags;
  106. break;
  107. case SSL_TFLAG_VFY:
  108. pflags = cctx->pvfy_flags;
  109. break;
  110. case SSL_TFLAG_OPTION:
  111. pflags = cctx->poptions;
  112. break;
  113. default:
  114. return;
  115. }
  116. if (onoff)
  117. *pflags |= option_value;
  118. else
  119. *pflags &= ~option_value;
  120. }
  121. static int ssl_match_option(SSL_CONF_CTX *cctx, const ssl_flag_tbl *tbl,
  122. const char *name, int namelen, int onoff)
  123. {
  124. /* If name not relevant for context skip */
  125. if (!(cctx->flags & tbl->name_flags & SSL_TFLAG_BOTH))
  126. return 0;
  127. if (namelen == -1) {
  128. if (strcmp(tbl->name, name))
  129. return 0;
  130. } else if (tbl->namelen != namelen || strncasecmp(tbl->name, name, namelen))
  131. return 0;
  132. ssl_set_option(cctx, tbl->name_flags, tbl->option_value, onoff);
  133. return 1;
  134. }
  135. static int ssl_set_option_list(const char *elem, int len, void *usr)
  136. {
  137. SSL_CONF_CTX *cctx = usr;
  138. size_t i;
  139. const ssl_flag_tbl *tbl;
  140. int onoff = 1;
  141. /*
  142. * len == -1 indicates not being called in list context, just for single
  143. * command line switches, so don't allow +, -.
  144. */
  145. if (elem == NULL)
  146. return 0;
  147. if (len != -1) {
  148. if (*elem == '+') {
  149. elem++;
  150. len--;
  151. onoff = 1;
  152. } else if (*elem == '-') {
  153. elem++;
  154. len--;
  155. onoff = 0;
  156. }
  157. }
  158. for (i = 0, tbl = cctx->tbl; i < cctx->ntbl; i++, tbl++) {
  159. if (ssl_match_option(cctx, tbl, elem, len, onoff))
  160. return 1;
  161. }
  162. return 0;
  163. }
  164. /* Set supported signature algorithms */
  165. static int cmd_SignatureAlgorithms(SSL_CONF_CTX *cctx, const char *value)
  166. {
  167. int rv;
  168. if (cctx->ssl)
  169. rv = SSL_set1_sigalgs_list(cctx->ssl, value);
  170. /* NB: ctx == NULL performs syntax checking only */
  171. else
  172. rv = SSL_CTX_set1_sigalgs_list(cctx->ctx, value);
  173. return rv > 0;
  174. }
  175. /* Set supported client signature algorithms */
  176. static int cmd_ClientSignatureAlgorithms(SSL_CONF_CTX *cctx, const char *value)
  177. {
  178. int rv;
  179. if (cctx->ssl)
  180. rv = SSL_set1_client_sigalgs_list(cctx->ssl, value);
  181. /* NB: ctx == NULL performs syntax checking only */
  182. else
  183. rv = SSL_CTX_set1_client_sigalgs_list(cctx->ctx, value);
  184. return rv > 0;
  185. }
  186. static int cmd_Groups(SSL_CONF_CTX *cctx, const char *value)
  187. {
  188. int rv;
  189. if (cctx->ssl)
  190. rv = SSL_set1_groups_list(cctx->ssl, value);
  191. /* NB: ctx == NULL performs syntax checking only */
  192. else
  193. rv = SSL_CTX_set1_groups_list(cctx->ctx, value);
  194. return rv > 0;
  195. }
  196. /* This is the old name for cmd_Groups - retained for backwards compatibility */
  197. static int cmd_Curves(SSL_CONF_CTX *cctx, const char *value)
  198. {
  199. return cmd_Groups(cctx, value);
  200. }
  201. #ifndef OPENSSL_NO_EC
  202. /* ECDH temporary parameters */
  203. static int cmd_ECDHParameters(SSL_CONF_CTX *cctx, const char *value)
  204. {
  205. int rv = 1;
  206. EC_KEY *ecdh;
  207. int nid;
  208. /* Ignore values supported by 1.0.2 for the automatic selection */
  209. if ((cctx->flags & SSL_CONF_FLAG_FILE)
  210. && (strcasecmp(value, "+automatic") == 0
  211. || strcasecmp(value, "automatic") == 0))
  212. return 1;
  213. if ((cctx->flags & SSL_CONF_FLAG_CMDLINE) &&
  214. strcmp(value, "auto") == 0)
  215. return 1;
  216. nid = EC_curve_nist2nid(value);
  217. if (nid == NID_undef)
  218. nid = OBJ_sn2nid(value);
  219. if (nid == 0)
  220. return 0;
  221. ecdh = EC_KEY_new_by_curve_name(nid);
  222. if (!ecdh)
  223. return 0;
  224. if (cctx->ctx)
  225. rv = SSL_CTX_set_tmp_ecdh(cctx->ctx, ecdh);
  226. else if (cctx->ssl)
  227. rv = SSL_set_tmp_ecdh(cctx->ssl, ecdh);
  228. EC_KEY_free(ecdh);
  229. return rv > 0;
  230. }
  231. #endif
  232. static int cmd_CipherString(SSL_CONF_CTX *cctx, const char *value)
  233. {
  234. int rv = 1;
  235. if (cctx->ctx)
  236. rv = SSL_CTX_set_cipher_list(cctx->ctx, value);
  237. if (cctx->ssl)
  238. rv = SSL_set_cipher_list(cctx->ssl, value);
  239. return rv > 0;
  240. }
  241. static int cmd_Ciphersuites(SSL_CONF_CTX *cctx, const char *value)
  242. {
  243. int rv = 1;
  244. if (cctx->ctx)
  245. rv = SSL_CTX_set_ciphersuites(cctx->ctx, value);
  246. if (cctx->ssl)
  247. rv = SSL_set_ciphersuites(cctx->ssl, value);
  248. return rv > 0;
  249. }
  250. static int cmd_Protocol(SSL_CONF_CTX *cctx, const char *value)
  251. {
  252. static const ssl_flag_tbl ssl_protocol_list[] = {
  253. SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK),
  254. SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2),
  255. SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3),
  256. SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1),
  257. SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1),
  258. SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2),
  259. SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3),
  260. SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1),
  261. SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2)
  262. };
  263. cctx->tbl = ssl_protocol_list;
  264. cctx->ntbl = OSSL_NELEM(ssl_protocol_list);
  265. return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
  266. }
  267. /*
  268. * protocol_from_string - converts a protocol version string to a number
  269. *
  270. * Returns -1 on failure or the version on success
  271. */
  272. static int protocol_from_string(const char *value)
  273. {
  274. struct protocol_versions {
  275. const char *name;
  276. int version;
  277. };
  278. /*
  279. * Note: To avoid breaking previously valid configurations, we must retain
  280. * legacy entries in this table even if the underlying protocol is no
  281. * longer supported. This also means that the constants SSL3_VERSION, ...
  282. * need to be retained indefinitely. This table can only grow, never
  283. * shrink.
  284. */
  285. static const struct protocol_versions versions[] = {
  286. {"None", 0},
  287. {"SSLv3", SSL3_VERSION},
  288. {"TLSv1", TLS1_VERSION},
  289. {"TLSv1.1", TLS1_1_VERSION},
  290. {"TLSv1.2", TLS1_2_VERSION},
  291. {"TLSv1.3", TLS1_3_VERSION},
  292. {"DTLSv1", DTLS1_VERSION},
  293. {"DTLSv1.2", DTLS1_2_VERSION}
  294. };
  295. size_t i;
  296. size_t n = OSSL_NELEM(versions);
  297. for (i = 0; i < n; i++)
  298. if (strcmp(versions[i].name, value) == 0)
  299. return versions[i].version;
  300. return -1;
  301. }
  302. static int min_max_proto(SSL_CONF_CTX *cctx, const char *value, int *bound)
  303. {
  304. int method_version;
  305. int new_version;
  306. if (cctx->ctx != NULL)
  307. method_version = cctx->ctx->method->version;
  308. else if (cctx->ssl != NULL)
  309. method_version = cctx->ssl->ctx->method->version;
  310. else
  311. return 0;
  312. if ((new_version = protocol_from_string(value)) < 0)
  313. return 0;
  314. return ssl_set_version_bound(method_version, new_version, bound);
  315. }
  316. /*
  317. * cmd_MinProtocol - Set min protocol version
  318. * @cctx: config structure to save settings in
  319. * @value: The min protocol version in string form
  320. *
  321. * Returns 1 on success and 0 on failure.
  322. */
  323. static int cmd_MinProtocol(SSL_CONF_CTX *cctx, const char *value)
  324. {
  325. return min_max_proto(cctx, value, cctx->min_version);
  326. }
  327. /*
  328. * cmd_MaxProtocol - Set max protocol version
  329. * @cctx: config structure to save settings in
  330. * @value: The max protocol version in string form
  331. *
  332. * Returns 1 on success and 0 on failure.
  333. */
  334. static int cmd_MaxProtocol(SSL_CONF_CTX *cctx, const char *value)
  335. {
  336. return min_max_proto(cctx, value, cctx->max_version);
  337. }
  338. static int cmd_Options(SSL_CONF_CTX *cctx, const char *value)
  339. {
  340. static const ssl_flag_tbl ssl_option_list[] = {
  341. SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
  342. SSL_FLAG_TBL_INV("EmptyFragments",
  343. SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
  344. SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
  345. SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
  346. SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE),
  347. SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
  348. SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION),
  349. SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE),
  350. SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE),
  351. SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
  352. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
  353. SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC),
  354. SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION),
  355. SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX),
  356. SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA),
  357. SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT),
  358. SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY)
  359. };
  360. if (value == NULL)
  361. return -3;
  362. cctx->tbl = ssl_option_list;
  363. cctx->ntbl = OSSL_NELEM(ssl_option_list);
  364. return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
  365. }
  366. static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value)
  367. {
  368. static const ssl_flag_tbl ssl_vfy_list[] = {
  369. SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER),
  370. SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER),
  371. SSL_FLAG_VFY_SRV("Require",
  372. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  373. SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE),
  374. SSL_FLAG_VFY_SRV("RequestPostHandshake",
  375. SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE),
  376. SSL_FLAG_VFY_SRV("RequirePostHandshake",
  377. SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE |
  378. SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  379. };
  380. if (value == NULL)
  381. return -3;
  382. cctx->tbl = ssl_vfy_list;
  383. cctx->ntbl = OSSL_NELEM(ssl_vfy_list);
  384. return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
  385. }
  386. static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
  387. {
  388. int rv = 1;
  389. CERT *c = NULL;
  390. if (cctx->ctx) {
  391. rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
  392. c = cctx->ctx->cert;
  393. }
  394. if (cctx->ssl) {
  395. rv = SSL_use_certificate_chain_file(cctx->ssl, value);
  396. c = cctx->ssl->cert;
  397. }
  398. if (rv > 0 && c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
  399. char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
  400. OPENSSL_free(*pfilename);
  401. *pfilename = OPENSSL_strdup(value);
  402. if (!*pfilename)
  403. rv = 0;
  404. }
  405. return rv > 0;
  406. }
  407. static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
  408. {
  409. int rv = 1;
  410. if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
  411. return -2;
  412. if (cctx->ctx)
  413. rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
  414. if (cctx->ssl)
  415. rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
  416. return rv > 0;
  417. }
  418. static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
  419. {
  420. int rv = 1;
  421. if (cctx->ctx)
  422. rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
  423. return rv > 0;
  424. }
  425. static int do_store(SSL_CONF_CTX *cctx,
  426. const char *CAfile, const char *CApath, int verify_store)
  427. {
  428. CERT *cert;
  429. X509_STORE **st;
  430. if (cctx->ctx)
  431. cert = cctx->ctx->cert;
  432. else if (cctx->ssl)
  433. cert = cctx->ssl->cert;
  434. else
  435. return 1;
  436. st = verify_store ? &cert->verify_store : &cert->chain_store;
  437. if (*st == NULL) {
  438. *st = X509_STORE_new();
  439. if (*st == NULL)
  440. return 0;
  441. }
  442. return X509_STORE_load_locations(*st, CAfile, CApath) > 0;
  443. }
  444. static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value)
  445. {
  446. return do_store(cctx, NULL, value, 0);
  447. }
  448. static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value)
  449. {
  450. return do_store(cctx, value, NULL, 0);
  451. }
  452. static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value)
  453. {
  454. return do_store(cctx, NULL, value, 1);
  455. }
  456. static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value)
  457. {
  458. return do_store(cctx, value, NULL, 1);
  459. }
  460. static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value)
  461. {
  462. if (cctx->canames == NULL)
  463. cctx->canames = sk_X509_NAME_new_null();
  464. if (cctx->canames == NULL)
  465. return 0;
  466. return SSL_add_file_cert_subjects_to_stack(cctx->canames, value);
  467. }
  468. static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value)
  469. {
  470. return cmd_RequestCAFile(cctx, value);
  471. }
  472. static int cmd_RequestCAPath(SSL_CONF_CTX *cctx, const char *value)
  473. {
  474. if (cctx->canames == NULL)
  475. cctx->canames = sk_X509_NAME_new_null();
  476. if (cctx->canames == NULL)
  477. return 0;
  478. return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value);
  479. }
  480. static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value)
  481. {
  482. return cmd_RequestCAPath(cctx, value);
  483. }
  484. #ifndef OPENSSL_NO_DH
  485. static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
  486. {
  487. int rv = 0;
  488. DH *dh = NULL;
  489. BIO *in = NULL;
  490. if (cctx->ctx || cctx->ssl) {
  491. in = BIO_new(BIO_s_file());
  492. if (in == NULL)
  493. goto end;
  494. if (BIO_read_filename(in, value) <= 0)
  495. goto end;
  496. dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
  497. if (dh == NULL)
  498. goto end;
  499. } else
  500. return 1;
  501. if (cctx->ctx)
  502. rv = SSL_CTX_set_tmp_dh(cctx->ctx, dh);
  503. if (cctx->ssl)
  504. rv = SSL_set_tmp_dh(cctx->ssl, dh);
  505. end:
  506. DH_free(dh);
  507. BIO_free(in);
  508. return rv > 0;
  509. }
  510. #endif
  511. static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value)
  512. {
  513. int rv = 0;
  514. int block_size = atoi(value);
  515. /*
  516. * All we care about is a non-negative value,
  517. * the setters check the range
  518. */
  519. if (block_size >= 0) {
  520. if (cctx->ctx)
  521. rv = SSL_CTX_set_block_padding(cctx->ctx, block_size);
  522. if (cctx->ssl)
  523. rv = SSL_set_block_padding(cctx->ssl, block_size);
  524. }
  525. return rv;
  526. }
  527. static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value)
  528. {
  529. int rv = 0;
  530. int num_tickets = atoi(value);
  531. if (num_tickets >= 0) {
  532. if (cctx->ctx)
  533. rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets);
  534. if (cctx->ssl)
  535. rv = SSL_set_num_tickets(cctx->ssl, num_tickets);
  536. }
  537. return rv;
  538. }
  539. typedef struct {
  540. int (*cmd) (SSL_CONF_CTX *cctx, const char *value);
  541. const char *str_file;
  542. const char *str_cmdline;
  543. unsigned short flags;
  544. unsigned short value_type;
  545. } ssl_conf_cmd_tbl;
  546. /* Table of supported parameters */
  547. #define SSL_CONF_CMD(name, cmdopt, flags, type) \
  548. {cmd_##name, #name, cmdopt, flags, type}
  549. #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
  550. SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
  551. #define SSL_CONF_CMD_SWITCH(name, flags) \
  552. {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
  553. /* See apps/apps.h if you change this table. */
  554. static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
  555. SSL_CONF_CMD_SWITCH("no_ssl3", 0),
  556. SSL_CONF_CMD_SWITCH("no_tls1", 0),
  557. SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
  558. SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
  559. SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
  560. SSL_CONF_CMD_SWITCH("bugs", 0),
  561. SSL_CONF_CMD_SWITCH("no_comp", 0),
  562. SSL_CONF_CMD_SWITCH("comp", 0),
  563. SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER),
  564. SSL_CONF_CMD_SWITCH("no_ticket", 0),
  565. SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER),
  566. SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
  567. SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_SERVER),
  568. SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
  569. SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER),
  570. SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_SERVER),
  571. SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
  572. SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER),
  573. SSL_CONF_CMD_SWITCH("strict", 0),
  574. SSL_CONF_CMD_SWITCH("no_middlebox", 0),
  575. SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER),
  576. SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER),
  577. SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0),
  578. SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
  579. SSL_CONF_CMD_STRING(Curves, "curves", 0),
  580. SSL_CONF_CMD_STRING(Groups, "groups", 0),
  581. #ifndef OPENSSL_NO_EC
  582. SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
  583. #endif
  584. SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
  585. SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0),
  586. SSL_CONF_CMD_STRING(Protocol, NULL, 0),
  587. SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
  588. SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
  589. SSL_CONF_CMD_STRING(Options, NULL, 0),
  590. SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
  591. SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
  592. SSL_CONF_TYPE_FILE),
  593. SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
  594. SSL_CONF_TYPE_FILE),
  595. SSL_CONF_CMD(ServerInfoFile, NULL,
  596. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  597. SSL_CONF_TYPE_FILE),
  598. SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
  599. SSL_CONF_TYPE_DIR),
  600. SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
  601. SSL_CONF_TYPE_FILE),
  602. SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
  603. SSL_CONF_TYPE_DIR),
  604. SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
  605. SSL_CONF_TYPE_FILE),
  606. SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
  607. SSL_CONF_TYPE_FILE),
  608. SSL_CONF_CMD(ClientCAFile, NULL,
  609. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  610. SSL_CONF_TYPE_FILE),
  611. SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
  612. SSL_CONF_TYPE_DIR),
  613. SSL_CONF_CMD(ClientCAPath, NULL,
  614. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  615. SSL_CONF_TYPE_DIR),
  616. #ifndef OPENSSL_NO_DH
  617. SSL_CONF_CMD(DHParameters, "dhparam",
  618. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  619. SSL_CONF_TYPE_FILE),
  620. #endif
  621. SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0),
  622. SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER),
  623. };
  624. /* Supported switches: must match order of switches in ssl_conf_cmds */
  625. static const ssl_switch_tbl ssl_cmd_switches[] = {
  626. {SSL_OP_NO_SSLv3, 0}, /* no_ssl3 */
  627. {SSL_OP_NO_TLSv1, 0}, /* no_tls1 */
  628. {SSL_OP_NO_TLSv1_1, 0}, /* no_tls1_1 */
  629. {SSL_OP_NO_TLSv1_2, 0}, /* no_tls1_2 */
  630. {SSL_OP_NO_TLSv1_3, 0}, /* no_tls1_3 */
  631. {SSL_OP_ALL, 0}, /* bugs */
  632. {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */
  633. {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */
  634. {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */
  635. {SSL_OP_NO_TICKET, 0}, /* no_ticket */
  636. {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */
  637. /* legacy_renegotiation */
  638. {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0},
  639. /* legacy_server_connect */
  640. {SSL_OP_LEGACY_SERVER_CONNECT, 0},
  641. /* no_renegotiation */
  642. {SSL_OP_NO_RENEGOTIATION, 0},
  643. /* no_resumption_on_reneg */
  644. {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0},
  645. /* no_legacy_server_connect */
  646. {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV},
  647. /* allow_no_dhe_kex */
  648. {SSL_OP_ALLOW_NO_DHE_KEX, 0},
  649. /* chacha reprioritization */
  650. {SSL_OP_PRIORITIZE_CHACHA, 0},
  651. {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */
  652. /* no_middlebox */
  653. {SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV},
  654. /* anti_replay */
  655. {SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV},
  656. /* no_anti_replay */
  657. {SSL_OP_NO_ANTI_REPLAY, 0},
  658. };
  659. static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
  660. {
  661. if (!pcmd || !*pcmd)
  662. return 0;
  663. /* If a prefix is set, check and skip */
  664. if (cctx->prefix) {
  665. if (strlen(*pcmd) <= cctx->prefixlen)
  666. return 0;
  667. if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
  668. strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
  669. return 0;
  670. if (cctx->flags & SSL_CONF_FLAG_FILE &&
  671. strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
  672. return 0;
  673. *pcmd += cctx->prefixlen;
  674. } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
  675. if (**pcmd != '-' || !(*pcmd)[1])
  676. return 0;
  677. *pcmd += 1;
  678. }
  679. return 1;
  680. }
  681. /* Determine if a command is allowed according to cctx flags */
  682. static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * t)
  683. {
  684. unsigned int tfl = t->flags;
  685. unsigned int cfl = cctx->flags;
  686. if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
  687. return 0;
  688. if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
  689. return 0;
  690. if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
  691. && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
  692. return 0;
  693. return 1;
  694. }
  695. static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
  696. const char *cmd)
  697. {
  698. const ssl_conf_cmd_tbl *t;
  699. size_t i;
  700. if (cmd == NULL)
  701. return NULL;
  702. /* Look for matching parameter name in table */
  703. for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
  704. if (ssl_conf_cmd_allowed(cctx, t)) {
  705. if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
  706. if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
  707. return t;
  708. }
  709. if (cctx->flags & SSL_CONF_FLAG_FILE) {
  710. if (t->str_file && strcasecmp(t->str_file, cmd) == 0)
  711. return t;
  712. }
  713. }
  714. }
  715. return NULL;
  716. }
  717. static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * cmd)
  718. {
  719. /* Find index of command in table */
  720. size_t idx = cmd - ssl_conf_cmds;
  721. const ssl_switch_tbl *scmd;
  722. /* Sanity check index */
  723. if (idx >= OSSL_NELEM(ssl_cmd_switches))
  724. return 0;
  725. /* Obtain switches entry with same index */
  726. scmd = ssl_cmd_switches + idx;
  727. ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
  728. return 1;
  729. }
  730. int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
  731. {
  732. const ssl_conf_cmd_tbl *runcmd;
  733. if (cmd == NULL) {
  734. SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_INVALID_NULL_CMD_NAME);
  735. return 0;
  736. }
  737. if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
  738. return -2;
  739. runcmd = ssl_conf_cmd_lookup(cctx, cmd);
  740. if (runcmd) {
  741. int rv;
  742. if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
  743. return ctrl_switch_option(cctx, runcmd);
  744. }
  745. if (value == NULL)
  746. return -3;
  747. rv = runcmd->cmd(cctx, value);
  748. if (rv > 0)
  749. return 2;
  750. if (rv == -2)
  751. return -2;
  752. if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
  753. SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_BAD_VALUE);
  754. ERR_add_error_data(4, "cmd=", cmd, ", value=", value);
  755. }
  756. return 0;
  757. }
  758. if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
  759. SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_UNKNOWN_CMD_NAME);
  760. ERR_add_error_data(2, "cmd=", cmd);
  761. }
  762. return -2;
  763. }
  764. int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
  765. {
  766. int rv;
  767. const char *arg = NULL, *argn;
  768. if (pargc && *pargc == 0)
  769. return 0;
  770. if (!pargc || *pargc > 0)
  771. arg = **pargv;
  772. if (arg == NULL)
  773. return 0;
  774. if (!pargc || *pargc > 1)
  775. argn = (*pargv)[1];
  776. else
  777. argn = NULL;
  778. cctx->flags &= ~SSL_CONF_FLAG_FILE;
  779. cctx->flags |= SSL_CONF_FLAG_CMDLINE;
  780. rv = SSL_CONF_cmd(cctx, arg, argn);
  781. if (rv > 0) {
  782. /* Success: update pargc, pargv */
  783. (*pargv) += rv;
  784. if (pargc)
  785. (*pargc) -= rv;
  786. return rv;
  787. }
  788. /* Unknown switch: indicate no arguments processed */
  789. if (rv == -2)
  790. return 0;
  791. /* Some error occurred processing command, return fatal error */
  792. if (rv == 0)
  793. return -1;
  794. return rv;
  795. }
  796. int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
  797. {
  798. if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
  799. const ssl_conf_cmd_tbl *runcmd;
  800. runcmd = ssl_conf_cmd_lookup(cctx, cmd);
  801. if (runcmd)
  802. return runcmd->value_type;
  803. }
  804. return SSL_CONF_TYPE_UNKNOWN;
  805. }
  806. SSL_CONF_CTX *SSL_CONF_CTX_new(void)
  807. {
  808. SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
  809. return ret;
  810. }
  811. int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
  812. {
  813. /* See if any certificates are missing private keys */
  814. size_t i;
  815. CERT *c = NULL;
  816. if (cctx->ctx)
  817. c = cctx->ctx->cert;
  818. else if (cctx->ssl)
  819. c = cctx->ssl->cert;
  820. if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
  821. for (i = 0; i < SSL_PKEY_NUM; i++) {
  822. const char *p = cctx->cert_filename[i];
  823. /*
  824. * If missing private key try to load one from certificate file
  825. */
  826. if (p && !c->pkeys[i].privatekey) {
  827. if (!cmd_PrivateKey(cctx, p))
  828. return 0;
  829. }
  830. }
  831. }
  832. if (cctx->canames) {
  833. if (cctx->ssl)
  834. SSL_set0_CA_list(cctx->ssl, cctx->canames);
  835. else if (cctx->ctx)
  836. SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
  837. else
  838. sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
  839. cctx->canames = NULL;
  840. }
  841. return 1;
  842. }
  843. void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
  844. {
  845. if (cctx) {
  846. size_t i;
  847. for (i = 0; i < SSL_PKEY_NUM; i++)
  848. OPENSSL_free(cctx->cert_filename[i]);
  849. OPENSSL_free(cctx->prefix);
  850. sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
  851. OPENSSL_free(cctx);
  852. }
  853. }
  854. unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
  855. {
  856. cctx->flags |= flags;
  857. return cctx->flags;
  858. }
  859. unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
  860. {
  861. cctx->flags &= ~flags;
  862. return cctx->flags;
  863. }
  864. int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
  865. {
  866. char *tmp = NULL;
  867. if (pre) {
  868. tmp = OPENSSL_strdup(pre);
  869. if (tmp == NULL)
  870. return 0;
  871. }
  872. OPENSSL_free(cctx->prefix);
  873. cctx->prefix = tmp;
  874. if (tmp)
  875. cctx->prefixlen = strlen(tmp);
  876. else
  877. cctx->prefixlen = 0;
  878. return 1;
  879. }
  880. void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
  881. {
  882. cctx->ssl = ssl;
  883. cctx->ctx = NULL;
  884. if (ssl) {
  885. cctx->poptions = &ssl->options;
  886. cctx->min_version = &ssl->min_proto_version;
  887. cctx->max_version = &ssl->max_proto_version;
  888. cctx->pcert_flags = &ssl->cert->cert_flags;
  889. cctx->pvfy_flags = &ssl->verify_mode;
  890. } else {
  891. cctx->poptions = NULL;
  892. cctx->min_version = NULL;
  893. cctx->max_version = NULL;
  894. cctx->pcert_flags = NULL;
  895. cctx->pvfy_flags = NULL;
  896. }
  897. }
  898. void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
  899. {
  900. cctx->ctx = ctx;
  901. cctx->ssl = NULL;
  902. if (ctx) {
  903. cctx->poptions = &ctx->options;
  904. cctx->min_version = &ctx->min_proto_version;
  905. cctx->max_version = &ctx->max_proto_version;
  906. cctx->pcert_flags = &ctx->cert->cert_flags;
  907. cctx->pvfy_flags = &ctx->verify_mode;
  908. } else {
  909. cctx->poptions = NULL;
  910. cctx->min_version = NULL;
  911. cctx->max_version = NULL;
  912. cctx->pcert_flags = NULL;
  913. cctx->pvfy_flags = NULL;
  914. }
  915. }