Fix pk_parse_key() validation of RSA private keys #363
No reviewers
Labels
No labels
api-spec
bug
CLA requested
CLA valid
compliance
DO NOT MERGE
duplicate
enhancement
feature branch
good first issue
HEADER FORMATTING ISSUE
help wanted
Information
invalid
Jira status: AWAITING REVIEW
Jira status: CLOSED
Jira status: DONE
Jira status: IN PROGRESS
Jira status: IN REVIEW
Jira status: OPEN
Jira status: REOPENED
Jira status: RESOLVED
mirrored
mirror internally
needs: backports
needs: ci
needs: design review
needs: preceding PR
needs: review
needs: work
notabug
open for community contribution
Product Backlog
question
ready for merge
security
stale
type: bug
type: enhancement
type: question
web pages
wontfix
writing
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-ARMmbed/mbed-crypto!363
Loading…
Reference in a new issue
No description provided.
Delete branch "fix-pkparse-rsapriv"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
We recently added a check of the return value of
mbedtls_rsa_exportaftermbedtls_pk_parse_keyinfuzz_privkey(https://github.com/ARMmbed/mbedtls/pull/2999) and oss-fuzz found an issue (restricted link: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=20467).This PR fixes the issue by making sure that
pk_parse_key()rejects keys that are obviously not valid private keys, as its documentation promises (and asrsa_export()does).Status: WIP
Todo
pk_parse_key()d == 0and silently fix the value tod = p * q, I don't think we shouldBackports
@ -401,0 +401,4 @@int mbedtls_rsa_is_private( const mbedtls_rsa_context *ctx ){return(mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&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.@ -401,0 +405,4 @@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 );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.
@ -401,0 +405,4 @@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 );Thanks for your feedback! Actually I think the checks in
rsa_check_context()(called frommbedtls_rsa_complete()called frommbedtls_pk_parse_key()) already prevent that, but I agree that it should be more obvious. I was intending to rework the PR anyway, as I'm no longer convincedmbedtls_rsa_is_private()is the right function here, so I'll keep that in mind when reworking.@ -401,0 +401,4 @@int mbedtls_rsa_is_private( const mbedtls_rsa_context *ctx ){return(mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 &&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.
@gilles-peskine-arm @jack-fortanix I decided to switch to a new approach and I though it would be cleaner to open a new PR for that, so this PR is now superseded by: https://github.com/ARMmbed/mbed-crypto/pull/367
Pull request closed