1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-12 01:53:07 +03:00

Add cryptographically signed update support (#5213)

Using a pluggable architecture, allow updates delivered via the Update
class to be verified as signed by a certificate.  By using plugins, avoid
pulling either axTLS or BearSSL into normal builds.

A signature is appended to a binary image, followed by the size of the
signature as a 32-bit int.  The updater takes a verification function
and checks this signature using whatever method it chooses, and if it
fails the update is not applied.

A SHA256 hash class is presently implemented for the signing hash (since
MD5 is a busted algorithm).

A BearSSLPublicKey based verifier is implemented for RSA keys.  The
application only needs the Public Key, while to sign you can use
OpenSSL and your private key (which should never leave your control
or be deployed on any endpoints).

An example using automatic signing is included.

Update the docs to show the signing steps and how to use it in the
automatic and manual modes.

Also remove one debugging line from the signing tool.

Saves ~600 bytes when in debug mode by moving strings to PMEM

Windows can't run the signing script, nor does it normally have OpenSSL
installed.  When trying to build an automatically signed binary, warn
and don't run the python.
This commit is contained in:
Earle F. Philhower, III
2018-12-02 19:57:47 -08:00
committed by GitHub
parent e5d50a6e4b
commit d8acfffdb0
11 changed files with 532 additions and 27 deletions

View File

@ -826,6 +826,62 @@ bool X509List::append(const uint8_t *derCert, size_t derLen) {
return true;
}
// SHA256 hash for updater
void HashSHA256::begin() {
br_sha256_init( &_cc );
memset( _sha256, 0, sizeof(_sha256) );
}
void HashSHA256::add(const void *data, uint32_t len) {
br_sha256_update( &_cc, data, len );
}
void HashSHA256::end() {
br_sha256_out( &_cc, _sha256 );
}
int HashSHA256::len() {
return sizeof(_sha256);
}
const void *HashSHA256::hash() {
return (const void*) _sha256;
}
// SHA256 verifier
uint32_t SigningVerifier::length()
{
if (!_pubKey) {
return 0;
} else if (_pubKey->isRSA()) {
return _pubKey->getRSA()->nlen;
} else if (_pubKey->isEC()) {
return _pubKey->getEC()->qlen;
} else {
return 0;
}
}
bool SigningVerifier::verify(UpdaterHashClass *hash, const void *signature, uint32_t signatureLen) {
if (!_pubKey || !hash || !signature || signatureLen != length()) return false;
if (_pubKey->isRSA()) {
bool ret;
unsigned char vrf[hash->len()];
br_rsa_pkcs1_vrfy vrfy = br_rsa_pkcs1_vrfy_get_default();
ret = vrfy((const unsigned char *)signature, signatureLen, NULL, sizeof(vrf), _pubKey->getRSA(), vrf);
if (!ret || memcmp(vrf, hash->hash(), sizeof(vrf)) ) {
return false;
} else {
return true;
}
} else {
br_ecdsa_vrfy vrfy = br_ecdsa_vrfy_raw_get_default();
// The EC verifier actually does the compare, unlike the RSA one
return vrfy(br_ec_get_default(), hash->hash(), hash->len(), _pubKey->getEC(), (const unsigned char *)signature, signatureLen);
}
};
#if !CORE_MOCK
// Second stack thunked helpers

View File

@ -24,6 +24,8 @@
#define _BEARSSLHELPERS_H
#include <bearssl/bearssl.h>
#include <Updater.h>
// Internal opaque structures, not needed by user applications
namespace brssl {
@ -136,6 +138,31 @@ class Session {
br_ssl_session_parameters _session;
};
// Updater SHA256 hash and signature verification
class HashSHA256 : public UpdaterHashClass {
public:
virtual void begin() override;
virtual void add(const void *data, uint32_t len) override;
virtual void end() override;
virtual int len() override;
virtual const void *hash() override;
private:
br_sha256_context _cc;
unsigned char _sha256[32];
};
class SigningVerifier : public UpdaterVerifyClass {
public:
virtual uint32_t length() override;
virtual bool verify(UpdaterHashClass *hash, const void *signature, uint32_t signatureLen) override;
public:
SigningVerifier(PublicKey *pubKey) { _pubKey = pubKey; }
private:
PublicKey *_pubKey;
};
// Stack thunked versions of calls
extern "C" {
extern unsigned char *thunk_br_ssl_engine_recvapp_buf( const br_ssl_engine_context *cc, size_t *len);