ssl_conf.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*
  2. * Copyright 2012-2022 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 (strstr(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->ctx->method->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("PrioritizeChaCha", SSL_OP_PRIORITIZE_CHACHA),
  356. SSL_FLAG_TBL("MiddleboxCompat", SSL_OP_ENABLE_MIDDLEBOX_COMPAT),
  357. SSL_FLAG_TBL_INV("AntiReplay", SSL_OP_NO_ANTI_REPLAY),
  358. SSL_FLAG_TBL_INV("ExtendedMasterSecret", SSL_OP_NO_EXTENDED_MASTER_SECRET),
  359. SSL_FLAG_TBL_INV("CANames", SSL_OP_DISABLE_TLSEXT_CA_NAMES),
  360. SSL_FLAG_TBL("KTLS", SSL_OP_ENABLE_KTLS),
  361. SSL_FLAG_TBL_CERT("StrictCertCheck", SSL_CERT_FLAG_TLS_STRICT)
  362. };
  363. if (value == NULL)
  364. return -3;
  365. cctx->tbl = ssl_option_list;
  366. cctx->ntbl = OSSL_NELEM(ssl_option_list);
  367. return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
  368. }
  369. static int cmd_VerifyMode(SSL_CONF_CTX *cctx, const char *value)
  370. {
  371. static const ssl_flag_tbl ssl_vfy_list[] = {
  372. SSL_FLAG_VFY_CLI("Peer", SSL_VERIFY_PEER),
  373. SSL_FLAG_VFY_SRV("Request", SSL_VERIFY_PEER),
  374. SSL_FLAG_VFY_SRV("Require",
  375. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  376. SSL_FLAG_VFY_SRV("Once", SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE),
  377. SSL_FLAG_VFY_SRV("RequestPostHandshake",
  378. SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE),
  379. SSL_FLAG_VFY_SRV("RequirePostHandshake",
  380. SSL_VERIFY_PEER | SSL_VERIFY_POST_HANDSHAKE |
  381. SSL_VERIFY_FAIL_IF_NO_PEER_CERT),
  382. };
  383. if (value == NULL)
  384. return -3;
  385. cctx->tbl = ssl_vfy_list;
  386. cctx->ntbl = OSSL_NELEM(ssl_vfy_list);
  387. return CONF_parse_list(value, ',', 1, ssl_set_option_list, cctx);
  388. }
  389. static int cmd_Certificate(SSL_CONF_CTX *cctx, const char *value)
  390. {
  391. int rv = 1;
  392. CERT *c = NULL;
  393. if (cctx->ctx) {
  394. rv = SSL_CTX_use_certificate_chain_file(cctx->ctx, value);
  395. c = cctx->ctx->cert;
  396. }
  397. if (cctx->ssl) {
  398. rv = SSL_use_certificate_chain_file(cctx->ssl, value);
  399. c = cctx->ssl->cert;
  400. }
  401. if (rv > 0 && c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
  402. char **pfilename = &cctx->cert_filename[c->key - c->pkeys];
  403. OPENSSL_free(*pfilename);
  404. *pfilename = OPENSSL_strdup(value);
  405. if (*pfilename == NULL)
  406. rv = 0;
  407. }
  408. return rv > 0;
  409. }
  410. static int cmd_PrivateKey(SSL_CONF_CTX *cctx, const char *value)
  411. {
  412. int rv = 1;
  413. if (!(cctx->flags & SSL_CONF_FLAG_CERTIFICATE))
  414. return -2;
  415. if (cctx->ctx)
  416. rv = SSL_CTX_use_PrivateKey_file(cctx->ctx, value, SSL_FILETYPE_PEM);
  417. if (cctx->ssl)
  418. rv = SSL_use_PrivateKey_file(cctx->ssl, value, SSL_FILETYPE_PEM);
  419. return rv > 0;
  420. }
  421. static int cmd_ServerInfoFile(SSL_CONF_CTX *cctx, const char *value)
  422. {
  423. int rv = 1;
  424. if (cctx->ctx)
  425. rv = SSL_CTX_use_serverinfo_file(cctx->ctx, value);
  426. return rv > 0;
  427. }
  428. static int do_store(SSL_CONF_CTX *cctx,
  429. const char *CAfile, const char *CApath, const char *CAstore,
  430. int verify_store)
  431. {
  432. CERT *cert;
  433. X509_STORE **st;
  434. SSL_CTX *ctx;
  435. OSSL_LIB_CTX *libctx = NULL;
  436. const char *propq = NULL;
  437. if (cctx->ctx != NULL) {
  438. cert = cctx->ctx->cert;
  439. ctx = cctx->ctx;
  440. } else if (cctx->ssl != NULL) {
  441. cert = cctx->ssl->cert;
  442. ctx = cctx->ssl->ctx;
  443. } else {
  444. return 1;
  445. }
  446. if (ctx != NULL) {
  447. libctx = ctx->libctx;
  448. propq = ctx->propq;
  449. }
  450. st = verify_store ? &cert->verify_store : &cert->chain_store;
  451. if (*st == NULL) {
  452. *st = X509_STORE_new();
  453. if (*st == NULL)
  454. return 0;
  455. }
  456. if (CAfile != NULL && !X509_STORE_load_file_ex(*st, CAfile, libctx, propq))
  457. return 0;
  458. if (CApath != NULL && !X509_STORE_load_path(*st, CApath))
  459. return 0;
  460. if (CAstore != NULL && !X509_STORE_load_store_ex(*st, CAstore, libctx,
  461. propq))
  462. return 0;
  463. return 1;
  464. }
  465. static int cmd_ChainCAPath(SSL_CONF_CTX *cctx, const char *value)
  466. {
  467. return do_store(cctx, NULL, value, NULL, 0);
  468. }
  469. static int cmd_ChainCAFile(SSL_CONF_CTX *cctx, const char *value)
  470. {
  471. return do_store(cctx, value, NULL, NULL, 0);
  472. }
  473. static int cmd_ChainCAStore(SSL_CONF_CTX *cctx, const char *value)
  474. {
  475. return do_store(cctx, NULL, NULL, value, 0);
  476. }
  477. static int cmd_VerifyCAPath(SSL_CONF_CTX *cctx, const char *value)
  478. {
  479. return do_store(cctx, NULL, value, NULL, 1);
  480. }
  481. static int cmd_VerifyCAFile(SSL_CONF_CTX *cctx, const char *value)
  482. {
  483. return do_store(cctx, value, NULL, NULL, 1);
  484. }
  485. static int cmd_VerifyCAStore(SSL_CONF_CTX *cctx, const char *value)
  486. {
  487. return do_store(cctx, NULL, NULL, value, 1);
  488. }
  489. static int cmd_RequestCAFile(SSL_CONF_CTX *cctx, const char *value)
  490. {
  491. if (cctx->canames == NULL)
  492. cctx->canames = sk_X509_NAME_new_null();
  493. if (cctx->canames == NULL)
  494. return 0;
  495. return SSL_add_file_cert_subjects_to_stack(cctx->canames, value);
  496. }
  497. static int cmd_ClientCAFile(SSL_CONF_CTX *cctx, const char *value)
  498. {
  499. return cmd_RequestCAFile(cctx, value);
  500. }
  501. static int cmd_RequestCAPath(SSL_CONF_CTX *cctx, const char *value)
  502. {
  503. if (cctx->canames == NULL)
  504. cctx->canames = sk_X509_NAME_new_null();
  505. if (cctx->canames == NULL)
  506. return 0;
  507. return SSL_add_dir_cert_subjects_to_stack(cctx->canames, value);
  508. }
  509. static int cmd_ClientCAPath(SSL_CONF_CTX *cctx, const char *value)
  510. {
  511. return cmd_RequestCAPath(cctx, value);
  512. }
  513. static int cmd_RequestCAStore(SSL_CONF_CTX *cctx, const char *value)
  514. {
  515. if (cctx->canames == NULL)
  516. cctx->canames = sk_X509_NAME_new_null();
  517. if (cctx->canames == NULL)
  518. return 0;
  519. return SSL_add_store_cert_subjects_to_stack(cctx->canames, value);
  520. }
  521. static int cmd_ClientCAStore(SSL_CONF_CTX *cctx, const char *value)
  522. {
  523. return cmd_RequestCAStore(cctx, value);
  524. }
  525. static int cmd_DHParameters(SSL_CONF_CTX *cctx, const char *value)
  526. {
  527. int rv = 0;
  528. EVP_PKEY *dhpkey = NULL;
  529. BIO *in = NULL;
  530. SSL_CTX *sslctx = (cctx->ssl != NULL) ? cctx->ssl->ctx : cctx->ctx;
  531. OSSL_DECODER_CTX *decoderctx = NULL;
  532. if (cctx->ctx != NULL || cctx->ssl != NULL) {
  533. in = BIO_new(BIO_s_file());
  534. if (in == NULL)
  535. goto end;
  536. if (BIO_read_filename(in, value) <= 0)
  537. goto end;
  538. decoderctx
  539. = OSSL_DECODER_CTX_new_for_pkey(&dhpkey, "PEM", NULL, "DH",
  540. OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
  541. sslctx->libctx, sslctx->propq);
  542. if (decoderctx == NULL)
  543. goto end;
  544. ERR_set_mark();
  545. while (!OSSL_DECODER_from_bio(decoderctx, in)
  546. && dhpkey == NULL
  547. && !BIO_eof(in));
  548. OSSL_DECODER_CTX_free(decoderctx);
  549. if (dhpkey == NULL) {
  550. ERR_clear_last_mark();
  551. goto end;
  552. }
  553. ERR_pop_to_mark();
  554. } else {
  555. return 1;
  556. }
  557. if (cctx->ctx != NULL) {
  558. if ((rv = SSL_CTX_set0_tmp_dh_pkey(cctx->ctx, dhpkey)) > 0)
  559. dhpkey = NULL;
  560. }
  561. if (cctx->ssl != NULL) {
  562. if ((rv = SSL_set0_tmp_dh_pkey(cctx->ssl, dhpkey)) > 0)
  563. dhpkey = NULL;
  564. }
  565. end:
  566. EVP_PKEY_free(dhpkey);
  567. BIO_free(in);
  568. return rv > 0;
  569. }
  570. static int cmd_RecordPadding(SSL_CONF_CTX *cctx, const char *value)
  571. {
  572. int rv = 0;
  573. int block_size = atoi(value);
  574. /*
  575. * All we care about is a non-negative value,
  576. * the setters check the range
  577. */
  578. if (block_size >= 0) {
  579. if (cctx->ctx)
  580. rv = SSL_CTX_set_block_padding(cctx->ctx, block_size);
  581. if (cctx->ssl)
  582. rv = SSL_set_block_padding(cctx->ssl, block_size);
  583. }
  584. return rv;
  585. }
  586. static int cmd_NumTickets(SSL_CONF_CTX *cctx, const char *value)
  587. {
  588. int rv = 0;
  589. int num_tickets = atoi(value);
  590. if (num_tickets >= 0) {
  591. if (cctx->ctx)
  592. rv = SSL_CTX_set_num_tickets(cctx->ctx, num_tickets);
  593. if (cctx->ssl)
  594. rv = SSL_set_num_tickets(cctx->ssl, num_tickets);
  595. }
  596. return rv;
  597. }
  598. typedef struct {
  599. int (*cmd) (SSL_CONF_CTX *cctx, const char *value);
  600. const char *str_file;
  601. const char *str_cmdline;
  602. unsigned short flags;
  603. unsigned short value_type;
  604. } ssl_conf_cmd_tbl;
  605. /* Table of supported parameters */
  606. #define SSL_CONF_CMD(name, cmdopt, flags, type) \
  607. {cmd_##name, #name, cmdopt, flags, type}
  608. #define SSL_CONF_CMD_STRING(name, cmdopt, flags) \
  609. SSL_CONF_CMD(name, cmdopt, flags, SSL_CONF_TYPE_STRING)
  610. #define SSL_CONF_CMD_SWITCH(name, flags) \
  611. {0, NULL, name, flags, SSL_CONF_TYPE_NONE}
  612. /* See apps/include/opt.h if you change this table. */
  613. /* The SSL_CONF_CMD_SWITCH should be the same order as ssl_cmd_switches */
  614. static const ssl_conf_cmd_tbl ssl_conf_cmds[] = {
  615. SSL_CONF_CMD_SWITCH("no_ssl3", 0),
  616. SSL_CONF_CMD_SWITCH("no_tls1", 0),
  617. SSL_CONF_CMD_SWITCH("no_tls1_1", 0),
  618. SSL_CONF_CMD_SWITCH("no_tls1_2", 0),
  619. SSL_CONF_CMD_SWITCH("no_tls1_3", 0),
  620. SSL_CONF_CMD_SWITCH("bugs", 0),
  621. SSL_CONF_CMD_SWITCH("no_comp", 0),
  622. SSL_CONF_CMD_SWITCH("comp", 0),
  623. SSL_CONF_CMD_SWITCH("ecdh_single", SSL_CONF_FLAG_SERVER),
  624. SSL_CONF_CMD_SWITCH("no_ticket", 0),
  625. SSL_CONF_CMD_SWITCH("serverpref", SSL_CONF_FLAG_SERVER),
  626. SSL_CONF_CMD_SWITCH("legacy_renegotiation", 0),
  627. SSL_CONF_CMD_SWITCH("client_renegotiation", SSL_CONF_FLAG_SERVER),
  628. SSL_CONF_CMD_SWITCH("legacy_server_connect", SSL_CONF_FLAG_CLIENT),
  629. SSL_CONF_CMD_SWITCH("no_renegotiation", 0),
  630. SSL_CONF_CMD_SWITCH("no_resumption_on_reneg", SSL_CONF_FLAG_SERVER),
  631. SSL_CONF_CMD_SWITCH("no_legacy_server_connect", SSL_CONF_FLAG_CLIENT),
  632. SSL_CONF_CMD_SWITCH("allow_no_dhe_kex", 0),
  633. SSL_CONF_CMD_SWITCH("prioritize_chacha", SSL_CONF_FLAG_SERVER),
  634. SSL_CONF_CMD_SWITCH("strict", 0),
  635. SSL_CONF_CMD_SWITCH("no_middlebox", 0),
  636. SSL_CONF_CMD_SWITCH("anti_replay", SSL_CONF_FLAG_SERVER),
  637. SSL_CONF_CMD_SWITCH("no_anti_replay", SSL_CONF_FLAG_SERVER),
  638. SSL_CONF_CMD_SWITCH("no_etm", 0),
  639. SSL_CONF_CMD_SWITCH("no_ems", 0),
  640. SSL_CONF_CMD_STRING(SignatureAlgorithms, "sigalgs", 0),
  641. SSL_CONF_CMD_STRING(ClientSignatureAlgorithms, "client_sigalgs", 0),
  642. SSL_CONF_CMD_STRING(Curves, "curves", 0),
  643. SSL_CONF_CMD_STRING(Groups, "groups", 0),
  644. SSL_CONF_CMD_STRING(ECDHParameters, "named_curve", SSL_CONF_FLAG_SERVER),
  645. SSL_CONF_CMD_STRING(CipherString, "cipher", 0),
  646. SSL_CONF_CMD_STRING(Ciphersuites, "ciphersuites", 0),
  647. SSL_CONF_CMD_STRING(Protocol, NULL, 0),
  648. SSL_CONF_CMD_STRING(MinProtocol, "min_protocol", 0),
  649. SSL_CONF_CMD_STRING(MaxProtocol, "max_protocol", 0),
  650. SSL_CONF_CMD_STRING(Options, NULL, 0),
  651. SSL_CONF_CMD_STRING(VerifyMode, NULL, 0),
  652. SSL_CONF_CMD(Certificate, "cert", SSL_CONF_FLAG_CERTIFICATE,
  653. SSL_CONF_TYPE_FILE),
  654. SSL_CONF_CMD(PrivateKey, "key", SSL_CONF_FLAG_CERTIFICATE,
  655. SSL_CONF_TYPE_FILE),
  656. SSL_CONF_CMD(ServerInfoFile, NULL,
  657. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  658. SSL_CONF_TYPE_FILE),
  659. SSL_CONF_CMD(ChainCAPath, "chainCApath", SSL_CONF_FLAG_CERTIFICATE,
  660. SSL_CONF_TYPE_DIR),
  661. SSL_CONF_CMD(ChainCAFile, "chainCAfile", SSL_CONF_FLAG_CERTIFICATE,
  662. SSL_CONF_TYPE_FILE),
  663. SSL_CONF_CMD(ChainCAStore, "chainCAstore", SSL_CONF_FLAG_CERTIFICATE,
  664. SSL_CONF_TYPE_STORE),
  665. SSL_CONF_CMD(VerifyCAPath, "verifyCApath", SSL_CONF_FLAG_CERTIFICATE,
  666. SSL_CONF_TYPE_DIR),
  667. SSL_CONF_CMD(VerifyCAFile, "verifyCAfile", SSL_CONF_FLAG_CERTIFICATE,
  668. SSL_CONF_TYPE_FILE),
  669. SSL_CONF_CMD(VerifyCAStore, "verifyCAstore", SSL_CONF_FLAG_CERTIFICATE,
  670. SSL_CONF_TYPE_STORE),
  671. SSL_CONF_CMD(RequestCAFile, "requestCAFile", SSL_CONF_FLAG_CERTIFICATE,
  672. SSL_CONF_TYPE_FILE),
  673. SSL_CONF_CMD(ClientCAFile, NULL,
  674. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  675. SSL_CONF_TYPE_FILE),
  676. SSL_CONF_CMD(RequestCAPath, NULL, SSL_CONF_FLAG_CERTIFICATE,
  677. SSL_CONF_TYPE_DIR),
  678. SSL_CONF_CMD(ClientCAPath, NULL,
  679. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  680. SSL_CONF_TYPE_DIR),
  681. SSL_CONF_CMD(RequestCAStore, "requestCAStore", SSL_CONF_FLAG_CERTIFICATE,
  682. SSL_CONF_TYPE_STORE),
  683. SSL_CONF_CMD(ClientCAStore, NULL,
  684. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  685. SSL_CONF_TYPE_STORE),
  686. SSL_CONF_CMD(DHParameters, "dhparam",
  687. SSL_CONF_FLAG_SERVER | SSL_CONF_FLAG_CERTIFICATE,
  688. SSL_CONF_TYPE_FILE),
  689. SSL_CONF_CMD_STRING(RecordPadding, "record_padding", 0),
  690. SSL_CONF_CMD_STRING(NumTickets, "num_tickets", SSL_CONF_FLAG_SERVER),
  691. };
  692. /* Supported switches: must match order of switches in ssl_conf_cmds */
  693. static const ssl_switch_tbl ssl_cmd_switches[] = {
  694. {SSL_OP_NO_SSLv3, 0}, /* no_ssl3 */
  695. {SSL_OP_NO_TLSv1, 0}, /* no_tls1 */
  696. {SSL_OP_NO_TLSv1_1, 0}, /* no_tls1_1 */
  697. {SSL_OP_NO_TLSv1_2, 0}, /* no_tls1_2 */
  698. {SSL_OP_NO_TLSv1_3, 0}, /* no_tls1_3 */
  699. {SSL_OP_ALL, 0}, /* bugs */
  700. {SSL_OP_NO_COMPRESSION, 0}, /* no_comp */
  701. {SSL_OP_NO_COMPRESSION, SSL_TFLAG_INV}, /* comp */
  702. {SSL_OP_SINGLE_ECDH_USE, 0}, /* ecdh_single */
  703. {SSL_OP_NO_TICKET, 0}, /* no_ticket */
  704. {SSL_OP_CIPHER_SERVER_PREFERENCE, 0}, /* serverpref */
  705. /* legacy_renegotiation */
  706. {SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION, 0},
  707. /* Allow client renegotiation */
  708. {SSL_OP_ALLOW_CLIENT_RENEGOTIATION, 0},
  709. /* legacy_server_connect */
  710. {SSL_OP_LEGACY_SERVER_CONNECT, 0},
  711. /* no_renegotiation */
  712. {SSL_OP_NO_RENEGOTIATION, 0},
  713. /* no_resumption_on_reneg */
  714. {SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION, 0},
  715. /* no_legacy_server_connect */
  716. {SSL_OP_LEGACY_SERVER_CONNECT, SSL_TFLAG_INV},
  717. /* allow_no_dhe_kex */
  718. {SSL_OP_ALLOW_NO_DHE_KEX, 0},
  719. /* chacha reprioritization */
  720. {SSL_OP_PRIORITIZE_CHACHA, 0},
  721. {SSL_CERT_FLAG_TLS_STRICT, SSL_TFLAG_CERT}, /* strict */
  722. /* no_middlebox */
  723. {SSL_OP_ENABLE_MIDDLEBOX_COMPAT, SSL_TFLAG_INV},
  724. /* anti_replay */
  725. {SSL_OP_NO_ANTI_REPLAY, SSL_TFLAG_INV},
  726. /* no_anti_replay */
  727. {SSL_OP_NO_ANTI_REPLAY, 0},
  728. /* no Encrypt-then-Mac */
  729. {SSL_OP_NO_ENCRYPT_THEN_MAC, 0},
  730. /* no Extended master secret */
  731. {SSL_OP_NO_EXTENDED_MASTER_SECRET, 0},
  732. };
  733. static int ssl_conf_cmd_skip_prefix(SSL_CONF_CTX *cctx, const char **pcmd)
  734. {
  735. if (pcmd == NULL || *pcmd == NULL)
  736. return 0;
  737. /* If a prefix is set, check and skip */
  738. if (cctx->prefix) {
  739. if (strlen(*pcmd) <= cctx->prefixlen)
  740. return 0;
  741. if (cctx->flags & SSL_CONF_FLAG_CMDLINE &&
  742. strncmp(*pcmd, cctx->prefix, cctx->prefixlen))
  743. return 0;
  744. if (cctx->flags & SSL_CONF_FLAG_FILE &&
  745. OPENSSL_strncasecmp(*pcmd, cctx->prefix, cctx->prefixlen))
  746. return 0;
  747. *pcmd += cctx->prefixlen;
  748. } else if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
  749. if (**pcmd != '-' || !(*pcmd)[1])
  750. return 0;
  751. *pcmd += 1;
  752. }
  753. return 1;
  754. }
  755. /* Determine if a command is allowed according to cctx flags */
  756. static int ssl_conf_cmd_allowed(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * t)
  757. {
  758. unsigned int tfl = t->flags;
  759. unsigned int cfl = cctx->flags;
  760. if ((tfl & SSL_CONF_FLAG_SERVER) && !(cfl & SSL_CONF_FLAG_SERVER))
  761. return 0;
  762. if ((tfl & SSL_CONF_FLAG_CLIENT) && !(cfl & SSL_CONF_FLAG_CLIENT))
  763. return 0;
  764. if ((tfl & SSL_CONF_FLAG_CERTIFICATE)
  765. && !(cfl & SSL_CONF_FLAG_CERTIFICATE))
  766. return 0;
  767. return 1;
  768. }
  769. static const ssl_conf_cmd_tbl *ssl_conf_cmd_lookup(SSL_CONF_CTX *cctx,
  770. const char *cmd)
  771. {
  772. const ssl_conf_cmd_tbl *t;
  773. size_t i;
  774. if (cmd == NULL)
  775. return NULL;
  776. /* Look for matching parameter name in table */
  777. for (i = 0, t = ssl_conf_cmds; i < OSSL_NELEM(ssl_conf_cmds); i++, t++) {
  778. if (ssl_conf_cmd_allowed(cctx, t)) {
  779. if (cctx->flags & SSL_CONF_FLAG_CMDLINE) {
  780. if (t->str_cmdline && strcmp(t->str_cmdline, cmd) == 0)
  781. return t;
  782. }
  783. if (cctx->flags & SSL_CONF_FLAG_FILE) {
  784. if (t->str_file && OPENSSL_strcasecmp(t->str_file, cmd) == 0)
  785. return t;
  786. }
  787. }
  788. }
  789. return NULL;
  790. }
  791. static int ctrl_switch_option(SSL_CONF_CTX *cctx, const ssl_conf_cmd_tbl * cmd)
  792. {
  793. /* Find index of command in table */
  794. size_t idx = cmd - ssl_conf_cmds;
  795. const ssl_switch_tbl *scmd;
  796. /* Sanity check index */
  797. if (idx >= OSSL_NELEM(ssl_cmd_switches))
  798. return 0;
  799. /* Obtain switches entry with same index */
  800. scmd = ssl_cmd_switches + idx;
  801. ssl_set_option(cctx, scmd->name_flags, scmd->option_value, 1);
  802. return 1;
  803. }
  804. int SSL_CONF_cmd(SSL_CONF_CTX *cctx, const char *cmd, const char *value)
  805. {
  806. const ssl_conf_cmd_tbl *runcmd;
  807. if (cmd == NULL) {
  808. ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_NULL_CMD_NAME);
  809. return 0;
  810. }
  811. if (!ssl_conf_cmd_skip_prefix(cctx, &cmd))
  812. return -2;
  813. runcmd = ssl_conf_cmd_lookup(cctx, cmd);
  814. if (runcmd) {
  815. int rv;
  816. if (runcmd->value_type == SSL_CONF_TYPE_NONE) {
  817. return ctrl_switch_option(cctx, runcmd);
  818. }
  819. if (value == NULL)
  820. return -3;
  821. rv = runcmd->cmd(cctx, value);
  822. if (rv > 0)
  823. return 2;
  824. if (rv == -2)
  825. return -2;
  826. if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
  827. ERR_raise_data(ERR_LIB_SSL, SSL_R_BAD_VALUE,
  828. "cmd=%s, value=%s", cmd, value);
  829. return 0;
  830. }
  831. if (cctx->flags & SSL_CONF_FLAG_SHOW_ERRORS)
  832. ERR_raise_data(ERR_LIB_SSL, SSL_R_UNKNOWN_CMD_NAME, "cmd=%s", cmd);
  833. return -2;
  834. }
  835. int SSL_CONF_cmd_argv(SSL_CONF_CTX *cctx, int *pargc, char ***pargv)
  836. {
  837. int rv;
  838. const char *arg = NULL, *argn;
  839. if (pargc != NULL && *pargc == 0)
  840. return 0;
  841. if (pargc == NULL || *pargc > 0)
  842. arg = **pargv;
  843. if (arg == NULL)
  844. return 0;
  845. if (pargc == NULL || *pargc > 1)
  846. argn = (*pargv)[1];
  847. else
  848. argn = NULL;
  849. cctx->flags &= ~SSL_CONF_FLAG_FILE;
  850. cctx->flags |= SSL_CONF_FLAG_CMDLINE;
  851. rv = SSL_CONF_cmd(cctx, arg, argn);
  852. if (rv > 0) {
  853. /* Success: update pargc, pargv */
  854. (*pargv) += rv;
  855. if (pargc)
  856. (*pargc) -= rv;
  857. return rv;
  858. }
  859. /* Unknown switch: indicate no arguments processed */
  860. if (rv == -2)
  861. return 0;
  862. /* Some error occurred processing command, return fatal error */
  863. if (rv == 0)
  864. return -1;
  865. return rv;
  866. }
  867. int SSL_CONF_cmd_value_type(SSL_CONF_CTX *cctx, const char *cmd)
  868. {
  869. if (ssl_conf_cmd_skip_prefix(cctx, &cmd)) {
  870. const ssl_conf_cmd_tbl *runcmd;
  871. runcmd = ssl_conf_cmd_lookup(cctx, cmd);
  872. if (runcmd)
  873. return runcmd->value_type;
  874. }
  875. return SSL_CONF_TYPE_UNKNOWN;
  876. }
  877. SSL_CONF_CTX *SSL_CONF_CTX_new(void)
  878. {
  879. SSL_CONF_CTX *ret = OPENSSL_zalloc(sizeof(*ret));
  880. return ret;
  881. }
  882. int SSL_CONF_CTX_finish(SSL_CONF_CTX *cctx)
  883. {
  884. /* See if any certificates are missing private keys */
  885. size_t i;
  886. CERT *c = NULL;
  887. if (cctx->ctx)
  888. c = cctx->ctx->cert;
  889. else if (cctx->ssl)
  890. c = cctx->ssl->cert;
  891. if (c && cctx->flags & SSL_CONF_FLAG_REQUIRE_PRIVATE) {
  892. for (i = 0; i < SSL_PKEY_NUM; i++) {
  893. const char *p = cctx->cert_filename[i];
  894. /*
  895. * If missing private key try to load one from certificate file
  896. */
  897. if (p && !c->pkeys[i].privatekey) {
  898. if (!cmd_PrivateKey(cctx, p))
  899. return 0;
  900. }
  901. }
  902. }
  903. if (cctx->canames) {
  904. if (cctx->ssl)
  905. SSL_set0_CA_list(cctx->ssl, cctx->canames);
  906. else if (cctx->ctx)
  907. SSL_CTX_set0_CA_list(cctx->ctx, cctx->canames);
  908. else
  909. sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
  910. cctx->canames = NULL;
  911. }
  912. return 1;
  913. }
  914. void SSL_CONF_CTX_free(SSL_CONF_CTX *cctx)
  915. {
  916. if (cctx) {
  917. size_t i;
  918. for (i = 0; i < SSL_PKEY_NUM; i++)
  919. OPENSSL_free(cctx->cert_filename[i]);
  920. OPENSSL_free(cctx->prefix);
  921. sk_X509_NAME_pop_free(cctx->canames, X509_NAME_free);
  922. OPENSSL_free(cctx);
  923. }
  924. }
  925. unsigned int SSL_CONF_CTX_set_flags(SSL_CONF_CTX *cctx, unsigned int flags)
  926. {
  927. cctx->flags |= flags;
  928. return cctx->flags;
  929. }
  930. unsigned int SSL_CONF_CTX_clear_flags(SSL_CONF_CTX *cctx, unsigned int flags)
  931. {
  932. cctx->flags &= ~flags;
  933. return cctx->flags;
  934. }
  935. int SSL_CONF_CTX_set1_prefix(SSL_CONF_CTX *cctx, const char *pre)
  936. {
  937. char *tmp = NULL;
  938. if (pre) {
  939. tmp = OPENSSL_strdup(pre);
  940. if (tmp == NULL)
  941. return 0;
  942. }
  943. OPENSSL_free(cctx->prefix);
  944. cctx->prefix = tmp;
  945. if (tmp)
  946. cctx->prefixlen = strlen(tmp);
  947. else
  948. cctx->prefixlen = 0;
  949. return 1;
  950. }
  951. void SSL_CONF_CTX_set_ssl(SSL_CONF_CTX *cctx, SSL *ssl)
  952. {
  953. cctx->ssl = ssl;
  954. cctx->ctx = NULL;
  955. if (ssl) {
  956. cctx->poptions = &ssl->options;
  957. cctx->min_version = &ssl->min_proto_version;
  958. cctx->max_version = &ssl->max_proto_version;
  959. cctx->pcert_flags = &ssl->cert->cert_flags;
  960. cctx->pvfy_flags = &ssl->verify_mode;
  961. } else {
  962. cctx->poptions = NULL;
  963. cctx->min_version = NULL;
  964. cctx->max_version = NULL;
  965. cctx->pcert_flags = NULL;
  966. cctx->pvfy_flags = NULL;
  967. }
  968. }
  969. void SSL_CONF_CTX_set_ssl_ctx(SSL_CONF_CTX *cctx, SSL_CTX *ctx)
  970. {
  971. cctx->ctx = ctx;
  972. cctx->ssl = NULL;
  973. if (ctx) {
  974. cctx->poptions = &ctx->options;
  975. cctx->min_version = &ctx->min_proto_version;
  976. cctx->max_version = &ctx->max_proto_version;
  977. cctx->pcert_flags = &ctx->cert->cert_flags;
  978. cctx->pvfy_flags = &ctx->verify_mode;
  979. } else {
  980. cctx->poptions = NULL;
  981. cctx->min_version = NULL;
  982. cctx->max_version = NULL;
  983. cctx->pcert_flags = NULL;
  984. cctx->pvfy_flags = NULL;
  985. }
  986. }