ssl_ctx_test.c 5.4 KB

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