2
0

ssl_conf.c 35 KB

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