#native-tls #platform #gmssl #tls-connector #gm-ssl

native-tls-gmssl

封装了平台的本地TLS实现(GmSSL)

1 个不稳定版本

使用旧的Rust 2015

0.1.0 2023年4月16日

#7 in #tls-connector


用于tokio-tls-gmssl

MIT/Apache

100KB
2K SLoC

native-tls-gmssl

文档

对特定平台TLS实现的抽象。

具体来说,该crate在Windows上使用SChannel(通过 schannel crate),在macOS上使用Secure Transport(通过 security-framework crate),在其他所有平台上使用OpenSSL(通过 gmssl crate)。

安装

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

用法

一个示例客户端看起来像

extern crate native_tls_gmssl;

use native_tls_gmssl::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));
}

作为服务器接受远程客户端的连接

extern crate native_tls_gmssl;

use native_tls_gmssl::{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 */ }
        }
    }
}

许可证

native-tls-gmssl主要在MIT许可证和Apache许可证(版本2.0)的条款下分发,部分内容受各种类似BSD的许可证覆盖。

请参阅LICENSE-APACHE和LICENSE-MIT以获取详细信息。

依赖项

~0–9.5MB
~92K SLoC