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.
 
 
 
 
 
 

319 lines
12 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. { "-verify-peer-if-no-obc", &TestConfig::verify_peer_if_no_obc },
  91. { "-expect-verify-result", &TestConfig::expect_verify_result },
  92. { "-renegotiate-once", &TestConfig::renegotiate_once },
  93. { "-renegotiate-freely", &TestConfig::renegotiate_freely },
  94. { "-renegotiate-ignore", &TestConfig::renegotiate_ignore },
  95. { "-p384-only", &TestConfig::p384_only },
  96. { "-enable-all-curves", &TestConfig::enable_all_curves },
  97. { "-use-old-client-cert-callback",
  98. &TestConfig::use_old_client_cert_callback },
  99. { "-send-alert", &TestConfig::send_alert },
  100. { "-peek-then-read", &TestConfig::peek_then_read },
  101. { "-enable-grease", &TestConfig::enable_grease },
  102. { "-use-exporter-between-reads", &TestConfig::use_exporter_between_reads },
  103. { "-retain-only-sha256-client-cert-initial",
  104. &TestConfig::retain_only_sha256_client_cert_initial },
  105. { "-retain-only-sha256-client-cert-resume",
  106. &TestConfig::retain_only_sha256_client_cert_resume },
  107. { "-expect-sha256-client-cert-initial",
  108. &TestConfig::expect_sha256_client_cert_initial },
  109. { "-expect-sha256-client-cert-resume",
  110. &TestConfig::expect_sha256_client_cert_resume },
  111. { "-read-with-unfinished-write", &TestConfig::read_with_unfinished_write },
  112. { "-expect-secure-renegotiation",
  113. &TestConfig::expect_secure_renegotiation },
  114. { "-expect-no-secure-renegotiation",
  115. &TestConfig::expect_no_secure_renegotiation },
  116. { "-expect-session-id", &TestConfig::expect_session_id },
  117. { "-expect-no-session-id", &TestConfig::expect_no_session_id },
  118. { "-expect-accept-early-data", &TestConfig::expect_accept_early_data },
  119. { "-expect-reject-early-data", &TestConfig::expect_reject_early_data },
  120. { "-no-op-extra-handshake", &TestConfig::no_op_extra_handshake },
  121. { "-handshake-twice", &TestConfig::handshake_twice },
  122. { "-allow-unknown-alpn-protos", &TestConfig::allow_unknown_alpn_protos },
  123. { "-enable-ed25519", &TestConfig::enable_ed25519 },
  124. };
  125. const Flag<std::string> kStringFlags[] = {
  126. { "-write-settings", &TestConfig::write_settings },
  127. { "-digest-prefs", &TestConfig::digest_prefs },
  128. { "-key-file", &TestConfig::key_file },
  129. { "-cert-file", &TestConfig::cert_file },
  130. { "-expect-server-name", &TestConfig::expected_server_name },
  131. { "-advertise-npn", &TestConfig::advertise_npn },
  132. { "-expect-next-proto", &TestConfig::expected_next_proto },
  133. { "-select-next-proto", &TestConfig::select_next_proto },
  134. { "-send-channel-id", &TestConfig::send_channel_id },
  135. { "-host-name", &TestConfig::host_name },
  136. { "-advertise-alpn", &TestConfig::advertise_alpn },
  137. { "-expect-alpn", &TestConfig::expected_alpn },
  138. { "-expect-late-alpn", &TestConfig::expected_late_alpn },
  139. { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
  140. { "-select-alpn", &TestConfig::select_alpn },
  141. { "-psk", &TestConfig::psk },
  142. { "-psk-identity", &TestConfig::psk_identity },
  143. { "-srtp-profiles", &TestConfig::srtp_profiles },
  144. { "-cipher", &TestConfig::cipher },
  145. { "-export-label", &TestConfig::export_label },
  146. { "-export-context", &TestConfig::export_context },
  147. { "-expect-peer-cert-file", &TestConfig::expect_peer_cert_file },
  148. { "-use-client-ca-list", &TestConfig::use_client_ca_list },
  149. { "-expect-client-ca-list", &TestConfig::expected_client_ca_list },
  150. };
  151. const Flag<std::string> kBase64Flags[] = {
  152. { "-expect-certificate-types", &TestConfig::expected_certificate_types },
  153. { "-expect-channel-id", &TestConfig::expected_channel_id },
  154. { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
  155. { "-expect-signed-cert-timestamps",
  156. &TestConfig::expected_signed_cert_timestamps },
  157. { "-ocsp-response", &TestConfig::ocsp_response },
  158. { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
  159. { "-ticket-key", &TestConfig::ticket_key },
  160. };
  161. const Flag<int> kIntFlags[] = {
  162. { "-port", &TestConfig::port },
  163. { "-resume-count", &TestConfig::resume_count },
  164. { "-min-version", &TestConfig::min_version },
  165. { "-max-version", &TestConfig::max_version },
  166. { "-mtu", &TestConfig::mtu },
  167. { "-export-keying-material", &TestConfig::export_keying_material },
  168. { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
  169. { "-expect-peer-signature-algorithm",
  170. &TestConfig::expect_peer_signature_algorithm },
  171. { "-expect-curve-id", &TestConfig::expect_curve_id },
  172. { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms },
  173. { "-max-cert-list", &TestConfig::max_cert_list },
  174. { "-expect-cipher-aes", &TestConfig::expect_cipher_aes },
  175. { "-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes },
  176. { "-resumption-delay", &TestConfig::resumption_delay },
  177. { "-max-send-fragment", &TestConfig::max_send_fragment },
  178. { "-read-size", &TestConfig::read_size },
  179. { "-expect-ticket-age-skew", &TestConfig::expect_ticket_age_skew },
  180. };
  181. const Flag<std::vector<int>> kIntVectorFlags[] = {
  182. { "-signing-prefs", &TestConfig::signing_prefs },
  183. { "-verify-prefs", &TestConfig::verify_prefs },
  184. };
  185. bool ParseFlag(char *flag, int argc, char **argv, int *i,
  186. bool skip, TestConfig *out_config) {
  187. bool *bool_field = FindField(out_config, kBoolFlags, flag);
  188. if (bool_field != NULL) {
  189. if (!skip) {
  190. *bool_field = true;
  191. }
  192. return true;
  193. }
  194. std::string *string_field = FindField(out_config, kStringFlags, flag);
  195. if (string_field != NULL) {
  196. *i = *i + 1;
  197. if (*i >= argc) {
  198. fprintf(stderr, "Missing parameter\n");
  199. return false;
  200. }
  201. if (!skip) {
  202. string_field->assign(argv[*i]);
  203. }
  204. return true;
  205. }
  206. std::string *base64_field = FindField(out_config, kBase64Flags, flag);
  207. if (base64_field != NULL) {
  208. *i = *i + 1;
  209. if (*i >= argc) {
  210. fprintf(stderr, "Missing parameter\n");
  211. return false;
  212. }
  213. size_t len;
  214. if (!EVP_DecodedLength(&len, strlen(argv[*i]))) {
  215. fprintf(stderr, "Invalid base64: %s\n", argv[*i]);
  216. return false;
  217. }
  218. std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
  219. if (!EVP_DecodeBase64(decoded.get(), &len, len,
  220. reinterpret_cast<const uint8_t *>(argv[*i]),
  221. strlen(argv[*i]))) {
  222. fprintf(stderr, "Invalid base64: %s\n", argv[*i]);
  223. return false;
  224. }
  225. if (!skip) {
  226. base64_field->assign(reinterpret_cast<const char *>(decoded.get()),
  227. len);
  228. }
  229. return true;
  230. }
  231. int *int_field = FindField(out_config, kIntFlags, flag);
  232. if (int_field) {
  233. *i = *i + 1;
  234. if (*i >= argc) {
  235. fprintf(stderr, "Missing parameter\n");
  236. return false;
  237. }
  238. if (!skip) {
  239. *int_field = atoi(argv[*i]);
  240. }
  241. return true;
  242. }
  243. std::vector<int> *int_vector_field =
  244. FindField(out_config, kIntVectorFlags, flag);
  245. if (int_vector_field) {
  246. *i = *i + 1;
  247. if (*i >= argc) {
  248. fprintf(stderr, "Missing parameter\n");
  249. return false;
  250. }
  251. // Each instance of the flag adds to the list.
  252. if (!skip) {
  253. int_vector_field->push_back(atoi(argv[*i]));
  254. }
  255. return true;
  256. }
  257. fprintf(stderr, "Unknown argument: %s\n", flag);
  258. return false;
  259. }
  260. const char kInit[] = "-on-initial";
  261. const char kResume[] = "-on-resume";
  262. const char kRetry[] = "-on-retry";
  263. } // namespace
  264. bool ParseConfig(int argc, char **argv,
  265. TestConfig *out_initial,
  266. TestConfig *out_resume,
  267. TestConfig *out_retry) {
  268. for (int i = 0; i < argc; i++) {
  269. bool skip = false;
  270. char *flag = argv[i];
  271. if (strncmp(flag, kInit, strlen(kInit)) == 0) {
  272. if (!ParseFlag(flag + strlen(kInit), argc, argv, &i, skip, out_initial)) {
  273. return false;
  274. }
  275. } else if (strncmp(flag, kResume, strlen(kResume)) == 0) {
  276. if (!ParseFlag(flag + strlen(kResume), argc, argv, &i, skip,
  277. out_resume)) {
  278. return false;
  279. }
  280. } else if (strncmp(flag, kRetry, strlen(kRetry)) == 0) {
  281. if (!ParseFlag(flag + strlen(kRetry), argc, argv, &i, skip, out_retry)) {
  282. return false;
  283. }
  284. } else {
  285. int i_init = i;
  286. int i_resume = i;
  287. if (!ParseFlag(flag, argc, argv, &i_init, skip, out_initial) ||
  288. !ParseFlag(flag, argc, argv, &i_resume, skip, out_resume) ||
  289. !ParseFlag(flag, argc, argv, &i, skip, out_retry)) {
  290. return false;
  291. }
  292. }
  293. }
  294. return true;
  295. }