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
15 #include "test_config.h"
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <memory>
22
23 #include <openssl/base64.h>
24
25 namespace {
26
27 template <typename T>
28 struct Flag {
29 const char *flag;
30 T TestConfig::*member;
31 };
32
33 // FindField looks for the flag in |flags| that matches |flag|. If one is found,
34 // it returns a pointer to the corresponding field in |config|. Otherwise, it
35 // returns NULL.
36 template<typename T, size_t N>
FindField(TestConfig * config,const Flag<T> (& flags)[N],const char * flag)37 T *FindField(TestConfig *config, const Flag<T> (&flags)[N], const char *flag) {
38 for (size_t i = 0; i < N; i++) {
39 if (strcmp(flag, flags[i].flag) == 0) {
40 return &(config->*(flags[i].member));
41 }
42 }
43 return NULL;
44 }
45
46 const Flag<bool> kBoolFlags[] = {
47 { "-server", &TestConfig::is_server },
48 { "-dtls", &TestConfig::is_dtls },
49 { "-fallback-scsv", &TestConfig::fallback_scsv },
50 { "-require-any-client-certificate",
51 &TestConfig::require_any_client_certificate },
52 { "-false-start", &TestConfig::false_start },
53 { "-async", &TestConfig::async },
54 { "-write-different-record-sizes",
55 &TestConfig::write_different_record_sizes },
56 { "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
57 { "-partial-write", &TestConfig::partial_write },
58 { "-no-tls13", &TestConfig::no_tls13 },
59 { "-no-tls12", &TestConfig::no_tls12 },
60 { "-no-tls11", &TestConfig::no_tls11 },
61 { "-no-tls1", &TestConfig::no_tls1 },
62 { "-no-ssl3", &TestConfig::no_ssl3 },
63 { "-enable-channel-id", &TestConfig::enable_channel_id },
64 { "-shim-writes-first", &TestConfig::shim_writes_first },
65 { "-expect-session-miss", &TestConfig::expect_session_miss },
66 { "-decline-alpn", &TestConfig::decline_alpn },
67 { "-expect-extended-master-secret",
68 &TestConfig::expect_extended_master_secret },
69 { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
70 { "-enable-signed-cert-timestamps",
71 &TestConfig::enable_signed_cert_timestamps },
72 { "-implicit-handshake", &TestConfig::implicit_handshake },
73 { "-use-early-callback", &TestConfig::use_early_callback },
74 { "-fail-early-callback", &TestConfig::fail_early_callback },
75 { "-install-ddos-callback", &TestConfig::install_ddos_callback },
76 { "-fail-ddos-callback", &TestConfig::fail_ddos_callback },
77 { "-fail-second-ddos-callback", &TestConfig::fail_second_ddos_callback },
78 { "-fail-cert-callback", &TestConfig::fail_cert_callback },
79 { "-handshake-never-done", &TestConfig::handshake_never_done },
80 { "-use-export-context", &TestConfig::use_export_context },
81 { "-tls-unique", &TestConfig::tls_unique },
82 { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal },
83 { "-expect-no-session", &TestConfig::expect_no_session },
84 { "-expect-ticket-supports-early-data",
85 &TestConfig::expect_ticket_supports_early_data },
86 { "-use-ticket-callback", &TestConfig::use_ticket_callback },
87 { "-renew-ticket", &TestConfig::renew_ticket },
88 { "-enable-early-data", &TestConfig::enable_early_data },
89 { "-enable-client-custom-extension",
90 &TestConfig::enable_client_custom_extension },
91 { "-enable-server-custom-extension",
92 &TestConfig::enable_server_custom_extension },
93 { "-custom-extension-skip", &TestConfig::custom_extension_skip },
94 { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add },
95 { "-check-close-notify", &TestConfig::check_close_notify },
96 { "-shim-shuts-down", &TestConfig::shim_shuts_down },
97 { "-verify-fail", &TestConfig::verify_fail },
98 { "-verify-peer", &TestConfig::verify_peer },
99 { "-verify-peer-if-no-obc", &TestConfig::verify_peer_if_no_obc },
100 { "-expect-verify-result", &TestConfig::expect_verify_result },
101 { "-renegotiate-once", &TestConfig::renegotiate_once },
102 { "-renegotiate-freely", &TestConfig::renegotiate_freely },
103 { "-renegotiate-ignore", &TestConfig::renegotiate_ignore },
104 { "-p384-only", &TestConfig::p384_only },
105 { "-enable-all-curves", &TestConfig::enable_all_curves },
106 { "-use-old-client-cert-callback",
107 &TestConfig::use_old_client_cert_callback },
108 { "-send-alert", &TestConfig::send_alert },
109 { "-peek-then-read", &TestConfig::peek_then_read },
110 { "-enable-grease", &TestConfig::enable_grease },
111 { "-use-exporter-between-reads", &TestConfig::use_exporter_between_reads },
112 { "-retain-only-sha256-client-cert",
113 &TestConfig::retain_only_sha256_client_cert },
114 { "-expect-sha256-client-cert",
115 &TestConfig::expect_sha256_client_cert },
116 { "-read-with-unfinished-write", &TestConfig::read_with_unfinished_write },
117 { "-expect-secure-renegotiation",
118 &TestConfig::expect_secure_renegotiation },
119 { "-expect-no-secure-renegotiation",
120 &TestConfig::expect_no_secure_renegotiation },
121 { "-expect-session-id", &TestConfig::expect_session_id },
122 { "-expect-no-session-id", &TestConfig::expect_no_session_id },
123 { "-expect-accept-early-data", &TestConfig::expect_accept_early_data },
124 { "-expect-reject-early-data", &TestConfig::expect_reject_early_data },
125 { "-expect-no-offer-early-data", &TestConfig::expect_no_offer_early_data },
126 { "-no-op-extra-handshake", &TestConfig::no_op_extra_handshake },
127 { "-handshake-twice", &TestConfig::handshake_twice },
128 { "-allow-unknown-alpn-protos", &TestConfig::allow_unknown_alpn_protos },
129 { "-enable-ed25519", &TestConfig::enable_ed25519 },
130 { "-use-custom-verify-callback", &TestConfig::use_custom_verify_callback },
131 { "-allow-false-start-without-alpn",
132 &TestConfig::allow_false_start_without_alpn },
133 { "-expect-draft-downgrade", &TestConfig::expect_draft_downgrade },
134 { "-handoff", &TestConfig::handoff },
135 };
136
137 const Flag<std::string> kStringFlags[] = {
138 { "-write-settings", &TestConfig::write_settings },
139 { "-key-file", &TestConfig::key_file },
140 { "-cert-file", &TestConfig::cert_file },
141 { "-expect-server-name", &TestConfig::expected_server_name },
142 { "-advertise-npn", &TestConfig::advertise_npn },
143 { "-expect-next-proto", &TestConfig::expected_next_proto },
144 { "-select-next-proto", &TestConfig::select_next_proto },
145 { "-send-channel-id", &TestConfig::send_channel_id },
146 { "-host-name", &TestConfig::host_name },
147 { "-advertise-alpn", &TestConfig::advertise_alpn },
148 { "-expect-alpn", &TestConfig::expected_alpn },
149 { "-expect-late-alpn", &TestConfig::expected_late_alpn },
150 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
151 { "-select-alpn", &TestConfig::select_alpn },
152 { "-psk", &TestConfig::psk },
153 { "-psk-identity", &TestConfig::psk_identity },
154 { "-srtp-profiles", &TestConfig::srtp_profiles },
155 { "-cipher", &TestConfig::cipher },
156 { "-export-label", &TestConfig::export_label },
157 { "-export-context", &TestConfig::export_context },
158 { "-expect-peer-cert-file", &TestConfig::expect_peer_cert_file },
159 { "-use-client-ca-list", &TestConfig::use_client_ca_list },
160 { "-expect-client-ca-list", &TestConfig::expected_client_ca_list },
161 { "-expect-msg-callback", &TestConfig::expect_msg_callback },
162 };
163
164 const Flag<std::string> kBase64Flags[] = {
165 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
166 { "-expect-channel-id", &TestConfig::expected_channel_id },
167 { "-token-binding-params", &TestConfig::send_token_binding_params },
168 { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
169 { "-expect-signed-cert-timestamps",
170 &TestConfig::expected_signed_cert_timestamps },
171 { "-ocsp-response", &TestConfig::ocsp_response },
172 { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
173 { "-ticket-key", &TestConfig::ticket_key },
174 { "-quic-transport-params", &TestConfig::quic_transport_params },
175 { "-expected-quic-transport-params",
176 &TestConfig::expected_quic_transport_params },
177 };
178
179 const Flag<int> kIntFlags[] = {
180 { "-port", &TestConfig::port },
181 { "-resume-count", &TestConfig::resume_count },
182 { "-expected-token-binding-param",
183 &TestConfig::expected_token_binding_param },
184 { "-min-version", &TestConfig::min_version },
185 { "-max-version", &TestConfig::max_version },
186 { "-expect-version", &TestConfig::expect_version },
187 { "-mtu", &TestConfig::mtu },
188 { "-export-early-keying-material",
189 &TestConfig::export_early_keying_material },
190 { "-export-keying-material", &TestConfig::export_keying_material },
191 { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
192 { "-expect-peer-signature-algorithm",
193 &TestConfig::expect_peer_signature_algorithm },
194 { "-expect-curve-id", &TestConfig::expect_curve_id },
195 { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms },
196 { "-max-cert-list", &TestConfig::max_cert_list },
197 { "-expect-cipher-aes", &TestConfig::expect_cipher_aes },
198 { "-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes },
199 { "-resumption-delay", &TestConfig::resumption_delay },
200 { "-max-send-fragment", &TestConfig::max_send_fragment },
201 { "-read-size", &TestConfig::read_size },
202 { "-expect-ticket-age-skew", &TestConfig::expect_ticket_age_skew },
203 { "-tls13-variant", &TestConfig::tls13_variant },
204 { "-dummy-pq-padding-len", &TestConfig::dummy_pq_padding_len },
205 };
206
207 const Flag<std::vector<int>> kIntVectorFlags[] = {
208 { "-signing-prefs", &TestConfig::signing_prefs },
209 { "-verify-prefs", &TestConfig::verify_prefs },
210 };
211
ParseFlag(char * flag,int argc,char ** argv,int * i,bool skip,TestConfig * out_config)212 bool ParseFlag(char *flag, int argc, char **argv, int *i,
213 bool skip, TestConfig *out_config) {
214 bool *bool_field = FindField(out_config, kBoolFlags, flag);
215 if (bool_field != NULL) {
216 if (!skip) {
217 *bool_field = true;
218 }
219 return true;
220 }
221
222 std::string *string_field = FindField(out_config, kStringFlags, flag);
223 if (string_field != NULL) {
224 *i = *i + 1;
225 if (*i >= argc) {
226 fprintf(stderr, "Missing parameter\n");
227 return false;
228 }
229 if (!skip) {
230 string_field->assign(argv[*i]);
231 }
232 return true;
233 }
234
235 std::string *base64_field = FindField(out_config, kBase64Flags, flag);
236 if (base64_field != NULL) {
237 *i = *i + 1;
238 if (*i >= argc) {
239 fprintf(stderr, "Missing parameter\n");
240 return false;
241 }
242 size_t len;
243 if (!EVP_DecodedLength(&len, strlen(argv[*i]))) {
244 fprintf(stderr, "Invalid base64: %s\n", argv[*i]);
245 return false;
246 }
247 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
248 if (!EVP_DecodeBase64(decoded.get(), &len, len,
249 reinterpret_cast<const uint8_t *>(argv[*i]),
250 strlen(argv[*i]))) {
251 fprintf(stderr, "Invalid base64: %s\n", argv[*i]);
252 return false;
253 }
254 if (!skip) {
255 base64_field->assign(reinterpret_cast<const char *>(decoded.get()),
256 len);
257 }
258 return true;
259 }
260
261 int *int_field = FindField(out_config, kIntFlags, flag);
262 if (int_field) {
263 *i = *i + 1;
264 if (*i >= argc) {
265 fprintf(stderr, "Missing parameter\n");
266 return false;
267 }
268 if (!skip) {
269 *int_field = atoi(argv[*i]);
270 }
271 return true;
272 }
273
274 std::vector<int> *int_vector_field =
275 FindField(out_config, kIntVectorFlags, flag);
276 if (int_vector_field) {
277 *i = *i + 1;
278 if (*i >= argc) {
279 fprintf(stderr, "Missing parameter\n");
280 return false;
281 }
282
283 // Each instance of the flag adds to the list.
284 if (!skip) {
285 int_vector_field->push_back(atoi(argv[*i]));
286 }
287 return true;
288 }
289
290 fprintf(stderr, "Unknown argument: %s\n", flag);
291 return false;
292 }
293
294 const char kInit[] = "-on-initial";
295 const char kResume[] = "-on-resume";
296 const char kRetry[] = "-on-retry";
297
298 } // namespace
299
ParseConfig(int argc,char ** argv,TestConfig * out_initial,TestConfig * out_resume,TestConfig * out_retry)300 bool ParseConfig(int argc, char **argv,
301 TestConfig *out_initial,
302 TestConfig *out_resume,
303 TestConfig *out_retry) {
304 for (int i = 0; i < argc; i++) {
305 bool skip = false;
306 char *flag = argv[i];
307 if (strncmp(flag, kInit, strlen(kInit)) == 0) {
308 if (!ParseFlag(flag + strlen(kInit), argc, argv, &i, skip, out_initial)) {
309 return false;
310 }
311 } else if (strncmp(flag, kResume, strlen(kResume)) == 0) {
312 if (!ParseFlag(flag + strlen(kResume), argc, argv, &i, skip,
313 out_resume)) {
314 return false;
315 }
316 } else if (strncmp(flag, kRetry, strlen(kRetry)) == 0) {
317 if (!ParseFlag(flag + strlen(kRetry), argc, argv, &i, skip, out_retry)) {
318 return false;
319 }
320 } else {
321 int i_init = i;
322 int i_resume = i;
323 if (!ParseFlag(flag, argc, argv, &i_init, skip, out_initial) ||
324 !ParseFlag(flag, argc, argv, &i_resume, skip, out_resume) ||
325 !ParseFlag(flag, argc, argv, &i, skip, out_retry)) {
326 return false;
327 }
328 }
329 }
330
331 return true;
332 }
333