• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-early-data-info", &TestConfig::expect_early_data_info },
85   { "-use-ticket-callback", &TestConfig::use_ticket_callback },
86   { "-renew-ticket", &TestConfig::renew_ticket },
87   { "-enable-early-data", &TestConfig::enable_early_data },
88   { "-enable-resume-early-data", &TestConfig::enable_resume_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   { "-expect-verify-result", &TestConfig::expect_verify_result },
100   { "-renegotiate-once", &TestConfig::renegotiate_once },
101   { "-renegotiate-freely", &TestConfig::renegotiate_freely },
102   { "-renegotiate-ignore", &TestConfig::renegotiate_ignore },
103   { "-p384-only", &TestConfig::p384_only },
104   { "-enable-all-curves", &TestConfig::enable_all_curves },
105   { "-use-sparse-dh-prime", &TestConfig::use_sparse_dh_prime },
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-initial",
113     &TestConfig::retain_only_sha256_client_cert_initial },
114   { "-retain-only-sha256-client-cert-resume",
115     &TestConfig::retain_only_sha256_client_cert_resume },
116   { "-expect-sha256-client-cert-initial",
117     &TestConfig::expect_sha256_client_cert_initial },
118   { "-expect-sha256-client-cert-resume",
119     &TestConfig::expect_sha256_client_cert_resume },
120   { "-read-with-unfinished-write", &TestConfig::read_with_unfinished_write },
121   { "-expect-secure-renegotiation",
122     &TestConfig::expect_secure_renegotiation },
123   { "-expect-no-secure-renegotiation",
124     &TestConfig::expect_no_secure_renegotiation },
125   { "-expect-session-id", &TestConfig::expect_session_id },
126   { "-expect-no-session-id", &TestConfig::expect_no_session_id },
127   { "-expect-accept-early-data", &TestConfig::expect_accept_early_data },
128   { "-expect-reject-early-data", &TestConfig::expect_reject_early_data },
129   { "-expect-no-alpn", &TestConfig::expect_no_alpn },
130   { "-expect-no-resume-alpn", &TestConfig::expect_no_resume_alpn },
131 };
132 
133 const Flag<std::string> kStringFlags[] = {
134   { "-digest-prefs", &TestConfig::digest_prefs },
135   { "-key-file", &TestConfig::key_file },
136   { "-cert-file", &TestConfig::cert_file },
137   { "-expect-server-name", &TestConfig::expected_server_name },
138   { "-advertise-npn", &TestConfig::advertise_npn },
139   { "-expect-next-proto", &TestConfig::expected_next_proto },
140   { "-select-next-proto", &TestConfig::select_next_proto },
141   { "-send-channel-id", &TestConfig::send_channel_id },
142   { "-host-name", &TestConfig::host_name },
143   { "-advertise-alpn", &TestConfig::advertise_alpn },
144   { "-expect-alpn", &TestConfig::expected_alpn },
145   { "-expect-resume-alpn", &TestConfig::expected_resume_alpn },
146   { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
147   { "-select-alpn", &TestConfig::select_alpn },
148   { "-select-resume-alpn", &TestConfig::select_resume_alpn },
149   { "-psk", &TestConfig::psk },
150   { "-psk-identity", &TestConfig::psk_identity },
151   { "-srtp-profiles", &TestConfig::srtp_profiles },
152   { "-cipher", &TestConfig::cipher },
153   { "-export-label", &TestConfig::export_label },
154   { "-export-context", &TestConfig::export_context },
155   { "-expect-peer-cert-file", &TestConfig::expect_peer_cert_file },
156   { "-use-client-ca-list", &TestConfig::use_client_ca_list },
157   { "-expect-client-ca-list", &TestConfig::expected_client_ca_list },
158 };
159 
160 const Flag<std::string> kBase64Flags[] = {
161   { "-expect-certificate-types", &TestConfig::expected_certificate_types },
162   { "-expect-channel-id", &TestConfig::expected_channel_id },
163   { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
164   { "-expect-signed-cert-timestamps",
165     &TestConfig::expected_signed_cert_timestamps },
166   { "-ocsp-response", &TestConfig::ocsp_response },
167   { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
168   { "-ticket-key", &TestConfig::ticket_key },
169 };
170 
171 const Flag<int> kIntFlags[] = {
172   { "-port", &TestConfig::port },
173   { "-resume-count", &TestConfig::resume_count },
174   { "-min-version", &TestConfig::min_version },
175   { "-max-version", &TestConfig::max_version },
176   { "-mtu", &TestConfig::mtu },
177   { "-export-keying-material", &TestConfig::export_keying_material },
178   { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
179   { "-expect-peer-signature-algorithm",
180     &TestConfig::expect_peer_signature_algorithm },
181   { "-expect-curve-id", &TestConfig::expect_curve_id },
182   { "-expect-resume-curve-id", &TestConfig::expect_resume_curve_id },
183   { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms },
184   { "-max-cert-list", &TestConfig::max_cert_list },
185   { "-expect-cipher-aes", &TestConfig::expect_cipher_aes },
186   { "-expect-cipher-no-aes", &TestConfig::expect_cipher_no_aes },
187   { "-resumption-delay", &TestConfig::resumption_delay },
188   { "-max-send-fragment", &TestConfig::max_send_fragment },
189   { "-read-size", &TestConfig::read_size },
190   { "-expect-ticket-age-skew", &TestConfig::expect_ticket_age_skew },
191 };
192 
193 const Flag<std::vector<int>> kIntVectorFlags[] = {
194   { "-signing-prefs", &TestConfig::signing_prefs },
195 };
196 
197 }  // namespace
198 
ParseConfig(int argc,char ** argv,TestConfig * out_config)199 bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
200   for (int i = 0; i < argc; i++) {
201     bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
202     if (bool_field != NULL) {
203       *bool_field = true;
204       continue;
205     }
206 
207     std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
208     if (string_field != NULL) {
209       i++;
210       if (i >= argc) {
211         fprintf(stderr, "Missing parameter\n");
212         return false;
213       }
214       string_field->assign(argv[i]);
215       continue;
216     }
217 
218     std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
219     if (base64_field != NULL) {
220       i++;
221       if (i >= argc) {
222         fprintf(stderr, "Missing parameter\n");
223         return false;
224       }
225       size_t len;
226       if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
227         fprintf(stderr, "Invalid base64: %s\n", argv[i]);
228         return false;
229       }
230       std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
231       if (!EVP_DecodeBase64(decoded.get(), &len, len,
232                             reinterpret_cast<const uint8_t *>(argv[i]),
233                             strlen(argv[i]))) {
234         fprintf(stderr, "Invalid base64: %s\n", argv[i]);
235         return false;
236       }
237       base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
238       continue;
239     }
240 
241     int *int_field = FindField(out_config, kIntFlags, argv[i]);
242     if (int_field) {
243       i++;
244       if (i >= argc) {
245         fprintf(stderr, "Missing parameter\n");
246         return false;
247       }
248       *int_field = atoi(argv[i]);
249       continue;
250     }
251 
252     std::vector<int> *int_vector_field =
253         FindField(out_config, kIntVectorFlags, argv[i]);
254     if (int_vector_field) {
255       i++;
256       if (i >= argc) {
257         fprintf(stderr, "Missing parameter\n");
258         return false;
259       }
260 
261       // Each instance of the flag adds to the list.
262       int_vector_field->push_back(atoi(argv[i]));
263       continue;
264     }
265 
266     fprintf(stderr, "Unknown argument: %s\n", argv[i]);
267     return false;
268   }
269 
270   return true;
271 }
272