@@ -334,19 +334,18 @@ bool pointperfectProvisionDevice()
334334 }
335335 else
336336 {
337- const int tempHolderSize = 2000 ;
338- tempHolderPtr = (char *)malloc (tempHolderSize);
337+ tempHolderPtr = (char *)malloc (MQTT_CERT_SIZE);
339338 if (!tempHolderPtr)
340339 {
341340 systemPrintln (" ERROR - Failed to allocate tempHolderPtr buffer!\r\n " );
342341 break ;
343342 }
344- strncpy (tempHolderPtr, (const char *)((*jsonZtp)[" certificate" ]), tempHolderSize - 1 );
343+ strncpy (tempHolderPtr, (const char *)((*jsonZtp)[" certificate" ]), MQTT_CERT_SIZE - 1 );
345344 // log_d("len of PrivateCert: %d", strlen(tempHolderPtr));
346345 // log_d("privateCert: %s", tempHolderPtr);
347346 recordFile (" certificate" , tempHolderPtr, strlen (tempHolderPtr));
348347
349- strncpy (tempHolderPtr, (const char *)((*jsonZtp)[" privateKey" ]), tempHolderSize - 1 );
348+ strncpy (tempHolderPtr, (const char *)((*jsonZtp)[" privateKey" ]), MQTT_CERT_SIZE - 1 );
350349 // log_d("len of privateKey: %d", strlen(tempHolderPtr));
351350 // log_d("privateKey: %s", tempHolderPtr);
352351 recordFile (" privateKey" , tempHolderPtr, strlen (tempHolderPtr));
@@ -432,7 +431,7 @@ bool checkCertificates()
432431 memset (keyContents, 0 , MQTT_CERT_SIZE);
433432 loadFile (" privateKey" , keyContents);
434433
435- if (checkCertificateValidity (keyContents, strlen (keyContents)) == false )
434+ if (checkPrivateKeyValidity (keyContents, strlen (keyContents)) == false )
436435 {
437436 if (settings.debugPpCertificate )
438437 systemPrintln (" PrivateKey is corrupt." );
@@ -468,13 +467,38 @@ bool checkCertificateValidity(char *certificateContent, int certificateContentSi
468467 if (result_code < 0 )
469468 {
470469 if (settings.debugPpCertificate )
471- systemPrintln (" Cert formatting invalid " );
470+ systemPrintln (" ERROR - Invalid certificate format! " );
472471 return (false );
473472 }
474473
475474 return (true );
476475}
477476
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+
478502// When called, removes the files used for SSL to PointPerfect obtained during provisioning
479503// Also deletes keys so the user can immediately re-provision
480504void erasePointperfectCredentials ()
0 commit comments