88 lines
11 KiB
HTML
88 lines
11 KiB
HTML
<!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."><title>native_tls - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="native_tls" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.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]--><rustdoc-topbar><h2><a href="#">Crate native_tls</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../native_tls/index.html">native_<wbr>tls</a><span class="version">0.2.18</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#how-is-this-implemented" title="How is this implemented?">How is this implemented?</a></li><li><a href="#supported-features" title="Supported features">Supported features</a></li><li><a href="#cargo-features" title="Cargo Features">Cargo Features</a></li><li><a href="#examples" title="Examples">Examples</a></li></ul><h3><a href="#structs">Crate Items</a></h3><ul class="block"><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#enums" title="Enums">Enums</a></li><li><a href="#types" title="Type Aliases">Type Aliases</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>native_<wbr>tls</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/native_tls/lib.rs.html#1-747">Source</a> </span></div><details class="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 class="doc-anchor" href="#how-is-this-implemented">§</a>How is this implemented?</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 class="doc-anchor" href="#supported-features">§</a>Supported features</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 class="doc-anchor" href="#cargo-features">§</a>Cargo Features</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>
|
||
<li><code>alpn</code> - enables <code>request_alpns()</code> for negotiating HTTP/2, etc.</li>
|
||
<li><code>alpn-accept</code> - enables ALPN for <code>TlsAcceptor</code>.</li>
|
||
</ul>
|
||
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</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">"google.com:443"</span>).unwrap();
|
||
<span class="kw">let </span><span class="kw-2">mut </span>stream = connector.connect(<span class="string">"google.com"</span>, stream).unwrap();
|
||
|
||
stream.write_all(<span class="string">b"GET / HTTP/1.0\r\n\r\n"</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">&mut </span>res).unwrap();
|
||
<span class="macro">println!</span>(<span class="string">"{}"</span>, String::from_utf8_lossy(<span class="kw-2">&</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">"identity.pfx"</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">&mut </span>identity).unwrap();
|
||
<span class="kw">let </span>identity = Identity::from_pkcs12(<span class="kw-2">&</span>identity, <span class="string">"hunter2"</span>).unwrap();
|
||
|
||
<span class="kw">let </span>listener = TcpListener::bind(<span class="string">"0.0.0.0:8443"</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<TcpStream>) {
|
||
<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) => {
|
||
<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) => { <span class="comment">/* connection failed */ </span>},
|
||
}
|
||
}</code></pre></div></div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.Certificate.html" title="struct native_tls::Certificate">Certificate</a></dt><dd>An X509 certificate.</dd><dt><a class="struct" href="struct.Error.html" title="struct native_tls::Error">Error</a></dt><dd>An error returned from the TLS implementation.</dd><dt><a class="struct" href="struct.Identity.html" title="struct native_tls::Identity">Identity</a></dt><dd>A cryptographic identity.</dd><dt><a class="struct" href="struct.MidHandshakeTlsStream.html" title="struct native_tls::MidHandshakeTlsStream">MidHandshake<wbr>TlsStream</a></dt><dd>A TLS stream which has been interrupted midway through the handshake process.</dd><dt><a class="struct" href="struct.TlsAcceptor.html" title="struct native_tls::TlsAcceptor">TlsAcceptor</a></dt><dd>A builder for server-side TLS connections.</dd><dt><a class="struct" href="struct.TlsAcceptorBuilder.html" title="struct native_tls::TlsAcceptorBuilder">TlsAcceptor<wbr>Builder</a></dt><dd>A builder for <code>TlsAcceptor</code>s.</dd><dt><a class="struct" href="struct.TlsConnector.html" title="struct native_tls::TlsConnector">TlsConnector</a></dt><dd>A builder for client-side TLS connections.</dd><dt><a class="struct" href="struct.TlsConnectorBuilder.html" title="struct native_tls::TlsConnectorBuilder">TlsConnector<wbr>Builder</a></dt><dd>A builder for <code>TlsConnector</code>s.</dd><dt><a class="struct" href="struct.TlsStream.html" title="struct native_tls::TlsStream">TlsStream</a></dt><dd>A stream managing a TLS session.</dd></dl><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><dl class="item-table"><dt><a class="enum" href="enum.HandshakeError.html" title="enum native_tls::HandshakeError">Handshake<wbr>Error</a></dt><dd>An error returned from <code>ClientBuilder::handshake</code>.</dd><dt><a class="enum" href="enum.Protocol.html" title="enum native_tls::Protocol">Protocol</a></dt><dd>SSL/TLS protocol versions.</dd></dl><h2 id="types" class="section-header">Type Aliases<a href="#types" class="anchor">§</a></h2><dl class="item-table"><dt><a class="type" href="type.Result.html" title="type native_tls::Result">Result</a></dt><dd>A typedef of the result-type returned by many methods.</dd></dl></section></div></main></body></html> |