-
Notifications
You must be signed in to change notification settings - Fork 96
Fix pk_parse_key() validation of RSA private keys #363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -398,23 +398,24 @@ int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx, | |
return( ret ); | ||
} | ||
|
||
int mbedtls_rsa_is_private( const mbedtls_rsa_context *ctx ) | ||
{ | ||
return( | ||
mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two comments - first this seems to accept values which are negative, values which could be deserialized from the DER structure. Also it seems to ignore the CRT params, which should probably also be checked, if CRT is enabled at least. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your feedback! Actually I think the checks in |
||
} | ||
|
||
int mbedtls_rsa_export( const mbedtls_rsa_context *ctx, | ||
mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q, | ||
mbedtls_mpi *D, mbedtls_mpi *E ) | ||
{ | ||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||
int is_priv; | ||
RSA_VALIDATE_RET( ctx != NULL ); | ||
|
||
/* Check if key is private or public */ | ||
is_priv = | ||
mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0; | ||
|
||
if( !is_priv ) | ||
if( !mbedtls_rsa_is_private( ctx ) ) | ||
{ | ||
/* If we're trying to export private parameters for a public key, | ||
* something must be wrong. */ | ||
|
@@ -447,18 +448,9 @@ int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx, | |
mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP ) | ||
{ | ||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | ||
int is_priv; | ||
RSA_VALIDATE_RET( ctx != NULL ); | ||
|
||
/* Check if key is private or public */ | ||
is_priv = | ||
mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 && | ||
mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0; | ||
|
||
if( !is_priv ) | ||
if( !mbedtls_rsa_is_private( ctx ) ) | ||
return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); | ||
|
||
#if !defined(MBEDTLS_RSA_NO_CRT) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function returns 0 on an incomplete key. I find this behavior surprising. I'd prefer
mbedtls_rsa_is_private(rsa)
to return the same thing before and aftermbedtls_rsa_complete(rsa)
. If it doesn't, this should be documented clearly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I agree this should be documented. I'm likely to rework the PR in a way that no longer relies on this function, but I'll make sure to properly document that aspect of the new function.