Path: blob/main/contrib/bearssl/samples/client_basic.c
39507 views
/*1* Copyright (c) 2016 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include <stdio.h>25#include <stdlib.h>26#include <string.h>27#include <stdint.h>28#include <errno.h>29#include <signal.h>3031#include <sys/types.h>32#include <sys/socket.h>33#include <netdb.h>34#include <netinet/in.h>35#include <arpa/inet.h>36#include <unistd.h>3738#include "bearssl.h"3940/*41* Connect to the specified host and port. The connected socket is42* returned, or -1 on error.43*/44static int45host_connect(const char *host, const char *port)46{47struct addrinfo hints, *si, *p;48int fd;49int err;5051memset(&hints, 0, sizeof hints);52hints.ai_family = PF_UNSPEC;53hints.ai_socktype = SOCK_STREAM;54err = getaddrinfo(host, port, &hints, &si);55if (err != 0) {56fprintf(stderr, "ERROR: getaddrinfo(): %s\n",57gai_strerror(err));58return -1;59}60fd = -1;61for (p = si; p != NULL; p = p->ai_next) {62struct sockaddr *sa;63void *addr;64char tmp[INET6_ADDRSTRLEN + 50];6566sa = (struct sockaddr *)p->ai_addr;67if (sa->sa_family == AF_INET) {68addr = &((struct sockaddr_in *)sa)->sin_addr;69} else if (sa->sa_family == AF_INET6) {70addr = &((struct sockaddr_in6 *)sa)->sin6_addr;71} else {72addr = NULL;73}74if (addr != NULL) {75inet_ntop(p->ai_family, addr, tmp, sizeof tmp);76} else {77sprintf(tmp, "<unknown family: %d>",78(int)sa->sa_family);79}80fprintf(stderr, "connecting to: %s\n", tmp);81fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);82if (fd < 0) {83perror("socket()");84continue;85}86if (connect(fd, p->ai_addr, p->ai_addrlen) < 0) {87perror("connect()");88close(fd);89continue;90}91break;92}93if (p == NULL) {94freeaddrinfo(si);95fprintf(stderr, "ERROR: failed to connect\n");96return -1;97}98freeaddrinfo(si);99fprintf(stderr, "connected.\n");100return fd;101}102103/*104* Low-level data read callback for the simplified SSL I/O API.105*/106static int107sock_read(void *ctx, unsigned char *buf, size_t len)108{109for (;;) {110ssize_t rlen;111112rlen = read(*(int *)ctx, buf, len);113if (rlen <= 0) {114if (rlen < 0 && errno == EINTR) {115continue;116}117return -1;118}119return (int)rlen;120}121}122123/*124* Low-level data write callback for the simplified SSL I/O API.125*/126static int127sock_write(void *ctx, const unsigned char *buf, size_t len)128{129for (;;) {130ssize_t wlen;131132wlen = write(*(int *)ctx, buf, len);133if (wlen <= 0) {134if (wlen < 0 && errno == EINTR) {135continue;136}137return -1;138}139return (int)wlen;140}141}142143/*144* The hardcoded trust anchors. These are the two DN + public key that145* correspond to the self-signed certificates cert-root-rsa.pem and146* cert-root-ec.pem.147*148* C code for hardcoded trust anchors can be generated with the "brssl"149* command-line tool (with the "ta" command).150*/151152static const unsigned char TA0_DN[] = {1530x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,1540x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,1550x13, 0x04, 0x52, 0x6F, 0x6F, 0x74156};157158static const unsigned char TA0_RSA_N[] = {1590xB6, 0xD9, 0x34, 0xD4, 0x50, 0xFD, 0xB3, 0xAF, 0x7A, 0x73, 0xF1, 0xCE,1600x38, 0xBF, 0x5D, 0x6F, 0x45, 0xE1, 0xFD, 0x4E, 0xB1, 0x98, 0xC6, 0x60,1610x83, 0x26, 0xD2, 0x17, 0xD1, 0xC5, 0xB7, 0x9A, 0xA3, 0xC1, 0xDE, 0x63,1620x39, 0x97, 0x9C, 0xF0, 0x5E, 0x5C, 0xC8, 0x1C, 0x17, 0xB9, 0x88, 0x19,1630x6D, 0xF0, 0xB6, 0x2E, 0x30, 0x50, 0xA1, 0x54, 0x6E, 0x93, 0xC0, 0xDB,1640xCF, 0x30, 0xCB, 0x9F, 0x1E, 0x27, 0x79, 0xF1, 0xC3, 0x99, 0x52, 0x35,1650xAA, 0x3D, 0xB6, 0xDF, 0xB0, 0xAD, 0x7C, 0xCB, 0x49, 0xCD, 0xC0, 0xED,1660xE7, 0x66, 0x10, 0x2A, 0xE9, 0xCE, 0x28, 0x1F, 0x21, 0x50, 0xFA, 0x77,1670x4C, 0x2D, 0xDA, 0xEF, 0x3C, 0x58, 0xEB, 0x4E, 0xBF, 0xCE, 0xE9, 0xFB,1680x1A, 0xDA, 0xA3, 0x83, 0xA3, 0xCD, 0xA3, 0xCA, 0x93, 0x80, 0xDC, 0xDA,1690xF3, 0x17, 0xCC, 0x7A, 0xAB, 0x33, 0x80, 0x9C, 0xB2, 0xD4, 0x7F, 0x46,1700x3F, 0xC5, 0x3C, 0xDC, 0x61, 0x94, 0xB7, 0x27, 0x29, 0x6E, 0x2A, 0xBC,1710x5B, 0x09, 0x36, 0xD4, 0xC6, 0x3B, 0x0D, 0xEB, 0xBE, 0xCE, 0xDB, 0x1D,1720x1C, 0xBC, 0x10, 0x6A, 0x71, 0x71, 0xB3, 0xF2, 0xCA, 0x28, 0x9A, 0x77,1730xF2, 0x8A, 0xEC, 0x42, 0xEF, 0xB1, 0x4A, 0x8E, 0xE2, 0xF2, 0x1A, 0x32,1740x2A, 0xCD, 0xC0, 0xA6, 0x46, 0x2C, 0x9A, 0xC2, 0x85, 0x37, 0x91, 0x7F,1750x46, 0xA1, 0x93, 0x81, 0xA1, 0x74, 0x66, 0xDF, 0xBA, 0xB3, 0x39, 0x20,1760x91, 0x93, 0xFA, 0x1D, 0xA1, 0xA8, 0x85, 0xE7, 0xE4, 0xF9, 0x07, 0xF6,1770x10, 0xF6, 0xA8, 0x27, 0x01, 0xB6, 0x7F, 0x12, 0xC3, 0x40, 0xC3, 0xC9,1780xE2, 0xB0, 0xAB, 0x49, 0x18, 0x3A, 0x64, 0xB6, 0x59, 0xB7, 0x95, 0xB5,1790x96, 0x36, 0xDF, 0x22, 0x69, 0xAA, 0x72, 0x6A, 0x54, 0x4E, 0x27, 0x29,1800xA3, 0x0E, 0x97, 0x15181};182183static const unsigned char TA0_RSA_E[] = {1840x01, 0x00, 0x01185};186187static const unsigned char TA1_DN[] = {1880x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,1890x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04, 0x03,1900x13, 0x04, 0x52, 0x6F, 0x6F, 0x74191};192193static const unsigned char TA1_EC_Q[] = {1940x04, 0x71, 0x74, 0xBA, 0xAB, 0xB9, 0x30, 0x2E, 0x81, 0xD5, 0xE5, 0x57,1950xF9, 0xF3, 0x20, 0x68, 0x0C, 0x9C, 0xF9, 0x64, 0xDB, 0xB4, 0x20, 0x0D,1960x6D, 0xEA, 0x40, 0xD0, 0x4A, 0x6E, 0x42, 0xFD, 0xB6, 0x9A, 0x68, 0x25,1970x44, 0xF6, 0xDF, 0x7B, 0xC4, 0xFC, 0xDE, 0xDD, 0x7B, 0xBB, 0xC5, 0xDB,1980x7C, 0x76, 0x3F, 0x41, 0x66, 0x40, 0x6E, 0xDB, 0xA7, 0x87, 0xC2, 0xE5,1990xD8, 0xC5, 0xF3, 0x7F, 0x8D200};201202static const br_x509_trust_anchor TAs[2] = {203{204{ (unsigned char *)TA0_DN, sizeof TA0_DN },205BR_X509_TA_CA,206{207BR_KEYTYPE_RSA,208{ .rsa = {209(unsigned char *)TA0_RSA_N, sizeof TA0_RSA_N,210(unsigned char *)TA0_RSA_E, sizeof TA0_RSA_E,211} }212}213},214{215{ (unsigned char *)TA1_DN, sizeof TA1_DN },216BR_X509_TA_CA,217{218BR_KEYTYPE_EC,219{ .ec = {220BR_EC_secp256r1,221(unsigned char *)TA1_EC_Q, sizeof TA1_EC_Q,222} }223}224}225};226227#define TAs_NUM 2228229/*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 will232* open a SSL connection with that server and port. It will then send233* a simple HTTP GET request, using the third argument as target path234* ("/" is used as path if no third argument was provided). The HTTP235* response, complete with header and contents, is received and written236* on stdout.237*/238int239main(int argc, char *argv[])240{241const char *host, *port, *path;242int fd;243br_ssl_client_context sc;244br_x509_minimal_context xc;245unsigned char iobuf[BR_SSL_BUFSIZE_BIDI];246br_sslio_context ioc;247248/*249* Parse command-line argument: host, port, and path. The path250* is optional; if absent, "/" is used.251*/252if (argc < 3 || argc > 4) {253return EXIT_FAILURE;254}255host = argv[1];256port = argv[2];257if (argc == 4) {258path = argv[3];259} else {260path = "/";261}262263/*264* Ignore SIGPIPE to avoid crashing in case of abrupt socket close.265*/266signal(SIGPIPE, SIG_IGN);267268/*269* Open the socket to the target server.270*/271fd = host_connect(host, port);272if (fd < 0) {273return EXIT_FAILURE;274}275276/*277* Initialise the client context:278* -- Use the "full" profile (all supported algorithms).279* -- The provided X.509 validation engine is initialised, with280* the hardcoded trust anchor.281*/282br_ssl_client_init_full(&sc, &xc, TAs, TAs_NUM);283284/*285* Set the I/O buffer to the provided array. We allocated a286* buffer large enough for full-duplex behaviour with all287* allowed sizes of SSL records, hence we set the last argument288* to 1 (which means "split the buffer into separate input and289* output areas").290*/291br_ssl_engine_set_buffer(&sc.eng, iobuf, sizeof iobuf, 1);292293/*294* Reset the client context, for a new handshake. We provide the295* target host name: it will be used for the SNI extension. The296* last parameter is 0: we are not trying to resume a session.297*/298br_ssl_client_reset(&sc, host, 0);299300/*301* Initialise the simplified I/O wrapper context, to use our302* SSL client context, and the two callbacks for socket I/O.303*/304br_sslio_init(&ioc, &sc.eng, sock_read, &fd, sock_write, &fd);305306/*307* Note that while the context has, at that point, already308* assembled the ClientHello to send, nothing happened on the309* network yet. Real I/O will occur only with the next call.310*311* We write our simple HTTP request. We could test each call312* for an error (-1), but this is not strictly necessary, since313* the error state "sticks": if the context fails for any reason314* (e.g. bad server certificate), then it will remain in failed315* state and all subsequent calls will return -1 as well.316*/317br_sslio_write_all(&ioc, "GET ", 4);318br_sslio_write_all(&ioc, path, strlen(path));319br_sslio_write_all(&ioc, " HTTP/1.0\r\nHost: ", 17);320br_sslio_write_all(&ioc, host, strlen(host));321br_sslio_write_all(&ioc, "\r\n\r\n", 4);322323/*324* SSL is a buffered protocol: we make sure that all our request325* bytes are sent onto the wire.326*/327br_sslio_flush(&ioc);328329/*330* Read the server's response. We use here a small 512-byte buffer,331* but most of the buffering occurs in the client context: the332* server will send full records (up to 16384 bytes worth of data333* each), and the client context buffers one full record at a time.334*/335for (;;) {336int rlen;337unsigned char tmp[512];338339rlen = br_sslio_read(&ioc, tmp, sizeof tmp);340if (rlen < 0) {341break;342}343fwrite(tmp, 1, rlen, stdout);344}345346/*347* Close the socket.348*/349close(fd);350351/*352* Check whether we closed properly or not. If the engine is353* closed, then its error status allows to distinguish between354* a normal closure and a SSL error.355*356* If the engine is NOT closed, then this means that the357* underlying network socket was closed or failed in some way.358* Note that many Web servers out there do not properly close359* their SSL connections (they don't send a close_notify alert),360* which will be reported here as "socket closed without proper361* SSL termination".362*/363if (br_ssl_engine_current_state(&sc.eng) == BR_SSL_CLOSED) {364int err;365366err = br_ssl_engine_last_error(&sc.eng);367if (err == 0) {368fprintf(stderr, "closed.\n");369return EXIT_SUCCESS;370} else {371fprintf(stderr, "SSL error %d\n", err);372return EXIT_FAILURE;373}374} else {375fprintf(stderr,376"socket closed without proper SSL termination\n");377return EXIT_FAILURE;378}379}380381382