From 035eecff84c5462bec46b6bfe89ac5bebdc7bf48 Mon Sep 17 00:00:00 2001 From: suwatchai Date: Wed, 13 Nov 2019 20:15:00 +0700 Subject: [PATCH] Fix internal clock setting for BearSSL if root CA was set (ESP8266 Core SDK 2.5.x or later). --- README.md | 4 +- .../Basic_with_rootCA/Basic_with_rootCA.ino | 7 ++- library.properties | 2 +- src/FirebaseESP8266.cpp | 47 ++++++++++--------- src/FirebaseESP8266.h | 23 ++++----- src/FirebaseESP8266HTTPClient.cpp | 25 +++++----- src/FirebaseESP8266HTTPClient.h | 2 +- src/README.md | 11 +++-- 8 files changed, 64 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index d1ca96cd..8a9fc1ab 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. diff --git a/examples/Basic_with_rootCA/Basic_with_rootCA.ino b/examples/Basic_with_rootCA/Basic_with_rootCA.ino index dbf8afb5..4d0bef54 100644 --- a/examples/Basic_with_rootCA/Basic_with_rootCA.ino +++ b/examples/Basic_with_rootCA/Basic_with_rootCA.ino @@ -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. diff --git a/library.properties b/library.properties index 1c1b37fd..5896b8eb 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=Firebase ESP8266 Client -version=2.6.7 +version=2.6.8 author=Mobizt diff --git a/src/FirebaseESP8266.cpp b/src/FirebaseESP8266.cpp index a40e23f8..2a3189eb 100644 --- a/src/FirebaseESP8266.cpp +++ b/src/FirebaseESP8266.cpp @@ -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 @@ -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(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) @@ -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 @@ -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)) @@ -3293,7 +3295,7 @@ bool FirebaseESP8266::getDownloadResponse(FirebaseData &dataObj) } else { - + if (contentLength <= 4) { dataObj._httpCode = _HTTP_CODE_NOT_FOUND; @@ -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) @@ -3335,7 +3337,7 @@ bool FirebaseESP8266::getDownloadResponse(FirebaseData &dataObj) else base64_decode_file(file, buff, toRead); } - if(cnt == 0) + if (cnt == 0) break; continue; } @@ -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); } } @@ -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) @@ -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) diff --git a/src/FirebaseESP8266.h b/src/FirebaseESP8266.h index 1e6900e7..55c30e2b 100644 --- a/src/FirebaseESP8266.h +++ b/src/FirebaseESP8266.h @@ -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 @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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) { @@ -2503,11 +2502,9 @@ class FirebaseESP8266 uint16_t _reconnectTimeout = 10000; File file; fs::File _file; - + float _gmtOffset = 0.0; }; - - class FirebaseData { diff --git a/src/FirebaseESP8266HTTPClient.cpp b/src/FirebaseESP8266HTTPClient.cpp index 67124662..9dae688d 100644 --- a/src/FirebaseESP8266HTTPClient.cpp +++ b/src/FirebaseESP8266HTTPClient.cpp @@ -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) @@ -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(); @@ -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) diff --git a/src/FirebaseESP8266HTTPClient.h b/src/FirebaseESP8266HTTPClient.h index 064be769..3c0ddf5a 100644 --- a/src/FirebaseESP8266HTTPClient.h +++ b/src/FirebaseESP8266HTTPClient.h @@ -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. diff --git a/src/README.md b/src/README.md index 9233dd33..f5c26782 100644 --- a/src/README.md +++ b/src/README.md @@ -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 @@ -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); ```