5 SSL_CTX_set_tmp_rsa_callback, SSL_CTX_set_tmp_rsa, SSL_CTX_need_tmp_rsa, SSL_set_tmp_rsa_callback, SSL_set_tmp_rsa, SSL_need_tmp_rsa - handle RSA keys for ephemeral key exchange
9 #include <openssl/ssl.h>
11 void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
12 RSA *(*tmp_rsa_callback)(SSL *ssl, int is_export, int keylength));
13 long SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, RSA *rsa);
14 long SSL_CTX_need_tmp_rsa(SSL_CTX *ctx);
16 void SSL_set_tmp_rsa_callback(SSL_CTX *ctx,
17 RSA *(*tmp_rsa_callback)(SSL *ssl, int is_export, int keylength));
18 long SSL_set_tmp_rsa(SSL *ssl, RSA *rsa)
19 long SSL_need_tmp_rsa(SSL *ssl)
21 RSA *(*tmp_rsa_callback)(SSL *ssl, int is_export, int keylength));
25 SSL_CTX_set_tmp_rsa_callback() sets the callback function for B<ctx> to be
26 used when a temporary/ephemeral RSA key is required to B<tmp_rsa_callback>.
27 The callback is inherited by all B<ssl> objects created from B<ctx>.
29 SSL_CTX_set_tmp_rsa() sets the temporary/ephemeral RSA key to be used to be
30 B<rsa>. The key is inherited by all B<ssl> objects created from B<ctx>.
32 SSL_CTX_need_tmp_rsa() returns 1, if a temporay/ephemeral RSA key is needed,
33 because a RSA key with a keysize larger than 512 bits is installed.
35 SSL_set_tmp_rsa_callback() sets the callback only for B<ssl>.
37 SSL_set_tmp_rsa() sets the key only for B<ssl>.
39 SSL_need_tmp_rsa() returns 1, if a temporay/ephemeral RSA key is needed,
40 because a RSA key with a keysize larger than 512 bits is installed.
42 These functions apply to SSL/TLS servers only.
46 When using a cipher with RSA authentication, an ephemeral RSA key exchange
47 can take place. In this case the session data are negotiated using the
48 ephemeral/temporary RSA key and the RSA key supplied and certified
49 by the certificate chain is only used for signing.
51 Using ephemeral RSA key exchange yields forward secrecy, as the connection
52 can only be decrypted, when the RSA key is known. By generating a temporary
53 RSA key inside the server application that is lost when the application
54 is left, it becomes impossible for an attacker to decrypt past sessions,
55 even if he gets hold of the normal (certified) RSA key, as this key was
56 only used for signing. The downside is that creating a RSA key is
57 computationally expensive. On OpenSSL servers ephemeral RSA key exchange
58 is therefore disabled by default and must be explicitly enabled using the
59 SSL_OP_EPHEMERAL_RSA option of
60 L<SSL_CTX_set_options(3)|SSL_CTX_set_options(3)>, except for certain
63 Under previous export restrictions, ciphers with RSA keys shorter (512 bits)
64 than the usual key length of 1024 bits were created. To use these ciphers
65 with RSA keys of usual length, an ephemeral key exchange must be performed,
66 as the normal (certified) key cannot be used.
68 An application my either directly specify the key or
69 can supply the key via a callback function. The callback approach has
70 the advantage, that the callback may generate the key only in case it is
71 actually needed. As the generation of a RSA key is however costly, it
72 will lead to a significant delay in the handshake procedure.
73 Another advantage of the callback function is that it can supply keys
74 of different size (e.g. for SSL_OP_EPHEMERAL_RSA usage) while the
75 explicit setting of the key is only useful for key size of 512 bits
76 to satisfy the export restricted ciphers and does give away key length
77 if a longer key would be allowed.
79 The B<tmp_rsa_callback> is called with the B<keylength> needed and
80 the B<is_export> information. The B<is_export> flag is set, when the
81 ephemeral RSA key exchange is performed with an export cipher.
85 Generate temporary RSA keys to prepare ephemeral RSA key exchange. As the
86 generation of a RSA key costs a lot of computer time, it is saved for later
87 reuse. For demonstration purposes, two keys for 512 bits and 1024 bits
88 respectively are generated.
91 /* Set up ephemeral RSA stuff */
94 if (prepare_export_in_advance || always_use_ephemeral_rsa) {
95 rsa_512 = RSA_generate_key(512,RSA_F4,NULL,NULL);
97 evaluate_error_queue();
99 if (always_use_ephemeral_rsa) {
100 /* Only spend the time to generate the key, if it will actually be
102 rsa_1024 = RSA_generate_key(1024,RSA_F4,NULL,NULL);
103 if (rsa_1024 == NULL)
104 evaluate_error_queue();
105 SSL_CTX_set_options(SSL_OP_EPHEMERAL_RSA);
109 RSA *tmp_rsa_callback(SSL *s, int is_export, int keylength)
117 else { /* generate on the fly */
118 rsa_tmp = RSA_generate_key(512,RSA_F4,NULL,NULL);
119 rsa_512 = rsa_tmp; /* Remember for later reuse */
126 this_should_never_happen_as_we_are_prepared();
129 /* Generating a key on the fly is very costly, so use what is there */
133 rsa_tmp=rsa_512; /* Use at least a shorter key */
140 SSL_CTX_set_tmp_rsa_callback() and SSL_set_tmp_rsa_callback() do not return
143 SSL_CTX_set_tmp_rsa() and SSL_set_tmp_rsa() do return 1 on success and 0
144 on failure. Check the error queue to find out the reason of failure.
146 SSL_CTX_need_tmp_rsa() and SSL_need_tmp_rsa() return 1 if a temporary
147 RSA key is needed and 0 otherwise.
151 L<ssl(3)|ssl(3)>, L<SSL_CTX_set_cipher_list(3)|SSL_CTX_set_cipher_list(3)>,
152 L<SSL_CTX_set_options(3)|SSL_CTX_set_options(3)>,
153 L<ciphers(1)|ciphers(1)>