1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 using System.Globalization;
29 using Mono.Security.Protocol.Tls.Handshake;
30 using Mono.Security.Protocol.Tls.Handshake.Server;
32 namespace Mono.Security.Protocol.Tls
34 internal class ServerRecordProtocol : RecordProtocol
38 public ServerRecordProtocol(
40 ServerContext context) : base(innerStream, context)
48 public override HandshakeMessage GetMessage(HandshakeType type)
50 // Create and process the record message
51 HandshakeMessage msg = this.createServerHandshakeMessage(type);
58 #region Handshake Processing Methods
60 protected override void ProcessChangeCipherSpec()
62 // Reset sequence numbers
63 this.context.ReadSequenceNumber = 0;
65 // Make the pending state to be the current state
66 this.context.IsActual = true;
69 protected override void ProcessHandshakeMessage(TlsStream handMsg)
71 HandshakeType handshakeType = (HandshakeType)handMsg.ReadByte();
72 HandshakeMessage message = null;
74 // Read message length
75 int length = handMsg.ReadInt24();
78 byte[] data = new byte[length];
79 handMsg.Read(data, 0, length);
81 // Create and process the server message
82 message = this.createClientHandshakeMessage(handshakeType, data);
85 // Update the last handshake message
86 this.Context.LastHandshakeMsg = handshakeType;
92 this.Context.HandshakeMessages.WriteByte ((byte) handshakeType);
93 this.Context.HandshakeMessages.WriteInt24 (length);
94 this.Context.HandshakeMessages.Write (data, 0, data.Length);
100 #region Server Handshake Message Factories
102 private HandshakeMessage createClientHandshakeMessage(
103 HandshakeType type, byte[] buffer)
107 case HandshakeType.ClientHello:
108 return new TlsClientHello(this.context, buffer);
110 case HandshakeType.Certificate:
111 return new TlsClientCertificate(this.context, buffer);
113 case HandshakeType.ClientKeyExchange:
114 return new TlsClientKeyExchange(this.context, buffer);
116 case HandshakeType.CertificateVerify:
117 return new TlsClientCertificateVerify(this.context, buffer);
119 case HandshakeType.Finished:
120 return new TlsClientFinished(this.context, buffer);
123 throw new TlsException(
124 AlertDescription.UnexpectedMessage,
125 String.Format(CultureInfo.CurrentUICulture,
126 "Unknown server handshake message received ({0})",
131 private HandshakeMessage createServerHandshakeMessage(
136 case HandshakeType.HelloRequest:
137 this.SendRecord(HandshakeType.ClientHello);
140 case HandshakeType.ServerHello:
141 return new TlsServerHello(this.context);
143 case HandshakeType.Certificate:
144 return new TlsServerCertificate(this.context);
146 case HandshakeType.ServerKeyExchange:
147 return new TlsServerKeyExchange(this.context);
149 case HandshakeType.CertificateRequest:
150 return new TlsServerCertificateRequest(this.context);
152 case HandshakeType.ServerHelloDone:
153 return new TlsServerHelloDone(this.context);
155 case HandshakeType.Finished:
156 return new TlsServerFinished(this.context);
159 throw new InvalidOperationException("Unknown server handshake message type: " + type.ToString() );