Skip to content
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

Fix psa_generate_key(): return PSA_ERROR_INVALID_ARGUMENT for public key #5037

Merged
merged 5 commits into from
Oct 18, 2021

Conversation

mprse
Copy link
Contributor

@mprse mprse commented Oct 7, 2021

Description

Fixes #4551

Status

READY

Requires Backporting

Yes
development_2.x
PR #5038

Migrations

NO

Additional comments

Verified that with this fix the following code returns PSA_ERROR_INVALID_ARGUMENT (-135):

int main( int argc, char** argv )
{
    (void)argc;
    (void)argv;
    psa_status_t status;
    mbedtls_svc_key_id_t key_handle;
    psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_PERSISTENT;

    status = psa_crypto_init();
    if (status != PSA_SUCCESS) {
            printf("Init failed (status = %d)\n", status);
            return 1;
    }

    psa_key_attributes_t key_pair_attributes = PSA_KEY_ATTRIBUTES_INIT;
    psa_set_key_id(&key_pair_attributes, mbedtls_svc_key_id_make( 1, 1 ));
    psa_set_key_lifetime(&key_pair_attributes, lifetime);
    psa_set_key_usage_flags(&key_pair_attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH |
                                                  PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT |
                                                  PSA_KEY_USAGE_EXPORT );
    psa_set_key_algorithm(&key_pair_attributes, PSA_ALG_CTR);
    psa_set_key_type(&key_pair_attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(0));
    psa_set_key_bits(&key_pair_attributes, 128U);

    status = psa_generate_key(&key_pair_attributes, &key_handle);
    if (status != PSA_SUCCESS) {
            printf("Key generation failed (status = %d)\n", status);
            return 1;
    }
}

Result:
Key generation failed (status = -135)

mprse added 2 commits October 7, 2021 11:08
Signed-off-by: Przemyslaw Stekiel <przemyslaw.stekiel@mobica.com>
Signed-off-by: Przemyslaw Stekiel <przemyslaw.stekiel@mobica.com>
@mprse mprse added needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Oct 7, 2021
@mprse mprse changed the title Issue 4551 Fix psa_generate_key(): return PSA_ERROR_INVALID_ARGUMENT for public key Oct 7, 2021
@AndrzejKurek AndrzejKurek self-requested a review October 7, 2021 11:43
@AndrzejKurek
Copy link
Contributor

There are some tests that will expect this new code as a result. Please see the travis logs, search for "failed".

mprse added 2 commits October 8, 2021 12:26
Signed-off-by: Przemyslaw Stekiel <przemyslaw.stekiel@mobica.com>
Signed-off-by: Przemyslaw Stekiel <przemyslaw.stekiel@mobica.com>
Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change is correct, but I think the Python code (which admittedly was already not very well-structured before this PR) could be clearer.

ChangeLog.d/fix-psa_gen_key-status.txt Outdated Show resolved Hide resolved
tests/suites/test_suite_psa_crypto_not_supported.function Outdated Show resolved Hide resolved
@@ -148,10 +148,15 @@ def test_case_for_key_type_not_supported(
adverb = 'not' if dependencies else 'never'
if param_descr:
adverb = param_descr + ' ' + adverb
tc.set_description('PSA {} {} {}-bit {} supported'
.format(verb, short_key_type, bits, adverb))
if (verb == "generate") and ("PUBLIC" in short_key_type):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In NotSupported.test_cases_for_key_type_not_supported, the test for is-a-public-key is a bit different (kt.name.endswith('_PUBLIC_KEY')). I'm working on a follow-up that adds a method kt.is_public(). It would be better to have a single place that implements the test “is this key type for a public key?”, otherwise they risk diverging. Can you arrange to pass the information “is public key” down to this function?

Or maybe it would be better to have a completely separate function for the invalid-argument-for-public key case? I find this function rather messy. That's unavoidable to some extent because we want to produce output that has a lot of tweakable parts. Here, I suspect that keeping test_case_for_key_type_not_supported as it originally was, and making a separate function for the new case invalid-argument-for-public key, would make the code easier to maintain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to confirm if I understand correctly your proposition:

  • restore original version of test_case_for_key_type_not_supported()
  • add test_case_for_key_type_invalid_argument()

In this solution we would also require to modify NotSupported class (change it to NotSupported_InvalidArgumentor addInvalidArgument class". What option you suggest?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I meant.

If we want to make this really clean, taking a high-level view, the job of NotSupported is to enumerate combinations of (key type, algorithm, operation) and generate test cases for when one of them is not supported. If a combination is inherently impossible, rather than not supported by the current configuration of the library, then it's not NotSupported's job to handle this. #4444 will introduce the generation of negative test cases for impossible combinations, but only for operations on existing keys, not for key generation (and not for key import either, but there are no impossible combinations for import). This leaves a gap of impossible combinations for key generation, which are just the cases where the key type is a public key. So, to be clean, there should be a separate CreationFail class (distinct from NotSupported and OpFail).

However, this is an evolving test script, we shouldn't expend too much energy on it. Since NotSupported already does much of the job of CreationFail, we can keep it doing that, with a comment explaining that it's doing a little extra job on the side.

Despite my saing we shouldn't expend too much energy on this, I do still prefer to restore the original test_case_for_key_type_not_supported and make a separate function for the invalid-generate case. I see renaming NotSupported as optional: not-supported is still its main job.

Copy link
Contributor

@gilles-peskine-arm gilles-peskine-arm Oct 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've filed #5060 as a follow-up. It's not a top priority, but it is something we'll need eventually and it's a well-defined task that both adds a feature to generate_psa_tests.py and, I think, will make it a bit better structured. So @mprse if you don't have a more urgent task, I suggest you pick it up next.

@gilles-peskine-arm gilles-peskine-arm added bug component-psa PSA keystore/dispatch layer (storage, drivers, …) needs-work and removed needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review labels Oct 8, 2021
Signed-off-by: Przemyslaw Stekiel <przemyslaw.stekiel@mobica.com>
@mprse mprse added needs-review Every commit must be reviewed by at least two team members, needs-reviewer This PR needs someone to pick it up for review and removed needs-work labels Oct 11, 2021
@gilles-peskine-arm gilles-peskine-arm removed the needs-reviewer This PR needs someone to pick it up for review label Oct 11, 2021
@mprse
Copy link
Contributor Author

mprse commented Oct 18, 2021

@gilles-peskine-arm Can we merge this one? It would be easier to work on #5060.

@gilles-peskine-arm gilles-peskine-arm added approved Design and code approved - may be waiting for CI or backports and removed needs-review Every commit must be reviewed by at least two team members, labels Oct 18, 2021
@gilles-peskine-arm gilles-peskine-arm merged commit 7637ab0 into Mbed-TLS:development Oct 18, 2021
gilles-peskine-arm added a commit that referenced this pull request Oct 18, 2021
Backport 2.x: Fix psa_generate_key(): return PSA_ERROR_INVALID_ARGUMENT for public key #5037
bensze01 added a commit to bensze01/psa-arch-tests that referenced this pull request Nov 8, 2021
Test psa_generate_key with RSA 2048 Public key

Changed the error code from PSA_ERROR_NOT_SUPPORTED to
PSA_ERROR_INVALID_ARGUMENT as in the documentation of
psa_generate_key() returned values:
"PSA_ERROR_INVALID_ARGUMENT The key type is an asymmetric public key type."

See:
Mbed-TLS/mbedtls#4551
Mbed-TLS/mbedtls#5037
Mbed-TLS/mbedtls#5038

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
bensze01 added a commit to bensze01/psa-arch-tests that referenced this pull request Nov 10, 2021
Test psa_generate_key with RSA 2048 Public key

Changed the error code from PSA_ERROR_NOT_SUPPORTED to
PSA_ERROR_INVALID_ARGUMENT as in the documentation of
psa_generate_key() returned values:
"PSA_ERROR_INVALID_ARGUMENT The key type is an asymmetric public
key type."

See:
Mbed-TLS/mbedtls#4551
Mbed-TLS/mbedtls#5037
Mbed-TLS/mbedtls#5038

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
bensze01 added a commit to bensze01/psa-arch-tests that referenced this pull request Jan 12, 2022
Test psa_generate_key with RSA 2048 Public key

Changed the error code from PSA_ERROR_NOT_SUPPORTED to
PSA_ERROR_INVALID_ARGUMENT as in the documentation of
psa_generate_key() returned values:
"PSA_ERROR_INVALID_ARGUMENT The key type is an asymmetric public
key type."

See:
Mbed-TLS/mbedtls#4551
Mbed-TLS/mbedtls#5037
Mbed-TLS/mbedtls#5038

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Design and code approved - may be waiting for CI or backports bug component-psa PSA keystore/dispatch layer (storage, drivers, …)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

psa_generate_key should return INVALID_ARGUMENT for a public key type
3 participants