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
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
41 * Connect to the specified host and port. The connected socket is
42 * returned, or -1 on error.
45 host_connect(const char *host
, const char *port
)
47 struct addrinfo hints
, *si
, *p
;
51 memset(&hints
, 0, sizeof hints
);
52 hints
.ai_family
= PF_UNSPEC
;
53 hints
.ai_socktype
= SOCK_STREAM
;
54 err
= getaddrinfo(host
, port
, &hints
, &si
);
56 fprintf(stderr
, "ERROR: getaddrinfo(): %s\n",
61 for (p
= si
; p
!= NULL
; p
= p
->ai_next
) {
64 char tmp
[INET6_ADDRSTRLEN
+ 50];
66 sa
= (struct sockaddr
*)p
->ai_addr
;
67 if (sa
->sa_family
== AF_INET
) {
68 addr
= &((struct sockaddr_in
*)sa
)->sin_addr
;
69 } else if (sa
->sa_family
== AF_INET6
) {
70 addr
= &((struct sockaddr_in6
*)sa
)->sin6_addr
;
75 inet_ntop(p
->ai_family
, addr
, tmp
, sizeof tmp
);
77 sprintf(tmp
, "<unknown family: %d>",
80 fprintf(stderr
, "connecting to: %s\n", tmp
);
81 fd
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
);
86 if (connect(fd
, p
->ai_addr
, p
->ai_addrlen
) < 0) {
95 fprintf(stderr
, "ERROR: failed to connect\n");
99 fprintf(stderr
, "connected.\n");
104 * Low-level data read callback for the simplified SSL I/O API.
107 sock_read(void *ctx
, unsigned char *buf
, size_t len
)
112 rlen
= read(*(int *)ctx
, buf
, len
);
114 if (rlen
< 0 && errno
== EINTR
) {
124 * Low-level data write callback for the simplified SSL I/O API.
127 sock_write(void *ctx
, const unsigned char *buf
, size_t len
)
132 wlen
= write(*(int *)ctx
, buf
, len
);
134 if (wlen
< 0 && errno
== EINTR
) {
144 * The hardcoded trust anchors. These are the two DN + public key that
145 * correspond to the self-signed certificates cert-root-rsa.pem and
148 * C code for hardcoded trust anchors can be generated with the "brssl"
149 * command-line tool (with the "ta" command).
152 static const unsigned char TA0_DN
[] = {
153 0x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
154 0x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
155 0x13, 0x04, 0x52, 0x6F, 0x6F, 0x74
158 static const unsigned char TA0_RSA_N
[] = {
159 0xB6, 0xD9, 0x34, 0xD4, 0x50, 0xFD, 0xB3, 0xAF, 0x7A, 0x73, 0xF1, 0xCE,
160 0x38, 0xBF, 0x5D, 0x6F, 0x45, 0xE1, 0xFD, 0x4E, 0xB1, 0x98, 0xC6, 0x60,
161 0x83, 0x26, 0xD2, 0x17, 0xD1, 0xC5, 0xB7, 0x9A, 0xA3, 0xC1, 0xDE, 0x63,
162 0x39, 0x97, 0x9C, 0xF0, 0x5E, 0x5C, 0xC8, 0x1C, 0x17, 0xB9, 0x88, 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7F, 0x12, 0xC3, 0x40, 0xC3, 0xC9,
178 0xE2, 0xB0, 0xAB, 0x49, 0x18, 0x3A, 0x64, 0xB6, 0x59, 0xB7, 0x95, 0xB5,
179 0x96, 0x36, 0xDF, 0x22, 0x69, 0xAA, 0x72, 0x6A, 0x54, 0x4E, 0x27, 0x29,
180 0xA3, 0x0E, 0x97, 0x15
183 static const unsigned char TA0_RSA_E
[] = {
187 static const unsigned char TA1_DN
[] = {
188 0x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
189 0x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,
190 0x13, 0x04, 0x52, 0x6F, 0x6F, 0x74
193 static const unsigned char TA1_EC_Q
[] = {
194 0x04, 0x71, 0x74, 0xBA, 0xAB, 0xB9, 0x30, 0x2E, 0x81, 0xD5, 0xE5, 0x57,
195 0xF9, 0xF3, 0x20, 0x68, 0x0C, 0x9C, 0xF9, 0x64, 0xDB, 0xB4, 0x20, 0x0D,
196 0x6D, 0xEA, 0x40, 0xD0, 0x4A, 0x6E, 0x42, 0xFD, 0xB6, 0x9A, 0x68, 0x25,
197 0x44, 0xF6, 0xDF, 0x7B, 0xC4, 0xFC, 0xDE, 0xDD, 0x7B, 0xBB, 0xC5, 0xDB,
198 0x7C, 0x76, 0x3F, 0x41, 0x66, 0x40, 0x6E, 0xDB, 0xA7, 0x87, 0xC2, 0xE5,
199 0xD8, 0xC5, 0xF3, 0x7F, 0x8D
202 static const br_x509_trust_anchor TAs
[2] = {
204 (unsigned char *)TA0_DN
, sizeof TA0_DN
,
209 (unsigned char *)TA0_RSA_N
, sizeof TA0_RSA_N
,
210 (unsigned char *)TA0_RSA_E
, sizeof TA0_RSA_E
,
215 (unsigned char *)TA1_DN
, sizeof TA1_DN
,
221 (unsigned char *)TA1_EC_Q
, sizeof TA1_EC_Q
,
230 * Main program: this is a simple program that expects 2 or 3 arguments.
231 * The first two arguments are a hostname and a port; the program will
232 * open a SSL connection with that server and port. It will then send
233 * a simple HTTP GET request, using the third argument as target path
234 * ("/" is used as path if no third argument was provided). The HTTP
235 * response, complete with header and contents, is received and written
239 main(int argc
, char *argv
[])
241 const char *host
, *port
, *path
;
243 br_ssl_client_context sc
;
244 br_x509_minimal_context xc
;
245 unsigned char iobuf
[BR_SSL_BUFSIZE_BIDI
];
246 br_sslio_context ioc
;
249 * Parse command-line argument: host, port, and path. The path
250 * is optional; if absent, "/" is used.
252 if (argc
< 3 || argc
> 4) {
264 * Open the socket to the target server.
266 fd
= host_connect(host
, port
);
272 * Initialise the client context:
273 * -- Use the "full" profile (all supported algorithms).
274 * -- The provided X.509 validation engine is initialised, with
275 * the hardcoded trust anchor.
277 br_ssl_client_init_full(&sc
, &xc
, TAs
, TAs_NUM
);
280 * Set the I/O buffer to the provided array. We allocated a
281 * buffer large enough for full-duplex behaviour with all
282 * allowed sizes of SSL records, hence we set the last argument
283 * to 1 (which means "split the buffer into separate input and
286 br_ssl_engine_set_buffer(&sc
.eng
, iobuf
, sizeof iobuf
, 1);
289 * Reset the client context, for a new handshake. We provide the
290 * target host name: it will be used for the SNI extension. The
291 * last parameter is 0: we are not trying to resume a session.
293 br_ssl_client_reset(&sc
, host
, 0);
296 * Initialise the simplified I/O wrapper context, to use our
297 * SSL client context, and the two callbacks for socket I/O.
299 br_sslio_init(&ioc
, &sc
.eng
, sock_read
, &fd
, sock_write
, &fd
);
302 * Note that while the context has, at that point, already
303 * assembled the ClientHello to send, nothing happened on the
304 * network yet. Real I/O will occur only with the next call.
306 * We write our simple HTTP request. We could test each call
307 * for an error (-1), but this is not strictly necessary, since
308 * the error state "sticks": if the context fails for any reason
309 * (e.g. bad server certificate), then it will remain in failed
310 * state and all subsequent calls will return -1 as well.
312 br_sslio_write_all(&ioc
, "GET ", 4);
313 br_sslio_write_all(&ioc
, path
, strlen(path
));
314 br_sslio_write_all(&ioc
, " HTTP/1.0\r\nHost: ", 17);
315 br_sslio_write_all(&ioc
, host
, strlen(host
));
316 br_sslio_write_all(&ioc
, "\r\n\r\n", 4);
319 * SSL is a buffered protocol: we make sure that all our request
320 * bytes are sent onto the wire.
322 br_sslio_flush(&ioc
);
325 * Read the server's response. We use here a small 512-byte buffer,
326 * but most of the buffering occurs in the client context: the
327 * server will send full records (up to 16384 bytes worth of data
328 * each), and the client context buffers one full record at a time.
332 unsigned char tmp
[512];
334 rlen
= br_sslio_read(&ioc
, tmp
, sizeof tmp
);
338 fwrite(tmp
, 1, rlen
, stdout
);
347 * Check whether we closed properly or not. If the engine is
348 * closed, then its error status allows to distinguish between
349 * a normal closure and a SSL error.
351 * If the engine is NOT closed, then this means that the
352 * underlying network socket was closed or failed in some way.
353 * Note that many Web servers out there do not properly close
354 * their SSL connections (they don't send a close_notify alert),
355 * which will be reported here as "socket closed without proper
358 if (br_ssl_engine_current_state(&sc
.eng
) == BR_SSL_CLOSED
) {
361 err
= br_ssl_engine_last_error(&sc
.eng
);
363 fprintf(stderr
, "closed.\n");
366 fprintf(stderr
, "SSL error %d\n", err
);
371 "socket closed without proper SSL termination\n");