-
Notifications
You must be signed in to change notification settings - Fork 149
Expose client/server-side ECH #309
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| use crate::error::ErrorStack; | ||
| use crate::{cvt_0i, cvt_p, ffi}; | ||
|
|
||
| use foreign_types::ForeignType; | ||
|
|
||
| foreign_type_and_impl_send_sync! { | ||
| type CType = ffi::EVP_HPKE_KEY; | ||
| fn drop = ffi::EVP_HPKE_KEY_free; | ||
|
|
||
| pub struct HpkeKey; | ||
| } | ||
|
|
||
| impl HpkeKey { | ||
| /// Allocates and initializes a key with the `EVP_HPKE_KEY` type using the | ||
| /// `EVP_hpke_x25519_hkdf_sha256` KEM algorithm. | ||
| pub fn dhkem_p256_sha256(pkey: &[u8]) -> Result<HpkeKey, ErrorStack> { | ||
| unsafe { | ||
| ffi::init(); | ||
| let hpke = cvt_p(ffi::EVP_HPKE_KEY_new()).map(|p| HpkeKey::from_ptr(p))?; | ||
|
|
||
| cvt_0i(ffi::EVP_HPKE_KEY_init( | ||
| hpke.as_ptr(), | ||
| ffi::EVP_hpke_x25519_hkdf_sha256(), | ||
| pkey.as_ptr(), | ||
| pkey.len(), | ||
| ))?; | ||
|
|
||
| Ok(hpke) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| use crate::ffi; | ||
| use foreign_types::{ForeignType, ForeignTypeRef}; | ||
| use libc::c_int; | ||
|
|
||
| use crate::error::ErrorStack; | ||
| use crate::hpke::HpkeKey; | ||
| use crate::{cvt_0i, cvt_p}; | ||
|
|
||
| foreign_type_and_impl_send_sync! { | ||
| type CType = ffi::SSL_ECH_KEYS; | ||
| fn drop = ffi::SSL_ECH_KEYS_free; | ||
|
|
||
| pub struct SslEchKeys; | ||
| } | ||
|
|
||
| impl SslEchKeys { | ||
| pub fn new() -> Result<SslEchKeys, ErrorStack> { | ||
| unsafe { | ||
| ffi::init(); | ||
| cvt_p(ffi::SSL_ECH_KEYS_new()).map(|p| SslEchKeys::from_ptr(p)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl SslEchKeysRef { | ||
| pub fn add_key( | ||
| &mut self, | ||
| is_retry_config: bool, | ||
| ech_config: &[u8], | ||
| key: HpkeKey, | ||
| ) -> Result<(), ErrorStack> { | ||
| unsafe { | ||
| cvt_0i(ffi::SSL_ECH_KEYS_add( | ||
| self.as_ptr(), | ||
| is_retry_config as c_int, | ||
| ech_config.as_ptr(), | ||
| ech_config.len(), | ||
| key.as_ptr(), | ||
| )) | ||
| .map(|_| ()) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| use crate::hpke::HpkeKey; | ||
| use crate::ssl::ech::SslEchKeys; | ||
| use crate::ssl::test::Server; | ||
| use crate::ssl::HandshakeError; | ||
|
|
||
| // For future reference, these configs are generated by building the bssl tool (the binary is built | ||
| // alongside boringssl) and running the following command: | ||
| // | ||
| // ./bssl generate-ech -out-ech-config-list ./list -out-ech-config ./config -out-private-key ./key | ||
| // -public-name ech.com -config-id 1 | ||
| static ECH_CONFIG_LIST: &[u8] = include_bytes!("../../../test/echconfiglist"); | ||
| static ECH_CONFIG: &[u8] = include_bytes!("../../../test/echconfig"); | ||
| static ECH_KEY: &[u8] = include_bytes!("../../../test/echkey"); | ||
|
|
||
| static ECH_CONFIG_2: &[u8] = include_bytes!("../../../test/echconfig-2"); | ||
| static ECH_KEY_2: &[u8] = include_bytes!("../../../test/echkey-2"); | ||
|
|
||
| #[test] | ||
| fn ech() { | ||
| let server = { | ||
| let key = HpkeKey::dhkem_p256_sha256(ECH_KEY).unwrap(); | ||
| let mut ech_keys = SslEchKeys::new().unwrap(); | ||
| ech_keys.add_key(true, ECH_CONFIG, key).unwrap(); | ||
|
|
||
| let mut builder = Server::builder(); | ||
| builder.ctx().set_ech_keys(ech_keys).unwrap(); | ||
|
|
||
| builder.build() | ||
| }; | ||
|
|
||
| let mut client = server.client_with_root_ca().build().builder(); | ||
| client.ssl().set_ech_config_list(ECH_CONFIG_LIST).unwrap(); | ||
| client.ssl().set_hostname("foobar.com").unwrap(); | ||
|
|
||
| let ssl_stream = client.connect(); | ||
| assert!(ssl_stream.ssl().ech_accepted()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn ech_rejection() { | ||
| let server = { | ||
| let key = HpkeKey::dhkem_p256_sha256(ECH_KEY_2).unwrap(); | ||
| let mut ech_keys = SslEchKeys::new().unwrap(); | ||
| ech_keys.add_key(true, ECH_CONFIG_2, key).unwrap(); | ||
|
|
||
| let mut builder = Server::builder(); | ||
| builder.ctx().set_ech_keys(ech_keys).unwrap(); | ||
|
|
||
| builder.build() | ||
| }; | ||
|
|
||
| let mut client = server.client_with_root_ca().build().builder(); | ||
| // Server is initialized using `ECH_CONFIG_2`, so using `ECH_CONFIG_LIST` instead of | ||
| // `ECH_CONFIG_LIST_2` should trigger rejection. | ||
| client.ssl().set_ech_config_list(ECH_CONFIG_LIST).unwrap(); | ||
| client.ssl().set_hostname("foobar.com").unwrap(); | ||
| let HandshakeError::Failure(failed_ssl_stream) = client.connect_err() else { | ||
| panic!("wrong HandshakeError failure variant!"); | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| failed_ssl_stream.ssl().get_ech_name_override(), | ||
| Some(b"ech.com".to_vec().as_ref()) | ||
| ); | ||
| assert!(failed_ssl_stream.ssl().get_ech_retry_configs().is_some()); | ||
| assert!(!failed_ssl_stream.ssl().ech_accepted()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| §5D$lþ°SLb~Ê.V<À.j¢ç¯:}´r¶ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.