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

Use PSA EC-JPAKE in TLS (1.2) #5886

Closed

Conversation

superna9999
Copy link
Contributor

@superna9999 superna9999 commented May 31, 2022

Description

TLS 1.2 has optional support for a key exchange based on EC J-PAKE, defined as part of the Thread standard, see MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED. Currently the implementation always uses the mbedtls_ecjpake API regardless of whether MBEDTLS_USE_PSA_CRYPTO is enabled.

This task it to make it use the PSA API when MBEDTLS_USE_PSA_CRYPTO is enabled.

Resolves #5847

Status

IN DEVELOPMENT

Requires Backporting

NO

Migrations

NO

Additional comments

Depends on #6115 to be merged

Todos

  • Tests

Steps to test or reproduce

ssl-opt.sh must run clean

@superna9999 superna9999 added needs-work needs-ci Needs to pass CI tests needs-preceding-pr Requires another PR to be merged first labels May 31, 2022
@superna9999 superna9999 force-pushed the 5847-use-psa-pake-in-tls branch 4 times, most recently from d5424e0 to 546840c Compare June 1, 2022 15:14
@daverodgman daverodgman added the priority-medium Medium priority - this can be reviewed as time permits label Jun 6, 2022
@mpg mpg removed the needs-ci Needs to pass CI tests label Jun 8, 2022
@superna9999 superna9999 force-pushed the 5847-use-psa-pake-in-tls branch from 546840c to 5cc2469 Compare July 21, 2022 08:20
@superna9999 superna9999 force-pushed the 5847-use-psa-pake-in-tls branch from 5cc2469 to 3f9437d Compare August 31, 2022 13:31
@superna9999 superna9999 force-pushed the 5847-use-psa-pake-in-tls branch from 3f9437d to aee9b3e Compare September 9, 2022 12:38
@mpg
Copy link
Contributor

mpg commented Sep 13, 2022

Next time you update this, can you also remove the line about EC J-PAKE in docs/use-psa-crypto.md?

@superna9999 superna9999 force-pushed the 5847-use-psa-pake-in-tls branch 3 times, most recently from 076d18a to 68e86da Compare September 15, 2022 07:30
@superna9999 superna9999 marked this pull request as ready for review September 15, 2022 07:37
@superna9999 superna9999 added needs-review Every commit must be reviewed by at least two team members, needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review and removed needs-work labels Sep 15, 2022
@superna9999 superna9999 force-pushed the 5847-use-psa-pake-in-tls branch from 68e86da to 266e2e7 Compare September 15, 2022 09:44
@mpg
Copy link
Contributor

mpg commented Sep 16, 2022

Note: the failure in ABI-API checking is not a problem, see #6115 (comment)

@mpg mpg mentioned this pull request Sep 19, 2022
@mpg
Copy link
Contributor

mpg commented Sep 28, 2022

Now that #6115 has been merged, this should be rebased. @superna9999 Can you take care of this? Otherwise please let us know and we'll handle it.

@mpg mpg added needs-work and removed needs-preceding-pr Requires another PR to be merged first labels Sep 28, 2022
@mpg mpg removed the needs-review Every commit must be reviewed by at least two team members, label Sep 28, 2022
Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
@superna9999 superna9999 force-pushed the 5847-use-psa-pake-in-tls branch from 266e2e7 to f254b85 Compare September 28, 2022 09:49
@superna9999
Copy link
Contributor Author

@mpg done!

@mpg mpg self-requested a review September 28, 2022 10:02
@mpg
Copy link
Contributor

mpg commented Sep 28, 2022

Great, thanks!

Copy link
Contributor

@mpg mpg left a comment

Choose a reason for hiding this comment

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

I only found very minor things by reviewing, but did some more testing and the results were not good, see my next message.

psa_role = PSA_PAKE_ROLE_CLIENT;


if( pw_len > 0 )
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: unless I'm missing something, we could merge this we the next if( pw_len > 0 ) block. This would avoid the need to destroy the key in the failure path of the next two blocks, and keep related things together. (While at it, I think we could make attributes local to that block as well.)

#if defined(MBEDTLS_USE_PSA_CRYPTO)
psa_pake_abort( &handshake->psa_pake_ctx );
psa_destroy_key( handshake->psa_pake_password );
handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: not setting psa_pake_ctx_is_ok is correct because it's going to be done by the call to zeroize() near the end of this function.

@@ -5731,6 +5807,55 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
else
#endif
{
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Copy link
Contributor

Choose a reason for hiding this comment

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

Partially pre-existing: with the new EC J-PAKE block added, the structure become hard to follow:

if( direct_psk_to_ms )
{
    /* get MS directly */
}
else
{
    if( ecjpake )
    {
         /* compute PMS, dozens of line */
    }
    // here I have lost track of where in are in the control flow
    /* derive MS from PMS */
}

I think we could directly return success at the end of the "get MS directly" block, saving one nesting level (no else).

return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
}
}
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor and partially pre-existing (the few lines below): I find it weird that we are first computing the MS from the PMS, and only then printing out the PMS. It's a bit worse IMO with the addition of this new block: now it's (1) compute the PMS, (2) compute the MS, (3) print the PMS, while 1-3-2 would seem more natural to me.

@@ -5769,6 +5894,7 @@ int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
return( ret );
}


Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: seems spurious to me, we usually don't leave two blank lines in a row.

{
status = psa_pake_output( &ssl->handshake->psa_pake_ctx,
step, p + 2 + output_offset,
end - p - output_offset - 2,
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: I think p - 2 - output_offset would be slightly better for consistency (and as it happens, alignment) with the line above.

output_offset += output_len;
}

content_len = output_offset;
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: why do we stop there instead of also computing the PMS as we do in the non-PSA case? If there's a reason, it's not obvious to me.

Generally speaking, it's a pre-existing issue with the code that the PMS is computed in various places for various key exchanges and this makes things really hard to follow at times. Let's not introduce more variations unless we have a good reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm it wasn't necessary before the addition of the custom SHA256 PMS for PAKE, so indeed I may have forgotten a step while migrating and it may explain why it doesn't work with the non-PSA implementation.

@mpg
Copy link
Contributor

mpg commented Sep 30, 2022

It's hard to validate correctness (are we using the correct format? computing the expected value for the shared secret? etc), not just for this PR, but for the entire EC J-PAKE line of work, because the key exchange is randomized and we can't use known-answer tests.

So, I guess the only thing we can do is test interop with other implementations. I'm not aware of other implementations of the ECJPAKE key exchange in TLS out there, but now we have two in Mbed TLS: the version that uses PSA and the one that doesn't. Let's see if they can talk to each other:

git checkout -- include/mbedtls/mbedtls_config.h
scripts/config.py set MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
make clean

make -C programs ssl/ssl_server2 ssl/ssl_client2
cp programs/ssl/ssl_server2 s2_no_use_psa
cp programs/ssl/ssl_client2 c2_no_use_psa


scripts/config.py set MBEDTLS_USE_PSA_CRYPTO
make clean

make -C programs ssl/ssl_server2 ssl/ssl_client2
make -C programs test/udp_proxy test/query_compile_time_config

P_SRV=../s2_no_use_psa tests/ssl-opt.sh -f ECJPAKE
P_CLI=../c2_no_use_psa tests/ssl-opt.sh -f ECJPAKE

And unfortunately the results are not looking good on my machine:

ECJPAKE: client not configured ......................................... PASS
ECJPAKE: server not configured ......................................... PASS
ECJPAKE: working, TLS .................................................. FAIL
  ! bad client exit code (expected 0, got 1)
  ! outputs saved to o-XXX-3.log
ECJPAKE: password mismatch, TLS ........................................ FAIL
  ! pattern 'SSL - Verification of the message MAC failed' MUST be present in the Server output
  ! outputs saved to o-XXX-4.log
ECJPAKE: working, DTLS ................................................. FAIL
  ! bad client exit code (expected 0, got 1)
  ! outputs saved to o-XXX-5.log
ECJPAKE: working, DTLS, no cookie ...................................... FAIL
  ! bad client exit code (expected 0, got 1)
  ! outputs saved to o-XXX-6.log
ECJPAKE: password mismatch, DTLS ....................................... FAIL
  ! pattern 're-using cached ecjpake parameters' MUST be present in the Client output
  ! outputs saved to o-XXX-7.log
ECJPAKE: working, DTLS, nolog .......................................... FAIL
  ! bad client exit code (expected 0, got 1)
  ! outputs saved to o-XXX-8.log
------------------------------------------------------------------------
FAILED (2 / 8 tests (0 skipped))

I don't have time to debug now, I can have a look next week until you beat be to it, @superna9999

Also, based on this, I'm going to advocate that we add an all.sh component doing something similar to the above.

@superna9999
Copy link
Contributor Author

@mpg thanks, I think I may missed something when adding support for #6115, in the first implementation it worked while developement when I implemented only the client and left the server using non-PSA. But when migrated to the custom KDF I may have missed step.

I'll add the all.sh component to this PR as you suggest.

@mpg
Copy link
Contributor

mpg commented Oct 28, 2022

Note: this will need rework once the I/O format of psa_pake_input/output() has been fixed to match the spec, as done in #6390

@mpg mpg added the needs-preceding-pr Requires another PR to be merged first label Oct 28, 2022
@mpg mpg mentioned this pull request Oct 28, 2022
@superna9999
Copy link
Contributor Author

Finally I'm lacking time to review and update those PRs, I'm OK if those are taken over by @mprse @AndrzejKurek or @valeriosetti

@valeriosetti
Copy link
Contributor

Finally I'm lacking time to review and update those PRs, I'm OK if those are taken over by @mprse @AndrzejKurek or @valeriosetti

I'm currently trying to fix test issues reported by @mpg by rebasing this PR on top of PR #6390. Once I'll have something ready I'll open a new PR to continue this work

@superna9999
Copy link
Contributor Author

Taken over by @valeriosetti in #6533

@superna9999 superna9999 closed this Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci Needs to pass CI tests needs-preceding-pr Requires another PR to be merged first needs-reviewer This PR needs someone to pick it up for review needs-work priority-medium Medium priority - this can be reviewed as time permits
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EC-JPAKE 2: use in TLS (1.2)
4 participants