1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
extern crate tls_api; extern crate openssl; use std::io; use std::result; use std::fmt; pub struct Pkcs12(openssl::pkcs12::ParsedPkcs12); pub struct Certificate(openssl::x509::X509); pub struct TlsConnectorBuilder(openssl::ssl::SslConnectorBuilder); pub struct TlsConnector(openssl::ssl::SslConnector); pub struct TlsAcceptorBuilder(openssl::ssl::SslAcceptorBuilder); pub struct TlsAcceptor(openssl::ssl::SslAcceptor); use tls_api::Error; use tls_api::Result; fn map_error_stack(e: openssl::error::ErrorStack) -> Error { Error::new(e) } impl tls_api::Pkcs12 for Pkcs12 { fn from_der(der: &[u8], password: &str) -> Result<Self> { let pkcs12 = openssl::pkcs12::Pkcs12::from_der(der).map_err(map_error_stack)?; let parsed = pkcs12.parse(password).map_err(map_error_stack)?; Ok(Pkcs12(parsed)) } } impl tls_api::Certificate for Certificate { fn from_der(der: &[u8]) -> Result<Self> where Self: Sized { openssl::x509::X509::from_der(der) .map(Certificate) .map_err(Error::new) } } impl tls_api::TlsConnectorBuilder for TlsConnectorBuilder { type Connector = TlsConnector; fn add_root_certificate(&mut self, cert: Certificate) -> Result<&mut Self> { self.0 .builder_mut() .cert_store_mut() .add_cert(cert.0) .map_err(map_error_stack)?; Ok(self) } fn build(self) -> Result<TlsConnector> { Ok(TlsConnector(self.0.build())) } } #[derive(Debug)] struct TlsStream<S : io::Read + io::Write + fmt::Debug>(openssl::ssl::SslStream<S>); impl<S : io::Read + io::Write + fmt::Debug> TlsStream<S> { } impl<S : io::Read + io::Write + fmt::Debug> io::Read for TlsStream<S> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.0.read(buf) } } impl<S : io::Read + io::Write + fmt::Debug> io::Write for TlsStream<S> { fn write(&mut self, buf: &[u8]) -> io::Result<usize> { self.0.write(buf) } fn flush(&mut self) -> io::Result<()> { self.0.flush() } } impl<S : io::Read + io::Write + fmt::Debug + Send + Sync + 'static> tls_api::TlsStreamImpl<S> for TlsStream<S> { fn shutdown(&mut self) -> io::Result<()> { match self.0.shutdown() { Ok(_) | Err(openssl::ssl::Error::ZeroReturn) => Ok(()), Err(openssl::ssl::Error::Stream(e)) | Err(openssl::ssl::Error::WantRead(e)) | Err(openssl::ssl::Error::WantWrite(e)) => Err(e), Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)), } } fn get_mut(&mut self) -> &mut S { self.0.get_mut() } } struct MidHandshakeTlsStream<S : io::Read + io::Write + 'static>(Option<openssl::ssl::MidHandshakeSslStream<S>>); impl<S : io::Read + io::Write> fmt::Debug for MidHandshakeTlsStream<S> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("MidHandshakeTlsStream").finish() } } impl<S : io::Read + io::Write + fmt::Debug + Send + Sync + 'static> tls_api::MidHandshakeTlsStreamImpl<S> for MidHandshakeTlsStream<S> { fn handshake(&mut self) -> result::Result<tls_api::TlsStream<S>, tls_api::HandshakeError<S>> { self.0.take().unwrap().handshake() .map(|s| tls_api::TlsStream::new(TlsStream(s))) .map_err(map_handshake_error) } } fn map_handshake_error<S>(e: openssl::ssl::HandshakeError<S>) -> tls_api::HandshakeError<S> where S : io::Read + io::Write + fmt::Debug + Send + Sync + 'static { match e { openssl::ssl::HandshakeError::SetupFailure(e) => { tls_api::HandshakeError::Failure(Error::new(openssl::ssl::Error::Ssl(e))) } openssl::ssl::HandshakeError::Failure(e) => { tls_api::HandshakeError::Failure(Error::new(e.into_error())) }, openssl::ssl::HandshakeError::Interrupted(s) => { tls_api::HandshakeError::Interrupted( tls_api::MidHandshakeTlsStream::new(MidHandshakeTlsStream(Some(s)))) } } } impl tls_api::TlsConnector for TlsConnector { type Builder = TlsConnectorBuilder; type Certificate = Certificate; fn builder() -> Result<TlsConnectorBuilder> { openssl::ssl::SslConnectorBuilder::new(openssl::ssl::SslMethod::tls()) .map(TlsConnectorBuilder) .map_err(Error::new) } fn connect<S>(&self, domain: &str, stream: S) -> result::Result<tls_api::TlsStream<S>, tls_api::HandshakeError<S>> where S : io::Read + io::Write + fmt::Debug + Send + Sync + 'static { self.0.connect(domain, stream) .map(|s| tls_api::TlsStream::new(TlsStream(s))) .map_err(map_handshake_error) } fn danger_connect_without_providing_domain_for_certificate_verification_and_server_name_indication<S>( &self, stream: S) -> result::Result<tls_api::TlsStream<S>, tls_api::HandshakeError<S>> where S : io::Read + io::Write + fmt::Debug + Send + Sync + 'static { self.0.danger_connect_without_providing_domain_for_certificate_verification_and_server_name_indication(stream) .map(|s| tls_api::TlsStream::new(TlsStream(s))) .map_err(map_handshake_error) } } impl tls_api::TlsAcceptorBuilder for TlsAcceptorBuilder { type Acceptor = TlsAcceptor; fn build(self) -> Result<TlsAcceptor> { Ok(TlsAcceptor(self.0.build())) } } impl tls_api::TlsAcceptor for TlsAcceptor { type Pkcs12 = Pkcs12; type Builder = TlsAcceptorBuilder; fn builder(pkcs12: Pkcs12) -> Result<TlsAcceptorBuilder> { openssl::ssl::SslAcceptorBuilder::mozilla_intermediate( openssl::ssl::SslMethod::tls(), &pkcs12.0.pkey, &pkcs12.0.cert, &pkcs12.0.chain) .map(TlsAcceptorBuilder) .map_err(map_error_stack) } fn accept<S>(&self, stream: S) -> result::Result<tls_api::TlsStream<S>, tls_api::HandshakeError<S>> where S : io::Read + io::Write + fmt::Debug + Send + Sync + 'static { self.0.accept(stream) .map(|s| tls_api::TlsStream::new(TlsStream(s))) .map_err(map_handshake_error) } }