#![allow(unreachable_patterns, dead_code, unused_variables)]
use std::collections::VecDeque;
use std::fmt;
use std::time::Instant;
use crate::net::DatagramSend;
use super::{CryptoError, Fingerprint, KeyingMaterial, SrtpProfile};
pub const DTLS_CERT_IDENTITY: &str = "WebRTC";
pub enum DtlsEvent {
Connected,
SrtpKeyingMaterial(KeyingMaterial, SrtpProfile),
RemoteFingerprint(Fingerprint),
Data(Vec<u8>),
}
#[derive(Clone)]
pub struct DtlsCert(DtlsCertInner);
#[derive(Debug, Clone)]
enum DtlsCertInner {
#[cfg(feature = "openssl")]
OpenSsl(super::ossl::OsslDtlsCert),
}
impl DtlsCert {
#[cfg(feature = "openssl")]
pub fn new_openssl() -> Self {
let cert = super::ossl::OsslDtlsCert::new();
DtlsCert(DtlsCertInner::OpenSsl(cert))
}
pub fn fingerprint(&self) -> Fingerprint {
match &self.0 {
#[cfg(feature = "openssl")]
DtlsCertInner::OpenSsl(v) => v.fingerprint(),
_ => unreachable!(),
}
}
pub(crate) fn create_dtls_impl(&self) -> Result<DtlsImpl, CryptoError> {
match &self.0 {
#[cfg(feature = "openssl")]
DtlsCertInner::OpenSsl(c) => Ok(DtlsImpl::OpenSsl(super::ossl::OsslDtlsImpl::new(
c.clone(),
)?)),
_ => unreachable!(),
}
}
}
impl fmt::Debug for DtlsCert {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
#[cfg(feature = "openssl")]
DtlsCertInner::OpenSsl(c) => c.fmt(f),
_ => unreachable!(),
}
}
}
pub trait DtlsInner: Sized {
fn set_active(&mut self, active: bool);
fn handle_handshake(&mut self, o: &mut VecDeque<DtlsEvent>) -> Result<bool, CryptoError>;
fn is_active(&self) -> Option<bool>;
fn handle_receive(&mut self, m: &[u8], o: &mut VecDeque<DtlsEvent>) -> Result<(), CryptoError>;
fn poll_datagram(&mut self) -> Option<DatagramSend>;
fn poll_timeout(&mut self, now: Instant) -> Option<Instant>;
fn handle_input(&mut self, data: &[u8]) -> Result<(), CryptoError>;
fn is_connected(&self) -> bool;
}
pub enum DtlsImpl {
#[cfg(feature = "openssl")]
OpenSsl(super::ossl::OsslDtlsImpl),
}
impl DtlsImpl {
pub fn set_active(&mut self, active: bool) {
match self {
#[cfg(feature = "openssl")]
DtlsImpl::OpenSsl(i) => i.set_active(active),
_ => unreachable!(),
}
}
pub fn handle_handshake(&mut self, o: &mut VecDeque<DtlsEvent>) -> Result<bool, CryptoError> {
match self {
#[cfg(feature = "openssl")]
DtlsImpl::OpenSsl(i) => i.handle_handshake(o),
_ => unreachable!(),
}
}
pub fn is_active(&self) -> Option<bool> {
match self {
#[cfg(feature = "openssl")]
DtlsImpl::OpenSsl(i) => i.is_active(),
_ => unreachable!(),
}
}
pub fn handle_receive(
&mut self,
m: &[u8],
o: &mut VecDeque<DtlsEvent>,
) -> Result<(), CryptoError> {
match self {
#[cfg(feature = "openssl")]
DtlsImpl::OpenSsl(i) => i.handle_receive(m, o),
_ => unreachable!(),
}
}
pub fn poll_datagram(&mut self) -> Option<DatagramSend> {
match self {
#[cfg(feature = "openssl")]
DtlsImpl::OpenSsl(i) => i.poll_datagram(),
_ => unreachable!(),
}
}
pub fn poll_timeout(&mut self, now: Instant) -> Option<Instant> {
match self {
#[cfg(feature = "openssl")]
DtlsImpl::OpenSsl(i) => i.poll_timeout(now),
_ => unreachable!(),
}
}
pub fn handle_input(&mut self, data: &[u8]) -> Result<(), CryptoError> {
match self {
#[cfg(feature = "openssl")]
DtlsImpl::OpenSsl(i) => i.handle_input(data),
_ => unreachable!(),
}
}
pub fn is_connected(&self) -> bool {
match self {
#[cfg(feature = "openssl")]
DtlsImpl::OpenSsl(i) => i.is_connected(),
_ => unreachable!(),
}
}
}