#tls #ssl #native #windows #osx #openssl #schannel

native-tls

A wrapper over a platform’s native TLS implementation

10 releases

0.2.3 Apr 27, 2019
0.2.2 Oct 22, 2018
0.2.1 Aug 5, 2018
0.2.0 Jun 26, 2018
0.1.0 Nov 9, 2016
Download history 47112/week @ 2019-08-16 48050/week @ 2019-08-23 45513/week @ 2019-08-30 48260/week @ 2019-09-06 49229/week @ 2019-09-13 48215/week @ 2019-09-20 54017/week @ 2019-09-27 63627/week @ 2019-10-04 57173/week @ 2019-10-11 60276/week @ 2019-10-18 60278/week @ 2019-10-25 60835/week @ 2019-11-01 60010/week @ 2019-11-08 63466/week @ 2019-11-15 63834/week @ 2019-11-22

164,748 downloads per month
Used in 1,395 crates (176 directly)

MIT/Apache

78KB
1.5K SLoC

rust-native-tls

CircleCI Build Status

Documentation

An abstraction over platform-specific TLS implementations.

Specifically, this crate uses SChannel on Windows (via the schannel crate), Secure Transport on OSX (via the security-framework crate), and OpenSSL (via the openssl crate) on all other platforms.

Installation

# Cargo.toml
[dependencies]
native-tls = "0.2"

Usage

An example client looks like:

extern crate native_tls;

use native_tls::TlsConnector;
use std::io::{Read, Write};
use std::net::TcpStream;

fn main() {
    let connector = TlsConnector::new().unwrap();

    let stream = TcpStream::connect("google.com:443").unwrap();
    let mut stream = connector.connect("google.com", stream).unwrap();

    stream.write_all(b"GET / HTTP/1.0\r\n\r\n").unwrap();
    let mut res = vec![];
    stream.read_to_end(&mut res).unwrap();
    println!("{}", String::from_utf8_lossy(&res));
}

To accept connections as a server from remote clients:

extern crate native_tls;

use native_tls::{Identity, TlsAcceptor, TlsStream};
use std::fs::File;
use std::io::{Read};
use std::net::{TcpListener, TcpStream};
use std::sync::Arc;
use std::thread;

fn main() {
    let mut file = File::open("identity.pfx").unwrap();
    let mut identity = vec![];
    file.read_to_end(&mut identity).unwrap();
    let identity = Identity::from_pkcs12(&identity, "hunter2").unwrap();

    let acceptor = TlsAcceptor::new(identity).unwrap();
    let acceptor = Arc::new(acceptor);

    let listener = TcpListener::bind("0.0.0.0:8443").unwrap();

    fn handle_client(stream: TlsStream<TcpStream>) {
        // ...
    }

    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                let acceptor = acceptor.clone();
                thread::spawn(move || {
                    let stream = acceptor.accept(stream).unwrap();
                    handle_client(stream);
                });
            }
            Err(e) => { /* connection failed */ }
        }
    }
}

License

rust-native-tls is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0), with portions covered by various BSD-like licenses.

See LICENSE-APACHE, and LICENSE-MIT for details.

Dependencies

~0–760KB
~17K SLoC