84f290d2f15235dcc1980e67c43a98e4ebb6ddda
[openssl.git] / test / ossl_shim / test_config.cc
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/evp.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>
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   { "-handshake-never-done", &TestConfig::handshake_never_done },
79   { "-use-export-context", &TestConfig::use_export_context },
80   { "-tls-unique", &TestConfig::tls_unique },
81   { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal },
82   { "-expect-no-session", &TestConfig::expect_no_session },
83   { "-use-ticket-callback", &TestConfig::use_ticket_callback },
84   { "-renew-ticket", &TestConfig::renew_ticket },
85   { "-enable-client-custom-extension",
86     &TestConfig::enable_client_custom_extension },
87   { "-enable-server-custom-extension",
88     &TestConfig::enable_server_custom_extension },
89   { "-custom-extension-skip", &TestConfig::custom_extension_skip },
90   { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add },
91   { "-check-close-notify", &TestConfig::check_close_notify },
92   { "-shim-shuts-down", &TestConfig::shim_shuts_down },
93   { "-verify-fail", &TestConfig::verify_fail },
94   { "-verify-peer", &TestConfig::verify_peer },
95   { "-expect-verify-result", &TestConfig::expect_verify_result },
96   { "-renegotiate-once", &TestConfig::renegotiate_once },
97   { "-renegotiate-freely", &TestConfig::renegotiate_freely },
98   { "-renegotiate-ignore", &TestConfig::renegotiate_ignore },
99   { "-disable-npn", &TestConfig::disable_npn },
100   { "-p384-only", &TestConfig::p384_only },
101   { "-enable-all-curves", &TestConfig::enable_all_curves },
102   { "-use-sparse-dh-prime", &TestConfig::use_sparse_dh_prime },
103   { "-use-old-client-cert-callback",
104     &TestConfig::use_old_client_cert_callback },
105   { "-use-null-client-ca-list", &TestConfig::use_null_client_ca_list },
106   { "-send-alert", &TestConfig::send_alert },
107   { "-peek-then-read", &TestConfig::peek_then_read },
108   { "-enable-grease", &TestConfig::enable_grease },
109 };
110
111 const Flag<std::string> kStringFlags[] = {
112   { "-digest-prefs", &TestConfig::digest_prefs },
113   { "-key-file", &TestConfig::key_file },
114   { "-cert-file", &TestConfig::cert_file },
115   { "-expect-server-name", &TestConfig::expected_server_name },
116   { "-advertise-npn", &TestConfig::advertise_npn },
117   { "-expect-next-proto", &TestConfig::expected_next_proto },
118   { "-select-next-proto", &TestConfig::select_next_proto },
119   { "-send-channel-id", &TestConfig::send_channel_id },
120   { "-host-name", &TestConfig::host_name },
121   { "-advertise-alpn", &TestConfig::advertise_alpn },
122   { "-expect-alpn", &TestConfig::expected_alpn },
123   { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
124   { "-select-alpn", &TestConfig::select_alpn },
125   { "-psk", &TestConfig::psk },
126   { "-psk-identity", &TestConfig::psk_identity },
127   { "-srtp-profiles", &TestConfig::srtp_profiles },
128   { "-cipher", &TestConfig::cipher },
129   { "-cipher-tls10", &TestConfig::cipher_tls10 },
130   { "-cipher-tls11", &TestConfig::cipher_tls11 },
131   { "-export-label", &TestConfig::export_label },
132   { "-export-context", &TestConfig::export_context },
133 };
134
135 const Flag<std::string> kBase64Flags[] = {
136   { "-expect-certificate-types", &TestConfig::expected_certificate_types },
137   { "-expect-channel-id", &TestConfig::expected_channel_id },
138   { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
139   { "-expect-signed-cert-timestamps",
140     &TestConfig::expected_signed_cert_timestamps },
141   { "-ocsp-response", &TestConfig::ocsp_response },
142   { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
143 };
144
145 const Flag<int> kIntFlags[] = {
146   { "-port", &TestConfig::port },
147   { "-resume-count", &TestConfig::resume_count },
148   { "-min-version", &TestConfig::min_version },
149   { "-max-version", &TestConfig::max_version },
150   { "-mtu", &TestConfig::mtu },
151   { "-export-keying-material", &TestConfig::export_keying_material },
152   { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
153   { "-expect-peer-signature-algorithm",
154     &TestConfig::expect_peer_signature_algorithm },
155   { "-expect-curve-id", &TestConfig::expect_curve_id },
156   { "-expect-dhe-group-size", &TestConfig::expect_dhe_group_size },
157   { "-initial-timeout-duration-ms", &TestConfig::initial_timeout_duration_ms },
158   { "-max-cert-list", &TestConfig::max_cert_list },
159 };
160
161 const Flag<std::vector<int>> kIntVectorFlags[] = {
162   { "-signing-prefs", &TestConfig::signing_prefs },
163 };
164
165 }  // namespace
166
167 bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
168   for (int i = 0; i < argc; i++) {
169     bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
170     if (bool_field != NULL) {
171       *bool_field = true;
172       continue;
173     }
174
175     std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
176     if (string_field != NULL) {
177       i++;
178       if (i >= argc) {
179         fprintf(stderr, "Missing parameter\n");
180         return false;
181       }
182       string_field->assign(argv[i]);
183       continue;
184     }
185
186     std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
187     if (base64_field != NULL) {
188       i++;
189       if (i >= argc) {
190         fprintf(stderr, "Missing parameter\n");
191         return false;
192       }
193       std::unique_ptr<uint8_t[]> decoded(new uint8_t[strlen(argv[i])]);
194       int len = EVP_DecodeBlock(decoded.get(),
195                                 reinterpret_cast<const uint8_t *>(argv[i]),
196                                 strlen(argv[i]));
197       if (len < 0) {
198         fprintf(stderr, "Invalid base64: %s\n", argv[i]);
199         return false;
200       }
201       base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
202       continue;
203     }
204
205     int *int_field = FindField(out_config, kIntFlags, argv[i]);
206     if (int_field) {
207       i++;
208       if (i >= argc) {
209         fprintf(stderr, "Missing parameter\n");
210         return false;
211       }
212       *int_field = atoi(argv[i]);
213       continue;
214     }
215
216     std::vector<int> *int_vector_field =
217         FindField(out_config, kIntVectorFlags, argv[i]);
218     if (int_vector_field) {
219       i++;
220       if (i >= argc) {
221         fprintf(stderr, "Missing parameter\n");
222         return false;
223       }
224
225       // Each instance of the flag adds to the list.
226       int_vector_field->push_back(atoi(argv[i]));
227       continue;
228     }
229
230     fprintf(stderr, "Unknown argument: %s\n", argv[i]);
231     return false;
232   }
233
234   return true;
235 }