-
Notifications
You must be signed in to change notification settings - Fork 83
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
Add PSK support #205
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* Licensed under the GNU General Public License, version 2 <LICENSE-GPL or | ||
* https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version | ||
* 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your | ||
* option. This file may not be copied, modified, or distributed except | ||
* according to those terms. */ | ||
|
||
#![cfg(not(target_env = "sgx"))] | ||
|
||
// needed to have common code for `mod support` in unit and integrations tests | ||
extern crate mbedtls; | ||
|
||
use std::net::TcpStream; | ||
use std::sync::Arc; | ||
|
||
use mbedtls::rng::CtrDrbg; | ||
use mbedtls::ssl::config::{Endpoint, Preset, Transport}; | ||
use mbedtls::ssl::{Config, Context}; | ||
use mbedtls::ssl::context::HandshakeContext; | ||
use mbedtls::Result as TlsResult; | ||
use mbedtls::ssl::config::PskCallback; | ||
|
||
mod support; | ||
use support::entropy::entropy_new; | ||
|
||
fn client(conn: TcpStream, psk: &[u8]) -> TlsResult<()> | ||
{ | ||
let entropy = Arc::new(entropy_new()); | ||
let rng = Arc::new(CtrDrbg::new(entropy, None)?); | ||
let mut config = Config::new(Endpoint::Client, Transport::Stream, Preset::Default); | ||
config.set_rng(rng); | ||
config.set_psk(psk, "Client_identity")?; | ||
let mut ctx = Context::new(Arc::new(config)); | ||
ctx.establish(conn, None).map(|_| ()) | ||
} | ||
|
||
fn server<F>(conn: TcpStream, psk_callback: F) -> TlsResult<()> | ||
where | ||
F: PskCallback + Send + 'static, | ||
{ | ||
let entropy = Arc::new(entropy_new()); | ||
let rng = Arc::new(CtrDrbg::new(entropy, None)?); | ||
let mut config = Config::new(Endpoint::Server, Transport::Stream, Preset::Default); | ||
config.set_rng(rng); | ||
config.set_psk_callback(psk_callback); | ||
let mut ctx = Context::new(Arc::new(config)); | ||
ctx.establish(conn, None).map(|_| ()) | ||
} | ||
|
||
mod test { | ||
use super::*; | ||
use std::thread; | ||
use crate::support::net::create_tcp_pair; | ||
use crate::support::keys; | ||
|
||
#[test] | ||
fn callback_standard_psk() { | ||
let (c, s) = create_tcp_pair().unwrap(); | ||
|
||
let psk_callback = | ||
|ctx: &mut HandshakeContext, _: &str| { | ||
ctx.set_psk(keys::PRESHARED_KEY) | ||
}; | ||
let c = thread::spawn(move || super::client(c, keys::PRESHARED_KEY).unwrap()); | ||
let s = thread::spawn(move || super::server(s, psk_callback).unwrap()); | ||
c.join().unwrap(); | ||
s.join().unwrap(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,3 +92,7 @@ pub const ROOT_CA_KEY: &'static str = concat!(include_str!("./keys/ca.key"),"\0" | |
pub const EXPIRED_CERT_SUBJECT: &'static str = "CN=ExpiredNode"; | ||
pub const EXPIRED_CERT: &'static str = concat!(include_str!("./keys/expired.crt"),"\0"); | ||
pub const EXPIRED_KEY: &'static str = concat!(include_str!("./keys/expired.key"),"\0"); | ||
|
||
pub const PRESHARED_KEY: &'static [u8] = &[ | ||
234, 206, 151, 23, 219, 21, 71, 144, | ||
107, 42, 23, 67, 249, 173, 182, 224 ]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 What kind of behaviour do we expect for leading zero bytes? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 checkedfrom_utf8
so the error is more apparent?There was a problem hiding this comment.
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
toso there isn't a need to convert it.