Skip to content

Commit

Permalink
Fix internal clock setting for BearSSL if root CA was set (ESP8266 Co…
Browse files Browse the repository at this point in the history
…re SDK 2.5.x or later).
  • Loading branch information
mobizt committed Nov 13, 2019
1 parent 1c86495 commit 035eecf
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 57 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase Realtime Database Arduino Library for ESP8266


Google's Firebase Realtime Database Arduino Library for ESP8266 v 2.6.7
Google's Firebase Realtime Database Arduino Library for ESP8266 v 2.6.8


This library supports ESP8266 MCU from Espressif. The following are platforms which library are also available.
Expand Down Expand Up @@ -48,7 +48,7 @@ This library supports ESP8266 MCU from Espressif. The following are platforms wh

## Changes from earlier version

For library v 2.6.7 (comes with FirebaseJson v 2.2.5) or later, FirebaseJson object will be used to handle JSON data instead of JSON string which, the following functions are affected:
For library v 2.6.8 (comes with FirebaseJson v 2.2.5) or later, FirebaseJson object will be used to handle JSON data instead of JSON string which, the following functions are affected:

getJson, setJson, pushJson, updateNode and updateNodeSilent.

Expand Down
7 changes: 5 additions & 2 deletions examples/Basic_with_rootCA/Basic_with_rootCA.ino
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@ void setup()
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();


//GMT time offset in hour is required to set time in order to make BearSSL data decryption/encryption to work.
//This parameter is only required in ESP8266 Core SDK v2 .5.x or later.
//Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH, root_ca, 9.3);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH, root_ca);

/*
//The following feature is for ESP8266 Arduino Core SDK v2.5.x.
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=Firebase ESP8266 Client

version=2.6.7
version=2.6.8

author=Mobizt

Expand Down
47 changes: 26 additions & 21 deletions src/FirebaseESP8266.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Google's Firebase Realtime Database Arduino Library for ESP8266, version 2.6.7
* Google's Firebase Realtime Database Arduino Library for ESP8266, version 2.6.8
*
* November 12, 2019
* November 13, 2019
*
* Feature Added:
*
* Feature Fixed:
* - FirebaseJson array parsing.
* - Fix internal clock setting for BearSSL if root CA was set (ESP8266 Core SDK 2.5.x or later).
*
*
* This library provides ESP8266 to perform REST API by GET PUT, POST, PATCH, DELETE data from/to with Google's Firebase database using get, set, update
Expand Down Expand Up @@ -151,22 +151,26 @@ void FirebaseESP8266::begin(const String &host, const String &auth)
delete[] tmp;
}

void FirebaseESP8266::begin(const String &host, const String &auth, const char *rootCA)
void FirebaseESP8266::begin(const String &host, const String &auth, const char *rootCA, float GMTOffset)
{
begin(host, auth);
if (rootCA)
{
setClock();
if (GMTOffset >= -12.0 && GMTOffset <= 14.0)
_gmtOffset = GMTOffset;
setClock(_gmtOffset);
_rootCA = std::shared_ptr<const char>(rootCA);
}
}

void FirebaseESP8266::begin(const String &host, const String &auth, const String &rootCAFile, uint8_t storageType)
void FirebaseESP8266::begin(const String &host, const String &auth, const String &rootCAFile, uint8_t storageType, float GMTOffset)
{
begin(host, auth);
if (rootCAFile.length() > 0)
{
setClock();
if (GMTOffset >= -12.0 && GMTOffset <= 14.0)
_gmtOffset = GMTOffset;
setClock(_gmtOffset);
_rootCAFile = rootCAFile.c_str();
_rootCAFileStoreageType = storageType;
if (storageType == StorageType::SD && !_sdOk)
Expand Down Expand Up @@ -2065,7 +2069,6 @@ int FirebaseESP8266::firebaseConnect(FirebaseData &dataObj, const std::string &p
memset(slash, 0, 10);
strcpy_P(slash, ESP8266_FIREBASE_STR_1);


if (method == FirebaseMethod::STREAM)
{
//stream path change? reset the current (keep alive) connection
Expand Down Expand Up @@ -2577,7 +2580,6 @@ bool FirebaseESP8266::sendRequest(FirebaseData &dataObj, uint8_t storageType, co
return flag;
}


bool FirebaseESP8266::clientAvailable(FirebaseData &dataObj, bool available)
{
if (!reconnect(dataObj))
Expand Down Expand Up @@ -3293,7 +3295,7 @@ bool FirebaseESP8266::getDownloadResponse(FirebaseData &dataObj)
}
else
{

if (contentLength <= 4)
{
dataObj._httpCode = _HTTP_CODE_NOT_FOUND;
Expand All @@ -3311,14 +3313,14 @@ bool FirebaseESP8266::getDownloadResponse(FirebaseData &dataObj)
{
res = dataObj._net._client->read();
if (res < 0)
continue;
continue;
c = (char)res;
if (dataObj._fileName == "" || (dataObj._fileName != "" && c != '"'))
buff[cnt] = c;
cnt++;
}
dataTime = millis();
count -=cnt;
count -= cnt;
toRead = cnt;
buff[cnt] = '\0';
if (dataObj._storageType == StorageType::SPIFFS)
Expand All @@ -3335,7 +3337,7 @@ bool FirebaseESP8266::getDownloadResponse(FirebaseData &dataObj)
else
base64_decode_file(file, buff, toRead);
}
if(cnt == 0)
if (cnt == 0)
break;
continue;
}
Expand Down Expand Up @@ -3419,7 +3421,7 @@ bool FirebaseESP8266::getDownloadResponse(FirebaseData &dataObj)
{
for (size_t i = 0; i < strlen_P(ESP8266_FIREBASE_STR_93); i++)
dataObj._net._client->read();
count = contentLength -strlen_P(ESP8266_FIREBASE_STR_93);
count = contentLength - strlen_P(ESP8266_FIREBASE_STR_93);
}
}

Expand Down Expand Up @@ -4383,14 +4385,17 @@ void FirebaseESP8266::setDataType(FirebaseData &dataObj, const char *data)

void FirebaseESP8266::setSecure(FirebaseData &dataObj)
{
dataObj._net._bsslLowBuf= _bsslLowBuf;
dataObj._net._bsslLowBuf = _bsslLowBuf;
if (dataObj._net._certType == -1)
{
if (!_clockReady)
setClock();

dataObj._net._clockReady = _clockReady;

#ifndef USING_AXTLS
if (!_clockReady && (_rootCAFile.length() > 0 || _rootCA))
{
setClock(_gmtOffset);
dataObj._net._clockReady = _clockReady;
}
#endif
if (_rootCAFile.length() == 0)
{
if (_rootCA)
Expand Down Expand Up @@ -5629,10 +5634,10 @@ char *FirebaseESP8266::rstrstr(const char *haystack, const char *needle)
return 0;
}

void FirebaseESP8266::setClock()
void FirebaseESP8266::setClock(float offset)
{
reconnect();
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
configTime(offset * 3600, 0, "pool.ntp.org", "time.nist.gov");
time_t now = time(nullptr);
uint8_t tryCount = 0;
while (now < 8 * 3600 * 2)
Expand Down
23 changes: 10 additions & 13 deletions src/FirebaseESP8266.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Google's Firebase Realtime Database Arduino Library for ESP8266, version 2.6.7
* Google's Firebase Realtime Database Arduino Library for ESP8266, version 2.6.8
*
* November 12, 2019
* November 13, 2019
*
* Feature Added:
*
* Feature Fixed:
* - FirebaseJson array parsing.
* - Fix internal clock setting for BearSSL if root CA was set (ESP8266 Core SDK 2.5.x or later).
*
*
* This library provides ESP8266 to perform REST API by GET PUT, POST, PATCH, DELETE data from/to with Google's Firebase database using get, set, update
Expand Down Expand Up @@ -546,15 +546,18 @@ class FirebaseESP8266
@param rootCA - Root CA certificate base64 string (PEM file).
@param rootCAFile - Root CA certificate DER file (binary).
@param StorageType - Type of storage, StorageType::SD and StorageType::SPIFFS.
@param GMTOffset - GMT time offset in hour is required to set time in order to make BearSSL
data decryption/encryption to work.
This parameter is only required for ESP8266 Core SDK v2.5.x or later.
Root CA certificate DER file is only support in Core SDK v2.5.x
*/
void begin(const String &host, const String &auth);

void begin(const String &host, const String &auth, const char *rootCA);
void begin(const String &host, const String &auth, const char *rootCA, float GMTOffset = 0.0);

void begin(const String &host, const String &auth, const String &rootCAFile, uint8_t storageType);
void begin(const String &host, const String &auth, const String &rootCAFile, uint8_t storageType, float GMTOffset = 0.0);

/*
Reconnect WiFi if lost connection.
Expand Down Expand Up @@ -1391,7 +1394,6 @@ class FirebaseESP8266

bool set(FirebaseData &dataObj, const String &path, FirebaseJsonArray &arr);


/*
Set FirebaseJsonArray object and virtual child ".priority" at the defined database path.
Expand Down Expand Up @@ -1576,7 +1578,6 @@ class FirebaseESP8266
*/
bool setTimestamp(FirebaseData &dataObj, const String &path);


/*
Update child nodes's key or exising key's value (using FirebaseJson object) under the defined database path.
Expand Down Expand Up @@ -1605,7 +1606,6 @@ class FirebaseESP8266

bool updateNode(FirebaseData &dataObj, const String &path, FirebaseJson &json, float priority);


/*
Update child nodes's key or exising key's value (using FirebaseJson object) under the defined database path.
Expand All @@ -1629,7 +1629,6 @@ class FirebaseESP8266

bool updateNodeSilent(FirebaseData &dataObj, const String &path, FirebaseJson &json, float priority);


/*
Read the any type of value at the defined database path.
Expand Down Expand Up @@ -2479,7 +2478,7 @@ class FirebaseESP8266
int strpos(const char *haystack, const char *needle, int offset);
int rstrpos(const char *haystack, const char *needle, int offset);
char *rstrstr(const char *haystack, const char *needle);
void setClock();
void setClock(float offset);

void set_scheduled_callback(callback_function_t callback)
{
Expand All @@ -2503,11 +2502,9 @@ class FirebaseESP8266
uint16_t _reconnectTimeout = 10000;
File file;
fs::File _file;

float _gmtOffset = 0.0;
};



class FirebaseData
{

Expand Down
25 changes: 11 additions & 14 deletions src/FirebaseESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* HTTP Client wrapper v1.0.5
* HTTP Client wrapper v1.0.6
*
* The MIT License (MIT)
* Copyright (c) 2019 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -41,25 +41,22 @@ FirebaseHTTPClient::~FirebaseHTTPClient()

void FirebaseHTTPClient::setRootCA(const char *rootCA)
{
if (_clockReady)
{
#ifndef USING_AXTLS

if(_bsslLowBuf)
#ifndef USING_AXTLS
if (_bsslLowBuf)
_client->setBufferSizes(512, 512);
#endif

if (rootCA)
_client->setTrustAnchors(new X509List(rootCA));

if (rootCA)
{
#ifndef USING_AXTLS
_client->setTrustAnchors(new X509List(rootCA));
#else
if (rootCA)
_client->setCACert_P(rootCA, strlen_P(rootCA));
_client->setCACert_P(rootCA, strlen_P(rootCA));
#endif

_certType = 1;
}

if (rootCA == nullptr)
else
{
#ifndef USING_AXTLS
_client->setInsecure();
Expand All @@ -75,7 +72,7 @@ void FirebaseHTTPClient::setRootCAFile(std::string &rootCAFile, uint8_t storageT

#ifndef USING_AXTLS
_sdPin = sdPin;
if(_bsslLowBuf)
if (_bsslLowBuf)
_client->setBufferSizes(512, 512);

if (_clockReady && rootCAFile.length() > 0)
Expand Down
2 changes: 1 addition & 1 deletion src/FirebaseESP8266HTTPClient.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* HTTP Client wrapper v1.0.5
* HTTP Client wrapper v1.0.6
*
* This library provides ESP8266 to perform REST API by GET PUT, POST, PATCH, DELETE data from/to with Google's Firebase database using get, set, update
* and delete calls.
Expand Down
11 changes: 8 additions & 3 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase Realtime Database Arduino Library for ESP8266


Google's Firebase Realtime Database Arduino Library for ESP8266 v 2.6.7
Google's Firebase Realtime Database Arduino Library for ESP8266 v 2.6.8


## Global functions
Expand All @@ -19,14 +19,19 @@ param **`rootCAFile`** - Root CA certificate DER file (binary).

param **`storageType`** - Type of storage, StorageType::SD and StorageType::SPIFFS.

param **`GMTOffset`** - GMT time offset in hour is required to set time in order to make BearSSL
data decryption/encryption to work.

This parameter is only required for ESP8266 Core SDK v2.5.x or later.

Root CA certificate DER file is only support in Core SDK v2.5.x

```C++
void begin(const String &host, const String &auth);

void begin(const String &host, const String &auth, const char *rootCA);
void begin(const String &host, const String &auth, const char *rootCA, float GMTOffset = 0.0);

void begin(const String &host, const String &auth, const String &rootCAFile, uint8_t storageType);
void begin(const String &host, const String &auth, const String &rootCAFile, uint8_t storageType, float GMTOffset = 0.0);
```


Expand Down

0 comments on commit 035eecf

Please sign in to comment.