ssl_conf.c 30 KB

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