2
0

ssl_conf.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /*
  2. * Copyright 2012-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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_locl.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. int nid;
  207. /* Ignore values supported by 1.0.2 for the automatic selection */
  208. if ((cctx->flags & SSL_CONF_FLAG_FILE)
  209. && (strcasecmp(value, "+automatic") == 0
  210. || strcasecmp(value, "automatic") == 0))
  211. return 1;
  212. if ((cctx->flags & SSL_CONF_FLAG_CMDLINE) &&
  213. strcmp(value, "auto") == 0)
  214. return 1;
  215. nid = EC_curve_nist2nid(value);
  216. if (nid == NID_undef)
  217. nid = OBJ_sn2nid(value);
  218. if (nid == 0)
  219. return 0;
  220. if (cctx->ctx)
  221. rv = SSL_CTX_set1_groups(cctx->ctx, &nid, 1);
  222. else if (cctx->ssl)
  223. rv = SSL_set1_groups(cctx->ssl, &nid, 1);
  224. return rv > 0;
  225. }
  226. #endif
  227. static int cmd_CipherString(SSL_CONF_CTX *cctx, const char *value)
  228. {
  229. int rv = 1;
  230. if (cctx->ctx)
  231. rv = SSL_CTX_set_cipher_list(cctx->ctx, value);
  232. if (cctx->ssl)
  233. rv = SSL_set_cipher_list(cctx->ssl, value);
  234. return rv > 0;
  235. }
  236. static int cmd_Ciphersuites(SSL_CONF_CTX *cctx, const char *value)
  237. {
  238. int rv = 1;
  239. if (cctx->ctx)
  240. rv = SSL_CTX_set_ciphersuites(cctx->ctx, value);
  241. if (cctx->ssl)
  242. rv = SSL_set_ciphersuites(cctx->ssl, value);
  243. return rv > 0;
  244. }
  245. static int cmd_Protocol(SSL_CONF_CTX *cctx, const char *value)
  246. {
  247. static const ssl_flag_tbl ssl_protocol_list[] = {
  248. SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK),
  249. SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2),
  250. SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3),
  251. SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1),
  252. SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1),
  253. SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2),
  254. SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3),
  255. SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1),
  256. SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2)
  257. };
  258. cctx->tbl = ssl_protocol_list;
  259. cctx->ntbl = OSSL_NELEM(ssl_protocol_list);
  260. return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
  261. }
  262. /*
  263. * protocol_from_string - converts a protocol version string to a number
  264. *
  265. * Returns -1 on failure or the version on success
  266. */
  267. static int protocol_from_string(const char *value)
  268. {
  269. struct protocol_versions {
  270. const char *name;
  271. int version;
  272. };
  273. static const struct protocol_versions versions[] = {
  274. {"None", 0},
  275. {"SSLv3", SSL3_VERSION},
  276. {"TLSv1", TLS1_VERSION},
  277. {"TLSv1.1", TLS1_1_VERSION},
  278. {"TLSv1.2", TLS1_2_VERSION},
  279. {"TLSv1.3", TLS1_3_VERSION},
  280. {"DTLSv1", DTLS1_VERSION},
  281. {"DTLSv1.2", DTLS1_2_VERSION}
  282. };
  283. size_t i;
  284. size_t n = OSSL_NELEM(versions);
  285. for (i = 0; i < n; i++)
  286. if (strcmp(versions[i].name, value) == 0)
  287. return versions[i].version;
  288. return -1;
  289. }
  290. static int min_max_proto(SSL_CONF_CTX *cctx, const char *value, int *bound)
  291. {
  292. int method_version;
  293. int new_version;
  294. if (cctx->ctx != NULL)
  295. method_version = cctx->ctx->method->version;
  296. else if (cctx->ssl != NULL)
  297. method_version = cctx->ssl->ctx->method->version;
  298. else
  299. return 0;
  300. if ((new_version = protocol_from_string(value)) < 0)
  301. return 0;
  302. return ssl_set_version_bound(method_version, new_version, bound);
  303. }
  304. /*
  305. * cmd_MinProtocol - Set min protocol version
  306. * @cctx: config structure to save settings in
  307. * @value: The min protocol version in string form
  308. *
  309. * Returns 1 on success and 0 on failure.
  310. */
  311. static int cmd_MinProtocol(SSL_CONF_CTX *cctx, const char *value)
  312. {
  313. return min_max_proto(cctx, value, cctx->min_version);
  314. }
  315. /*
  316. * cmd_MaxProtocol - Set max protocol version
  317. * @cctx: config structure to save settings in
  318. * @value: The max protocol version in string form
  319. *
  320. * Returns 1 on success and 0 on failure.
  321. */
  322. static int cmd_MaxProtocol(SSL_CONF_CTX *cctx, const char *value)
  323. {
  324. return min_max_proto(cctx, value, cctx->max_version);
  325. }
  326. static int cmd_Options(SSL_CONF_CTX *cctx, const char *value)
  327. {
  328. static const ssl_flag_tbl ssl_option_list[] = {
  329. SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
  330. SSL_FLAG_TBL_INV("EmptyFragments",
  331. SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
  332. SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
  333. SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
  334. SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE),
  335. SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
  336. SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION),
  337. SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE),
  338. SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE),
  339. SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
  340. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
  341. SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC),
  342. SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION),
  343. SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX),
  344. SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA),
  345. SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT),
  346. SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY)
  347. };
  348. if (value == NULL)
  349. return -3;
  350. cctx->tbl = ssl_option_list;
  351. cctx->ntbl = OSSL_NELEM(ssl_option_list);
  352. return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
  353. }
  354. static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value)
  355. {
  356. static const ssl_flag_tbl ssl_vfy_list[] = {
  357. SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER),
  358. SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER),
  359. SSL_FLAG_VFY_SRV("Require",
  360. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  361. SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE),
  362. SSL_FLAG_VFY_SRV("RequestPostHandshake",
  363. SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE),
  364. SSL_FLAG_VFY_SRV("RequirePostHandshake",
  365. SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE |
  366. SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  367. };
  368. if (value == NULL)
  369. return -3;
  370. cctx->tbl = ssl_vfy_list;
  371. cctx->ntbl = OSSL_NELEM(ssl_vfy_list);
  372. return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
  373. }
  374. static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
  375. {
  376. int rv = 1;
  377. CERT *c = NULL;
  378. if (cctx->ctx) {
  379. rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
  380. c = cctx->ctx->cert;
  381. }
  382. if (cctx->ssl) {
  383. rv = SSL_use_certificate_chain_file(cctx->ssl, value);
  384. c = cctx->ssl->cert;
  385. }
  386. if (rv > 0 && c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
  387. char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
  388. OPENSSL_free(*pfilename);
  389. *pfilename = OPENSSL_strdup(value);
  390. if (!*pfilename)
  391. rv = 0;
  392. }
  393. return rv > 0;
  394. }
  395. static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
  396. {
  397. int rv = 1;
  398. if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
  399. return -2;
  400. if (cctx->ctx)
  401. rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
  402. if (cctx->ssl)
  403. rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
  404. return rv > 0;
  405. }
  406. static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
  407. {
  408. int rv = 1;
  409. if (cctx->ctx)
  410. rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
  411. return rv > 0;
  412. }
  413. static int do_store(SSL_CONF_CTX *cctx,
  414. const char *CAfile, const char *CApath, int verify_store)
  415. {
  416. CERT *cert;
  417. X509_STORE **st;
  418. if (cctx->ctx)
  419. cert = cctx->ctx->cert;
  420. else if (cctx->ssl)
  421. cert = cctx->ssl->cert;
  422. else
  423. return 1;
  424. st = verify_store ? &cert->verify_store : &cert->chain_store;
  425. if (*st == NULL) {
  426. *st = X509_STORE_new();
  427. if (*st == NULL)
  428. return 0;
  429. }
  430. return X509_STORE_load_locations(*st, CAfile, CApath) > 0;
  431. }
  432. static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value)
  433. {
  434. return do_store(cctx, NULL, value, 0);
  435. }
  436. static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value)
  437. {
  438. return do_store(cctx, value, NULL, 0);
  439. }
  440. static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value)
  441. {
  442. return do_store(cctx, NULL, value, 1);
  443. }
  444. static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value)
  445. {
  446. return do_store(cctx, value, NULL, 1);
  447. }
  448. static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value)
  449. {
  450. if (cctx->canames == NULL)
  451. cctx->canames = sk_X509_NAME_new_null();
  452. if (cctx->canames == NULL)
  453. return 0;
  454. return SSL_add_file_cert_subjects_to_stack(cctx->canames, value);
  455. }
  456. static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value)
  457. {
  458. return cmd_RequestCAFile(cctx, value);
  459. }
  460. static int cmd_RequestCAPath(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_dir_cert_subjects_to_stack(cctx->canames, value);
  467. }
  468. static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value)
  469. {
  470. return cmd_RequestCAPath(cctx, value);
  471. }
  472. #ifndef OPENSSL_NO_DH
  473. static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
  474. {
  475. int rv = 0;
  476. DH *dh = NULL;
  477. BIO *in = NULL;
  478. if (cctx->ctx || cctx->ssl) {
  479. in = BIO_new(BIO_s_file());
  480. if (in == NULL)
  481. goto end;
  482. if (BIO_read_filename(in, value) <= 0)
  483. goto end;
  484. dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
  485. if (dh == NULL)
  486. goto end;
  487. } else
  488. return 1;
  489. if (cctx->ctx)
  490. rv = SSL_CTX_set_tmp_dh(cctx->ctx, dh);
  491. if (cctx->ssl)
  492. rv = SSL_set_tmp_dh(cctx->ssl, dh);
  493. end:
  494. DH_free(dh);
  495. BIO_free(in);
  496. return rv > 0;
  497. }
  498. #endif
  499. static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value)
  500. {
  501. int rv = 0;
  502. int block_size = atoi(value);
  503. /*
  504. * All we care about is a non-negative value,
  505. * the setters check the range
  506. */
  507. if (block_size >= 0) {
  508. if (cctx->ctx)
  509. rv = SSL_CTX_set_block_padding(cctx->ctx, block_size);
  510. if (cctx->ssl)
  511. rv = SSL_set_block_padding(cctx->ssl, block_size);
  512. }
  513. return rv;
  514. }
  515. static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value)
  516. {
  517. int rv = 0;
  518. int num_tickets = atoi(value);
  519. if (num_tickets >= 0) {
  520. if (cctx->ctx)
  521. rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets);
  522. if (cctx->ssl)
  523. rv = SSL_set_num_tickets(cctx->ssl, num_tickets);
  524. }
  525. return rv;
  526. }
  527. typedef struct {
  528. int (*cmd) (SSL_CONF_CTX *cctx, const char *value);
  529. const char *str_file;
  530. const char *str_cmdline;
  531. unsigned short flags;
  532. unsigned short value_type;
  533. } ssl_conf_cmd_tbl;
  534. /* Table of supported parameters */
  535. #define SSL_CONF_CMD(name, cmdopt, flags, type) \
  536. {cmd_##name, #name, cmdopt, flags, type}
  537. #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
  538. SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
  539. #define SSL_CONF_CMD_SWITCH(name, flags) \
  540. {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
  541. /* See apps/apps.h if you change this table. */
  542. static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
  543. SSL_CONF_CMD_SWITCH("no_ssl3", 0),
  544. SSL_CONF_CMD_SWITCH("no_tls1", 0),
  545. SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
  546. SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
  547. SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
  548. SSL_CONF_CMD_SWITCH("bugs", 0),
  549. SSL_CONF_CMD_SWITCH("no_comp", 0),
  550. SSL_CONF_CMD_SWITCH("comp", 0),
  551. SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER),
  552. SSL_CONF_CMD_SWITCH("no_ticket", 0),
  553. SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER),
  554. SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
  555. SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_SERVER),
  556. SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
  557. SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER),
  558. SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_SERVER),
  559. SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
  560. SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER),
  561. SSL_CONF_CMD_SWITCH("strict", 0),
  562. SSL_CONF_CMD_SWITCH("no_middlebox", 0),
  563. SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER),
  564. SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER),
  565. SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0),
  566. SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
  567. SSL_CONF_CMD_STRING(Curves, "curves", 0),
  568. SSL_CONF_CMD_STRING(Groups, "groups", 0),
  569. #ifndef OPENSSL_NO_EC
  570. SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
  571. #endif
  572. SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
  573. SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0),
  574. SSL_CONF_CMD_STRING(Protocol, NULL, 0),
  575. SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
  576. SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
  577. SSL_CONF_CMD_STRING(Options, NULL, 0),
  578. SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
  579. SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
  580. SSL_CONF_TYPE_FILE),
  581. SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
  582. SSL_CONF_TYPE_FILE),
  583. SSL_CONF_CMD(ServerInfoFile, NULL,
  584. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  585. SSL_CONF_TYPE_FILE),
  586. SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
  587. SSL_CONF_TYPE_DIR),
  588. SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
  589. SSL_CONF_TYPE_FILE),
  590. SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
  591. SSL_CONF_TYPE_DIR),
  592. SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
  593. SSL_CONF_TYPE_FILE),
  594. SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
  595. SSL_CONF_TYPE_FILE),
  596. SSL_CONF_CMD(ClientCAFile, NULL,
  597. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  598. SSL_CONF_TYPE_FILE),
  599. SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
  600. SSL_CONF_TYPE_DIR),
  601. SSL_CONF_CMD(ClientCAPath, NULL,
  602. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  603. SSL_CONF_TYPE_DIR),
  604. #ifndef OPENSSL_NO_DH
  605. SSL_CONF_CMD(DHParameters, "dhparam",
  606. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  607. SSL_CONF_TYPE_FILE),
  608. #endif
  609. SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0),
  610. SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER),
  611. };
  612. /* Supported switches: must match order of switches in ssl_conf_cmds */
  613. static const ssl_switch_tbl ssl_cmd_switches[] = {
  614. {SSL_OP_NO_SSLv3, 0}, /* no_ssl3 */
  615. {SSL_OP_NO_TLSv1, 0}, /* no_tls1 */
  616. {SSL_OP_NO_TLSv1_1, 0}, /* no_tls1_1 */
  617. {SSL_OP_NO_TLSv1_2, 0}, /* no_tls1_2 */
  618. {SSL_OP_NO_TLSv1_3, 0}, /* no_tls1_3 */
  619. {SSL_OP_ALL, 0}, /* bugs */
  620. {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */
  621. {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */
  622. {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */
  623. {SSL_OP_NO_TICKET, 0}, /* no_ticket */
  624. {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */
  625. /* legacy_renegotiation */
  626. {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0},
  627. /* legacy_server_connect */
  628. {SSL_OP_LEGACY_SERVER_CONNECT, 0},
  629. /* no_renegotiation */
  630. {SSL_OP_NO_RENEGOTIATION, 0},
  631. /* no_resumption_on_reneg */
  632. {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0},
  633. /* no_legacy_server_connect */
  634. {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV},
  635. /* allow_no_dhe_kex */
  636. {SSL_OP_ALLOW_NO_DHE_KEX, 0},
  637. /* chacha reprioritization */
  638. {SSL_OP_PRIORITIZE_CHACHA, 0},
  639. {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */
  640. /* no_middlebox */
  641. {SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV},
  642. /* anti_replay */
  643. {SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV},
  644. /* no_anti_replay */
  645. {SSL_OP_NO_ANTI_REPLAY, 0},
  646. };
  647. static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
  648. {
  649. if (!pcmd || !*pcmd)
  650. return 0;
  651. /* If a prefix is set, check and skip */
  652. if (cctx->prefix) {
  653. if (strlen(*pcmd) <= cctx->prefixlen)
  654. return 0;
  655. if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
  656. strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
  657. return 0;
  658. if (cctx->flags & SSL_CONF_FLAG_FILE &&
  659. strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
  660. return 0;
  661. *pcmd += cctx->prefixlen;
  662. } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
  663. if (**pcmd != '-' || !(*pcmd)[1])
  664. return 0;
  665. *pcmd += 1;
  666. }
  667. return 1;
  668. }
  669. /* Determine if a command is allowed according to cctx flags */
  670. static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * t)
  671. {
  672. unsigned int tfl = t->flags;
  673. unsigned int cfl = cctx->flags;
  674. if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
  675. return 0;
  676. if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
  677. return 0;
  678. if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
  679. && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
  680. return 0;
  681. return 1;
  682. }
  683. static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
  684. const char *cmd)
  685. {
  686. const ssl_conf_cmd_tbl *t;
  687. size_t i;
  688. if (cmd == NULL)
  689. return NULL;
  690. /* Look for matching parameter name in table */
  691. for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
  692. if (ssl_conf_cmd_allowed(cctx, t)) {
  693. if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
  694. if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
  695. return t;
  696. }
  697. if (cctx->flags & SSL_CONF_FLAG_FILE) {
  698. if (t->str_file && strcasecmp(t->str_file, cmd) == 0)
  699. return t;
  700. }
  701. }
  702. }
  703. return NULL;
  704. }
  705. static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * cmd)
  706. {
  707. /* Find index of command in table */
  708. size_t idx = cmd - ssl_conf_cmds;
  709. const ssl_switch_tbl *scmd;
  710. /* Sanity check index */
  711. if (idx >= OSSL_NELEM(ssl_cmd_switches))
  712. return 0;
  713. /* Obtain switches entry with same index */
  714. scmd = ssl_cmd_switches + idx;
  715. ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
  716. return 1;
  717. }
  718. int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
  719. {
  720. const ssl_conf_cmd_tbl *runcmd;
  721. if (cmd == NULL) {
  722. SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_INVALID_NULL_CMD_NAME);
  723. return 0;
  724. }
  725. if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
  726. return -2;
  727. runcmd = ssl_conf_cmd_lookup(cctx, cmd);
  728. if (runcmd) {
  729. int rv;
  730. if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
  731. return ctrl_switch_option(cctx, runcmd);
  732. }
  733. if (value == NULL)
  734. return -3;
  735. rv = runcmd->cmd(cctx, value);
  736. if (rv > 0)
  737. return 2;
  738. if (rv == -2)
  739. return -2;
  740. if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
  741. SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_BAD_VALUE);
  742. ERR_add_error_data(4, "cmd=", cmd, ", value=", value);
  743. }
  744. return 0;
  745. }
  746. if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS) {
  747. SSLerr(SSL_F_SSL_CONF_CMD, SSL_R_UNKNOWN_CMD_NAME);
  748. ERR_add_error_data(2, "cmd=", cmd);
  749. }
  750. return -2;
  751. }
  752. int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
  753. {
  754. int rv;
  755. const char *arg = NULL, *argn;
  756. if (pargc && *pargc == 0)
  757. return 0;
  758. if (!pargc || *pargc > 0)
  759. arg = **pargv;
  760. if (arg == NULL)
  761. return 0;
  762. if (!pargc || *pargc > 1)
  763. argn = (*pargv)[1];
  764. else
  765. argn = NULL;
  766. cctx->flags &= ~SSL_CONF_FLAG_FILE;
  767. cctx->flags |= SSL_CONF_FLAG_CMDLINE;
  768. rv = SSL_CONF_cmd(cctx, arg, argn);
  769. if (rv > 0) {
  770. /* Success: update pargc, pargv */
  771. (*pargv) += rv;
  772. if (pargc)
  773. (*pargc) -= rv;
  774. return rv;
  775. }
  776. /* Unknown switch: indicate no arguments processed */
  777. if (rv == -2)
  778. return 0;
  779. /* Some error occurred processing command, return fatal error */
  780. if (rv == 0)
  781. return -1;
  782. return rv;
  783. }
  784. int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
  785. {
  786. if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
  787. const ssl_conf_cmd_tbl *runcmd;
  788. runcmd = ssl_conf_cmd_lookup(cctx, cmd);
  789. if (runcmd)
  790. return runcmd->value_type;
  791. }
  792. return SSL_CONF_TYPE_UNKNOWN;
  793. }
  794. SSL_CONF_CTX *SSL_CONF_CTX_new(void)
  795. {
  796. SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
  797. return ret;
  798. }
  799. int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
  800. {
  801. /* See if any certificates are missing private keys */
  802. size_t i;
  803. CERT *c = NULL;
  804. if (cctx->ctx)
  805. c = cctx->ctx->cert;
  806. else if (cctx->ssl)
  807. c = cctx->ssl->cert;
  808. if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
  809. for (i = 0; i < SSL_PKEY_NUM; i++) {
  810. const char *p = cctx->cert_filename[i];
  811. /*
  812. * If missing private key try to load one from certificate file
  813. */
  814. if (p && !c->pkeys[i].privatekey) {
  815. if (!cmd_PrivateKey(cctx, p))
  816. return 0;
  817. }
  818. }
  819. }
  820. if (cctx->canames) {
  821. if (cctx->ssl)
  822. SSL_set0_CA_list(cctx->ssl, cctx->canames);
  823. else if (cctx->ctx)
  824. SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
  825. else
  826. sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
  827. cctx->canames = NULL;
  828. }
  829. return 1;
  830. }
  831. void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
  832. {
  833. if (cctx) {
  834. size_t i;
  835. for (i = 0; i < SSL_PKEY_NUM; i++)
  836. OPENSSL_free(cctx->cert_filename[i]);
  837. OPENSSL_free(cctx->prefix);
  838. sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
  839. OPENSSL_free(cctx);
  840. }
  841. }
  842. unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
  843. {
  844. cctx->flags |= flags;
  845. return cctx->flags;
  846. }
  847. unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
  848. {
  849. cctx->flags &= ~flags;
  850. return cctx->flags;
  851. }
  852. int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
  853. {
  854. char *tmp = NULL;
  855. if (pre) {
  856. tmp = OPENSSL_strdup(pre);
  857. if (tmp == NULL)
  858. return 0;
  859. }
  860. OPENSSL_free(cctx->prefix);
  861. cctx->prefix = tmp;
  862. if (tmp)
  863. cctx->prefixlen = strlen(tmp);
  864. else
  865. cctx->prefixlen = 0;
  866. return 1;
  867. }
  868. void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
  869. {
  870. cctx->ssl = ssl;
  871. cctx->ctx = NULL;
  872. if (ssl) {
  873. cctx->poptions = &ssl->options;
  874. cctx->min_version = &ssl->min_proto_version;
  875. cctx->max_version = &ssl->max_proto_version;
  876. cctx->pcert_flags = &ssl->cert->cert_flags;
  877. cctx->pvfy_flags = &ssl->verify_mode;
  878. } else {
  879. cctx->poptions = NULL;
  880. cctx->min_version = NULL;
  881. cctx->max_version = NULL;
  882. cctx->pcert_flags = NULL;
  883. cctx->pvfy_flags = NULL;
  884. }
  885. }
  886. void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
  887. {
  888. cctx->ctx = ctx;
  889. cctx->ssl = NULL;
  890. if (ctx) {
  891. cctx->poptions = &ctx->options;
  892. cctx->min_version = &ctx->min_proto_version;
  893. cctx->max_version = &ctx->max_proto_version;
  894. cctx->pcert_flags = &ctx->cert->cert_flags;
  895. cctx->pvfy_flags = &ctx->verify_mode;
  896. } else {
  897. cctx->poptions = NULL;
  898. cctx->min_version = NULL;
  899. cctx->max_version = NULL;
  900. cctx->pcert_flags = NULL;
  901. cctx->pvfy_flags = NULL;
  902. }
  903. }