ssl_conf.c 28 KB

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