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.
 
 
 
 
 
 

276 line
11 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-resume-early-data", &TestConfig::enable_resume_early_data },
  81. { "-enable-client-custom-extension",
  82. &TestConfig::enable_client_custom_extension },
  83. { "-enable-server-custom-extension",
  84. &TestConfig::enable_server_custom_extension },
  85. { "-custom-extension-skip", &TestConfig::custom_extension_skip },
  86. { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add },
  87. { "-check-close-notify", &TestConfig::check_close_notify },
  88. { "-shim-shuts-down", &TestConfig::shim_shuts_down },
  89. { "-verify-fail", &TestConfig::verify_fail },
  90. { "-verify-peer", &TestConfig::verify_peer },
  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. { "-expect-no-alpn", &TestConfig::expect_no_alpn },
  121. { "-expect-no-resume-alpn", &TestConfig::expect_no_resume_alpn },
  122. { "-no-op-extra-handshake", &TestConfig::no_op_extra_handshake },
  123. { "-handshake-twice", &TestConfig::handshake_twice },
  124. { "-allow-unknown-alpn-protos", &TestConfig::allow_unknown_alpn_protos },
  125. { "-enable-ed25519", &TestConfig::enable_ed25519 },
  126. };
  127. const Flag<std::string> kStringFlags[] = {
  128. { "-digest-prefs", &TestConfig::digest_prefs },
  129. { "-key-file", &TestConfig::key_file },
  130. { "-cert-file", &TestConfig::cert_file },
  131. { "-expect-server-name", &TestConfig::expected_server_name },
  132. { "-advertise-npn", &TestConfig::advertise_npn },
  133. { "-expect-next-proto", &TestConfig::expected_next_proto },
  134. { "-select-next-proto", &TestConfig::select_next_proto },
  135. { "-send-channel-id", &TestConfig::send_channel_id },
  136. { "-host-name", &TestConfig::host_name },
  137. { "-advertise-alpn", &TestConfig::advertise_alpn },
  138. { "-expect-alpn", &TestConfig::expected_alpn },
  139. { "-expect-resume-alpn", &TestConfig::expected_resume_alpn },
  140. { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
  141. { "-select-alpn", &TestConfig::select_alpn },
  142. { "-select-resume-alpn", &TestConfig::select_resume_alpn },
  143. { "-psk", &TestConfig::psk },
  144. { "-psk-identity", &TestConfig::psk_identity },
  145. { "-srtp-profiles", &TestConfig::srtp_profiles },
  146. { "-cipher", &TestConfig::cipher },
  147. { "-export-label", &TestConfig::export_label },
  148. { "-export-context", &TestConfig::export_context },
  149. { "-expect-peer-cert-file", &TestConfig::expect_peer_cert_file },
  150. { "-use-client-ca-list", &TestConfig::use_client_ca_list },
  151. { "-expect-client-ca-list", &TestConfig::expected_client_ca_list },
  152. };
  153. const Flag<std::string> kBase64Flags[] = {
  154. { "-expect-certificate-types", &TestConfig::expected_certificate_types },
  155. { "-expect-channel-id", &TestConfig::expected_channel_id },
  156. { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
  157. { "-expect-signed-cert-timestamps",
  158. &TestConfig::expected_signed_cert_timestamps },
  159. { "-ocsp-response", &TestConfig::ocsp_response },
  160. { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
  161. { "-ticket-key", &TestConfig::ticket_key },
  162. };
  163. const Flag<int> kIntFlags[] = {
  164. { "-port", &TestConfig::port },
  165. { "-resume-count", &TestConfig::resume_count },
  166. { "-min-version", &TestConfig::min_version },
  167. { "-max-version", &TestConfig::max_version },
  168. { "-mtu", &TestConfig::mtu },
  169. { "-export-keying-material", &TestConfig::export_keying_material },
  170. { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
  171. { "-expect-peer-signature-algorithm",
  172. &TestConfig::expect_peer_signature_algorithm },
  173. { "-expect-curve-id", &TestConfig::expect_curve_id },
  174. { "-expect-resume-curve-id", &TestConfig::expect_resume_curve_id },
  175. { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms },
  176. { "-max-cert-list", &TestConfig::max_cert_list },
  177. { "-expect-cipher-aes", &TestConfig::expect_cipher_aes },
  178. { "-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes },
  179. { "-resumption-delay", &TestConfig::resumption_delay },
  180. { "-max-send-fragment", &TestConfig::max_send_fragment },
  181. { "-read-size", &TestConfig::read_size },
  182. { "-expect-ticket-age-skew", &TestConfig::expect_ticket_age_skew },
  183. };
  184. const Flag<std::vector<int>> kIntVectorFlags[] = {
  185. { "-signing-prefs", &TestConfig::signing_prefs },
  186. { "-verify-prefs", &TestConfig::verify_prefs },
  187. };
  188. } // namespace
  189. bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
  190. for (int i = 0; i < argc; i++) {
  191. bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
  192. if (bool_field != NULL) {
  193. *bool_field = true;
  194. continue;
  195. }
  196. std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
  197. if (string_field != NULL) {
  198. i++;
  199. if (i >= argc) {
  200. fprintf(stderr, "Missing parameter\n");
  201. return false;
  202. }
  203. string_field->assign(argv[i]);
  204. continue;
  205. }
  206. std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
  207. if (base64_field != NULL) {
  208. i++;
  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. base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
  226. continue;
  227. }
  228. int *int_field = FindField(out_config, kIntFlags, argv[i]);
  229. if (int_field) {
  230. i++;
  231. if (i >= argc) {
  232. fprintf(stderr, "Missing parameter\n");
  233. return false;
  234. }
  235. *int_field = atoi(argv[i]);
  236. continue;
  237. }
  238. std::vector<int> *int_vector_field =
  239. FindField(out_config, kIntVectorFlags, argv[i]);
  240. if (int_vector_field) {
  241. i++;
  242. if (i >= argc) {
  243. fprintf(stderr, "Missing parameter\n");
  244. return false;
  245. }
  246. // Each instance of the flag adds to the list.
  247. int_vector_field->push_back(atoi(argv[i]));
  248. continue;
  249. }
  250. fprintf(stderr, "Unknown argument: %s\n", argv[i]);
  251. return false;
  252. }
  253. return true;
  254. }