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