Skip to content

Commit c400390

Browse files
committed
added example and updated docs
1 parent eb269b8 commit c400390

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

examples/simple-server-pkcs8.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
extern crate native_tls;
2+
3+
use native_tls::{Identity, TlsAcceptor, TlsStream};
4+
use std::fs::File;
5+
use std::io::{Read, Write};
6+
use std::net::{TcpListener, TcpStream};
7+
use std::sync::Arc;
8+
use std::thread;
9+
10+
fn main() {
11+
let mut cert_file = File::open("test/cert.pem").unwrap();
12+
let mut certs = vec![];
13+
cert_file.read_to_end(&mut certs).unwrap();
14+
let mut key_file = File::open("test/key.pem").unwrap();
15+
let mut key = vec![];
16+
key_file.read_to_end(&mut key).unwrap();
17+
let pkcs8 = Identity::from_pkcs8(&certs, &key).unwrap();
18+
19+
let acceptor = TlsAcceptor::new(pkcs8).unwrap();
20+
let acceptor = Arc::new(acceptor);
21+
22+
let listener = TcpListener::bind("0.0.0.0:8443").unwrap();
23+
24+
fn handle_client(mut stream: TlsStream<TcpStream>) {
25+
let mut buf = [0; 1024];
26+
let read = stream.read(&mut buf).unwrap();
27+
let received = std::str::from_utf8(&buf[0..read]).unwrap();
28+
stream.write_all(format!("received '{}'", received).as_bytes()).unwrap();
29+
}
30+
31+
for stream in listener.incoming() {
32+
match stream {
33+
Ok(stream) => {
34+
let acceptor = acceptor.clone();
35+
thread::spawn(move || {
36+
let stream = acceptor.accept(stream).unwrap();
37+
handle_client(stream);
38+
});
39+
}
40+
Err(_e) => { /* connection failed */ }
41+
}
42+
}
43+
}

src/lib.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
//! * TLS/SSL client communication
2828
//! * TLS/SSL server communication
2929
//! * PKCS#12 encoded identities
30+
//! * PKCS#8 encoded identities
3031
//! * Secure-by-default for client and server
3132
//! * Includes hostname verification for clients
3233
//! * Supports asynchronous I/O for both the server and the client
@@ -186,10 +187,15 @@ impl Identity {
186187
Ok(Identity(identity))
187188
}
188189

189-
/// buf is the contents of a file containing a chain of PEM encoded certificates
190-
/// key is the contents of a file containing a PEM encoded private key
191-
pub fn from_pkcs8(buf: &[u8], key: &[u8]) -> Result<Identity> {
192-
let identity = imp::Identity::from_pkcs8(buf, key)?;
190+
/// Parses a chain of PEM encoded X509 certificates, with the leaf certificate first.
191+
/// `key` is a PEM encoded PKCS #8 formatted private key for the leaf certificate.
192+
///
193+
/// The certificate chain should contain any intermediate cerficates that should be sent to
194+
/// clients to allow them to build a chain to a trusted root.
195+
///
196+
/// A certificate chain here means a series of PEM encoded certificates concatenated together.
197+
pub fn from_pkcs8(pem: &[u8], key: &[u8]) -> Result<Identity> {
198+
let identity = imp::Identity::from_pkcs8(pem, key)?;
193199
Ok(Identity(identity))
194200
}
195201
}

0 commit comments

Comments
 (0)