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