1*0957b409SSimon J. Gerraty /*
2*0957b409SSimon J. Gerraty  * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3*0957b409SSimon J. Gerraty  *
4*0957b409SSimon J. Gerraty  * Permission is hereby granted, free of charge, to any person obtaining
5*0957b409SSimon J. Gerraty  * a copy of this software and associated documentation files (the
6*0957b409SSimon J. Gerraty  * "Software"), to deal in the Software without restriction, including
7*0957b409SSimon J. Gerraty  * without limitation the rights to use, copy, modify, merge, publish,
8*0957b409SSimon J. Gerraty  * distribute, sublicense, and/or sell copies of the Software, and to
9*0957b409SSimon J. Gerraty  * permit persons to whom the Software is furnished to do so, subject to
10*0957b409SSimon J. Gerraty  * the following conditions:
11*0957b409SSimon J. Gerraty  *
12*0957b409SSimon J. Gerraty  * The above copyright notice and this permission notice shall be
13*0957b409SSimon J. Gerraty  * included in all copies or substantial portions of the Software.
14*0957b409SSimon J. Gerraty  *
15*0957b409SSimon J. Gerraty  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*0957b409SSimon J. Gerraty  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*0957b409SSimon J. Gerraty  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18*0957b409SSimon J. Gerraty  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19*0957b409SSimon J. Gerraty  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20*0957b409SSimon J. Gerraty  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21*0957b409SSimon J. Gerraty  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*0957b409SSimon J. Gerraty  * SOFTWARE.
23*0957b409SSimon J. Gerraty  */
24*0957b409SSimon J. Gerraty 
25*0957b409SSimon J. Gerraty #include <stdio.h>
26*0957b409SSimon J. Gerraty #include <stdlib.h>
27*0957b409SSimon J. Gerraty #include <string.h>
28*0957b409SSimon J. Gerraty #include <stdint.h>
29*0957b409SSimon J. Gerraty #include <errno.h>
30*0957b409SSimon J. Gerraty #include <signal.h>
31*0957b409SSimon J. Gerraty 
32*0957b409SSimon J. Gerraty #include <sys/types.h>
33*0957b409SSimon J. Gerraty #include <sys/socket.h>
34*0957b409SSimon J. Gerraty #include <netdb.h>
35*0957b409SSimon J. Gerraty #include <netinet/in.h>
36*0957b409SSimon J. Gerraty #include <arpa/inet.h>
37*0957b409SSimon J. Gerraty #include <unistd.h>
38*0957b409SSimon J. Gerraty 
39*0957b409SSimon J. Gerraty #include "bearssl.h"
40*0957b409SSimon J. Gerraty 
41*0957b409SSimon J. Gerraty /*
42*0957b409SSimon J. Gerraty  * Connect to the specified host and port. The connected socket is
43*0957b409SSimon J. Gerraty  * returned, or -1 on error.
44*0957b409SSimon J. Gerraty  */
45*0957b409SSimon J. Gerraty static int
host_connect(const char * host,const char * port)46*0957b409SSimon J. Gerraty host_connect(const char *host, const char *port)
47*0957b409SSimon J. Gerraty {
48*0957b409SSimon J. Gerraty 	struct addrinfo hints, *si, *p;
49*0957b409SSimon J. Gerraty 	int fd;
50*0957b409SSimon J. Gerraty 	int err;
51*0957b409SSimon J. Gerraty 
52*0957b409SSimon J. Gerraty 	memset(&hints, 0, sizeof hints);
53*0957b409SSimon J. Gerraty 	hints.ai_family = PF_UNSPEC;
54*0957b409SSimon J. Gerraty 	hints.ai_socktype = SOCK_STREAM;
55*0957b409SSimon J. Gerraty 	err = getaddrinfo(host, port, &hints, &si);
56*0957b409SSimon J. Gerraty 	if (err != 0) {
57*0957b409SSimon J. Gerraty 		fprintf(stderr, "ERROR: getaddrinfo(): %s\n",
58*0957b409SSimon J. Gerraty 			gai_strerror(err));
59*0957b409SSimon J. Gerraty 		return -1;
60*0957b409SSimon J. Gerraty 	}
61*0957b409SSimon J. Gerraty 	fd = -1;
62*0957b409SSimon J. Gerraty 	for (p = si; p != NULL; p = p->ai_next) {
63*0957b409SSimon J. Gerraty 		struct sockaddr *sa;
64*0957b409SSimon J. Gerraty 		void *addr;
65*0957b409SSimon J. Gerraty 		char tmp[INET6_ADDRSTRLEN + 50];
66*0957b409SSimon J. Gerraty 
67*0957b409SSimon J. Gerraty 		sa = (struct sockaddr *)p->ai_addr;
68*0957b409SSimon J. Gerraty 		if (sa->sa_family == AF_INET) {
69*0957b409SSimon J. Gerraty 			addr = &((struct sockaddr_in *)sa)->sin_addr;
70*0957b409SSimon J. Gerraty 		} else if (sa->sa_family == AF_INET6) {
71*0957b409SSimon J. Gerraty 			addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
72*0957b409SSimon J. Gerraty 		} else {
73*0957b409SSimon J. Gerraty 			addr = NULL;
74*0957b409SSimon J. Gerraty 		}
75*0957b409SSimon J. Gerraty 		if (addr != NULL) {
76*0957b409SSimon J. Gerraty 			inet_ntop(p->ai_family, addr, tmp, sizeof tmp);
77*0957b409SSimon J. Gerraty 		} else {
78*0957b409SSimon J. Gerraty 			sprintf(tmp, "<unknown family: %d>",
79*0957b409SSimon J. Gerraty 				(int)sa->sa_family);
80*0957b409SSimon J. Gerraty 		}
81*0957b409SSimon J. Gerraty 		fprintf(stderr, "connecting to: %s\n", tmp);
82*0957b409SSimon J. Gerraty 		fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
83*0957b409SSimon J. Gerraty 		if (fd < 0) {
84*0957b409SSimon J. Gerraty 			perror("socket()");
85*0957b409SSimon J. Gerraty 			continue;
86*0957b409SSimon J. Gerraty 		}
87*0957b409SSimon J. Gerraty 		if (connect(fd, p->ai_addr, p->ai_addrlen) < 0) {
88*0957b409SSimon J. Gerraty 			perror("connect()");
89*0957b409SSimon J. Gerraty 			close(fd);
90*0957b409SSimon J. Gerraty 			continue;
91*0957b409SSimon J. Gerraty 		}
92*0957b409SSimon J. Gerraty 		break;
93*0957b409SSimon J. Gerraty 	}
94*0957b409SSimon J. Gerraty 	if (p == NULL) {
95*0957b409SSimon J. Gerraty 		freeaddrinfo(si);
96*0957b409SSimon J. Gerraty 		fprintf(stderr, "ERROR: failed to connect\n");
97*0957b409SSimon J. Gerraty 		return -1;
98*0957b409SSimon J. Gerraty 	}
99*0957b409SSimon J. Gerraty 	freeaddrinfo(si);
100*0957b409SSimon J. Gerraty 	fprintf(stderr, "connected.\n");
101*0957b409SSimon J. Gerraty 	return fd;
102*0957b409SSimon J. Gerraty }
103*0957b409SSimon J. Gerraty 
104*0957b409SSimon J. Gerraty /*
105*0957b409SSimon J. Gerraty  * Low-level data read callback for the simplified SSL I/O API.
106*0957b409SSimon J. Gerraty  */
107*0957b409SSimon J. Gerraty static int
sock_read(void * ctx,unsigned char * buf,size_t len)108*0957b409SSimon J. Gerraty sock_read(void *ctx, unsigned char *buf, size_t len)
109*0957b409SSimon J. Gerraty {
110*0957b409SSimon J. Gerraty 	for (;;) {
111*0957b409SSimon J. Gerraty 		ssize_t rlen;
112*0957b409SSimon J. Gerraty 
113*0957b409SSimon J. Gerraty 		rlen = read(*(int *)ctx, buf, len);
114*0957b409SSimon J. Gerraty 		if (rlen <= 0) {
115*0957b409SSimon J. Gerraty 			if (rlen < 0 && errno == EINTR) {
116*0957b409SSimon J. Gerraty 				continue;
117*0957b409SSimon J. Gerraty 			}
118*0957b409SSimon J. Gerraty 			return -1;
119*0957b409SSimon J. Gerraty 		}
120*0957b409SSimon J. Gerraty 		return (int)rlen;
121*0957b409SSimon J. Gerraty 	}
122*0957b409SSimon J. Gerraty }
123*0957b409SSimon J. Gerraty 
124*0957b409SSimon J. Gerraty /*
125*0957b409SSimon J. Gerraty  * Low-level data write callback for the simplified SSL I/O API.
126*0957b409SSimon J. Gerraty  */
127*0957b409SSimon J. Gerraty static int
sock_write(void * ctx,const unsigned char * buf,size_t len)128*0957b409SSimon J. Gerraty sock_write(void *ctx, const unsigned char *buf, size_t len)
129*0957b409SSimon J. Gerraty {
130*0957b409SSimon J. Gerraty 	for (;;) {
131*0957b409SSimon J. Gerraty 		ssize_t wlen;
132*0957b409SSimon J. Gerraty 
133*0957b409SSimon J. Gerraty 		wlen = write(*(int *)ctx, buf, len);
134*0957b409SSimon J. Gerraty 		if (wlen <= 0) {
135*0957b409SSimon J. Gerraty 			if (wlen < 0 && errno == EINTR) {
136*0957b409SSimon J. Gerraty 				continue;
137*0957b409SSimon J. Gerraty 			}
138*0957b409SSimon J. Gerraty 			return -1;
139*0957b409SSimon J. Gerraty 		}
140*0957b409SSimon J. Gerraty 		return (int)wlen;
141*0957b409SSimon J. Gerraty 	}
142*0957b409SSimon J. Gerraty }
143*0957b409SSimon J. Gerraty 
144*0957b409SSimon J. Gerraty /*
145*0957b409SSimon J. Gerraty  * The hardcoded trust anchors. These are the two DN + public key that
146*0957b409SSimon J. Gerraty  * correspond to the self-signed certificates cert-root-rsa.pem and
147*0957b409SSimon J. Gerraty  * cert-root-ec.pem.
148*0957b409SSimon J. Gerraty  *
149*0957b409SSimon J. Gerraty  * C code for hardcoded trust anchors can be generated with the "brssl"
150*0957b409SSimon J. Gerraty  * command-line tool (with the "ta" command).
151*0957b409SSimon J. Gerraty  */
152*0957b409SSimon J. Gerraty 
153*0957b409SSimon J. Gerraty static const unsigned char TA0_DN[] = {
154*0957b409SSimon J. Gerraty 	0x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
155*0957b409SSimon J. Gerraty 	0x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
156*0957b409SSimon J. Gerraty 	0x13, 0x04, 0x52, 0x6F, 0x6F, 0x74
157*0957b409SSimon J. Gerraty };
158*0957b409SSimon J. Gerraty 
159*0957b409SSimon J. Gerraty static const unsigned char TA0_RSA_N[] = {
160*0957b409SSimon J. Gerraty 	0xB6, 0xD9, 0x34, 0xD4, 0x50, 0xFD, 0xB3, 0xAF, 0x7A, 0x73, 0xF1, 0xCE,
161*0957b409SSimon J. Gerraty 	0x38, 0xBF, 0x5D, 0x6F, 0x45, 0xE1, 0xFD, 0x4E, 0xB1, 0x98, 0xC6, 0x60,
162*0957b409SSimon J. Gerraty 	0x83, 0x26, 0xD2, 0x17, 0xD1, 0xC5, 0xB7, 0x9A, 0xA3, 0xC1, 0xDE, 0x63,
163*0957b409SSimon J. Gerraty 	0x39, 0x97, 0x9C, 0xF0, 0x5E, 0x5C, 0xC8, 0x1C, 0x17, 0xB9, 0x88, 0x19,
164*0957b409SSimon J. Gerraty 	0x6D, 0xF0, 0xB6, 0x2E, 0x30, 0x50, 0xA1, 0x54, 0x6E, 0x93, 0xC0, 0xDB,
165*0957b409SSimon J. Gerraty 	0xCF, 0x30, 0xCB, 0x9F, 0x1E, 0x27, 0x79, 0xF1, 0xC3, 0x99, 0x52, 0x35,
166*0957b409SSimon J. Gerraty 	0xAA, 0x3D, 0xB6, 0xDF, 0xB0, 0xAD, 0x7C, 0xCB, 0x49, 0xCD, 0xC0, 0xED,
167*0957b409SSimon J. Gerraty 	0xE7, 0x66, 0x10, 0x2A, 0xE9, 0xCE, 0x28, 0x1F, 0x21, 0x50, 0xFA, 0x77,
168*0957b409SSimon J. Gerraty 	0x4C, 0x2D, 0xDA, 0xEF, 0x3C, 0x58, 0xEB, 0x4E, 0xBF, 0xCE, 0xE9, 0xFB,
169*0957b409SSimon J. Gerraty 	0x1A, 0xDA, 0xA3, 0x83, 0xA3, 0xCD, 0xA3, 0xCA, 0x93, 0x80, 0xDC, 0xDA,
170*0957b409SSimon J. Gerraty 	0xF3, 0x17, 0xCC, 0x7A, 0xAB, 0x33, 0x80, 0x9C, 0xB2, 0xD4, 0x7F, 0x46,
171*0957b409SSimon J. Gerraty 	0x3F, 0xC5, 0x3C, 0xDC, 0x61, 0x94, 0xB7, 0x27, 0x29, 0x6E, 0x2A, 0xBC,
172*0957b409SSimon J. Gerraty 	0x5B, 0x09, 0x36, 0xD4, 0xC6, 0x3B, 0x0D, 0xEB, 0xBE, 0xCE, 0xDB, 0x1D,
173*0957b409SSimon J. Gerraty 	0x1C, 0xBC, 0x10, 0x6A, 0x71, 0x71, 0xB3, 0xF2, 0xCA, 0x28, 0x9A, 0x77,
174*0957b409SSimon J. Gerraty 	0xF2, 0x8A, 0xEC, 0x42, 0xEF, 0xB1, 0x4A, 0x8E, 0xE2, 0xF2, 0x1A, 0x32,
175*0957b409SSimon J. Gerraty 	0x2A, 0xCD, 0xC0, 0xA6, 0x46, 0x2C, 0x9A, 0xC2, 0x85, 0x37, 0x91, 0x7F,
176*0957b409SSimon J. Gerraty 	0x46, 0xA1, 0x93, 0x81, 0xA1, 0x74, 0x66, 0xDF, 0xBA, 0xB3, 0x39, 0x20,
177*0957b409SSimon J. Gerraty 	0x91, 0x93, 0xFA, 0x1D, 0xA1, 0xA8, 0x85, 0xE7, 0xE4, 0xF9, 0x07, 0xF6,
178*0957b409SSimon J. Gerraty 	0x10, 0xF6, 0xA8, 0x27, 0x01, 0xB6, 0x7F, 0x12, 0xC3, 0x40, 0xC3, 0xC9,
179*0957b409SSimon J. Gerraty 	0xE2, 0xB0, 0xAB, 0x49, 0x18, 0x3A, 0x64, 0xB6, 0x59, 0xB7, 0x95, 0xB5,
180*0957b409SSimon J. Gerraty 	0x96, 0x36, 0xDF, 0x22, 0x69, 0xAA, 0x72, 0x6A, 0x54, 0x4E, 0x27, 0x29,
181*0957b409SSimon J. Gerraty 	0xA3, 0x0E, 0x97, 0x15
182*0957b409SSimon J. Gerraty };
183*0957b409SSimon J. Gerraty 
184*0957b409SSimon J. Gerraty static const unsigned char TA0_RSA_E[] = {
185*0957b409SSimon J. Gerraty 	0x01, 0x00, 0x01
186*0957b409SSimon J. Gerraty };
187*0957b409SSimon J. Gerraty 
188*0957b409SSimon J. Gerraty static const unsigned char TA1_DN[] = {
189*0957b409SSimon J. Gerraty 	0x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
190*0957b409SSimon J. Gerraty 	0x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
191*0957b409SSimon J. Gerraty 	0x13, 0x04, 0x52, 0x6F, 0x6F, 0x74
192*0957b409SSimon J. Gerraty };
193*0957b409SSimon J. Gerraty 
194*0957b409SSimon J. Gerraty static const unsigned char TA1_EC_Q[] = {
195*0957b409SSimon J. Gerraty 	0x04, 0x71, 0x74, 0xBA, 0xAB, 0xB9, 0x30, 0x2E, 0x81, 0xD5, 0xE5, 0x57,
196*0957b409SSimon J. Gerraty 	0xF9, 0xF3, 0x20, 0x68, 0x0C, 0x9C, 0xF9, 0x64, 0xDB, 0xB4, 0x20, 0x0D,
197*0957b409SSimon J. Gerraty 	0x6D, 0xEA, 0x40, 0xD0, 0x4A, 0x6E, 0x42, 0xFD, 0xB6, 0x9A, 0x68, 0x25,
198*0957b409SSimon J. Gerraty 	0x44, 0xF6, 0xDF, 0x7B, 0xC4, 0xFC, 0xDE, 0xDD, 0x7B, 0xBB, 0xC5, 0xDB,
199*0957b409SSimon J. Gerraty 	0x7C, 0x76, 0x3F, 0x41, 0x66, 0x40, 0x6E, 0xDB, 0xA7, 0x87, 0xC2, 0xE5,
200*0957b409SSimon J. Gerraty 	0xD8, 0xC5, 0xF3, 0x7F, 0x8D
201*0957b409SSimon J. Gerraty };
202*0957b409SSimon J. Gerraty 
203*0957b409SSimon J. Gerraty static const br_x509_trust_anchor TAs[2] = {
204*0957b409SSimon J. Gerraty 	{
205*0957b409SSimon J. Gerraty 		{ (unsigned char *)TA0_DN, sizeof TA0_DN },
206*0957b409SSimon J. Gerraty 		BR_X509_TA_CA,
207*0957b409SSimon J. Gerraty 		{
208*0957b409SSimon J. Gerraty 			BR_KEYTYPE_RSA,
209*0957b409SSimon J. Gerraty 			{ .rsa = {
210*0957b409SSimon J. Gerraty 				(unsigned char *)TA0_RSA_N, sizeof TA0_RSA_N,
211*0957b409SSimon J. Gerraty 				(unsigned char *)TA0_RSA_E, sizeof TA0_RSA_E,
212*0957b409SSimon J. Gerraty 			} }
213*0957b409SSimon J. Gerraty 		}
214*0957b409SSimon J. Gerraty 	},
215*0957b409SSimon J. Gerraty 	{
216*0957b409SSimon J. Gerraty 		{ (unsigned char *)TA1_DN, sizeof TA1_DN },
217*0957b409SSimon J. Gerraty 		BR_X509_TA_CA,
218*0957b409SSimon J. Gerraty 		{
219*0957b409SSimon J. Gerraty 			BR_KEYTYPE_EC,
220*0957b409SSimon J. Gerraty 			{ .ec = {
221*0957b409SSimon J. Gerraty 				BR_EC_secp256r1,
222*0957b409SSimon J. Gerraty 				(unsigned char *)TA1_EC_Q, sizeof TA1_EC_Q,
223*0957b409SSimon J. Gerraty 			} }
224*0957b409SSimon J. Gerraty 		}
225*0957b409SSimon J. Gerraty 	}
226*0957b409SSimon J. Gerraty };
227*0957b409SSimon J. Gerraty 
228*0957b409SSimon J. Gerraty #define TAs_NUM   2
229*0957b409SSimon J. Gerraty 
230*0957b409SSimon J. Gerraty /*
231*0957b409SSimon J. Gerraty  * Main program: this is a simple program that expects 2 or 3 arguments.
232*0957b409SSimon J. Gerraty  * The first two arguments are a hostname and a port; the program will
233*0957b409SSimon J. Gerraty  * open a SSL connection with that server and port. It will then send
234*0957b409SSimon J. Gerraty  * a simple HTTP GET request, using the third argument as target path
235*0957b409SSimon J. Gerraty  * ("/" is used as path if no third argument was provided). The HTTP
236*0957b409SSimon J. Gerraty  * response, complete with header and contents, is received and written
237*0957b409SSimon J. Gerraty  * on stdout.
238*0957b409SSimon J. Gerraty  */
239*0957b409SSimon J. Gerraty int
main(int argc,char * argv[])240*0957b409SSimon J. Gerraty main(int argc, char *argv[])
241*0957b409SSimon J. Gerraty {
242*0957b409SSimon J. Gerraty 	const char *host, *port, *path;
243*0957b409SSimon J. Gerraty 	int fd;
244*0957b409SSimon J. Gerraty 	br_ssl_client_context sc;
245*0957b409SSimon J. Gerraty 	br_x509_minimal_context xc;
246*0957b409SSimon J. Gerraty 	unsigned char iobuf[BR_SSL_BUFSIZE_BIDI];
247*0957b409SSimon J. Gerraty 	br_sslio_context ioc;
248*0957b409SSimon J. Gerraty 
249*0957b409SSimon J. Gerraty 	/*
250*0957b409SSimon J. Gerraty 	 * Parse command-line argument: host, port, and path. The path
251*0957b409SSimon J. Gerraty 	 * is optional; if absent, "/" is used.
252*0957b409SSimon J. Gerraty 	 */
253*0957b409SSimon J. Gerraty 	if (argc < 3 || argc > 4) {
254*0957b409SSimon J. Gerraty 		return EXIT_FAILURE;
255*0957b409SSimon J. Gerraty 	}
256*0957b409SSimon J. Gerraty 	host = argv[1];
257*0957b409SSimon J. Gerraty 	port = argv[2];
258*0957b409SSimon J. Gerraty 	if (argc == 4) {
259*0957b409SSimon J. Gerraty 		path = argv[3];
260*0957b409SSimon J. Gerraty 	} else {
261*0957b409SSimon J. Gerraty 		path = "/";
262*0957b409SSimon J. Gerraty 	}
263*0957b409SSimon J. Gerraty 
264*0957b409SSimon J. Gerraty 	/*
265*0957b409SSimon J. Gerraty 	 * Ignore SIGPIPE to avoid crashing in case of abrupt socket close.
266*0957b409SSimon J. Gerraty 	 */
267*0957b409SSimon J. Gerraty 	signal(SIGPIPE, SIG_IGN);
268*0957b409SSimon J. Gerraty 
269*0957b409SSimon J. Gerraty 	/*
270*0957b409SSimon J. Gerraty 	 * Open the socket to the target server.
271*0957b409SSimon J. Gerraty 	 */
272*0957b409SSimon J. Gerraty 	fd = host_connect(host, port);
273*0957b409SSimon J. Gerraty 	if (fd < 0) {
274*0957b409SSimon J. Gerraty 		return EXIT_FAILURE;
275*0957b409SSimon J. Gerraty 	}
276*0957b409SSimon J. Gerraty 
277*0957b409SSimon J. Gerraty 	/*
278*0957b409SSimon J. Gerraty 	 * Initialise the client context:
279*0957b409SSimon J. Gerraty 	 * -- Use the "full" profile (all supported algorithms).
280*0957b409SSimon J. Gerraty 	 * -- The provided X.509 validation engine is initialised, with
281*0957b409SSimon J. Gerraty 	 *    the hardcoded trust anchor.
282*0957b409SSimon J. Gerraty 	 */
283*0957b409SSimon J. Gerraty 	br_ssl_client_init_full(&sc, &xc, TAs, TAs_NUM);
284*0957b409SSimon J. Gerraty 
285*0957b409SSimon J. Gerraty 	/*
286*0957b409SSimon J. Gerraty 	 * Set the I/O buffer to the provided array. We allocated a
287*0957b409SSimon J. Gerraty 	 * buffer large enough for full-duplex behaviour with all
288*0957b409SSimon J. Gerraty 	 * allowed sizes of SSL records, hence we set the last argument
289*0957b409SSimon J. Gerraty 	 * to 1 (which means "split the buffer into separate input and
290*0957b409SSimon J. Gerraty 	 * output areas").
291*0957b409SSimon J. Gerraty 	 */
292*0957b409SSimon J. Gerraty 	br_ssl_engine_set_buffer(&sc.eng, iobuf, sizeof iobuf, 1);
293*0957b409SSimon J. Gerraty 
294*0957b409SSimon J. Gerraty 	/*
295*0957b409SSimon J. Gerraty 	 * Reset the client context, for a new handshake. We provide the
296*0957b409SSimon J. Gerraty 	 * target host name: it will be used for the SNI extension. The
297*0957b409SSimon J. Gerraty 	 * last parameter is 0: we are not trying to resume a session.
298*0957b409SSimon J. Gerraty 	 */
299*0957b409SSimon J. Gerraty 	br_ssl_client_reset(&sc, host, 0);
300*0957b409SSimon J. Gerraty 
301*0957b409SSimon J. Gerraty 	/*
302*0957b409SSimon J. Gerraty 	 * Initialise the simplified I/O wrapper context, to use our
303*0957b409SSimon J. Gerraty 	 * SSL client context, and the two callbacks for socket I/O.
304*0957b409SSimon J. Gerraty 	 */
305*0957b409SSimon J. Gerraty 	br_sslio_init(&ioc, &sc.eng, sock_read, &fd, sock_write, &fd);
306*0957b409SSimon J. Gerraty 
307*0957b409SSimon J. Gerraty 	/*
308*0957b409SSimon J. Gerraty 	 * Note that while the context has, at that point, already
309*0957b409SSimon J. Gerraty 	 * assembled the ClientHello to send, nothing happened on the
310*0957b409SSimon J. Gerraty 	 * network yet. Real I/O will occur only with the next call.
311*0957b409SSimon J. Gerraty 	 *
312*0957b409SSimon J. Gerraty 	 * We write our simple HTTP request. We could test each call
313*0957b409SSimon J. Gerraty 	 * for an error (-1), but this is not strictly necessary, since
314*0957b409SSimon J. Gerraty 	 * the error state "sticks": if the context fails for any reason
315*0957b409SSimon J. Gerraty 	 * (e.g. bad server certificate), then it will remain in failed
316*0957b409SSimon J. Gerraty 	 * state and all subsequent calls will return -1 as well.
317*0957b409SSimon J. Gerraty 	 */
318*0957b409SSimon J. Gerraty 	br_sslio_write_all(&ioc, "GET ", 4);
319*0957b409SSimon J. Gerraty 	br_sslio_write_all(&ioc, path, strlen(path));
320*0957b409SSimon J. Gerraty 	br_sslio_write_all(&ioc, " HTTP/1.0\r\nHost: ", 17);
321*0957b409SSimon J. Gerraty 	br_sslio_write_all(&ioc, host, strlen(host));
322*0957b409SSimon J. Gerraty 	br_sslio_write_all(&ioc, "\r\n\r\n", 4);
323*0957b409SSimon J. Gerraty 
324*0957b409SSimon J. Gerraty 	/*
325*0957b409SSimon J. Gerraty 	 * SSL is a buffered protocol: we make sure that all our request
326*0957b409SSimon J. Gerraty 	 * bytes are sent onto the wire.
327*0957b409SSimon J. Gerraty 	 */
328*0957b409SSimon J. Gerraty 	br_sslio_flush(&ioc);
329*0957b409SSimon J. Gerraty 
330*0957b409SSimon J. Gerraty 	/*
331*0957b409SSimon J. Gerraty 	 * Read the server's response. We use here a small 512-byte buffer,
332*0957b409SSimon J. Gerraty 	 * but most of the buffering occurs in the client context: the
333*0957b409SSimon J. Gerraty 	 * server will send full records (up to 16384 bytes worth of data
334*0957b409SSimon J. Gerraty 	 * each), and the client context buffers one full record at a time.
335*0957b409SSimon J. Gerraty 	 */
336*0957b409SSimon J. Gerraty 	for (;;) {
337*0957b409SSimon J. Gerraty 		int rlen;
338*0957b409SSimon J. Gerraty 		unsigned char tmp[512];
339*0957b409SSimon J. Gerraty 
340*0957b409SSimon J. Gerraty 		rlen = br_sslio_read(&ioc, tmp, sizeof tmp);
341*0957b409SSimon J. Gerraty 		if (rlen < 0) {
342*0957b409SSimon J. Gerraty 			break;
343*0957b409SSimon J. Gerraty 		}
344*0957b409SSimon J. Gerraty 		fwrite(tmp, 1, rlen, stdout);
345*0957b409SSimon J. Gerraty 	}
346*0957b409SSimon J. Gerraty 
347*0957b409SSimon J. Gerraty 	/*
348*0957b409SSimon J. Gerraty 	 * Close the socket.
349*0957b409SSimon J. Gerraty 	 */
350*0957b409SSimon J. Gerraty 	close(fd);
351*0957b409SSimon J. Gerraty 
352*0957b409SSimon J. Gerraty 	/*
353*0957b409SSimon J. Gerraty 	 * Check whether we closed properly or not. If the engine is
354*0957b409SSimon J. Gerraty 	 * closed, then its error status allows to distinguish between
355*0957b409SSimon J. Gerraty 	 * a normal closure and a SSL error.
356*0957b409SSimon J. Gerraty 	 *
357*0957b409SSimon J. Gerraty 	 * If the engine is NOT closed, then this means that the
358*0957b409SSimon J. Gerraty 	 * underlying network socket was closed or failed in some way.
359*0957b409SSimon J. Gerraty 	 * Note that many Web servers out there do not properly close
360*0957b409SSimon J. Gerraty 	 * their SSL connections (they don't send a close_notify alert),
361*0957b409SSimon J. Gerraty 	 * which will be reported here as "socket closed without proper
362*0957b409SSimon J. Gerraty 	 * SSL termination".
363*0957b409SSimon J. Gerraty 	 */
364*0957b409SSimon J. Gerraty 	if (br_ssl_engine_current_state(&sc.eng) == BR_SSL_CLOSED) {
365*0957b409SSimon J. Gerraty 		int err;
366*0957b409SSimon J. Gerraty 
367*0957b409SSimon J. Gerraty 		err = br_ssl_engine_last_error(&sc.eng);
368*0957b409SSimon J. Gerraty 		if (err == 0) {
369*0957b409SSimon J. Gerraty 			fprintf(stderr, "closed.\n");
370*0957b409SSimon J. Gerraty 			return EXIT_SUCCESS;
371*0957b409SSimon J. Gerraty 		} else {
372*0957b409SSimon J. Gerraty 			fprintf(stderr, "SSL error %d\n", err);
373*0957b409SSimon J. Gerraty 			return EXIT_FAILURE;
374*0957b409SSimon J. Gerraty 		}
375*0957b409SSimon J. Gerraty 	} else {
376*0957b409SSimon J. Gerraty 		fprintf(stderr,
377*0957b409SSimon J. Gerraty 			"socket closed without proper SSL termination\n");
378*0957b409SSimon J. Gerraty 		return EXIT_FAILURE;
379*0957b409SSimon J. Gerraty 	}
380*0957b409SSimon J. Gerraty }
381