2 * Simple GnuTLS client used for testing.
4 * Copyright (C) 2011-2014 Simon Ruderich
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.
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.
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/>.
22 #include <arpa/inet.h>
30 #include <sys/socket.h>
31 #include <sys/types.h>
34 #include <gnutls/gnutls.h>
35 #include <gnutls/x509.h>
38 #define MAX_REQUEST_LINE 4096
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);
45 static void log_function_gnutls(int level, const char *string) {
47 fprintf(stderr, " => %s", string);
51 int main (int argc, char *argv[]) {
54 char buffer[MAX_REQUEST_LINE];
56 FILE *fd_read, *fd_write;
58 gnutls_session_t session;
59 gnutls_certificate_credentials_t xcred;
61 gnutls_x509_crt_t cert;
62 const gnutls_datum_t *cert_list;
63 unsigned int cert_list_size;
65 if (argc != 5 && argc != 6) {
67 "Usage: %s <ca-file> <hostname> <port> <hostname-verify> "
68 "[<digest-authentication>]\n",
74 gnutls_certificate_allocate_credentials(&xcred);
77 gnutls_global_set_log_level(10);
78 gnutls_global_set_log_function(log_function_gnutls);
81 gnutls_certificate_set_x509_trust_file(xcred,
82 argv[1], GNUTLS_X509_FMT_PEM);
84 gnutls_init(&session, GNUTLS_CLIENT);
85 gnutls_priority_set_direct(session, "NORMAL", NULL);
86 gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
88 server = connect_to_host("localhost", "4711");
92 if (fdopen_read_write(server, &fd_read, &fd_write) != 0) {
96 /* Talk to tlsproxy. */
97 fprintf(fd_write, "CONNECT %s:%s HTTP/1.0\r\n", argv[2], argv[3]);
99 fprintf(fd_write, "Proxy-Authorization: Basic %s\r\n", argv[5]);
101 fprintf(fd_write, "\r\n");
103 if (read_http_request(fd_read, buffer, sizeof(buffer)) != 0) {
104 fprintf(stderr, "invalid proxy response\n");
108 printf("response: %s\n", buffer);
110 if (sscanf(buffer, "HTTP/1.0 %d", &response) != 1) {
111 fprintf(stderr, "invalid proxy response: %s\n", buffer);
115 if (response != 200) {
116 fprintf(stderr, "proxy failure\n");
120 #ifdef HAVE_GNUTLS_TRANSPORT_SET_INT2
121 /* gnutls_transport_set_int() is a macro. */
122 gnutls_transport_set_int(session, server);
124 gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t)server);
127 result = gnutls_handshake(session);
128 if (result != GNUTLS_E_SUCCESS) {
129 fprintf(stderr, "gnutls_handshake() failed\n");
130 gnutls_perror(result);
134 /* Verify the proxy certificate. */
135 result = gnutls_certificate_verify_peers2(session, &status);
137 fprintf(stderr, "gnutls_certificate_verify_peers2() failed\n");
138 gnutls_perror(result);
142 if (status & GNUTLS_CERT_INVALID) {
143 fprintf(stderr, "certificate invalid\n");
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);
153 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
154 if (cert_list == NULL) {
155 fprintf(stderr, "gnutls_certificate_get_peers() failed");
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);
166 /* Check hostname. */
167 if (!gnutls_x509_crt_check_hostname(cert, argv[4])) {
168 fprintf(stderr, "hostname didn't match '%s'\n", argv[4]);
172 gnutls_x509_crt_deinit(cert);
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"));
179 gnutls_bye(session, GNUTLS_SHUT_RDWR);
183 gnutls_deinit(session);
184 gnutls_certificate_free_credentials(xcred);
185 gnutls_global_deinit();
191 /* Copied from src/connection.c (and removed LOG_* stuff)! Don't modify. */
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");
200 *write_fd = fdopen(dup(socket), "w");
201 if (*write_fd == NULL) {
202 perror("fdopen_read_write(): fdopen(\"w\") failed");
204 *read_fd = NULL; /* "tell" caller read_fd is already closed */
211 static int connect_to_host(const char *hostname, const char *port) {
212 struct addrinfo gai_hints;
213 struct addrinfo *gai_result;
217 struct addrinfo *server;
219 if (hostname == NULL || port == NULL) {
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 */
230 | AI_ADDRCONFIG /* supported by this computer */
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()");
238 fprintf(stderr, "connect_to_host(): getaddrinfo(): %s",
239 gai_strerror(gai_return));
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,
249 server->ai_protocol);
250 if (server_socket < 0) {
251 perror("connect_to_host(): socket(), trying next");
255 if (connect(server_socket, server->ai_addr, server->ai_addrlen) == 0) {
258 perror("connect_to_host(): connect(), trying next");
260 close(server_socket);
262 /* Make sure we free the result from getaddrinfo(). */
263 freeaddrinfo(gai_result);
265 if (server == NULL) {
266 perror("connect_to_host(): no server found, abort");
270 return server_socket;
273 static int read_http_request(FILE *client_fd, char *request, size_t length) {
274 char buffer[MAX_REQUEST_LINE];
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()");
286 while (fgets(buffer, sizeof(buffer), client_fd) != NULL) {
288 if (!strcmp(buffer, "\n") || !strcmp(buffer, "\r\n")) {
292 if (ferror(client_fd)) {
293 perror("read_http_request(): fgets()");
295 } else if (feof(client_fd)) {