41
41
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
42
42
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
43
43
44
- #define FW_SIGNATURE_LENGTH 512
45
44
46
45
47
46
static int64_t getHTTPStream ( esp32FOTA* fota, int partition );
@@ -181,13 +180,14 @@ void esp32FOTA::setConfig( FOTAConfig_t cfg )
181
180
_cfg.use_device_id = cfg.use_device_id ;
182
181
_cfg.root_ca = cfg.root_ca ;
183
182
_cfg.pub_key = cfg.pub_key ;
183
+ _cfg.signature_len = cfg.signature_len ;
184
184
}
185
185
186
186
187
187
void esp32FOTA::printConfig ( FOTAConfig_t *cfg )
188
188
{
189
189
if ( cfg == nullptr ) cfg = &_cfg;
190
- log_d (" Name: %s\n Manifest URL:%s\n Semantic Version: %d.%d.%d\n Check Sig: %s\n Unsafe: %s\n Use Device ID: %s\n RootCA: %s\n PubKey: %s\n " ,
190
+ log_d (" Name: %s\n Manifest URL:%s\n Semantic Version: %d.%d.%d\n Check Sig: %s\n Unsafe: %s\n Use Device ID: %s\n RootCA: %s\n PubKey: %s\n SignatureLen: %d \ n" ,
191
191
cfg->name ? cfg->name : " None" ,
192
192
cfg->manifest_url ? cfg->manifest_url : " None" ,
193
193
cfg->sem .ver ()->major ,
@@ -197,11 +197,17 @@ void esp32FOTA::printConfig( FOTAConfig_t *cfg )
197
197
cfg->unsafe ?" true" :" false" ,
198
198
cfg->use_device_id ?" true" :" false" ,
199
199
cfg->root_ca ?" true" :" false" ,
200
- cfg->pub_key ?" true" :" false"
200
+ cfg->pub_key ?" true" :" false" ,
201
+ cfg->signature_len
201
202
);
202
203
}
203
204
204
205
206
+ void esp32FOTA::setSignatureLen ( size_t len )
207
+ {
208
+ _cfg.signature_len = len;
209
+ }
210
+
205
211
206
212
void esp32FOTA::setCertFileSystem ( fs::FS *cert_filesystem )
207
213
{
@@ -327,7 +333,7 @@ bool esp32FOTA::validate_sig( const esp_partition_t* partition, unsigned char *s
327
333
}
328
334
mbedtls_md_finish ( &rsa, hash );
329
335
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 );
331
337
332
338
free ( hash );
333
339
mbedtls_md_free ( &rsa );
@@ -513,11 +519,11 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
513
519
log_e (" Compressed && signed image is not (yet) supported" );
514
520
return false ;
515
521
}
516
- if ( updateSize == UPDATE_SIZE_UNKNOWN || updateSize <= FW_SIGNATURE_LENGTH ) {
522
+ if ( updateSize == UPDATE_SIZE_UNKNOWN || updateSize <= _cfg. signature_len ) {
517
523
log_e (" Malformed signature+fw combo" );
518
524
return false ;
519
525
}
520
- updateSize -= FW_SIGNATURE_LENGTH ;
526
+ updateSize -= _cfg. signature_len ;
521
527
}
522
528
523
529
// If using compression, the size is implicitely unknown
@@ -541,9 +547,9 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
541
547
});
542
548
}
543
549
544
- unsigned char signature[FW_SIGNATURE_LENGTH ];
550
+ unsigned char * signature = new unsigned char [_cfg. signature_len ];
545
551
if ( _cfg.check_sig ) {
546
- _stream->readBytes ( signature, FW_SIGNATURE_LENGTH );
552
+ _stream->readBytes ( signature, _cfg. signature_len );
547
553
}
548
554
549
555
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 )
560
566
} else {
561
567
log_e (" Written only : %d/%d Premature end of stream?" , written, updateSize);
562
568
F_abort ();
569
+ delete[] signature;
563
570
return false ;
564
571
}
565
572
566
573
if (!F_UpdateEnd ()) {
567
574
log_e (" An Update Error Occurred. Error #: %s" , F_Update.getError ());
575
+ delete[] signature;
568
576
return false ;
569
577
}
570
578
@@ -582,6 +590,7 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
582
590
if ( !_target_partition ) {
583
591
log_e (" Can't access partition #%d to check signature!" , partition);
584
592
if ( onUpdateCheckFail ) onUpdateCheckFail ( partition, CHECK_SIG_ERROR_PARTITION_NOT_FOUND );
593
+ delete[] signature;
585
594
return false ;
586
595
}
587
596
@@ -601,6 +610,7 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
601
610
}
602
611
603
612
if ( !validate_sig ( _target_partition, signature, updateSize ) ) {
613
+ delete[] signature;
604
614
// erase partition
605
615
esp_partition_erase_range ( _target_partition, _target_partition->address , _target_partition->size );
606
616
@@ -613,6 +623,7 @@ bool esp32FOTA::execOTA( int partition, bool restart_after )
613
623
}
614
624
return false ;
615
625
} else {
626
+ delete[] signature;
616
627
log_d (" Signature check successful!" );
617
628
if ( partition == U_FLASH ) {
618
629
// Set updated partition as bootable now that it's been verified
0 commit comments