mpvrc/native_tls/index.html

88 lines
No EOL
11 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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-Regular-0fe48ade.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" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-42caa33d.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.84.1 (e71f9a9a9 2025-01-27)" data-channel="1.84.1" data-search-js="search-92e6798f.js" data-settings-js="settings-0f613d39.js" ><script src="../static.files/storage-59e33391.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-893ab5e7.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-6580c154.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]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../native_tls/index.html">native_<wbr>tls</a><span class="version">0.2.12</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"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>native_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-719">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 isnt 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>
</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">&amp;mut </span>res).unwrap();
<span class="macro">println!</span>(<span class="string">"{}"</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">"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">&amp;mut </span>identity).unwrap();
<span class="kw">let </span>identity = Identity::from_pkcs12(<span class="kw-2">&amp;</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&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="section-header">Structs<a href="#structs" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.Certificate.html" title="struct native_tls::Certificate">Certificate</a></div><div class="desc docblock-short">An X509 certificate.</div></li><li><div class="item-name"><a class="struct" href="struct.Error.html" title="struct native_tls::Error">Error</a></div><div class="desc docblock-short">An error returned from the TLS implementation.</div></li><li><div class="item-name"><a class="struct" href="struct.Identity.html" title="struct native_tls::Identity">Identity</a></div><div class="desc docblock-short">A cryptographic identity.</div></li><li><div class="item-name"><a class="struct" href="struct.MidHandshakeTlsStream.html" title="struct native_tls::MidHandshakeTlsStream">MidHandshake<wbr>TlsStream</a></div><div class="desc docblock-short">A TLS stream which has been interrupted midway through the handshake process.</div></li><li><div class="item-name"><a class="struct" href="struct.TlsAcceptor.html" title="struct native_tls::TlsAcceptor">TlsAcceptor</a></div><div class="desc docblock-short">A builder for server-side TLS connections.</div></li><li><div class="item-name"><a class="struct" href="struct.TlsAcceptorBuilder.html" title="struct native_tls::TlsAcceptorBuilder">TlsAcceptor<wbr>Builder</a></div><div class="desc docblock-short">A builder for <code>TlsAcceptor</code>s.</div></li><li><div class="item-name"><a class="struct" href="struct.TlsConnector.html" title="struct native_tls::TlsConnector">TlsConnector</a></div><div class="desc docblock-short">A builder for client-side TLS connections.</div></li><li><div class="item-name"><a class="struct" href="struct.TlsConnectorBuilder.html" title="struct native_tls::TlsConnectorBuilder">TlsConnector<wbr>Builder</a></div><div class="desc docblock-short">A builder for <code>TlsConnector</code>s.</div></li><li><div class="item-name"><a class="struct" href="struct.TlsStream.html" title="struct native_tls::TlsStream">TlsStream</a></div><div class="desc docblock-short">A stream managing a TLS session.</div></li></ul><h2 id="enums" class="section-header">Enums<a href="#enums" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="enum" href="enum.HandshakeError.html" title="enum native_tls::HandshakeError">Handshake<wbr>Error</a></div><div class="desc docblock-short">An error returned from <code>ClientBuilder::handshake</code>.</div></li><li><div class="item-name"><a class="enum" href="enum.Protocol.html" title="enum native_tls::Protocol">Protocol</a></div><div class="desc docblock-short">SSL/TLS protocol versions.</div></li></ul><h2 id="types" class="section-header">Type Aliases<a href="#types" class="anchor">§</a></h2><ul class="item-table"><li><div class="item-name"><a class="type" href="type.Result.html" title="type native_tls::Result">Result</a></div><div class="desc docblock-short">A typedef of the result-type returned by many methods.</div></li></ul></section></div></main></body></html>