75b03f254fd66efb64d1a52f3f3bceb8a34921ae
[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   { "-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   { "-expect-session-miss", &TestConfig::expect_session_miss },
65   { "-expect-extended-master-secret",
66     &TestConfig::expect_extended_master_secret },
67   { "-enable-ocsp-stapling", &TestConfig::enable_ocsp_stapling },
68   { "-enable-signed-cert-timestamps",
69     &TestConfig::enable_signed_cert_timestamps },
70   { "-implicit-handshake", &TestConfig::implicit_handshake },
71   { "-use-early-callback", &TestConfig::use_early_callback },
72   { "-fail-early-callback", &TestConfig::fail_early_callback },
73   { "-install-ddos-callback", &TestConfig::install_ddos_callback },
74   { "-fail-ddos-callback", &TestConfig::fail_ddos_callback },
75   { "-fail-second-ddos-callback", &TestConfig::fail_second_ddos_callback },
76   { "-handshake-never-done", &TestConfig::handshake_never_done },
77   { "-use-export-context", &TestConfig::use_export_context },
78   { "-tls-unique", &TestConfig::tls_unique },
79   { "-expect-ticket-renewal", &TestConfig::expect_ticket_renewal },
80   { "-expect-no-session", &TestConfig::expect_no_session },
81   { "-use-ticket-callback", &TestConfig::use_ticket_callback },
82   { "-renew-ticket", &TestConfig::renew_ticket },
83   { "-enable-client-custom-extension",
84     &TestConfig::enable_client_custom_extension },
85   { "-enable-server-custom-extension",
86     &TestConfig::enable_server_custom_extension },
87   { "-custom-extension-skip", &TestConfig::custom_extension_skip },
88   { "-custom-extension-fail-add", &TestConfig::custom_extension_fail_add },
89   { "-check-close-notify", &TestConfig::check_close_notify },
90   { "-shim-shuts-down", &TestConfig::shim_shuts_down },
91   { "-verify-fail", &TestConfig::verify_fail },
92   { "-verify-peer", &TestConfig::verify_peer },
93   { "-expect-verify-result", &TestConfig::expect_verify_result },
94   { "-renegotiate-once", &TestConfig::renegotiate_once },
95   { "-renegotiate-freely", &TestConfig::renegotiate_freely },
96   { "-renegotiate-ignore", &TestConfig::renegotiate_ignore },
97   { "-disable-npn", &TestConfig::disable_npn },
98   { "-p384-only", &TestConfig::p384_only },
99   { "-enable-all-curves", &TestConfig::enable_all_curves },
100   { "-use-sparse-dh-prime", &TestConfig::use_sparse_dh_prime },
101   { "-use-old-client-cert-callback",
102     &TestConfig::use_old_client_cert_callback },
103 };
104
105 const Flag<std::string> kStringFlags[] = {
106   { "-digest-prefs", &TestConfig::digest_prefs },
107   { "-key-file", &TestConfig::key_file },
108   { "-cert-file", &TestConfig::cert_file },
109   { "-expect-server-name", &TestConfig::expected_server_name },
110   { "-advertise-npn", &TestConfig::advertise_npn },
111   { "-expect-next-proto", &TestConfig::expected_next_proto },
112   { "-select-next-proto", &TestConfig::select_next_proto },
113   { "-send-channel-id", &TestConfig::send_channel_id },
114   { "-host-name", &TestConfig::host_name },
115   { "-advertise-alpn", &TestConfig::advertise_alpn },
116   { "-expect-alpn", &TestConfig::expected_alpn },
117   { "-expect-advertised-alpn", &TestConfig::expected_advertised_alpn },
118   { "-select-alpn", &TestConfig::select_alpn },
119   { "-psk", &TestConfig::psk },
120   { "-psk-identity", &TestConfig::psk_identity },
121   { "-srtp-profiles", &TestConfig::srtp_profiles },
122   { "-cipher", &TestConfig::cipher },
123   { "-cipher-tls10", &TestConfig::cipher_tls10 },
124   { "-cipher-tls11", &TestConfig::cipher_tls11 },
125   { "-export-label", &TestConfig::export_label },
126   { "-export-context", &TestConfig::export_context },
127 };
128
129 const Flag<std::string> kBase64Flags[] = {
130   { "-expect-certificate-types", &TestConfig::expected_certificate_types },
131   { "-expect-channel-id", &TestConfig::expected_channel_id },
132   { "-expect-ocsp-response", &TestConfig::expected_ocsp_response },
133   { "-expect-signed-cert-timestamps",
134     &TestConfig::expected_signed_cert_timestamps },
135   { "-ocsp-response", &TestConfig::ocsp_response },
136   { "-signed-cert-timestamps", &TestConfig::signed_cert_timestamps },
137 };
138
139 const Flag<int> kIntFlags[] = {
140   { "-port", &TestConfig::port },
141   { "-min-version", &TestConfig::min_version },
142   { "-max-version", &TestConfig::max_version },
143   { "-mtu", &TestConfig::mtu },
144   { "-export-keying-material", &TestConfig::export_keying_material },
145   { "-expect-total-renegotiations", &TestConfig::expect_total_renegotiations },
146   { "-expect-server-key-exchange-hash",
147     &TestConfig::expect_server_key_exchange_hash },
148   { "-expect-key-exchange-info",
149     &TestConfig::expect_key_exchange_info },
150 };
151
152 }  // namespace
153
154 bool ParseConfig(int argc, char **argv, TestConfig *out_config) {
155   for (int i = 0; i < argc; i++) {
156     bool *bool_field = FindField(out_config, kBoolFlags, argv[i]);
157     if (bool_field != NULL) {
158       *bool_field = true;
159       continue;
160     }
161
162     std::string *string_field = FindField(out_config, kStringFlags, argv[i]);
163     if (string_field != NULL) {
164       i++;
165       if (i >= argc) {
166         fprintf(stderr, "Missing parameter\n");
167         return false;
168       }
169       string_field->assign(argv[i]);
170       continue;
171     }
172
173     std::string *base64_field = FindField(out_config, kBase64Flags, argv[i]);
174     if (base64_field != NULL) {
175       i++;
176       if (i >= argc) {
177         fprintf(stderr, "Missing parameter\n");
178         return false;
179       }
180       std::unique_ptr<uint8_t[]> decoded(new uint8_t[strlen(argv[i])]);
181       int len = EVP_DecodeBlock(decoded.get(),
182                                 reinterpret_cast<const uint8_t *>(argv[i]),
183                                 strlen(argv[i]));
184       if (len < 0) {
185         fprintf(stderr, "Invalid base64: %s\n", argv[i]);
186         return false;
187       }
188       base64_field->assign(reinterpret_cast<const char *>(decoded.get()), len);
189       continue;
190     }
191
192     int *int_field = FindField(out_config, kIntFlags, argv[i]);
193     if (int_field) {
194       i++;
195       if (i >= argc) {
196         fprintf(stderr, "Missing parameter\n");
197         return false;
198       }
199       *int_field = atoi(argv[i]);
200       continue;
201     }
202
203     fprintf(stderr, "Unknown argument: %s\n", argv[i]);
204     return false;
205   }
206
207   return true;
208 }