Merge pull request #905 from sfackler/cleanup

Misc cleanup
This commit is contained in:
Steven Fackler 2018-04-27 15:55:39 -07:00 committed by GitHub
commit 6f59406067
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 102 additions and 84 deletions

View File

@ -29,9 +29,8 @@ const DEFINES: &'static [&'static str] = &[
]; ];
enum Version { enum Version {
Openssl110, Openssl11x,
Openssl102, Openssl10x,
Openssl101,
Libressl, Libressl,
} }
@ -89,10 +88,8 @@ fn main() {
let libs = match libs_env.as_ref().and_then(|s| s.to_str()) { let libs = match libs_env.as_ref().and_then(|s| s.to_str()) {
Some(ref v) => v.split(":").collect(), Some(ref v) => v.split(":").collect(),
None => match version { None => match version {
Version::Openssl101 | Version::Openssl102 if target.contains("windows") => { Version::Openssl10x if target.contains("windows") => vec!["ssleay32", "libeay32"],
vec!["ssleay32", "libeay32"] Version::Openssl11x if target.contains("windows") => vec!["libssl", "libcrypto"],
}
Version::Openssl110 if target.contains("windows") => vec!["libssl", "libcrypto"],
_ => vec!["ssl", "crypto"], _ => vec!["ssl", "crypto"],
}, },
}; };
@ -446,25 +443,25 @@ See rust-openssl README for more information:
println!("cargo:rustc-cfg=ossl111"); println!("cargo:rustc-cfg=ossl111");
println!("cargo:rustc-cfg=ossl110"); println!("cargo:rustc-cfg=ossl110");
println!("cargo:version=111"); println!("cargo:version=111");
Version::Openssl110 Version::Openssl11x
} else if openssl_version >= 0x1_01_00_06_0 { } else if openssl_version >= 0x1_01_00_06_0 {
println!("cargo:rustc-cfg=ossl110"); println!("cargo:rustc-cfg=ossl110");
println!("cargo:rustc-cfg=ossl110f"); println!("cargo:rustc-cfg=ossl110f");
println!("cargo:version=110"); println!("cargo:version=110");
println!("cargo:patch=f"); println!("cargo:patch=f");
Version::Openssl110 Version::Openssl11x
} else if openssl_version >= 0x1_01_00_00_0 { } else if openssl_version >= 0x1_01_00_00_0 {
println!("cargo:rustc-cfg=ossl110"); println!("cargo:rustc-cfg=ossl110");
println!("cargo:version=110"); println!("cargo:version=110");
Version::Openssl110 Version::Openssl11x
} else if openssl_version >= 0x1_00_02_00_0 { } else if openssl_version >= 0x1_00_02_00_0 {
println!("cargo:rustc-cfg=ossl102"); println!("cargo:rustc-cfg=ossl102");
println!("cargo:version=102"); println!("cargo:version=102");
Version::Openssl102 Version::Openssl10x
} else if openssl_version >= 0x1_00_01_00_0 { } else if openssl_version >= 0x1_00_01_00_0 {
println!("cargo:rustc-cfg=ossl101"); println!("cargo:rustc-cfg=ossl101");
println!("cargo:version=101"); println!("cargo:version=101");
Version::Openssl101 Version::Openssl10x
} else { } else {
version_error() version_error()
} }

View File

@ -17,16 +17,16 @@ use tempdir::TempDir;
use dh::Dh; use dh::Dh;
use hash::MessageDigest; use hash::MessageDigest;
use ocsp::{OcspResponse, OcspResponseStatus}; use ocsp::{OcspResponse, OcspResponseStatus};
use pkey::PKey;
use ssl; use ssl;
use ssl::{Error, HandshakeError, ShutdownResult, Ssl, SslAcceptor, SslConnector, SslContext,
SslFiletype, SslMethod, SslSessionCacheMode, SslStream, MidHandshakeSslStream,
SslVerifyMode, StatusType};
#[cfg(any(ossl110, ossl111))] #[cfg(any(ossl110, ossl111))]
use ssl::SslVersion; use ssl::SslVersion;
use x509::{X509, X509Name, X509StoreContext, X509VerifyResult}; use ssl::{Error, HandshakeError, MidHandshakeSslStream, ShutdownResult, Ssl, SslAcceptor,
SslConnector, SslContext, SslFiletype, SslMethod, SslSessionCacheMode, SslStream,
SslVerifyMode, StatusType};
#[cfg(any(ossl102, ossl110))] #[cfg(any(ossl102, ossl110))]
use x509::verify::X509CheckFlags; use x509::verify::X509CheckFlags;
use pkey::PKey; use x509::{X509, X509Name, X509StoreContext, X509VerifyResult};
use std::net::UdpSocket; use std::net::UdpSocket;
@ -1391,74 +1391,89 @@ fn _check_kinds() {
is_sync::<SslStream<TcpStream>>(); is_sync::<SslStream<TcpStream>>();
} }
#[derive(Debug)]
struct MemoryStream {
incoming: io::Cursor<Vec<u8>>,
outgoing: Vec<u8>,
}
impl MemoryStream {
pub fn new() -> Self { Self {
incoming: io::Cursor::new(Vec::new()),
outgoing: Vec::new(),
}}
pub fn extend_incoming(&mut self, data: &[u8]) {
self.incoming.get_mut().extend_from_slice(data);
}
pub fn take_outgoing(&mut self) -> Outgoing { Outgoing(&mut self.outgoing) }
}
impl Read for MemoryStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let n = self.incoming.read(buf)?;
if self.incoming.position() == self.incoming.get_ref().len() as u64 {
self.incoming.set_position(0);
self.incoming.get_mut().clear();
}
if n == 0 {
return Err(io::Error::new(io::ErrorKind::WouldBlock, "no data available"));
}
Ok(n)
}
}
impl Write for MemoryStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.outgoing.write(buf)
}
fn flush(&mut self) -> io::Result<()> { Ok(()) }
}
pub struct Outgoing<'a>(&'a mut Vec<u8>);
impl<'a> Drop for Outgoing<'a> {
fn drop(&mut self) {
self.0.clear();
}
}
impl<'a> ::std::ops::Deref for Outgoing<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] { &self.0 }
}
impl<'a> AsRef<[u8]> for Outgoing<'a> {
fn as_ref(&self) -> &[u8] { &self.0 }
}
fn send(from: &mut MemoryStream, to: &mut MemoryStream) {
to.extend_incoming(&from.take_outgoing());
}
#[test] #[test]
#[cfg(ossl111)] #[cfg(ossl111)]
fn stateless() { fn stateless() {
use super::SslOptions; use super::SslOptions;
fn hs<S: ::std::fmt::Debug>(stream: Result<SslStream<S>, HandshakeError<S>>) -> Result<SslStream<S>, MidHandshakeSslStream<S>> { #[derive(Debug)]
struct MemoryStream {
incoming: io::Cursor<Vec<u8>>,
outgoing: Vec<u8>,
}
impl MemoryStream {
pub fn new() -> Self {
Self {
incoming: io::Cursor::new(Vec::new()),
outgoing: Vec::new(),
}
}
pub fn extend_incoming(&mut self, data: &[u8]) {
self.incoming.get_mut().extend_from_slice(data);
}
pub fn take_outgoing(&mut self) -> Outgoing {
Outgoing(&mut self.outgoing)
}
}
impl Read for MemoryStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let n = self.incoming.read(buf)?;
if self.incoming.position() == self.incoming.get_ref().len() as u64 {
self.incoming.set_position(0);
self.incoming.get_mut().clear();
}
if n == 0 {
return Err(io::Error::new(
io::ErrorKind::WouldBlock,
"no data available",
));
}
Ok(n)
}
}
impl Write for MemoryStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.outgoing.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
pub struct Outgoing<'a>(&'a mut Vec<u8>);
impl<'a> Drop for Outgoing<'a> {
fn drop(&mut self) {
self.0.clear();
}
}
impl<'a> ::std::ops::Deref for Outgoing<'a> {
type Target = [u8];
fn deref(&self) -> &[u8] {
&self.0
}
}
impl<'a> AsRef<[u8]> for Outgoing<'a> {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
fn send(from: &mut MemoryStream, to: &mut MemoryStream) {
to.extend_incoming(&from.take_outgoing());
}
fn hs<S: ::std::fmt::Debug>(
stream: Result<SslStream<S>, HandshakeError<S>>,
) -> Result<SslStream<S>, MidHandshakeSslStream<S>> {
match stream { match stream {
Ok(stream) => Ok(stream), Ok(stream) => Ok(stream),
Err(HandshakeError::WouldBlock(stream)) => Err(stream), Err(HandshakeError::WouldBlock(stream)) => Err(stream),
@ -1475,14 +1490,20 @@ fn stateless() {
let client_stream = Ssl::new(&client_ctx.build()).unwrap(); let client_stream = Ssl::new(&client_ctx.build()).unwrap();
let mut server_ctx = SslContext::builder(SslMethod::tls()).unwrap(); let mut server_ctx = SslContext::builder(SslMethod::tls()).unwrap();
server_ctx.set_certificate_file(&Path::new("test/cert.pem"), SslFiletype::PEM) server_ctx
.set_certificate_file(&Path::new("test/cert.pem"), SslFiletype::PEM)
.unwrap(); .unwrap();
server_ctx.set_private_key_file(&Path::new("test/key.pem"), SslFiletype::PEM) server_ctx
.set_private_key_file(&Path::new("test/key.pem"), SslFiletype::PEM)
.unwrap(); .unwrap();
const COOKIE: &[u8] = b"chocolate chip"; const COOKIE: &[u8] = b"chocolate chip";
server_ctx.set_stateless_cookie_generate_cb(|_tls, buf| { buf[0..COOKIE.len()].copy_from_slice(COOKIE); Ok(COOKIE.len()) }); server_ctx.set_stateless_cookie_generate_cb(|_tls, buf| {
buf[0..COOKIE.len()].copy_from_slice(COOKIE);
Ok(COOKIE.len())
});
server_ctx.set_stateless_cookie_verify_cb(|_tls, buf| buf == COOKIE); server_ctx.set_stateless_cookie_verify_cb(|_tls, buf| buf == COOKIE);
let mut server_stream = ssl::SslStreamBuilder::new(Ssl::new(&server_ctx.build()).unwrap(), MemoryStream::new()); let mut server_stream =
ssl::SslStreamBuilder::new(Ssl::new(&server_ctx.build()).unwrap(), MemoryStream::new());
// //
// Handshake // Handshake