]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/blob - tests/client.c
4995536c548bdd14f4e2fcd661c9c3e175b20205
[tlsproxy/tlsproxy.git] / tests / client.c
1 /*
2  * Simple GnuTLS client used for testing.
3  *
4  * Copyright (C) 2011-2013  Simon Ruderich
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21
22 #include <arpa/inet.h>
23 #include <assert.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <netdb.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/socket.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 #include <gnutls/gnutls.h>
35 #include <gnutls/x509.h>
36
37
38 #define MAX_REQUEST_LINE 4096
39
40 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd);
41 static int connect_to_host(const char *hostname, const char *port);
42 static int read_http_request(FILE *client_fd, char *request, size_t length);
43
44 #if 0
45 static void log_function_gnutls(int level, const char *string) {
46     (void)level;
47     fprintf(stderr, "    => %s", string);
48 }
49 #endif
50
51 int main (int argc, char *argv[]) {
52     int result, response;
53     unsigned int status;
54     char buffer[MAX_REQUEST_LINE];
55     int server;
56     FILE *fd_read, *fd_write;
57
58     gnutls_session_t session;
59     gnutls_certificate_credentials_t xcred;
60
61     gnutls_x509_crt_t cert;
62     const gnutls_datum_t *cert_list;
63     unsigned int cert_list_size;
64
65     if (argc != 5 && argc != 6) {
66         fprintf(stderr,
67                 "Usage: %s <ca-file> <hostname> <port> <hostname-verify> "
68                           "[<digest-authentication>]\n",
69                 argv[0]);
70         return EXIT_FAILURE;
71     }
72
73     gnutls_global_init();
74     gnutls_certificate_allocate_credentials(&xcred);
75
76 #if 0
77     gnutls_global_set_log_level(10);
78     gnutls_global_set_log_function(log_function_gnutls);
79 #endif
80
81     gnutls_certificate_set_x509_trust_file(xcred,
82                                            argv[1], GNUTLS_X509_FMT_PEM);
83
84     gnutls_init(&session, GNUTLS_CLIENT);
85     gnutls_priority_set_direct(session, "NORMAL", NULL);
86     gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
87
88     server = connect_to_host("localhost", "4711");
89     if (server == -1) {
90         return EXIT_FAILURE;
91     }
92     if (fdopen_read_write(server, &fd_read, &fd_write) != 0) {
93         return EXIT_FAILURE;
94     }
95
96     /* Talk to tlsproxy. */
97     fprintf(fd_write, "CONNECT %s:%s HTTP/1.0\r\n", argv[2], argv[3]);
98     if (argc == 6) {
99         fprintf(fd_write, "Proxy-Authorization: Basic %s\r\n", argv[5]);
100     }
101     fprintf(fd_write, "\r\n");
102     fflush(fd_write);
103     if (read_http_request(fd_read, buffer, sizeof(buffer)) != 0) {
104         fprintf(stderr, "invalid proxy response\n");
105         return EXIT_FAILURE;
106     }
107
108     printf("response: %s\n", buffer);
109
110     if (sscanf(buffer, "HTTP/1.0 %d", &response) != 1) {
111         fprintf(stderr, "invalid proxy response: %s\n", buffer);
112         return EXIT_FAILURE;
113     }
114
115     if (response != 200) {
116         fprintf(stderr, "proxy failure\n");
117         return EXIT_FAILURE;
118     }
119
120 #ifdef HAVE_GNUTLS_TRANSPORT_SET_INT2
121     /* gnutls_transport_set_int() is a macro. */
122     gnutls_transport_set_int(session, server);
123 #else
124     gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t)server);
125 #endif
126
127     result = gnutls_handshake(session);
128     if (result != GNUTLS_E_SUCCESS) {
129         fprintf(stderr, "gnutls_handshake() failed\n");
130         gnutls_perror(result);
131         return EXIT_FAILURE;
132     }
133
134     /* Verify the proxy certificate. */
135     result = gnutls_certificate_verify_peers2(session, &status);
136     if (result < 0) {
137         fprintf(stderr, "gnutls_certificate_verify_peers2() failed\n");
138         gnutls_perror(result);
139         return EXIT_FAILURE;
140     }
141
142     if (status & GNUTLS_CERT_INVALID) {
143         fprintf(stderr, "certificate invalid\n");
144     }
145
146     /* Get proxy certificate. */
147     if ((result = gnutls_x509_crt_init(&cert)) < 0) {
148         fprintf(stderr, "gnutls_x509_crt_init() failed");
149         gnutls_perror(result);
150         return EXIT_FAILURE;
151     }
152
153     cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
154     if (cert_list == NULL) {
155         fprintf(stderr, "gnutls_certificate_get_peers() failed");
156         return EXIT_FAILURE;
157     }
158
159     if ((result = gnutls_x509_crt_import(cert, &cert_list[0],
160                                          GNUTLS_X509_FMT_DER)) < 0) {
161         fprintf(stderr, "gnutls_x509_crt_import() failed");
162         gnutls_perror(result);
163         return EXIT_FAILURE;
164     }
165
166     /* Check hostname. */
167     if (!gnutls_x509_crt_check_hostname(cert, argv[4])) {
168         fprintf(stderr, "hostname didn't match '%s'\n", argv[4]);
169         return EXIT_FAILURE;
170     }
171
172     gnutls_x509_crt_deinit(cert);
173
174     /* Send a bogus request to the server. Otherwise recent gnutls-serv won't
175      * terminate the connection when gnutls_bye() is used. */
176     gnutls_record_send(session, "GET / HTTP/1.0\r\n\r\n",
177                                 strlen("GET / HTTP/1.0\r\n\r\n"));
178
179     gnutls_bye(session, GNUTLS_SHUT_RDWR);
180     fclose(fd_read);
181     fclose(fd_write);
182
183     gnutls_deinit(session);
184     gnutls_certificate_free_credentials(xcred);
185     gnutls_global_deinit();
186
187     return EXIT_SUCCESS;
188 }
189
190
191 /* Copied from src/connection.c (and removed LOG_* stuff)! Don't modify. */
192
193 static int fdopen_read_write(int socket, FILE **read_fd, FILE **write_fd) {
194     *read_fd = fdopen(socket, "r");
195     if (*read_fd == NULL) {
196         perror("fdopen_read_write(): fdopen(\"r\") failed");
197         return -1;
198     }
199
200     *write_fd = fdopen(dup(socket), "w");
201     if (*write_fd == NULL) {
202         perror("fdopen_read_write(): fdopen(\"w\") failed");
203         fclose(*read_fd);
204         *read_fd = NULL; /* "tell" caller read_fd is already closed */
205         return -1;
206     }
207
208     return 0;
209 }
210
211 static int connect_to_host(const char *hostname, const char *port) {
212     struct addrinfo gai_hints;
213     struct addrinfo *gai_result;
214     int gai_return;
215
216     int server_socket;
217     struct addrinfo *server;
218
219     if (hostname == NULL || port == NULL) {
220         return -1;
221     }
222
223     /* Get IP of hostname server. */
224     memset(&gai_hints, 0, sizeof(gai_hints));
225     gai_hints.ai_family   = AF_UNSPEC;
226     gai_hints.ai_socktype = SOCK_STREAM;
227     gai_hints.ai_protocol = 0;
228     gai_hints.ai_flags    = AI_NUMERICSERV /* given port is numeric */
229 #ifdef AI_ADDRCONFIG
230                           | AI_ADDRCONFIG  /* supported by this computer */
231 #endif
232                           ;
233     gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
234     if (gai_return != 0) {
235         if (gai_return == EAI_SYSTEM) {
236             perror("connect_to_host(): getaddrinfo()");
237         } else {
238             fprintf(stderr, "connect_to_host(): getaddrinfo(): %s",
239                             gai_strerror(gai_return));
240         }
241         return -1;
242     }
243
244     /* Now try to connect to each server returned by getaddrinfo(), use the
245      * first successful connect. */
246     for (server = gai_result; server != NULL; server = server->ai_next) {
247         server_socket = socket(server->ai_family,
248                                server->ai_socktype,
249                                server->ai_protocol);
250         if (server_socket < 0) {
251             perror("connect_to_host(): socket(), trying next");
252             continue;
253         }
254
255         if (connect(server_socket, server->ai_addr, server->ai_addrlen) == 0) {
256             break;
257         }
258         perror("connect_to_host(): connect(), trying next");
259
260         close(server_socket);
261     }
262     /* Make sure we free the result from getaddrinfo(). */
263     freeaddrinfo(gai_result);
264
265     if (server == NULL) {
266         perror("connect_to_host(): no server found, abort");
267         return -1;
268     }
269
270     return server_socket;
271 }
272
273 static int read_http_request(FILE *client_fd, char *request, size_t length) {
274     char buffer[MAX_REQUEST_LINE];
275
276     assert(length <= INT_MAX);
277     if (fgets(request, (int)length, client_fd) == NULL) {
278         if (ferror(client_fd)) {
279             perror("read_http_request(): fgets()");
280             return -1;
281         }
282         /* EOF */
283         return -2;
284     }
285
286     while (fgets(buffer, sizeof(buffer), client_fd) != NULL) {
287         /* End of header. */
288         if (!strcmp(buffer, "\n") || !strcmp(buffer, "\r\n")) {
289             break;
290         }
291     }
292     if (ferror(client_fd)) {
293         perror("read_http_request(): fgets()");
294         return -1;
295     } else if (feof(client_fd)) {
296         return -2;
297     }
298
299     return 0;
300 }