Skip to content

Commit daf68a1

Browse files
authored
feat(bindings): expose custom critical extension API (#5337)
1 parent e12a1fe commit daf68a1

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

.github/workflows/ci_rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ jobs:
6161
working-directory: ${{env.STANDARD_PATH}}
6262
run: cargo test
6363

64-
- name: "Feature Tests: Fingerprint, kTLS, QUIC, and PQ"
64+
- name: "Feature Tests: Fingerprint, kTLS, QUIC, PQ, and unstable-custom_x509_extensions"
6565
working-directory: ${{env.ROOT_PATH}}
6666
# Test all features except for FIPS, which is tested separately.
67-
run: cargo test --features unstable-fingerprint,unstable-ktls,quic,pq
67+
run: cargo test --features unstable-fingerprint,unstable-ktls,quic,pq,unstable-custom_x509_extensions
6868

6969
- name: "Feature Test: Renegotiate"
7070
working-directory: ${{env.ROOT_PATH}}

bindings/rust/extended/s2n-tls/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ unstable-fingerprint = ["s2n-tls-sys/unstable-fingerprint"]
1414
unstable-ktls = ["s2n-tls-sys/unstable-ktls"]
1515
unstable-renegotiate = ["s2n-tls-sys/unstable-renegotiate"]
1616
unstable-cert_authorities = ["s2n-tls-sys/unstable-cert_authorities"]
17+
unstable-custom_x509_extensions = ["s2n-tls-sys/unstable-custom_x509_extensions"]
1718
quic = ["s2n-tls-sys/quic"]
1819
fips = ["s2n-tls-sys/fips"]
1920
pq = ["s2n-tls-sys/pq"]

bindings/rust/extended/s2n-tls/src/config.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,25 @@ impl Builder {
604604
Ok(self)
605605
}
606606

607+
/// Corresponds to [s2n_config_add_custom_x509_extension].
608+
#[cfg(feature = "unstable-custom_x509_extensions")]
609+
pub fn add_custom_x509_extension(&mut self, extension_oid: &str) -> Result<&mut Self, Error> {
610+
let extension_oid_len: u32 = extension_oid
611+
.len()
612+
.try_into()
613+
.map_err(|_| Error::INVALID_INPUT)?;
614+
let extension_oid = extension_oid.as_ptr() as *mut u8;
615+
unsafe {
616+
s2n_config_add_custom_x509_extension(
617+
self.as_mut_ptr(),
618+
extension_oid,
619+
extension_oid_len,
620+
)
621+
.into_result()
622+
}?;
623+
Ok(self)
624+
}
625+
607626
/// Set a custom callback function which is run after parsing the client hello.
608627
///
609628
/// Corresponds to [s2n_config_set_client_hello_cb].
@@ -1163,4 +1182,52 @@ mod tests {
11631182

11641183
Ok(())
11651184
}
1185+
1186+
#[cfg(all(
1187+
// The `add_custom_x509_extension` API is only exposed when its unstable feature is enabled.
1188+
feature = "unstable-custom_x509_extensions",
1189+
// The `add_custom_x509_extension` API is only supported with AWS-LC, so
1190+
// this test is disabled for the external build, which may link to other libcryptos.
1191+
not(s2n_tls_external_build),
1192+
// The `add_custom_x509_extension` API is currently unsupported with AWS-LC-FIPS.
1193+
not(feature = "fips")
1194+
))]
1195+
#[test]
1196+
fn custom_critical_extensions() -> Result<(), Error> {
1197+
use crate::testing::*;
1198+
1199+
let certs = CertKeyPair::from_path(
1200+
"custom_oids/",
1201+
"single_oid_cert_chain",
1202+
"single_oid_key",
1203+
"ca-cert",
1204+
);
1205+
let single_oid = "1.3.187.25240.2";
1206+
1207+
for add_oid in [true, false] {
1208+
let config = {
1209+
let mut config = Builder::new();
1210+
config.set_security_policy(&security::DEFAULT_TLS13)?;
1211+
config.set_verify_host_callback(InsecureAcceptAllCertificatesHandler {})?;
1212+
1213+
if add_oid {
1214+
config.add_custom_x509_extension(single_oid)?;
1215+
}
1216+
1217+
config.load_pem(certs.cert(), certs.key())?;
1218+
config.trust_pem(certs.cert())?;
1219+
config.build()?
1220+
};
1221+
let mut pair = TestPair::from_config(&config);
1222+
1223+
if add_oid {
1224+
pair.handshake()?;
1225+
} else {
1226+
let s2n_err = pair.handshake().unwrap_err();
1227+
assert_eq!(s2n_err.name(), "S2N_ERR_CERT_UNHANDLED_CRITICAL_EXTENSION");
1228+
}
1229+
}
1230+
1231+
Ok(())
1232+
}
11661233
}

0 commit comments

Comments
 (0)