ssl_conf.c 29 KB

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