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
3639BearSSLClient::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
4159BearSSLClient::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
86113int 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
312340void 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
385415void 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
432463int BearSSLClient::errorCode ()
433464{
@@ -436,8 +467,12 @@ int BearSSLClient::errorCode()
436467
437468int 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
578618void 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
585627void 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
0 commit comments