1*de980744Stb.\" $OpenBSD: SSL_CTX_set_tmp_dh_callback.3,v 1.11 2025/01/18 10:45:12 tb Exp $ 23de50743Sschwarze.\" OpenSSL b97fdb57 Nov 11 09:33:09 2016 +0100 3f1a3c524Sschwarze.\" 43de50743Sschwarze.\" This file was written by Lutz Jaenicke <jaenicke@openssl.org>. 53de50743Sschwarze.\" Copyright (c) 2001, 2014, 2015 The OpenSSL Project. All rights reserved. 6f1a3c524Sschwarze.\" 73de50743Sschwarze.\" Redistribution and use in source and binary forms, with or without 83de50743Sschwarze.\" modification, are permitted provided that the following conditions 93de50743Sschwarze.\" are met: 103de50743Sschwarze.\" 113de50743Sschwarze.\" 1. Redistributions of source code must retain the above copyright 123de50743Sschwarze.\" notice, this list of conditions and the following disclaimer. 133de50743Sschwarze.\" 143de50743Sschwarze.\" 2. Redistributions in binary form must reproduce the above copyright 153de50743Sschwarze.\" notice, this list of conditions and the following disclaimer in 163de50743Sschwarze.\" the documentation and/or other materials provided with the 173de50743Sschwarze.\" distribution. 183de50743Sschwarze.\" 193de50743Sschwarze.\" 3. All advertising materials mentioning features or use of this 203de50743Sschwarze.\" software must display the following acknowledgment: 213de50743Sschwarze.\" "This product includes software developed by the OpenSSL Project 223de50743Sschwarze.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 233de50743Sschwarze.\" 243de50743Sschwarze.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 253de50743Sschwarze.\" endorse or promote products derived from this software without 263de50743Sschwarze.\" prior written permission. For written permission, please contact 273de50743Sschwarze.\" openssl-core@openssl.org. 283de50743Sschwarze.\" 293de50743Sschwarze.\" 5. Products derived from this software may not be called "OpenSSL" 303de50743Sschwarze.\" nor may "OpenSSL" appear in their names without prior written 313de50743Sschwarze.\" permission of the OpenSSL Project. 323de50743Sschwarze.\" 333de50743Sschwarze.\" 6. Redistributions of any form whatsoever must retain the following 343de50743Sschwarze.\" acknowledgment: 353de50743Sschwarze.\" "This product includes software developed by the OpenSSL Project 363de50743Sschwarze.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 373de50743Sschwarze.\" 383de50743Sschwarze.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 393de50743Sschwarze.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 403de50743Sschwarze.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 413de50743Sschwarze.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 423de50743Sschwarze.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 433de50743Sschwarze.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 443de50743Sschwarze.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 453de50743Sschwarze.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 463de50743Sschwarze.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 473de50743Sschwarze.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 483de50743Sschwarze.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 493de50743Sschwarze.\" OF THE POSSIBILITY OF SUCH DAMAGE. 503de50743Sschwarze.\" 51*de980744Stb.Dd $Mdocdate: January 18 2025 $ 52f1a3c524Sschwarze.Dt SSL_CTX_SET_TMP_DH_CALLBACK 3 53f1a3c524Sschwarze.Os 54f1a3c524Sschwarze.Sh NAME 55f1a3c524Sschwarze.Nm SSL_CTX_set_tmp_dh_callback , 56f1a3c524Sschwarze.Nm SSL_CTX_set_tmp_dh , 57f1a3c524Sschwarze.Nm SSL_set_tmp_dh_callback , 58f1a3c524Sschwarze.Nm SSL_set_tmp_dh 59f1a3c524Sschwarze.Nd handle DH keys for ephemeral key exchange 60f1a3c524Sschwarze.Sh SYNOPSIS 61f1a3c524Sschwarze.In openssl/ssl.h 62f1a3c524Sschwarze.Ft void 63f1a3c524Sschwarze.Fo SSL_CTX_set_tmp_dh_callback 64f1a3c524Sschwarze.Fa "SSL_CTX *ctx" 65f1a3c524Sschwarze.Fa "DH *(*tmp_dh_callback)(SSL *ssl, int is_export, int keylength)" 66f1a3c524Sschwarze.Fc 67f1a3c524Sschwarze.Ft long 68f1a3c524Sschwarze.Fn SSL_CTX_set_tmp_dh "SSL_CTX *ctx" "DH *dh" 69f1a3c524Sschwarze.Ft void 70f1a3c524Sschwarze.Fo SSL_set_tmp_dh_callback 71f1a3c524Sschwarze.Fa "SSL *ssl" 72f1a3c524Sschwarze.Fa "DH *(*tmp_dh_callback)(SSL *ssl, int is_export, int keylength" 73f1a3c524Sschwarze.Fc 74f1a3c524Sschwarze.Ft long 75f1a3c524Sschwarze.Fn SSL_set_tmp_dh "SSL *ssl" "DH *dh" 76f1a3c524Sschwarze.Sh DESCRIPTION 77f1a3c524Sschwarze.Fn SSL_CTX_set_tmp_dh_callback 78f1a3c524Sschwarzesets the callback function for 79f1a3c524Sschwarze.Fa ctx 80f1a3c524Sschwarzeto be used when a DH parameters are required to 81f1a3c524Sschwarze.Fa tmp_dh_callback . 82f1a3c524SschwarzeThe callback is inherited by all 83f1a3c524Sschwarze.Vt ssl 84f1a3c524Sschwarzeobjects created from 85f1a3c524Sschwarze.Fa ctx . 86f1a3c524Sschwarze.Pp 87f1a3c524Sschwarze.Fn SSL_CTX_set_tmp_dh 8845224523Sschwarzesets DH parameters to be used by 8945224523Sschwarze.Fa ctx . 90f1a3c524SschwarzeThe key is inherited by all 91f1a3c524Sschwarze.Fa ssl 92f1a3c524Sschwarzeobjects created from 93f1a3c524Sschwarze.Fa ctx . 94f1a3c524Sschwarze.Pp 95f1a3c524Sschwarze.Fn SSL_set_tmp_dh_callback 96f1a3c524Sschwarzesets the callback only for 97f1a3c524Sschwarze.Fa ssl . 98f1a3c524Sschwarze.Pp 99f1a3c524Sschwarze.Fn SSL_set_tmp_dh 100f1a3c524Sschwarzesets the parameters only for 101f1a3c524Sschwarze.Fa ssl . 102f1a3c524Sschwarze.Pp 103f1a3c524SschwarzeThese functions apply to SSL/TLS servers only. 1043de50743Sschwarze.Pp 105f1a3c524SschwarzeWhen using a cipher with RSA authentication, 106f1a3c524Sschwarzean ephemeral DH key exchange can take place. 107f1a3c524SschwarzeIn these cases, the session data are negotiated using the ephemeral/temporary 108f1a3c524SschwarzeDH key and the key supplied and certified by the certificate chain is only used 109f1a3c524Sschwarzefor signing. 110f1a3c524SschwarzeAnonymous ciphers (without a permanent server key) also use ephemeral DH keys. 111f1a3c524Sschwarze.Pp 112f1a3c524SschwarzeUsing ephemeral DH key exchange yields forward secrecy, 113f1a3c524Sschwarzeas the connection can only be decrypted when the DH key is known. 114f1a3c524SschwarzeBy generating a temporary DH key inside the server application that is lost 11525a24f0bSjsgwhen the application is left, it becomes impossible for attackers to decrypt 11625a24f0bSjsgpast sessions, even if they get hold of the normal (certified) key, 117f1a3c524Sschwarzeas this key was only used for signing. 118f1a3c524Sschwarze.Pp 11941ce3b17SnaddyIn order to perform a DH key exchange, the server must use a DH group 120f1a3c524Sschwarze(DH parameters) and generate a DH key. 1213de50743SschwarzeThe server will always generate a new DH key during the negotiation. 122f1a3c524Sschwarze.Pp 123f1a3c524SschwarzeAs generating DH parameters is extremely time consuming, an application should 124f1a3c524Sschwarzenot generate the parameters on the fly but supply the parameters. 125f1a3c524SschwarzeDH parameters can be reused, 126f1a3c524Sschwarzeas the actual key is newly generated during the negotiation. 127f1a3c524SschwarzeThe risk in reusing DH parameters is that an attacker may specialize on a very 128f1a3c524Sschwarzeoften used DH group. 129f1a3c524SschwarzeApplications should therefore generate their own DH parameters during the 1303de50743Sschwarzeinstallation process using the 131f1a3c524Sschwarze.Xr openssl 1 1323de50743Sschwarze.Cm dhparam 133f1a3c524Sschwarzeapplication. 1343de50743SschwarzeThis application guarantees that "strong" primes are used. 135f1a3c524Sschwarze.Pp 136f1a3c524SschwarzeFiles 1373de50743Sschwarze.Pa dh2048.pem 138f1a3c524Sschwarzeand 139f1a3c524Sschwarze.Pa dh4096.pem 140f1a3c524Sschwarzein the 141f1a3c524Sschwarze.Pa apps 142f1a3c524Sschwarzedirectory of the current version of the OpenSSL distribution contain the 143f1a3c524Sschwarze.Sq SKIP 144f1a3c524SschwarzeDH parameters, 145f1a3c524Sschwarzewhich use safe primes and were generated verifiably pseudo-randomly. 146f1a3c524SschwarzeThese files can be converted into C code using the 147f1a3c524Sschwarze.Fl C 148f1a3c524Sschwarzeoption of the 149f1a3c524Sschwarze.Xr openssl 1 1503de50743Sschwarze.Cm dhparam 151f1a3c524Sschwarzeapplication. 1523de50743SschwarzeGeneration of custom DH parameters during installation should still 1533de50743Sschwarzebe preferred to stop an attacker from specializing on a commonly 1543de50743Sschwarzeused group. 1553de50743SschwarzeThe file 1563de50743Sschwarze.Pa dh1024.pem 1573de50743Sschwarzecontains old parameters that must not be used by applications. 158f1a3c524Sschwarze.Pp 159f1a3c524SschwarzeAn application may either directly specify the DH parameters or can supply the 160f1a3c524SschwarzeDH parameters via a callback function. 161f1a3c524Sschwarze.Pp 1623de50743SschwarzePrevious versions of the callback used 1633de50743Sschwarze.Fa is_export 1643de50743Sschwarzeand 165f1a3c524Sschwarze.Fa keylength 1663de50743Sschwarzeparameters to control parameter generation for export and non-export 1673de50743Sschwarzecipher suites. 1683de50743SschwarzeModern servers that do not support export ciphersuites are advised 1693de50743Sschwarzeto either use 1703de50743Sschwarze.Fn SSL_CTX_set_tmp_dh 1713de50743Sschwarzeor alternatively, use the callback but ignore 1723de50743Sschwarze.Fa keylength 1733de50743Sschwarzeand 174f1a3c524Sschwarze.Fa is_export 1753de50743Sschwarzeand simply supply at least 2048-bit parameters in the callback. 176f1a3c524Sschwarze.Sh RETURN VALUES 177f1a3c524Sschwarze.Fn SSL_CTX_set_tmp_dh 178f1a3c524Sschwarzeand 179f1a3c524Sschwarze.Fn SSL_set_tmp_dh 180f1a3c524Sschwarzedo return 1 on success and 0 on failure. 181f1a3c524SschwarzeCheck the error queue to find out the reason of failure. 182f1a3c524Sschwarze.Sh EXAMPLES 1833de50743SschwarzeSet up DH parameters with a key length of 2048 bits. 1843de50743SschwarzeError handling is partly left out. 1853de50743Sschwarze.Pp 1863de50743SschwarzeCommand-line parameter generation: 1873de50743Sschwarze.Pp 1883de50743Sschwarze.Dl openssl dhparam -out dh_param_2048.pem 2048 1893de50743Sschwarze.Pp 1903de50743SschwarzeCode for setting up parameters during server initialization: 191f1a3c524Sschwarze.Bd -literal 1923de50743SschwarzeSSL_CTX ctx = SSL_CTX_new(); 193f1a3c524Sschwarze\&... 1943de50743Sschwarze 1953de50743Sschwarze/* Set up ephemeral DH parameters. */ 1963de50743SschwarzeDH *dh_2048 = NULL; 197f1a3c524SschwarzeFILE *paramfile; 1983de50743Sschwarzeparamfile = fopen("dh_param_2048.pem", "r"); 199f1a3c524Sschwarzeif (paramfile) { 2003de50743Sschwarze dh_2048 = PEM_read_DHparams(paramfile, NULL, NULL, NULL); 201f1a3c524Sschwarze fclose(paramfile); 2023de50743Sschwarze} else { 2033de50743Sschwarze /* Error. */ 204f1a3c524Sschwarze} 2053de50743Sschwarzeif (dh_2048 == NULL) { 2063de50743Sschwarze /* Error. */ 207f1a3c524Sschwarze} 2083de50743Sschwarzeif (SSL_CTX_set_tmp_dh(ctx, dh_2048) != 1) { 2093de50743Sschwarze /* Error. */ 210f1a3c524Sschwarze} 211f1a3c524Sschwarze.Ed 212f1a3c524Sschwarze.Sh SEE ALSO 213f1a3c524Sschwarze.Xr openssl 1 , 214f1a3c524Sschwarze.Xr ssl 3 , 215f1a3c524Sschwarze.Xr SSL_CTX_set_cipher_list 3 , 216ee3c233fSschwarze.Xr SSL_CTX_set_options 3 , 217ee3c233fSschwarze.Xr SSL_set_tmp_ecdh 3 2188fba1ec8Sschwarze.Sh HISTORY 2198fba1ec8Sschwarze.Fn SSL_CTX_set_tmp_dh_callback 2208fba1ec8Sschwarzeand 2218fba1ec8Sschwarze.Fn SSL_CTX_set_tmp_dh 22210e00d17Sschwarzefirst appeared in SSLeay 0.8.0 and have been available since 2238fba1ec8Sschwarze.Ox 2.4 . 224a14b330bSschwarze.Pp 225a14b330bSschwarze.Fn SSL_set_tmp_dh_callback 226a14b330bSschwarzeand 227a14b330bSschwarze.Fn SSL_set_tmp_dh 228a14b330bSschwarzefirst appeared in OpenSSL 0.9.2b and have been available since 229a14b330bSschwarze.Ox 2.6 . 230