Rename INIT funtions, deprecate old ones.
[openssl.git] / ssl / methods.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57 /* ====================================================================
58  * Copyright (c) 1998-2015 The OpenSSL Project.  All rights reserved.
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  *    notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in
69  *    the documentation and/or other materials provided with the
70  *    distribution.
71  *
72  * 3. All advertising materials mentioning features or use of this
73  *    software must display the following acknowledgment:
74  *    "This product includes software developed by the OpenSSL Project
75  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76  *
77  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78  *    endorse or promote products derived from this software without
79  *    prior written permission. For written permission, please contact
80  *    openssl-core@openssl.org.
81  *
82  * 5. Products derived from this software may not be called "OpenSSL"
83  *    nor may "OpenSSL" appear in their names without prior written
84  *    permission of the OpenSSL Project.
85  *
86  * 6. Redistributions of any form whatsoever must retain the following
87  *    acknowledgment:
88  *    "This product includes software developed by the OpenSSL Project
89  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90  *
91  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
95  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102  * OF THE POSSIBILITY OF SUCH DAMAGE.
103  * ====================================================================
104  *
105  * This product includes cryptographic software written by Eric Young
106  * (eay@cryptsoft.com).  This product includes software written by Tim
107  * Hudson (tjh@cryptsoft.com).
108  *
109  */
110  
111 #include <stdio.h>
112 #include <openssl/objects.h>
113 #include "ssl_locl.h"
114
115 /*
116  * TLS/SSLv3 methods
117  */
118
119 static const SSL_METHOD *tls1_get_method(int ver)
120 {
121     if (ver == TLS_ANY_VERSION)
122         return TLS_method();
123 #ifndef OPENSSL_NO_TLS1_2
124     if (ver == TLS1_2_VERSION)
125         return TLSv1_2_method();
126 #endif
127 #ifndef OPENSSL_NO_TLS1_1
128     if (ver == TLS1_1_VERSION)
129         return TLSv1_1_method();
130 #endif
131 #ifndef OPENSSL_NO_TLS1
132     if (ver == TLS1_VERSION)
133         return TLSv1_method();
134 #endif
135 #ifndef OPENSSL_NO_SSL3
136     if (ver == SSL3_VERSION)
137         return (SSLv3_method());
138     else
139 #endif
140     return NULL;
141 }
142
143 IMPLEMENT_tls_meth_func(TLS_ANY_VERSION, 0, 0,
144                         TLS_method,
145                         ossl_statem_accept,
146                         ossl_statem_connect, tls1_get_method, TLSv1_2_enc_data)
147
148 #ifndef OPENSSL_NO_TLS1_2_METHOD
149 IMPLEMENT_tls_meth_func(TLS1_2_VERSION, 0, SSL_OP_NO_TLSv1_2,
150                         TLSv1_2_method,
151                         ossl_statem_accept,
152                         ossl_statem_connect, tls1_get_method, TLSv1_2_enc_data)
153 #endif
154
155 #ifndef OPENSSL_NO_TLS1_1_METHOD
156 IMPLEMENT_tls_meth_func(TLS1_1_VERSION, SSL_METHOD_NO_SUITEB, SSL_OP_NO_TLSv1_1,
157                         TLSv1_1_method,
158                         ossl_statem_accept,
159                         ossl_statem_connect, tls1_get_method, TLSv1_1_enc_data)
160 #endif
161
162 #ifndef OPENSSL_NO_TLS1_METHOD
163 IMPLEMENT_tls_meth_func(TLS1_VERSION, SSL_METHOD_NO_SUITEB, SSL_OP_NO_TLSv1,
164                         TLSv1_method,
165                         ossl_statem_accept,
166                         ossl_statem_connect, tls1_get_method, TLSv1_enc_data)
167 #endif
168
169 #ifndef OPENSSL_NO_SSL3_METHOD
170 IMPLEMENT_ssl3_meth_func(SSLv3_method, ossl_statem_accept, ossl_statem_connect,
171                          tls1_get_method)
172 #endif
173
174
175 /*
176  * TLS/SSLv3 server methods
177  */
178
179 static const SSL_METHOD *tls1_get_server_method(int ver)
180 {
181     if (ver == TLS_ANY_VERSION)
182         return TLS_server_method();
183 #ifndef OPENSSL_NO_TLS1_2
184     if (ver == TLS1_2_VERSION)
185         return TLSv1_2_server_method();
186 #endif
187 #ifndef OPENSSL_NO_TLS1_1
188     if (ver == TLS1_1_VERSION)
189         return TLSv1_1_server_method();
190 #endif
191 #ifndef OPENSSL_NO_TLS1
192     if (ver == TLS1_VERSION)
193         return TLSv1_server_method();
194 #endif
195 #ifndef OPENSSL_NO_SSL3
196     if (ver == SSL3_VERSION)
197         return (SSLv3_server_method());
198 #endif
199     return NULL;
200 }
201
202 IMPLEMENT_tls_meth_func(TLS_ANY_VERSION, 0, 0,
203                         TLS_server_method,
204                         ossl_statem_accept,
205                         ssl_undefined_function,
206                         tls1_get_server_method, TLSv1_2_enc_data)
207
208 #ifndef OPENSSL_NO_TLS1_2_METHOD
209 IMPLEMENT_tls_meth_func(TLS1_2_VERSION, 0, SSL_OP_NO_TLSv1_2,
210                         TLSv1_2_server_method,
211                         ossl_statem_accept,
212                         ssl_undefined_function,
213                         tls1_get_server_method, TLSv1_2_enc_data)
214 #endif
215
216 #ifndef OPENSSL_NO_TLS1_1_METHOD
217 IMPLEMENT_tls_meth_func(TLS1_1_VERSION, SSL_METHOD_NO_SUITEB, SSL_OP_NO_TLSv1_1,
218                         TLSv1_1_server_method,
219                         ossl_statem_accept,
220                         ssl_undefined_function,
221                         tls1_get_server_method, TLSv1_1_enc_data)
222 #endif
223
224 #ifndef OPENSSL_NO_TLS1_METHOD
225 IMPLEMENT_tls_meth_func(TLS1_VERSION, SSL_METHOD_NO_SUITEB, SSL_OP_NO_TLSv1,
226                         TLSv1_server_method,
227                         ossl_statem_accept,
228                         ssl_undefined_function,
229                         tls1_get_server_method, TLSv1_enc_data)
230 #endif
231
232 #ifndef OPENSSL_NO_SSL3_METHOD
233 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
234                          ossl_statem_accept,
235                          ssl_undefined_function, tls1_get_server_method)
236 #endif
237
238
239 /*
240  * TLS/SSLv3 client methods
241  */
242
243 static const SSL_METHOD *tls1_get_client_method(int ver)
244 {
245     if (ver == TLS_ANY_VERSION)
246         return TLS_client_method();
247 #ifndef OPENSSL_NO_TLS1_2
248     if (ver == TLS1_2_VERSION)
249         return TLSv1_2_client_method();
250 #endif
251 #ifndef OPENSSL_NO_TLS1_1
252     if (ver == TLS1_1_VERSION)
253         return TLSv1_1_client_method();
254 #endif
255 #ifndef OPENSSL_NO_TLS1
256     if (ver == TLS1_VERSION)
257         return TLSv1_client_method();
258 #endif
259 #ifndef OPENSSL_NO_SSL3
260     if (ver == SSL3_VERSION)
261         return (SSLv3_client_method());
262 #endif
263     return NULL;
264 }
265
266 IMPLEMENT_tls_meth_func(TLS_ANY_VERSION, 0, 0,
267                         TLS_client_method,
268                         ssl_undefined_function,
269                         ossl_statem_connect,
270                         tls1_get_client_method, TLSv1_2_enc_data)
271
272 #ifndef OPENSSL_NO_TLS1_2_METHOD
273 IMPLEMENT_tls_meth_func(TLS1_2_VERSION, 0, SSL_OP_NO_TLSv1_2,
274                         TLSv1_2_client_method,
275                         ssl_undefined_function,
276                         ossl_statem_connect,
277                         tls1_get_client_method, TLSv1_2_enc_data)
278 #endif
279
280 #ifndef OPENSSL_NO_TLS1_1_METHOD
281 IMPLEMENT_tls_meth_func(TLS1_1_VERSION, SSL_METHOD_NO_SUITEB, SSL_OP_NO_TLSv1_1,
282                         TLSv1_1_client_method,
283                         ssl_undefined_function,
284                         ossl_statem_connect,
285                         tls1_get_client_method, TLSv1_1_enc_data)
286 #endif
287
288 #ifndef OPENSSL_NO_TLS1_METHOD
289 IMPLEMENT_tls_meth_func(TLS1_VERSION, SSL_METHOD_NO_SUITEB, SSL_OP_NO_TLSv1,
290                         TLSv1_client_method,
291                         ssl_undefined_function,
292                         ossl_statem_connect,
293                         tls1_get_client_method, TLSv1_enc_data)
294 #endif
295
296 #ifndef OPENSSL_NO_SSL3_METHOD
297 IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
298                          ssl_undefined_function,
299                          ossl_statem_connect, tls1_get_client_method)
300 #endif
301
302
303 /*
304  * DTLS methods
305  */
306 static const SSL_METHOD *dtls1_get_method(int ver)
307 {
308     if (ver == DTLS_ANY_VERSION)
309         return DTLS_method();
310 #ifndef OPENSSL_NO_DTLS1
311     else if (ver == DTLS1_VERSION)
312         return DTLSv1_method();
313 #endif
314 #ifndef OPENSSL_NO_DTLS1_2
315     else if (ver == DTLS1_2_VERSION)
316         return DTLSv1_2_method();
317 #endif
318     else
319         return NULL;
320 }
321
322 #ifndef OPENSSL_NO_DTLS1_METHOD
323 IMPLEMENT_dtls1_meth_func(DTLS1_VERSION, SSL_METHOD_NO_SUITEB, SSL_OP_NO_DTLSv1,
324                           DTLSv1_method,
325                           ossl_statem_accept,
326                           ossl_statem_connect,
327                           dtls1_get_method, DTLSv1_enc_data)
328 #endif
329
330 #ifndef OPENSSL_NO_DTLS1_2_METHOD
331 IMPLEMENT_dtls1_meth_func(DTLS1_2_VERSION, 0, SSL_OP_NO_DTLSv1_2,
332                           DTLSv1_2_method,
333                           ossl_statem_accept,
334                           ossl_statem_connect,
335                           dtls1_get_method, DTLSv1_2_enc_data)
336 #endif
337
338 IMPLEMENT_dtls1_meth_func(DTLS_ANY_VERSION, 0, 0,
339                           DTLS_method,
340                           ossl_statem_accept,
341                           ossl_statem_connect,
342                           dtls1_get_method, DTLSv1_2_enc_data)
343
344 /*
345  * DTLS server methods
346  */
347
348 static const SSL_METHOD *dtls1_get_server_method(int ver)
349 {
350     if (ver == DTLS_ANY_VERSION)
351         return DTLS_server_method();
352 #ifndef OPENSSL_NO_DTLS1
353     else if (ver == DTLS1_VERSION)
354         return DTLSv1_server_method();
355 #endif
356 #ifndef OPENSSL_NO_DTLS1_2
357     else if (ver == DTLS1_2_VERSION)
358         return DTLSv1_2_server_method();
359 #endif
360     else
361         return NULL;
362 }
363
364 #ifndef OPENSSL_NO_DTLS1_METHOD
365 IMPLEMENT_dtls1_meth_func(DTLS1_VERSION, SSL_METHOD_NO_SUITEB, SSL_OP_NO_DTLSv1,
366                           DTLSv1_server_method,
367                           ossl_statem_accept,
368                           ssl_undefined_function,
369                           dtls1_get_server_method, DTLSv1_enc_data)
370 #endif
371
372 #ifndef OPENSSL_NO_DTLS1_2_METHOD
373 IMPLEMENT_dtls1_meth_func(DTLS1_2_VERSION, 0, SSL_OP_NO_DTLSv1_2,
374                           DTLSv1_2_server_method,
375                           ossl_statem_accept,
376                           ssl_undefined_function,
377                           dtls1_get_server_method, DTLSv1_2_enc_data)
378 #endif
379
380 IMPLEMENT_dtls1_meth_func(DTLS_ANY_VERSION, 0, 0,
381                           DTLS_server_method,
382                           ossl_statem_accept,
383                           ssl_undefined_function,
384                           dtls1_get_server_method, DTLSv1_2_enc_data)
385
386
387 /*
388  * DTLS client methods
389  */
390
391 static const SSL_METHOD *dtls1_get_client_method(int ver)
392 {
393     if (ver == DTLS_ANY_VERSION)
394         return DTLS_client_method();
395 #ifndef OPENSSL_NO_DTLS1
396     else if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER)
397         return DTLSv1_client_method();
398 #endif
399 #ifndef OPENSSL_NO_DTLS1_2
400     else if (ver == DTLS1_2_VERSION)
401         return DTLSv1_2_client_method();
402 #endif
403     else
404         return NULL;
405 }
406
407 #ifndef OPENSSL_NO_DTLS1_METHOD
408 IMPLEMENT_dtls1_meth_func(DTLS1_VERSION, SSL_METHOD_NO_SUITEB, SSL_OP_NO_DTLSv1,
409                           DTLSv1_client_method,
410                           ssl_undefined_function,
411                           ossl_statem_connect,
412                           dtls1_get_client_method, DTLSv1_enc_data)
413 #endif
414
415 #ifndef OPENSSL_NO_DTLS1_2_METHOD
416 IMPLEMENT_dtls1_meth_func(DTLS1_2_VERSION, 0, SSL_OP_NO_DTLSv1_2,
417                           DTLSv1_2_client_method,
418                           ssl_undefined_function,
419                           ossl_statem_connect,
420                           dtls1_get_client_method, DTLSv1_2_enc_data)
421 #endif
422
423 IMPLEMENT_dtls1_meth_func(DTLS_ANY_VERSION, 0, 0,
424                           DTLS_client_method,
425                           ssl_undefined_function,
426                           ossl_statem_connect,
427                           dtls1_get_client_method, DTLSv1_2_enc_data)