1.\" $OpenBSD: SSL_CTX_set_tmp_dh_callback.3,v 1.11 2025/01/18 10:45:12 tb Exp $ 2.\" OpenSSL b97fdb57 Nov 11 09:33:09 2016 +0100 3.\" 4.\" This file was written by Lutz Jaenicke <jaenicke@openssl.org>. 5.\" Copyright (c) 2001, 2014, 2015 The OpenSSL Project. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in 16.\" the documentation and/or other materials provided with the 17.\" distribution. 18.\" 19.\" 3. All advertising materials mentioning features or use of this 20.\" software must display the following acknowledgment: 21.\" "This product includes software developed by the OpenSSL Project 22.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 23.\" 24.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 25.\" endorse or promote products derived from this software without 26.\" prior written permission. For written permission, please contact 27.\" openssl-core@openssl.org. 28.\" 29.\" 5. Products derived from this software may not be called "OpenSSL" 30.\" nor may "OpenSSL" appear in their names without prior written 31.\" permission of the OpenSSL Project. 32.\" 33.\" 6. Redistributions of any form whatsoever must retain the following 34.\" acknowledgment: 35.\" "This product includes software developed by the OpenSSL Project 36.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 37.\" 38.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 39.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 40.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 41.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 42.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 47.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 48.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 49.\" OF THE POSSIBILITY OF SUCH DAMAGE. 50.\" 51.Dd $Mdocdate: January 18 2025 $ 52.Dt SSL_CTX_SET_TMP_DH_CALLBACK 3 53.Os 54.Sh NAME 55.Nm SSL_CTX_set_tmp_dh_callback , 56.Nm SSL_CTX_set_tmp_dh , 57.Nm SSL_set_tmp_dh_callback , 58.Nm SSL_set_tmp_dh 59.Nd handle DH keys for ephemeral key exchange 60.Sh SYNOPSIS 61.In openssl/ssl.h 62.Ft void 63.Fo SSL_CTX_set_tmp_dh_callback 64.Fa "SSL_CTX *ctx" 65.Fa "DH *(*tmp_dh_callback)(SSL *ssl, int is_export, int keylength)" 66.Fc 67.Ft long 68.Fn SSL_CTX_set_tmp_dh "SSL_CTX *ctx" "DH *dh" 69.Ft void 70.Fo SSL_set_tmp_dh_callback 71.Fa "SSL *ssl" 72.Fa "DH *(*tmp_dh_callback)(SSL *ssl, int is_export, int keylength" 73.Fc 74.Ft long 75.Fn SSL_set_tmp_dh "SSL *ssl" "DH *dh" 76.Sh DESCRIPTION 77.Fn SSL_CTX_set_tmp_dh_callback 78sets the callback function for 79.Fa ctx 80to be used when a DH parameters are required to 81.Fa tmp_dh_callback . 82The callback is inherited by all 83.Vt ssl 84objects created from 85.Fa ctx . 86.Pp 87.Fn SSL_CTX_set_tmp_dh 88sets DH parameters to be used by 89.Fa ctx . 90The key is inherited by all 91.Fa ssl 92objects created from 93.Fa ctx . 94.Pp 95.Fn SSL_set_tmp_dh_callback 96sets the callback only for 97.Fa ssl . 98.Pp 99.Fn SSL_set_tmp_dh 100sets the parameters only for 101.Fa ssl . 102.Pp 103These functions apply to SSL/TLS servers only. 104.Pp 105When using a cipher with RSA authentication, 106an ephemeral DH key exchange can take place. 107In these cases, the session data are negotiated using the ephemeral/temporary 108DH key and the key supplied and certified by the certificate chain is only used 109for signing. 110Anonymous ciphers (without a permanent server key) also use ephemeral DH keys. 111.Pp 112Using ephemeral DH key exchange yields forward secrecy, 113as the connection can only be decrypted when the DH key is known. 114By generating a temporary DH key inside the server application that is lost 115when the application is left, it becomes impossible for attackers to decrypt 116past sessions, even if they get hold of the normal (certified) key, 117as this key was only used for signing. 118.Pp 119In order to perform a DH key exchange, the server must use a DH group 120(DH parameters) and generate a DH key. 121The server will always generate a new DH key during the negotiation. 122.Pp 123As generating DH parameters is extremely time consuming, an application should 124not generate the parameters on the fly but supply the parameters. 125DH parameters can be reused, 126as the actual key is newly generated during the negotiation. 127The risk in reusing DH parameters is that an attacker may specialize on a very 128often used DH group. 129Applications should therefore generate their own DH parameters during the 130installation process using the 131.Xr openssl 1 132.Cm dhparam 133application. 134This application guarantees that "strong" primes are used. 135.Pp 136Files 137.Pa dh2048.pem 138and 139.Pa dh4096.pem 140in the 141.Pa apps 142directory of the current version of the OpenSSL distribution contain the 143.Sq SKIP 144DH parameters, 145which use safe primes and were generated verifiably pseudo-randomly. 146These files can be converted into C code using the 147.Fl C 148option of the 149.Xr openssl 1 150.Cm dhparam 151application. 152Generation of custom DH parameters during installation should still 153be preferred to stop an attacker from specializing on a commonly 154used group. 155The file 156.Pa dh1024.pem 157contains old parameters that must not be used by applications. 158.Pp 159An application may either directly specify the DH parameters or can supply the 160DH parameters via a callback function. 161.Pp 162Previous versions of the callback used 163.Fa is_export 164and 165.Fa keylength 166parameters to control parameter generation for export and non-export 167cipher suites. 168Modern servers that do not support export ciphersuites are advised 169to either use 170.Fn SSL_CTX_set_tmp_dh 171or alternatively, use the callback but ignore 172.Fa keylength 173and 174.Fa is_export 175and simply supply at least 2048-bit parameters in the callback. 176.Sh RETURN VALUES 177.Fn SSL_CTX_set_tmp_dh 178and 179.Fn SSL_set_tmp_dh 180do return 1 on success and 0 on failure. 181Check the error queue to find out the reason of failure. 182.Sh EXAMPLES 183Set up DH parameters with a key length of 2048 bits. 184Error handling is partly left out. 185.Pp 186Command-line parameter generation: 187.Pp 188.Dl openssl dhparam -out dh_param_2048.pem 2048 189.Pp 190Code for setting up parameters during server initialization: 191.Bd -literal 192SSL_CTX ctx = SSL_CTX_new(); 193\&... 194 195/* Set up ephemeral DH parameters. */ 196DH *dh_2048 = NULL; 197FILE *paramfile; 198paramfile = fopen("dh_param_2048.pem", "r"); 199if (paramfile) { 200 dh_2048 = PEM_read_DHparams(paramfile, NULL, NULL, NULL); 201 fclose(paramfile); 202} else { 203 /* Error. */ 204} 205if (dh_2048 == NULL) { 206 /* Error. */ 207} 208if (SSL_CTX_set_tmp_dh(ctx, dh_2048) != 1) { 209 /* Error. */ 210} 211.Ed 212.Sh SEE ALSO 213.Xr openssl 1 , 214.Xr ssl 3 , 215.Xr SSL_CTX_set_cipher_list 3 , 216.Xr SSL_CTX_set_options 3 , 217.Xr SSL_set_tmp_ecdh 3 218.Sh HISTORY 219.Fn SSL_CTX_set_tmp_dh_callback 220and 221.Fn SSL_CTX_set_tmp_dh 222first appeared in SSLeay 0.8.0 and have been available since 223.Ox 2.4 . 224.Pp 225.Fn SSL_set_tmp_dh_callback 226and 227.Fn SSL_set_tmp_dh 228first appeared in OpenSSL 0.9.2b and have been available since 229.Ox 2.6 . 230