ssl_conf.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. /*
  2. * Copyright 2012-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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_local.h"
  11. #include <openssl/conf.h>
  12. #include <openssl/objects.h>
  13. #include <openssl/decoder.h>
  14. #include <openssl/core_dispatch.h>
  15. #include "internal/nelem.h"
  16. /*
  17. * structure holding name tables. This is used for permitted elements in lists
  18. * such as TLSv1.
  19. */
  20. typedef struct {
  21. const char *name;
  22. int namelen;
  23. unsigned int name_flags;
  24. uint64_t option_value;
  25. } ssl_flag_tbl;
  26. /* Switch table: use for single command line switches like no_tls2 */
  27. typedef struct {
  28. uint64_t option_value;
  29. unsigned int name_flags;
  30. } ssl_switch_tbl;
  31. /* Sense of name is inverted e.g. "TLSv1" will clear SSL_OP_NO_TLSv1 */
  32. #define SSL_TFLAG_INV 0x1
  33. /* Mask for type of flag referred to */
  34. #define SSL_TFLAG_TYPE_MASK 0xf00
  35. /* Flag is for options */
  36. #define SSL_TFLAG_OPTION 0x000
  37. /* Flag is for cert_flags */
  38. #define SSL_TFLAG_CERT 0x100
  39. /* Flag is for verify mode */
  40. #define SSL_TFLAG_VFY 0x200
  41. /* Option can only be used for clients */
  42. #define SSL_TFLAG_CLIENT SSL_CONF_FLAG_CLIENT
  43. /* Option can only be used for servers */
  44. #define SSL_TFLAG_SERVER SSL_CONF_FLAG_SERVER
  45. #define SSL_TFLAG_BOTH (SSL_TFLAG_CLIENT|SSL_TFLAG_SERVER)
  46. #define SSL_FLAG_TBL(str, flag) \
  47. {str, (int)(sizeof(str) - 1), SSL_TFLAG_BOTH, flag}
  48. #define SSL_FLAG_TBL_SRV(str, flag) \
  49. {str, (int)(sizeof(str) - 1), SSL_TFLAG_SERVER, flag}
  50. #define SSL_FLAG_TBL_CLI(str, flag) \
  51. {str, (int)(sizeof(str) - 1), SSL_TFLAG_CLIENT, flag}
  52. #define SSL_FLAG_TBL_INV(str, flag) \
  53. {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_BOTH, flag}
  54. #define SSL_FLAG_TBL_SRV_INV(str, flag) \
  55. {str, (int)(sizeof(str) - 1), SSL_TFLAG_INV|SSL_TFLAG_SERVER, flag}
  56. #define SSL_FLAG_TBL_CERT(str, flag) \
  57. {str, (int)(sizeof(str) - 1), SSL_TFLAG_CERT|SSL_TFLAG_BOTH, flag}
  58. #define SSL_FLAG_VFY_CLI(str, flag) \
  59. {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_CLIENT, flag}
  60. #define SSL_FLAG_VFY_SRV(str, flag) \
  61. {str, (int)(sizeof(str) - 1), SSL_TFLAG_VFY | SSL_TFLAG_SERVER, flag}
  62. /*
  63. * Opaque structure containing SSL configuration context.
  64. */
  65. struct ssl_conf_ctx_st {
  66. /*
  67. * Various flags indicating (among other things) which options we will
  68. * recognise.
  69. */
  70. unsigned int flags;
  71. /* Prefix and length of commands */
  72. char *prefix;
  73. size_t prefixlen;
  74. /* SSL_CTX or SSL structure to perform operations on */
  75. SSL_CTX *ctx;
  76. SSL *ssl;
  77. /* Pointer to SSL or SSL_CTX options field or NULL if none */
  78. uint64_t *poptions;
  79. /* Certificate filenames for each type */
  80. char **cert_filename;
  81. /* Number of elements in the cert_filename array */
  82. size_t num_cert_filename;
  83. /* Pointer to SSL or SSL_CTX cert_flags or NULL if none */
  84. uint32_t *pcert_flags;
  85. /* Pointer to SSL or SSL_CTX verify_mode or NULL if none */
  86. uint32_t *pvfy_flags;
  87. /* Pointer to SSL or SSL_CTX min_version field or NULL if none */
  88. int *min_version;
  89. /* Pointer to SSL or SSL_CTX max_version field or NULL if none */
  90. int *max_version;
  91. /* Current flag table being worked on */
  92. const ssl_flag_tbl *tbl;
  93. /* Size of table */
  94. size_t ntbl;
  95. /* Client CA names */
  96. STACK_OF(X509_NAME) *canames;
  97. };
  98. static void ssl_set_option(SSL_CONF_CTX *cctx, unsigned int name_flags,
  99. uint64_t option_value, int onoff)
  100. {
  101. uint32_t *pflags;
  102. if (cctx->poptions == NULL)
  103. return;
  104. if (name_flags & SSL_TFLAG_INV)
  105. onoff ^= 1;
  106. switch (name_flags & SSL_TFLAG_TYPE_MASK) {
  107. case SSL_TFLAG_CERT:
  108. pflags = cctx->pcert_flags;
  109. break;
  110. case SSL_TFLAG_VFY:
  111. pflags = cctx->pvfy_flags;
  112. break;
  113. case SSL_TFLAG_OPTION:
  114. if (onoff)
  115. *cctx->poptions |= option_value;
  116. else
  117. *cctx->poptions &= ~option_value;
  118. return;
  119. default:
  120. return;
  121. }
  122. if (onoff)
  123. *pflags |= option_value;
  124. else
  125. *pflags &= ~option_value;
  126. }
  127. static int ssl_match_option(SSL_CONF_CTX *cctx, const ssl_flag_tbl *tbl,
  128. const char *name, int namelen, int onoff)
  129. {
  130. /* If name not relevant for context skip */
  131. if (!(cctx->flags & tbl->name_flags & SSL_TFLAG_BOTH))
  132. return 0;
  133. if (namelen == -1) {
  134. if (strcmp(tbl->name, name))
  135. return 0;
  136. } else if (tbl->namelen != namelen
  137. || OPENSSL_strncasecmp(tbl->name, name, namelen))
  138. return 0;
  139. ssl_set_option(cctx, tbl->name_flags, tbl->option_value, onoff);
  140. return 1;
  141. }
  142. static int ssl_set_option_list(const char *elem, int len, void *usr)
  143. {
  144. SSL_CONF_CTX *cctx = usr;
  145. size_t i;
  146. const ssl_flag_tbl *tbl;
  147. int onoff = 1;
  148. /*
  149. * len == -1 indicates not being called in list context, just for single
  150. * command line switches, so don't allow +, -.
  151. */
  152. if (elem == NULL)
  153. return 0;
  154. if (len != -1) {
  155. if (*elem == '+') {
  156. elem++;
  157. len--;
  158. onoff = 1;
  159. } else if (*elem == '-') {
  160. elem++;
  161. len--;
  162. onoff = 0;
  163. }
  164. }
  165. for (i = 0, tbl = cctx->tbl; i < cctx->ntbl; i++, tbl++) {
  166. if (ssl_match_option(cctx, tbl, elem, len, onoff))
  167. return 1;
  168. }
  169. return 0;
  170. }
  171. /* Set supported signature algorithms */
  172. static int cmd_SignatureAlgorithms(SSL_CONF_CTX *cctx, const char *value)
  173. {
  174. int rv;
  175. if (cctx->ssl)
  176. rv = SSL_set1_sigalgs_list(cctx->ssl, value);
  177. /* NB: ctx == NULL performs syntax checking only */
  178. else
  179. rv = SSL_CTX_set1_sigalgs_list(cctx->ctx, value);
  180. return rv > 0;
  181. }
  182. /* Set supported client signature algorithms */
  183. static int cmd_ClientSignatureAlgorithms(SSL_CONF_CTX *cctx, const char *value)
  184. {
  185. int rv;
  186. if (cctx->ssl)
  187. rv = SSL_set1_client_sigalgs_list(cctx->ssl, value);
  188. /* NB: ctx == NULL performs syntax checking only */
  189. else
  190. rv = SSL_CTX_set1_client_sigalgs_list(cctx->ctx, value);
  191. return rv > 0;
  192. }
  193. static int cmd_Groups(SSL_CONF_CTX *cctx, const char *value)
  194. {
  195. int rv;
  196. if (cctx->ssl)
  197. rv = SSL_set1_groups_list(cctx->ssl, value);
  198. /* NB: ctx == NULL performs syntax checking only */
  199. else
  200. rv = SSL_CTX_set1_groups_list(cctx->ctx, value);
  201. return rv > 0;
  202. }
  203. /* This is the old name for cmd_Groups - retained for backwards compatibility */
  204. static int cmd_Curves(SSL_CONF_CTX *cctx, const char *value)
  205. {
  206. return cmd_Groups(cctx, value);
  207. }
  208. /* ECDH temporary parameters */
  209. static int cmd_ECDHParameters(SSL_CONF_CTX *cctx, const char *value)
  210. {
  211. int rv = 1;
  212. /* Ignore values supported by 1.0.2 for the automatic selection */
  213. if ((cctx->flags & SSL_CONF_FLAG_FILE)
  214. && (OPENSSL_strcasecmp(value, "+automatic") == 0
  215. || OPENSSL_strcasecmp(value, "automatic") == 0))
  216. return 1;
  217. if ((cctx->flags & SSL_CONF_FLAG_CMDLINE) &&
  218. strcmp(value, "auto") == 0)
  219. return 1;
  220. /* ECDHParameters accepts a single group name */
  221. if (strchr(value, ':') != NULL)
  222. return 0;
  223. if (cctx->ctx)
  224. rv = SSL_CTX_set1_groups_list(cctx->ctx, value);
  225. else if (cctx->ssl)
  226. rv = SSL_set1_groups_list(cctx->ssl, value);
  227. return rv > 0;
  228. }
  229. static int cmd_CipherString(SSL_CONF_CTX *cctx, const char *value)
  230. {
  231. int rv = 1;
  232. if (cctx->ctx)
  233. rv = SSL_CTX_set_cipher_list(cctx->ctx, value);
  234. if (cctx->ssl)
  235. rv = SSL_set_cipher_list(cctx->ssl, value);
  236. return rv > 0;
  237. }
  238. static int cmd_Ciphersuites(SSL_CONF_CTX *cctx, const char *value)
  239. {
  240. int rv = 1;
  241. if (cctx->ctx)
  242. rv = SSL_CTX_set_ciphersuites(cctx->ctx, value);
  243. if (cctx->ssl)
  244. rv = SSL_set_ciphersuites(cctx->ssl, value);
  245. return rv > 0;
  246. }
  247. static int cmd_Protocol(SSL_CONF_CTX *cctx, const char *value)
  248. {
  249. static const ssl_flag_tbl ssl_protocol_list[] = {
  250. SSL_FLAG_TBL_INV("ALL", SSL_OP_NO_SSL_MASK),
  251. SSL_FLAG_TBL_INV("SSLv2", SSL_OP_NO_SSLv2),
  252. SSL_FLAG_TBL_INV("SSLv3", SSL_OP_NO_SSLv3),
  253. SSL_FLAG_TBL_INV("TLSv1", SSL_OP_NO_TLSv1),
  254. SSL_FLAG_TBL_INV("TLSv1.1", SSL_OP_NO_TLSv1_1),
  255. SSL_FLAG_TBL_INV("TLSv1.2", SSL_OP_NO_TLSv1_2),
  256. SSL_FLAG_TBL_INV("TLSv1.3", SSL_OP_NO_TLSv1_3),
  257. SSL_FLAG_TBL_INV("DTLSv1", SSL_OP_NO_DTLSv1),
  258. SSL_FLAG_TBL_INV("DTLSv1.2", SSL_OP_NO_DTLSv1_2)
  259. };
  260. cctx->tbl = ssl_protocol_list;
  261. cctx->ntbl = OSSL_NELEM(ssl_protocol_list);
  262. return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
  263. }
  264. /*
  265. * protocol_from_string - converts a protocol version string to a number
  266. *
  267. * Returns -1 on failure or the version on success
  268. */
  269. static int protocol_from_string(const char *value)
  270. {
  271. struct protocol_versions {
  272. const char *name;
  273. int version;
  274. };
  275. /*
  276. * Note: To avoid breaking previously valid configurations, we must retain
  277. * legacy entries in this table even if the underlying protocol is no
  278. * longer supported. This also means that the constants SSL3_VERSION, ...
  279. * need to be retained indefinitely. This table can only grow, never
  280. * shrink.
  281. */
  282. static const struct protocol_versions versions[] = {
  283. {"None", 0},
  284. {"SSLv3", SSL3_VERSION},
  285. {"TLSv1", TLS1_VERSION},
  286. {"TLSv1.1", TLS1_1_VERSION},
  287. {"TLSv1.2", TLS1_2_VERSION},
  288. {"TLSv1.3", TLS1_3_VERSION},
  289. {"DTLSv1", DTLS1_VERSION},
  290. {"DTLSv1.2", DTLS1_2_VERSION}
  291. };
  292. size_t i;
  293. size_t n = OSSL_NELEM(versions);
  294. for (i = 0; i < n; i++)
  295. if (strcmp(versions[i].name, value) == 0)
  296. return versions[i].version;
  297. return -1;
  298. }
  299. static int min_max_proto(SSL_CONF_CTX *cctx, const char *value, int *bound)
  300. {
  301. int method_version;
  302. int new_version;
  303. if (cctx->ctx != NULL)
  304. method_version = cctx->ctx->method->version;
  305. else if (cctx->ssl != NULL)
  306. method_version = cctx->ssl->defltmeth->version;
  307. else
  308. return 0;
  309. if ((new_version = protocol_from_string(value)) < 0)
  310. return 0;
  311. return ssl_set_version_bound(method_version, new_version, bound);
  312. }
  313. /*
  314. * cmd_MinProtocol - Set min protocol version
  315. * @cctx: config structure to save settings in
  316. * @value: The min protocol version in string form
  317. *
  318. * Returns 1 on success and 0 on failure.
  319. */
  320. static int cmd_MinProtocol(SSL_CONF_CTX *cctx, const char *value)
  321. {
  322. return min_max_proto(cctx, value, cctx->min_version);
  323. }
  324. /*
  325. * cmd_MaxProtocol - Set max protocol version
  326. * @cctx: config structure to save settings in
  327. * @value: The max protocol version in string form
  328. *
  329. * Returns 1 on success and 0 on failure.
  330. */
  331. static int cmd_MaxProtocol(SSL_CONF_CTX *cctx, const char *value)
  332. {
  333. return min_max_proto(cctx, value, cctx->max_version);
  334. }
  335. static int cmd_Options(SSL_CONF_CTX *cctx, const char *value)
  336. {
  337. static const ssl_flag_tbl ssl_option_list[] = {
  338. SSL_FLAG_TBL_INV("SessionTicket", SSL_OP_NO_TICKET),
  339. SSL_FLAG_TBL_INV("EmptyFragments",
  340. SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS),
  341. SSL_FLAG_TBL("Bugs", SSL_OP_ALL),
  342. SSL_FLAG_TBL_INV("Compression", SSL_OP_NO_COMPRESSION),
  343. SSL_FLAG_TBL_SRV("ServerPreference", SSL_OP_CIPHER_SERVER_PREFERENCE),
  344. SSL_FLAG_TBL_SRV("NoResumptionOnRenegotiation",
  345. SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION),
  346. SSL_FLAG_TBL_SRV("DHSingle", SSL_OP_SINGLE_DH_USE),
  347. SSL_FLAG_TBL_SRV("ECDHSingle", SSL_OP_SINGLE_ECDH_USE),
  348. SSL_FLAG_TBL("UnsafeLegacyRenegotiation",
  349. SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION),
  350. SSL_FLAG_TBL("UnsafeLegacyServerConnect",
  351. SSL_OP_LEGACY_SERVER_CONNECT),
  352. SSL_FLAG_TBL("ClientRenegotiation",
  353. SSL_OP_ALLOW_CLIENT_RENEGOTIATION),
  354. SSL_FLAG_TBL_INV("EncryptThenMac", SSL_OP_NO_ENCRYPT_THEN_MAC),
  355. SSL_FLAG_TBL("NoRenegotiation", SSL_OP_NO_RENEGOTIATION),
  356. SSL_FLAG_TBL("AllowNoDHEKEX", SSL_OP_ALLOW_NO_DHE_KEX),
  357. SSL_FLAG_TBL("PreferNoDHEKEX", SSL_OP_PREFER_NO_DHE_KEX),
  358. SSL_FLAG_TBL("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA),
  359. SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT),
  360. SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY),
  361. SSL_FLAG_TBL_INV("ExtendedMasterSecret", SSL_OP_NO_EXTENDED_MASTER_SECRET),
  362. SSL_FLAG_TBL_INV("CANames", SSL_OP_DISABLE_TLSEXT_CA_NAMES),
  363. SSL_FLAG_TBL("KTLS", SSL_OP_ENABLE_KTLS),
  364. SSL_FLAG_TBL_CERT("StrictCertCheck", SSL_CERT_FLAG_TLS_STRICT),
  365. SSL_FLAG_TBL_INV("TxCertificateCompression", SSL_OP_NO_TX_CERTIFICATE_COMPRESSION),
  366. SSL_FLAG_TBL_INV("RxCertificateCompression", SSL_OP_NO_RX_CERTIFICATE_COMPRESSION),
  367. SSL_FLAG_TBL("KTLSTxZerocopySendfile", SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE),
  368. SSL_FLAG_TBL("IgnoreUnexpectedEOF", SSL_OP_IGNORE_UNEXPECTED_EOF),
  369. };
  370. if (value == NULL)
  371. return -3;
  372. cctx->tbl = ssl_option_list;
  373. cctx->ntbl = OSSL_NELEM(ssl_option_list);
  374. return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
  375. }
  376. static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value)
  377. {
  378. static const ssl_flag_tbl ssl_vfy_list[] = {
  379. SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER),
  380. SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER),
  381. SSL_FLAG_VFY_SRV("Require",
  382. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  383. SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE),
  384. SSL_FLAG_VFY_SRV("RequestPostHandshake",
  385. SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE),
  386. SSL_FLAG_VFY_SRV("RequirePostHandshake",
  387. SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE |
  388. SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  389. };
  390. if (value == NULL)
  391. return -3;
  392. cctx->tbl = ssl_vfy_list;
  393. cctx->ntbl = OSSL_NELEM(ssl_vfy_list);
  394. return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
  395. }
  396. static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
  397. {
  398. int rv = 1;
  399. CERT *c = NULL;
  400. if (cctx->ctx != NULL) {
  401. rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
  402. c = cctx->ctx->cert;
  403. }
  404. if (cctx->ssl != NULL) {
  405. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
  406. if (sc != NULL) {
  407. rv = SSL_use_certificate_chain_file(cctx->ssl, value);
  408. c = sc->cert;
  409. } else {
  410. rv = 0;
  411. }
  412. }
  413. if (rv > 0 && c != NULL && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
  414. size_t fileidx = c->key - c->pkeys;
  415. if (fileidx >= cctx->num_cert_filename) {
  416. rv = 0;
  417. } else {
  418. char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
  419. OPENSSL_free(*pfilename);
  420. *pfilename = OPENSSL_strdup(value);
  421. if (*pfilename == NULL)
  422. rv = 0;
  423. }
  424. }
  425. return rv > 0;
  426. }
  427. static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
  428. {
  429. int rv = 1;
  430. if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
  431. return -2;
  432. if (cctx->ctx)
  433. rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
  434. if (cctx->ssl)
  435. rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
  436. return rv > 0;
  437. }
  438. static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
  439. {
  440. int rv = 1;
  441. if (cctx->ctx)
  442. rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
  443. return rv > 0;
  444. }
  445. static int do_store(SSL_CONF_CTX *cctx,
  446. const char *CAfile, const char *CApath, const char *CAstore,
  447. int verify_store)
  448. {
  449. CERT *cert;
  450. X509_STORE **st;
  451. SSL_CTX *ctx;
  452. OSSL_LIB_CTX *libctx = NULL;
  453. const char *propq = NULL;
  454. if (cctx->ctx != NULL) {
  455. cert = cctx->ctx->cert;
  456. ctx = cctx->ctx;
  457. } else if (cctx->ssl != NULL) {
  458. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
  459. if (sc == NULL)
  460. return 0;
  461. cert = sc->cert;
  462. ctx = cctx->ssl->ctx;
  463. } else {
  464. return 1;
  465. }
  466. if (ctx != NULL) {
  467. libctx = ctx->libctx;
  468. propq = ctx->propq;
  469. }
  470. st = verify_store ? &cert->verify_store : &cert->chain_store;
  471. if (*st == NULL) {
  472. *st = X509_STORE_new();
  473. if (*st == NULL)
  474. return 0;
  475. }
  476. if (CAfile != NULL && !X509_STORE_load_file_ex(*st, CAfile, libctx, propq))
  477. return 0;
  478. if (CApath != NULL && !X509_STORE_load_path(*st, CApath))
  479. return 0;
  480. if (CAstore != NULL && !X509_STORE_load_store_ex(*st, CAstore, libctx,
  481. propq))
  482. return 0;
  483. return 1;
  484. }
  485. static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value)
  486. {
  487. return do_store(cctx, NULL, value, NULL, 0);
  488. }
  489. static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value)
  490. {
  491. return do_store(cctx, value, NULL, NULL, 0);
  492. }
  493. static int cmd_ChainCAStore(SSL_CONF_CTX *cctx, const char *value)
  494. {
  495. return do_store(cctx, NULL, NULL, value, 0);
  496. }
  497. static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value)
  498. {
  499. return do_store(cctx, NULL, value, NULL, 1);
  500. }
  501. static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value)
  502. {
  503. return do_store(cctx, value, NULL, NULL, 1);
  504. }
  505. static int cmd_VerifyCAStore(SSL_CONF_CTX *cctx, const char *value)
  506. {
  507. return do_store(cctx, NULL, NULL, value, 1);
  508. }
  509. static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value)
  510. {
  511. if (cctx->canames == NULL)
  512. cctx->canames = sk_X509_NAME_new_null();
  513. if (cctx->canames == NULL)
  514. return 0;
  515. return SSL_add_file_cert_subjects_to_stack(cctx->canames, value);
  516. }
  517. static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value)
  518. {
  519. return cmd_RequestCAFile(cctx, value);
  520. }
  521. static int cmd_RequestCAPath(SSL_CONF_CTX *cctx, const char *value)
  522. {
  523. if (cctx->canames == NULL)
  524. cctx->canames = sk_X509_NAME_new_null();
  525. if (cctx->canames == NULL)
  526. return 0;
  527. return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value);
  528. }
  529. static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value)
  530. {
  531. return cmd_RequestCAPath(cctx, value);
  532. }
  533. static int cmd_RequestCAStore(SSL_CONF_CTX *cctx, const char *value)
  534. {
  535. if (cctx->canames == NULL)
  536. cctx->canames = sk_X509_NAME_new_null();
  537. if (cctx->canames == NULL)
  538. return 0;
  539. return SSL_add_store_cert_subjects_to_stack(cctx->canames, value);
  540. }
  541. static int cmd_ClientCAStore(SSL_CONF_CTX *cctx, const char *value)
  542. {
  543. return cmd_RequestCAStore(cctx, value);
  544. }
  545. static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
  546. {
  547. int rv = 0;
  548. EVP_PKEY *dhpkey = NULL;
  549. BIO *in = NULL;
  550. SSL_CTX *sslctx = (cctx->ssl != NULL) ? cctx->ssl->ctx : cctx->ctx;
  551. OSSL_DECODER_CTX *decoderctx = NULL;
  552. if (cctx->ctx != NULL || cctx->ssl != NULL) {
  553. in = BIO_new(BIO_s_file());
  554. if (in == NULL)
  555. goto end;
  556. if (BIO_read_filename(in, value) <= 0)
  557. goto end;
  558. decoderctx
  559. = OSSL_DECODER_CTX_new_for_pkey(&dhpkey, "PEM", NULL, "DH",
  560. OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
  561. sslctx->libctx, sslctx->propq);
  562. if (decoderctx == NULL)
  563. goto end;
  564. ERR_set_mark();
  565. while (!OSSL_DECODER_from_bio(decoderctx, in)
  566. && dhpkey == NULL
  567. && !BIO_eof(in));
  568. OSSL_DECODER_CTX_free(decoderctx);
  569. if (dhpkey == NULL) {
  570. ERR_clear_last_mark();
  571. goto end;
  572. }
  573. ERR_pop_to_mark();
  574. } else {
  575. return 1;
  576. }
  577. if (cctx->ctx != NULL) {
  578. if ((rv = SSL_CTX_set0_tmp_dh_pkey(cctx->ctx, dhpkey)) > 0)
  579. dhpkey = NULL;
  580. }
  581. if (cctx->ssl != NULL) {
  582. if ((rv = SSL_set0_tmp_dh_pkey(cctx->ssl, dhpkey)) > 0)
  583. dhpkey = NULL;
  584. }
  585. end:
  586. EVP_PKEY_free(dhpkey);
  587. BIO_free(in);
  588. return rv > 0;
  589. }
  590. static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value)
  591. {
  592. int rv = 0;
  593. int block_size = atoi(value);
  594. /*
  595. * All we care about is a non-negative value,
  596. * the setters check the range
  597. */
  598. if (block_size >= 0) {
  599. if (cctx->ctx)
  600. rv = SSL_CTX_set_block_padding(cctx->ctx, block_size);
  601. if (cctx->ssl)
  602. rv = SSL_set_block_padding(cctx->ssl, block_size);
  603. }
  604. return rv;
  605. }
  606. static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value)
  607. {
  608. int rv = 0;
  609. int num_tickets = atoi(value);
  610. if (num_tickets >= 0) {
  611. if (cctx->ctx)
  612. rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets);
  613. if (cctx->ssl)
  614. rv = SSL_set_num_tickets(cctx->ssl, num_tickets);
  615. }
  616. return rv;
  617. }
  618. typedef struct {
  619. int (*cmd) (SSL_CONF_CTX *cctx, const char *value);
  620. const char *str_file;
  621. const char *str_cmdline;
  622. unsigned short flags;
  623. unsigned short value_type;
  624. } ssl_conf_cmd_tbl;
  625. /* Table of supported parameters */
  626. #define SSL_CONF_CMD(name, cmdopt, flags, type) \
  627. {cmd_##name, #name, cmdopt, flags, type}
  628. #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
  629. SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
  630. #define SSL_CONF_CMD_SWITCH(name, flags) \
  631. {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
  632. /* See apps/include/opt.h if you change this table. */
  633. /* The SSL_CONF_CMD_SWITCH should be the same order as ssl_cmd_switches */
  634. static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
  635. SSL_CONF_CMD_SWITCH("no_ssl3", 0),
  636. SSL_CONF_CMD_SWITCH("no_tls1", 0),
  637. SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
  638. SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
  639. SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
  640. SSL_CONF_CMD_SWITCH("bugs", 0),
  641. SSL_CONF_CMD_SWITCH("no_comp", 0),
  642. SSL_CONF_CMD_SWITCH("comp", 0),
  643. SSL_CONF_CMD_SWITCH("no_tx_cert_comp", 0),
  644. SSL_CONF_CMD_SWITCH("tx_cert_comp", 0),
  645. SSL_CONF_CMD_SWITCH("no_rx_cert_comp", 0),
  646. SSL_CONF_CMD_SWITCH("rx_cert_comp", 0),
  647. SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER),
  648. SSL_CONF_CMD_SWITCH("no_ticket", 0),
  649. SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER),
  650. SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
  651. SSL_CONF_CMD_SWITCH("client_renegotiation", SSL_CONF_FLAG_SERVER),
  652. SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_CLIENT),
  653. SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
  654. SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER),
  655. SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_CLIENT),
  656. SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
  657. SSL_CONF_CMD_SWITCH("prefer_no_dhe_kex", 0),
  658. SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER),
  659. SSL_CONF_CMD_SWITCH("strict", 0),
  660. SSL_CONF_CMD_SWITCH("no_middlebox", 0),
  661. SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER),
  662. SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER),
  663. SSL_CONF_CMD_SWITCH("no_etm", 0),
  664. SSL_CONF_CMD_SWITCH("no_ems", 0),
  665. SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0),
  666. SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
  667. SSL_CONF_CMD_STRING(Curves, "curves", 0),
  668. SSL_CONF_CMD_STRING(Groups, "groups", 0),
  669. SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
  670. SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
  671. SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0),
  672. SSL_CONF_CMD_STRING(Protocol, NULL, 0),
  673. SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
  674. SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
  675. SSL_CONF_CMD_STRING(Options, NULL, 0),
  676. SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
  677. SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
  678. SSL_CONF_TYPE_FILE),
  679. SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
  680. SSL_CONF_TYPE_FILE),
  681. SSL_CONF_CMD(ServerInfoFile, NULL,
  682. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  683. SSL_CONF_TYPE_FILE),
  684. SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
  685. SSL_CONF_TYPE_DIR),
  686. SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
  687. SSL_CONF_TYPE_FILE),
  688. SSL_CONF_CMD(ChainCAStore, "chainCAstore", SSL_CONF_FLAG_CERTIFICATE,
  689. SSL_CONF_TYPE_STORE),
  690. SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
  691. SSL_CONF_TYPE_DIR),
  692. SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
  693. SSL_CONF_TYPE_FILE),
  694. SSL_CONF_CMD(VerifyCAStore, "verifyCAstore", SSL_CONF_FLAG_CERTIFICATE,
  695. SSL_CONF_TYPE_STORE),
  696. SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
  697. SSL_CONF_TYPE_FILE),
  698. SSL_CONF_CMD(ClientCAFile, NULL,
  699. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  700. SSL_CONF_TYPE_FILE),
  701. SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
  702. SSL_CONF_TYPE_DIR),
  703. SSL_CONF_CMD(ClientCAPath, NULL,
  704. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  705. SSL_CONF_TYPE_DIR),
  706. SSL_CONF_CMD(RequestCAStore, "requestCAStore", SSL_CONF_FLAG_CERTIFICATE,
  707. SSL_CONF_TYPE_STORE),
  708. SSL_CONF_CMD(ClientCAStore, NULL,
  709. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  710. SSL_CONF_TYPE_STORE),
  711. SSL_CONF_CMD(DHParameters, "dhparam",
  712. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  713. SSL_CONF_TYPE_FILE),
  714. SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0),
  715. SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER),
  716. };
  717. /* Supported switches: must match order of switches in ssl_conf_cmds */
  718. static const ssl_switch_tbl ssl_cmd_switches[] = {
  719. {SSL_OP_NO_SSLv3, 0}, /* no_ssl3 */
  720. {SSL_OP_NO_TLSv1, 0}, /* no_tls1 */
  721. {SSL_OP_NO_TLSv1_1, 0}, /* no_tls1_1 */
  722. {SSL_OP_NO_TLSv1_2, 0}, /* no_tls1_2 */
  723. {SSL_OP_NO_TLSv1_3, 0}, /* no_tls1_3 */
  724. {SSL_OP_ALL, 0}, /* bugs */
  725. {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */
  726. {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */
  727. {SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, 0}, /* no_tx_cert_comp */
  728. {SSL_OP_NO_TX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV}, /* tx_cert_comp */
  729. {SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, 0}, /* no_rx_cert_comp */
  730. {SSL_OP_NO_RX_CERTIFICATE_COMPRESSION, SSL_TFLAG_INV}, /* rx_cert_comp */
  731. {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */
  732. {SSL_OP_NO_TICKET, 0}, /* no_ticket */
  733. {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */
  734. /* legacy_renegotiation */
  735. {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0},
  736. /* Allow client renegotiation */
  737. {SSL_OP_ALLOW_CLIENT_RENEGOTIATION, 0},
  738. /* legacy_server_connect */
  739. {SSL_OP_LEGACY_SERVER_CONNECT, 0},
  740. /* no_renegotiation */
  741. {SSL_OP_NO_RENEGOTIATION, 0},
  742. /* no_resumption_on_reneg */
  743. {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0},
  744. /* no_legacy_server_connect */
  745. {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV},
  746. /* allow_no_dhe_kex */
  747. {SSL_OP_ALLOW_NO_DHE_KEX, 0},
  748. /* prefer_no_dhe_kex */
  749. {SSL_OP_PREFER_NO_DHE_KEX, 0},
  750. /* chacha reprioritization */
  751. {SSL_OP_PRIORITIZE_CHACHA, 0},
  752. {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */
  753. /* no_middlebox */
  754. {SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV},
  755. /* anti_replay */
  756. {SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV},
  757. /* no_anti_replay */
  758. {SSL_OP_NO_ANTI_REPLAY, 0},
  759. /* no Encrypt-then-Mac */
  760. {SSL_OP_NO_ENCRYPT_THEN_MAC, 0},
  761. /* no Extended master secret */
  762. {SSL_OP_NO_EXTENDED_MASTER_SECRET, 0},
  763. };
  764. static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
  765. {
  766. if (pcmd == NULL || *pcmd == NULL)
  767. return 0;
  768. /* If a prefix is set, check and skip */
  769. if (cctx->prefix) {
  770. if (strlen(*pcmd) <= cctx->prefixlen)
  771. return 0;
  772. if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
  773. strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
  774. return 0;
  775. if (cctx->flags & SSL_CONF_FLAG_FILE &&
  776. OPENSSL_strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
  777. return 0;
  778. *pcmd += cctx->prefixlen;
  779. } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
  780. if (**pcmd != '-' || !(*pcmd)[1])
  781. return 0;
  782. *pcmd += 1;
  783. }
  784. return 1;
  785. }
  786. /* Determine if a command is allowed according to cctx flags */
  787. static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *t)
  788. {
  789. unsigned int tfl = t->flags;
  790. unsigned int cfl = cctx->flags;
  791. if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
  792. return 0;
  793. if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
  794. return 0;
  795. if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
  796. && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
  797. return 0;
  798. return 1;
  799. }
  800. static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
  801. const char *cmd)
  802. {
  803. const ssl_conf_cmd_tbl *t;
  804. size_t i;
  805. if (cmd == NULL)
  806. return NULL;
  807. /* Look for matching parameter name in table */
  808. for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
  809. if (ssl_conf_cmd_allowed(cctx, t)) {
  810. if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
  811. if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
  812. return t;
  813. }
  814. if (cctx->flags & SSL_CONF_FLAG_FILE) {
  815. if (t->str_file && OPENSSL_strcasecmp(t->str_file, cmd) == 0)
  816. return t;
  817. }
  818. }
  819. }
  820. return NULL;
  821. }
  822. static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl *cmd)
  823. {
  824. /* Find index of command in table */
  825. size_t idx = cmd - ssl_conf_cmds;
  826. const ssl_switch_tbl *scmd;
  827. /* Sanity check index */
  828. if (idx >= OSSL_NELEM(ssl_cmd_switches)) {
  829. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  830. return 0;
  831. }
  832. /* Obtain switches entry with same index */
  833. scmd = ssl_cmd_switches + idx;
  834. ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
  835. return 1;
  836. }
  837. int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
  838. {
  839. const ssl_conf_cmd_tbl *runcmd;
  840. if (cmd == NULL) {
  841. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_NULL_CMD_NAME);
  842. return 0;
  843. }
  844. if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
  845. goto unknown_cmd;
  846. runcmd = ssl_conf_cmd_lookup(cctx, cmd);
  847. if (runcmd) {
  848. int rv = -3;
  849. if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
  850. return ctrl_switch_option(cctx, runcmd);
  851. }
  852. if (value == NULL)
  853. goto bad_value;
  854. rv = runcmd->cmd(cctx, value);
  855. if (rv > 0)
  856. return 2;
  857. if (rv != -2)
  858. rv = 0;
  859. bad_value:
  860. if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
  861. ERR_raise_data(ERR_LIB_SSL, SSL_R_BAD_VALUE,
  862. "cmd=%s, value=%s", cmd,
  863. value != NULL ? value : "<EMPTY>");
  864. return rv;
  865. }
  866. unknown_cmd:
  867. if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
  868. ERR_raise_data(ERR_LIB_SSL, SSL_R_UNKNOWN_CMD_NAME, "cmd=%s", cmd);
  869. return -2;
  870. }
  871. int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
  872. {
  873. int rv;
  874. const char *arg = NULL, *argn;
  875. if (pargc != NULL && *pargc == 0)
  876. return 0;
  877. if (pargc == NULL || *pargc > 0)
  878. arg = **pargv;
  879. if (arg == NULL)
  880. return 0;
  881. if (pargc == NULL || *pargc > 1)
  882. argn = (*pargv)[1];
  883. else
  884. argn = NULL;
  885. cctx->flags &= ~SSL_CONF_FLAG_FILE;
  886. cctx->flags |= SSL_CONF_FLAG_CMDLINE;
  887. rv = SSL_CONF_cmd(cctx, arg, argn);
  888. if (rv > 0) {
  889. /* Success: update pargc, pargv */
  890. (*pargv) += rv;
  891. if (pargc)
  892. (*pargc) -= rv;
  893. return rv;
  894. }
  895. /* Unknown switch: indicate no arguments processed */
  896. if (rv == -2)
  897. return 0;
  898. /* Some error occurred processing command, return fatal error */
  899. if (rv == 0)
  900. return -1;
  901. return rv;
  902. }
  903. int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
  904. {
  905. if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
  906. const ssl_conf_cmd_tbl *runcmd;
  907. runcmd = ssl_conf_cmd_lookup(cctx, cmd);
  908. if (runcmd)
  909. return runcmd->value_type;
  910. }
  911. return SSL_CONF_TYPE_UNKNOWN;
  912. }
  913. SSL_CONF_CTX *SSL_CONF_CTX_new(void)
  914. {
  915. SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
  916. return ret;
  917. }
  918. int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
  919. {
  920. /* See if any certificates are missing private keys */
  921. size_t i;
  922. CERT *c = NULL;
  923. if (cctx->ctx != NULL) {
  924. c = cctx->ctx->cert;
  925. } else if (cctx->ssl != NULL) {
  926. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(cctx->ssl);
  927. if (sc != NULL)
  928. c = sc->cert;
  929. }
  930. if (c != NULL && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
  931. for (i = 0; i < cctx->num_cert_filename; i++) {
  932. const char *p = cctx->cert_filename[i];
  933. /*
  934. * If missing private key try to load one from certificate file
  935. */
  936. if (p != NULL && c->pkeys[i].privatekey == NULL) {
  937. if (!cmd_PrivateKey(cctx, p))
  938. return 0;
  939. }
  940. }
  941. }
  942. if (cctx->canames) {
  943. if (cctx->ssl)
  944. SSL_set0_CA_list(cctx->ssl, cctx->canames);
  945. else if (cctx->ctx)
  946. SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
  947. else
  948. sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
  949. cctx->canames = NULL;
  950. }
  951. return 1;
  952. }
  953. static void free_cert_filename(SSL_CONF_CTX *cctx)
  954. {
  955. size_t i;
  956. for (i = 0; i < cctx->num_cert_filename; i++)
  957. OPENSSL_free(cctx->cert_filename[i]);
  958. OPENSSL_free(cctx->cert_filename);
  959. cctx->cert_filename = NULL;
  960. cctx->num_cert_filename = 0;
  961. }
  962. void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
  963. {
  964. if (cctx) {
  965. free_cert_filename(cctx);
  966. OPENSSL_free(cctx->prefix);
  967. sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
  968. OPENSSL_free(cctx);
  969. }
  970. }
  971. unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
  972. {
  973. cctx->flags |= flags;
  974. return cctx->flags;
  975. }
  976. unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
  977. {
  978. cctx->flags &= ~flags;
  979. return cctx->flags;
  980. }
  981. int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
  982. {
  983. char *tmp = NULL;
  984. if (pre) {
  985. tmp = OPENSSL_strdup(pre);
  986. if (tmp == NULL)
  987. return 0;
  988. }
  989. OPENSSL_free(cctx->prefix);
  990. cctx->prefix = tmp;
  991. if (tmp)
  992. cctx->prefixlen = strlen(tmp);
  993. else
  994. cctx->prefixlen = 0;
  995. return 1;
  996. }
  997. void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
  998. {
  999. cctx->ssl = ssl;
  1000. cctx->ctx = NULL;
  1001. free_cert_filename(cctx);
  1002. if (ssl != NULL) {
  1003. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
  1004. if (sc == NULL)
  1005. return;
  1006. cctx->poptions = &sc->options;
  1007. cctx->min_version = &sc->min_proto_version;
  1008. cctx->max_version = &sc->max_proto_version;
  1009. cctx->pcert_flags = &sc->cert->cert_flags;
  1010. cctx->pvfy_flags = &sc->verify_mode;
  1011. cctx->cert_filename = OPENSSL_zalloc(sc->ssl_pkey_num
  1012. * sizeof(*cctx->cert_filename));
  1013. if (cctx->cert_filename != NULL)
  1014. cctx->num_cert_filename = sc->ssl_pkey_num;
  1015. } else {
  1016. cctx->poptions = NULL;
  1017. cctx->min_version = NULL;
  1018. cctx->max_version = NULL;
  1019. cctx->pcert_flags = NULL;
  1020. cctx->pvfy_flags = NULL;
  1021. }
  1022. }
  1023. void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
  1024. {
  1025. cctx->ctx = ctx;
  1026. cctx->ssl = NULL;
  1027. free_cert_filename(cctx);
  1028. if (ctx) {
  1029. cctx->poptions = &ctx->options;
  1030. cctx->min_version = &ctx->min_proto_version;
  1031. cctx->max_version = &ctx->max_proto_version;
  1032. cctx->pcert_flags = &ctx->cert->cert_flags;
  1033. cctx->pvfy_flags = &ctx->verify_mode;
  1034. cctx->cert_filename = OPENSSL_zalloc((SSL_PKEY_NUM + ctx->sigalg_list_len)
  1035. * sizeof(*cctx->cert_filename));
  1036. if (cctx->cert_filename != NULL)
  1037. cctx->num_cert_filename = SSL_PKEY_NUM + ctx->sigalg_list_len;
  1038. } else {
  1039. cctx->poptions = NULL;
  1040. cctx->min_version = NULL;
  1041. cctx->max_version = NULL;
  1042. cctx->pcert_flags = NULL;
  1043. cctx->pvfy_flags = NULL;
  1044. }
  1045. }