Add BearSSL extra (#340)

Add BearSSL extra + http_get example, TLS server example
This commit is contained in:
Milosch Meriac 2017-02-15 11:44:11 +00:00 committed by Ruslan V. Uss
parent 54ce6bbe1c
commit 398ed46776
9 changed files with 849 additions and 0 deletions

3
.gitmodules vendored
View file

@ -19,6 +19,9 @@
[submodule "tests/fs-test"]
path = tests/fs-test
url = https://github.com/sheinz/fs-test
[submodule "extras/bearssl/BearSSL"]
path = extras/bearssl/BearSSL
url = https://www.bearssl.org/git/BearSSL
[submodule "extras/http-parser/http-parser"]
path = extras/http-parser/http-parser
url = https://github.com/nodejs/http-parser

View file

@ -0,0 +1,6 @@
PROGRAM=http_get_bearssl
EXTRA_COMPONENTS = extras/bearssl
EXTRA_CFLAGS +=-DCONFIG_EPOCH_TIME=$(shell date --utc '+%s')
include ../../common.mk

View file

@ -0,0 +1,342 @@
/* http_get_bearssl - HTTPS version of the http_get example, using BearSSL.
*
* Retrieves a JSON response from the howsmyssl.com API via HTTPS over TLS v1.2.
*
* Validates the server's certificate using a hardcoded public key.
*
* Adapted from the client_basic sample in BearSSL.
*
* Original Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>, MIT License.
* Additions Copyright (C) 2016 Stefan Schake, MIT License.
*/
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "esp/hwrand.h"
#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "lwip/api.h"
#include "ssid_config.h"
#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"
/*
* Low-level data read callback for the simplified SSL I/O API.
*/
static int
sock_read(void *ctx, unsigned char *buf, size_t len)
{
for (;;) {
ssize_t rlen;
rlen = read(*(int *)ctx, buf, len);
if (rlen <= 0) {
if (rlen < 0 && errno == EINTR) {
continue;
}
return -1;
}
return (int)rlen;
}
}
/*
* Low-level data write callback for the simplified SSL I/O API.
*/
static int
sock_write(void *ctx, const unsigned char *buf, size_t len)
{
for (;;) {
ssize_t wlen;
wlen = write(*(int *)ctx, buf, len);
if (wlen <= 0) {
if (wlen < 0 && errno == EINTR) {
continue;
}
return -1;
}
return (int)wlen;
}
}
/*
* 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[] =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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.
* This could be even smaller by using max_fragment_len, but
* the howsmyssl.com server doesn't seem to support it.
*/
static unsigned char bearssl_buffer[BR_SSL_BUFSIZE_MONO];
static br_ssl_client_context sc;
static br_x509_minimal_context xc;
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
* our network is probably working...
*/
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *res = NULL;
int dns_err = 0;
do {
if (res)
freeaddrinfo(res);
vTaskDelay(1000 / portTICK_PERIOD_MS);
dns_err = getaddrinfo(WEB_SERVER, WEB_PORT, &hints, &res);
} while(dns_err != 0 || res == NULL);
int fd = socket(res->ai_family, res->ai_socktype, 0);
if (fd < 0) {
freeaddrinfo(res);
printf("socket failed\n");
failures++;
continue;
}
printf("Initializing BearSSL... ");
br_ssl_client_init_full(&sc, &xc, TAs, TAs_NUM);
/*
* Set the I/O buffer to the provided array. We allocated a
* buffer large enough for full-duplex behaviour with all
* allowed sizes of SSL records, hence we set the last argument
* to 1 (which means "split the buffer into separate input and
* output areas").
*/
br_ssl_engine_set_buffer(&sc.eng, bearssl_buffer, sizeof bearssl_buffer, 0);
/*
* Inject some entropy from the ESP hardware RNG
* This is necessary because we don't support any of the BearSSL methods
*/
for (int i = 0; i < 10; i++) {
int rand = hwrand();
br_ssl_engine_inject_entropy(&sc.eng, &rand, 4);
}
/*
* Reset the client context, for a new handshake. We provide the
* target host name: it will be used for the SNI extension. The
* last parameter is 0: we are not trying to resume a session.
*/
br_ssl_client_reset(&sc, WEB_SERVER, 0);
/*
* Initialise the simplified I/O wrapper context, to use our
* SSL client context, and the two callbacks for socket I/O.
*/
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);
freeaddrinfo(res);
printf("connect failed\n");
failures++;
continue;
}
printf("Connected\r\n");
/*
* Note that while the context has, at that point, already
* assembled the ClientHello to send, nothing happened on the
* network yet. Real I/O will occur only with the next call.
*
* We write our simple HTTP request. We test the call
* for an error (-1), but this is not strictly necessary, since
* the error state "sticks": if the context fails for any reason
* (e.g. bad server certificate), then it will remain in failed
* state and all subsequent calls will return -1 as well.
*/
if (br_sslio_write_all(&ioc, GET_REQUEST, strlen(GET_REQUEST)) != BR_ERR_OK) {
close(fd);
freeaddrinfo(res);
printf("br_sslio_write_all failed: %d\r\n", br_ssl_engine_last_error(&sc.eng));
failures++;
continue;
}
/*
* SSL is a buffered protocol: we make sure that all our request
* bytes are sent onto the wire.
*/
br_sslio_flush(&ioc);
/*
* Read and print the server response
*/
for (;;)
{
int rlen;
unsigned char buf[128];
bzero(buf, 128);
// Leave the final byte for zero termination
rlen = br_sslio_read(&ioc, buf, sizeof(buf) - 1);
if (rlen < 0) {
break;
}
if (rlen > 0) {
printf("%s", buf);
}
}
/*
* If reading the response failed for any reason, we detect it here
*/
if (br_ssl_engine_last_error(&sc.eng) != BR_ERR_OK) {
close(fd);
freeaddrinfo(res);
printf("failure, error = %d\r\n", br_ssl_engine_last_error(&sc.eng));
failures++;
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("successes = %d failures = %d\r\n", successes, failures);
for(int countdown = 10; countdown >= 0; countdown--) {
printf("%d...\n", countdown);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
printf("Starting again!\r\n\r\n");
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
struct sdk_station_config config = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
};
/* required to call wifi_set_opmode before station_set_config */
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&config);
xTaskCreate(&http_get_task, "get_task", 2048, NULL, 2, NULL);
}

View file

@ -0,0 +1,4 @@
PROGRAM=tls_server_bearssl
EXTRA_COMPONENTS = extras/bearssl
include ../../common.mk

View file

@ -0,0 +1,183 @@
/*
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "bearssl.h"
/*
* A sample server certificate chain with a single intermediate CA.
* Certificate key type: RSA
* Signing algorithm for both certificates: RSA
*/
static const unsigned char CERT0[] = {
0x30, 0x82, 0x03, 0x3C, 0x30, 0x82, 0x02, 0x24, 0xA0, 0x03, 0x02, 0x01,
0x02, 0x02, 0x14, 0x58, 0xDA, 0xBA, 0x36, 0xCD, 0xED, 0xA0, 0xDA, 0x5C,
0x10, 0x33, 0x73, 0x8E, 0x0C, 0x64, 0x92, 0x79, 0x92, 0xAF, 0x03, 0x30,
0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
0x05, 0x00, 0x30, 0x27, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55,
0x04, 0x03, 0x13, 0x0F, 0x49, 0x6E, 0x74, 0x65, 0x72, 0x6D, 0x65, 0x64,
0x69, 0x61, 0x74, 0x65, 0x20, 0x43, 0x41, 0x30, 0x1E, 0x17, 0x0D, 0x31,
0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5A,
0x17, 0x0D, 0x33, 0x37, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x35, 0x39,
0x35, 0x39, 0x5A, 0x30, 0x21, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
0x04, 0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03,
0x55, 0x04, 0x03, 0x13, 0x09, 0x6C, 0x6F, 0x63, 0x61, 0x6C, 0x68, 0x6F,
0x73, 0x74, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86,
0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
0x0F, 0x00, 0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xD4,
0x7A, 0x1D, 0x27, 0xBA, 0x2B, 0x3A, 0x67, 0xB2, 0x91, 0x6A, 0xFB, 0xE7,
0x83, 0x44, 0xCA, 0xED, 0x1C, 0x75, 0xAD, 0xDD, 0x4D, 0x83, 0x62, 0xD6,
0xAA, 0x68, 0x95, 0xB2, 0x24, 0x21, 0x7B, 0x15, 0xAE, 0x2A, 0x99, 0x68,
0x15, 0xED, 0x66, 0xF0, 0xB8, 0x58, 0xE7, 0xD3, 0xF5, 0x2E, 0xC6, 0xD9,
0x2A, 0x5E, 0xE7, 0x0E, 0x2E, 0xE7, 0xFC, 0x67, 0x59, 0xC0, 0xC8, 0x61,
0x7D, 0x4B, 0xA4, 0x6F, 0xDD, 0x9F, 0xD9, 0xC8, 0x85, 0x87, 0x64, 0xC7,
0xBA, 0x1A, 0x0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, 0xB5, 0x33, 0x7F, 0xC7, 0x21, 0xBD, 0x28, 0xEF,
0xFE, 0x66, 0x74, 0x03
};
static const unsigned char CERT1[] = {
0x30, 0x82, 0x03, 0x34, 0x30, 0x82, 0x02, 0x1C, 0xA0, 0x03, 0x02, 0x01,
0x02, 0x02, 0x14, 0x70, 0x0F, 0x60, 0xEE, 0xF0, 0x07, 0x9A, 0x9C, 0x69,
0xAC, 0x97, 0x62, 0x26, 0x4F, 0x5D, 0x05, 0xB6, 0xF9, 0x8F, 0x48, 0x30,
0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
0x05, 0x00, 0x30, 0x1C, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
0x04, 0x03, 0x13, 0x04, 0x52, 0x6F, 0x6F, 0x74, 0x30, 0x1E, 0x17, 0x0D,
0x31, 0x30, 0x30, 0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
0x5A, 0x17, 0x0D, 0x33, 0x37, 0x31, 0x32, 0x33, 0x31, 0x32, 0x33, 0x35,
0x39, 0x35, 0x39, 0x5A, 0x30, 0x27, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03,
0x55, 0x04, 0x06, 0x13, 0x02, 0x43, 0x41, 0x31, 0x18, 0x30, 0x16, 0x06,
0x03, 0x55, 0x04, 0x03, 0x13, 0x0F, 0x49, 0x6E, 0x74, 0x65, 0x72, 0x6D,
0x65, 0x64, 0x69, 0x61, 0x74, 0x65, 0x20, 0x43, 0x41, 0x30, 0x82, 0x01,
0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01,
0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82, 0x01,
0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0xDA, 0x66, 0x20, 0xF4, 0xF8, 0x0F, 0xF6, 0xDC, 0x2F, 0x72, 0x97,
0x6B, 0x78, 0x40, 0x79, 0x9D, 0x5B, 0x6D, 0xBE, 0x03, 0xCE, 0x70, 0x42,
0xA8, 0x2A, 0x71, 0x11, 0xDF, 0x2E, 0x81, 0x01, 0xEA, 0x86, 0x12, 0x91,
0x4F, 0xD0, 0x18, 0xED, 0x84, 0xA4, 0x48, 0x30, 0xB9, 0xB9, 0x7B, 0x56,
0x3E, 0xEF, 0x8D, 0xE1, 0x1F, 0xFD, 0x90, 0xB2, 0x44, 0x85, 0x58, 0x6F,
0xA7, 0x30, 0x74, 0x7C, 0xF1, 0xAE, 0x8C, 0x99, 0x3F, 0xF1, 0x4D, 0x57,
0xC5, 0x95, 0x68, 0xC2, 0x36, 0xB2, 0xEC, 0xC2, 0x68, 0x7A, 0xC0, 0x81,
0xA9, 0x55, 0x05, 0xE7, 0xCF, 0xAF, 0x50, 0xAC
};
static const br_x509_certificate SERVER_CERTIFICATE_CHAIN[] = {
{ (unsigned char *)CERT0, sizeof CERT0 },
{ (unsigned char *)CERT1, sizeof CERT1 }
};
#define SERVER_CERTIFICATE_CHAIN_LEN 2

View file

@ -0,0 +1,108 @@
/*
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "bearssl.h"
/*
* The private key for the server certificate (RSA).
*/
static const unsigned char RSA_P[] = {
0xF9, 0xF6, 0x32, 0x9C, 0x51, 0xD7, 0x71, 0xB2, 0x62, 0xED, 0x81, 0x24,
0xDA, 0xF6, 0xA9, 0x2C, 0xC3, 0x2D, 0x55, 0x94, 0x95, 0x51, 0xE5, 0x36,
0x12, 0x9E, 0x2E, 0x1F, 0x8E, 0xD8, 0x22, 0x22, 0x64, 0x5E, 0xA3, 0xFC,
0x60, 0xF0, 0x14, 0xDC, 0x10, 0x48, 0x6F, 0x4F, 0x1A, 0x7D, 0xCC, 0x37,
0x23, 0x27, 0x76, 0xB6, 0x31, 0xAA, 0xDE, 0x70, 0xC9, 0xEC, 0xAB, 0x0A,
0x50, 0xF1, 0xEA, 0x79, 0x79, 0x74, 0xBE, 0x3B, 0xAB, 0x2E, 0x63, 0x97,
0xA2, 0x6F, 0x0A, 0x45, 0x7B, 0xD7, 0x6C, 0xD2, 0x6D, 0x1B, 0x70, 0x31,
0x03, 0xAF, 0x26, 0x8B, 0x28, 0xC5, 0xF8, 0x49, 0x67, 0x45, 0x41, 0xF1,
0xD3, 0x35, 0x05, 0x27, 0x2A, 0x79, 0x21, 0xDA, 0x06, 0xB6, 0x3D, 0xA2,
0xE8, 0x3C, 0x40, 0xFA, 0x6F, 0xC4, 0xFC, 0xF5, 0xC4, 0x05, 0xEB, 0x9A,
0x31, 0xAA, 0x40, 0x7E, 0x5E, 0xEE, 0x22, 0x5B
};
static const unsigned char RSA_Q[] = {
0xD9, 0x9C, 0x1A, 0xDC, 0x94, 0x51, 0xF6, 0xC1, 0x96, 0xDA, 0x0E, 0x11,
0x3F, 0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0xE5, 0xB9, 0xD4, 0xF2, 0x42, 0x55,
0x09, 0x76, 0x4F, 0x4C, 0x3D, 0x9D, 0x25, 0x09
};
static const unsigned char RSA_IQ[] = {
0x72, 0x0C, 0xA7, 0xCF, 0x06, 0x95, 0x69, 0xF4, 0x75, 0x20, 0x34, 0x03,
0xE0, 0xCF, 0x9A, 0x51, 0x93, 0xF5, 0x42, 0x2E, 0xF2, 0x85, 0xBE, 0xCE,
0x4F, 0x38, 0xB5, 0x8C, 0xA2, 0x99, 0x42, 0xF3, 0xBD, 0x65, 0x38, 0xE2,
0x34, 0x3F, 0x21, 0x9D, 0xF5, 0xBD, 0xB3, 0xBF, 0x73, 0x3C, 0x18, 0xDE,
0xF6, 0xF0, 0x7F, 0xA1, 0xC2, 0x55, 0xF2, 0x38, 0xE9, 0x0E, 0x1E, 0x31,
0xE7, 0xDB, 0x51, 0xC5, 0x71, 0x8D, 0x67, 0x71, 0x3A, 0x9F, 0x55, 0x52,
0x60, 0xEE, 0x45, 0xF6, 0x08, 0x98, 0x81, 0xB7, 0x7B, 0x2F, 0xF2, 0x96,
0x7D, 0x73, 0xD0, 0xA6, 0xAB, 0xAA, 0x83, 0x49, 0x41, 0x35, 0xA9, 0x90,
0x67, 0xCE, 0xD3, 0xB9, 0x73, 0x54, 0xAA, 0x84, 0x00, 0x88, 0x88, 0x90,
0x1D, 0x86, 0x9B, 0xE5, 0xB8, 0xCE, 0x89, 0x0A, 0x1B, 0x47, 0x62, 0x31,
0xC2, 0x3F, 0xC3, 0x8C, 0x86, 0x09, 0x3C, 0x86
};
static const br_rsa_private_key SERVER_PRIVATE_KEY = {
2048,
(unsigned char *)RSA_P, sizeof RSA_P,
(unsigned char *)RSA_Q, sizeof RSA_Q,
(unsigned char *)RSA_DP, sizeof RSA_DP,
(unsigned char *)RSA_DQ, sizeof RSA_DQ,
(unsigned char *)RSA_IQ, sizeof RSA_IQ
};

View file

@ -0,0 +1,188 @@
/* tls_server_bearssl - simple BearSSL TLS server that outputs some statistics to a connecting client,
* then closes the socket.
*
* Uses code from the server_basic example in BearSSL and the tls_server example.
*
* To test this program, connect to the ESP using openssl command line like this:
*
* openssl s_client -connect 192.168.66.209:800
*
* The openssl command line client will print some information for the (self-signed) server certificate,
* then after a couple of seconds (validation) there will be a few lines of text output sent from the ESP.
*
* See the certificate.h and key.h files for private key & certificate, plus information for generation.
*
* Original Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>, MIT License.
* Additions Copyright (C) 2016 Stefan Schake, MIT License.
*/
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "esp/hwrand.h"
#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "lwip/api.h"
#include "ssid_config.h"
/* Server cert & key */
#include "certificate.h"
#include "key.h"
#include "bearssl.h"
#define PORT 800
/*
* Low-level data read callback for the simplified SSL I/O API.
*/
static int
sock_read(void *ctx, unsigned char *buf, size_t len)
{
for (;;) {
ssize_t rlen;
rlen = read(*(int *)ctx, buf, len);
if (rlen <= 0) {
if (rlen < 0 && errno == EINTR) {
continue;
}
return -1;
}
return (int)rlen;
}
}
/*
* Low-level data write callback for the simplified SSL I/O API.
*/
static int
sock_write(void *ctx, const unsigned char *buf, size_t len)
{
for (;;) {
ssize_t wlen;
wlen = write(*(int *)ctx, buf, len);
if (wlen <= 0) {
if (wlen < 0 && errno == EINTR) {
continue;
}
return -1;
}
return (int)wlen;
}
}
/*
* BearSSL IO buffer and server state
*/
static unsigned char iobuf[BR_SSL_BUFSIZE_MONO];
br_ssl_server_context sc;
br_sslio_context ioc;
void tls_server_task(void *pvParameters)
{
int successes = 0, failures = 0;
printf("TLS server task starting...\n");
/* Prepare server socket */
int sfd = socket(PF_INET, SOCK_STREAM, 0);
if (sfd < 0) {
printf("Failed to allocate socket.\r\n");
return;
}
struct sockaddr_in addr;
bzero(&addr, sizeof(addr));
addr.sin_port = htons(PORT);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sfd, (const struct sockaddr*) &addr, sizeof(addr)) < 0) {
close(sfd);
printf("Failed to bind socket\r\n");
return;
}
if (listen(sfd, 0) < 0) {
close(sfd);
printf("Failed to listen\r\n");
return;
}
/* Initialize engine */
br_ssl_server_init_full_rsa(&sc, SERVER_CERTIFICATE_CHAIN, SERVER_CERTIFICATE_CHAIN_LEN, &SERVER_PRIVATE_KEY);
br_ssl_engine_set_buffer(&sc.eng, iobuf, sizeof iobuf, 0);
/*
* Inject some entropy from the ESP hardware RNG
* This is necessary because we don't support any of the BearSSL methods
*/
for (int i = 0; i < 10; i++) {
int rand = hwrand();
br_ssl_engine_inject_entropy(&sc.eng, &rand, 4);
}
while(1) {
printf("Top of the loop, free heap = %u\r\n", xPortGetFreeHeapSize());
/* Accept a new client */
struct sockaddr_in sa;
socklen_t sa_len = sizeof(sa);
int cfd = accept(sfd, (struct sockaddr*)&sa, &sa_len);
if (cfd < 0) {
continue;
}
/* Prepare for a new handshake */
br_ssl_server_reset(&sc);
/* Initialize the simplified IO wrapper */
br_sslio_init(&ioc, &sc.eng, sock_read, &cfd, sock_write, &cfd);
/* Prepare a message to the client */
unsigned char buf[100];
int len = sprintf((char *) buf, "O hai, client %d.%d.%d.%d:%d\r\nFree heap size is %d bytes\r\n",
ip4_addr1(&sa.sin_addr), ip4_addr2(&sa.sin_addr),
ip4_addr3(&sa.sin_addr), ip4_addr4(&sa.sin_addr),
ntohs(sa.sin_port), xPortGetFreeHeapSize());
/* Send the message and close the connection */
br_sslio_write_all(&ioc, buf, len);
br_sslio_close(&ioc);
/* Check if something bad happened */
if (br_ssl_engine_last_error(&sc.eng) != BR_ERR_OK) {
close(cfd);
printf("failure, error = %d\r\n", br_ssl_engine_last_error(&sc.eng));
failures++;
continue;
}
close(cfd);
successes++;
printf("successes = %d failures = %d\r\n", successes, failures);
printf("Waiting for next client...\r\n");
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
struct sdk_station_config config = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
};
/* required to call wifi_set_opmode before station_set_config */
sdk_wifi_set_opmode(STATION_MODE);
sdk_wifi_station_set_config(&config);
xTaskCreate(&tls_server_task, "server_task", 2048, NULL, 2, NULL);
}

@ -0,0 +1 @@
Subproject commit f0c00466018e4bcdaa2d965ac723d53f015cde9a

View file

@ -0,0 +1,14 @@
# Component makefile for BearSSL
BEARSSL_DIR = $(bearssl_ROOT)BearSSL/
INC_DIRS += $(BEARSSL_DIR)inc
# args for passing into compile rule generation
bearssl_INC_DIR = $(BEARSSL_DIR)inc $(BEARSSL_DIR)src
bearssl_SRC_DIR = $(BEARSSL_DIR)src $(sort $(dir $(wildcard $(BEARSSL_DIR)src/*/)))
$(eval $(call component_compile_rules,bearssl))
# Helpful error if git submodule not initialised
$(BEARSSL_DIR):
$(error "bearssl git submodule not installed. Please run 'git submodule update --init'")