191094Sdes=pod
2115619Sdes
3228690Sdes=head1 NAME
491094Sdes
591094SdesSSL_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
691094Sdes
799158Sdes=head1 SYNOPSIS
899158Sdes
999158Sdes #include <openssl/ssl.h>
1091094Sdes
1191094Sdes void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
1291094Sdes            RSA *(*tmp_rsa_callback)(SSL *ssl, int is_export, int keylength));
1391094Sdes long SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, RSA *rsa);
1491094Sdes long SSL_CTX_need_tmp_rsa(SSL_CTX *ctx);
1591094Sdes
1691094Sdes void SSL_set_tmp_rsa_callback(SSL_CTX *ctx,
1791094Sdes            RSA *(*tmp_rsa_callback)(SSL *ssl, int is_export, int keylength));
1891094Sdes long SSL_set_tmp_rsa(SSL *ssl, RSA *rsa)
1991094Sdes long SSL_need_tmp_rsa(SSL *ssl)
2091094Sdes
2191094Sdes RSA *(*tmp_rsa_callback)(SSL *ssl, int is_export, int keylength);
2291094Sdes
2391094Sdes=head1 DESCRIPTION
2491094Sdes
2591094SdesSSL_CTX_set_tmp_rsa_callback() sets the callback function for B<ctx> to be
2691094Sdesused when a temporary/ephemeral RSA key is required to B<tmp_rsa_callback>.
2791094SdesThe callback is inherited by all SSL objects newly created from B<ctx>
2891094Sdeswith <SSL_new(3)|SSL_new(3)>. Already created SSL objects are not affected.
2991094Sdes
3091094SdesSSL_CTX_set_tmp_rsa() sets the temporary/ephemeral RSA key to be used to be
3191094SdesB<rsa>. The key is inherited by all SSL objects newly created from B<ctx>
3291094Sdeswith <SSL_new(3)|SSL_new(3)>. Already created SSL objects are not affected.
3391094Sdes
3491094SdesSSL_CTX_need_tmp_rsa() returns 1, if a temporary/ephemeral RSA key is needed
35255376Sdesfor RSA-based strength-limited 'exportable' ciphersuites because a RSA key
3691094Sdeswith a keysize larger than 512 bits is installed.
3791094Sdes
38228690SdesSSL_set_tmp_rsa_callback() sets the callback only for B<ssl>.
39228690Sdes
40228690SdesSSL_set_tmp_rsa() sets the key only for B<ssl>.
41228690Sdes
4291094SdesSSL_need_tmp_rsa() returns 1, if a temporary/ephemeral RSA key is needed,
4391094Sdesfor RSA-based strength-limited 'exportable' ciphersuites because a RSA key
4491100Sdeswith a keysize larger than 512 bits is installed.
4591100Sdes
4691100SdesThese functions apply to SSL/TLS servers only.
4791100Sdes
4891100Sdes=head1 NOTES
4991100Sdes
5091100SdesWhen using a cipher with RSA authentication, an ephemeral RSA key exchange
5191094Sdescan take place. In this case the session data are negotiated using the
5291094Sdesephemeral/temporary RSA key and the RSA key supplied and certified
5391094Sdesby the certificate chain is only used for signing.
5491094Sdes
5591094SdesUnder previous export restrictions, ciphers with RSA keys shorter (512 bits)
5691094Sdesthan the usual key length of 1024 bits were created. To use these ciphers
5791094Sdeswith RSA keys of usual length, an ephemeral key exchange must be performed,
5891094Sdesas the normal (certified) key cannot be directly used.
5991094Sdes
60107937SdesUsing ephemeral RSA key exchange yields forward secrecy, as the connection
61107937Sdescan only be decrypted, when the RSA key is known. By generating a temporary
6291094SdesRSA key inside the server application that is lost when the application
6391100Sdesis left, it becomes impossible for an attacker to decrypt past sessions,
6491100Sdeseven if he gets hold of the normal (certified) RSA key, as this key was
6591100Sdesused for signing only. The downside is that creating a RSA key is
6691100Sdescomputationally expensive.
67
68Additionally, the use of ephemeral RSA key exchange is only allowed in
69the TLS standard, when the RSA key can be used for signing only, that is
70for export ciphers. Using ephemeral RSA key exchange for other purposes
71violates the standard and can break interoperability with clients.
72It is therefore strongly recommended to not use ephemeral RSA key
73exchange and use EDH (Ephemeral Diffie-Hellman) key exchange instead
74in order to achieve forward secrecy (see
75L<SSL_CTX_set_tmp_dh_callback(3)|SSL_CTX_set_tmp_dh_callback(3)>).
76
77An application may either directly specify the key or can supply the key via a
78callback function. The callback approach has the advantage, that the callback
79may generate the key only in case it is actually needed. As the generation of a
80RSA key is however costly, it will lead to a significant delay in the handshake
81procedure.  Another advantage of the callback function is that it can supply
82keys of different size while the explicit setting of the key is only useful for
83key size of 512 bits to satisfy the export restricted ciphers and does give
84away key length if a longer key would be allowed.
85
86The B<tmp_rsa_callback> is called with the B<keylength> needed and
87the B<is_export> information. The B<is_export> flag is set, when the
88ephemeral RSA key exchange is performed with an export cipher.
89
90=head1 EXAMPLES
91
92Generate temporary RSA keys to prepare ephemeral RSA key exchange. As the
93generation of a RSA key costs a lot of computer time, they saved for later
94reuse. For demonstration purposes, two keys for 512 bits and 1024 bits
95respectively are generated.
96
97 ...
98 /* Set up ephemeral RSA stuff */
99 RSA *rsa_512 = NULL;
100 RSA *rsa_1024 = NULL;
101
102 rsa_512 = RSA_generate_key(512,RSA_F4,NULL,NULL);
103 if (rsa_512 == NULL)
104     evaluate_error_queue();
105
106 rsa_1024 = RSA_generate_key(1024,RSA_F4,NULL,NULL);
107 if (rsa_1024 == NULL)
108   evaluate_error_queue();
109
110 ...
111
112 RSA *tmp_rsa_callback(SSL *s, int is_export, int keylength)
113 {
114    RSA *rsa_tmp=NULL;
115
116    switch (keylength) {
117    case 512:
118      if (rsa_512)
119        rsa_tmp = rsa_512;
120      else { /* generate on the fly, should not happen in this example */
121        rsa_tmp = RSA_generate_key(keylength,RSA_F4,NULL,NULL);
122        rsa_512 = rsa_tmp; /* Remember for later reuse */
123      }
124      break;
125    case 1024:
126      if (rsa_1024)
127        rsa_tmp=rsa_1024;
128      else
129        should_not_happen_in_this_example();
130      break;
131    default:
132      /* Generating a key on the fly is very costly, so use what is there */
133      if (rsa_1024)
134        rsa_tmp=rsa_1024;
135      else
136        rsa_tmp=rsa_512; /* Use at least a shorter key */
137    }
138    return(rsa_tmp);
139 }
140
141=head1 RETURN VALUES
142
143SSL_CTX_set_tmp_rsa_callback() and SSL_set_tmp_rsa_callback() do not return
144diagnostic output.
145
146SSL_CTX_set_tmp_rsa() and SSL_set_tmp_rsa() do return 1 on success and 0
147on failure. Check the error queue to find out the reason of failure.
148
149SSL_CTX_need_tmp_rsa() and SSL_need_tmp_rsa() return 1 if a temporary
150RSA key is needed and 0 otherwise.
151
152=head1 SEE ALSO
153
154L<ssl(3)|ssl(3)>, L<SSL_CTX_set_cipher_list(3)|SSL_CTX_set_cipher_list(3)>,
155L<SSL_CTX_set_options(3)|SSL_CTX_set_options(3)>,
156L<SSL_CTX_set_tmp_dh_callback(3)|SSL_CTX_set_tmp_dh_callback(3)>,
157L<SSL_new(3)|SSL_new(3)>, L<ciphers(1)|ciphers(1)>
158
159=cut
160