ssl_test_ctx.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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/e_os2.h>
  11. #include <openssl/crypto.h>
  12. #include "e_os.h"
  13. #include "ssl_test_ctx.h"
  14. /* True enums and other test configuration values that map to an int. */
  15. typedef struct {
  16. const char *name;
  17. int value;
  18. } test_enum;
  19. __owur static int parse_enum(const test_enum *enums, size_t num_enums,
  20. int *value, const char *name)
  21. {
  22. size_t i;
  23. for (i = 0; i < num_enums; i++) {
  24. if (strcmp(enums[i].name, name) == 0) {
  25. *value = enums[i].value;
  26. return 1;
  27. }
  28. }
  29. return 0;
  30. }
  31. static const char *enum_name(const test_enum *enums, size_t num_enums,
  32. int value)
  33. {
  34. size_t i;
  35. for (i = 0; i < num_enums; i++) {
  36. if (enums[i].value == value) {
  37. return enums[i].name;
  38. }
  39. }
  40. return "InvalidValue";
  41. }
  42. /*******************/
  43. /* ExpectedResult. */
  44. /*******************/
  45. static const test_enum ssl_test_results[] = {
  46. {"Success", SSL_TEST_SUCCESS},
  47. {"ServerFail", SSL_TEST_SERVER_FAIL},
  48. {"ClientFail", SSL_TEST_CLIENT_FAIL},
  49. {"InternalError", SSL_TEST_INTERNAL_ERROR},
  50. };
  51. __owur static int parse_expected_result(SSL_TEST_CTX *test_ctx, const char *value)
  52. {
  53. int ret_value;
  54. if (!parse_enum(ssl_test_results, OSSL_NELEM(ssl_test_results),
  55. &ret_value, value)) {
  56. return 0;
  57. }
  58. test_ctx->expected_result = ret_value;
  59. return 1;
  60. }
  61. const char *ssl_test_result_name(ssl_test_result_t result)
  62. {
  63. return enum_name(ssl_test_results, OSSL_NELEM(ssl_test_results), result);
  64. }
  65. /******************************/
  66. /* ClientAlert / ServerAlert. */
  67. /******************************/
  68. static const test_enum ssl_alerts[] = {
  69. {"UnknownCA", SSL_AD_UNKNOWN_CA},
  70. {"HandshakeFailure", SSL_AD_HANDSHAKE_FAILURE},
  71. };
  72. __owur static int parse_alert(int *alert, const char *value)
  73. {
  74. return parse_enum(ssl_alerts, OSSL_NELEM(ssl_alerts), alert, value);
  75. }
  76. __owur static int parse_client_alert(SSL_TEST_CTX *test_ctx, const char *value)
  77. {
  78. return parse_alert(&test_ctx->client_alert, value);
  79. }
  80. __owur static int parse_server_alert(SSL_TEST_CTX *test_ctx, const char *value)
  81. {
  82. return parse_alert(&test_ctx->server_alert, value);
  83. }
  84. const char *ssl_alert_name(int alert)
  85. {
  86. return enum_name(ssl_alerts, OSSL_NELEM(ssl_alerts), alert);
  87. }
  88. /************/
  89. /* Protocol */
  90. /************/
  91. static const test_enum ssl_protocols[] = {
  92. {"TLSv1.2", TLS1_2_VERSION},
  93. {"TLSv1.1", TLS1_1_VERSION},
  94. {"TLSv1", TLS1_VERSION},
  95. {"SSLv3", SSL3_VERSION},
  96. };
  97. __owur static int parse_protocol(SSL_TEST_CTX *test_ctx, const char *value)
  98. {
  99. return parse_enum(ssl_protocols, OSSL_NELEM(ssl_protocols),
  100. &test_ctx->protocol, value);
  101. }
  102. const char *ssl_protocol_name(int protocol)
  103. {
  104. return enum_name(ssl_protocols, OSSL_NELEM(ssl_protocols), protocol);
  105. }
  106. /***********************/
  107. /* CertVerifyCallback. */
  108. /***********************/
  109. static const test_enum ssl_verify_callbacks[] = {
  110. {"None", SSL_TEST_VERIFY_NONE},
  111. {"AcceptAll", SSL_TEST_VERIFY_ACCEPT_ALL},
  112. {"RejectAll", SSL_TEST_VERIFY_REJECT_ALL},
  113. };
  114. __owur static int parse_client_verify_callback(SSL_TEST_CTX *test_ctx,
  115. const char *value)
  116. {
  117. int ret_value;
  118. if (!parse_enum(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
  119. &ret_value, value)) {
  120. return 0;
  121. }
  122. test_ctx->client_verify_callback = ret_value;
  123. return 1;
  124. }
  125. const char *ssl_verify_callback_name(ssl_verify_callback_t callback)
  126. {
  127. return enum_name(ssl_verify_callbacks, OSSL_NELEM(ssl_verify_callbacks),
  128. callback);
  129. }
  130. /*************************************************************/
  131. /* Known test options and their corresponding parse methods. */
  132. /*************************************************************/
  133. typedef struct {
  134. const char *name;
  135. int (*parse)(SSL_TEST_CTX *test_ctx, const char *value);
  136. } ssl_test_ctx_option;
  137. static const ssl_test_ctx_option ssl_test_ctx_options[] = {
  138. { "ExpectedResult", &parse_expected_result },
  139. { "ClientAlert", &parse_client_alert },
  140. { "ServerAlert", &parse_server_alert },
  141. { "Protocol", &parse_protocol },
  142. { "ClientVerifyCallback", &parse_client_verify_callback },
  143. };
  144. /*
  145. * Since these methods are used to create tests, we use OPENSSL_assert liberally
  146. * for malloc failures and other internal errors.
  147. */
  148. SSL_TEST_CTX *SSL_TEST_CTX_new()
  149. {
  150. SSL_TEST_CTX *ret;
  151. ret = OPENSSL_zalloc(sizeof(*ret));
  152. OPENSSL_assert(ret != NULL);
  153. return ret;
  154. }
  155. void SSL_TEST_CTX_free(SSL_TEST_CTX *ctx)
  156. {
  157. OPENSSL_free(ctx);
  158. }
  159. SSL_TEST_CTX *SSL_TEST_CTX_create(const CONF *conf, const char *test_section)
  160. {
  161. STACK_OF(CONF_VALUE) *sk_conf;
  162. SSL_TEST_CTX *ctx;
  163. int i;
  164. size_t j;
  165. sk_conf = NCONF_get_section(conf, test_section);
  166. OPENSSL_assert(sk_conf != NULL);
  167. ctx = SSL_TEST_CTX_new();
  168. OPENSSL_assert(ctx != NULL);
  169. for (i = 0; i < sk_CONF_VALUE_num(sk_conf); i++) {
  170. int found = 0;
  171. const CONF_VALUE *option = sk_CONF_VALUE_value(sk_conf, i);
  172. for (j = 0; j < OSSL_NELEM(ssl_test_ctx_options); j++) {
  173. if (strcmp(option->name, ssl_test_ctx_options[j].name) == 0) {
  174. if (!ssl_test_ctx_options[j].parse(ctx, option->value)) {
  175. fprintf(stderr, "Bad value %s for option %s\n",
  176. option->value, option->name);
  177. goto err;
  178. }
  179. found = 1;
  180. break;
  181. }
  182. }
  183. if (!found) {
  184. fprintf(stderr, "Unknown test option: %s\n", option->name);
  185. goto err;
  186. }
  187. }
  188. goto done;
  189. err:
  190. SSL_TEST_CTX_free(ctx);
  191. ctx = NULL;
  192. done:
  193. return ctx;
  194. }