@@ -334,19 +334,18 @@ bool pointperfectProvisionDevice()
334
334
}
335
335
else
336
336
{
337
- const int tempHolderSize = 2000 ;
338
- tempHolderPtr = (char *)malloc (tempHolderSize);
337
+ tempHolderPtr = (char *)malloc (MQTT_CERT_SIZE);
339
338
if (!tempHolderPtr)
340
339
{
341
340
systemPrintln (" ERROR - Failed to allocate tempHolderPtr buffer!\r\n " );
342
341
break ;
343
342
}
344
- strncpy (tempHolderPtr, (const char *)((*jsonZtp)[" certificate" ]), tempHolderSize - 1 );
343
+ strncpy (tempHolderPtr, (const char *)((*jsonZtp)[" certificate" ]), MQTT_CERT_SIZE - 1 );
345
344
// log_d("len of PrivateCert: %d", strlen(tempHolderPtr));
346
345
// log_d("privateCert: %s", tempHolderPtr);
347
346
recordFile (" certificate" , tempHolderPtr, strlen (tempHolderPtr));
348
347
349
- strncpy (tempHolderPtr, (const char *)((*jsonZtp)[" privateKey" ]), tempHolderSize - 1 );
348
+ strncpy (tempHolderPtr, (const char *)((*jsonZtp)[" privateKey" ]), MQTT_CERT_SIZE - 1 );
350
349
// log_d("len of privateKey: %d", strlen(tempHolderPtr));
351
350
// log_d("privateKey: %s", tempHolderPtr);
352
351
recordFile (" privateKey" , tempHolderPtr, strlen (tempHolderPtr));
@@ -432,7 +431,7 @@ bool checkCertificates()
432
431
memset (keyContents, 0 , MQTT_CERT_SIZE);
433
432
loadFile (" privateKey" , keyContents);
434
433
435
- if (checkCertificateValidity (keyContents, strlen (keyContents)) == false )
434
+ if (checkPrivateKeyValidity (keyContents, strlen (keyContents)) == false )
436
435
{
437
436
if (settings.debugPpCertificate )
438
437
systemPrintln (" PrivateKey is corrupt." );
@@ -468,13 +467,38 @@ bool checkCertificateValidity(char *certificateContent, int certificateContentSi
468
467
if (result_code < 0 )
469
468
{
470
469
if (settings.debugPpCertificate )
471
- systemPrintln (" Cert formatting invalid " );
470
+ systemPrintln (" ERROR - Invalid certificate format! " );
472
471
return (false );
473
472
}
474
473
475
474
return (true );
476
475
}
477
476
477
+ // Check if a given private key is in a valid format
478
+ // This was created to detect corrupt or invalid private keys caused by bugs in v3.0 to and including v3.3.
479
+ // See https://github.com/Mbed-TLS/mbedtls/blob/development/library/pkparse.c
480
+ bool checkPrivateKeyValidity (char *privateKey, int privateKeySize)
481
+ {
482
+ // Check for valid format of private key
483
+ // From ssl_client.cpp
484
+ // https://stackoverflow.com/questions/70670070/mbedtls-cannot-parse-valid-x509-certificate
485
+ mbedtls_pk_context pk;
486
+ mbedtls_pk_init (&pk);
487
+
488
+ int result_code =
489
+ mbedtls_pk_parse_key (&pk,
490
+ (unsigned char *)privateKey, privateKeySize + 1 ,
491
+ nullptr , 0 );
492
+ mbedtls_pk_free (&pk);
493
+ if (result_code < 0 )
494
+ {
495
+ if (settings.debugPpCertificate )
496
+ systemPrintln (" ERROR - Invalid private key format!" );
497
+ return (false );
498
+ }
499
+ return (true );
500
+ }
501
+
478
502
// When called, removes the files used for SSL to PointPerfect obtained during provisioning
479
503
// Also deletes keys so the user can immediately re-provision
480
504
void erasePointperfectCredentials ()
0 commit comments