1SSL_CTX_set_tmp_dh_callback(3) OpenSSL SSL_CTX_set_tmp_dh_callback(3)
2
3
4
6 SSL_CTX_set_tmp_dh_callback, SSL_CTX_set_tmp_dh, SSL_set_tmp_dh_call‐
7 back, SSL_set_tmp_dh - handle DH keys for ephemeral key exchange
8
10 #include <openssl/ssl.h>
11
12 void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
13 DH *(*tmp_dh_callback)(SSL *ssl, int is_export, int keylength));
14 long SSL_CTX_set_tmp_dh(SSL_CTX *ctx, DH *dh);
15
16 void SSL_set_tmp_dh_callback(SSL_CTX *ctx,
17 DH *(*tmp_dh_callback)(SSL *ssl, int is_export, int keylength));
18 long SSL_set_tmp_dh(SSL *ssl, DH *dh)
19
20 DH *(*tmp_dh_callback)(SSL *ssl, int is_export, int keylength));
21
23 SSL_CTX_set_tmp_dh_callback() sets the callback function for ctx to be
24 used when a DH parameters are required to tmp_dh_callback. The call‐
25 back is inherited by all ssl objects created from ctx.
26
27 SSL_CTX_set_tmp_dh() sets DH parameters to be used to be dh. The key
28 is inherited by all ssl objects created from ctx.
29
30 SSL_set_tmp_dh_callback() sets the callback only for ssl.
31
32 SSL_set_tmp_dh() sets the parameters only for ssl.
33
34 These functions apply to SSL/TLS servers only.
35
37 When using a cipher with RSA authentication, an ephemeral DH key
38 exchange can take place. Ciphers with DSA keys always use ephemeral DH
39 keys as well. In these cases, the session data are negotiated using
40 the ephemeral/temporary DH key and the key supplied and certified by
41 the certificate chain is only used for signing. Anonymous ciphers
42 (without a permanent server key) also use ephemeral DH keys.
43
44 Using ephemeral DH key exchange yields forward secrecy, as the connec‐
45 tion can only be decrypted, when the DH key is known. By generating a
46 temporary DH key inside the server application that is lost when the
47 application is left, it becomes impossible for an attacker to decrypt
48 past sessions, even if he gets hold of the normal (certified) key, as
49 this key was only used for signing.
50
51 In order to perform a DH key exchange the server must use a DH group
52 (DH parameters) and generate a DH key. The server will always generate
53 a new DH key during the negotiation, when the DH parameters are sup‐
54 plied via callback and/or when the SSL_OP_SINGLE_DH_USE option of
55 SSL_CTX_set_options(3) is set. It will immediately create a DH key,
56 when DH parameters are supplied via SSL_CTX_set_tmp_dh() and
57 SSL_OP_SINGLE_DH_USE is not set. In this case, it may happen that a key
58 is generated on initialization without later being needed, while on the
59 other hand the computer time during the negotiation is being saved.
60
61 If "strong" primes were used to generate the DH parameters, it is not
62 strictly necessary to generate a new key for each handshake but it does
63 improve forward secrecy. If it is not assured, that "strong" primes
64 were used (see especially the section about DSA parameters below),
65 SSL_OP_SINGLE_DH_USE must be used in order to prevent small subgroup
66 attacks. Always using SSL_OP_SINGLE_DH_USE has an impact on the com‐
67 puter time needed during negotiation, but it is not very large, so
68 application authors/users should consider to always enable this option.
69
70 As generating DH parameters is extremely time consuming, an application
71 should not generate the parameters on the fly but supply the parame‐
72 ters. DH parameters can be reused, as the actual key is newly gener‐
73 ated during the negotiation. The risk in reusing DH parameters is that
74 an attacker may specialize on a very often used DH group. Applications
75 should therefore generate their own DH parameters during the installa‐
76 tion process using the openssl dhparam(1) application. In order to
77 reduce the computer time needed for this generation, it is possible to
78 use DSA parameters instead (see dhparam(1)), but in this case
79 SSL_OP_SINGLE_DH_USE is mandatory.
80
81 Application authors may compile in DH parameters. Files dh512.pem,
82 dh1024.pem, dh2048.pem, and dh4096 in the 'apps' directory of current
83 version of the OpenSSL distribution contain the 'SKIP' DH parameters,
84 which use safe primes and were generated verifiably pseudo-randomly.
85 These files can be converted into C code using the -C option of the
86 dhparam(1) application. Authors may also generate their own set of
87 parameters using dhparam(1), but a user may not be sure how the parame‐
88 ters were generated. The generation of DH parameters during installa‐
89 tion is therefore recommended.
90
91 An application may either directly specify the DH parameters or can
92 supply the DH parameters via a callback function. The callback approach
93 has the advantage, that the callback may supply DH parameters for dif‐
94 ferent key lengths.
95
96 The tmp_dh_callback is called with the keylength needed and the
97 is_export information. The is_export flag is set, when the ephemeral DH
98 key exchange is performed with an export cipher.
99
101 Handle DH parameters for key lengths of 512 and 1024 bits. (Error han‐
102 dling partly left out.)
103
104 ...
105 /* Set up ephemeral DH stuff */
106 DH *dh_512 = NULL;
107 DH *dh_1024 = NULL;
108 FILE *paramfile;
109
110 ...
111 /* "openssl dhparam -out dh_param_512.pem -2 512" */
112 paramfile = fopen("dh_param_512.pem", "r");
113 if (paramfile) {
114 dh_512 = PEM_read_DHparams(paramfile, NULL, NULL, NULL);
115 fclose(paramfile);
116 }
117 /* "openssl dhparam -out dh_param_1024.pem -2 1024" */
118 paramfile = fopen("dh_param_1024.pem", "r");
119 if (paramfile) {
120 dh_1024 = PEM_read_DHparams(paramfile, NULL, NULL, NULL);
121 fclose(paramfile);
122 }
123 ...
124
125 /* "openssl dhparam -C -2 512" etc... */
126 DH *get_dh512() { ... }
127 DH *get_dh1024() { ... }
128
129 DH *tmp_dh_callback(SSL *s, int is_export, int keylength)
130 {
131 DH *dh_tmp=NULL;
132
133 switch (keylength) {
134 case 512:
135 if (!dh_512)
136 dh_512 = get_dh512();
137 dh_tmp = dh_512;
138 break;
139 case 1024:
140 if (!dh_1024)
141 dh_1024 = get_dh1024();
142 dh_tmp = dh_1024;
143 break;
144 default:
145 /* Generating a key on the fly is very costly, so use what is there */
146 setup_dh_parameters_like_above();
147 }
148 return(dh_tmp);
149 }
150
152 SSL_CTX_set_tmp_dh_callback() and SSL_set_tmp_dh_callback() do not
153 return diagnostic output.
154
155 SSL_CTX_set_tmp_dh() and SSL_set_tmp_dh() do return 1 on success and 0
156 on failure. Check the error queue to find out the reason of failure.
157
159 ssl(3), SSL_CTX_set_cipher_list(3), SSL_CTX_set_tmp_rsa_callback(3),
160 SSL_CTX_set_options(3), ciphers(1), dhparam(1)
161
162
163
1640.9.8b 2001-09-07 SSL_CTX_set_tmp_dh_callback(3)