1 /* $OpenBSD: ssl_methods.c,v 1.32 2024/07/23 14:40:54 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 "dtls_local.h"
60 #include "ssl_local.h"
61 #include "tls13_internal.h"
62
63 static const SSL_METHOD DTLS_method_data = {
64 .dtls = 1,
65 .server = 1,
66 .version = DTLS1_2_VERSION,
67 .min_tls_version = TLS1_1_VERSION,
68 .max_tls_version = TLS1_2_VERSION,
69 .ssl_new = dtls1_new,
70 .ssl_clear = dtls1_clear,
71 .ssl_free = dtls1_free,
72 .ssl_accept = ssl3_accept,
73 .ssl_connect = ssl3_connect,
74 .ssl_shutdown = ssl3_shutdown,
75 .ssl_renegotiate = ssl3_renegotiate,
76 .ssl_renegotiate_check = ssl3_renegotiate_check,
77 .ssl_pending = ssl3_pending,
78 .ssl_read_bytes = dtls1_read_bytes,
79 .ssl_write_bytes = dtls1_write_app_data_bytes,
80 .enc_flags = TLSV1_2_ENC_FLAGS,
81 };
82
83 static const SSL_METHOD DTLS_client_method_data = {
84 .dtls = 1,
85 .server = 0,
86 .version = DTLS1_2_VERSION,
87 .min_tls_version = TLS1_1_VERSION,
88 .max_tls_version = TLS1_2_VERSION,
89 .ssl_new = dtls1_new,
90 .ssl_clear = dtls1_clear,
91 .ssl_free = dtls1_free,
92 .ssl_accept = ssl_undefined_function,
93 .ssl_connect = ssl3_connect,
94 .ssl_shutdown = ssl3_shutdown,
95 .ssl_renegotiate = ssl3_renegotiate,
96 .ssl_renegotiate_check = ssl3_renegotiate_check,
97 .ssl_pending = ssl3_pending,
98 .ssl_read_bytes = dtls1_read_bytes,
99 .ssl_write_bytes = dtls1_write_app_data_bytes,
100 .enc_flags = TLSV1_2_ENC_FLAGS,
101 };
102
103 static const SSL_METHOD DTLSv1_method_data = {
104 .dtls = 1,
105 .server = 1,
106 .version = DTLS1_VERSION,
107 .min_tls_version = TLS1_1_VERSION,
108 .max_tls_version = TLS1_1_VERSION,
109 .ssl_new = dtls1_new,
110 .ssl_clear = dtls1_clear,
111 .ssl_free = dtls1_free,
112 .ssl_accept = ssl3_accept,
113 .ssl_connect = ssl3_connect,
114 .ssl_shutdown = ssl3_shutdown,
115 .ssl_renegotiate = ssl3_renegotiate,
116 .ssl_renegotiate_check = ssl3_renegotiate_check,
117 .ssl_pending = ssl3_pending,
118 .ssl_read_bytes = dtls1_read_bytes,
119 .ssl_write_bytes = dtls1_write_app_data_bytes,
120 .enc_flags = TLSV1_1_ENC_FLAGS,
121 };
122
123 static const SSL_METHOD DTLSv1_client_method_data = {
124 .dtls = 1,
125 .server = 0,
126 .version = DTLS1_VERSION,
127 .min_tls_version = TLS1_1_VERSION,
128 .max_tls_version = TLS1_1_VERSION,
129 .ssl_new = dtls1_new,
130 .ssl_clear = dtls1_clear,
131 .ssl_free = dtls1_free,
132 .ssl_accept = ssl_undefined_function,
133 .ssl_connect = ssl3_connect,
134 .ssl_shutdown = ssl3_shutdown,
135 .ssl_renegotiate = ssl3_renegotiate,
136 .ssl_renegotiate_check = ssl3_renegotiate_check,
137 .ssl_pending = ssl3_pending,
138 .ssl_read_bytes = dtls1_read_bytes,
139 .ssl_write_bytes = dtls1_write_app_data_bytes,
140 .enc_flags = TLSV1_1_ENC_FLAGS,
141 };
142
143 static const SSL_METHOD DTLSv1_2_method_data = {
144 .dtls = 1,
145 .server = 1,
146 .version = DTLS1_2_VERSION,
147 .min_tls_version = TLS1_2_VERSION,
148 .max_tls_version = TLS1_2_VERSION,
149 .ssl_new = dtls1_new,
150 .ssl_clear = dtls1_clear,
151 .ssl_free = dtls1_free,
152 .ssl_accept = ssl3_accept,
153 .ssl_connect = ssl3_connect,
154 .ssl_shutdown = ssl3_shutdown,
155 .ssl_renegotiate = ssl3_renegotiate,
156 .ssl_renegotiate_check = ssl3_renegotiate_check,
157 .ssl_pending = ssl3_pending,
158 .ssl_read_bytes = dtls1_read_bytes,
159 .ssl_write_bytes = dtls1_write_app_data_bytes,
160 .enc_flags = TLSV1_2_ENC_FLAGS,
161 };
162
163 static const SSL_METHOD DTLSv1_2_client_method_data = {
164 .dtls = 1,
165 .server = 0,
166 .version = DTLS1_2_VERSION,
167 .min_tls_version = TLS1_2_VERSION,
168 .max_tls_version = TLS1_2_VERSION,
169 .ssl_new = dtls1_new,
170 .ssl_clear = dtls1_clear,
171 .ssl_free = dtls1_free,
172 .ssl_accept = ssl_undefined_function,
173 .ssl_connect = ssl3_connect,
174 .ssl_shutdown = ssl3_shutdown,
175 .ssl_renegotiate = ssl3_renegotiate,
176 .ssl_renegotiate_check = ssl3_renegotiate_check,
177 .ssl_pending = ssl3_pending,
178 .ssl_read_bytes = dtls1_read_bytes,
179 .ssl_write_bytes = dtls1_write_app_data_bytes,
180 .enc_flags = TLSV1_2_ENC_FLAGS,
181 };
182
183 const SSL_METHOD *
DTLSv1_client_method(void)184 DTLSv1_client_method(void)
185 {
186 return &DTLSv1_client_method_data;
187 }
188 LSSL_ALIAS(DTLSv1_client_method);
189
190 const SSL_METHOD *
DTLSv1_method(void)191 DTLSv1_method(void)
192 {
193 return &DTLSv1_method_data;
194 }
195 LSSL_ALIAS(DTLSv1_method);
196
197 const SSL_METHOD *
DTLSv1_server_method(void)198 DTLSv1_server_method(void)
199 {
200 return &DTLSv1_method_data;
201 }
202 LSSL_ALIAS(DTLSv1_server_method);
203
204 const SSL_METHOD *
DTLSv1_2_client_method(void)205 DTLSv1_2_client_method(void)
206 {
207 return &DTLSv1_2_client_method_data;
208 }
209 LSSL_ALIAS(DTLSv1_2_client_method);
210
211 const SSL_METHOD *
DTLSv1_2_method(void)212 DTLSv1_2_method(void)
213 {
214 return &DTLSv1_2_method_data;
215 }
216 LSSL_ALIAS(DTLSv1_2_method);
217
218 const SSL_METHOD *
DTLSv1_2_server_method(void)219 DTLSv1_2_server_method(void)
220 {
221 return &DTLSv1_2_method_data;
222 }
223 LSSL_ALIAS(DTLSv1_2_server_method);
224
225 const SSL_METHOD *
DTLS_client_method(void)226 DTLS_client_method(void)
227 {
228 return &DTLS_client_method_data;
229 }
230 LSSL_ALIAS(DTLS_client_method);
231
232 const SSL_METHOD *
DTLS_method(void)233 DTLS_method(void)
234 {
235 return &DTLS_method_data;
236 }
237 LSSL_ALIAS(DTLS_method);
238
239 const SSL_METHOD *
DTLS_server_method(void)240 DTLS_server_method(void)
241 {
242 return &DTLS_method_data;
243 }
244 LSSL_ALIAS(DTLS_server_method);
245
246 static const SSL_METHOD TLS_method_data = {
247 .dtls = 0,
248 .server = 1,
249 .version = TLS1_3_VERSION,
250 .min_tls_version = TLS1_VERSION,
251 .max_tls_version = TLS1_3_VERSION,
252 .ssl_new = tls1_new,
253 .ssl_clear = tls1_clear,
254 .ssl_free = tls1_free,
255 .ssl_accept = tls13_legacy_accept,
256 .ssl_connect = tls13_legacy_connect,
257 .ssl_shutdown = tls13_legacy_shutdown,
258 .ssl_renegotiate = ssl_undefined_function,
259 .ssl_renegotiate_check = ssl_ok,
260 .ssl_pending = tls13_legacy_pending,
261 .ssl_read_bytes = tls13_legacy_read_bytes,
262 .ssl_write_bytes = tls13_legacy_write_bytes,
263 .enc_flags = TLSV1_3_ENC_FLAGS,
264 };
265
266 static const SSL_METHOD TLS_legacy_method_data = {
267 .dtls = 0,
268 .server = 1,
269 .version = TLS1_2_VERSION,
270 .min_tls_version = TLS1_VERSION,
271 .max_tls_version = TLS1_2_VERSION,
272 .ssl_new = tls1_new,
273 .ssl_clear = tls1_clear,
274 .ssl_free = tls1_free,
275 .ssl_accept = ssl3_accept,
276 .ssl_connect = ssl3_connect,
277 .ssl_shutdown = ssl3_shutdown,
278 .ssl_renegotiate = ssl_undefined_function,
279 .ssl_renegotiate_check = ssl_ok,
280 .ssl_pending = ssl3_pending,
281 .ssl_read_bytes = ssl3_read_bytes,
282 .ssl_write_bytes = ssl3_write_bytes,
283 .enc_flags = TLSV1_2_ENC_FLAGS,
284 };
285
286 static const SSL_METHOD TLS_client_method_data = {
287 .dtls = 0,
288 .server = 0,
289 .version = TLS1_3_VERSION,
290 .min_tls_version = TLS1_VERSION,
291 .max_tls_version = TLS1_3_VERSION,
292 .ssl_new = tls1_new,
293 .ssl_clear = tls1_clear,
294 .ssl_free = tls1_free,
295 .ssl_accept = tls13_legacy_accept,
296 .ssl_connect = tls13_legacy_connect,
297 .ssl_shutdown = tls13_legacy_shutdown,
298 .ssl_renegotiate = ssl_undefined_function,
299 .ssl_renegotiate_check = ssl_ok,
300 .ssl_pending = tls13_legacy_pending,
301 .ssl_read_bytes = tls13_legacy_read_bytes,
302 .ssl_write_bytes = tls13_legacy_write_bytes,
303 .enc_flags = TLSV1_3_ENC_FLAGS,
304 };
305
306 static const SSL_METHOD TLSv1_method_data = {
307 .dtls = 0,
308 .server = 1,
309 .version = TLS1_VERSION,
310 .min_tls_version = TLS1_VERSION,
311 .max_tls_version = TLS1_VERSION,
312 .ssl_new = tls1_new,
313 .ssl_clear = tls1_clear,
314 .ssl_free = tls1_free,
315 .ssl_accept = ssl3_accept,
316 .ssl_connect = ssl3_connect,
317 .ssl_shutdown = ssl3_shutdown,
318 .ssl_renegotiate = ssl3_renegotiate,
319 .ssl_renegotiate_check = ssl3_renegotiate_check,
320 .ssl_pending = ssl3_pending,
321 .ssl_read_bytes = ssl3_read_bytes,
322 .ssl_write_bytes = ssl3_write_bytes,
323 .enc_flags = TLSV1_ENC_FLAGS,
324 };
325
326 static const SSL_METHOD TLSv1_client_method_data = {
327 .dtls = 0,
328 .server = 0,
329 .version = TLS1_VERSION,
330 .min_tls_version = TLS1_VERSION,
331 .max_tls_version = TLS1_VERSION,
332 .ssl_new = tls1_new,
333 .ssl_clear = tls1_clear,
334 .ssl_free = tls1_free,
335 .ssl_accept = ssl_undefined_function,
336 .ssl_connect = ssl3_connect,
337 .ssl_shutdown = ssl3_shutdown,
338 .ssl_renegotiate = ssl3_renegotiate,
339 .ssl_renegotiate_check = ssl3_renegotiate_check,
340 .ssl_pending = ssl3_pending,
341 .ssl_read_bytes = ssl3_read_bytes,
342 .ssl_write_bytes = ssl3_write_bytes,
343 .enc_flags = TLSV1_ENC_FLAGS,
344 };
345
346 static const SSL_METHOD TLSv1_1_method_data = {
347 .dtls = 0,
348 .server = 1,
349 .version = TLS1_1_VERSION,
350 .min_tls_version = TLS1_1_VERSION,
351 .max_tls_version = TLS1_1_VERSION,
352 .ssl_new = tls1_new,
353 .ssl_clear = tls1_clear,
354 .ssl_free = tls1_free,
355 .ssl_accept = ssl3_accept,
356 .ssl_connect = ssl3_connect,
357 .ssl_shutdown = ssl3_shutdown,
358 .ssl_renegotiate = ssl3_renegotiate,
359 .ssl_renegotiate_check = ssl3_renegotiate_check,
360 .ssl_pending = ssl3_pending,
361 .ssl_read_bytes = ssl3_read_bytes,
362 .ssl_write_bytes = ssl3_write_bytes,
363 .enc_flags = TLSV1_1_ENC_FLAGS,
364 };
365
366 static const SSL_METHOD TLSv1_1_client_method_data = {
367 .dtls = 0,
368 .server = 0,
369 .version = TLS1_1_VERSION,
370 .min_tls_version = TLS1_1_VERSION,
371 .max_tls_version = TLS1_1_VERSION,
372 .ssl_new = tls1_new,
373 .ssl_clear = tls1_clear,
374 .ssl_free = tls1_free,
375 .ssl_accept = ssl_undefined_function,
376 .ssl_connect = ssl3_connect,
377 .ssl_shutdown = ssl3_shutdown,
378 .ssl_renegotiate = ssl3_renegotiate,
379 .ssl_renegotiate_check = ssl3_renegotiate_check,
380 .ssl_pending = ssl3_pending,
381 .ssl_read_bytes = ssl3_read_bytes,
382 .ssl_write_bytes = ssl3_write_bytes,
383 .enc_flags = TLSV1_1_ENC_FLAGS,
384 };
385
386 static const SSL_METHOD TLSv1_2_method_data = {
387 .dtls = 0,
388 .server = 1,
389 .version = TLS1_2_VERSION,
390 .min_tls_version = TLS1_2_VERSION,
391 .max_tls_version = TLS1_2_VERSION,
392 .ssl_new = tls1_new,
393 .ssl_clear = tls1_clear,
394 .ssl_free = tls1_free,
395 .ssl_accept = ssl3_accept,
396 .ssl_connect = ssl3_connect,
397 .ssl_shutdown = ssl3_shutdown,
398 .ssl_renegotiate = ssl3_renegotiate,
399 .ssl_renegotiate_check = ssl3_renegotiate_check,
400 .ssl_pending = ssl3_pending,
401 .ssl_read_bytes = ssl3_read_bytes,
402 .ssl_write_bytes = ssl3_write_bytes,
403 .enc_flags = TLSV1_2_ENC_FLAGS,
404 };
405
406 static const SSL_METHOD TLSv1_2_client_method_data = {
407 .dtls = 0,
408 .server = 0,
409 .version = TLS1_2_VERSION,
410 .min_tls_version = TLS1_2_VERSION,
411 .max_tls_version = TLS1_2_VERSION,
412 .ssl_new = tls1_new,
413 .ssl_clear = tls1_clear,
414 .ssl_free = tls1_free,
415 .ssl_accept = ssl_undefined_function,
416 .ssl_connect = ssl3_connect,
417 .ssl_shutdown = ssl3_shutdown,
418 .ssl_renegotiate = ssl3_renegotiate,
419 .ssl_renegotiate_check = ssl3_renegotiate_check,
420 .ssl_pending = ssl3_pending,
421 .ssl_read_bytes = ssl3_read_bytes,
422 .ssl_write_bytes = ssl3_write_bytes,
423 .enc_flags = TLSV1_2_ENC_FLAGS,
424 };
425
426 const SSL_METHOD *
TLS_client_method(void)427 TLS_client_method(void)
428 {
429 return (&TLS_client_method_data);
430 }
431 LSSL_ALIAS(TLS_client_method);
432
433 const SSL_METHOD *
TLS_method(void)434 TLS_method(void)
435 {
436 return (&TLS_method_data);
437 }
438 LSSL_ALIAS(TLS_method);
439
440 const SSL_METHOD *
TLS_server_method(void)441 TLS_server_method(void)
442 {
443 return TLS_method();
444 }
445 LSSL_ALIAS(TLS_server_method);
446
447 const SSL_METHOD *
tls_legacy_method(void)448 tls_legacy_method(void)
449 {
450 return (&TLS_legacy_method_data);
451 }
452
453 const SSL_METHOD *
SSLv23_client_method(void)454 SSLv23_client_method(void)
455 {
456 return TLS_client_method();
457 }
458 LSSL_ALIAS(SSLv23_client_method);
459
460 const SSL_METHOD *
SSLv23_method(void)461 SSLv23_method(void)
462 {
463 return TLS_method();
464 }
465 LSSL_ALIAS(SSLv23_method);
466
467 const SSL_METHOD *
SSLv23_server_method(void)468 SSLv23_server_method(void)
469 {
470 return TLS_method();
471 }
472 LSSL_ALIAS(SSLv23_server_method);
473
474 const SSL_METHOD *
TLSv1_client_method(void)475 TLSv1_client_method(void)
476 {
477 return (&TLSv1_client_method_data);
478 }
479 LSSL_ALIAS(TLSv1_client_method);
480
481 const SSL_METHOD *
TLSv1_method(void)482 TLSv1_method(void)
483 {
484 return (&TLSv1_method_data);
485 }
486 LSSL_ALIAS(TLSv1_method);
487
488 const SSL_METHOD *
TLSv1_server_method(void)489 TLSv1_server_method(void)
490 {
491 return (&TLSv1_method_data);
492 }
493 LSSL_ALIAS(TLSv1_server_method);
494
495 const SSL_METHOD *
TLSv1_1_client_method(void)496 TLSv1_1_client_method(void)
497 {
498 return (&TLSv1_1_client_method_data);
499 }
500 LSSL_ALIAS(TLSv1_1_client_method);
501
502 const SSL_METHOD *
TLSv1_1_method(void)503 TLSv1_1_method(void)
504 {
505 return (&TLSv1_1_method_data);
506 }
507 LSSL_ALIAS(TLSv1_1_method);
508
509 const SSL_METHOD *
TLSv1_1_server_method(void)510 TLSv1_1_server_method(void)
511 {
512 return (&TLSv1_1_method_data);
513 }
514 LSSL_ALIAS(TLSv1_1_server_method);
515
516 const SSL_METHOD *
TLSv1_2_client_method(void)517 TLSv1_2_client_method(void)
518 {
519 return (&TLSv1_2_client_method_data);
520 }
521 LSSL_ALIAS(TLSv1_2_client_method);
522
523 const SSL_METHOD *
TLSv1_2_method(void)524 TLSv1_2_method(void)
525 {
526 return (&TLSv1_2_method_data);
527 }
528 LSSL_ALIAS(TLSv1_2_method);
529
530 const SSL_METHOD *
TLSv1_2_server_method(void)531 TLSv1_2_server_method(void)
532 {
533 return (&TLSv1_2_method_data);
534 }
535 LSSL_ALIAS(TLSv1_2_server_method);
536
537 const SSL_METHOD *
ssl_get_method(uint16_t version)538 ssl_get_method(uint16_t version)
539 {
540 if (version == TLS1_3_VERSION)
541 return (TLS_method());
542 if (version == TLS1_2_VERSION)
543 return (TLSv1_2_method());
544 if (version == TLS1_1_VERSION)
545 return (TLSv1_1_method());
546 if (version == TLS1_VERSION)
547 return (TLSv1_method());
548 if (version == DTLS1_VERSION)
549 return (DTLSv1_method());
550 if (version == DTLS1_2_VERSION)
551 return (DTLSv1_2_method());
552
553 return (NULL);
554 }
555