ssl_conf.c 28 KB

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