Skip to content

Commit 0dd25bb

Browse files
committed
change Crypto -> SslConfig and add doc comments
1 parent aa12d52 commit 0dd25bb

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

src/crypto.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,26 @@ use openssl::{
1111
x509::{X509NameBuilder, X509},
1212
};
1313

14+
/// A TLS private / public key pair and certificate.
1415
#[derive(Clone)]
15-
pub struct Crypto {
16+
pub struct SslConfig {
1617
pub(crate) fingerprint: String,
1718
pub(crate) ssl_acceptor: Arc<SslAcceptor>,
1819
}
1920

20-
impl Crypto {
21-
pub fn init() -> Result<Crypto, ErrorStack> {
21+
impl SslConfig {
22+
/// Generates an anonymous private / public key pair and self-signed certificate.
23+
///
24+
/// The certificate can be self-signed because the trust in the `webrtc-unreliable` server comes
25+
/// form the certificate fingerprint embedded in the session response. If the session response
26+
/// descriptor is deliviered over a trusted channel (such as HTTPS with a valid server
27+
/// certificate), the client will verify that the self-signed certificate matches
28+
/// the fingerprint, and so the resulting DTLS connection will have the same level of
29+
/// authentication.
30+
///
31+
/// Client connections are assumed to be anonymous and are unverified, authentication can be
32+
/// handled through the resulting WebRTC data channel.
33+
pub fn create() -> Result<SslConfig, ErrorStack> {
2234
const X509_DAYS_NOT_BEFORE: u32 = 0;
2335
const X509_DAYS_NOT_AFTER: u32 = 365;
2436

@@ -73,7 +85,7 @@ impl Crypto {
7385
ssl_acceptor_builder.set_certificate(&x509)?;
7486
let ssl_acceptor = Arc::new(ssl_acceptor_builder.build());
7587

76-
Ok(Crypto {
88+
Ok(SslConfig {
7789
fingerprint,
7890
ssl_acceptor,
7991
})

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod stun;
99
mod util;
1010

1111
pub use client::{MessageType, MAX_MESSAGE_LEN};
12-
pub use crypto::Crypto;
12+
pub use crypto::SslConfig;
1313
pub use server::{MessageBuffer, MessageResult, SendError, Server, SessionEndpoint};
1414

1515
#[cfg(feature = "tokio")]

src/server.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rand::thread_rng;
2323
use crate::{
2424
buffer_pool::{BufferHandle, BufferPool, OwnedBuffer},
2525
client::{Client, ClientError, MessageType, MAX_UDP_PAYLOAD_SIZE},
26-
crypto::Crypto,
26+
crypto::SslConfig,
2727
runtime::{Runtime, UdpSocket},
2828
sdp::{gen_sdp_response, parse_sdp_fields, SdpFields},
2929
stun::{parse_stun_binding_request, write_stun_success_response},
@@ -123,12 +123,14 @@ pub struct SessionEndpoint {
123123

124124
impl SessionEndpoint {
125125
/// Receives an incoming SDP descriptor of an `RTCSessionDescription` from a browser, informs
126-
/// the corresponding `Server` of the new WebRTC session, and returns a JSON object containing
127-
/// objects which can construct an `RTCSessionDescription` and an `RTCIceCandidate` in a
128-
/// browser.
126+
/// the corresponding `Server` of the new WebRTC session, and returns a JSON object
127+
/// containing two fields:
128+
/// 1) An `answer` field which is an SDP descriptor that can be used to construct an
129+
/// `RTCSessionDescription`.
130+
/// 2) a `candidate` field which is a configuration object for an `RTCIceCandidate`.
129131
///
130-
/// The returned JSON object contains a digest of the x509 certificate the server will use for
131-
/// DTLS, and the browser will ensure that this digest matches before starting a WebRTC
132+
/// The returned SDP descriptor contains a digest of the x509 certificate the server will use
133+
/// for DTLS, and the browser will ensure that this digest matches before starting a WebRTC
132134
/// connection.
133135
pub async fn session_request<I, E, S>(
134136
&mut self,
@@ -224,20 +226,25 @@ impl<R: Runtime> Server<R> {
224226
listen_addr: SocketAddr,
225227
public_addr: SocketAddr,
226228
) -> Result<Self, IoError> {
227-
Server::with_crypto(
229+
Server::with_ssl_config(
228230
runtime,
229231
listen_addr,
230232
public_addr,
231-
Crypto::init().expect("WebRTC server could not initialize OpenSSL primitives"),
233+
SslConfig::create().expect("WebRTC server could not initialize OpenSSL primitives"),
232234
)
233235
.await
234236
}
235237

236-
pub async fn with_crypto(
238+
/// Start a new WebRTC data channel server with the given `SslConfig`.
239+
///
240+
/// This can be used to share self-signed TLS certificates between different `Server` instances,
241+
/// which is important in certain browsers (Firefox) when connecting to multiple WebRTC
242+
/// endpoints from the same page.
243+
pub async fn with_ssl_config(
237244
runtime: R,
238245
listen_addr: SocketAddr,
239246
public_addr: SocketAddr,
240-
crypto: Crypto,
247+
crypto: SslConfig,
241248
) -> Result<Self, IoError> {
242249
const SESSION_BUFFER_SIZE: usize = 8;
243250

0 commit comments

Comments
 (0)