Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tls options mutable #2

Open
wants to merge 2 commits into
base: feature/tls-options
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 16 additions & 47 deletions src/tls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{addr::HostSocketAddr, AddrType, Dns, TcpClientStack};
use crate::{addr::HostSocketAddr, TcpClientStack};
use core::convert::{TryFrom, TryInto};
use heapless::{consts, Vec};

Expand Down Expand Up @@ -136,48 +136,17 @@ impl Default for Protocol {
}
}

trait DnsTls: Tls + Dns {
fn connect(
&self,
socket: &mut <Self as TcpClientStack>::TcpSocket,
hostname: &str,
connector: &Self::TlsConnector,
) -> nb::Result<(), <Self as Tls>::Error>;
}

impl<T> DnsTls for T
where
T: Tls + Dns,
<T as Dns>::Error: Into<<T as Tls>::Error>,
{
fn connect(
&self,
socket: &mut <Self as TcpClientStack>::TcpSocket,
addr: &str,
connector: &Self::TlsConnector,
) -> nb::Result<(), <Self as Tls>::Error> {
// TODO: Document and verify `addr`
let mut iter = addr.rsplitn(2, ':');
let hostname = iter.next().unwrap();
let port = iter.next().map(|p| p.parse().unwrap()).unwrap();

let remote = Dns::get_host_by_name(self, hostname, AddrType::IPv4).map_err(|e| e.into())?;
Tls::connect(self, socket, HostSocketAddr::new(remote, port), connector)
}
}

/// This trait extends implementer of TCP/IP stacks with Tls capability.
pub trait Tls: TcpClientStack {
type Error: From<<Self as TcpClientStack>::Error>;
type TlsConnector;
pub trait TlsConnect<T> where T: TcpClientStack {
type Error: From<<T as TcpClientStack>::Error>;

/// Connect securely to the given remote host and port.
fn connect(
&self,
socket: &mut <Self as TcpClientStack>::TcpSocket,
&mut self,
client: &T,
socket: &mut T::TcpSocket,
remote: HostSocketAddr,
connector: &Self::TlsConnector,
) -> nb::Result<(), <Self as Tls>::Error>;
) -> nb::Result<(), Self::Error>;
}

// A collection of TLS configuration options plus a user-defined contextual
Expand All @@ -192,7 +161,7 @@ pub trait Tls: TcpClientStack {
// offer>.
#[derive(Clone, Debug, Default)]
pub struct TlsConnectorConfig<'a, CTX> {
context: CTX,
context: Option<CTX>,
identity: Option<Identity<'a>>,
min_protocol: Protocol,
max_protocol: Option<Protocol>,
Expand All @@ -205,13 +174,13 @@ pub struct TlsConnectorConfig<'a, CTX> {
impl<'a, CTX> TlsConnectorConfig<'a, CTX> {
/// Returns a reference to `CTX` which has been passed to the `build` method
/// earlier.
pub fn context(&self) -> &CTX {
&self.context
pub fn context(&mut self) -> &mut Option<CTX> {
&mut self.context
}

/// Returns an identity.
pub fn identity(&self) -> &Option<Identity<'a>> {
&self.identity
pub fn identity(&mut self) -> &mut Option<Identity<'a>> {
&mut self.identity
}

/// Returns the minimum supported protocol version.
Expand All @@ -224,8 +193,8 @@ impl<'a, CTX> TlsConnectorConfig<'a, CTX> {
&self.max_protocol
}

pub fn root_certificates(&self) -> &Vec<Certificate<'a>, consts::U10> {
&self.root_certificates
pub fn root_certificates(&mut self) -> &mut Vec<Certificate<'a>, consts::U10> {
&mut self.root_certificates
}

pub fn accept_invalid_certs(&self) -> bool {
Expand Down Expand Up @@ -346,9 +315,9 @@ impl<'a> TlsConnectorBuilder<'a> {
self
}

pub fn build<'b, CTX, CONN>(&'b mut self, ctx: &'b CTX) -> Result<CONN, CONN::Error>
pub fn build<'b, CTX, CONN>(&'b mut self, ctx: CTX) -> Result<CONN, CONN::Error>
where
CONN: TryFrom<TlsConnectorConfig<'a, &'b CTX>>,
CONN: TryFrom<TlsConnectorConfig<'a, CTX>>,
{
self.context(ctx).try_into()
}
Expand Down