xref: /openbsd-src/lib/libssl/ssl_methods.c (revision 9fef1c44a60220d617ce7edb2d07c208216a8adb)
1 /* $OpenBSD: ssl_methods.c,v 1.18 2020/10/11 02:22:27 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include "ssl_locl.h"
60 #include "tls13_internal.h"
61 
62 static const SSL_METHOD_INTERNAL DTLSv1_method_internal_data = {
63 	.version = DTLS1_VERSION,
64 	.min_version = DTLS1_VERSION,
65 	.max_version = DTLS1_VERSION,
66 	.ssl_new = dtls1_new,
67 	.ssl_clear = dtls1_clear,
68 	.ssl_free = dtls1_free,
69 	.ssl_accept = ssl3_accept,
70 	.ssl_connect = ssl3_connect,
71 	.ssl_shutdown = ssl3_shutdown,
72 	.ssl_renegotiate = ssl3_renegotiate,
73 	.ssl_renegotiate_check = ssl3_renegotiate_check,
74 	.ssl_pending = ssl3_pending,
75 	.ssl_read_bytes = dtls1_read_bytes,
76 	.ssl_write_bytes = dtls1_write_app_data_bytes,
77 	.ssl3_enc = &TLSv1_1_enc_data,
78 };
79 
80 static const SSL_METHOD DTLSv1_method_data = {
81 	.ssl_dispatch_alert = dtls1_dispatch_alert,
82 	.num_ciphers = ssl3_num_ciphers,
83 	.get_cipher = dtls1_get_cipher,
84 	.get_cipher_by_char = ssl3_get_cipher_by_char,
85 	.put_cipher_by_char = ssl3_put_cipher_by_char,
86 	.internal = &DTLSv1_method_internal_data,
87 };
88 
89 const SSL_METHOD *
90 DTLSv1_client_method(void)
91 {
92 	return &DTLSv1_method_data;
93 }
94 
95 const SSL_METHOD *
96 DTLSv1_method(void)
97 {
98 	return &DTLSv1_method_data;
99 }
100 
101 const SSL_METHOD *
102 DTLSv1_server_method(void)
103 {
104 	return &DTLSv1_method_data;
105 }
106 
107 const SSL_METHOD *
108 DTLS_client_method(void)
109 {
110 	return DTLSv1_method();
111 }
112 
113 const SSL_METHOD *
114 DTLS_method(void)
115 {
116 	return DTLSv1_method();
117 }
118 
119 const SSL_METHOD *
120 DTLS_server_method(void)
121 {
122 	return DTLSv1_method();
123 }
124 
125 #if defined(LIBRESSL_HAS_TLS1_3_CLIENT) && defined(LIBRESSL_HAS_TLS1_3_SERVER)
126 static const SSL_METHOD_INTERNAL TLS_method_internal_data = {
127 	.version = TLS1_3_VERSION,
128 	.min_version = TLS1_VERSION,
129 	.max_version = TLS1_3_VERSION,
130 	.ssl_new = tls1_new,
131 	.ssl_clear = tls1_clear,
132 	.ssl_free = tls1_free,
133 	.ssl_accept = tls13_legacy_accept,
134 	.ssl_connect = tls13_legacy_connect,
135 	.ssl_shutdown = tls13_legacy_shutdown,
136 	.ssl_renegotiate = ssl_undefined_function,
137 	.ssl_renegotiate_check = ssl_ok,
138 	.ssl_pending = tls13_legacy_pending,
139 	.ssl_read_bytes = tls13_legacy_read_bytes,
140 	.ssl_write_bytes = tls13_legacy_write_bytes,
141 	.ssl3_enc = &TLSv1_3_enc_data,
142 };
143 
144 static const SSL_METHOD TLS_method_data = {
145 	.ssl_dispatch_alert = ssl3_dispatch_alert,
146 	.num_ciphers = ssl3_num_ciphers,
147 	.get_cipher = ssl3_get_cipher,
148 	.get_cipher_by_char = ssl3_get_cipher_by_char,
149 	.put_cipher_by_char = ssl3_put_cipher_by_char,
150 	.internal = &TLS_method_internal_data,
151 };
152 #endif
153 
154 static const SSL_METHOD_INTERNAL TLS_legacy_method_internal_data = {
155 	.version = TLS1_2_VERSION,
156 	.min_version = TLS1_VERSION,
157 	.max_version = TLS1_2_VERSION,
158 	.ssl_new = tls1_new,
159 	.ssl_clear = tls1_clear,
160 	.ssl_free = tls1_free,
161 	.ssl_accept = ssl3_accept,
162 	.ssl_connect = ssl3_connect,
163 	.ssl_shutdown = ssl3_shutdown,
164 	.ssl_renegotiate = ssl_undefined_function,
165 	.ssl_renegotiate_check = ssl_ok,
166 	.ssl_pending = ssl3_pending,
167 	.ssl_read_bytes = ssl3_read_bytes,
168 	.ssl_write_bytes = ssl3_write_bytes,
169 	.ssl3_enc = &TLSv1_2_enc_data,
170 };
171 
172 static const SSL_METHOD TLS_legacy_method_data = {
173 	.ssl_dispatch_alert = ssl3_dispatch_alert,
174 	.num_ciphers = ssl3_num_ciphers,
175 	.get_cipher = ssl3_get_cipher,
176 	.get_cipher_by_char = ssl3_get_cipher_by_char,
177 	.put_cipher_by_char = ssl3_put_cipher_by_char,
178 	.internal = &TLS_legacy_method_internal_data,
179 };
180 
181 static const SSL_METHOD_INTERNAL TLSv1_method_internal_data = {
182 	.version = TLS1_VERSION,
183 	.min_version = TLS1_VERSION,
184 	.max_version = TLS1_VERSION,
185 	.ssl_new = tls1_new,
186 	.ssl_clear = tls1_clear,
187 	.ssl_free = tls1_free,
188 	.ssl_accept = ssl3_accept,
189 	.ssl_connect = ssl3_connect,
190 	.ssl_shutdown = ssl3_shutdown,
191 	.ssl_renegotiate = ssl3_renegotiate,
192 	.ssl_renegotiate_check = ssl3_renegotiate_check,
193 	.ssl_pending = ssl3_pending,
194 	.ssl_read_bytes = ssl3_read_bytes,
195 	.ssl_write_bytes = ssl3_write_bytes,
196 	.ssl3_enc = &TLSv1_enc_data,
197 };
198 
199 static const SSL_METHOD TLSv1_method_data = {
200 	.ssl_dispatch_alert = ssl3_dispatch_alert,
201 	.num_ciphers = ssl3_num_ciphers,
202 	.get_cipher = ssl3_get_cipher,
203 	.get_cipher_by_char = ssl3_get_cipher_by_char,
204 	.put_cipher_by_char = ssl3_put_cipher_by_char,
205 	.internal = &TLSv1_method_internal_data,
206 };
207 
208 static const SSL_METHOD_INTERNAL TLSv1_1_method_internal_data = {
209 	.version = TLS1_1_VERSION,
210 	.min_version = TLS1_1_VERSION,
211 	.max_version = TLS1_1_VERSION,
212 	.ssl_new = tls1_new,
213 	.ssl_clear = tls1_clear,
214 	.ssl_free = tls1_free,
215 	.ssl_accept = ssl3_accept,
216 	.ssl_connect = ssl3_connect,
217 	.ssl_shutdown = ssl3_shutdown,
218 	.ssl_renegotiate = ssl3_renegotiate,
219 	.ssl_renegotiate_check = ssl3_renegotiate_check,
220 	.ssl_pending = ssl3_pending,
221 	.ssl_read_bytes = ssl3_read_bytes,
222 	.ssl_write_bytes = ssl3_write_bytes,
223 	.ssl3_enc = &TLSv1_1_enc_data,
224 };
225 
226 static const SSL_METHOD TLSv1_1_method_data = {
227 	.ssl_dispatch_alert = ssl3_dispatch_alert,
228 	.num_ciphers = ssl3_num_ciphers,
229 	.get_cipher = ssl3_get_cipher,
230 	.get_cipher_by_char = ssl3_get_cipher_by_char,
231 	.put_cipher_by_char = ssl3_put_cipher_by_char,
232 	.internal = &TLSv1_1_method_internal_data,
233 };
234 
235 static const SSL_METHOD_INTERNAL TLSv1_2_method_internal_data = {
236 	.version = TLS1_2_VERSION,
237 	.min_version = TLS1_2_VERSION,
238 	.max_version = TLS1_2_VERSION,
239 	.ssl_new = tls1_new,
240 	.ssl_clear = tls1_clear,
241 	.ssl_free = tls1_free,
242 	.ssl_accept = ssl3_accept,
243 	.ssl_connect = ssl3_connect,
244 	.ssl_shutdown = ssl3_shutdown,
245 	.ssl_renegotiate = ssl3_renegotiate,
246 	.ssl_renegotiate_check = ssl3_renegotiate_check,
247 	.ssl_pending = ssl3_pending,
248 	.ssl_read_bytes = ssl3_read_bytes,
249 	.ssl_write_bytes = ssl3_write_bytes,
250 	.ssl3_enc = &TLSv1_2_enc_data,
251 };
252 
253 static const SSL_METHOD TLSv1_2_method_data = {
254 	.ssl_dispatch_alert = ssl3_dispatch_alert,
255 	.num_ciphers = ssl3_num_ciphers,
256 	.get_cipher = ssl3_get_cipher,
257 	.get_cipher_by_char = ssl3_get_cipher_by_char,
258 	.put_cipher_by_char = ssl3_put_cipher_by_char,
259 	.internal = &TLSv1_2_method_internal_data,
260 };
261 
262 const SSL_METHOD *
263 TLS_client_method(void)
264 {
265 	return TLS_method();
266 }
267 
268 const SSL_METHOD *
269 TLS_method(void)
270 {
271 #if defined(LIBRESSL_HAS_TLS1_3_CLIENT) && defined(LIBRESSL_HAS_TLS1_3_SERVER)
272 	return (&TLS_method_data);
273 #else
274 	return tls_legacy_method();
275 #endif
276 }
277 
278 const SSL_METHOD *
279 TLS_server_method(void)
280 {
281 	return TLS_method();
282 }
283 
284 const SSL_METHOD *
285 tls_legacy_method(void)
286 {
287 	return (&TLS_legacy_method_data);
288 }
289 
290 const SSL_METHOD *
291 SSLv23_client_method(void)
292 {
293 	return TLS_method();
294 }
295 
296 const SSL_METHOD *
297 SSLv23_method(void)
298 {
299 	return TLS_method();
300 }
301 
302 const SSL_METHOD *
303 SSLv23_server_method(void)
304 {
305 	return TLS_method();
306 }
307 
308 const SSL_METHOD *
309 TLSv1_client_method(void)
310 {
311 	return (&TLSv1_method_data);
312 }
313 
314 const SSL_METHOD *
315 TLSv1_method(void)
316 {
317 	return (&TLSv1_method_data);
318 }
319 
320 const SSL_METHOD *
321 TLSv1_server_method(void)
322 {
323 	return (&TLSv1_method_data);
324 }
325 
326 const SSL_METHOD *
327 TLSv1_1_client_method(void)
328 {
329 	return (&TLSv1_1_method_data);
330 }
331 
332 const SSL_METHOD *
333 TLSv1_1_method(void)
334 {
335 	return (&TLSv1_1_method_data);
336 }
337 
338 const SSL_METHOD *
339 TLSv1_1_server_method(void)
340 {
341 	return (&TLSv1_1_method_data);
342 }
343 
344 const SSL_METHOD *
345 TLSv1_2_client_method(void)
346 {
347 	return (&TLSv1_2_method_data);
348 }
349 
350 const SSL_METHOD *
351 TLSv1_2_method(void)
352 {
353 	return (&TLSv1_2_method_data);
354 }
355 
356 const SSL_METHOD *
357 TLSv1_2_server_method(void)
358 {
359 	return (&TLSv1_2_method_data);
360 }
361 
362 const SSL_METHOD *
363 ssl_get_method(uint16_t version)
364 {
365 	if (version == TLS1_3_VERSION)
366 		return (TLS_method());
367 	if (version == TLS1_2_VERSION)
368 		return (TLSv1_2_method());
369 	if (version == TLS1_1_VERSION)
370 		return (TLSv1_1_method());
371 	if (version == TLS1_VERSION)
372 		return (TLSv1_method());
373 	if (version == DTLS1_VERSION)
374 		return (DTLSv1_method());
375 
376 	return (NULL);
377 }
378