Skip to content

Commit c422679

Browse files
committed
Check public part when parsing private RSA key
1 parent a04a2c3 commit c422679

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

library/pkparse.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,20 @@ static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
820820
goto cleanup;
821821
#endif
822822

823-
/* Complete the RSA private key */
824-
if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
823+
/* rsa_complete() doesn't complete anything with the default
824+
* implementation but is still called:
825+
* - for the benefit of alternative implementation that may want to
826+
* pre-compute stuff beyond what's provided (eg Montgomery factors)
827+
* - as is also sanity-checks the key
828+
*
829+
* Furthermore, we also check the public part for consistency with
830+
* mbedtls_pk_parse_pubkey(), as it includes size minima for example.
831+
*/
832+
if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
833+
( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
834+
{
825835
goto cleanup;
836+
}
826837

827838
if( p != end )
828839
{

0 commit comments

Comments
 (0)