ssl_conf.c 29 KB

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