Skip to content

Commit 2667b0f

Browse files
authored
Merge pull request #17 from nox/handshake-error
Improve error printing
2 parents adb00bd + 8fc84f0 commit 2667b0f

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

boring/src/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl fmt::Display for ErrorStack {
6666
let mut first = true;
6767
for err in &self.0 {
6868
if !first {
69-
fmt.write_str(", ")?;
69+
fmt.write_str("\n--\n")?;
7070
}
7171
write!(fmt, "{}", err)?;
7272
first = false;

boring/src/ssl/error.rs

+49-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::error;
44
use std::error::Error as StdError;
55
use std::fmt;
66
use std::io;
7+
use std::path::Path;
78

89
use error::ErrorStack;
910
use ssl::MidHandshakeSslStream;
@@ -150,29 +151,63 @@ impl<S: fmt::Debug> StdError for HandshakeError<S> {
150151
}
151152
}
152153

153-
impl<S: fmt::Debug> fmt::Display for HandshakeError<S> {
154+
impl<S> fmt::Display for HandshakeError<S> {
154155
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
155156
match *self {
156-
HandshakeError::SetupFailure(ref e) => write!(f, "stream setup failed: {}", e)?,
157-
HandshakeError::Failure(ref s) => {
158-
write!(f, "the handshake failed: {}", s.error())?;
159-
let verify = s.ssl().verify_result();
160-
if verify != X509VerifyResult::OK {
161-
write!(f, ": {}", verify)?;
162-
}
157+
HandshakeError::SetupFailure(ref e) => {
158+
write!(f, "TLS stream setup failed:\n\n{}", e)
163159
}
160+
HandshakeError::Failure(ref s) => fmt_mid_handshake_error(s, f, "TLS handshake failed"),
164161
HandshakeError::WouldBlock(ref s) => {
165-
write!(f, "the handshake was interrupted: {}", s.error())?;
166-
let verify = s.ssl().verify_result();
167-
if verify != X509VerifyResult::OK {
168-
write!(f, ": {}", verify)?;
169-
}
162+
fmt_mid_handshake_error(s, f, "TLS handshake interrupted")
170163
}
171164
}
172-
Ok(())
173165
}
174166
}
175167

168+
fn fmt_mid_handshake_error(
169+
s: &MidHandshakeSslStream<impl Sized>,
170+
f: &mut fmt::Formatter,
171+
prefix: &str,
172+
) -> fmt::Result {
173+
match s.ssl().verify_result() {
174+
X509VerifyResult::OK => write!(f, "{}", prefix)?,
175+
verify => write!(f, "{}: cert verification failed - {}", prefix, verify)?,
176+
}
177+
178+
if let Some(error) = s.error().io_error() {
179+
return write!(f, " ({})", error);
180+
}
181+
182+
if let Some(error) = s.error().ssl_error() {
183+
let errors = error.errors();
184+
185+
if errors.is_empty() {
186+
return Ok(());
187+
}
188+
189+
f.write_str(":\n")?;
190+
191+
for error in errors {
192+
let path = error.file();
193+
let file = Path::new(path)
194+
.file_name()
195+
.and_then(|name| name.to_str())
196+
.unwrap_or(path);
197+
198+
write!(
199+
f,
200+
"\n{} [{}] ({}:{})",
201+
error.reason().unwrap_or("unknown error"),
202+
error.code(),
203+
file,
204+
error.line()
205+
)?;
206+
}
207+
}
208+
Ok(())
209+
}
210+
176211
impl<S> From<ErrorStack> for HandshakeError<S> {
177212
fn from(e: ErrorStack) -> HandshakeError<S> {
178213
HandshakeError::SetupFailure(e)

boring/src/ssl/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2876,6 +2876,11 @@ impl<S> MidHandshakeSslStream<S> {
28762876
self.stream.into_inner()
28772877
}
28782878

2879+
/// Returns both the error and the source data stream, consuming `self`.
2880+
pub fn into_parts(self) -> (Error, S) {
2881+
(self.error, self.stream.into_inner())
2882+
}
2883+
28792884
/// Restarts the handshake process.
28802885
///
28812886
/// This corresponds to [`SSL_do_handshake`].

tokio-boring/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,21 @@ impl<S> HandshakeError<S> {
277277
}
278278
}
279279

280-
/// Converts error to the source data stream tha was used for the handshake.
280+
/// Converts error to the source data stream that was used for the handshake.
281281
pub fn into_source_stream(self) -> Option<S> {
282282
match self.0 {
283283
ssl::HandshakeError::Failure(s) => Some(s.into_source_stream().stream),
284284
_ => None,
285285
}
286286
}
287+
288+
/// Returns a reference to the source data stream.
289+
pub fn as_source_stream(&self) -> Option<&S> {
290+
match &self.0 {
291+
ssl::HandshakeError::Failure(s) => Some(&s.get_ref().stream),
292+
_ => None,
293+
}
294+
}
287295
}
288296

289297
impl<S> fmt::Debug for HandshakeError<S>

0 commit comments

Comments
 (0)