ssl_conf.c 34 KB

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