Skip to content

Add PSK support #205

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

Closed
wants to merge 1 commit into from
Closed

Conversation

dongcarl
Copy link

A rebased and cleaned up version of #107

@dongcarl
Copy link
Author

I don't think that the verify_chain failure is related, can anyone confirm?

@dongcarl
Copy link
Author

dongcarl commented Aug 2, 2022

Ping @raoulstrackx @Pagten

let cb = &mut *(closure as *mut F);
let ctx = UnsafeFrom::from(ctx).unwrap();

let psk_identity = std::str::from_utf8_unchecked(from_raw_parts(psk_identity, identity_len));
Copy link
Contributor

Choose a reason for hiding this comment

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

There doesn't seem to be a guarantee that psk_identity is always a valid utf8 string.

Copy link
Author

Choose a reason for hiding this comment

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

What's the best thing to do here? unwrap a checked from_utf8 so the error is more apparent?

Copy link
Contributor

Choose a reason for hiding this comment

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

No you could change the type of PskCallback to

callback!(PskCallback: Fn(&mut HandshakeContext, &[u8]) -> Result<()>);

so there isn't a need to convert it.

ctx.establish(conn, None).map(|_| ())
}

#[cfg(unix)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this restricted to unix-like systems? This should also run correctly on for example x86_64-fortanix-unknown-sgx

Copy link
Author

Choose a reason for hiding this comment

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

Removed

Copy link
Author

Choose a reason for hiding this comment

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

See update in: #205 (comment)

@@ -0,0 +1,61 @@
#![allow(dead_code)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't be needed

Copy link
Author

Choose a reason for hiding this comment

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

Removed

Copy link
Author

Choose a reason for hiding this comment

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

See update in: #205 (comment)

@raoulstrackx
Copy link
Contributor

I also fixed CI in #206, so could you rebase on top of that one?

@dongcarl
Copy link
Author

dongcarl commented Aug 4, 2022

@dongcarl dongcarl force-pushed the 2022-07-add-psk branch 2 times, most recently from 6a926c9 to 09650b3 Compare August 5, 2022 02:23
Co-authored-by: Natnatee Dokmai <[email protected]>
@dongcarl
Copy link
Author

dongcarl commented Aug 5, 2022

Push 8162ee4 -> b71ec09

After running the CI, we see that we actually do need some kind of attribute guard for this test because x86_64-fortanix-unknown-sgx does not have crate::support::net, see:

There are more than one way to do the attribute guard that can be seen in existing tests:

  • mbedtls/tests/ssl_conf_ca_cb.rs uses #![allow(dead_code)] and #[cfg(unix)] since client and server will be dead code on non-unix
  • mbedtls/tests/client_server.rs uses a module level #![cfg(not(target_env = "sgx"))]

I ended up using a module level #![cfg(not(target_env = "sgx"))], but am happy to do either way.

Ping @raoulstrackx

let cb = &mut *(closure as *mut F);
let ctx = UnsafeFrom::from(ctx).unwrap();

let psk_identity = std::str::from_utf8_unchecked(from_raw_parts(psk_identity, identity_len));
Copy link
Contributor

Choose a reason for hiding this comment

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

No you could change the type of PskCallback to

callback!(PskCallback: Fn(&mut HandshakeContext, &[u8]) -> Result<()>);

so there isn't a need to convert it.


pub const PRESHARED_KEY: &'static [u8] = &[
234, 206, 151, 23, 219, 21, 71, 144,
107, 42, 23, 67, 249, 173, 182, 224 ];
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you pick a preshared key that is not valid utf8 and also contains a zero byte? It may trigger some weird code paths.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, could we add a case with a leading zero byte? I'm certain it is correctly handled, but I have seen way too many leading zero bugs in the wild/

(Orthogonal but also see https://mbed-tls.readthedocs.io/en/latest/security-advisories/advisories/mbedtls-security-advisory-2020-09-3/).

Copy link
Author

@dongcarl dongcarl Aug 11, 2022

Choose a reason for hiding this comment

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

Huh! If it's not valid UTF-8, we should expect an error from from_utf8?

What kind of behaviour do we expect for leading zero bytes?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is there a requirement that pre-shared keys are valid UTF-8? At least in RFC4279, only PSK identities are assumed to be UTF-8 (Sec. 5.1). Also Sec. 4 states that PSK are the result of Diffie Hellman computations, so no UTF-8 expected.

@raoulstrackx
Copy link
Contributor

After running the CI, we see that we actually do need some kind of attribute guard for this test because x86_64-fortanix-unknown-sgx does not have crate::support::net, see:

Oh I thought CI for x86_64-fortanix-unknown-sgx was better than it is. It could use some work, but that's out of scope for this PR. A module level is not ideal but I'd say it's acceptable until the CI for sgx gets straightened out.

@zugzwang could you also take a look when these two small comments of mine are addressed?

@zugzwang
Copy link
Collaborator

In the meantime we decided to merge #202 since it also added DTLS support.

@Taowyoo
Copy link
Collaborator

Taowyoo commented May 10, 2023

Thank you for contribution.
Because long time no updates on this PR and PSK support is added in #202.
So I decided to close this PR.

@Taowyoo Taowyoo closed this May 10, 2023
@Taowyoo Taowyoo mentioned this pull request May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants