ssl_conf.c 29 KB

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