[−][src]Struct libtls::tls::Tls
TLS connection clients and servers.
A TLS connection is represented as a Tls
object called a
"context". A new context is created by either the Tls::client
or Tls::server
functions. Tls::client
is used in TLS client
programs, Tls::server
in TLS server programs.
The context can then be configured with the configure
method.
The same TlsConfig
object can be used to configure multiple contexts.
After configuration, connect
can be called on objects created with
Tls::client
, and accept_socket
on objects created with
Tls::server
.
After use, a TLS context should be closed with tls_close
, which
is also called when the object is dropped. A TLS context can be
reset by calling reset
, allowing for it to be reused.
Methods
impl Tls
[src]
pub fn client() -> Result<Self>
[src]
pub fn server() -> Result<Self>
[src]
pub fn configure(&mut self, config: &TlsConfig) -> Result<()>
[src]
Configure the TLS context.
The configure
method configures a TLS connection. The
same TlsConfig
object can be used to configure multiple
contexts.
See also
pub unsafe fn from_sys(tls: *mut tls) -> Self
[src]
Wrap a raw C tls
object.
Safety
This function assumes that the raw pointer is valid, and takes
ownership of the libtls object.
Do not call tls_free
yourself, since the drop
destructor will
take care of it.
Panics
Panics if tls
is a null pointer.
pub fn reset(&mut self)
[src]
pub fn accept_fds(&mut self, fd_read: RawFd, fd_write: RawFd) -> Result<Tls>
[src]
Accept a new TLS connection on a pair of existing file descriptors.
The accept_fds
method can accept a new client connection on a pair
of existing file descriptors.
See also
pub fn accept_socket(&mut self, socket: RawFd) -> Result<Tls>
[src]
Accept a new TLS connection on a socket.
The accept_socket
method can accept a new client
connection on an already established
socket connection.
The socket RawFd
is not closed after dropping the object.
See also
pub fn accept_raw_fd<T>(&mut self, raw_fd: &T) -> Result<Tls> where
T: AsRawFd,
[src]
T: AsRawFd,
Accept a new TLS connection on an established connection.
The accept_raw_fd
method can accept a new client connection on an
already established connection that implements the AsRawFd
trait,
e.g. TcpStream
.
It is a wrapper function on top of accept_socket
.
See also
pub unsafe fn accept_cbs(
&mut self,
read_cb: TlsReadCb,
write_cb: TlsWriteCb,
cb_arg: Option<*mut c_void>
) -> Result<Tls>
[src]
&mut self,
read_cb: TlsReadCb,
write_cb: TlsWriteCb,
cb_arg: Option<*mut c_void>
) -> Result<Tls>
Accept a new TLS connection with custom I/O callbacks.
The accept_cbs
method allows read and write callback functions to
handle data transfers. The specified cb_arg
parameter is passed back to
the functions, and can contain a pointer to any caller-specified data.
See also
pub fn connect(&mut self, host: &str, port: Option<&str>) -> Result<()>
[src]
Initiate a new TLS connection.
The connect
method initiates a new client connection on a
Tls
object that has been configured with configure
.
This method will create a new socket, connect to the specified host and port,
and then establish a secure connection. The port may be numeric or a
service name. If it is None, then a host of the format "hostname:port"
is permitted. The name to use for verification is inferred from the host value.
See also
pub fn connect_fds(
&mut self,
fd_read: RawFd,
fd_write: RawFd,
servername: &str
) -> Result<()>
[src]
&mut self,
fd_read: RawFd,
fd_write: RawFd,
servername: &str
) -> Result<()>
Initiate a new TLS connection over a pair of existing file descriptors.
The connect_fds
method is a variant of connect
that
establishes a secure connection over a pair of existing file
descriptors. The servername
argument is used for verification of
the TLS server name.
See also
pub fn connect_servername<A: ToSocketAddrs>(
&mut self,
host: A,
servername: &str
) -> Result<()>
[src]
&mut self,
host: A,
servername: &str
) -> Result<()>
Initiate a new TLS connection with a specified server name.
The connect_servername
method has the same behaviour as connect
,
however the name to use for verification is explicitly provided,
for the case where the TLS server name differs from the DNS name.
See also
pub fn connect_socket(&mut self, socket: RawFd, servername: &str) -> Result<()>
[src]
Initiate a new TLS connection over an established socket.
The connect_socket
method is a variant of connect_servername
that
can upgrade an already existing socket to TLS.
The socket RawFd
is not closed after dropping the object.
See also
pub fn connect_raw_fd<T>(&mut self, raw_fd: &T, servername: &str) -> Result<()> where
T: AsRawFd,
[src]
T: AsRawFd,
Initiate a new TLS connection over an established connection.
The connect_raw_fd
method can upgrade a connection to TLS on an
already established connection that implements the [AsRawFd] trait, e.g. [
TcpStream]. It is a wrapper function on top of [
connect_socket`].
See also
pub unsafe fn connect_cbs(
&mut self,
read_cb: TlsReadCb,
write_cb: TlsWriteCb,
cb_arg: Option<*mut c_void>,
servername: &str
) -> Result<()>
[src]
&mut self,
read_cb: TlsReadCb,
write_cb: TlsWriteCb,
cb_arg: Option<*mut c_void>,
servername: &str
) -> Result<()>
Initiate a new TLS connection with custom I/O callbacks.
The connect_cbs
method allows read and write callback functions to
handle data transfers. The specified cb_arg
parameter is passed back to
the functions, and can contain a pointer to any caller-specified data.
The servername
is used to validate the TLS server name.
See also
pub fn tls_handshake(&mut self) -> Result<isize>
[src]
Explicitly perform the TLS handshake.
The tls_handshake
method explicitly performs the TLS handshake. It is only
necessary to call this method if you need to guarantee that the
handshake has completed, as both tls_read
and tls_write
automatically
perform the TLS handshake when necessary.
The tls_read
, tls_write
, tls_handshake
, and tls_close
methods
return -1 on error and also have two special return values:
TLS_WANT_POLLIN
: The underlying read file descriptor needs to be readable in order to continue.TLS_WANT_POLLOUT
: The underlying write file descriptor needs to be writeable in order to continue.
In the case of blocking file descriptors, the same function call should be repeated immediately. In the case of non-blocking file descriptors, the same function call should be repeated when the required condition has been met.
On success, the tls_read
and tls_write
methods return a size and
the tls_handshake
and tls_close
methods return 0.
See also
pub fn tls_read(&mut self, buf: &mut [u8]) -> Result<isize>
[src]
Read bytes from the TLS connection.
The tls_read
method reads bytes of data from the connection into buf
. It
returns the amount of data read or an error as described in tls_handshake
.
This function is provided for the completeness of the API, programs should
use the implemented read
function of the Read
trait instead.
See also
pub fn tls_write(&mut self, buf: &[u8]) -> Result<isize>
[src]
Write bytes to the TLS connection.
The tls_write
method writes bytes of data from buf
to connection. It
returns the amount of data written or an error as described in tls_handshake
.
This function is provided for the completeness of the API, programs should
use the implemented write
function of the Write
trait instead.
See also
pub fn tls_close(&mut self) -> Result<isize>
[src]
Close the TLS connection.
The tls_close
method closes a connection after use. Only the TLS layer will be
shut down and the caller is responsible for closing the file descriptors,
unless the connection was established using connect
or
connect_servername
.
It returns 0 on success or an error as decribed in tls_handshake
.
See also
pub fn close(&mut self) -> Result<()>
[src]
Close the TLS connection.
The close
method closes a connection after use.
It calls tls_close
and converts the result into an io::Error
.
See also
pub fn peer_cert_provided(&mut self) -> bool
[src]
Check for peer certificate.
The peer_cert_provided
methods checks if the peer has provided a
certificate.
See also
pub fn peer_cert_contains_name(&mut self, name: &str) -> Result<bool>
[src]
Check if the peer certificate includes a matching name.
The peer_cert_contains_name
method checks if the peer has
provided a certificate that contains a SAN or CN that matches name.
See also
pub fn peer_cert_hash(&mut self) -> Result<String>
[src]
Return hash of the peer certificate.
The peer_cert_hash
method returns a string corresponding to a hash
of the raw peer certificate prefixed by a hash name followed by a colon.
The hash currently used is SHA256, though this could change in the
future.
The hash string for a certificate in file mycert.crt
can be
generated using the commands:
h=$(openssl x509 -outform der -in mycert.crt | sha256)
printf "SHA256:${h}\n"
See also
pub fn peer_cert_issuer(&mut self) -> Result<String>
[src]
Return the issuer of the peer certificate.
The peer_cert_issuer
method returns a string corresponding to the issuer of
the peer certificate.
See also
pub fn peer_cert_subject(&mut self) -> Result<String>
[src]
Return the subject of the peer certificate.
The peer_cert_subject
method returns a string corresponding to the subject of
the peer certificate.
See also
pub fn peer_cert_notbefore(&mut self) -> Result<SystemTime>
[src]
Return the start of the validity period of the peer certififcate.
The peer_cert_notbefore
method returns the time corresponding to the start of
the validity period of the peer certificate.
See also
pub fn peer_cert_notafter(&mut self) -> Result<SystemTime>
[src]
Return the end of the validity period of the peer certififcate.
The peer_cert_notafter
method returns the time corresponding to the end of
the validity period of the peer certificate.
The peer_cert_notafter
method
See also
pub fn peer_cert_chain_pem(&mut self) -> Result<Vec<u8>>
[src]
Return the PEM-encoded peer certificate.
The peer_cert_chain_pem
method returns a vector of memory containing a PEM-
encoded certificate chain for the peer certificate.
See also
pub fn conn_alpn_selected(&mut self) -> Option<String>
[src]
Return the selected ALPN protocol.
The conn_alpn_selected
method returns a string that specifies the ALPN
protocol selected for use with the peer. If no protocol
was selected then None
is returned.
See also
pub fn conn_cipher(&mut self) -> Result<String>
[src]
Return the negotiated cipher suite.
The conn_cipher
method returns a string corresponding to the cipher suite
negotiated with the peer.
See also
pub fn conn_servername(&mut self) -> Result<String>
[src]
Return the client's server name.
The conn_servername
method returns a string corresponding to the servername
that the client connected to the server requested by sending a TLS Server Name
Indication extension (server only).
See also
pub fn conn_session_resumed(&mut self) -> bool
[src]
Check if a TLS session has been resumed.
The conn_session_resumed
method indicates whether a TLS session has been
resumed during the handshake with the server connected to the client (client
only).
See also
pub fn conn_version(&mut self) -> Result<String>
[src]
Return the negotiated TLS version as a string.
The conn_version
method returns a string corresponding to a TLS version
negotiated with the peer.
See also
pub fn ocsp_process_response(&mut self, response: &[u8]) -> Result<()>
[src]
Process a raw OCSP response.
The ocsp_process_response
method processes a raw OCSP response in response of
size size to check the revocation status of the peer certificate.
A successful result indicates that the certificate has not been revoked.
See also
pub fn peer_ocsp_cert_status(&mut self) -> Result<isize>
[src]
OCSP certificate status.
The peer_ocsp_cert_status
method returns the OCSP certificate status code as
per RFC 6960 section 2.2.
See also
pub fn peer_ocsp_crl_reason(&mut self) -> Result<isize>
[src]
OCSP certificate revocation reason.
The peer_ocsp_crl_reason
method returns the OCSP certificate revocation reason
status code as per RFC 5280 section 5.3.1.
See also
pub fn peer_ocsp_next_update(&mut self) -> Result<SystemTime>
[src]
pub fn peer_ocsp_response_status(&mut self) -> Result<isize>
[src]
OCSP response status.
The peer_ocsp_response_status
method returns the OCSP response status as per
RFC 6960 section 2.3.
See also
pub fn peer_ocsp_result(&mut self) -> Result<String>
[src]
Textual representation of the OCSP status code.
The peer_ocsp_result
method returns a textual representation of the OCSP
status code returned by one of the previous three functions. If the OCSP
response was valid and the certificate was not revoked, the string
indicates the OCSP certificate status. Otherwise, the string indicates
the OCSP certificate revocation reason or the OCSP error.
See also
pub fn peer_ocsp_revocation_time(&mut self) -> Result<SystemTime>
[src]
OCSP revocation time.
The peer_ocsp_revocation_time
method returns the OCSP revocation time.
See also
pub fn peer_ocsp_this_update(&mut self) -> Result<SystemTime>
[src]
pub fn peer_ocsp_url(&mut self) -> Result<String>
[src]
OCSP validation URL.
The peer_ocsp_url
method returns the URL for OCSP validation of the peer
certificate.
See also
Trait Implementations
impl LastError for Tls
[src]
fn last_error(&self) -> Result<String>
[src]
Returns the last error of the TLS context.
The last_error
method returns an error if no error occurred with
the TLS context during or since the last call to tls_handshake
,
tls_read
, tls_write
, tls_close
, or reset
involving the context,
or if memory allocation failed while trying to assemble the string
describing the most recent error related to the context.
See also
fn to_error<T>(errstr: String) -> Result<T>
[src]
impl Send for Tls
[src]
impl Drop for Tls
[src]
fn drop(&mut self)
[src]
The drop
method frees the Tls
context and forcibly closes
the connection.
Please note that it calls both tls_close(3)
and tls_free(3)
internally to avoid leaking the internal socket file descriptor.
libtls
itself does not close the socket when calling tls_free(3)
and requires the program to call tls_close(3)
itself but
this would be unsafe in Rust when applied to the Drop
trait.
See also
impl Sync for Tls
[src]
impl Debug for Tls
[src]
impl Read for Tls
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[src]
Read from the TLS connection.
The read
method reads bytes of data from the connection into buf
.
See also
fn read_vectored(&mut self, bufs: &mut [IoSliceMut]) -> Result<usize, Error>
1.36.0[src]
unsafe fn initializer(&self) -> Initializer
[src]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
1.0.0[src]
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
fn bytes(self) -> Bytes<Self>
1.0.0[src]
fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
R: Read,
fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
impl Write for Tls
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>
[src]
Write to the TLS connection.
The write
method writes bytes of data from buf
to the connection.
See also
fn flush(&mut self) -> Result<()>
[src]
fn write_vectored(&mut self, bufs: &[IoSlice]) -> Result<usize, Error>
1.36.0[src]
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
fn write_fmt(&mut self, fmt: Arguments) -> Result<(), Error>
1.0.0[src]
fn by_ref(&mut self) -> &mut Self
1.0.0[src]
impl AsRawFd for Tls
[src]
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,