Update to latest BearSSL version
- Update subrepository to latest BearSSL version - Use full certificate chain verification - Add Let's-Encrypt root certificate (IdenTrust cross-signed) - Add workaround for missing date & time (use time of compilation for SSL) - Add updated certificates for tls_server_bearssl example from BearSSL server example, previous ones were expired
This commit is contained in:
parent
c66730960e
commit
48e129dcc4
5 changed files with 347 additions and 194 deletions
|
|
@ -1,4 +1,6 @@
|
|||
PROGRAM=http_get_bearssl
|
||||
EXTRA_COMPONENTS = extras/bearssl
|
||||
|
||||
EXTRA_CFLAGS +=-DCONFIG_EPOCH_TIME=$(shell date --utc '+%s')
|
||||
|
||||
include ../../common.mk
|
||||
|
|
|
|||
|
|
@ -29,53 +29,18 @@
|
|||
|
||||
#include "bearssl.h"
|
||||
|
||||
#define CLOCK_SECONDS_PER_MINUTE (60UL)
|
||||
#define CLOCK_MINUTES_PER_HOUR (60UL)
|
||||
#define CLOCK_HOURS_PER_DAY (24UL)
|
||||
#define CLOCK_SECONDS_PER_HOUR (CLOCK_MINUTES_PER_HOUR*CLOCK_SECONDS_PER_MINUTE)
|
||||
#define CLOCK_SECONDS_PER_DAY (CLOCK_HOURS_PER_DAY*CLOCK_SECONDS_PER_HOUR)
|
||||
|
||||
#define WEB_SERVER "www.howsmyssl.com"
|
||||
#define WEB_PORT "443"
|
||||
#define WEB_URL "https://www.howsmyssl.com/a/check"
|
||||
|
||||
#define GET_REQUEST "GET "WEB_URL" HTTP/1.1\nHost: "WEB_SERVER"\n\n"
|
||||
|
||||
/*
|
||||
* howsmyssl.com public key
|
||||
* BearSSL doesn't allow disabling validation and we don't have
|
||||
* a correct date/time on the ESP so it would always fail.
|
||||
* Instead, we compare the server key against this static public key
|
||||
* to validate the server.
|
||||
*/
|
||||
static const unsigned char SERVER_PKEY_RSA_N[] = {
|
||||
0xA9, 0x11, 0x5C, 0xAF, 0x4F, 0xDD, 0xEB, 0x88, 0xCD, 0xF6, 0x47, 0x11,
|
||||
0xB9, 0x7F, 0xFA, 0x08, 0xD7, 0x90, 0x01, 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 0x88, 0x3A,
|
||||
0xAC, 0xEE, 0x64, 0x08, 0xBD, 0x74, 0x82, 0x24, 0x82, 0x11, 0xD4, 0x93,
|
||||
0xFA, 0xEF, 0x52, 0x5F, 0x2C, 0x51, 0x0C, 0x88, 0x09, 0xB9, 0x77, 0xA0,
|
||||
0xFA, 0x6F, 0xAB, 0xEB, 0xE1, 0x08, 0x5C, 0xA5, 0x02, 0x53, 0xA1, 0x18,
|
||||
0xFC, 0xB8, 0x06, 0x67, 0x7E, 0x80, 0xDF, 0xF1, 0xCD, 0xAE, 0x23, 0x58,
|
||||
0xCD, 0xCE, 0x7C, 0x69, 0x2E, 0x4D, 0xD6, 0xB0, 0xD2, 0x6D, 0x11, 0x85,
|
||||
0x5E, 0x36, 0x41, 0x27
|
||||
};
|
||||
|
||||
static const unsigned char SERVER_PKEY_RSA_E[] = {
|
||||
0x01, 0x00, 0x01
|
||||
};
|
||||
|
||||
static const br_rsa_public_key server_pkey = {
|
||||
(unsigned char *)SERVER_PKEY_RSA_N, sizeof SERVER_PKEY_RSA_N,
|
||||
(unsigned char *)SERVER_PKEY_RSA_E, sizeof SERVER_PKEY_RSA_E,
|
||||
};
|
||||
|
||||
/*
|
||||
* Low-level data read callback for the simplified SSL I/O API.
|
||||
*/
|
||||
|
|
@ -116,6 +81,76 @@ sock_write(void *ctx, const unsigned char *buf, size_t len)
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* The hardcoded trust anchors. These are the two DN + public key that
|
||||
* correspond to the self-signed certificates cert-root-rsa.pem and
|
||||
* cert-root-ec.pem.
|
||||
*
|
||||
* C code for hardcoded trust anchors can be generated with the "brssl"
|
||||
* command-line tool (with the "ta" command).
|
||||
*
|
||||
* Below is the imported "Let's Encrypt" root certificate, as howsmyssl
|
||||
* is depending on it:
|
||||
*
|
||||
* https://letsencrypt.org/certs/letsencryptauthorityx3.pem
|
||||
*
|
||||
*/
|
||||
|
||||
static const unsigned char TA0_DN[] = {
|
||||
0x30, 0x4A, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
|
||||
0x02, 0x55, 0x53, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0A,
|
||||
0x13, 0x0D, 0x4C, 0x65, 0x74, 0x27, 0x73, 0x20, 0x45, 0x6E, 0x63, 0x72,
|
||||
0x79, 0x70, 0x74, 0x31, 0x23, 0x30, 0x21, 0x06, 0x03, 0x55, 0x04, 0x03,
|
||||
0x13, 0x1A, 0x4C, 0x65, 0x74, 0x27, 0x73, 0x20, 0x45, 0x6E, 0x63, 0x72,
|
||||
0x79, 0x70, 0x74, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6F, 0x72, 0x69, 0x74,
|
||||
0x79, 0x20, 0x58, 0x33
|
||||
};
|
||||
|
||||
static const unsigned char TA0_RSA_N[] = {
|
||||
0x9C, 0xD3, 0x0C, 0xF0, 0x5A, 0xE5, 0x2E, 0x47, 0xB7, 0x72, 0x5D, 0x37,
|
||||
0x83, 0xB3, 0x68, 0x63, 0x30, 0xEA, 0xD7, 0x35, 0x26, 0x19, 0x25, 0xE1,
|
||||
0xBD, 0xBE, 0x35, 0xF1, 0x70, 0x92, 0x2F, 0xB7, 0xB8, 0x4B, 0x41, 0x05,
|
||||
0xAB, 0xA9, 0x9E, 0x35, 0x08, 0x58, 0xEC, 0xB1, 0x2A, 0xC4, 0x68, 0x87,
|
||||
0x0B, 0xA3, 0xE3, 0x75, 0xE4, 0xE6, 0xF3, 0xA7, 0x62, 0x71, 0xBA, 0x79,
|
||||
0x81, 0x60, 0x1F, 0xD7, 0x91, 0x9A, 0x9F, 0xF3, 0xD0, 0x78, 0x67, 0x71,
|
||||
0xC8, 0x69, 0x0E, 0x95, 0x91, 0xCF, 0xFE, 0xE6, 0x99, 0xE9, 0x60, 0x3C,
|
||||
0x48, 0xCC, 0x7E, 0xCA, 0x4D, 0x77, 0x12, 0x24, 0x9D, 0x47, 0x1B, 0x5A,
|
||||
0xEB, 0xB9, 0xEC, 0x1E, 0x37, 0x00, 0x1C, 0x9C, 0xAC, 0x7B, 0xA7, 0x05,
|
||||
0xEA, 0xCE, 0x4A, 0xEB, 0xBD, 0x41, 0xE5, 0x36, 0x98, 0xB9, 0xCB, 0xFD,
|
||||
0x6D, 0x3C, 0x96, 0x68, 0xDF, 0x23, 0x2A, 0x42, 0x90, 0x0C, 0x86, 0x74,
|
||||
0x67, 0xC8, 0x7F, 0xA5, 0x9A, 0xB8, 0x52, 0x61, 0x14, 0x13, 0x3F, 0x65,
|
||||
0xE9, 0x82, 0x87, 0xCB, 0xDB, 0xFA, 0x0E, 0x56, 0xF6, 0x86, 0x89, 0xF3,
|
||||
0x85, 0x3F, 0x97, 0x86, 0xAF, 0xB0, 0xDC, 0x1A, 0xEF, 0x6B, 0x0D, 0x95,
|
||||
0x16, 0x7D, 0xC4, 0x2B, 0xA0, 0x65, 0xB2, 0x99, 0x04, 0x36, 0x75, 0x80,
|
||||
0x6B, 0xAC, 0x4A, 0xF3, 0x1B, 0x90, 0x49, 0x78, 0x2F, 0xA2, 0x96, 0x4F,
|
||||
0x2A, 0x20, 0x25, 0x29, 0x04, 0xC6, 0x74, 0xC0, 0xD0, 0x31, 0xCD, 0x8F,
|
||||
0x31, 0x38, 0x95, 0x16, 0xBA, 0xA8, 0x33, 0xB8, 0x43, 0xF1, 0xB1, 0x1F,
|
||||
0xC3, 0x30, 0x7F, 0xA2, 0x79, 0x31, 0x13, 0x3D, 0x2D, 0x36, 0xF8, 0xE3,
|
||||
0xFC, 0xF2, 0x33, 0x6A, 0xB9, 0x39, 0x31, 0xC5, 0xAF, 0xC4, 0x8D, 0x0D,
|
||||
0x1D, 0x64, 0x16, 0x33, 0xAA, 0xFA, 0x84, 0x29, 0xB6, 0xD4, 0x0B, 0xC0,
|
||||
0xD8, 0x7D, 0xC3, 0x93
|
||||
};
|
||||
|
||||
static const unsigned char TA0_RSA_E[] = {
|
||||
0x01, 0x00, 0x01
|
||||
};
|
||||
|
||||
static const br_x509_trust_anchor TAs[1] = {
|
||||
{
|
||||
{ (unsigned char *)TA0_DN, sizeof TA0_DN },
|
||||
BR_X509_TA_CA,
|
||||
{
|
||||
BR_KEYTYPE_RSA,
|
||||
{ .rsa = {
|
||||
(unsigned char *)TA0_RSA_N, sizeof TA0_RSA_N,
|
||||
(unsigned char *)TA0_RSA_E, sizeof TA0_RSA_E,
|
||||
} }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#define TAs_NUM 1
|
||||
|
||||
/*
|
||||
* Buffer to store a record + BearSSL state
|
||||
* We use MONO mode to save 16k of RAM.
|
||||
|
|
@ -126,12 +161,13 @@ static unsigned char bearssl_buffer[BR_SSL_BUFSIZE_MONO];
|
|||
|
||||
static br_ssl_client_context sc;
|
||||
static br_x509_minimal_context xc;
|
||||
static br_x509_knownkey_context kkc;
|
||||
static br_sslio_context ioc;
|
||||
|
||||
void http_get_task(void *pvParameters)
|
||||
{
|
||||
int successes = 0, failures = 0;
|
||||
int provisional_time = 0;
|
||||
|
||||
while (1) {
|
||||
/*
|
||||
* Wait until we can resolve the DNS for the server, as an indication
|
||||
|
|
@ -159,15 +195,7 @@ void http_get_task(void *pvParameters)
|
|||
}
|
||||
|
||||
printf("Initializing BearSSL... ");
|
||||
br_ssl_client_init_full(&sc, &xc, NULL, 0);
|
||||
|
||||
/*
|
||||
* Overwrite the default X509 engine with a much simpler known key one
|
||||
* This sidesteps validation failure in the default engine, as we have no date/time
|
||||
* Instead, the servers key is compared to the hardcoded public key above
|
||||
*/
|
||||
br_x509_knownkey_init_rsa(&kkc, &server_pkey);
|
||||
br_ssl_engine_set_x509(&sc.eng, &kkc.vtable);
|
||||
br_ssl_client_init_full(&sc, &xc, TAs, TAs_NUM);
|
||||
|
||||
/*
|
||||
* Set the I/O buffer to the provided array. We allocated a
|
||||
|
|
@ -201,6 +229,15 @@ void http_get_task(void *pvParameters)
|
|||
br_sslio_init(&ioc, &sc.eng, sock_read, &fd, sock_write, &fd);
|
||||
printf("done.\r\n");
|
||||
|
||||
/* FIXME: set date & time using epoch time precompiler flag for now */
|
||||
provisional_time = CONFIG_EPOCH_TIME + (xTaskGetTickCount()/configTICK_RATE_HZ);
|
||||
xc.days = (provisional_time / CLOCK_SECONDS_PER_DAY) + 719528;
|
||||
xc.seconds = provisional_time % CLOCK_SECONDS_PER_DAY;
|
||||
printf("Time: %02i:%02i\r\n",
|
||||
(int)(xc.seconds / CLOCK_SECONDS_PER_HOUR),
|
||||
(int)((xc.seconds % CLOCK_SECONDS_PER_HOUR)/CLOCK_SECONDS_PER_MINUTE)
|
||||
);
|
||||
|
||||
if (connect(fd, res->ai_addr, res->ai_addrlen) != 0)
|
||||
{
|
||||
close(fd);
|
||||
|
|
@ -267,13 +304,18 @@ void http_get_task(void *pvParameters)
|
|||
continue;
|
||||
}
|
||||
|
||||
printf("\r\n\r\nfree heap pre = %u\r\n", xPortGetFreeHeapSize());
|
||||
|
||||
/*
|
||||
* Close the connection and start over after a delay
|
||||
*/
|
||||
close(fd);
|
||||
freeaddrinfo(res);
|
||||
|
||||
printf("free heap post = %u\r\n", xPortGetFreeHeapSize());
|
||||
|
||||
successes++;
|
||||
printf("\r\n\r\nsuccesses = %d failures = %d\r\n", successes, failures);
|
||||
printf("successes = %d failures = %d\r\n", successes, failures);
|
||||
for(int countdown = 10; countdown >= 0; countdown--) {
|
||||
printf("%d...\n", countdown);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue