@@ -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