ssl_conf.c 35 KB

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