xref: /openbsd-src/lib/libssl/ssl_methods.c (revision 354e02d3863e354b89a34196bc9a9f80c45196b0)
1*354e02d3Sjsing /* $OpenBSD: ssl_methods.c,v 1.20 2020/10/14 16:44:15 jsing Exp $ */
29158af98Sjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
39158af98Sjsing  * All rights reserved.
49158af98Sjsing  *
59158af98Sjsing  * This package is an SSL implementation written
69158af98Sjsing  * by Eric Young (eay@cryptsoft.com).
79158af98Sjsing  * The implementation was written so as to conform with Netscapes SSL.
89158af98Sjsing  *
99158af98Sjsing  * This library is free for commercial and non-commercial use as long as
109158af98Sjsing  * the following conditions are aheared to.  The following conditions
119158af98Sjsing  * apply to all code found in this distribution, be it the RC4, RSA,
129158af98Sjsing  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
139158af98Sjsing  * included with this distribution is covered by the same copyright terms
149158af98Sjsing  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
159158af98Sjsing  *
169158af98Sjsing  * Copyright remains Eric Young's, and as such any Copyright notices in
179158af98Sjsing  * the code are not to be removed.
189158af98Sjsing  * If this package is used in a product, Eric Young should be given attribution
199158af98Sjsing  * as the author of the parts of the library used.
209158af98Sjsing  * This can be in the form of a textual message at program startup or
219158af98Sjsing  * in documentation (online or textual) provided with the package.
229158af98Sjsing  *
239158af98Sjsing  * Redistribution and use in source and binary forms, with or without
249158af98Sjsing  * modification, are permitted provided that the following conditions
259158af98Sjsing  * are met:
269158af98Sjsing  * 1. Redistributions of source code must retain the copyright
279158af98Sjsing  *    notice, this list of conditions and the following disclaimer.
289158af98Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
299158af98Sjsing  *    notice, this list of conditions and the following disclaimer in the
309158af98Sjsing  *    documentation and/or other materials provided with the distribution.
319158af98Sjsing  * 3. All advertising materials mentioning features or use of this software
329158af98Sjsing  *    must display the following acknowledgement:
339158af98Sjsing  *    "This product includes cryptographic software written by
349158af98Sjsing  *     Eric Young (eay@cryptsoft.com)"
359158af98Sjsing  *    The word 'cryptographic' can be left out if the rouines from the library
369158af98Sjsing  *    being used are not cryptographic related :-).
379158af98Sjsing  * 4. If you include any Windows specific code (or a derivative thereof) from
389158af98Sjsing  *    the apps directory (application code) you must include an acknowledgement:
399158af98Sjsing  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
409158af98Sjsing  *
419158af98Sjsing  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
429158af98Sjsing  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
439158af98Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
449158af98Sjsing  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
459158af98Sjsing  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
469158af98Sjsing  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
479158af98Sjsing  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
489158af98Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
499158af98Sjsing  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
509158af98Sjsing  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
519158af98Sjsing  * SUCH DAMAGE.
529158af98Sjsing  *
539158af98Sjsing  * The licence and distribution terms for any publically available version or
549158af98Sjsing  * derivative of this code cannot be changed.  i.e. this code cannot simply be
559158af98Sjsing  * copied and put under another distribution licence
569158af98Sjsing  * [including the GNU Public Licence.]
579158af98Sjsing  */
589158af98Sjsing 
599158af98Sjsing #include "ssl_locl.h"
60efee3f2fSjsing #include "tls13_internal.h"
619158af98Sjsing 
629158af98Sjsing static const SSL_METHOD_INTERNAL DTLSv1_method_internal_data = {
63*354e02d3Sjsing 	.dtls = 1,
649158af98Sjsing 	.version = DTLS1_VERSION,
659158af98Sjsing 	.min_version = DTLS1_VERSION,
669158af98Sjsing 	.max_version = DTLS1_VERSION,
679158af98Sjsing 	.ssl_new = dtls1_new,
689158af98Sjsing 	.ssl_clear = dtls1_clear,
699158af98Sjsing 	.ssl_free = dtls1_free,
709158af98Sjsing 	.ssl_accept = ssl3_accept,
719158af98Sjsing 	.ssl_connect = ssl3_connect,
721a6e1177Sjsing 	.ssl_shutdown = ssl3_shutdown,
739158af98Sjsing 	.ssl_renegotiate = ssl3_renegotiate,
749158af98Sjsing 	.ssl_renegotiate_check = ssl3_renegotiate_check,
758dc90bbaSjsing 	.ssl_pending = ssl3_pending,
769158af98Sjsing 	.ssl_read_bytes = dtls1_read_bytes,
779158af98Sjsing 	.ssl_write_bytes = dtls1_write_app_data_bytes,
781d2a9be2Sguenther 	.enc_flags = TLSV1_1_ENC_FLAGS,
799158af98Sjsing };
809158af98Sjsing 
819158af98Sjsing static const SSL_METHOD DTLSv1_method_data = {
829158af98Sjsing 	.ssl_dispatch_alert = dtls1_dispatch_alert,
839158af98Sjsing 	.num_ciphers = ssl3_num_ciphers,
849158af98Sjsing 	.get_cipher = dtls1_get_cipher,
859158af98Sjsing 	.get_cipher_by_char = ssl3_get_cipher_by_char,
869158af98Sjsing 	.put_cipher_by_char = ssl3_put_cipher_by_char,
879158af98Sjsing 	.internal = &DTLSv1_method_internal_data,
889158af98Sjsing };
899158af98Sjsing 
909158af98Sjsing const SSL_METHOD *
919fef1c44Sjsing DTLSv1_client_method(void)
929fef1c44Sjsing {
939fef1c44Sjsing 	return &DTLSv1_method_data;
949fef1c44Sjsing }
959fef1c44Sjsing 
969fef1c44Sjsing const SSL_METHOD *
979158af98Sjsing DTLSv1_method(void)
989158af98Sjsing {
999158af98Sjsing 	return &DTLSv1_method_data;
1009158af98Sjsing }
1019158af98Sjsing 
10271023d34Sjsing const SSL_METHOD *
1039fef1c44Sjsing DTLSv1_server_method(void)
1049fef1c44Sjsing {
1059fef1c44Sjsing 	return &DTLSv1_method_data;
1069fef1c44Sjsing }
1079fef1c44Sjsing 
1089fef1c44Sjsing const SSL_METHOD *
1099fef1c44Sjsing DTLS_client_method(void)
1109fef1c44Sjsing {
1119fef1c44Sjsing 	return DTLSv1_method();
1129fef1c44Sjsing }
1139fef1c44Sjsing 
1149fef1c44Sjsing const SSL_METHOD *
11571023d34Sjsing DTLS_method(void)
11671023d34Sjsing {
11771023d34Sjsing 	return DTLSv1_method();
11871023d34Sjsing }
11971023d34Sjsing 
1209158af98Sjsing const SSL_METHOD *
12171023d34Sjsing DTLS_server_method(void)
12271023d34Sjsing {
1239fef1c44Sjsing 	return DTLSv1_method();
1249158af98Sjsing }
1259158af98Sjsing 
12694149d15Sjsing #if defined(LIBRESSL_HAS_TLS1_3_CLIENT) && defined(LIBRESSL_HAS_TLS1_3_SERVER)
1279158af98Sjsing static const SSL_METHOD_INTERNAL TLS_method_internal_data = {
128*354e02d3Sjsing 	.dtls = 0,
12994149d15Sjsing 	.version = TLS1_3_VERSION,
13094149d15Sjsing 	.min_version = TLS1_VERSION,
13194149d15Sjsing 	.max_version = TLS1_3_VERSION,
13294149d15Sjsing 	.ssl_new = tls1_new,
13394149d15Sjsing 	.ssl_clear = tls1_clear,
13494149d15Sjsing 	.ssl_free = tls1_free,
13594149d15Sjsing 	.ssl_accept = tls13_legacy_accept,
13694149d15Sjsing 	.ssl_connect = tls13_legacy_connect,
13794149d15Sjsing 	.ssl_shutdown = tls13_legacy_shutdown,
13894149d15Sjsing 	.ssl_renegotiate = ssl_undefined_function,
13994149d15Sjsing 	.ssl_renegotiate_check = ssl_ok,
14094149d15Sjsing 	.ssl_pending = tls13_legacy_pending,
14194149d15Sjsing 	.ssl_read_bytes = tls13_legacy_read_bytes,
14294149d15Sjsing 	.ssl_write_bytes = tls13_legacy_write_bytes,
1431d2a9be2Sguenther 	.enc_flags = TLSV1_3_ENC_FLAGS,
14494149d15Sjsing };
14594149d15Sjsing 
14694149d15Sjsing static const SSL_METHOD TLS_method_data = {
14794149d15Sjsing 	.ssl_dispatch_alert = ssl3_dispatch_alert,
14894149d15Sjsing 	.num_ciphers = ssl3_num_ciphers,
14994149d15Sjsing 	.get_cipher = ssl3_get_cipher,
15094149d15Sjsing 	.get_cipher_by_char = ssl3_get_cipher_by_char,
15194149d15Sjsing 	.put_cipher_by_char = ssl3_put_cipher_by_char,
15294149d15Sjsing 	.internal = &TLS_method_internal_data,
15394149d15Sjsing };
15494149d15Sjsing #endif
15594149d15Sjsing 
15694149d15Sjsing static const SSL_METHOD_INTERNAL TLS_legacy_method_internal_data = {
157*354e02d3Sjsing 	.dtls = 0,
1589158af98Sjsing 	.version = TLS1_2_VERSION,
1599158af98Sjsing 	.min_version = TLS1_VERSION,
1609158af98Sjsing 	.max_version = TLS1_2_VERSION,
1619158af98Sjsing 	.ssl_new = tls1_new,
1629158af98Sjsing 	.ssl_clear = tls1_clear,
1639158af98Sjsing 	.ssl_free = tls1_free,
1649158af98Sjsing 	.ssl_accept = ssl3_accept,
1659158af98Sjsing 	.ssl_connect = ssl3_connect,
1661a6e1177Sjsing 	.ssl_shutdown = ssl3_shutdown,
1679158af98Sjsing 	.ssl_renegotiate = ssl_undefined_function,
1689158af98Sjsing 	.ssl_renegotiate_check = ssl_ok,
1698dc90bbaSjsing 	.ssl_pending = ssl3_pending,
1709158af98Sjsing 	.ssl_read_bytes = ssl3_read_bytes,
1719158af98Sjsing 	.ssl_write_bytes = ssl3_write_bytes,
1721d2a9be2Sguenther 	.enc_flags = TLSV1_2_ENC_FLAGS,
1739158af98Sjsing };
1749158af98Sjsing 
17594149d15Sjsing static const SSL_METHOD TLS_legacy_method_data = {
1769158af98Sjsing 	.ssl_dispatch_alert = ssl3_dispatch_alert,
1779158af98Sjsing 	.num_ciphers = ssl3_num_ciphers,
1789158af98Sjsing 	.get_cipher = ssl3_get_cipher,
1799158af98Sjsing 	.get_cipher_by_char = ssl3_get_cipher_by_char,
1809158af98Sjsing 	.put_cipher_by_char = ssl3_put_cipher_by_char,
18194149d15Sjsing 	.internal = &TLS_legacy_method_internal_data,
1829158af98Sjsing };
1839158af98Sjsing 
1849158af98Sjsing static const SSL_METHOD_INTERNAL TLSv1_method_internal_data = {
185*354e02d3Sjsing 	.dtls = 0,
1869158af98Sjsing 	.version = TLS1_VERSION,
1879158af98Sjsing 	.min_version = TLS1_VERSION,
1889158af98Sjsing 	.max_version = TLS1_VERSION,
1899158af98Sjsing 	.ssl_new = tls1_new,
1909158af98Sjsing 	.ssl_clear = tls1_clear,
1919158af98Sjsing 	.ssl_free = tls1_free,
1929158af98Sjsing 	.ssl_accept = ssl3_accept,
1939158af98Sjsing 	.ssl_connect = ssl3_connect,
1941a6e1177Sjsing 	.ssl_shutdown = ssl3_shutdown,
1959158af98Sjsing 	.ssl_renegotiate = ssl3_renegotiate,
1969158af98Sjsing 	.ssl_renegotiate_check = ssl3_renegotiate_check,
1978dc90bbaSjsing 	.ssl_pending = ssl3_pending,
1989158af98Sjsing 	.ssl_read_bytes = ssl3_read_bytes,
1999158af98Sjsing 	.ssl_write_bytes = ssl3_write_bytes,
2001d2a9be2Sguenther 	.enc_flags = TLSV1_ENC_FLAGS,
2019158af98Sjsing };
2029158af98Sjsing 
2039158af98Sjsing static const SSL_METHOD TLSv1_method_data = {
2049158af98Sjsing 	.ssl_dispatch_alert = ssl3_dispatch_alert,
2059158af98Sjsing 	.num_ciphers = ssl3_num_ciphers,
2069158af98Sjsing 	.get_cipher = ssl3_get_cipher,
2079158af98Sjsing 	.get_cipher_by_char = ssl3_get_cipher_by_char,
2089158af98Sjsing 	.put_cipher_by_char = ssl3_put_cipher_by_char,
2099158af98Sjsing 	.internal = &TLSv1_method_internal_data,
2109158af98Sjsing };
2119158af98Sjsing 
2129158af98Sjsing static const SSL_METHOD_INTERNAL TLSv1_1_method_internal_data = {
213*354e02d3Sjsing 	.dtls = 0,
2149158af98Sjsing 	.version = TLS1_1_VERSION,
2159158af98Sjsing 	.min_version = TLS1_1_VERSION,
2169158af98Sjsing 	.max_version = TLS1_1_VERSION,
2179158af98Sjsing 	.ssl_new = tls1_new,
2189158af98Sjsing 	.ssl_clear = tls1_clear,
2199158af98Sjsing 	.ssl_free = tls1_free,
2209158af98Sjsing 	.ssl_accept = ssl3_accept,
2219158af98Sjsing 	.ssl_connect = ssl3_connect,
2221a6e1177Sjsing 	.ssl_shutdown = ssl3_shutdown,
2239158af98Sjsing 	.ssl_renegotiate = ssl3_renegotiate,
2249158af98Sjsing 	.ssl_renegotiate_check = ssl3_renegotiate_check,
2258dc90bbaSjsing 	.ssl_pending = ssl3_pending,
2269158af98Sjsing 	.ssl_read_bytes = ssl3_read_bytes,
2279158af98Sjsing 	.ssl_write_bytes = ssl3_write_bytes,
2281d2a9be2Sguenther 	.enc_flags = TLSV1_1_ENC_FLAGS,
2299158af98Sjsing };
2309158af98Sjsing 
2319158af98Sjsing static const SSL_METHOD TLSv1_1_method_data = {
2329158af98Sjsing 	.ssl_dispatch_alert = ssl3_dispatch_alert,
2339158af98Sjsing 	.num_ciphers = ssl3_num_ciphers,
2349158af98Sjsing 	.get_cipher = ssl3_get_cipher,
2359158af98Sjsing 	.get_cipher_by_char = ssl3_get_cipher_by_char,
2369158af98Sjsing 	.put_cipher_by_char = ssl3_put_cipher_by_char,
2379158af98Sjsing 	.internal = &TLSv1_1_method_internal_data,
2389158af98Sjsing };
2399158af98Sjsing 
2409158af98Sjsing static const SSL_METHOD_INTERNAL TLSv1_2_method_internal_data = {
241*354e02d3Sjsing 	.dtls = 0,
2429158af98Sjsing 	.version = TLS1_2_VERSION,
2439158af98Sjsing 	.min_version = TLS1_2_VERSION,
2449158af98Sjsing 	.max_version = TLS1_2_VERSION,
2459158af98Sjsing 	.ssl_new = tls1_new,
2469158af98Sjsing 	.ssl_clear = tls1_clear,
2479158af98Sjsing 	.ssl_free = tls1_free,
2489158af98Sjsing 	.ssl_accept = ssl3_accept,
2499158af98Sjsing 	.ssl_connect = ssl3_connect,
2501a6e1177Sjsing 	.ssl_shutdown = ssl3_shutdown,
2519158af98Sjsing 	.ssl_renegotiate = ssl3_renegotiate,
2529158af98Sjsing 	.ssl_renegotiate_check = ssl3_renegotiate_check,
2538dc90bbaSjsing 	.ssl_pending = ssl3_pending,
2549158af98Sjsing 	.ssl_read_bytes = ssl3_read_bytes,
2559158af98Sjsing 	.ssl_write_bytes = ssl3_write_bytes,
2561d2a9be2Sguenther 	.enc_flags = TLSV1_2_ENC_FLAGS,
2579158af98Sjsing };
2589158af98Sjsing 
2599158af98Sjsing static const SSL_METHOD TLSv1_2_method_data = {
2609158af98Sjsing 	.ssl_dispatch_alert = ssl3_dispatch_alert,
2619158af98Sjsing 	.num_ciphers = ssl3_num_ciphers,
2629158af98Sjsing 	.get_cipher = ssl3_get_cipher,
2639158af98Sjsing 	.get_cipher_by_char = ssl3_get_cipher_by_char,
2649158af98Sjsing 	.put_cipher_by_char = ssl3_put_cipher_by_char,
2659158af98Sjsing 	.internal = &TLSv1_2_method_internal_data,
2669158af98Sjsing };
2679158af98Sjsing 
2689158af98Sjsing const SSL_METHOD *
2699fef1c44Sjsing TLS_client_method(void)
2709158af98Sjsing {
2719fef1c44Sjsing 	return TLS_method();
2729158af98Sjsing }
2739158af98Sjsing 
2749158af98Sjsing const SSL_METHOD *
2759158af98Sjsing TLS_method(void)
2769158af98Sjsing {
27794149d15Sjsing #if defined(LIBRESSL_HAS_TLS1_3_CLIENT) && defined(LIBRESSL_HAS_TLS1_3_SERVER)
27894149d15Sjsing 	return (&TLS_method_data);
27994149d15Sjsing #else
28094149d15Sjsing 	return tls_legacy_method();
28194149d15Sjsing #endif
28294149d15Sjsing }
28394149d15Sjsing 
28494149d15Sjsing const SSL_METHOD *
2859fef1c44Sjsing TLS_server_method(void)
2869fef1c44Sjsing {
2879fef1c44Sjsing 	return TLS_method();
2889fef1c44Sjsing }
2899fef1c44Sjsing 
2909fef1c44Sjsing const SSL_METHOD *
29194149d15Sjsing tls_legacy_method(void)
29294149d15Sjsing {
29394149d15Sjsing 	return (&TLS_legacy_method_data);
2949158af98Sjsing }
2959158af98Sjsing 
2969158af98Sjsing const SSL_METHOD *
2979fef1c44Sjsing SSLv23_client_method(void)
2989fef1c44Sjsing {
2999fef1c44Sjsing 	return TLS_method();
3009fef1c44Sjsing }
3019fef1c44Sjsing 
3029fef1c44Sjsing const SSL_METHOD *
3039fef1c44Sjsing SSLv23_method(void)
3049fef1c44Sjsing {
3059fef1c44Sjsing 	return TLS_method();
3069fef1c44Sjsing }
3079fef1c44Sjsing 
3089fef1c44Sjsing const SSL_METHOD *
3099fef1c44Sjsing SSLv23_server_method(void)
3109fef1c44Sjsing {
3119fef1c44Sjsing 	return TLS_method();
3129fef1c44Sjsing }
3139fef1c44Sjsing 
3149fef1c44Sjsing const SSL_METHOD *
3159fef1c44Sjsing TLSv1_client_method(void)
3169fef1c44Sjsing {
3179fef1c44Sjsing 	return (&TLSv1_method_data);
3189fef1c44Sjsing }
3199fef1c44Sjsing 
3209fef1c44Sjsing const SSL_METHOD *
3219158af98Sjsing TLSv1_method(void)
3229158af98Sjsing {
3239158af98Sjsing 	return (&TLSv1_method_data);
3249158af98Sjsing }
3259158af98Sjsing 
3269158af98Sjsing const SSL_METHOD *
3279fef1c44Sjsing TLSv1_server_method(void)
3289fef1c44Sjsing {
3299fef1c44Sjsing 	return (&TLSv1_method_data);
3309fef1c44Sjsing }
3319fef1c44Sjsing 
3329fef1c44Sjsing const SSL_METHOD *
3339fef1c44Sjsing TLSv1_1_client_method(void)
3349fef1c44Sjsing {
3359fef1c44Sjsing 	return (&TLSv1_1_method_data);
3369fef1c44Sjsing }
3379fef1c44Sjsing 
3389fef1c44Sjsing const SSL_METHOD *
3399158af98Sjsing TLSv1_1_method(void)
3409158af98Sjsing {
3419158af98Sjsing 	return (&TLSv1_1_method_data);
3429158af98Sjsing }
3439158af98Sjsing 
3449158af98Sjsing const SSL_METHOD *
3459fef1c44Sjsing TLSv1_1_server_method(void)
3469fef1c44Sjsing {
3479fef1c44Sjsing 	return (&TLSv1_1_method_data);
3489fef1c44Sjsing }
3499fef1c44Sjsing 
3509fef1c44Sjsing const SSL_METHOD *
3519fef1c44Sjsing TLSv1_2_client_method(void)
3529fef1c44Sjsing {
3539fef1c44Sjsing 	return (&TLSv1_2_method_data);
3549fef1c44Sjsing }
3559fef1c44Sjsing 
3569fef1c44Sjsing const SSL_METHOD *
3579158af98Sjsing TLSv1_2_method(void)
3589158af98Sjsing {
3599158af98Sjsing 	return (&TLSv1_2_method_data);
3609158af98Sjsing }
3619158af98Sjsing 
3629158af98Sjsing const SSL_METHOD *
3639158af98Sjsing TLSv1_2_server_method(void)
3649158af98Sjsing {
3659fef1c44Sjsing 	return (&TLSv1_2_method_data);
3669158af98Sjsing }
367bfc125deSjsing 
368bfc125deSjsing const SSL_METHOD *
3699fef1c44Sjsing ssl_get_method(uint16_t version)
370bfc125deSjsing {
371bfc125deSjsing 	if (version == TLS1_3_VERSION)
3729fef1c44Sjsing 		return (TLS_method());
373bfc125deSjsing 	if (version == TLS1_2_VERSION)
3749fef1c44Sjsing 		return (TLSv1_2_method());
375bfc125deSjsing 	if (version == TLS1_1_VERSION)
3769fef1c44Sjsing 		return (TLSv1_1_method());
377bfc125deSjsing 	if (version == TLS1_VERSION)
3789fef1c44Sjsing 		return (TLSv1_method());
379bfc125deSjsing 	if (version == DTLS1_VERSION)
3809fef1c44Sjsing 		return (DTLSv1_method());
381bfc125deSjsing 
382bfc125deSjsing 	return (NULL);
383bfc125deSjsing }
384