Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit e77db6e

Browse files
authored
v1.9.3 to add support to Seeeduino nRF52
### Releases v1.9.3 1. Add support to Seeeduino nRF52840-based boards such as **Seeed XIAO_NRF52840 and XIAO_NRF52840_SENSE**, etc. using Seeed `mbed` or `nRF52` core 2. Add astyle using `allman` style. Restyle the library 3. Display warning only when `_ETHERNET_WEBSERVER_LOGLEVEL_` > 3 4. Update examples 5. Update `Packages' Patches` to add Seeeduino `nRF52` core
1 parent 85147cb commit e77db6e

21 files changed

+817
-706
lines changed

src/SSLClient/SSLClient.h

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
Check out README.md for more info.
3434
*/
3535

36-
class EthernetSSLClient : public Client
36+
class EthernetSSLClient : public Client
3737
{
3838
public:
3939
/**
@@ -44,7 +44,7 @@ class EthernetSSLClient : public Client
4444
checking the errors, you can do so with EthernetSSLClient::getWriteError(),
4545
which will return one of these values.
4646
*/
47-
enum Error
47+
enum Error
4848
{
4949
SSL_OK = 0,
5050
/** The underlying client failed to connect, probably not an issue with SSL */
@@ -67,7 +67,7 @@ class EthernetSSLClient : public Client
6767
Use these values when initializing EthernetSSLClient to set how many logs you
6868
would like to see in the Serial monitor.
6969
*/
70-
enum DebugLevel
70+
enum DebugLevel
7171
{
7272
/** No logging output */
7373
SSL_NONE = 0,
@@ -212,9 +212,9 @@ class EthernetSSLClient : public Client
212212
fails to become ready for writing data.
213213
*/
214214
size_t write(const uint8_t *buf, size_t size) override;
215-
215+
216216
/** @see EthernetSSLClient::write(uint8_t*, size_t) */
217-
size_t write(uint8_t b) override
217+
size_t write(uint8_t b) override
218218
{
219219
return write(&b, 1);
220220
}
@@ -265,10 +265,10 @@ class EthernetSSLClient : public Client
265265
@brief Read a single byte, or -1 if none is available.
266266
@see EthernetSSLClient::read(uint8_t*, size_t)
267267
*/
268-
int read() override
268+
int read() override
269269
{
270270
uint8_t read_val;
271-
271+
272272
return read(&read_val, 1) > 0 ? read_val : -1;
273273
};
274274

@@ -361,7 +361,7 @@ class EthernetSSLClient : public Client
361361
362362
@returns The SessionCache template parameter.
363363
*/
364-
size_t getSessionCount() const
364+
size_t getSessionCount() const
365365
{
366366
return m_sessions.size();
367367
}
@@ -371,13 +371,13 @@ class EthernetSSLClient : public Client
371371
372372
@returns true if connected, false if not
373373
*/
374-
operator bool()
374+
operator bool()
375375
{
376376
return connected() > 0;
377377
}
378378

379379
/** @brief Returns a reference to the client object stored in this class. Take care not to break it. */
380-
Client& getClient()
380+
Client& getClient()
381381
{
382382
return m_client;
383383
}
@@ -386,7 +386,7 @@ class EthernetSSLClient : public Client
386386
@brief Set the timeout when waiting for an SSL response.
387387
@param t The timeout value, in milliseconds (defaults to 30 seconds if not set). Do not set to zero.
388388
*/
389-
void setTimeout(unsigned int t)
389+
void setTimeout(unsigned int t)
390390
{
391391
m_timeout = t;
392392
}
@@ -395,33 +395,33 @@ class EthernetSSLClient : public Client
395395
@brief Get the timeout when waiting for an SSL response.
396396
@returns The timeout value in milliseconds.
397397
*/
398-
unsigned int getTimeout() const
398+
unsigned int getTimeout() const
399399
{
400400
return m_timeout;
401401
}
402-
403-
/**
404-
@brief Change the time used during x509 verification to a different value.
405402

406-
This function directly calls br_x509_minimal_set_time to change the validation
407-
time used by the minimal verification engine. You can use this function if the default value
408-
of the compile time is causing issues. See https://bearssl.org/apidoc/bearssl__x509_8h.html#a7f3558b1999ce904084d578700b1002c
409-
for more information what this function does and how to use it.
403+
/**
404+
@brief Change the time used during x509 verification to a different value.
410405
411-
@param days Days are counted in a proleptic Gregorian calendar since January 1st, 0 AD.
412-
@param seconds Seconds are counted since midnight, from 0 to 86400 (a count of 86400 is possible only if a leap second happened).
406+
This function directly calls br_x509_minimal_set_time to change the validation
407+
time used by the minimal verification engine. You can use this function if the default value
408+
of the compile time is causing issues. See https://bearssl.org/apidoc/bearssl__x509_8h.html#a7f3558b1999ce904084d578700b1002c
409+
for more information what this function does and how to use it.
410+
411+
@param days Days are counted in a proleptic Gregorian calendar since January 1st, 0 AD.
412+
@param seconds Seconds are counted since midnight, from 0 to 86400 (a count of 86400 is possible only if a leap second happened).
413413
*/
414-
414+
415415
void setVerificationTime(uint32_t days, uint32_t seconds);
416416

417417
private:
418418
/** @brief Returns an instance of m_client that is polymorphic and can be used by EthernetSSLClient */
419-
Client& get_arduino_client()
419+
Client& get_arduino_client()
420420
{
421421
return m_client;
422422
}
423-
424-
const Client& get_arduino_client() const
423+
424+
const Client& get_arduino_client() const
425425
{
426426
return m_client;
427427
}
@@ -451,12 +451,12 @@ class EthernetSSLClient : public Client
451451

452452
/** @brief debugging print function, only prints if m_debug is true */
453453
template<typename T>
454-
void m_print(const T str, const char* func_name, const DebugLevel level) const
454+
void m_print(const T str, const char* func_name, const DebugLevel level) const
455455
{
456456
// check the current debug level and serial status
457-
if (level > m_debug || !Serial)
457+
if (level > m_debug || !Serial)
458458
return;
459-
459+
460460
// print prefix
461461
m_print_prefix(func_name, level);
462462
// print the message
@@ -465,19 +465,19 @@ class EthernetSSLClient : public Client
465465

466466
/** @brief Prints a info message to serial, if info messages are enabled */
467467
template<typename T>
468-
void m_info(const T str, const char* func_name) const
468+
void m_info(const T str, const char* func_name) const
469469
{
470470
m_print(str, func_name, SSL_INFO);
471471
}
472472

473473
template<typename T>
474-
void m_warn(const T str, const char* func_name) const
474+
void m_warn(const T str, const char* func_name) const
475475
{
476476
m_print(str, func_name, SSL_WARN);
477477
}
478478

479479
template<typename T>
480-
void m_error(const T str, const char* func_name) const
480+
void m_error(const T str, const char* func_name) const
481481
{
482482
m_print(str, func_name, SSL_ERROR);
483483
}
@@ -487,7 +487,7 @@ class EthernetSSLClient : public Client
487487
//============================================
488488
// create a reference the client
489489
Client& m_client;
490-
490+
491491
// also store an array of SSLSessions, so we can resume communication with multiple websites
492492
std::vector<SSLSession> m_sessions;
493493
// as well as the maximmum number of sessions we can store
@@ -520,7 +520,7 @@ class EthernetSSLClient : public Client
520520
unsigned char m_iobuf[2048];
521521
//unsigned char m_iobuf[4096];
522522
//////
523-
523+
524524
// store the index of where we are writing in the buffer
525525
// so we can send our records all at once to prevent
526526
// weird timing issues

src/SSLClient/SSLClientParameters.cpp

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@
2727

2828
#include "SSLClient/SSLClientParameters.h"
2929

30-
struct ssl_pem_decode_state
30+
struct ssl_pem_decode_state
3131
{
3232
std::vector<char>* vect;
3333
size_t index = 0;
3434
};
3535

36-
static void ssl_pem_decode_callback(void *dest_ctx, const void *src, size_t len)
36+
static void ssl_pem_decode_callback(void *dest_ctx, const void *src, size_t len)
3737
{
3838
ssl_pem_decode_state* ctx = static_cast<ssl_pem_decode_state*>(dest_ctx);
39-
40-
for (size_t i = 0; i < len; i++)
39+
40+
for (size_t i = 0; i < len; i++)
4141
ctx->vect->emplace_back(static_cast<const char*>(src)[i]);
42-
42+
4343
// update index
4444
ctx->index += len;
4545
}
@@ -52,50 +52,55 @@ static void ssl_pem_decode_callback(void *dest_ctx, const void *src, size_t len)
5252
@param len Number of characters to process, MUST include a whole certificate.
5353
@return A vector of bytes representing the certificate in DER format.
5454
*/
55-
static std::vector<char> make_vector_pem(const char* data, const size_t len)
55+
static std::vector<char> make_vector_pem(const char* data, const size_t len)
5656
{
57-
if (data == nullptr || len < 80)
57+
if (data == nullptr || len < 80)
5858
return {};
59-
59+
6060
// initialize the bearssl PEM context
6161
br_pem_decoder_context pctx;
62+
6263
br_pem_decoder_init(&pctx);
63-
64+
6465
// create a temporary vector
6566
std::vector<char> temp;
67+
6668
// initialize the DER storage context
6769
ssl_pem_decode_state state;
70+
6871
state.vect = &temp;
72+
6973
state.index = 0;
70-
74+
7175
// set the byte reciever
7276
br_pem_decoder_setdest(&pctx, &ssl_pem_decode_callback, &state);
73-
77+
7478
// start decoding!
7579
int br_state = 0;
80+
7681
size_t index = 0;
77-
78-
do
82+
83+
do
7984
{
8085
index += br_pem_decoder_push(&pctx, static_cast<const void*>(&data[index]), len - index);
8186
br_state = br_pem_decoder_event(&pctx);
82-
87+
8388
// if we found the begining object, reserve the vector based on the remaining relavent bytes
84-
if (br_state == BR_PEM_BEGIN_OBJ)
89+
if (br_state == BR_PEM_BEGIN_OBJ)
8590
{
8691
// 22 = five dashes for header and footer + four newlines - character difference between `BEGIN` and `END`
8792
const size_t relavant_bytes_base64 = len - (2 * strlen(br_pem_decoder_name(&pctx)) + 22);
8893
temp.reserve(relavant_bytes_base64 * 3 / 4);
8994
}
9095
} while (br_state != BR_PEM_ERROR && br_state != BR_PEM_END_OBJ && len != index);
91-
96+
9297
// error check
93-
if (br_state == BR_PEM_ERROR)
98+
if (br_state == BR_PEM_ERROR)
9499
{
95100
// set data to error
96101
temp.clear();
97102
}
98-
103+
99104
// else we're good!
100105
return temp;
101106
}
@@ -108,30 +113,33 @@ static std::vector<char> make_vector_pem(const char* data, const size_t len)
108113
@returns context used by BearSSL to store information about the keys. You can
109114
use the br_skey_* family of APIs to access information from this context.
110115
*/
111-
static br_skey_decoder_context make_key_from_der(const std::vector<char>& der)
116+
static br_skey_decoder_context make_key_from_der(const std::vector<char>& der)
112117
{
113118
br_skey_decoder_context out;
114119
br_skey_decoder_init(&out);
115120
br_skey_decoder_push(&out, der.data(), der.size());
116-
121+
117122
return out;
118123
}
119124

120125
/* See SSLClientParams.h */
121-
SSLClientParameters::SSLClientParameters(const char* cert, const size_t cert_len, const char* key, const size_t key_len, bool is_der)
126+
SSLClientParameters::SSLClientParameters(const char* cert, const size_t cert_len, const char* key, const size_t key_len,
127+
bool is_der)
122128
: m_cert(is_der ? std::vector<char>(cert, cert + cert_len) : make_vector_pem(cert, cert_len))
123129
, m_cert_struct{ const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(m_cert.data())), m_cert.size() }
124-
, m_key_struct( make_key_from_der( is_der ? std::vector<char>(key, key + key_len) : make_vector_pem(key, key_len) ) )
130+
, m_key_struct( make_key_from_der( is_der ? std::vector<char>(key, key + key_len) : make_vector_pem(key, key_len) ) )
125131
{}
126132

127133
/* See SSLClientParams.h */
128-
SSLClientParameters SSLClientParameters::fromPEM(const char* cert_pem, const size_t cert_len, const char* key_pem, const size_t key_len)
134+
SSLClientParameters SSLClientParameters::fromPEM(const char* cert_pem, const size_t cert_len, const char* key_pem,
135+
const size_t key_len)
129136
{
130137
return SSLClientParameters(cert_pem, cert_len, key_pem, key_len, false);
131138
}
132139

133140
/* See SSLClientParams.h */
134-
SSLClientParameters SSLClientParameters::fromDER(const char* cert_der, const size_t cert_len, const char* key_der, const size_t key_len)
141+
SSLClientParameters SSLClientParameters::fromDER(const char* cert_der, const size_t cert_len, const char* key_der,
142+
const size_t key_len)
135143
{
136144
return SSLClientParameters(cert_der, cert_len, key_der, key_len, true);
137145
}

src/SSLClient/SSLClientParameters.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
ECC certificates if possible, as SSLClientParameters will make a copy of both the
4949
certificate and the private key in memory, and ECC keys tend to be smaller than RSA ones.
5050
*/
51-
class SSLClientParameters
51+
class SSLClientParameters
5252
{
5353
public:
5454
/**
@@ -81,7 +81,8 @@ class SSLClientParameters
8181
@param key_len The number of bytes in key_pem
8282
@return An SSLClientParameters context, to be used with SSLClient::setMutualAuthParams.
8383
*/
84-
static SSLClientParameters fromPEM(const char* cert_pem, const size_t cert_len, const char* key_pem, const size_t key_len);
84+
static SSLClientParameters fromPEM(const char* cert_pem, const size_t cert_len, const char* key_pem,
85+
const size_t key_len);
8586

8687
/**
8788
@brief Create mutual authentication parameters from a DER certificate and private key
@@ -109,28 +110,29 @@ class SSLClientParameters
109110
@param key_len The number of bytes in key_ders
110111
@return An SSLClientParameters context, to be used with SSLClient::setMutualAuthParams.
111112
*/
112-
static SSLClientParameters fromDER(const char* cert_der, const size_t cert_len, const char* key_der, const size_t key_len);
113+
static SSLClientParameters fromDER(const char* cert_der, const size_t cert_len, const char* key_der,
114+
const size_t key_len);
113115

114116
/** mTLS information used by SSLClient during authentication */
115-
const br_x509_certificate* getCertChain() const
117+
const br_x509_certificate* getCertChain() const
116118
{
117119
return &m_cert_struct;
118120
}
119121

120122
/** mTLS information used by SSLClient during authentication */
121-
int getCertType() const
123+
int getCertType() const
122124
{
123125
return br_skey_decoder_key_type(&m_key_struct);
124126
}
125127

126128
/** mTLS information used by SSLClient during authentication */
127-
const br_ec_private_key* getECKey() const
129+
const br_ec_private_key* getECKey() const
128130
{
129131
return br_skey_decoder_get_ec(&m_key_struct);
130132
}
131133

132134
/** mTLS information used by SSLClient during authentication */
133-
const br_rsa_private_key* getRSAKey() const
135+
const br_rsa_private_key* getRSAKey() const
134136
{
135137
return br_skey_decoder_get_rsa(&m_key_struct);
136138
}

0 commit comments

Comments
 (0)