Fix pk_parse_key()'s use of rsa_complete() #367
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!367
Loading…
Reference in a new issue
No description provided.
Delete branch "fix-rsa-complete"
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?
This PR fixes bugs and inconsistencies in
rsa_complete()andpk_parse_key():pk_parse_key()used to accept keys that were later rejected byrsa_export(), as found by oss-fuzzpk_parse_pubkey()which was inconsistent.The PR also enhances testing of
pk_parse_key_pkcs1_der()by exercising all of its possible failure modes (new and old).This PR is meant to replace #363 by using a simpler and more consistent fix, and avoiding API extensions in order to ease backporting to the LTS branches.
Fixes #189
Closes #363
Backports
Changelog
The CI only failed the tests that rely on Mbed OS, which are currently expected to fail until Mbed OS catches up with the latest changes in the PSA Crypto API.
I disagree with adding more cases where we depend on whether the data pointer in a bignum is null. In the long term, I want to be able to change the bignum representation to a more opaque one which may not have any equivalent to “null”. I'm ok with the approach taken in #363 (but haven't reviewed it fully).
I disagree with exposing this implementation detail of
mbedtls_mpi. IfN->n == 0then it shouldn't make a difference whetherN->pis null. For example, I recently changedmbedtls_mpi_copynot to make a difference, partly because it misled Coverity into thinking that more null checks are necessary and partly to make the code more robust against possible changes in the details of the behavior of the bignum module.mbedtls_rsa_completehas treated zero (which is never a valid value for any of the fields) as meaning “to be completed” ever since it was introduced. Please keep it that way.This doesn't fix the bug of treating a key with e.g. d ≠ 0 but p=q=0 as public.
@ -1072,33 +1072,84 @@ Parse EC Key #15 (SEC1 DER, secp256k1, SpecifiedECDomain)depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256K1_ENABLED:MBEDTLS_PK_PARSE_EC_EXTENDEDIf
N->n == 0, thenmbedtls_mpi_cmp_int()still finds it equal to 0. The only reliable way to differentiate 0 from unset is checkingN->pat the moment.I agree that this is not ideal and that we should not rely on implementation details, but I don't think that this PR is the best place to fix this.
Because there is no such bug. There are only two legal ways of importing a key:
rsa_import()followed by a call torsa_complete().Once you imported a key in one of these ways, the condition you mention can never happen.
My disagreement is about treating any bignum value as “unset”.
IMO that behaviour
rsa_complete()was a design mistake, which had unintended consequences, and since it was never documented I don't think we should feel forced to keep it.But I take note of your opposition and will look for options we can both agree on.
s/bug/lack of robustness/ then
I'd still consider that quite orthogonal to the primary issue the PR is addressing, and I'd rather address it separately.
I'm fine with leaving it for later. But I found it surprising that you wouldn't fix this since you were changing this code anyway.
@gilles-peskine-arm @yanesca I just force-pushed a new version using the strategy we discussed yesterday.
While at it, I noticed a bug in a previous fix (reading extra CRT values) and even though it's not directly related to the core goal of this PR, I fixed it as well since it's a one-line fix.
I'll start working on backports as soon as this has been reviewed and approved by at least one of you.
Since we're only verifying “existance” (sic) here, why test for zero?
For consistency with the CRT case. I'd like to avoid changing the set of keys accepted by this function too much depending on whether
MBEDTLS_RSA_NO_CRTis enabled or not, at least when we can conveniently avoid differences.LGTM
The CI still fails due to a known issue that is unrelated to the changes in this PR.