You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

262 line
10 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include "test_config.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <memory>
  19. #include <openssl/base64.h>
  20. namespace {
  21. template <typename T>
  22. struct Flag {
  23. const char *flag;
  24. T TestConfig::*member;
  25. };
  26. // FindField looks for the flag in |flags| that matches |flag|. If one is found,
  27. // it returns a pointer to the corresponding field in |config|. Otherwise, it
  28. // returns NULL.
  29. template<typename T, size_t N>
  30. T *FindField(TestConfig *config, const Flag<T> (&flags)[N], const char *flag) {
  31. for (size_t i = 0; i < N; i++) {
  32. if (strcmp(flag, flags[i].flag) == 0) {
  33. return &(config->*(flags[i].member));
  34. }
  35. }
  36. return NULL;
  37. }
  38. const Flag<bool> kBoolFlags[] = {
  39. { "-server", &TestConfig::is_server },
  40. { "-dtls", &TestConfig::is_dtls },
  41. { "-fallback-scsv", &TestConfig::fallback_scsv },
  42. { "-require-any-client-certificate",
  43. &TestConfig::require_any_client_certificate },
  44. { "-false-start", &TestConfig::false_start },
  45. { "-async", &TestConfig::async },
  46. { "-write-different-record-sizes",
  47. &TestConfig::write_different_record_sizes },
  48. { "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
  49. { "-partial-write", &TestConfig::partial_write },
  50. { "-no-tls13", &TestConfig::no_tls13 },
  51. { "-no-tls12", &TestConfig::no_tls12 },
  52. { "-no-tls11", &TestConfig::no_tls11 },
  53. { "-no-tls1", &TestConfig::no_tls1 },
  54. { "-no-ssl3", &TestConfig::no_ssl3 },
  55. { "-enable-channel-id", &TestConfig::enable_channel_id },
  56. { "-shim-writes-first", &TestConfig::shim_writes_first },
  57. { "-expect-session-miss", &TestConfig::expect_session_miss },
  58. { "-decline-alpn", &TestConfig::decline_alpn },
  59. { "-expect-extended-master-secret",
  60. &TestConfig::expect_extended_master_secret },
  61. { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
  62. { "-enable-signed-cert-timestamps",
  63. &TestConfig::enable_signed_cert_timestamps },
  64. { "-implicit-handshake", &TestConfig::implicit_handshake },
  65. { "-use-early-callback", &TestConfig::use_early_callback },
  66. { "-fail-early-callback", &TestConfig::fail_early_callback },
  67. { "-install-ddos-callback", &TestConfig::install_ddos_callback },
  68. { "-fail-ddos-callback", &TestConfig::fail_ddos_callback },
  69. { "-fail-second-ddos-callback", &TestConfig::fail_second_ddos_callback },
  70. { "-fail-cert-callback", &TestConfig::fail_cert_callback },
  71. { "-handshake-never-done", &TestConfig::handshake_never_done },
  72. { "-use-export-context", &TestConfig::use_export_context },
  73. { "-tls-unique", &TestConfig::tls_unique },
  74. { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal },
  75. { "-expect-no-session", &TestConfig::expect_no_session },
  76. { "-expect-early-data-info", &TestConfig::expect_early_data_info },
  77. { "-use-ticket-callback", &TestConfig::use_ticket_callback },
  78. { "-renew-ticket", &TestConfig::renew_ticket },
  79. { "-enable-early-data", &TestConfig::enable_early_data },
  80. { "-enable-client-custom-extension",
  81. &TestConfig::enable_client_custom_extension },
  82. { "-enable-server-custom-extension",
  83. &TestConfig::enable_server_custom_extension },
  84. { "-custom-extension-skip", &TestConfig::custom_extension_skip },
  85. { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add },
  86. { "-check-close-notify", &TestConfig::check_close_notify },
  87. { "-shim-shuts-down", &TestConfig::shim_shuts_down },
  88. { "-verify-fail", &TestConfig::verify_fail },
  89. { "-verify-peer", &TestConfig::verify_peer },
  90. { "-expect-verify-result", &TestConfig::expect_verify_result },
  91. { "-renegotiate-once", &TestConfig::renegotiate_once },
  92. { "-renegotiate-freely", &TestConfig::renegotiate_freely },
  93. { "-renegotiate-ignore", &TestConfig::renegotiate_ignore },
  94. { "-p384-only", &TestConfig::p384_only },
  95. { "-enable-all-curves", &TestConfig::enable_all_curves },
  96. { "-use-sparse-dh-prime", &TestConfig::use_sparse_dh_prime },
  97. { "-use-old-client-cert-callback",
  98. &TestConfig::use_old_client_cert_callback },
  99. { "-use-null-client-ca-list", &TestConfig::use_null_client_ca_list },
  100. { "-send-alert", &TestConfig::send_alert },
  101. { "-peek-then-read", &TestConfig::peek_then_read },
  102. { "-enable-grease", &TestConfig::enable_grease },
  103. { "-use-exporter-between-reads", &TestConfig::use_exporter_between_reads },
  104. { "-retain-only-sha256-client-cert-initial",
  105. &TestConfig::retain_only_sha256_client_cert_initial },
  106. { "-retain-only-sha256-client-cert-resume",
  107. &TestConfig::retain_only_sha256_client_cert_resume },
  108. { "-expect-sha256-client-cert-initial",
  109. &TestConfig::expect_sha256_client_cert_initial },
  110. { "-expect-sha256-client-cert-resume",
  111. &TestConfig::expect_sha256_client_cert_resume },
  112. { "-enable-short-header", &TestConfig::enable_short_header },
  113. { "-read-with-unfinished-write", &TestConfig::read_with_unfinished_write },
  114. { "-expect-secure-renegotiation",
  115. &TestConfig::expect_secure_renegotiation },
  116. { "-expect-no-secure-renegotiation",
  117. &TestConfig::expect_no_secure_renegotiation },
  118. };
  119. const Flag<std::string> kStringFlags[] = {
  120. { "-digest-prefs", &TestConfig::digest_prefs },
  121. { "-key-file", &TestConfig::key_file },
  122. { "-cert-file", &TestConfig::cert_file },
  123. { "-expect-server-name", &TestConfig::expected_server_name },
  124. { "-advertise-npn", &TestConfig::advertise_npn },
  125. { "-expect-next-proto", &TestConfig::expected_next_proto },
  126. { "-select-next-proto", &TestConfig::select_next_proto },
  127. { "-send-channel-id", &TestConfig::send_channel_id },
  128. { "-host-name", &TestConfig::host_name },
  129. { "-advertise-alpn", &TestConfig::advertise_alpn },
  130. { "-expect-alpn", &TestConfig::expected_alpn },
  131. { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
  132. { "-select-alpn", &TestConfig::select_alpn },
  133. { "-psk", &TestConfig::psk },
  134. { "-psk-identity", &TestConfig::psk_identity },
  135. { "-srtp-profiles", &TestConfig::srtp_profiles },
  136. { "-cipher", &TestConfig::cipher },
  137. { "-export-label", &TestConfig::export_label },
  138. { "-export-context", &TestConfig::export_context },
  139. { "-expect-peer-cert-file", &TestConfig::expect_peer_cert_file },
  140. };
  141. const Flag<std::string> kBase64Flags[] = {
  142. { "-expect-certificate-types", &TestConfig::expected_certificate_types },
  143. { "-expect-channel-id", &TestConfig::expected_channel_id },
  144. { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
  145. { "-expect-signed-cert-timestamps",
  146. &TestConfig::expected_signed_cert_timestamps },
  147. { "-ocsp-response", &TestConfig::ocsp_response },
  148. { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
  149. { "-ticket-key", &TestConfig::ticket_key },
  150. };
  151. const Flag<int> kIntFlags[] = {
  152. { "-port", &TestConfig::port },
  153. { "-resume-count", &TestConfig::resume_count },
  154. { "-min-version", &TestConfig::min_version },
  155. { "-max-version", &TestConfig::max_version },
  156. { "-mtu", &TestConfig::mtu },
  157. { "-export-keying-material", &TestConfig::export_keying_material },
  158. { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
  159. { "-expect-peer-signature-algorithm",
  160. &TestConfig::expect_peer_signature_algorithm },
  161. { "-expect-curve-id", &TestConfig::expect_curve_id },
  162. { "-expect-resume-curve-id", &TestConfig::expect_resume_curve_id },
  163. { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms },
  164. { "-max-cert-list", &TestConfig::max_cert_list },
  165. { "-expect-cipher-aes", &TestConfig::expect_cipher_aes },
  166. { "-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes },
  167. { "-resumption-delay", &TestConfig::resumption_delay },
  168. { "-max-send-fragment", &TestConfig::max_send_fragment },
  169. { "-read-size", &TestConfig::read_size },
  170. };
  171. const Flag<std::vector<int>> kIntVectorFlags[] = {
  172. { "-signing-prefs", &TestConfig::signing_prefs },
  173. };
  174. } // namespace
  175. bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
  176. for (int i = 0; i < argc; i++) {
  177. bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
  178. if (bool_field != NULL) {
  179. *bool_field = true;
  180. continue;
  181. }
  182. std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
  183. if (string_field != NULL) {
  184. i++;
  185. if (i >= argc) {
  186. fprintf(stderr, "Missing parameter\n");
  187. return false;
  188. }
  189. string_field->assign(argv[i]);
  190. continue;
  191. }
  192. std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
  193. if (base64_field != NULL) {
  194. i++;
  195. if (i >= argc) {
  196. fprintf(stderr, "Missing parameter\n");
  197. return false;
  198. }
  199. size_t len;
  200. if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
  201. fprintf(stderr, "Invalid base64: %s\n", argv[i]);
  202. return false;
  203. }
  204. std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
  205. if (!EVP_DecodeBase64(decoded.get(), &len, len,
  206. reinterpret_cast<const uint8_t *>(argv[i]),
  207. strlen(argv[i]))) {
  208. fprintf(stderr, "Invalid base64: %s\n", argv[i]);
  209. return false;
  210. }
  211. base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
  212. continue;
  213. }
  214. int *int_field = FindField(out_config, kIntFlags, argv[i]);
  215. if (int_field) {
  216. i++;
  217. if (i >= argc) {
  218. fprintf(stderr, "Missing parameter\n");
  219. return false;
  220. }
  221. *int_field = atoi(argv[i]);
  222. continue;
  223. }
  224. std::vector<int> *int_vector_field =
  225. FindField(out_config, kIntVectorFlags, argv[i]);
  226. if (int_vector_field) {
  227. i++;
  228. if (i >= argc) {
  229. fprintf(stderr, "Missing parameter\n");
  230. return false;
  231. }
  232. // Each instance of the flag adds to the list.
  233. int_vector_field->push_back(atoi(argv[i]));
  234. continue;
  235. }
  236. fprintf(stderr, "Unknown argument: %s\n", argv[i]);
  237. return false;
  238. }
  239. return true;
  240. }