sslprovidertest.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2019 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 <string.h>
  10. #include <openssl/provider.h>
  11. #include "ssltestlib.h"
  12. #include "testutil.h"
  13. static char *cert = NULL;
  14. static char *privkey = NULL;
  15. static char *modulename = NULL;
  16. static char *configfile = NULL;
  17. static OSSL_PROVIDER *defctxlegacy = NULL;
  18. static int test_different_libctx(void)
  19. {
  20. SSL_CTX *cctx = NULL, *sctx = NULL;
  21. SSL *clientssl = NULL, *serverssl = NULL;
  22. int testresult = 0;
  23. OPENSSL_CTX *libctx = OPENSSL_CTX_new();
  24. OSSL_PROVIDER *prov = NULL;
  25. /*
  26. * Verify that the default and fips providers in the default libctx are not
  27. * available
  28. */
  29. if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
  30. || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
  31. goto end;
  32. if (!TEST_true(OPENSSL_CTX_load_config(libctx, configfile)))
  33. goto end;
  34. prov = OSSL_PROVIDER_load(libctx, modulename);
  35. if (!TEST_ptr(prov)
  36. /* Check we have the provider available */
  37. || !TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
  38. goto end;
  39. /* Check the default provider is not available */
  40. if (strcmp(modulename, "default") != 0
  41. && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
  42. goto end;
  43. TEST_note("%s provider loaded", modulename);
  44. /*
  45. * TODO(3.0): Make this work in TLSv1.3. Currently we can only do RSA key
  46. * exchange, because we don't have key gen/param gen for EC yet - which
  47. * implies TLSv1.2 only
  48. */
  49. if (!TEST_true(create_ssl_ctx_pair(libctx,
  50. TLS_server_method(),
  51. TLS_client_method(),
  52. TLS1_VERSION,
  53. TLS1_2_VERSION,
  54. &sctx, &cctx, cert, privkey)))
  55. goto end;
  56. /* Ensure we use a FIPS compatible ciphersuite and sigalg */
  57. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES128-SHA256"))
  58. || !TEST_true(SSL_CTX_set1_sigalgs_list(cctx, "RSA+SHA256")))
  59. goto end;
  60. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  61. NULL, NULL)))
  62. goto end;
  63. /* This time we expect success */
  64. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  65. goto end;
  66. /*
  67. * Verify that the default and fips providers in the default libctx are
  68. * still not available
  69. */
  70. if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
  71. || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
  72. goto end;
  73. testresult = 1;
  74. end:
  75. SSL_free(serverssl);
  76. SSL_free(clientssl);
  77. SSL_CTX_free(sctx);
  78. SSL_CTX_free(cctx);
  79. OSSL_PROVIDER_unload(prov);
  80. OPENSSL_CTX_free(libctx);
  81. return testresult;
  82. }
  83. int setup_tests(void)
  84. {
  85. char *certsdir = NULL;
  86. if (!test_skip_common_options()) {
  87. TEST_error("Error parsing test options\n");
  88. return 0;
  89. }
  90. if (!TEST_ptr(certsdir = test_get_argument(0))
  91. || !TEST_ptr(modulename = test_get_argument(1))
  92. || !TEST_ptr(configfile = test_get_argument(2)))
  93. return 0;
  94. cert = test_mk_file_path(certsdir, "servercert.pem");
  95. if (cert == NULL)
  96. return 0;
  97. privkey = test_mk_file_path(certsdir, "serverkey.pem");
  98. if (privkey == NULL) {
  99. OPENSSL_free(cert);
  100. return 0;
  101. }
  102. /*
  103. * For tests in this file we want to ensure the default ctx does not have
  104. * the default provider loaded into the default ctx. So we load "legacy" to
  105. * prevent default from being auto-loaded. This tests that there is no
  106. * "leakage", i.e. when using SSL_CTX_new_with_libctx() we expect only the
  107. * specific libctx to be used - nothing should fall back to the default
  108. * libctx
  109. */
  110. defctxlegacy = OSSL_PROVIDER_load(NULL, "legacy");
  111. ADD_TEST(test_different_libctx);
  112. return 1;
  113. }
  114. void cleanup_tests(void)
  115. {
  116. OSSL_PROVIDER_unload(defctxlegacy);
  117. }