2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
42 * Connect to the specified host and port. The connected socket is
43 * returned, or -1 on error.
46 host_connect(const char *host, const char *port)
48 struct addrinfo hints, *si, *p;
52 memset(&hints, 0, sizeof hints);
53 hints.ai_family = PF_UNSPEC;
54 hints.ai_socktype = SOCK_STREAM;
55 err = getaddrinfo(host, port, &hints, &si);
57 fprintf(stderr, "ERROR: getaddrinfo(): %s\n",
62 for (p = si; p != NULL; p = p->ai_next) {
65 char tmp[INET6_ADDRSTRLEN + 50];
67 sa = (struct sockaddr *)p->ai_addr;
68 if (sa->sa_family == AF_INET) {
69 addr = &((struct sockaddr_in *)sa)->sin_addr;
70 } else if (sa->sa_family == AF_INET6) {
71 addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
76 inet_ntop(p->ai_family, addr, tmp, sizeof tmp);
78 sprintf(tmp, "<unknown family: %d>",
81 fprintf(stderr, "connecting to: %s\n", tmp);
82 fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
87 if (connect(fd, p->ai_addr, p->ai_addrlen) < 0) {
96 fprintf(stderr, "ERROR: failed to connect\n");
100 fprintf(stderr, "connected.\n");
105 * Low-level data read callback for the simplified SSL I/O API.
108 sock_read(void *ctx, unsigned char *buf, size_t len)
113 rlen = read(*(int *)ctx, buf, len);
115 if (rlen < 0 && errno == EINTR) {
125 * Low-level data write callback for the simplified SSL I/O API.
128 sock_write(void *ctx, const unsigned char *buf, size_t len)
133 wlen = write(*(int *)ctx, buf, len);
135 if (wlen < 0 && errno == EINTR) {
145 * The hardcoded trust anchors. These are the two DN + public key that
146 * correspond to the self-signed certificates cert-root-rsa.pem and
149 * C code for hardcoded trust anchors can be generated with the "brssl"
150 * command-line tool (with the "ta" command).
153 static const unsigned char TA0_DN[] = {
154 0x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
155 0x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
156 0x13, 0x04, 0x52, 0x6F, 0x6F, 0x74
159 static const unsigned char TA0_RSA_N[] = {
160 0xB6, 0xD9, 0x34, 0xD4, 0x50, 0xFD, 0xB3, 0xAF, 0x7A, 0x73, 0xF1, 0xCE,
161 0x38, 0xBF, 0x5D, 0x6F, 0x45, 0xE1, 0xFD, 0x4E, 0xB1, 0x98, 0xC6, 0x60,
162 0x83, 0x26, 0xD2, 0x17, 0xD1, 0xC5, 0xB7, 0x9A, 0xA3, 0xC1, 0xDE, 0x63,
163 0x39, 0x97, 0x9C, 0xF0, 0x5E, 0x5C, 0xC8, 0x1C, 0x17, 0xB9, 0x88, 0x19,
164 0x6D, 0xF0, 0xB6, 0x2E, 0x30, 0x50, 0xA1, 0x54, 0x6E, 0x93, 0xC0, 0xDB,
165 0xCF, 0x30, 0xC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x95, 0xB5,
180 0x96, 0x36, 0xDF, 0x22, 0x69, 0xAA, 0x72, 0x6A, 0x54, 0x4E, 0x27, 0x29,
181 0xA3, 0x0E, 0x97, 0x15
184 static const unsigned char TA0_RSA_E[] = {
188 static const unsigned char TA1_DN[] = {
189 0x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
190 0x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
191 0x13, 0x04, 0x52, 0x6F, 0x6F, 0x74
194 static const unsigned char TA1_EC_Q[] = {
195 0x04, 0x71, 0x74, 0xBA, 0xAB, 0xB9, 0x30, 0x2E, 0x81, 0xD5, 0xE5, 0x57,
196 0xF9, 0xF3, 0x20, 0x68, 0x0C, 0x9C, 0xF9, 0x64, 0xDB, 0xB4, 0x20, 0x0D,
197 0x6D, 0xEA, 0x40, 0xD0, 0x4A, 0x6E, 0x42, 0xFD, 0xB6, 0x9A, 0x68, 0x25,
198 0x44, 0xF6, 0xDF, 0x7B, 0xC4, 0xFC, 0xDE, 0xDD, 0x7B, 0xBB, 0xC5, 0xDB,
199 0x7C, 0x76, 0x3F, 0x41, 0x66, 0x40, 0x6E, 0xDB, 0xA7, 0x87, 0xC2, 0xE5,
200 0xD8, 0xC5, 0xF3, 0x7F, 0x8D
203 static const br_x509_trust_anchor TAs[2] = {
205 { (unsigned char *)TA0_DN, sizeof TA0_DN },
210 (unsigned char *)TA0_RSA_N, sizeof TA0_RSA_N,
211 (unsigned char *)TA0_RSA_E, sizeof TA0_RSA_E,
216 { (unsigned char *)TA1_DN, sizeof TA1_DN },
222 (unsigned char *)TA1_EC_Q, sizeof TA1_EC_Q,
231 * Main program: this is a simple program that expects 2 or 3 arguments.
232 * The first two arguments are a hostname and a port; the program will
233 * open a SSL connection with that server and port. It will then send
234 * a simple HTTP GET request, using the third argument as target path
235 * ("/" is used as path if no third argument was provided). The HTTP
236 * response, complete with header and contents, is received and written
240 main(int argc, char *argv[])
242 const char *host, *port, *path;
244 br_ssl_client_context sc;
245 br_x509_minimal_context xc;
246 unsigned char iobuf[BR_SSL_BUFSIZE_BIDI];
247 br_sslio_context ioc;
250 * Parse command-line argument: host, port, and path. The path
251 * is optional; if absent, "/" is used.
253 if (argc < 3 || argc > 4) {
265 * Ignore SIGPIPE to avoid crashing in case of abrupt socket close.
267 signal(SIGPIPE, SIG_IGN);
270 * Open the socket to the target server.
272 fd = host_connect(host, port);
278 * Initialise the client context:
279 * -- Use the "full" profile (all supported algorithms).
280 * -- The provided X.509 validation engine is initialised, with
281 * the hardcoded trust anchor.
283 br_ssl_client_init_full(&sc, &xc, TAs, TAs_NUM);
286 * Set the I/O buffer to the provided array. We allocated a
287 * buffer large enough for full-duplex behaviour with all
288 * allowed sizes of SSL records, hence we set the last argument
289 * to 1 (which means "split the buffer into separate input and
292 br_ssl_engine_set_buffer(&sc.eng, iobuf, sizeof iobuf, 1);
295 * Reset the client context, for a new handshake. We provide the
296 * target host name: it will be used for the SNI extension. The
297 * last parameter is 0: we are not trying to resume a session.
299 br_ssl_client_reset(&sc, host, 0);
302 * Initialise the simplified I/O wrapper context, to use our
303 * SSL client context, and the two callbacks for socket I/O.
305 br_sslio_init(&ioc, &sc.eng, sock_read, &fd, sock_write, &fd);
308 * Note that while the context has, at that point, already
309 * assembled the ClientHello to send, nothing happened on the
310 * network yet. Real I/O will occur only with the next call.
312 * We write our simple HTTP request. We could test each call
313 * for an error (-1), but this is not strictly necessary, since
314 * the error state "sticks": if the context fails for any reason
315 * (e.g. bad server certificate), then it will remain in failed
316 * state and all subsequent calls will return -1 as well.
318 br_sslio_write_all(&ioc, "GET ", 4);
319 br_sslio_write_all(&ioc, path, strlen(path));
320 br_sslio_write_all(&ioc, " HTTP/1.0\r\nHost: ", 17);
321 br_sslio_write_all(&ioc, host, strlen(host));
322 br_sslio_write_all(&ioc, "\r\n\r\n", 4);
325 * SSL is a buffered protocol: we make sure that all our request
326 * bytes are sent onto the wire.
328 br_sslio_flush(&ioc);
331 * Read the server's response. We use here a small 512-byte buffer,
332 * but most of the buffering occurs in the client context: the
333 * server will send full records (up to 16384 bytes worth of data
334 * each), and the client context buffers one full record at a time.
338 unsigned char tmp[512];
340 rlen = br_sslio_read(&ioc, tmp, sizeof tmp);
344 fwrite(tmp, 1, rlen, stdout);
353 * Check whether we closed properly or not. If the engine is
354 * closed, then its error status allows to distinguish between
355 * a normal closure and a SSL error.
357 * If the engine is NOT closed, then this means that the
358 * underlying network socket was closed or failed in some way.
359 * Note that many Web servers out there do not properly close
360 * their SSL connections (they don't send a close_notify alert),
361 * which will be reported here as "socket closed without proper
364 if (br_ssl_engine_current_state(&sc.eng) == BR_SSL_CLOSED) {
367 err = br_ssl_engine_last_error(&sc.eng);
369 fprintf(stderr, "closed.\n");
372 fprintf(stderr, "SSL error %d\n", err);
377 "socket closed without proper SSL termination\n");