]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/blob - tests/client.c
Check library functions for success values.
[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 connect_to_host(const char *hostname, const char *port);
39 static int read_http_request(FILE *client_fd, char *request, size_t length);
40
41
42 int main (int argc, char *argv[]) {
43     int result, response;
44     unsigned int status;
45     char buffer[MAX_REQUEST_LINE];
46     int server;
47     FILE *fd;
48
49     gnutls_session_t session;
50     gnutls_certificate_credentials_t xcred;
51
52     gnutls_x509_crt_t cert;
53     const gnutls_datum_t *cert_list;
54     unsigned int cert_list_size;
55
56     if (argc != 5 && argc != 6) {
57         fprintf(stderr,
58                 "Usage: %s <ca-file> <hostname> <port> <hostname-verify> "
59                           "[<digest-authentication>]\n",
60                 argv[0]);
61         return EXIT_FAILURE;
62     }
63
64     gnutls_global_init();
65     gnutls_certificate_allocate_credentials(&xcred);
66
67     gnutls_certificate_set_x509_trust_file(xcred,
68                                            argv[1], GNUTLS_X509_FMT_PEM);
69
70     gnutls_init(&session, GNUTLS_CLIENT);
71     gnutls_priority_set_direct(session, "NORMAL", NULL);
72     gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
73
74     server = connect_to_host("localhost", "4711");
75     if (server == -1) {
76         return EXIT_FAILURE;
77     }
78     fd = fdopen(server, "a+");
79     if (fd == NULL) {
80         perror("fdopen()");
81         return EXIT_FAILURE;
82     }
83
84     /* Talk to tlsproxy. */
85     fprintf(fd, "CONNECT %s:%s HTTP/1.0\r\n", argv[2], argv[3]);
86     if (argc == 6) {
87         fprintf(fd, "Proxy-Authorization: Basic %s\r\n", argv[5]);
88     }
89     fprintf(fd, "\r\n");
90     fflush(fd);
91     if (read_http_request(fd, buffer, sizeof(buffer)) == -1) {
92         fprintf(stderr, "invalid proxy response\n");
93         return EXIT_FAILURE;
94     }
95
96     printf("response: %s\n", buffer);
97
98     if (sscanf(buffer, "HTTP/1.0 %d", &response) != 1) {
99         fprintf(stderr, "invalid proxy response: %s\n", buffer);
100         return EXIT_FAILURE;
101     }
102
103     if (response != 200) {
104         fprintf(stderr, "proxy failure\n");
105         return EXIT_FAILURE;
106     }
107
108     gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t)server);
109
110     result = gnutls_handshake(session);
111     if (result != GNUTLS_E_SUCCESS) {
112         fprintf(stderr, "gnutls_handshake() failed\n");
113         gnutls_perror(result);
114         return EXIT_FAILURE;
115     }
116
117     /* Verify the proxy certificate. */
118     result = gnutls_certificate_verify_peers2(session, &status);
119     if (result < 0) {
120         fprintf(stderr, "gnutls_certificate_verify_peers2() failed\n");
121         gnutls_perror(result);
122         return EXIT_FAILURE;
123     }
124
125     if (status & GNUTLS_CERT_INVALID) {
126         fprintf(stderr, "certificate invalid\n");
127     }
128
129     /* Get proxy certificate. */
130     if ((result = gnutls_x509_crt_init(&cert)) < 0) {
131         fprintf(stderr, "gnutls_x509_crt_init() failed");
132         gnutls_perror(result);
133         return EXIT_FAILURE;
134     }
135
136     cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
137     if (cert_list == NULL) {
138         fprintf(stderr, "gnutls_certificate_get_peers() failed");
139         return EXIT_FAILURE;
140     }
141
142     if ((result = gnutls_x509_crt_import(cert, &cert_list[0],
143                                          GNUTLS_X509_FMT_DER)) < 0) {
144         fprintf(stderr, "gnutls_x509_crt_import() failed");
145         gnutls_perror(result);
146         return EXIT_FAILURE;
147     }
148
149     /* Check hostname. */
150     if (!gnutls_x509_crt_check_hostname(cert, argv[4])) {
151         fprintf(stderr, "hostname didn't match '%s'\n", argv[4]);
152         return EXIT_FAILURE;
153     }
154
155     gnutls_x509_crt_deinit(cert);
156
157     gnutls_bye(session, GNUTLS_SHUT_RDWR);
158     fclose(fd);
159
160     gnutls_deinit(session);
161     gnutls_certificate_free_credentials(xcred);
162     gnutls_global_deinit();
163
164     return EXIT_SUCCESS;
165 }
166
167
168 /* Copied from src/connection.c (and removed LOG_* stuff)! Don't modify. */
169
170 static int connect_to_host(const char *hostname, const char *port) {
171     struct addrinfo gai_hints;
172     struct addrinfo *gai_result;
173     int gai_return;
174
175     int server_socket;
176     struct addrinfo *server;
177
178     if (hostname == NULL || port == NULL) {
179         return -1;
180     }
181
182     /* Get IP of hostname server. */
183     memset(&gai_hints, 0, sizeof(gai_hints));
184     gai_hints.ai_family   = AF_UNSPEC;
185     gai_hints.ai_socktype = SOCK_STREAM;
186     gai_hints.ai_protocol = 0;
187     gai_hints.ai_flags    = AI_NUMERICSERV /* given port is numeric */
188                           | AI_ADDRCONFIG  /* supported by this computer */
189                           | AI_V4MAPPED;   /* support IPv4 through IPv6 */
190     gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
191     if (gai_return != 0) {
192         if (gai_return == EAI_SYSTEM) {
193             perror("connect_to_host(): getaddrinfo()");
194         } else {
195             fprintf(stderr, "connect_to_host(): getaddrinfo(): %s",
196                             gai_strerror(gai_return));
197         }
198         return -1;
199     }
200
201     /* Now try to connect to each server returned by getaddrinfo(), use the
202      * first successful connect. */
203     for (server = gai_result; server != NULL; server = server->ai_next) {
204         server_socket = socket(server->ai_family,
205                                server->ai_socktype,
206                                server->ai_protocol);
207         if (server_socket < 0) {
208             perror("connect_to_host(): socket(), trying next");
209             continue;
210         }
211
212         if (connect(server_socket, server->ai_addr, server->ai_addrlen) == 0) {
213             break;
214         }
215         perror("connect_to_host(): connect(), trying next");
216
217         close(server_socket);
218     }
219     /* Make sure we free the result from getaddrinfo(). */
220     freeaddrinfo(gai_result);
221
222     if (server == NULL) {
223         perror("connect_to_host(): no server found, abort");
224         return -1;
225     }
226
227     return server_socket;
228 }
229
230 static int read_http_request(FILE *client_fd, char *request, size_t length) {
231     char buffer[MAX_REQUEST_LINE];
232
233     if (fgets(request, (int)length, client_fd) == NULL) {
234         if (ferror(client_fd)) {
235             perror("read_http_request(): fgets()");
236             return -1;
237         }
238         /* EOF */
239         return -2;
240     }
241
242     while (fgets(buffer, sizeof(buffer), client_fd) != NULL) {
243         /* End of header. */
244         if (!strcmp(buffer, "\n") || !strcmp(buffer, "\r\n")) {
245             break;
246         }
247     }
248     if (ferror(client_fd)) {
249         perror("read_http_request(): fgets()");
250         return -1;
251     }
252
253     return 0;
254 }