Skip to content

Commit

Permalink
Merge pull request #17 from nox/handshake-error
Browse files Browse the repository at this point in the history
Improve error printing
  • Loading branch information
inikulin authored Mar 9, 2021
2 parents adb00bd + 8fc84f0 commit 2667b0f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
2 changes: 1 addition & 1 deletion boring/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl fmt::Display for ErrorStack {
let mut first = true;
for err in &self.0 {
if !first {
fmt.write_str(", ")?;
fmt.write_str("\n--\n")?;
}
write!(fmt, "{}", err)?;
first = false;
Expand Down
63 changes: 49 additions & 14 deletions boring/src/ssl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::error;
use std::error::Error as StdError;
use std::fmt;
use std::io;
use std::path::Path;

use error::ErrorStack;
use ssl::MidHandshakeSslStream;
Expand Down Expand Up @@ -150,29 +151,63 @@ impl<S: fmt::Debug> StdError for HandshakeError<S> {
}
}

impl<S: fmt::Debug> fmt::Display for HandshakeError<S> {
impl<S> fmt::Display for HandshakeError<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
HandshakeError::SetupFailure(ref e) => write!(f, "stream setup failed: {}", e)?,
HandshakeError::Failure(ref s) => {
write!(f, "the handshake failed: {}", s.error())?;
let verify = s.ssl().verify_result();
if verify != X509VerifyResult::OK {
write!(f, ": {}", verify)?;
}
HandshakeError::SetupFailure(ref e) => {
write!(f, "TLS stream setup failed:\n\n{}", e)
}
HandshakeError::Failure(ref s) => fmt_mid_handshake_error(s, f, "TLS handshake failed"),
HandshakeError::WouldBlock(ref s) => {
write!(f, "the handshake was interrupted: {}", s.error())?;
let verify = s.ssl().verify_result();
if verify != X509VerifyResult::OK {
write!(f, ": {}", verify)?;
}
fmt_mid_handshake_error(s, f, "TLS handshake interrupted")
}
}
Ok(())
}
}

fn fmt_mid_handshake_error(
s: &MidHandshakeSslStream<impl Sized>,
f: &mut fmt::Formatter,
prefix: &str,
) -> fmt::Result {
match s.ssl().verify_result() {
X509VerifyResult::OK => write!(f, "{}", prefix)?,
verify => write!(f, "{}: cert verification failed - {}", prefix, verify)?,
}

if let Some(error) = s.error().io_error() {
return write!(f, " ({})", error);
}

if let Some(error) = s.error().ssl_error() {
let errors = error.errors();

if errors.is_empty() {
return Ok(());
}

f.write_str(":\n")?;

for error in errors {
let path = error.file();
let file = Path::new(path)
.file_name()
.and_then(|name| name.to_str())
.unwrap_or(path);

write!(
f,
"\n{} [{}] ({}:{})",
error.reason().unwrap_or("unknown error"),
error.code(),
file,
error.line()
)?;
}
}
Ok(())
}

impl<S> From<ErrorStack> for HandshakeError<S> {
fn from(e: ErrorStack) -> HandshakeError<S> {
HandshakeError::SetupFailure(e)
Expand Down
5 changes: 5 additions & 0 deletions boring/src/ssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2876,6 +2876,11 @@ impl<S> MidHandshakeSslStream<S> {
self.stream.into_inner()
}

/// Returns both the error and the source data stream, consuming `self`.
pub fn into_parts(self) -> (Error, S) {
(self.error, self.stream.into_inner())
}

/// Restarts the handshake process.
///
/// This corresponds to [`SSL_do_handshake`].
Expand Down
10 changes: 9 additions & 1 deletion tokio-boring/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,21 @@ impl<S> HandshakeError<S> {
}
}

/// Converts error to the source data stream tha was used for the handshake.
/// Converts error to the source data stream that was used for the handshake.
pub fn into_source_stream(self) -> Option<S> {
match self.0 {
ssl::HandshakeError::Failure(s) => Some(s.into_source_stream().stream),
_ => None,
}
}

/// Returns a reference to the source data stream.
pub fn as_source_stream(&self) -> Option<&S> {
match &self.0 {
ssl::HandshakeError::Failure(s) => Some(&s.get_ref().stream),
_ => None,
}
}
}

impl<S> fmt::Debug for HandshakeError<S>
Expand Down

0 comments on commit 2667b0f

Please sign in to comment.