2347bc98afed4a5ecd3a0566ae00a195ca5682a1
[openssl.git] / doc / ssl / SSL_CTX_set_tmp_rsa_callback.pod
1 =pod
2
3 =head1 NAME
4
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
6
7 =head1 SYNOPSIS
8
9  #include <openssl/ssl.h>
10
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);
15
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)
20
21  RSA *(*tmp_rsa_callback)(SSL *ssl, int is_export, int keylength));
22
23 =head1 DESCRIPTION
24
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>.
28
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>.
31
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.
34
35 SSL_set_tmp_rsa_callback() sets the callback only for B<ssl>.
36
37 SSL_set_tmp_rsa() sets the key only for B<ssl>.
38
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.
41
42 These functions apply to SSL/TLS servers only.
43
44 =head1 NOTES
45
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.
50
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
61 export ciphers.
62
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.
67
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.
78
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.
82
83 =head1 EXAMPLES
84
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.
89
90  ...
91  /* Set up ephemeral RSA stuff */
92  RSA *rsa_512 = NULL;
93  RSA *rsa_1024 = NULL;
94  if (prepare_export_in_advance || always_use_ephemeral_rsa) {
95    rsa_512 = RSA_generate_key(512,RSA_F4,NULL,NULL);
96    if (rsa_512 == NULL)
97      evaluate_error_queue();
98  }
99  if (always_use_ephemeral_rsa) {
100    /* Only spend the time to generate the key, if it will actually be
101       needed */
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);
106  }
107  ...
108
109  RSA *tmp_rsa_callback(SSL *s, int is_export, int keylength)
110  {
111     RSA *rsa_tmp=NULL;
112
113     switch (keylength) {
114     case 512:
115       if (rsa_512)
116         rsa_tmp = rsa_512;
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 */
120       }
121       break;
122     case 1024:
123       if (rsa_1024)
124         rsa_tmp=rsa_1024;
125       else
126         this_should_never_happen_as_we_are_prepared();
127       break;
128     default:
129       /* Generating a key on the fly is very costly, so use what is there */
130       if (rsa_1024)
131         rsa_tmp=rsa_1024;
132       else
133         rsa_tmp=rsa_512; /* Use at least a shorter key */
134     }
135     return(rsa_tmp);
136  }
137
138 =head1 RETURN VALUES
139
140 SSL_CTX_set_tmp_rsa_callback() and SSL_set_tmp_rsa_callback() do not return
141 diagnostic output.
142
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.
145
146 SSL_CTX_need_tmp_rsa() and SSL_need_tmp_rsa() return 1 if a temporary
147 RSA key is needed and 0 otherwise.
148
149 =head1 SEE ALSO
150
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)>
154
155 =cut