Skip to content

Commit 2c34df1

Browse files
authored
Merge pull request #80 from pennam/aiotcloud
Extend library configurability using config file
2 parents d7a68ae + 504a875 commit 2c34df1

File tree

8 files changed

+88
-11
lines changed

8 files changed

+88
-11
lines changed

Diff for: .github/workflows/compile-examples.yml

+15
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ jobs:
4848
- fqbn: arduino:samd:mkrgsm1400
4949
type: gsm
5050
artifact-name-suffix: arduino-samd-mkrgsm1400
51+
- fqbn: arduino:samd:mkrnb1500
52+
type: nb
53+
artifact-name-suffix: arduino-samd-mkrnb1500
54+
- fqbn: arduino:mbed_portenta:envie_m7
55+
type: mbed_portenta
56+
artifact-name-suffix: arduino-mbed_portenta-envie_m7
57+
- fqbn: arduino:mbed_nano:nanorp2040connect
58+
type: nina
59+
artifact-name-suffix: arduino-mbed_nano-nanorp2040connect
60+
- fqbn: arduino:mbed_opta:opta
61+
type: mbed_opta
62+
artifact-name-suffix: arduino-mbed_opta-opta
63+
- fqbn: arduino:mbed_giga:giga
64+
type: mbed_giga
65+
artifact-name-suffix: arduino-mbed_giga-giga
5166
- fqbn: arduino:megaavr:uno2018
5267
type: megaavr
5368
artifact-name-suffix: arduino-megaavr-uno2018

Diff for: src/AES128.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* SOFTWARE.
2323
*/
2424

25+
#include <ArduinoBearSSL.h>
2526
#include "AES128.h"
2627

2728
AES128Class::AES128Class() :
@@ -49,6 +50,6 @@ int AES128Class::runDecryption(uint8_t *key, size_t size, uint8_t *input, size_t
4950
return 1;
5051
}
5152

52-
#ifndef ARDUINO_ARCH_MEGAAVR
53+
#if !defined(ARDUINO_BEARSSL_DISABLE_AES128) && !defined(ARDUINO_ARCH_MEGAAVR)
5354
AES128Class AES128;
5455
#endif

Diff for: src/BearSSLClient.cpp

+46-4
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,33 @@
2828
#include <ArduinoECCX08.h>
2929
#endif
3030

31+
#ifndef ARDUINO_BEARSSL_DISABLE_BUILTIN_TRUST_ANCHORS
3132
#include "BearSSLTrustAnchors.h"
33+
#endif
3234
#include "utility/eccX08_asn1.h"
3335

3436
#include "BearSSLClient.h"
3537

38+
#ifndef ARDUINO_BEARSSL_DISABLE_BUILTIN_TRUST_ANCHORS
3639
BearSSLClient::BearSSLClient(Client& client) :
3740
BearSSLClient(&client, TAs, TAs_NUM)
3841
{
3942
}
43+
#endif
44+
45+
BearSSLClient::BearSSLClient() :
46+
_noSNI(false)
47+
{
48+
_ecKey.curve = 0;
49+
_ecKey.x = NULL;
50+
_ecKey.xlen = 0;
51+
52+
for (size_t i = 0; i < BEAR_SSL_CLIENT_CHAIN_SIZE; i++) {
53+
_ecCert[i].data = NULL;
54+
_ecCert[i].data_len = 0;
55+
}
56+
_ecCertDynamic = false;
57+
}
4058

4159
BearSSLClient::BearSSLClient(Client& client, const br_x509_trust_anchor* myTAs, int myNumTAs)
4260
: BearSSLClient(&client, myTAs, myNumTAs)
@@ -48,8 +66,15 @@ BearSSLClient::BearSSLClient(Client* client, const br_x509_trust_anchor* myTAs,
4866
_TAs(myTAs),
4967
_numTAs(myNumTAs),
5068
_noSNI(false),
69+
#ifndef ARDUINO_BEARSSL_DISABLE_KEY_DECODER
5170
_skeyDecoder(NULL),
52-
_ecChainLen(0)
71+
#endif
72+
_ecChainLen(0),
73+
#ifndef ARDUINO_BEARSSL_DISABLE_FULL_CLIENT_PROFILE
74+
_br_ssl_client_init_function(br_ssl_client_init_full)
75+
#else
76+
_br_ssl_client_init_function(NULL)
77+
#endif
5378
{
5479
#ifndef ARDUINO_DISABLE_ECCX08
5580
_ecVrfy = eccX08_vrfy_asn1;
@@ -77,10 +102,12 @@ BearSSLClient::~BearSSLClient()
77102
_ecCert[0].data = NULL;
78103
}
79104

105+
#ifndef ARDUINO_BEARSSL_DISABLE_KEY_DECODER
80106
if (_skeyDecoder) {
81107
free(_skeyDecoder);
82108
_skeyDecoder = NULL;
83109
}
110+
#endif
84111
}
85112

86113
int BearSSLClient::connect(IPAddress ip, uint16_t port)
@@ -309,6 +336,7 @@ void BearSSLClient::setEccSlot(int ecc508KeySlot, const char cert[])
309336
}
310337
}
311338

339+
#ifndef ARDUINO_BEARSSL_DISABLE_KEY_DECODER
312340
void BearSSLClient::setKey(const char key[], const char cert[])
313341
{
314342
// try to decode the key and cert
@@ -381,7 +409,9 @@ void BearSSLClient::setKey(const char key[], const char cert[])
381409
}
382410
}
383411
}
412+
#endif
384413

414+
#if BEAR_SSL_CLIENT_CHAIN_SIZE > 1
385415
void BearSSLClient::setEccCertParent(const char cert[])
386416
{
387417
// try to decode the cert
@@ -428,6 +458,7 @@ void BearSSLClient::setEccCertParent(const char cert[])
428458
}
429459
}
430460
}
461+
#endif
431462

432463
int BearSSLClient::errorCode()
433464
{
@@ -436,8 +467,12 @@ int BearSSLClient::errorCode()
436467

437468
int BearSSLClient::connectSSL(const char* host)
438469
{
439-
// initialize client context with all algorithms and hardcoded trust anchors
440-
br_ssl_client_init_full(&_sc, &_xc, _TAs, _numTAs);
470+
if (!_br_ssl_client_init_function) {
471+
return 0;
472+
}
473+
474+
// initialize client context with enabled algorithms and trust anchors
475+
_br_ssl_client_init_function(&_sc, &_xc, _TAs, _numTAs);
441476

442477
br_ssl_engine_set_buffers_bidi(&_sc.eng, _ibuf, sizeof(_ibuf), _obuf, sizeof(_obuf));
443478

@@ -462,6 +497,7 @@ int BearSSLClient::connectSSL(const char* host)
462497

463498
// enable client auth
464499
if (_ecCert[0].data_len) {
500+
#ifndef ARDUINO_BEARSSL_DISABLE_KEY_DECODER
465501
if (_skeyDecoder) {
466502
int skeyType = br_skey_decoder_key_type(_skeyDecoder);
467503

@@ -471,8 +507,11 @@ int BearSSLClient::connectSSL(const char* host)
471507
br_ssl_client_set_single_rsa(&_sc, _ecCert, _ecChainLen, br_skey_decoder_get_rsa(_skeyDecoder), br_rsa_pkcs1_sign_get_default());
472508
}
473509
} else {
510+
#endif
474511
br_ssl_client_set_single_ec(&_sc, _ecCert, _ecChainLen, &_ecKey, BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN, BR_KEYTYPE_EC, br_ec_get_default(), _ecSign);
512+
#ifndef ARDUINO_BEARSSL_DISABLE_KEY_DECODER
475513
}
514+
#endif
476515
}
477516

478517
// set the hostname used for SNI
@@ -575,18 +614,21 @@ void BearSSLClient::clientAppendCert(void *ctx, const void *data, size_t len)
575614
c->_ecCert[0].data_len += len;
576615
}
577616

617+
#ifndef ARDUINO_BEARSSL_DISABLE_KEY_DECODER
578618
void BearSSLClient::clientAppendKey(void *ctx, const void *data, size_t len)
579619
{
580620
BearSSLClient* c = (BearSSLClient*)ctx;
581621

582622
br_skey_decoder_push(c->_skeyDecoder, data, len);
583623
}
624+
#endif
584625

626+
#if BEAR_SSL_CLIENT_CHAIN_SIZE > 1
585627
void BearSSLClient::parentAppendCert(void *ctx, const void *data, size_t len)
586628
{
587629
BearSSLClient* c = (BearSSLClient*)ctx;
588630

589631
memcpy(&c->_ecCert[1].data[c->_ecCert[1].data_len], data, len);
590632
c->_ecCert[1].data_len += len;
591633
}
592-
634+
#endif

Diff for: src/BearSSLClient.h

+16-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#endif
3333

3434
#ifndef BEAR_SSL_CLIENT_IBUF_SIZE
35-
#define BEAR_SSL_CLIENT_IBUF_SIZE 32768
35+
#define BEAR_SSL_CLIENT_IBUF_SIZE (16384 + 325)
3636
#endif
3737

3838
#else
@@ -59,14 +59,15 @@
5959
class BearSSLClient : public Client {
6060

6161
public:
62+
BearSSLClient();
6263
BearSSLClient(Client& client);
6364
BearSSLClient(Client& client, const br_x509_trust_anchor* myTAs, int myNumTAs);
6465
BearSSLClient(Client* client, const br_x509_trust_anchor* myTAs, int myNumTAs);
6566
virtual ~BearSSLClient();
6667

67-
6868
inline void setClient(Client& client) { _client = &client; }
69-
69+
inline void setProfile(void(*client_init_function)(br_ssl_client_context *cc, br_x509_minimal_context *xc, const br_x509_trust_anchor *trust_anchors, size_t trustrust_anchorst_anchors_num)) { _br_ssl_client_init_function = client_init_function; }
70+
inline void setTrustAnchors(const br_x509_trust_anchor* myTAs, int myNumTAs) { _TAs = myTAs; _numTAs = myNumTAs; }
7071

7172
virtual int connect(IPAddress ip, uint16_t port);
7273
virtual int connect(const char* host, uint16_t port);
@@ -97,8 +98,12 @@ class BearSSLClient : public Client {
9798

9899
void setEccSlot(int ecc508KeySlot, const byte cert[], int certLength);
99100
void setEccSlot(int ecc508KeySlot, const char cert[]);
101+
#ifndef ARDUINO_BEARSSL_DISABLE_KEY_DECODER
100102
void setKey(const char key[], const char cert[]);
103+
#endif
104+
#if BEAR_SSL_CLIENT_CHAIN_SIZE > 1
101105
void setEccCertParent(const char cert[]);
106+
#endif
102107

103108
int errorCode();
104109

@@ -107,8 +112,12 @@ class BearSSLClient : public Client {
107112
static int clientRead(void *ctx, unsigned char *buf, size_t len);
108113
static int clientWrite(void *ctx, const unsigned char *buf, size_t len);
109114
static void clientAppendCert(void *ctx, const void *data, size_t len);
115+
#ifndef ARDUINO_BEARSSL_DISABLE_KEY_DECODER
110116
static void clientAppendKey(void *ctx, const void *data, size_t len);
117+
#endif
118+
#if BEAR_SSL_CLIENT_CHAIN_SIZE > 1
111119
static void parentAppendCert(void *ctx, const void *data, size_t len);
120+
#endif
112121

113122
private:
114123
Client* _client;
@@ -121,7 +130,9 @@ class BearSSLClient : public Client {
121130
br_ecdsa_sign _ecSign;
122131

123132
br_ec_private_key _ecKey;
133+
#ifndef ARDUINO_BEARSSL_DISABLE_KEY_DECODER
124134
br_skey_decoder_context* _skeyDecoder;
135+
#endif
125136
br_x509_certificate _ecCert[BEAR_SSL_CLIENT_CHAIN_SIZE];
126137
int _ecChainLen;
127138
bool _ecCertDynamic;
@@ -131,6 +142,8 @@ class BearSSLClient : public Client {
131142
unsigned char _ibuf[BEAR_SSL_CLIENT_IBUF_SIZE];
132143
unsigned char _obuf[BEAR_SSL_CLIENT_OBUF_SIZE];
133144
br_sslio_context _ioc;
145+
146+
void (*_br_ssl_client_init_function)(br_ssl_client_context *cc, br_x509_minimal_context *xc, const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num);
134147
};
135148

136149
#endif

Diff for: src/DES.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* SOFTWARE.
2323
*/
2424

25+
#include <ArduinoBearSSL.h>
2526
#include "DES.h"
2627

2728
DESClass::DESClass() :
@@ -50,6 +51,6 @@ int DESClass::runDecryption(uint8_t *key, size_t size, uint8_t *input, size_t bl
5051
}
5152

5253

53-
#ifndef ARDUINO_ARCH_MEGAAVR
54+
#if !defined(ARDUINO_BEARSSL_DISABLE_DES) && !defined(ARDUINO_ARCH_MEGAAVR)
5455
DESClass DES;
5556
#endif

Diff for: src/MD5.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* SOFTWARE.
2323
*/
2424

25+
#include <ArduinoBearSSL.h>
2526
#include "MD5.h"
2627

2728
MD5Class::MD5Class() :
@@ -54,6 +55,6 @@ int MD5Class::end(uint8_t *digest)
5455
return 1;
5556
}
5657

57-
#ifndef ARDUINO_ARCH_MEGAAVR
58+
#if !defined(ARDUINO_BEARSSL_DISABLE_MD5) && !defined(ARDUINO_ARCH_MEGAAVR)
5859
MD5Class MD5;
5960
#endif

Diff for: src/SHA1.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* SOFTWARE.
2323
*/
2424

25+
#include <ArduinoBearSSL.h>
2526
#include "SHA1.h"
2627

2728
SHA1Class::SHA1Class() :
@@ -54,4 +55,6 @@ int SHA1Class::end(uint8_t *digest)
5455
return 1;
5556
}
5657

58+
#if !defined(ARDUINO_BEARSSL_DISABLE_SHA1)
5759
SHA1Class SHA1;
60+
#endif

Diff for: src/SHA256.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* SOFTWARE.
2323
*/
2424

25+
#include <ArduinoBearSSL.h>
2526
#include "SHA256.h"
2627

2728
SHA256Class::SHA256Class() :
@@ -54,6 +55,6 @@ int SHA256Class::end(uint8_t *digest)
5455
return 1;
5556
}
5657

57-
#ifndef ARDUINO_ARCH_MEGAAVR
58+
#if !defined(ARDUINO_BEARSSL_DISABLE_SHA256) && !defined(ARDUINO_ARCH_MEGAAVR)
5859
SHA256Class SHA256;
5960
#endif

0 commit comments

Comments
 (0)