1 /* 2 * Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (the "License"). You may not use 5 * this file except in compliance with the License. You can obtain a copy 6 * in the file LICENSE in the source distribution or at 7 * https://www.openssl.org/source/license.html 8 */ 9 10 #include "testutil.h" 11 #include <openssl/ssl.h> 12 13 typedef struct { 14 int proto; 15 int min_version; 16 int max_version; 17 int min_ok; 18 int max_ok; 19 int expected_min; 20 int expected_max; 21 } version_test; 22 23 #define PROTO_TLS 0 24 #define PROTO_DTLS 1 25 #define PROTO_QUIC 2 26 27 /* 28 * If a version is valid for *any* protocol then setting the min/max protocol is 29 * expected to return success, even if that version is not valid for *this* 30 * protocol. However it only has an effect if it is valid for *this* protocol - 31 * otherwise it is ignored. 32 */ 33 static const version_test version_testdata[] = { 34 /* proto min max ok expected min expected max */ 35 {PROTO_TLS, 0, 0, 1, 1, 0, 0}, 36 {PROTO_TLS, SSL3_VERSION, TLS1_3_VERSION, 1, 1, SSL3_VERSION, TLS1_3_VERSION}, 37 {PROTO_TLS, TLS1_VERSION, TLS1_3_VERSION, 1, 1, TLS1_VERSION, TLS1_3_VERSION}, 38 {PROTO_TLS, TLS1_VERSION, TLS1_2_VERSION, 1, 1, TLS1_VERSION, TLS1_2_VERSION}, 39 {PROTO_TLS, TLS1_2_VERSION, TLS1_2_VERSION, 1, 1, TLS1_2_VERSION, TLS1_2_VERSION}, 40 {PROTO_TLS, TLS1_2_VERSION, TLS1_1_VERSION, 1, 1, TLS1_2_VERSION, TLS1_1_VERSION}, 41 {PROTO_TLS, SSL3_VERSION - 1, TLS1_3_VERSION, 0, 1, 0, TLS1_3_VERSION}, 42 {PROTO_TLS, SSL3_VERSION, TLS1_3_VERSION + 1, 1, 0, SSL3_VERSION, 0}, 43 #ifndef OPENSSL_NO_DTLS 44 {PROTO_TLS, DTLS1_VERSION, DTLS1_2_VERSION, 1, 1, 0, 0}, 45 #endif 46 {PROTO_TLS, OSSL_QUIC1_VERSION, OSSL_QUIC1_VERSION, 0, 0, 0, 0}, 47 {PROTO_TLS, 7, 42, 0, 0, 0, 0}, 48 {PROTO_DTLS, 0, 0, 1, 1, 0, 0}, 49 {PROTO_DTLS, DTLS1_VERSION, DTLS1_2_VERSION, 1, 1, DTLS1_VERSION, DTLS1_2_VERSION}, 50 #ifndef OPENSSL_NO_DTLS1_2 51 {PROTO_DTLS, DTLS1_2_VERSION, DTLS1_2_VERSION, 1, 1, DTLS1_2_VERSION, DTLS1_2_VERSION}, 52 #endif 53 #ifndef OPENSSL_NO_DTLS1 54 {PROTO_DTLS, DTLS1_VERSION, DTLS1_VERSION, 1, 1, DTLS1_VERSION, DTLS1_VERSION}, 55 #endif 56 #if !defined(OPENSSL_NO_DTLS1) && !defined(OPENSSL_NO_DTLS1_2) 57 {PROTO_DTLS, DTLS1_2_VERSION, DTLS1_VERSION, 1, 1, DTLS1_2_VERSION, DTLS1_VERSION}, 58 #endif 59 {PROTO_DTLS, DTLS1_VERSION + 1, DTLS1_2_VERSION, 0, 1, 0, DTLS1_2_VERSION}, 60 {PROTO_DTLS, DTLS1_VERSION, DTLS1_2_VERSION - 1, 1, 0, DTLS1_VERSION, 0}, 61 {PROTO_DTLS, TLS1_VERSION, TLS1_3_VERSION, 1, 1, 0, 0}, 62 {PROTO_DTLS, OSSL_QUIC1_VERSION, OSSL_QUIC1_VERSION, 0, 0, 0, 0}, 63 /* These functions never have an effect when called on a QUIC object */ 64 {PROTO_QUIC, 0, 0, 1, 1, 0, 0}, 65 {PROTO_QUIC, OSSL_QUIC1_VERSION, OSSL_QUIC1_VERSION, 0, 0, 0, 0}, 66 {PROTO_QUIC, OSSL_QUIC1_VERSION, OSSL_QUIC1_VERSION + 1, 0, 0, 0, 0}, 67 {PROTO_QUIC, TLS1_VERSION, TLS1_3_VERSION, 1, 1, 0, 0}, 68 #ifndef OPENSSL_NO_DTLS 69 {PROTO_QUIC, DTLS1_VERSION, DTLS1_2_VERSION, 1, 1, 0, 0}, 70 #endif 71 }; 72 73 static int test_set_min_max_version(int idx_tst) 74 { 75 SSL_CTX *ctx = NULL; 76 SSL *ssl = NULL; 77 int testresult = 0; 78 version_test t = version_testdata[idx_tst]; 79 const SSL_METHOD *meth = NULL; 80 81 switch (t.proto) { 82 case PROTO_TLS: 83 meth = TLS_client_method(); 84 break; 85 86 #ifndef OPENSSL_NO_DTLS 87 case PROTO_DTLS: 88 meth = DTLS_client_method(); 89 break; 90 #endif 91 92 #ifndef OPENSSL_NO_QUIC 93 case PROTO_QUIC: 94 meth = OSSL_QUIC_client_method(); 95 break; 96 #endif 97 } 98 99 if (meth == NULL) 100 return TEST_skip("Protocol not supported"); 101 102 ctx = SSL_CTX_new(meth); 103 if (ctx == NULL) 104 goto end; 105 106 ssl = SSL_new(ctx); 107 if (ssl == NULL) 108 goto end; 109 110 if (!TEST_int_eq(SSL_CTX_set_min_proto_version(ctx, t.min_version), t.min_ok)) 111 goto end; 112 if (!TEST_int_eq(SSL_CTX_set_max_proto_version(ctx, t.max_version), t.max_ok)) 113 goto end; 114 if (!TEST_int_eq(SSL_CTX_get_min_proto_version(ctx), t.expected_min)) 115 goto end; 116 if (!TEST_int_eq(SSL_CTX_get_max_proto_version(ctx), t.expected_max)) 117 goto end; 118 119 if (!TEST_int_eq(SSL_set_min_proto_version(ssl, t.min_version), t.min_ok)) 120 goto end; 121 if (!TEST_int_eq(SSL_set_max_proto_version(ssl, t.max_version), t.max_ok)) 122 goto end; 123 if (!TEST_int_eq(SSL_get_min_proto_version(ssl), t.expected_min)) 124 goto end; 125 if (!TEST_int_eq(SSL_get_max_proto_version(ssl), t.expected_max)) 126 goto end; 127 128 testresult = 1; 129 130 end: 131 SSL_free(ssl); 132 SSL_CTX_free(ctx); 133 return testresult; 134 } 135 136 int setup_tests(void) 137 { 138 ADD_ALL_TESTS(test_set_min_max_version, sizeof(version_testdata) / sizeof(version_test)); 139 return 1; 140 } 141