Files
google-apis-rs/security_framework/secure_transport/index.html
2021-04-02 00:20:57 +08:00

83 lines
15 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="API documentation for the Rust `secure_transport` mod in crate `security_framework`."><meta name="keywords" content="rust, rustlang, rust-lang, secure_transport"><title>security_framework::secure_transport - 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">&#9776;</div><a href='../../security_framework/index.html'><div class='logo-container rust-logo'><img src='../../rust-logo.png' alt='logo'></div></a><p class="location">Module secure_transport</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#enums">Enums</a></li></ul></div><p class="location"><a href="../index.html">security_framework</a></p><div id="sidebar-vars" data-name="secure_transport" data-ty="mod" data-relpath="../"></div><script defer src="../sidebar-items.js"></script></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">Module <a href="../index.html">security_framework</a>::<wbr><a class="mod" href="">secure_transport</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">&#x2212;</span>]</a></span><a class="srclink" href="../../src/security_framework/secure_transport.rs.html#1-1837" title="goto source code">[src]</a></span></h1><div class="docblock"><p>SSL/TLS encryption support using Secure Transport.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>To connect as a client to a server with a certificate trusted by the system:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">net</span>::<span class="ident">TcpStream</span>;
<span class="kw">use</span> <span class="ident">security_framework</span>::<span class="ident">secure_transport</span>::<span class="ident">ClientBuilder</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">&quot;google.com:443&quot;</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">ClientBuilder</span>::<span class="ident">new</span>().<span class="ident">handshake</span>(<span class="string">&quot;google.com&quot;</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&quot;GET / HTTP/1.0\r\n\r\n&quot;</span>).<span class="ident">unwrap</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">page</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">&amp;</span><span class="kw-2">mut</span> <span class="ident">page</span>).<span class="ident">unwrap</span>();
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">String</span>::<span class="ident">from_utf8_lossy</span>(<span class="kw-2">&amp;</span><span class="ident">page</span>));</pre></div>
<p>To connect to a server with a certificate that's <em>not</em> trusted by the
system, specify the root certificates for the server's chain to the
<code>ClientBuilder</code>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::<span class="ident">prelude</span>::<span class="kw-2">*</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">net</span>::<span class="ident">TcpStream</span>;
<span class="kw">use</span> <span class="ident">security_framework</span>::<span class="ident">secure_transport</span>::<span class="ident">ClientBuilder</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">&quot;my_server.com:443&quot;</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">ClientBuilder</span>::<span class="ident">new</span>()
.<span class="ident">anchor_certificates</span>(<span class="kw-2">&amp;</span>[<span class="ident">root_cert</span>])
.<span class="ident">handshake</span>(<span class="string">&quot;my_server.com&quot;</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&quot;GET / HTTP/1.0\r\n\r\n&quot;</span>).<span class="ident">unwrap</span>();
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">page</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">&amp;</span><span class="kw-2">mut</span> <span class="ident">page</span>).<span class="ident">unwrap</span>();
<span class="macro">println</span><span class="macro">!</span>(<span class="string">&quot;{}&quot;</span>, <span class="ident">String</span>::<span class="ident">from_utf8_lossy</span>(<span class="kw-2">&amp;</span><span class="ident">page</span>));</pre></div>
<p>For more advanced configuration, the <code>SslContext</code> type can be used directly.</p>
<p>To run a server:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">net</span>::<span class="ident">TcpListener</span>;
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">thread</span>;
<span class="kw">use</span> <span class="ident">security_framework</span>::<span class="ident">secure_transport</span>::{<span class="ident">SslContext</span>, <span class="ident">SslProtocolSide</span>, <span class="ident">SslConnectionType</span>};
<span class="comment">// Create a TCP listener and start accepting on it.</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">listener</span> <span class="op">=</span> <span class="ident">TcpListener</span>::<span class="ident">bind</span>(<span class="string">&quot;0.0.0.0:443&quot;</span>).<span class="ident">unwrap</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">let</span> <span class="ident">stream</span> <span class="op">=</span> <span class="ident">stream</span>.<span class="ident">unwrap</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="comment">// Create a new context configured to operate on the server side of</span>
<span class="comment">// a traditional SSL/TLS session.</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">ctx</span> <span class="op">=</span> <span class="ident">SslContext</span>::<span class="ident">new</span>(<span class="ident">SslProtocolSide</span>::<span class="ident">SERVER</span>, <span class="ident">SslConnectionType</span>::<span class="ident">STREAM</span>)
.<span class="ident">unwrap</span>();
<span class="comment">// Install the certificate chain that we will be using.</span>
<span class="ident">ctx</span>.<span class="ident">set_certificate</span>(<span class="ident">identity</span>, <span class="kw-2">&amp;</span>[<span class="ident">intermediate_cert</span>, <span class="ident">root_cert</span>]).<span class="ident">unwrap</span>();
<span class="comment">// Perform the SSL/TLS handshake and get our stream.</span>
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">stream</span> <span class="op">=</span> <span class="ident">ctx</span>.<span class="ident">handshake</span>(<span class="ident">stream</span>).<span class="ident">unwrap</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.ClientBuilder.html" title="security_framework::secure_transport::ClientBuilder struct">ClientBuilder</a></td><td class="docblock-short"><p>A builder type to simplify the creation of client side <code>SslStream</code>s.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.MidHandshakeClientBuilder.html" title="security_framework::secure_transport::MidHandshakeClientBuilder struct">MidHandshakeClientBuilder</a></td><td class="docblock-short"><p>An SSL stream midway through the handshake process.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.MidHandshakeSslStream.html" title="security_framework::secure_transport::MidHandshakeSslStream struct">MidHandshakeSslStream</a></td><td class="docblock-short"><p>An SSL stream midway through the handshake process.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.ServerBuilder.html" title="security_framework::secure_transport::ServerBuilder struct">ServerBuilder</a></td><td class="docblock-short"><p>A builder type to simplify the creation of server-side <code>SslStream</code>s.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.SessionState.html" title="security_framework::secure_transport::SessionState struct">SessionState</a></td><td class="docblock-short"><p>Specifies the state of a TLS session.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.SslAuthenticate.html" title="security_framework::secure_transport::SslAuthenticate struct">SslAuthenticate</a></td><td class="docblock-short"><p>Specifies a server's requirement for client certificates.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.SslClientCertificateState.html" title="security_framework::secure_transport::SslClientCertificateState struct">SslClientCertificateState</a></td><td class="docblock-short"><p>Specifies the state of client certificate processing.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.SslConnectionType.html" title="security_framework::secure_transport::SslConnectionType struct">SslConnectionType</a></td><td class="docblock-short"><p>Specifies the type of TLS session.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.SslContext.html" title="security_framework::secure_transport::SslContext struct">SslContext</a></td><td class="docblock-short"><p>A Secure Transport SSL/TLS context object.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.SslProtocol.html" title="security_framework::secure_transport::SslProtocol struct">SslProtocol</a></td><td class="docblock-short"><p>Specifies protocol versions.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.SslProtocolSide.html" title="security_framework::secure_transport::SslProtocolSide struct">SslProtocolSide</a></td><td class="docblock-short"><p>Specifies a side of a TLS session.</p>
</td></tr><tr class="module-item"><td><a class="struct" href="struct.SslStream.html" title="security_framework::secure_transport::SslStream struct">SslStream</a></td><td class="docblock-short"><p>A type implementing SSL/TLS encryption over an underlying stream.</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.ClientHandshakeError.html" title="security_framework::secure_transport::ClientHandshakeError enum">ClientHandshakeError</a></td><td class="docblock-short"><p>An error or intermediate state after a TLS handshake attempt.</p>
</td></tr><tr class="module-item"><td><a class="enum" href="enum.HandshakeError.html" title="security_framework::secure_transport::HandshakeError enum">HandshakeError</a></td><td class="docblock-short"><p>An error or intermediate state after a TLS handshake attempt.</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="security_framework"></div>
<script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>