108 lines
14 KiB
HTML
108 lines
14 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."><meta name="keywords" content="rust, rustlang, rust-lang, native_tls"><title>native_tls - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><link rel="stylesheet" type="text/css" href="../dark.css" disabled ><link rel="stylesheet" type="text/css" href="../ayu.css" disabled ><script id="default-settings"></script><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="icon" type="image/svg+xml" href="../favicon.svg">
|
||
<link rel="alternate icon" type="image/png" href="../favicon-16x16.png">
|
||
<link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../native_tls/index.html'><div class='logo-container rust-logo'><img src='../rust-logo.png' alt='logo'></div></a><p class="location">Crate native_tls</p><div class="block version"><p>Version 0.2.7</p></div><div class="sidebar-elems"><a id="all-types" href="all.html"><p>See all native_tls's items</p></a><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li><li><a href="#types">Type Definitions</a></li></ul></div><p class="location"></p><div id="sidebar-vars" data-name="native_tls" data-ty="mod" data-relpath="../"></div></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices" role="menu"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" disabled autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" class="help-button">?</button>
|
||
<a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class="fqn"><span class="in-band">Crate <a class="mod" href="">native_tls</a></span><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span><a class="srclink" href="../src/native_tls/lib.rs.html#1-708" title="goto source code">[src]</a></span></h1><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>
|
||
<h1 id="how-is-this-implemented" class="section-header"><a href="#how-is-this-implemented">How is this implemented?</a></h1>
|
||
<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 futures 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>
|
||
<h1 id="supported-features" class="section-header"><a href="#supported-features">Supported features</a></h1>
|
||
<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>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>
|
||
<h1 id="cargo-features" class="section-header"><a href="#cargo-features">Cargo Features</a></h1>
|
||
<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>
|
||
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
|
||
<p>To connect as a client to a remote server:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">native_tls</span>::<span class="ident">TlsConnector</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::{<span class="ident">Read</span>, <span class="ident">Write</span>};
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">net</span>::<span class="ident">TcpStream</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">connector</span> <span class="op">=</span> <span class="ident">TlsConnector</span>::<span class="ident">new</span>().<span class="ident">unwrap</span>();
|
||
|
||
<span class="kw">let</span> <span class="ident">stream</span> <span class="op">=</span> <span class="ident">TcpStream</span>::<span class="ident">connect</span>(<span class="string">"google.com:443"</span>).<span class="ident">unwrap</span>();
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">stream</span> <span class="op">=</span> <span class="ident">connector</span>.<span class="ident">connect</span>(<span class="string">"google.com"</span>, <span class="ident">stream</span>).<span class="ident">unwrap</span>();
|
||
|
||
<span class="ident">stream</span>.<span class="ident">write_all</span>(<span class="string">b"GET / HTTP/1.0\r\n\r\n"</span>).<span class="ident">unwrap</span>();
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">res</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[];
|
||
<span class="ident">stream</span>.<span class="ident">read_to_end</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">res</span>).<span class="ident">unwrap</span>();
|
||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"{}"</span>, <span class="ident">String</span>::<span class="ident">from_utf8_lossy</span>(<span class="kw-2">&</span><span class="ident">res</span>));</pre></div>
|
||
<p>To accept connections as a server from remote clients:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">native_tls</span>::{<span class="ident">Identity</span>, <span class="ident">TlsAcceptor</span>, <span class="ident">TlsStream</span>};
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fs</span>::<span class="ident">File</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::{<span class="ident">Read</span>};
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">net</span>::{<span class="ident">TcpListener</span>, <span class="ident">TcpStream</span>};
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">sync</span>::<span class="ident">Arc</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
|
||
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">file</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">open</span>(<span class="string">"identity.pfx"</span>).<span class="ident">unwrap</span>();
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">identity</span> <span class="op">=</span> <span class="macro">vec</span><span class="macro">!</span>[];
|
||
<span class="ident">file</span>.<span class="ident">read_to_end</span>(<span class="kw-2">&</span><span class="kw-2">mut</span> <span class="ident">identity</span>).<span class="ident">unwrap</span>();
|
||
<span class="kw">let</span> <span class="ident">identity</span> <span class="op">=</span> <span class="ident">Identity</span>::<span class="ident">from_pkcs12</span>(<span class="kw-2">&</span><span class="ident">identity</span>, <span class="string">"hunter2"</span>).<span class="ident">unwrap</span>();
|
||
|
||
<span class="kw">let</span> <span class="ident">listener</span> <span class="op">=</span> <span class="ident">TcpListener</span>::<span class="ident">bind</span>(<span class="string">"0.0.0.0:8443"</span>).<span class="ident">unwrap</span>();
|
||
<span class="kw">let</span> <span class="ident">acceptor</span> <span class="op">=</span> <span class="ident">TlsAcceptor</span>::<span class="ident">new</span>(<span class="ident">identity</span>).<span class="ident">unwrap</span>();
|
||
<span class="kw">let</span> <span class="ident">acceptor</span> <span class="op">=</span> <span class="ident">Arc</span>::<span class="ident">new</span>(<span class="ident">acceptor</span>);
|
||
|
||
<span class="kw">fn</span> <span class="ident">handle_client</span>(<span class="ident">stream</span>: <span class="ident">TlsStream</span><span class="op"><</span><span class="ident">TcpStream</span><span class="op">></span>) {
|
||
<span class="comment">// ...</span>
|
||
}
|
||
|
||
<span class="kw">for</span> <span class="ident">stream</span> <span class="kw">in</span> <span class="ident">listener</span>.<span class="ident">incoming</span>() {
|
||
<span class="kw">match</span> <span class="ident">stream</span> {
|
||
<span class="prelude-val">Ok</span>(<span class="ident">stream</span>) <span class="op">=</span><span class="op">></span> {
|
||
<span class="kw">let</span> <span class="ident">acceptor</span> <span class="op">=</span> <span class="ident">acceptor</span>.<span class="ident">clone</span>();
|
||
<span class="ident">thread</span>::<span class="ident">spawn</span>(<span class="kw">move</span> <span class="op">|</span><span class="op">|</span> {
|
||
<span class="kw">let</span> <span class="ident">stream</span> <span class="op">=</span> <span class="ident">acceptor</span>.<span class="ident">accept</span>(<span class="ident">stream</span>).<span class="ident">unwrap</span>();
|
||
<span class="ident">handle_client</span>(<span class="ident">stream</span>);
|
||
});
|
||
}
|
||
<span class="prelude-val">Err</span>(<span class="ident">e</span>) <span class="op">=</span><span class="op">></span> { <span class="comment">/* connection failed */</span> }
|
||
}
|
||
}</pre></div>
|
||
</div><h2 id="structs" class="section-header"><a href="#structs">Structs</a></h2>
|
||
<table><tr class="module-item"><td><a class="struct" href="struct.Certificate.html" title="native_tls::Certificate struct">Certificate</a></td><td class="docblock-short"><p>An X509 certificate.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Error.html" title="native_tls::Error struct">Error</a></td><td class="docblock-short"><p>An error returned from the TLS implementation.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.Identity.html" title="native_tls::Identity struct">Identity</a></td><td class="docblock-short"><p>A cryptographic identity.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.MidHandshakeTlsStream.html" title="native_tls::MidHandshakeTlsStream struct">MidHandshakeTlsStream</a></td><td class="docblock-short"><p>A TLS stream which has been interrupted midway through the handshake process.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.TlsAcceptor.html" title="native_tls::TlsAcceptor struct">TlsAcceptor</a></td><td class="docblock-short"><p>A builder for server-side TLS connections.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.TlsAcceptorBuilder.html" title="native_tls::TlsAcceptorBuilder struct">TlsAcceptorBuilder</a></td><td class="docblock-short"><p>A builder for <code>TlsAcceptor</code>s.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.TlsConnector.html" title="native_tls::TlsConnector struct">TlsConnector</a></td><td class="docblock-short"><p>A builder for client-side TLS connections.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.TlsConnectorBuilder.html" title="native_tls::TlsConnectorBuilder struct">TlsConnectorBuilder</a></td><td class="docblock-short"><p>A builder for <code>TlsConnector</code>s.</p>
|
||
</td></tr><tr class="module-item"><td><a class="struct" href="struct.TlsStream.html" title="native_tls::TlsStream struct">TlsStream</a></td><td class="docblock-short"><p>A stream managing a TLS session.</p>
|
||
</td></tr></table><h2 id="enums" class="section-header"><a href="#enums">Enums</a></h2>
|
||
<table><tr class="module-item"><td><a class="enum" href="enum.HandshakeError.html" title="native_tls::HandshakeError enum">HandshakeError</a></td><td class="docblock-short"><p>An error returned from <code>ClientBuilder::handshake</code>.</p>
|
||
</td></tr><tr class="module-item"><td><a class="enum" href="enum.Protocol.html" title="native_tls::Protocol enum">Protocol</a></td><td class="docblock-short"><p>SSL/TLS protocol versions.</p>
|
||
</td></tr></table><h2 id="types" class="section-header"><a href="#types">Type Definitions</a></h2>
|
||
<table><tr class="module-item"><td><a class="type" href="type.Result.html" title="native_tls::Result type">Result</a></td><td class="docblock-short"><p>A typedef of the result-type returned by many methods.</p>
|
||
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><div id="rustdoc-vars" data-root-path="../" data-current-crate="native_tls"></div>
|
||
<script src="../main.js"></script><script defer src="../search-index.js"></script></body></html> |