aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/https/test_https_session_info.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/https/test_https_session_info.c')
-rw-r--r--src/testcurl/https/test_https_session_info.c170
1 files changed, 170 insertions, 0 deletions
diff --git a/src/testcurl/https/test_https_session_info.c b/src/testcurl/https/test_https_session_info.c
new file mode 100644
index 00000000..8050a45d
--- /dev/null
+++ b/src/testcurl/https/test_https_session_info.c
@@ -0,0 +1,170 @@
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 mhds_session_info_test.c
23 * @brief Testcase for libmicrohttpd HTTPS connection querying operations
24 * @author Sagie Amir
25 */
26
27#include "platform.h"
28#include "microhttpd.h"
29#include <curl/curl.h>
30
31#include "tls_test_common.h"
32
33extern int curl_check_version (const char *req_version, ...);
34extern const char srv_key_pem[];
35extern const char srv_self_signed_cert_pem[];
36
37struct MHD_Daemon *d;
38
39/*
40 * HTTP access handler call back
41 * used to query negotiated security parameters
42 */
43static int
44query_session_ahc (void *cls, struct MHD_Connection *connection,
45 const char *url, const char *method,
46 const char *upload_data, const char *version,
47 size_t *upload_data_size, void **ptr)
48{
49 struct MHD_Response *response;
50 int ret;
51
52 if (NULL == *ptr)
53 {
54 *ptr = &query_session_ahc;
55 return MHD_YES;
56 }
57
58 if (GNUTLS_SSL3 !=
59 (ret = MHD_get_connection_info
60 (connection,
61 MHD_CONNECTION_INFO_PROTOCOL)->protocol))
62 {
63 fprintf (stderr, "Error: requested protocol mismatch (wanted %d, got %d)\n",
64 GNUTLS_SSL3,
65 ret);
66 return -1;
67 }
68
69 response = MHD_create_response_from_buffer (strlen (EMPTY_PAGE),
70 (void *) EMPTY_PAGE,
71 MHD_RESPMEM_PERSISTENT);
72 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
73 MHD_destroy_response (response);
74 return ret;
75}
76
77
78/**
79 * negotiate a secure connection with server & query negotiated security parameters
80 */
81static int
82test_query_session ()
83{
84 CURL *c;
85 struct CBC cbc;
86 CURLcode errornum;
87 char url[256];
88
89 if (NULL == (cbc.buf = malloc (sizeof (char) * 255)))
90 return 16;
91 cbc.size = 255;
92 cbc.pos = 0;
93
94 gen_test_file_url (url, DEAMON_TEST_PORT);
95
96 /* setup test */
97 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL |
98 MHD_USE_DEBUG, DEAMON_TEST_PORT,
99 NULL, NULL, &query_session_ahc, NULL,
100 MHD_OPTION_HTTPS_PRIORITIES, "NORMAL:+ARCFOUR-128",
101 MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
102 MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
103 MHD_OPTION_END);
104
105 if (d == NULL)
106 return 2;
107
108 const char *aes256_sha = "AES256-SHA";
109 if (curl_uses_nss_ssl() == 0)
110 {
111 aes256_sha = "rsa_aes_256_sha";
112 }
113
114 c = curl_easy_init ();
115#if DEBUG_HTTPS_TEST
116 curl_easy_setopt (c, CURLOPT_VERBOSE, 1);
117#endif
118 curl_easy_setopt (c, CURLOPT_URL, url);
119 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
120 curl_easy_setopt (c, CURLOPT_TIMEOUT, 10L);
121 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 10L);
122 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
123 curl_easy_setopt (c, CURLOPT_FILE, &cbc);
124 /* TLS options */
125 curl_easy_setopt (c, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3);
126 curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, aes256_sha);
127 /* currently skip any peer authentication */
128 curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0);
129 curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0);
130 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
131
132 // NOTE: use of CONNECTTIMEOUT without also
133 // setting NOSIGNAL results in really weird
134 // crashes on my system!
135 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
136 if (CURLE_OK != (errornum = curl_easy_perform (c)))
137 {
138 fprintf (stderr, "curl_easy_perform failed: `%s'\n",
139 curl_easy_strerror (errornum));
140
141 MHD_stop_daemon (d);
142 curl_easy_cleanup (c);
143 free (cbc.buf);
144 return -1;
145 }
146
147 curl_easy_cleanup (c);
148 MHD_stop_daemon (d);
149 free (cbc.buf);
150 return 0;
151}
152
153
154int
155main (int argc, char *const *argv)
156{
157 unsigned int errorCount = 0;
158
159 if (0 != curl_global_init (CURL_GLOBAL_ALL))
160 {
161 fprintf (stderr, "Error (code: %u)\n", errorCount);
162 return -1;
163 }
164 errorCount += test_query_session ();
165 print_test_result (errorCount, argv[0]);
166 curl_global_cleanup ();
167 if (errorCount > 0)
168 fprintf (stderr, "Error (code: %u)\n", errorCount);
169 return errorCount;
170}