1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
.\" $OpenBSD: SSL_CTX_set_tmp_rsa_callback.3,v 1.3 2016/12/06 22:55:35 schwarze Exp $
.\" OpenSSL 0b30fc90 Dec 19 15:23:05 2013 -0500
.\"
.\" This file was written by Lutz Jaenicke <jaenicke@openssl.org>.
.\" Copyright (c) 2001, 2006, 2013 The OpenSSL Project. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\"
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\"
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in
.\" the documentation and/or other materials provided with the
.\" distribution.
.\"
.\" 3. All advertising materials mentioning features or use of this
.\" software must display the following acknowledgment:
.\" "This product includes software developed by the OpenSSL Project
.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
.\"
.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
.\" endorse or promote products derived from this software without
.\" prior written permission. For written permission, please contact
.\" openssl-core@openssl.org.
.\"
.\" 5. Products derived from this software may not be called "OpenSSL"
.\" nor may "OpenSSL" appear in their names without prior written
.\" permission of the OpenSSL Project.
.\"
.\" 6. Redistributions of any form whatsoever must retain the following
.\" acknowledgment:
.\" "This product includes software developed by the OpenSSL Project
.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
.\" OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: December 6 2016 $
.Dt SSL_CTX_SET_TMP_RSA_CALLBACK.POD 3
.Os
.Sh NAME
.Nm SSL_CTX_set_tmp_rsa_callback ,
.Nm SSL_CTX_set_tmp_rsa ,
.Nm SSL_CTX_need_tmp_RSA ,
.Nm SSL_set_tmp_rsa_callback ,
.Nm SSL_set_tmp_rsa ,
.Nm SSL_need_tmp_rsa
.Nd handle RSA keys for ephemeral key exchange
.Sh SYNOPSIS
.In openssl/ssl.h
.Ft void
.Fo SSL_CTX_set_tmp_rsa_callback
.Fa "SSL_CTX *ctx"
.Fa "RSA *(*tmp_rsa_callback)(SSL *ssl, int is_export, int keylength)"
.Fc
.Ft long
.Fn SSL_CTX_set_tmp_rsa "SSL_CTX *ctx" "RSA *rsa"
.Ft long
.Fn SSL_CTX_need_tmp_RSA "SSL_CTX *ctx"
.Ft void
.Fo SSL_set_tmp_rsa_callback
.Fa "SSL_CTX *ctx"
.Fa "RSA *(*tmp_rsa_callback)(SSL *ssl, int is_export, int keylength)"
.Fc
.Ft long
.Fn SSL_set_tmp_rsa "SSL *ssl" "RSA *rsa"
.Ft long
.Fn SSL_need_tmp_rsa "SSL *ssl"
.Ft RSA *
.Fn "(*tmp_rsa_callback)" "SSL *ssl" "int is_export" "int keylength"
.Sh DESCRIPTION
.Fn SSL_CTX_set_tmp_rsa_callback
sets the callback function for
.Fa ctx
to be used when a temporary/ephemeral RSA key is required to
.Fa tmp_rsa_callback .
The callback is inherited by all
.Vt SSL
objects newly created from
.Fa ctx
with
.Xr SSL_new 3 .
Already created SSL objects are not affected.
.Pp
.Fn SSL_CTX_set_tmp_rsa
sets the temporary/ephemeral RSA key to be used to be
.Fa rsa .
The key is inherited by all
.Vt SSL
objects newly created from
.Fa ctx
with
.Xr SSL_new 3 .
Already created SSL objects are not affected.
.Pp
.Fn SSL_CTX_need_tmp_RSA
returns 1,
if a temporary/ephemeral RSA key is needed for RSA-based strength-limited
.Sq exportable
ciphersuites because a RSA key with a keysize larger than 512 bits is installed.
.Pp
.Fn SSL_set_tmp_rsa_callback
sets the callback only for
.Fa ssl .
.Pp
.Fn SSL_set_tmp_rsa
sets the key only for
.Fa ssl .
.Pp
.Fn SSL_need_tmp_rsa
returns 1,
if a temporary/ephemeral RSA key is needed for RSA-based strength-limited
.Sq exportable
ciphersuites because a RSA key with a keysize larger than 512 bits is installed.
.Pp
These functions apply to SSL/TLS servers only.
.Sh NOTES
When using a cipher with RSA authentication,
an ephemeral RSA key exchange can take place.
In this case the session data are negotiated using the ephemeral/temporary RSA
key and the RSA key supplied and certified by the certificate chain is only
used for signing.
.Pp
Under previous export restrictions, ciphers with RSA keys shorter (512 bits)
than the usual key length of 1024 bits were created.
To use these ciphers with RSA keys of usual length, an ephemeral key exchange
must be performed, as the normal (certified) key cannot be directly used.
.Pp
Using ephemeral RSA key exchange yields forward secrecy,
as the connection can only be decrypted when the RSA key is known.
By generating a temporary RSA key inside the server application that is lost
when the application is left, it becomes impossible for an attacker to decrypt
past sessions, even if he gets hold of the normal (certified) RSA key,
as this key was used for signing only.
The downside is that creating a RSA key is computationally expensive.
.Pp
Additionally, the use of ephemeral RSA key exchange is only allowed in the TLS
standard when the RSA key can be used for signing only, that is,
for export ciphers.
Using ephemeral RSA key exchange for other purposes violates the standard and
can break interoperability with clients.
It is therefore strongly recommended to not use ephemeral RSA key exchange and
use DHE (Ephemeral Diffie-Hellman) key exchange instead in order to achieve
forward secrecy (see
.Xr SSL_CTX_set_tmp_dh_callback 3 ) .
.Pp
On OpenSSL servers ephemeral RSA key exchange is therefore disabled by default
and must be explicitly enabled using the
.Dv SSL_OP_EPHEMERAL_RSA
option of
.Xr SSL_CTX_set_options 3 ,
violating the TLS/SSL
standard.
When ephemeral RSA key exchange is required for export ciphers,
it will automatically be used without this option!
.Pp
An application may either directly specify the key or can supply the key via
a callback function.
The callback approach has the advantage that the callback may generate the key
only in case it is actually needed.
However, as the generation of a RSA key is costly,
it will lead to a significant delay in the handshake procedure.
Another advantage of the callback function is that it can supply keys of
different size (e.g., for
.Dv SSL_OP_EPHEMERAL_RSA
usage) while the explicit setting of the key is only useful for key size of
512 bits to satisfy the export restricted ciphers and does give away key length
if a longer key would be allowed.
.Pp
The
.Fa tmp_rsa_callback
is called with the
.Fa keylength
needed and the
.Fa is_export
information.
The
.Fa is_export
flag is set when the ephemeral RSA key exchange is performed with an export
cipher.
.Sh RETURN VALUES
.Fn SSL_CTX_set_tmp_rsa_callback
and
.Fn SSL_set_tmp_rsa_callback
do not return diagnostic output.
.Pp
.Fn SSL_CTX_set_tmp_rsa
and
.Fn SSL_set_tmp_rsa
return 1 on success and 0 on failure.
Check the error queue to find out the reason of failure.
.Pp
.Fn SSL_CTX_need_tmp_RSA
and
.Fn SSL_need_tmp_rsa
return 1 if a temporary RSA key is needed and 0 otherwise.
.Sh EXAMPLES
Generate temporary RSA keys to prepare ephemeral RSA key exchange.
As the generation of a RSA key costs a lot of computer time,
they are saved for later reuse.
For demonstration purposes, two keys for 512 bits and 1024 bits
respectively are generated.
.Bd -literal
\&...
/* Set up ephemeral RSA stuff */
RSA *rsa_512 = NULL;
RSA *rsa_1024 = NULL;
rsa_512 = RSA_generate_key(512, RSA_F4, NULL, NULL);
if (rsa_512 == NULL)
evaluate_error_queue();
rsa_1024 = RSA_generate_key(1024, RSA_F4, NULL, NULL);
if (rsa_1024 == NULL)
evaluate_error_queue();
\&...
RSA *
tmp_rsa_callback(SSL *s, int is_export, int keylength)
{
RSA *rsa_tmp = NULL;
switch (keylength) {
case 512:
if (rsa_512)
rsa_tmp = rsa_512;
else {
/*
* generate on the fly,
* should not happen in this example
*/
rsa_tmp = RSA_generate_key(keylength, RSA_F4, NULL,
NULL);
rsa_512 = rsa_tmp; /* Remember for later reuse */
}
break;
case 1024:
if (rsa_1024)
rsa_tmp = rsa_1024;
else
should_not_happen_in_this_example();
break;
default:
/*
* Generating a key on the fly is very costly,
* so use what is there
*/
if (rsa_1024)
rsa_tmp = rsa_1024;
else
/* Use at least a shorter key */
rsa_tmp = rsa_512;
}
return rsa_tmp;
}
.Ed
.Sh SEE ALSO
.Xr openssl 1 ,
.Xr ssl 3 ,
.Xr SSL_CTX_set_cipher_list 3 ,
.Xr SSL_CTX_set_options 3 ,
.Xr SSL_CTX_set_tmp_dh_callback 3 ,
.Xr SSL_new 3
|