-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve memory management of SSL fingerprint data (#1618)
* Improve memory management of SSL fingerprint data. Add methods to SSLFingerprints structure to manage allocation/de-allocation of fingerprint data. Create `SSLValidatorList` class for `TCPClient` and move relevant code into methods. Take care to set `SSLFingerprints` members to `nullptr` when ownership relinquished (see `TcpClient::pinCertificate()`) bugfix: In `TcpClient::onSslConnected`, the first matching validator causes checking to stop, which means the validator data for any subsequent fingerprints never gets released. This is fixed in the `SSLValidatorList` destructor. * Revise `Basic_Ssl` sample to use different web-page with fingerprint that doesn't change. Add length checks to SslFingerprints. Also change `HardwareSerial` to base on `ReadWriteStream` instead of `Stream`, allows incoming data to be streamed direct. * Review changes * Additional checks in `SslFingerprints` assignment operators * Output debug error if `SslFingerprint::setXXX` fails * Update fingerprints in `HttpClient` sample and enable checking * Move `pinCertificate` code into SslValidator Validator callback still responsible for de-allocating memory, but can be called with ssl = nullptr * Rename `SSLValidatorList` -> `SslValidatorList`, `SSLValidator` -> `SslValidator` * Accept invalid length of fingerprint to ensure validation fails Move SslFingerprint code out of header Add check for flash memory in `SslFingerprint::setValue`
- Loading branch information
Showing
13 changed files
with
411 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/**** | ||
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. | ||
* Created 2015 by Skurydin Alexey | ||
* http://github.com/anakod/Sming | ||
* All files of the Sming Core are provided under the LGPL v3 license. | ||
****/ | ||
|
||
#ifdef ENABLE_SSL | ||
|
||
#include "SslFingerprints.h" | ||
#include <user_config.h> | ||
#include "flashmem.h" | ||
|
||
static inline bool isFlashPtr(const uint8_t* ptr) | ||
{ | ||
auto addr = reinterpret_cast<uint32_t>(ptr); | ||
return addr >= INTERNAL_FLASH_START_ADDRESS; | ||
} | ||
|
||
static inline void freeValue(const uint8_t*& ptr) | ||
{ | ||
delete[] ptr; | ||
ptr = nullptr; | ||
} | ||
|
||
void SslFingerprints::free() | ||
{ | ||
freeValue(certSha1); | ||
freeValue(pkSha256); | ||
} | ||
|
||
bool SslFingerprints::setValue(const uint8_t*& value, unsigned requiredLength, const uint8_t* newValue, | ||
unsigned newLength) | ||
{ | ||
if(newValue == nullptr || newLength == 0) { | ||
freeValue(value); | ||
return true; | ||
} else { | ||
if(newLength != requiredLength) { | ||
debug_w("Warning: Invalid fingerprint length"); | ||
// Copy data anyway to prevent false positive validation | ||
} | ||
if(value == nullptr) { | ||
value = new uint8_t[requiredLength]; | ||
if(value == nullptr) { | ||
return false; | ||
} | ||
} | ||
// If new value is longer than buffer, copy short | ||
unsigned length = std::min(newLength, requiredLength); | ||
// Behave properly when source is flash memory and length is wrong or buffers misaligned | ||
if(isFlashPtr(newValue)) { | ||
memcpy_P(const_cast<uint8_t*>(value), newValue, length); | ||
} else { | ||
memcpy(const_cast<uint8_t*>(value), newValue, length); | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
SslFingerprints& SslFingerprints::operator=(SslFingerprints& source) | ||
{ | ||
if(this != &source) { | ||
freeValue(certSha1); | ||
certSha1 = source.certSha1; | ||
source.certSha1 = nullptr; | ||
|
||
freeValue(pkSha256); | ||
pkSha256 = source.pkSha256; | ||
source.pkSha256 = nullptr; | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
/** @brief Make copy of values from source */ | ||
SslFingerprints& SslFingerprints::operator=(const SslFingerprints& source) | ||
{ | ||
if(this != &source) { | ||
setSha1(source.certSha1, SHA1_SIZE); | ||
setSha256(source.pkSha256, SHA256_SIZE); | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
#endif // ENABLE_SSL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.