blob: f64d3ede7d1d03d40d3d7305906cfb8ea4a10fac [file] [log] [blame]
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="An abstraction over platform-specific TLS implementations."><meta name="keywords" content="rust, rustlang, rust-lang, native_tls"><title>native_tls - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../native_tls/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../native_tls/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate native_tls</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.2.11</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#types">Type Definitions</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">native_tls</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/native_tls/lib.rs.html#1-721">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>An abstraction over platform-specific TLS implementations.</p>
<p>Many applications require TLS/SSL communication in one form or another as
part of their implementation, but finding a library for this isn’t always
trivial! The purpose of this crate is to provide a seamless integration
experience on all platforms with a cross-platform API that deals with all
the underlying details for you.</p>
<h2 id="how-is-this-implemented"><a href="#how-is-this-implemented">How is this implemented?</a></h2>
<p>This crate uses SChannel on Windows (via the <code>schannel</code> crate), Secure
Transport on OSX (via the <code>security-framework</code> crate), and OpenSSL (via the
<code>openssl</code> crate) on all other platforms. Future features may also enable
other TLS frameworks as well, but these initial libraries are likely to
remain as the defaults.</p>
<p>Note that this crate also strives to be secure-by-default. For example when
using OpenSSL it will configure validation callbacks to ensure that
hostnames match certificates, use strong ciphers, etc. This implies that
this crate is <em>not</em> just a thin abstraction around the underlying libraries,
but also an implementation that strives to strike reasonable defaults.</p>
<h2 id="supported-features"><a href="#supported-features">Supported features</a></h2>
<p>This crate supports the following features out of the box:</p>
<ul>
<li>TLS/SSL client communication</li>
<li>TLS/SSL server communication</li>
<li>PKCS#12 encoded identities</li>
<li>X.509/PKCS#8 encoded identities</li>
<li>Secure-by-default for client and server
<ul>
<li>Includes hostname verification for clients</li>
</ul>
</li>
<li>Supports asynchronous I/O for both the server and the client</li>
</ul>
<h2 id="cargo-features"><a href="#cargo-features">Cargo Features</a></h2>
<ul>
<li><code>vendored</code> - If enabled, the crate will compile and statically link to a
vendored copy of OpenSSL. This feature has no effect on Windows and
macOS, where OpenSSL is not used.</li>
</ul>
<h2 id="examples"><a href="#examples">Examples</a></h2>
<p>To connect as a client to a remote server:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>native_tls::TlsConnector;
<span class="kw">use </span>std::io::{Read, Write};
<span class="kw">use </span>std::net::TcpStream;
<span class="kw">let </span>connector = TlsConnector::new().unwrap();
<span class="kw">let </span>stream = TcpStream::connect(<span class="string">&quot;google.com:443&quot;</span>).unwrap();
<span class="kw">let </span><span class="kw-2">mut </span>stream = connector.connect(<span class="string">&quot;google.com&quot;</span>, stream).unwrap();
stream.write_all(<span class="string">b&quot;GET / HTTP/1.0\r\n\r\n&quot;</span>).unwrap();
<span class="kw">let </span><span class="kw-2">mut </span>res = <span class="macro">vec!</span>[];
stream.read_to_end(<span class="kw-2">&amp;mut </span>res).unwrap();
<span class="macro">println!</span>(<span class="string">&quot;{}&quot;</span>, String::from_utf8_lossy(<span class="kw-2">&amp;</span>res));</code></pre></div>
<p>To accept connections as a server from remote clients:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>native_tls::{Identity, TlsAcceptor, TlsStream};
<span class="kw">use </span>std::fs::File;
<span class="kw">use </span>std::io::{Read};
<span class="kw">use </span>std::net::{TcpListener, TcpStream};
<span class="kw">use </span>std::sync::Arc;
<span class="kw">use </span>std::thread;
<span class="kw">let </span><span class="kw-2">mut </span>file = File::open(<span class="string">&quot;identity.pfx&quot;</span>).unwrap();
<span class="kw">let </span><span class="kw-2">mut </span>identity = <span class="macro">vec!</span>[];
file.read_to_end(<span class="kw-2">&amp;mut </span>identity).unwrap();
<span class="kw">let </span>identity = Identity::from_pkcs12(<span class="kw-2">&amp;</span>identity, <span class="string">&quot;hunter2&quot;</span>).unwrap();
<span class="kw">let </span>listener = TcpListener::bind(<span class="string">&quot;0.0.0.0:8443&quot;</span>).unwrap();
<span class="kw">let </span>acceptor = TlsAcceptor::new(identity).unwrap();
<span class="kw">let </span>acceptor = Arc::new(acceptor);
<span class="kw">fn </span>handle_client(stream: TlsStream&lt;TcpStream&gt;) {
<span class="comment">// ...
</span>}
<span class="kw">for </span>stream <span class="kw">in </span>listener.incoming() {
<span class="kw">match </span>stream {
<span class="prelude-val">Ok</span>(stream) =&gt; {
<span class="kw">let </span>acceptor = acceptor.clone();
thread::spawn(<span class="kw">move </span>|| {
<span class="kw">let </span>stream = acceptor.accept(stream).unwrap();
handle_client(stream);
});
}
<span class="prelude-val">Err</span>(e) =&gt; { <span class="comment">/* connection failed */ </span>}
}
}</code></pre></div>
</div></details><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Certificate.html" title="native_tls::Certificate struct">Certificate</a></div><div class="item-right docblock-short">An X509 certificate.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Error.html" title="native_tls::Error struct">Error</a></div><div class="item-right docblock-short">An error returned from the TLS implementation.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Identity.html" title="native_tls::Identity struct">Identity</a></div><div class="item-right docblock-short">A cryptographic identity.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.MidHandshakeTlsStream.html" title="native_tls::MidHandshakeTlsStream struct">MidHandshakeTlsStream</a></div><div class="item-right docblock-short">A TLS stream which has been interrupted midway through the handshake process.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.TlsAcceptor.html" title="native_tls::TlsAcceptor struct">TlsAcceptor</a></div><div class="item-right docblock-short">A builder for server-side TLS connections.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.TlsAcceptorBuilder.html" title="native_tls::TlsAcceptorBuilder struct">TlsAcceptorBuilder</a></div><div class="item-right docblock-short">A builder for <code>TlsAcceptor</code>s.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.TlsConnector.html" title="native_tls::TlsConnector struct">TlsConnector</a></div><div class="item-right docblock-short">A builder for client-side TLS connections.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.TlsConnectorBuilder.html" title="native_tls::TlsConnectorBuilder struct">TlsConnectorBuilder</a></div><div class="item-right docblock-short">A builder for <code>TlsConnector</code>s.</div></div><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.TlsStream.html" title="native_tls::TlsStream struct">TlsStream</a></div><div class="item-right docblock-short">A stream managing a TLS session.</div></div></div><h2 id="enums" class="small-section-header"><a href="#enums">Enums</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.HandshakeError.html" title="native_tls::HandshakeError enum">HandshakeError</a></div><div class="item-right docblock-short">An error returned from <code>ClientBuilder::handshake</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="enum" href="enum.Protocol.html" title="native_tls::Protocol enum">Protocol</a></div><div class="item-right docblock-short">SSL/TLS protocol versions.</div></div></div><h2 id="types" class="small-section-header"><a href="#types">Type Definitions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="type" href="type.Result.html" title="native_tls::Result type">Result</a></div><div class="item-right docblock-short">A typedef of the result-type returned by many methods.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="native_tls" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>