Warnings
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / RecordProtocol.cs
index ce2ee614bec6520ab6fb25ac126da981bb79c327..1744d00196f2071586bf278f89dbff458be52def 100644 (file)
@@ -81,7 +81,9 @@ namespace Mono.Security.Protocol.Tls
                {
                        if (this.context.ConnectionEnd)
                        {
-                               throw this.context.CreateException("The session is finished and it's no longer valid.");
+                               throw new TlsException(
+                                       AlertDescription.InternalError,
+                                       "The session is finished and it's no longer valid.");
                        }
                        
                        // Try to read the Record Content Type
@@ -106,13 +108,21 @@ namespace Mono.Security.Protocol.Tls
                                        buffer, received, buffer.Length - received);
                        }
 
+                       DebugHelper.WriteLine(
+                               ">>>> Read record ({0}|{1})", 
+                               this.context.DecodeProtocolCode(protocol),
+                               contentType);
+                       DebugHelper.WriteLine("Record data", buffer);
+
                        TlsStream message = new TlsStream(buffer);
                
                        // Check that the message has a valid protocol version
                        if (protocol != this.context.Protocol && 
                                this.context.ProtocolNegotiated)
                        {
-                               throw this.context.CreateException("Invalid protocol version on message received from server");
+                               throw new TlsException(
+                                       AlertDescription.ProtocolVersion,
+                                       "Invalid protocol version on message received from server");
                        }
 
                        // Decrypt message contents if needed
@@ -127,6 +137,8 @@ namespace Mono.Security.Protocol.Tls
                                        message = this.decryptRecordFragment(
                                                contentType, 
                                                message.ToArray());
+
+                                       DebugHelper.WriteLine("Decrypted record data", message.ToArray());
                                }
                        }
 
@@ -162,7 +174,9 @@ namespace Mono.Security.Protocol.Tls
                                        break;
 
                                default:
-                                       throw this.context.CreateException("Unknown record received from server.");
+                                       throw new TlsException(
+                                               AlertDescription.UnexpectedMessage,
+                                               "Unknown record received from server.");
                        }
 
                        return result;
@@ -185,7 +199,7 @@ namespace Mono.Security.Protocol.Tls
                        switch (alertLevel)
                        {
                                case AlertLevel.Fatal:
-                                       throw this.context.CreateException(alertLevel, alertDesc);                                      
+                                       throw new TlsException(alertLevel, alertDesc);
 
                                case AlertLevel.Warning:
                                default:
@@ -205,26 +219,29 @@ namespace Mono.Security.Protocol.Tls
 
                public void SendAlert(AlertDescription description)
                {
-                       this.SendAlert(new Alert(this.Context, description));
+                       this.SendAlert(new Alert(description));
                }
 
                public void SendAlert(
                        AlertLevel                      level, 
                        AlertDescription        description)
                {
-                       this.SendAlert(new Alert(this.Context, level, description));
+                       this.SendAlert(new Alert(level, description));
                }
 
                public void SendAlert(Alert alert)
-               {                       
-                       // Write record
-                       this.SendRecord(ContentType.Alert, alert.ToArray());
+               {
+                       DebugHelper.WriteLine(">>>> Write Alert ({0}|{1})", alert.Description, alert.Message);
 
-                       // Update session
-                       alert.Update();
+                       // Write record
+                       this.SendRecord(
+                               ContentType.Alert, 
+                               new byte[]{(byte)alert.Level, (byte)alert.Description});
 
-                       // Reset message contents
-                       alert.Reset();
+                       if (alert.IsCloseNotify)
+                       {
+                               this.context.ConnectionEnd = true;
+                       }
                }
 
                #endregion
@@ -233,6 +250,8 @@ namespace Mono.Security.Protocol.Tls
 
                public void SendChangeCipherSpec()
                {
+                       DebugHelper.WriteLine(">>>> Write Change Cipher Spec");
+
                        // Send Change Cipher Spec message as a plain message
                        this.context.IsActual = false;
 
@@ -253,7 +272,9 @@ namespace Mono.Security.Protocol.Tls
                {
                        if (this.context.ConnectionEnd)
                        {
-                               throw this.context.CreateException("The session is finished and it's no longer valid.");
+                               throw new TlsException(
+                                       AlertDescription.InternalError,
+                                       "The session is finished and it's no longer valid.");
                        }
 
                        byte[] record = this.EncodeRecord(contentType, recordData);
@@ -278,7 +299,9 @@ namespace Mono.Security.Protocol.Tls
                {
                        if (this.context.ConnectionEnd)
                        {
-                               throw this.context.CreateException("The session is finished and it's no longer valid.");
+                               throw new TlsException(
+                                       AlertDescription.InternalError,
+                                       "The session is finished and it's no longer valid.");
                        }
 
                        TlsStream record = new TlsStream();
@@ -315,6 +338,8 @@ namespace Mono.Security.Protocol.Tls
                                record.Write((short)fragment.Length);
                                record.Write(fragment);
 
+                               DebugHelper.WriteLine("Record data", fragment);
+
                                // Update buffer position
                                position += fragmentLength;
                        }
@@ -342,6 +367,8 @@ namespace Mono.Security.Protocol.Tls
                                mac     = this.context.Cipher.ComputeServerRecordMAC(contentType, fragment);
                        }
 
+                       DebugHelper.WriteLine(">>>> Record MAC", mac);
+
                        // Encrypt the message
                        byte[] ecr = this.context.Cipher.EncryptRecord(fragment, mac);
 
@@ -394,6 +421,8 @@ namespace Mono.Security.Protocol.Tls
                                mac = this.context.Cipher.ComputeClientRecordMAC(contentType, dcrFragment);
                        }
 
+                       DebugHelper.WriteLine(">>>> Record MAC", mac);
+
                        // Check record MAC
                        if (mac.Length != dcrMAC.Length)
                        {
@@ -413,12 +442,7 @@ namespace Mono.Security.Protocol.Tls
 
                        if (badRecordMac)
                        {
-                               if (this.context is ServerContext)
-                               {
-                                       this.Context.RecordProtocol.SendAlert(AlertDescription.BadRecordMAC);
-                               }
-
-                               throw new TlsException("Bad record MAC");
+                               throw new TlsException(AlertDescription.BadRecordMAC, "Bad record MAC");
                        }
 
                        // Update sequence number