From 4589defc7197db8d49edc67a354a34ce2ce5928b Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 7 Jun 2017 14:40:24 +0700 Subject: Specific exception TlsFatalAlertReceived for peer fatal alerts --- crypto/src/crypto/tls/TlsException.cs | 14 +++++++++++ crypto/src/crypto/tls/TlsFatalAlert.cs | 3 +-- crypto/src/crypto/tls/TlsFatalAlertReceived.cs | 21 +++++++++++++++++ crypto/src/crypto/tls/TlsProtocol.cs | 32 ++++++++++++-------------- 4 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 crypto/src/crypto/tls/TlsException.cs create mode 100644 crypto/src/crypto/tls/TlsFatalAlertReceived.cs (limited to 'crypto/src') diff --git a/crypto/src/crypto/tls/TlsException.cs b/crypto/src/crypto/tls/TlsException.cs new file mode 100644 index 000000000..cea9e3e77 --- /dev/null +++ b/crypto/src/crypto/tls/TlsException.cs @@ -0,0 +1,14 @@ +using System; +using System.IO; + +namespace Org.BouncyCastle.Crypto.Tls +{ + public class TlsException + : IOException + { + public TlsException(string message, Exception cause) + : base(message, cause) + { + } + } +} diff --git a/crypto/src/crypto/tls/TlsFatalAlert.cs b/crypto/src/crypto/tls/TlsFatalAlert.cs index 55d784dd9..6f1898179 100644 --- a/crypto/src/crypto/tls/TlsFatalAlert.cs +++ b/crypto/src/crypto/tls/TlsFatalAlert.cs @@ -1,10 +1,9 @@ using System; -using System.IO; namespace Org.BouncyCastle.Crypto.Tls { public class TlsFatalAlert - : IOException + : TlsException { private readonly byte alertDescription; diff --git a/crypto/src/crypto/tls/TlsFatalAlertReceived.cs b/crypto/src/crypto/tls/TlsFatalAlertReceived.cs new file mode 100644 index 000000000..044fc8027 --- /dev/null +++ b/crypto/src/crypto/tls/TlsFatalAlertReceived.cs @@ -0,0 +1,21 @@ +using System; + +namespace Org.BouncyCastle.Crypto.Tls +{ + public class TlsFatalAlertReceived + : TlsException + { + private readonly byte alertDescription; + + public TlsFatalAlertReceived(byte alertDescription) + : base(Tls.AlertDescription.GetText(alertDescription), null) + { + this.alertDescription = alertDescription; + } + + public virtual byte AlertDescription + { + get { return alertDescription; } + } + } +} diff --git a/crypto/src/crypto/tls/TlsProtocol.cs b/crypto/src/crypto/tls/TlsProtocol.cs index 5a1c08616..20ea3ede6 100644 --- a/crypto/src/crypto/tls/TlsProtocol.cs +++ b/crypto/src/crypto/tls/TlsProtocol.cs @@ -389,28 +389,26 @@ namespace Org.BouncyCastle.Crypto.Tls CleanupHandshake(); } - throw new IOException("Fatal alert received from TLS peer: " + AlertDescription.GetText(description)); + throw new TlsFatalAlertReceived(description); } - else + + /* + * RFC 5246 7.2.1. The other party MUST respond with a close_notify alert of its own + * and close down the connection immediately, discarding any pending writes. + */ + if (description == AlertDescription.close_notify) { - /* - * RFC 5246 7.2.1. The other party MUST respond with a close_notify alert of its own - * and close down the connection immediately, discarding any pending writes. - */ - if (description == AlertDescription.close_notify) + if (!mAppDataReady) { - if (!mAppDataReady) - { - throw new TlsFatalAlert(AlertDescription.handshake_failure); - } - HandleClose(false); + throw new TlsFatalAlert(AlertDescription.handshake_failure); } - - /* - * If it is just a warning, we continue. - */ - HandleWarningMessage(description); + HandleClose(false); } + + /* + * If it is just a warning, we continue. + */ + HandleWarningMessage(description); } } -- cgit 1.5.1