@@ -626,8 +626,7 @@ use crypto::CryptoProvider;
626
626
use crypto:: Fingerprint ;
627
627
628
628
mod dtls;
629
- use dtls:: DtlsCert ;
630
- use dtls:: { Dtls , DtlsEvent } ;
629
+ use dtls:: { Dtls , DtlsCert , DtlsCertOptions , DtlsEvent } ;
631
630
632
631
#[ path = "ice/mod.rs" ]
633
632
mod ice_;
@@ -637,7 +636,7 @@ pub use ice_::{Candidate, CandidateKind, IceConnectionState, IceCreds};
637
636
638
637
/// Additional configuration.
639
638
pub mod config {
640
- pub use super :: crypto:: { CryptoProvider , DtlsCert , Fingerprint } ;
639
+ pub use super :: crypto:: { CryptoProvider , DtlsCert , DtlsCertOptions , DtlsPKeyType , Fingerprint } ;
641
640
}
642
641
643
642
/// Low level ICE access.
@@ -1141,10 +1140,9 @@ impl Rtc {
1141
1140
ice. set_ice_lite ( config. ice_lite ) ;
1142
1141
}
1143
1142
1144
- let dtls_cert = if let Some ( c) = config. dtls_cert {
1145
- c
1146
- } else {
1147
- DtlsCert :: new ( config. crypto_provider )
1143
+ let dtls_cert = match config. dtls_cert_config {
1144
+ DtlsCertConfig :: Options ( options) => DtlsCert :: new ( config. crypto_provider , options) ,
1145
+ DtlsCertConfig :: PregeneratedCert ( cert) => cert,
1148
1146
} ;
1149
1147
1150
1148
let crypto_provider = dtls_cert. crypto_provider ( ) ;
@@ -1854,6 +1852,25 @@ impl Rtc {
1854
1852
}
1855
1853
}
1856
1854
1855
+ /// Configuation for the DTLS certificate used for the Rtc instance. This can be set to
1856
+ /// allow a pregenerated certificate, or options to pass when generating a certificate
1857
+ /// on-the-fly.
1858
+ ///
1859
+ /// The default value is DtlsCertConfig::Options(DtlsCertOptions::default())
1860
+ #[ derive( Clone , Debug ) ]
1861
+ pub enum DtlsCertConfig {
1862
+ /// The options to use for the DTLS certificate generated for this Rtc instance.
1863
+ Options ( DtlsCertOptions ) ,
1864
+ /// A pregenerated certificate to use for this Rtc instance.
1865
+ PregeneratedCert ( DtlsCert ) ,
1866
+ }
1867
+
1868
+ impl Default for DtlsCertConfig {
1869
+ fn default ( ) -> Self {
1870
+ DtlsCertConfig :: Options ( DtlsCertOptions :: default ( ) )
1871
+ }
1872
+ }
1873
+
1857
1874
/// Customized config for creating an [`Rtc`] instance.
1858
1875
///
1859
1876
/// ```
@@ -1871,7 +1888,7 @@ impl Rtc {
1871
1888
pub struct RtcConfig {
1872
1889
local_ice_credentials : Option < IceCreds > ,
1873
1890
crypto_provider : CryptoProvider ,
1874
- dtls_cert : Option < DtlsCert > ,
1891
+ dtls_cert_config : DtlsCertConfig ,
1875
1892
fingerprint_verification : bool ,
1876
1893
ice_lite : bool ,
1877
1894
codec_config : CodecConfig ,
@@ -1921,7 +1938,7 @@ impl RtcConfig {
1921
1938
///
1922
1939
/// This overrides what is set in [`CryptoProvider::install_process_default()`].
1923
1940
pub fn set_crypto_provider ( mut self , p : CryptoProvider ) -> Self {
1924
- if let Some ( c) = & self . dtls_cert {
1941
+ if let DtlsCertConfig :: PregeneratedCert ( c) = & self . dtls_cert_config {
1925
1942
if p != c. crypto_provider ( ) {
1926
1943
panic ! ( "set_dtls_cert() locked crypto provider to: {}" , p) ;
1927
1944
}
@@ -1939,46 +1956,48 @@ impl RtcConfig {
1939
1956
self . crypto_provider
1940
1957
}
1941
1958
1942
- /// Get the configured DTLS certificate, if set.
1943
- ///
1944
- /// Returns [`None`] if no DTLS certificate is set. In such cases,
1945
- /// the certificate will be created on build and you can use the
1946
- /// direct API on an [`Rtc`] instance to obtain the local
1947
- /// DTLS fingerprint.
1959
+ /// Returns the configured DTLS certificate configuration.
1948
1960
///
1961
+ /// Defaults to a configuration similar to libwebrtc:
1949
1962
/// ```
1950
- /// # #[cfg(feature = "openssl")] {
1951
- /// # use str0m::RtcConfig ;
1952
- /// let fingerprint = RtcConfig::default()
1953
- /// .build()
1954
- /// .direct_api()
1955
- /// .local_dtls_fingerprint();
1956
- /// # }
1963
+ /// # use str0m::DtlsCertConfig;
1964
+ /// # use str0m::config::{DtlsCertOptions, DtlsPKeyType} ;
1965
+ ///
1966
+ /// DtlsCertConfig::Options(DtlsCertOptions {
1967
+ /// common_name: "WebRTC".into(),
1968
+ /// pkey_type: DtlsPKeyType::EcDsaP256,
1969
+ /// });
1957
1970
/// ```
1958
- pub fn dtls_cert ( & self ) -> Option < & DtlsCert > {
1959
- self . dtls_cert . as_ref ( )
1971
+ pub fn dtls_cert_config ( & self ) -> & DtlsCertConfig {
1972
+ & self . dtls_cert_config
1960
1973
}
1961
1974
1962
- /// Set the DTLS certificate for secure communication .
1975
+ /// Set the DTLS certificate configuration for certificate generation .
1963
1976
///
1964
- /// Generating a certificate can be a time-consuming process.
1965
- /// Use this API to reuse a previously created [`DtlsCert`] if available.
1977
+ /// Setting this permits you to assign a Pregenerated certificate, or
1978
+ /// options for certificate generation, such as signing key type, and
1979
+ /// subject name.
1966
1980
///
1967
- /// Setting this locks the `crypto_provider()` setting to the [`CryptoProvider`],
1968
- /// for the DTLS certificate.
1981
+ /// If a Pregenerated certificate is set, this locks the `crypto_provider()`
1982
+ /// setting to the [`CryptoProvider`], for the DTLS certificate.
1969
1983
///
1970
1984
/// ```
1971
- /// # use str0m::RtcConfig;
1972
- /// # use str0m::config::{DtlsCert, CryptoProvider };
1985
+ /// # use str0m::{DtlsCertConfig, RtcConfig} ;
1986
+ /// # use str0m::config::{DtlsCertOptions, DtlsPKeyType };
1973
1987
///
1974
- /// let dtls_cert = DtlsCert::new(CryptoProvider::OpenSsl);
1988
+ /// let dtls_cert_config = DtlsCertConfig::Options(DtlsCertOptions {
1989
+ /// common_name: "Clark Kent".into(),
1990
+ /// pkey_type: DtlsPKeyType::EcDsaP256,
1991
+ /// });
1975
1992
///
1976
1993
/// let rtc_config = RtcConfig::default()
1977
- /// .set_dtls_cert(dtls_cert );
1994
+ /// .set_dtls_cert_config(dtls_cert_config );
1978
1995
/// ```
1979
- pub fn set_dtls_cert ( mut self , dtls_cert : DtlsCert ) -> Self {
1980
- self . crypto_provider = dtls_cert. crypto_provider ( ) ;
1981
- self . dtls_cert = Some ( dtls_cert) ;
1996
+ pub fn set_dtls_cert_config ( mut self , dtls_cert_config : DtlsCertConfig ) -> Self {
1997
+ if let DtlsCertConfig :: PregeneratedCert ( ref cert) = dtls_cert_config {
1998
+ self . crypto_provider = cert. crypto_provider ( ) ;
1999
+ }
2000
+ self . dtls_cert_config = dtls_cert_config;
1982
2001
self
1983
2002
}
1984
2003
@@ -2388,7 +2407,7 @@ impl Default for RtcConfig {
2388
2407
Self {
2389
2408
local_ice_credentials : None ,
2390
2409
crypto_provider : CryptoProvider :: process_default ( ) . unwrap_or ( CryptoProvider :: OpenSsl ) ,
2391
- dtls_cert : None ,
2410
+ dtls_cert_config : Default :: default ( ) ,
2392
2411
fingerprint_verification : true ,
2393
2412
ice_lite : false ,
2394
2413
codec_config : CodecConfig :: new_with_defaults ( ) ,
0 commit comments