• Stars
    star
    474
  • Rank 92,640 (Top 2 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

rust-native-tls

Documentation

An abstraction over platform-specific TLS implementations.

Specifically, this crate uses SChannel on Windows (via the schannel crate), Secure Transport on macOS (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.

More Repositories

1

rust-postgres

Native PostgreSQL driver for the Rust programming language
Rust
3,037
star
2

r2d2

A generic connection pool for Rust
Rust
1,345
star
3

rust-openssl

OpenSSL bindings for Rust
Rust
1,223
star
4

cargo-tree

Rust
531
star
5

rust-postgres-macros

Support macros for Rust-Postgres
Rust
154
star
6

serde-transcode

Rust
99
star
7

rust-socks

Rust
80
star
8

streaming-iterator

Rust
76
star
9

rust-fallible-iterator

Rust
54
star
10

rust-log-panics

Rust
54
star
11

foreign-types

Rust
50
star
12

scheduled-thread-pool

Rust
36
star
13

hyper-native-tls

Rust
36
star
14

rust-postgres-derive

Rust
35
star
15

rstack

Rust
32
star
16

tokio-io-timeout

Rust
32
star
17

hyper-openssl

Rust
25
star
18

shell-escape

Rust
20
star
19

rust-postgres-array

Rust
20
star
20

typed-headers

Rust
17
star
21

staged-builder

Rust
14
star
22

exponential-decay-histogram

Rust
13
star
23

rust-antidote

Rust
11
star
24

rust-postgres-large-object

Rust
9
star
25

jemalloc-ctl

Rust
9
star
26

rust-log-mdc

Rust
9
star
27

rust-openssl-verify

Rust
8
star
28

rust-postgres-range

Rust
7
star
29

rust-stringprep

Rust
7
star
30

stream-vbyte64

Rust
7
star
31

thread-local-object

Rust
6
star
32

log4rs-routing-appender

Rust
6
star
33

fallible-streaming-iterator

Rust
6
star
34

serde-smile

A Smile implementation for Serde
Rust
6
star
35

rust-postgres-protocol

5
star
36

syntax-ext-talk

JavaScript
5
star
37

rust-posix-ipc

Rust
5
star
38

log4rs-rolling-file

Rust
4
star
39

futures-state-stream

Rust
4
star
40

rust-hyper-socks

Rust
4
star
41

serde-humantime

Rust
4
star
42

rust-pg_query

Rust
3
star
43

hyper-timeout-connector

Rust
3
star
44

rust-time2

Rust
2
star
45

rust-docker-compose

Rust
1
star
46

serde-file-value

Rust
1
star
47

conduit-hyper

Rust
1
star
48

busted-crate

Rust
1
star
49

advent-of-code-2016

Rust
1
star
50

perf-hacks

Rust
1
star
51

advent-of-code

Rust
1
star
52

cargo-sls-distribution

Rust
1
star
53

rust-debug-builders

Rust
1
star