ssl_conf.c 33 KB

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