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 { "-resume", &TestConfig::resume },
50 { "-fallback-scsv", &TestConfig::fallback_scsv },
51 { "-require-any-client-certificate",
52 &TestConfig::require_any_client_certificate },
53 { "-false-start", &TestConfig::false_start },
54 { "-async", &TestConfig::async },
55 { "-write-different-record-sizes",
56 &TestConfig::write_different_record_sizes },
57 { "-cbc-record-splitting", &TestConfig::cbc_record_splitting },
58 { "-partial-write", &TestConfig::partial_write },
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 { "-shim-writes-first", &TestConfig::shim_writes_first },
64 { "-tls-d5-bug", &TestConfig::tls_d5_bug },
65 { "-expect-session-miss", &TestConfig::expect_session_miss },
66 { "-expect-extended-master-secret",
67 &TestConfig::expect_extended_master_secret },
68 { "-allow-unsafe-legacy-renegotiation",
69 &TestConfig::allow_unsafe_legacy_renegotiation },
70 { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
71 { "-enable-signed-cert-timestamps",
72 &TestConfig::enable_signed_cert_timestamps },
73 { "-fastradio-padding", &TestConfig::fastradio_padding },
74 { "-implicit-handshake", &TestConfig::implicit_handshake },
75 { "-use-early-callback", &TestConfig::use_early_callback },
76 { "-fail-early-callback", &TestConfig::fail_early_callback },
77 { "-install-ddos-callback", &TestConfig::install_ddos_callback },
78 { "-fail-ddos-callback", &TestConfig::fail_ddos_callback },
79 { "-fail-second-ddos-callback", &TestConfig::fail_second_ddos_callback },
80 { "-handshake-never-done", &TestConfig::handshake_never_done },
81 { "-use-export-context", &TestConfig::use_export_context },
82 { "-reject-peer-renegotiations", &TestConfig::reject_peer_renegotiations },
83 { "-no-legacy-server-connect", &TestConfig::no_legacy_server_connect },
84 { "-tls-unique", &TestConfig::tls_unique },
85 };
86
87 const Flag<std::string> kStringFlags[] = {
88 { "-key-file", &TestConfig::key_file },
89 { "-cert-file", &TestConfig::cert_file },
90 { "-expect-server-name", &TestConfig::expected_server_name },
91 { "-advertise-npn", &TestConfig::advertise_npn },
92 { "-expect-next-proto", &TestConfig::expected_next_proto },
93 { "-select-next-proto", &TestConfig::select_next_proto },
94 { "-send-channel-id", &TestConfig::send_channel_id },
95 { "-host-name", &TestConfig::host_name },
96 { "-advertise-alpn", &TestConfig::advertise_alpn },
97 { "-expect-alpn", &TestConfig::expected_alpn },
98 { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
99 { "-select-alpn", &TestConfig::select_alpn },
100 { "-psk", &TestConfig::psk },
101 { "-psk-identity", &TestConfig::psk_identity },
102 { "-srtp-profiles", &TestConfig::srtp_profiles },
103 { "-cipher", &TestConfig::cipher },
104 { "-export-label", &TestConfig::export_label },
105 { "-export-context", &TestConfig::export_context },
106 };
107
108 const Flag<std::string> kBase64Flags[] = {
109 { "-expect-certificate-types", &TestConfig::expected_certificate_types },
110 { "-expect-channel-id", &TestConfig::expected_channel_id },
111 { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
112 { "-expect-signed-cert-timestamps",
113 &TestConfig::expected_signed_cert_timestamps },
114 };
115
116 const Flag<int> kIntFlags[] = {
117 { "-port", &TestConfig::port },
118 { "-min-version", &TestConfig::min_version },
119 { "-max-version", &TestConfig::max_version },
120 { "-mtu", &TestConfig::mtu },
121 { "-export-keying-material", &TestConfig::export_keying_material },
122 };
123
124 } // namespace
125
ParseConfig(int argc,char ** argv,TestConfig * out_config)126 bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
127 for (int i = 0; i < argc; i++) {
128 bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
129 if (bool_field != NULL) {
130 *bool_field = true;
131 continue;
132 }
133
134 std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
135 if (string_field != NULL) {
136 i++;
137 if (i >= argc) {
138 fprintf(stderr, "Missing parameter\n");
139 return false;
140 }
141 string_field->assign(argv[i]);
142 continue;
143 }
144
145 std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
146 if (base64_field != NULL) {
147 i++;
148 if (i >= argc) {
149 fprintf(stderr, "Missing parameter\n");
150 return false;
151 }
152 size_t len;
153 if (!EVP_DecodedLength(&len, strlen(argv[i]))) {
154 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
155 }
156 std::unique_ptr<uint8_t[]> decoded(new uint8_t[len]);
157 if (!EVP_DecodeBase64(decoded.get(), &len, len,
158 reinterpret_cast<const uint8_t *>(argv[i]),
159 strlen(argv[i]))) {
160 fprintf(stderr, "Invalid base64: %s\n", argv[i]);
161 }
162 base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
163 continue;
164 }
165
166 int *int_field = FindField(out_config, kIntFlags, argv[i]);
167 if (int_field) {
168 i++;
169 if (i >= argc) {
170 fprintf(stderr, "Missing parameter\n");
171 return false;
172 }
173 *int_field = atoi(argv[i]);
174 continue;
175 }
176
177 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
178 return false;
179 }
180
181 return true;
182 }
183