2 * Simple GnuTLS client used for testing.
4 * Copyright (C) 2011-2013 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>
28 #include <sys/socket.h>
29 #include <sys/types.h>
32 #include <gnutls/gnutls.h>
33 #include <gnutls/x509.h>
36 #define MAX_REQUEST_LINE 4096
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);
42 int main (int argc, char *argv[]) {
45 char buffer[MAX_REQUEST_LINE];
49 gnutls_session_t session;
50 gnutls_certificate_credentials_t xcred;
52 gnutls_x509_crt_t cert;
53 const gnutls_datum_t *cert_list;
54 unsigned int cert_list_size;
56 if (argc != 5 && argc != 6) {
58 "Usage: %s <ca-file> <hostname> <port> <hostname-verify> "
59 "[<digest-authentication>]\n",
65 gnutls_certificate_allocate_credentials(&xcred);
67 gnutls_certificate_set_x509_trust_file(xcred,
68 argv[1], GNUTLS_X509_FMT_PEM);
70 gnutls_init(&session, GNUTLS_CLIENT);
71 gnutls_priority_set_direct(session, "NORMAL", NULL);
72 gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
74 server = connect_to_host("localhost", "4711");
78 fd = fdopen(server, "a+");
84 /* Talk to tlsproxy. */
85 fprintf(fd, "CONNECT %s:%s HTTP/1.0\r\n", argv[2], argv[3]);
87 fprintf(fd, "Proxy-Authorization: Basic %s\r\n", argv[5]);
91 if (read_http_request(fd, buffer, sizeof(buffer)) == -1) {
92 fprintf(stderr, "invalid proxy response\n");
96 printf("response: %s\n", buffer);
98 if (sscanf(buffer, "HTTP/1.0 %d", &response) != 1) {
99 fprintf(stderr, "invalid proxy response: %s\n", buffer);
103 if (response != 200) {
104 fprintf(stderr, "proxy failure\n");
108 gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t)server);
110 result = gnutls_handshake(session);
111 if (result != GNUTLS_E_SUCCESS) {
112 fprintf(stderr, "gnutls_handshake() failed\n");
113 gnutls_perror(result);
117 /* Verify the proxy certificate. */
118 result = gnutls_certificate_verify_peers2(session, &status);
120 fprintf(stderr, "gnutls_certificate_verify_peers2() failed\n");
121 gnutls_perror(result);
125 if (status & GNUTLS_CERT_INVALID) {
126 fprintf(stderr, "certificate invalid\n");
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);
136 cert_list = gnutls_certificate_get_peers(session, &cert_list_size);
137 if (cert_list == NULL) {
138 fprintf(stderr, "gnutls_certificate_get_peers() failed");
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);
149 /* Check hostname. */
150 if (!gnutls_x509_crt_check_hostname(cert, argv[4])) {
151 fprintf(stderr, "hostname didn't match '%s'\n", argv[4]);
155 gnutls_x509_crt_deinit(cert);
157 gnutls_bye(session, GNUTLS_SHUT_RDWR);
160 gnutls_deinit(session);
161 gnutls_certificate_free_credentials(xcred);
162 gnutls_global_deinit();
168 /* Copied from src/connection.c (and removed LOG_* stuff)! Don't modify. */
170 static int connect_to_host(const char *hostname, const char *port) {
171 struct addrinfo gai_hints;
172 struct addrinfo *gai_result;
176 struct addrinfo *server;
178 if (hostname == NULL || port == NULL) {
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()");
195 fprintf(stderr, "connect_to_host(): getaddrinfo(): %s",
196 gai_strerror(gai_return));
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,
206 server->ai_protocol);
207 if (server_socket < 0) {
208 perror("connect_to_host(): socket(), trying next");
212 if (connect(server_socket, server->ai_addr, server->ai_addrlen) == 0) {
215 perror("connect_to_host(): connect(), trying next");
217 close(server_socket);
219 /* Make sure we free the result from getaddrinfo(). */
220 freeaddrinfo(gai_result);
222 if (server == NULL) {
223 perror("connect_to_host(): no server found, abort");
227 return server_socket;
230 static int read_http_request(FILE *client_fd, char *request, size_t length) {
231 char buffer[MAX_REQUEST_LINE];
233 if (fgets(request, (int)length, client_fd) == NULL) {
234 if (ferror(client_fd)) {
235 perror("read_http_request(): fgets()");
242 while (fgets(buffer, sizeof(buffer), client_fd) != NULL) {
244 if (!strcmp(buffer, "\n") || !strcmp(buffer, "\r\n")) {
248 if (ferror(client_fd)) {
249 perror("read_http_request(): fgets()");