diff options
Diffstat (limited to 'src/testcurl/https/test_https_sni.c')
-rw-r--r-- | src/testcurl/https/test_https_sni.c | 156 |
1 files changed, 80 insertions, 76 deletions
diff --git a/src/testcurl/https/test_https_sni.c b/src/testcurl/https/test_https_sni.c index 8af1472b..16576656 100644 --- a/src/testcurl/https/test_https_sni.c +++ b/src/testcurl/https/test_https_sni.c | |||
@@ -60,9 +60,9 @@ static struct Hosts *hosts; | |||
60 | * (This code is largely taken from GnuTLS). | 60 | * (This code is largely taken from GnuTLS). |
61 | */ | 61 | */ |
62 | static void | 62 | static void |
63 | load_keys(const char *hostname, | 63 | load_keys (const char *hostname, |
64 | const char *CERT_FILE, | 64 | const char *CERT_FILE, |
65 | const char *KEY_FILE) | 65 | const char *KEY_FILE) |
66 | { | 66 | { |
67 | int ret; | 67 | int ret; |
68 | gnutls_datum_t data; | 68 | gnutls_datum_t data; |
@@ -77,32 +77,32 @@ load_keys(const char *hostname, | |||
77 | 77 | ||
78 | ret = gnutls_load_file (CERT_FILE, &data); | 78 | ret = gnutls_load_file (CERT_FILE, &data); |
79 | if (ret < 0) | 79 | if (ret < 0) |
80 | { | 80 | { |
81 | fprintf (stderr, | 81 | fprintf (stderr, |
82 | "*** Error loading certificate file %s.\n", | 82 | "*** Error loading certificate file %s.\n", |
83 | CERT_FILE); | 83 | CERT_FILE); |
84 | exit (1); | 84 | exit (1); |
85 | } | 85 | } |
86 | ret = | 86 | ret = |
87 | gnutls_pcert_import_x509_raw (&host->pcrt, &data, GNUTLS_X509_FMT_PEM, | 87 | gnutls_pcert_import_x509_raw (&host->pcrt, &data, GNUTLS_X509_FMT_PEM, |
88 | 0); | 88 | 0); |
89 | if (ret < 0) | 89 | if (ret < 0) |
90 | { | 90 | { |
91 | fprintf (stderr, | 91 | fprintf (stderr, |
92 | "*** Error loading certificate file: %s\n", | 92 | "*** Error loading certificate file: %s\n", |
93 | gnutls_strerror (ret)); | 93 | gnutls_strerror (ret)); |
94 | exit (1); | 94 | exit (1); |
95 | } | 95 | } |
96 | gnutls_free (data.data); | 96 | gnutls_free (data.data); |
97 | 97 | ||
98 | ret = gnutls_load_file (KEY_FILE, &data); | 98 | ret = gnutls_load_file (KEY_FILE, &data); |
99 | if (ret < 0) | 99 | if (ret < 0) |
100 | { | 100 | { |
101 | fprintf (stderr, | 101 | fprintf (stderr, |
102 | "*** Error loading key file %s.\n", | 102 | "*** Error loading key file %s.\n", |
103 | KEY_FILE); | 103 | KEY_FILE); |
104 | exit (1); | 104 | exit (1); |
105 | } | 105 | } |
106 | 106 | ||
107 | gnutls_privkey_init (&host->key); | 107 | gnutls_privkey_init (&host->key); |
108 | ret = | 108 | ret = |
@@ -110,12 +110,12 @@ load_keys(const char *hostname, | |||
110 | &data, GNUTLS_X509_FMT_PEM, | 110 | &data, GNUTLS_X509_FMT_PEM, |
111 | NULL, 0); | 111 | NULL, 0); |
112 | if (ret < 0) | 112 | if (ret < 0) |
113 | { | 113 | { |
114 | fprintf (stderr, | 114 | fprintf (stderr, |
115 | "*** Error loading key file: %s\n", | 115 | "*** Error loading key file: %s\n", |
116 | gnutls_strerror (ret)); | 116 | gnutls_strerror (ret)); |
117 | exit (1); | 117 | exit (1); |
118 | } | 118 | } |
119 | gnutls_free (data.data); | 119 | gnutls_free (data.data); |
120 | } | 120 | } |
121 | 121 | ||
@@ -133,19 +133,19 @@ load_keys(const char *hostname, | |||
133 | */ | 133 | */ |
134 | static int | 134 | static int |
135 | sni_callback (gnutls_session_t session, | 135 | sni_callback (gnutls_session_t session, |
136 | const gnutls_datum_t* req_ca_dn, | 136 | const gnutls_datum_t*req_ca_dn, |
137 | int nreqs, | 137 | int nreqs, |
138 | const gnutls_pk_algorithm_t* pk_algos, | 138 | const gnutls_pk_algorithm_t*pk_algos, |
139 | int pk_algos_length, | 139 | int pk_algos_length, |
140 | gnutls_pcert_st** pcert, | 140 | gnutls_pcert_st**pcert, |
141 | unsigned int *pcert_length, | 141 | unsigned int *pcert_length, |
142 | gnutls_privkey_t * pkey) | 142 | gnutls_privkey_t *pkey) |
143 | { | 143 | { |
144 | char name[256]; | 144 | char name[256]; |
145 | size_t name_len; | 145 | size_t name_len; |
146 | struct Hosts *host; | 146 | struct Hosts *host; |
147 | unsigned int type; | 147 | unsigned int type; |
148 | (void)req_ca_dn;(void)nreqs;(void)pk_algos;(void)pk_algos_length; /* Unused. Silent compiler warning. */ | 148 | (void) req_ca_dn; (void) nreqs; (void) pk_algos; (void) pk_algos_length; /* Unused. Silent compiler warning. */ |
149 | 149 | ||
150 | name_len = sizeof (name); | 150 | name_len = sizeof (name); |
151 | if (GNUTLS_E_SUCCESS != | 151 | if (GNUTLS_E_SUCCESS != |
@@ -159,13 +159,13 @@ sni_callback (gnutls_session_t session, | |||
159 | if (0 == strncmp (name, host->hostname, name_len)) | 159 | if (0 == strncmp (name, host->hostname, name_len)) |
160 | break; | 160 | break; |
161 | if (NULL == host) | 161 | if (NULL == host) |
162 | { | 162 | { |
163 | fprintf (stderr, | 163 | fprintf (stderr, |
164 | "Need certificate for %.*s\n", | 164 | "Need certificate for %.*s\n", |
165 | (int) name_len, | 165 | (int) name_len, |
166 | name); | 166 | name); |
167 | return -1; | 167 | return -1; |
168 | } | 168 | } |
169 | #if 0 | 169 | #if 0 |
170 | fprintf (stderr, | 170 | fprintf (stderr, |
171 | "Returning certificate for %.*s\n", | 171 | "Returning certificate for %.*s\n", |
@@ -192,10 +192,10 @@ do_get (const char *url, int port) | |||
192 | 192 | ||
193 | len = strlen (test_data); | 193 | len = strlen (test_data); |
194 | if (NULL == (cbc.buf = malloc (sizeof (char) * len))) | 194 | if (NULL == (cbc.buf = malloc (sizeof (char) * len))) |
195 | { | 195 | { |
196 | fprintf (stderr, MHD_E_MEM); | 196 | fprintf (stderr, MHD_E_MEM); |
197 | return -1; | 197 | return -1; |
198 | } | 198 | } |
199 | cbc.size = len; | 199 | cbc.size = len; |
200 | cbc.pos = 0; | 200 | cbc.pos = 0; |
201 | 201 | ||
@@ -204,7 +204,7 @@ do_get (const char *url, int port) | |||
204 | curl_easy_setopt (c, CURLOPT_VERBOSE, 1L); | 204 | curl_easy_setopt (c, CURLOPT_VERBOSE, 1L); |
205 | #endif | 205 | #endif |
206 | curl_easy_setopt (c, CURLOPT_URL, url); | 206 | curl_easy_setopt (c, CURLOPT_URL, url); |
207 | curl_easy_setopt (c, CURLOPT_PORT, (long)port); | 207 | curl_easy_setopt (c, CURLOPT_PORT, (long) port); |
208 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | 208 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); |
209 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 10L); | 209 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 10L); |
210 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 10L); | 210 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 10L); |
@@ -215,9 +215,9 @@ do_get (const char *url, int port) | |||
215 | /* TODO merge into send_curl_req */ | 215 | /* TODO merge into send_curl_req */ |
216 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0L); | 216 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0L); |
217 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 2L); | 217 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 2L); |
218 | sprintf(buf, "host1:%d:127.0.0.1", port); | 218 | sprintf (buf, "host1:%d:127.0.0.1", port); |
219 | dns_info = curl_slist_append (NULL, buf); | 219 | dns_info = curl_slist_append (NULL, buf); |
220 | sprintf(buf, "host2:%d:127.0.0.1", port); | 220 | sprintf (buf, "host2:%d:127.0.0.1", port); |
221 | dns_info = curl_slist_append (dns_info, buf); | 221 | dns_info = curl_slist_append (dns_info, buf); |
222 | curl_easy_setopt (c, CURLOPT_RESOLVE, dns_info); | 222 | curl_easy_setopt (c, CURLOPT_RESOLVE, dns_info); |
223 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); | 223 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); |
@@ -227,23 +227,23 @@ do_get (const char *url, int port) | |||
227 | crashes on my system! */ | 227 | crashes on my system! */ |
228 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); | 228 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); |
229 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | 229 | if (CURLE_OK != (errornum = curl_easy_perform (c))) |
230 | { | 230 | { |
231 | fprintf (stderr, "curl_easy_perform failed: `%s'\n", | 231 | fprintf (stderr, "curl_easy_perform failed: `%s'\n", |
232 | curl_easy_strerror (errornum)); | 232 | curl_easy_strerror (errornum)); |
233 | curl_easy_cleanup (c); | 233 | curl_easy_cleanup (c); |
234 | free (cbc.buf); | 234 | free (cbc.buf); |
235 | curl_slist_free_all (dns_info); | 235 | curl_slist_free_all (dns_info); |
236 | return errornum; | 236 | return errornum; |
237 | } | 237 | } |
238 | 238 | ||
239 | curl_easy_cleanup (c); | 239 | curl_easy_cleanup (c); |
240 | curl_slist_free_all (dns_info); | 240 | curl_slist_free_all (dns_info); |
241 | if (memcmp (cbc.buf, test_data, len) != 0) | 241 | if (memcmp (cbc.buf, test_data, len) != 0) |
242 | { | 242 | { |
243 | fprintf (stderr, "Error: local file & received file differ.\n"); | 243 | fprintf (stderr, "Error: local file & received file differ.\n"); |
244 | free (cbc.buf); | 244 | free (cbc.buf); |
245 | return -1; | 245 | return -1; |
246 | } | 246 | } |
247 | 247 | ||
248 | free (cbc.buf); | 248 | free (cbc.buf); |
249 | return 0; | 249 | return 0; |
@@ -256,7 +256,7 @@ main (int argc, char *const *argv) | |||
256 | unsigned int error_count = 0; | 256 | unsigned int error_count = 0; |
257 | struct MHD_Daemon *d; | 257 | struct MHD_Daemon *d; |
258 | int port; | 258 | int port; |
259 | (void)argc; /* Unused. Silent compiler warning. */ | 259 | (void) argc; /* Unused. Silent compiler warning. */ |
260 | 260 | ||
261 | if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) | 261 | if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT)) |
262 | port = 0; | 262 | port = 0; |
@@ -269,36 +269,40 @@ main (int argc, char *const *argv) | |||
269 | gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); | 269 | gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0); |
270 | #endif | 270 | #endif |
271 | #endif /* MHD_HTTPS_REQUIRE_GRYPT */ | 271 | #endif /* MHD_HTTPS_REQUIRE_GRYPT */ |
272 | if (!testsuite_curl_global_init ()) | 272 | if (! testsuite_curl_global_init ()) |
273 | return 99; | 273 | return 99; |
274 | if (NULL == curl_version_info (CURLVERSION_NOW)->ssl_version) | 274 | if (NULL == curl_version_info (CURLVERSION_NOW)->ssl_version) |
275 | { | 275 | { |
276 | fprintf (stderr, "Curl does not support SSL. Cannot run the test.\n"); | 276 | fprintf (stderr, "Curl does not support SSL. Cannot run the test.\n"); |
277 | curl_global_cleanup (); | 277 | curl_global_cleanup (); |
278 | return 77; | 278 | return 77; |
279 | } | 279 | } |
280 | 280 | ||
281 | load_keys ("host1", ABS_SRCDIR "/host1.crt", ABS_SRCDIR "/host1.key"); | 281 | load_keys ("host1", ABS_SRCDIR "/host1.crt", ABS_SRCDIR "/host1.key"); |
282 | load_keys ("host2", ABS_SRCDIR "/host2.crt", ABS_SRCDIR "/host2.key"); | 282 | load_keys ("host2", ABS_SRCDIR "/host2.crt", ABS_SRCDIR "/host2.key"); |
283 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS | MHD_USE_ERROR_LOG, | 283 | d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION |
284 | | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_TLS | ||
285 | | MHD_USE_ERROR_LOG, | ||
284 | port, | 286 | port, |
285 | NULL, NULL, | 287 | NULL, NULL, |
286 | &http_ahc, NULL, | 288 | &http_ahc, NULL, |
287 | MHD_OPTION_HTTPS_CERT_CALLBACK, &sni_callback, | 289 | MHD_OPTION_HTTPS_CERT_CALLBACK, &sni_callback, |
288 | MHD_OPTION_END); | 290 | MHD_OPTION_END); |
289 | if (d == NULL) | 291 | if (d == NULL) |
290 | { | 292 | { |
291 | fprintf (stderr, MHD_E_SERVER_INIT); | 293 | fprintf (stderr, MHD_E_SERVER_INIT); |
292 | return -1; | 294 | return -1; |
293 | } | 295 | } |
294 | if (0 == port) | 296 | if (0 == port) |
297 | { | ||
298 | const union MHD_DaemonInfo *dinfo; | ||
299 | dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); | ||
300 | if ((NULL == dinfo) ||(0 == dinfo->port) ) | ||
295 | { | 301 | { |
296 | const union MHD_DaemonInfo *dinfo; | 302 | MHD_stop_daemon (d); return -1; |
297 | dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); | ||
298 | if (NULL == dinfo || 0 == dinfo->port) | ||
299 | { MHD_stop_daemon (d); return -1; } | ||
300 | port = (int)dinfo->port; | ||
301 | } | 303 | } |
304 | port = (int) dinfo->port; | ||
305 | } | ||
302 | if (0 != do_get ("https://host1/", port)) | 306 | if (0 != do_get ("https://host1/", port)) |
303 | error_count++; | 307 | error_count++; |
304 | if (0 != do_get ("https://host2/", port)) | 308 | if (0 != do_get ("https://host2/", port)) |