diff options
Diffstat (limited to 'src/testcurl/https/tls_daemon_options_dh_test.c')
-rw-r--r-- | src/testcurl/https/tls_daemon_options_dh_test.c | 397 |
1 files changed, 0 insertions, 397 deletions
diff --git a/src/testcurl/https/tls_daemon_options_dh_test.c b/src/testcurl/https/tls_daemon_options_dh_test.c deleted file mode 100644 index ff659185..00000000 --- a/src/testcurl/https/tls_daemon_options_dh_test.c +++ /dev/null | |||
@@ -1,397 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2007 Christian Grothoff | ||
4 | |||
5 | libmicrohttpd is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | libmicrohttpd is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with libmicrohttpd; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file tls_daemon_options_dh_test.c | ||
23 | * @brief Testcase for libmicrohttpd HTTPS GET operations | ||
24 | * @author Sagie Amir | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "microhttpd.h" | ||
29 | |||
30 | #include <sys/stat.h> | ||
31 | |||
32 | #include "gnutls.h" | ||
33 | #include <curl/curl.h> | ||
34 | |||
35 | #define DEBUG_CURL_VERBOSE 0 | ||
36 | #define PAGE_NOT_FOUND "<html><head><title>File not found</title></head><body>File not found</body></html>" | ||
37 | |||
38 | #define MHD_E_MEM "Error: memory error\n" | ||
39 | #define MHD_E_SERVER_INIT "Error: failed to start server\n" | ||
40 | #define MHD_E_TEST_FILE_CREAT "Error: failed to setup test file\n" | ||
41 | #define MHD_E_CERT_FILE_CREAT "Error: failed to setup test certificate\n" | ||
42 | #define MHD_E_KEY_FILE_CREAT "Error: failed to setup test certificate\n" | ||
43 | |||
44 | #include "tls_test_keys.h" | ||
45 | |||
46 | const int DEBUG_GNUTLS_LOG_LEVEL = 0; | ||
47 | const char *test_file_name = "https_test_file"; | ||
48 | const char test_file_data[] = "Hello World\n"; | ||
49 | |||
50 | int curl_check_version (const char *req_version, ...); | ||
51 | |||
52 | struct CBC | ||
53 | { | ||
54 | char *buf; | ||
55 | size_t pos; | ||
56 | size_t size; | ||
57 | }; | ||
58 | |||
59 | static size_t | ||
60 | copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx) | ||
61 | { | ||
62 | struct CBC *cbc = ctx; | ||
63 | |||
64 | if (cbc->pos + size * nmemb > cbc->size) | ||
65 | return 0; /* overflow */ | ||
66 | memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); | ||
67 | cbc->pos += size * nmemb; | ||
68 | return size * nmemb; | ||
69 | } | ||
70 | |||
71 | static int | ||
72 | file_reader (void *cls, size_t pos, char *buf, int max) | ||
73 | { | ||
74 | FILE *file = cls; | ||
75 | fseek (file, pos, SEEK_SET); | ||
76 | return fread (buf, 1, max, file); | ||
77 | } | ||
78 | |||
79 | /* HTTP access handler call back */ | ||
80 | static int | ||
81 | http_ahc (void *cls, struct MHD_Connection *connection, | ||
82 | const char *url, const char *method, const char *upload_data, | ||
83 | const char *version, unsigned int *upload_data_size, void **ptr) | ||
84 | { | ||
85 | static int aptr; | ||
86 | struct MHD_Response *response; | ||
87 | int ret; | ||
88 | FILE *file; | ||
89 | struct stat buf; | ||
90 | |||
91 | if (0 != strcmp (method, MHD_HTTP_METHOD_GET)) | ||
92 | return MHD_NO; /* unexpected method */ | ||
93 | if (&aptr != *ptr) | ||
94 | { | ||
95 | /* do never respond on first call */ | ||
96 | *ptr = &aptr; | ||
97 | return MHD_YES; | ||
98 | } | ||
99 | *ptr = NULL; /* reset when done */ | ||
100 | |||
101 | file = fopen (url, "r"); | ||
102 | if (file == NULL) | ||
103 | { | ||
104 | response = MHD_create_response_from_data (strlen (PAGE_NOT_FOUND), | ||
105 | (void *) PAGE_NOT_FOUND, | ||
106 | MHD_NO, MHD_NO); | ||
107 | ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response); | ||
108 | MHD_destroy_response (response); | ||
109 | } | ||
110 | else | ||
111 | { | ||
112 | stat (url, &buf); | ||
113 | response = MHD_create_response_from_callback (buf.st_size, 32 * 1024, /* 32k PAGE_NOT_FOUND size */ | ||
114 | &file_reader, file, | ||
115 | (MHD_ContentReaderFreeCallback) | ||
116 | & fclose); | ||
117 | ret = MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
118 | MHD_destroy_response (response); | ||
119 | } | ||
120 | return ret; | ||
121 | } | ||
122 | |||
123 | /* | ||
124 | * test HTTPS transfer | ||
125 | * @param test_fd: file to attempt transfering | ||
126 | */ | ||
127 | static int | ||
128 | test_https_transfer (FILE * test_fd, char *cipher_suite, int proto_version) | ||
129 | { | ||
130 | CURL *c; | ||
131 | CURLcode errornum; | ||
132 | struct CBC cbc; | ||
133 | char *doc_path; | ||
134 | char url[255]; | ||
135 | struct stat statb; | ||
136 | |||
137 | stat (test_file_name, &statb); | ||
138 | |||
139 | int len = statb.st_size; | ||
140 | |||
141 | /* used to memcmp local copy & deamon supplied copy */ | ||
142 | unsigned char *mem_test_file_local; | ||
143 | |||
144 | /* setup test file path, url */ | ||
145 | doc_path = get_current_dir_name (); | ||
146 | |||
147 | if (NULL == (mem_test_file_local = malloc (len))) | ||
148 | { | ||
149 | fclose (test_fd); | ||
150 | fprintf (stderr, MHD_E_MEM); | ||
151 | return -1; | ||
152 | } | ||
153 | |||
154 | fseek (test_fd, 0, SEEK_SET); | ||
155 | if (fread (mem_test_file_local, sizeof (char), len, test_fd) != len) | ||
156 | { | ||
157 | fclose (test_fd); | ||
158 | fprintf (stderr, "Error: failed to read test file. %s\n", | ||
159 | strerror (errno)); | ||
160 | return -1; | ||
161 | } | ||
162 | |||
163 | if (NULL == (cbc.buf = malloc (sizeof (char) * len))) | ||
164 | { | ||
165 | fclose (test_fd); | ||
166 | fprintf (stderr, MHD_E_MEM); | ||
167 | return -1; | ||
168 | } | ||
169 | cbc.size = len; | ||
170 | cbc.pos = 0; | ||
171 | |||
172 | /* construct url - this might use doc_path */ | ||
173 | sprintf (url, "%s%s/%s", "https://localhost:42433", | ||
174 | doc_path, test_file_name); | ||
175 | |||
176 | c = curl_easy_init (); | ||
177 | #if DEBUG_CURL_VERBOSE | ||
178 | curl_easy_setopt (c, CURLOPT_VERBOSE, 1); | ||
179 | #endif | ||
180 | curl_easy_setopt (c, CURLOPT_URL, url); | ||
181 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
182 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 5L); | ||
183 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L); | ||
184 | curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, ©Buffer); | ||
185 | curl_easy_setopt (c, CURLOPT_FILE, &cbc); | ||
186 | |||
187 | /* TLS options */ | ||
188 | curl_easy_setopt (c, CURLOPT_SSLVERSION, proto_version); | ||
189 | curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, cipher_suite); | ||
190 | |||
191 | /* currently skip any peer authentication */ | ||
192 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0); | ||
193 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0); | ||
194 | |||
195 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
196 | |||
197 | /* NOTE: use of CONNECTTIMEOUT without also | ||
198 | setting NOSIGNAL results in really weird | ||
199 | crashes on my system! */ | ||
200 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
201 | if (CURLE_OK != (errornum = curl_easy_perform (c))) | ||
202 | { | ||
203 | fprintf (stderr, "curl_easy_perform failed: `%s'\n", | ||
204 | curl_easy_strerror (errornum)); | ||
205 | curl_easy_cleanup (c); | ||
206 | return errornum; | ||
207 | } | ||
208 | |||
209 | curl_easy_cleanup (c); | ||
210 | |||
211 | if (memcmp (cbc.buf, mem_test_file_local, len) != 0) | ||
212 | { | ||
213 | fprintf (stderr, "Error: local file & received file differ.\n"); | ||
214 | free (cbc.buf); | ||
215 | free (mem_test_file_local); | ||
216 | return -1; | ||
217 | } | ||
218 | |||
219 | free (mem_test_file_local); | ||
220 | free (cbc.buf); | ||
221 | free (doc_path); | ||
222 | return 0; | ||
223 | } | ||
224 | |||
225 | FILE * | ||
226 | setupTestFile () | ||
227 | { | ||
228 | FILE *test_fd; | ||
229 | |||
230 | if (NULL == (test_fd = fopen (test_file_name, "w+"))) | ||
231 | { | ||
232 | fprintf (stderr, "Error: failed to open `%s': %s\n", | ||
233 | test_file_name, strerror (errno)); | ||
234 | return NULL; | ||
235 | } | ||
236 | if (fwrite (test_file_data, sizeof (char), strlen (test_file_data), test_fd) | ||
237 | != strlen (test_file_data)) | ||
238 | { | ||
239 | fprintf (stderr, "Error: failed to write `%s. %s'\n", | ||
240 | test_file_name, strerror (errno)); | ||
241 | return NULL; | ||
242 | } | ||
243 | if (fflush (test_fd)) | ||
244 | { | ||
245 | fprintf (stderr, "Error: failed to flush test file stream. %s\n", | ||
246 | strerror (errno)); | ||
247 | return NULL; | ||
248 | } | ||
249 | |||
250 | return test_fd; | ||
251 | } | ||
252 | |||
253 | static int | ||
254 | setup (struct MHD_Daemon **d, va_list arg_list) | ||
255 | { | ||
256 | *d = MHD_start_daemon_va (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL | | ||
257 | MHD_USE_DEBUG, 42433, | ||
258 | NULL, NULL, &http_ahc, NULL, arg_list); | ||
259 | |||
260 | if (*d == NULL) | ||
261 | { | ||
262 | fprintf (stderr, MHD_E_SERVER_INIT); | ||
263 | return -1; | ||
264 | } | ||
265 | |||
266 | return 0; | ||
267 | } | ||
268 | |||
269 | static void | ||
270 | teardown (struct MHD_Daemon *d) | ||
271 | { | ||
272 | MHD_stop_daemon (d); | ||
273 | } | ||
274 | |||
275 | /* TODO test_wrap: change sig to (setup_func, test, va_list test_arg) & move to test_util.c */ | ||
276 | int | ||
277 | test_wrap (char *test_name, int | ||
278 | (*test) (FILE * test_fd, char *cipher_suite, int proto_version), | ||
279 | FILE * test_fd, char *cipher_suite, int proto_version, ...) | ||
280 | { | ||
281 | int ret; | ||
282 | va_list arg_list; | ||
283 | struct MHD_Daemon *d; | ||
284 | |||
285 | va_start (arg_list, proto_version); | ||
286 | if (setup (&d, arg_list) != 0) | ||
287 | { | ||
288 | va_end (arg_list); | ||
289 | return -1; | ||
290 | } | ||
291 | |||
292 | fprintf (stdout, "running test: %s ", test_name); | ||
293 | ret = test (test_fd, cipher_suite, proto_version); | ||
294 | |||
295 | if (ret == 0) | ||
296 | { | ||
297 | fprintf (stdout, "[pass]\n"); | ||
298 | } | ||
299 | else | ||
300 | { | ||
301 | fprintf (stdout, "[fail]\n"); | ||
302 | } | ||
303 | |||
304 | teardown (d); | ||
305 | va_end (arg_list); | ||
306 | return ret; | ||
307 | } | ||
308 | |||
309 | /* | ||
310 | * test server refuses to negotiate connections with unsupported protocol versions | ||
311 | */ | ||
312 | int | ||
313 | test_protocol_version (FILE * test_fd, char *cipher_suite, | ||
314 | int curl_proto_version) | ||
315 | { | ||
316 | CURL *c; | ||
317 | CURLcode errornum; | ||
318 | |||
319 | c = curl_easy_init (); | ||
320 | #if DEBUG_CURL_VERBOSE | ||
321 | curl_easy_setopt (c, CURLOPT_VERBOSE, 1); | ||
322 | #endif | ||
323 | curl_easy_setopt (c, CURLOPT_URL, "https://localhost:42433/"); | ||
324 | curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | ||
325 | curl_easy_setopt (c, CURLOPT_TIMEOUT, 5L); | ||
326 | curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 5L); | ||
327 | |||
328 | /* TLS options */ | ||
329 | curl_easy_setopt (c, CURLOPT_SSLVERSION, curl_proto_version); | ||
330 | curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, cipher_suite); | ||
331 | |||
332 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0); | ||
333 | curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0); | ||
334 | curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); | ||
335 | |||
336 | /* NOTE: use of CONNECTTIMEOUT without also | ||
337 | setting NOSIGNAL results in really weird | ||
338 | crashes on my system! */ | ||
339 | curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1); | ||
340 | |||
341 | /* assert daemon rejected request */ | ||
342 | if (CURLE_OK == (errornum = curl_easy_perform (c))) | ||
343 | { | ||
344 | fprintf (stderr, "curl_easy_perform failed: `%s'\n", | ||
345 | curl_easy_strerror (errornum)); | ||
346 | curl_easy_cleanup (c); | ||
347 | return -1; | ||
348 | } | ||
349 | |||
350 | return 0; | ||
351 | } | ||
352 | |||
353 | /* setup a temporary transfer test file */ | ||
354 | int | ||
355 | main (int argc, char *const *argv) | ||
356 | { | ||
357 | FILE *test_fd; | ||
358 | unsigned int errorCount = 0; | ||
359 | |||
360 | MHD_gtls_global_set_log_level (DEBUG_GNUTLS_LOG_LEVEL); | ||
361 | |||
362 | if (curl_check_version (MHD_REQ_CURL_VERSION)) | ||
363 | { | ||
364 | return -1; | ||
365 | } | ||
366 | |||
367 | if ((test_fd = setupTestFile ()) == NULL) | ||
368 | { | ||
369 | fprintf (stderr, MHD_E_TEST_FILE_CREAT); | ||
370 | return -1; | ||
371 | } | ||
372 | |||
373 | if (0 != curl_global_init (CURL_GLOBAL_ALL)) | ||
374 | { | ||
375 | fprintf (stderr, "Error: %s\n", strerror (errno)); | ||
376 | return -1; | ||
377 | } | ||
378 | |||
379 | int cipher[] = { MHD_GNUTLS_CIPHER_3DES_CBC, 0 }; | ||
380 | int kx[] = { MHD_GNUTLS_KX_ANON_DH, 0 }; | ||
381 | |||
382 | errorCount += | ||
383 | test_wrap ("kx ANON_DH", &test_https_transfer, test_fd, | ||
384 | "ADH-DES-CBC3-SHA", CURL_SSLVERSION_TLSv1, | ||
385 | MHD_OPTION_CRED_TYPE, MHD_GNUTLS_CRD_ANON, | ||
386 | MHD_OPTION_CIPHER_ALGORITHM, cipher, MHD_OPTION_KX_PRIORITY, | ||
387 | kx, MHD_OPTION_END); | ||
388 | if (errorCount != 0) | ||
389 | fprintf (stderr, "Failed test: %s.\n", argv[0]); | ||
390 | |||
391 | curl_global_cleanup (); | ||
392 | fclose (test_fd); | ||
393 | |||
394 | remove (test_file_name); | ||
395 | |||
396 | return errorCount != 0; | ||
397 | } | ||