Skip to content

Commit dfb1779

Browse files
committed
added signature len
1 parent e99f511 commit dfb1779

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/esp32FOTA.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
4242
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
4343

44-
#define FW_SIGNATURE_LENGTH 512
4544

4645

4746
static int64_t getHTTPStream( esp32FOTA* fota, int partition );
@@ -181,13 +180,14 @@ void esp32FOTA::setConfig( FOTAConfig_t cfg )
181180
_cfg.use_device_id = cfg.use_device_id;
182181
_cfg.root_ca = cfg.root_ca;
183182
_cfg.pub_key = cfg.pub_key;
183+
_cfg.signature_len = cfg.signature_len;
184184
}
185185

186186

187187
void esp32FOTA::printConfig( FOTAConfig_t *cfg )
188188
{
189189
if( cfg == nullptr ) cfg = &_cfg;
190-
log_d("Name: %s\nManifest URL:%s\nSemantic Version: %d.%d.%d\nCheck Sig: %s\nUnsafe: %s\nUse Device ID: %s\nRootCA: %s\nPubKey: %s\n",
190+
log_d("Name: %s\nManifest URL:%s\nSemantic Version: %d.%d.%d\nCheck Sig: %s\nUnsafe: %s\nUse Device ID: %s\nRootCA: %s\nPubKey: %s\nSignatureLen: %d\n",
191191
cfg->name ? cfg->name : "None",
192192
cfg->manifest_url ? cfg->manifest_url : "None",
193193
cfg->sem.ver()->major,
@@ -197,11 +197,17 @@ void esp32FOTA::printConfig( FOTAConfig_t *cfg )
197197
cfg->unsafe ?"true":"false",
198198
cfg->use_device_id ?"true":"false",
199199
cfg->root_ca ?"true":"false",
200-
cfg->pub_key ?"true":"false"
200+
cfg->pub_key ?"true":"false",
201+
cfg->signature_len
201202
);
202203
}
203204

204205

206+
void esp32FOTA::setSignatureLen( size_t len )
207+
{
208+
_cfg.signature_len = len;
209+
}
210+
205211

206212
void esp32FOTA::setCertFileSystem( fs::FS *cert_filesystem )
207213
{
@@ -327,7 +333,7 @@ bool esp32FOTA::validate_sig( const esp_partition_t* partition, unsigned char *s
327333
}
328334
mbedtls_md_finish( &rsa, hash );
329335

330-
ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, mdinfo->size, (unsigned char*)signature, FW_SIGNATURE_LENGTH );
336+
ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, mdinfo->size, (unsigned char*)signature, _cfg.signature_len );
331337

332338
free( hash );
333339
mbedtls_md_free( &rsa );
@@ -513,11 +519,11 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
513519
log_e("Compressed && signed image is not (yet) supported");
514520
return false;
515521
}
516-
if( updateSize == UPDATE_SIZE_UNKNOWN || updateSize <= FW_SIGNATURE_LENGTH ) {
522+
if( updateSize == UPDATE_SIZE_UNKNOWN || updateSize <= _cfg.signature_len ) {
517523
log_e("Malformed signature+fw combo");
518524
return false;
519525
}
520-
updateSize -= FW_SIGNATURE_LENGTH;
526+
updateSize -= _cfg.signature_len;
521527
}
522528

523529
// If using compression, the size is implicitely unknown
@@ -541,9 +547,9 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
541547
});
542548
}
543549

544-
unsigned char signature[FW_SIGNATURE_LENGTH];
550+
unsigned char* signature = new unsigned char[_cfg.signature_len];
545551
if( _cfg.check_sig ) {
546-
_stream->readBytes( signature, FW_SIGNATURE_LENGTH );
552+
_stream->readBytes( signature, _cfg.signature_len );
547553
}
548554

549555
log_i("Begin %s OTA. This may take 2 - 5 mins to complete. Things might be quiet for a while.. Patience!", partition==U_FLASH?"Firmware":"Filesystem");
@@ -560,11 +566,13 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
560566
} else {
561567
log_e("Written only : %d/%d Premature end of stream?", written, updateSize);
562568
F_abort();
569+
delete[] signature;
563570
return false;
564571
}
565572

566573
if (!F_UpdateEnd()) {
567574
log_e("An Update Error Occurred. Error #: %s", F_Update.getError());
575+
delete[] signature;
568576
return false;
569577
}
570578

@@ -582,6 +590,7 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
582590
if( !_target_partition ) {
583591
log_e("Can't access partition #%d to check signature!", partition);
584592
if( onUpdateCheckFail ) onUpdateCheckFail( partition, CHECK_SIG_ERROR_PARTITION_NOT_FOUND );
593+
delete[] signature;
585594
return false;
586595
}
587596

@@ -601,6 +610,7 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
601610
}
602611

603612
if( !validate_sig( _target_partition, signature, updateSize ) ) {
613+
delete[] signature;
604614
// erase partition
605615
esp_partition_erase_range( _target_partition, _target_partition->address, _target_partition->size );
606616

@@ -613,6 +623,7 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
613623
}
614624
return false;
615625
} else {
626+
delete[] signature;
616627
log_d("Signature check successful!");
617628
if( partition == U_FLASH ) {
618629
// Set updated partition as bootable now that it's been verified

src/esp32FOTA.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ extern "C" {
147147
#define F_writeStream() F_Update.writeStream(*_stream);
148148
#endif
149149

150-
150+
#define FW_SIGNATURE_LENGTH 512
151151

152152
struct SemverClass
153153
{
@@ -211,6 +211,7 @@ struct FOTAConfig_t
211211
bool use_device_id { false };
212212
CryptoAsset* root_ca { nullptr };
213213
CryptoAsset* pub_key { nullptr };
214+
size_t signature_len {FW_SIGNATURE_LENGTH};
214215
FOTAConfig_t() = default;
215216
};
216217

@@ -267,6 +268,9 @@ class esp32FOTA
267268
// use this to set "Authorization: Basic" or other specific headers to be sent with the queries
268269
void setExtraHTTPHeader( String name, String value ) { extraHTTPHeaders[name] = value; }
269270

271+
// set the signature len
272+
void setSignatureLen( size_t len );
273+
270274
// /!\ Only use this to change filesystem for **default** RootCA and PubKey paths.
271275
// Otherwise use setPubKey() and setRootCA()
272276
void setCertFileSystem( fs::FS *cert_filesystem = nullptr );

0 commit comments

Comments
 (0)