{
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
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
message = this.decryptRecordFragment(
contentType,
message.ToArray());
+
+ DebugHelper.WriteLine("Decrypted record data", message.ToArray());
}
}
break;
default:
- throw this.context.CreateException("Unknown record received from server.");
+ throw new TlsException(
+ AlertDescription.UnexpectedMessage,
+ "Unknown record received from server.");
}
return result;
switch (alertLevel)
{
case AlertLevel.Fatal:
- throw this.context.CreateException(alertLevel, alertDesc);
+ throw new TlsException(alertLevel, alertDesc);
case AlertLevel.Warning:
default:
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
public void SendChangeCipherSpec()
{
+ DebugHelper.WriteLine(">>>> Write Change Cipher Spec");
+
// Send Change Cipher Spec message as a plain message
this.context.IsActual = false;
{
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);
{
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();
record.Write((short)fragment.Length);
record.Write(fragment);
+ DebugHelper.WriteLine("Record data", fragment);
+
// Update buffer position
position += fragmentLength;
}
mac = this.context.Cipher.ComputeServerRecordMAC(contentType, fragment);
}
+ DebugHelper.WriteLine(">>>> Record MAC", mac);
+
// Encrypt the message
byte[] ecr = this.context.Cipher.EncryptRecord(fragment, mac);
mac = this.context.Cipher.ComputeClientRecordMAC(contentType, dcrFragment);
}
+ DebugHelper.WriteLine(">>>> Record MAC", mac);
+
// Check record MAC
if (mac.Length != dcrMAC.Length)
{
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