[Mono.Security]: More exception debug foo.
authorMartin Baulig <martin.baulig@xamarin.com>
Thu, 15 Jan 2015 18:08:46 +0000 (19:08 +0100)
committerMartin Baulig <martin.baulig@xamarin.com>
Thu, 15 Jan 2015 18:08:46 +0000 (19:08 +0100)
Note: This patch may cause some ugly exception messages.  This is intended to help
us debug and hopefully fix bug #19334.

mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs
mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslClientStream.cs
mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslStreamBase.cs

index 166f12f0d23c7d0790fc722c2d140494f75b1b11..589510685a63193d51b8308798be26820c61cf76 100644 (file)
@@ -669,14 +669,24 @@ namespace Mono.Security.Protocol.Tls
 
                #region Send Alert Methods
 
+               internal void SendAlert(ref Exception ex)
+               {
+                       var tlsEx = ex as TlsException;
+                       var alert = tlsEx != null ? tlsEx.Alert : new Alert(AlertDescription.InternalError);
+
+                       try {
+                               SendAlert(alert);
+                       } catch (Exception alertEx) {
+                               ex = new IOException (string.Format ("Error while sending TLS Alert ({0}:{1}): {2}", alert.Level, alert.Description, ex), ex);
+                       }
+               }
+
                public void SendAlert(AlertDescription description)
                {
                        this.SendAlert(new Alert(description));
                }
 
-               public void SendAlert(
-                       AlertLevel                      level, 
-                       AlertDescription        description)
+               public void SendAlert(AlertLevel level, AlertDescription description)
                {
                        this.SendAlert(new Alert(level, description));
                }
index d0d0721a12d106413f1fd9568245c9ebea49605a..08b69d5d6f69c723b84354c4516fd157668b047c 100644 (file)
@@ -586,16 +586,10 @@ namespace Mono.Security.Protocol.Tls
                                        break;
                                }
                        }
-                       catch (TlsException ex)
-                       {
-                               // FIXME: should the send alert also be done asynchronously here and below?
-                               this.protocol.SendAlert(ex.Alert);
-                               negotiate.SetComplete (new IOException("The authentication or decryption has failed.", ex));
-                       }
                        catch (Exception ex)
                        {
-                               this.protocol.SendAlert(AlertDescription.InternalError);
-                               negotiate.SetComplete (new IOException("The authentication or decryption has failed.", ex));
+                               this.protocol.SendAlert(ref ex);
+                               negotiate.SetComplete(new IOException("The authentication or decryption has failed.", ex));
                        }
                }
 
index 5c0032c02f0c5724a29fcf5052a639e39a606cc0..e257296cc23f828360bde833433c92bc8a13a8ae 100644 (file)
@@ -98,16 +98,9 @@ namespace Mono.Security.Protocol.Tls
                                {
                                        this.EndNegotiateHandshake(asyncResult);
                                }
-                               catch (TlsException ex)
-                               {
-                                       this.protocol.SendAlert(ex.Alert);
-
-                                       throw new IOException("The authentication or decryption has failed.", ex);
-                               }
                                catch (Exception ex)
                                {
-                                       this.protocol.SendAlert(AlertDescription.InternalError);
-
+                                       this.protocol.SendAlert(ref ex);
                                        throw new IOException("The authentication or decryption has failed.", ex);
                                }
 
@@ -502,17 +495,10 @@ namespace Mono.Security.Protocol.Tls
                                        }
                                }
                        }
-                       catch (TlsException ex)
-                       {
-                               this.negotiationComplete.Set();
-                               this.protocol.SendAlert(ex.Alert);
-
-                               throw new IOException("The authentication or decryption has failed.", ex);
-                       }
                        catch (Exception ex)
                        {
                                this.negotiationComplete.Set();
-                               this.protocol.SendAlert(AlertDescription.InternalError);
+                               this.protocol.SendAlert(ref ex);
 
                                throw new IOException("The authentication or decryption has failed.", ex);
                        }
@@ -628,15 +614,10 @@ namespace Mono.Security.Protocol.Tls
                                        asyncResult.SetComplete(0);
                                }
                        }
-                       catch (TlsException ex)
-                       {
-                               this.protocol.SendAlert(ex.Alert);
-
-                               throw new IOException("The authentication or decryption has failed.", ex);
-                       }
                        catch (Exception ex)
                        {
-                               throw new IOException("IO exception during read.", ex);
+                               this.protocol.SendAlert(ref ex);
+                               throw new IOException("The authentication or decryption has failed.", ex);
                        }
                }
 
@@ -781,17 +762,13 @@ namespace Mono.Security.Protocol.Tls
                                                record, 0, record.Length, new AsyncCallback(InternalWriteCallback), asyncResult);
                                }
                        }
-                       catch (TlsException ex)
+                       catch (Exception ex)
                        {
-                               this.protocol.SendAlert(ex.Alert);
+                               this.protocol.SendAlert (ref ex);
                                this.Close();
 
                                throw new IOException("The authentication or decryption has failed.", ex);
                        }
-                       catch (Exception ex)
-                       {
-                               throw new IOException("IO exception during Write.", ex);
-                       }
                }
 
                private void InternalWriteCallback(IAsyncResult ar)
@@ -1121,16 +1098,12 @@ namespace Mono.Security.Protocol.Tls
                                        byte[] record = this.protocol.EncodeRecord (ContentType.ApplicationData, buffer, offset, count);
                                        this.innerStream.Write (record, 0, record.Length);
                                }
-                               catch (TlsException ex)
+                               catch (Exception ex)
                                {
-                                       this.protocol.SendAlert(ex.Alert);
+                                       this.protocol.SendAlert(ref ex);
                                        this.Close();
                                        throw new IOException("The authentication or decryption has failed.", ex);
                                }
-                               catch (Exception ex)
-                               {
-                                       throw new IOException("IO exception during Write.", ex);
-                               }
                        }
                }