3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 /* Transport Security Layer (TLS)
23 * Copyright (c) 2003-2004 Carlos Guzman Alvarez
25 * Permission is hereby granted, free of charge, to any person
26 * obtaining a copy of this software and associated documentation
27 * files (the "Software"), to deal in the Software without restriction,
28 * including without limitation the rights to use, copy, modify, merge,
29 * publish, distribute, sublicense, and/or sell copies of the Software,
30 * and to permit persons to whom the Software is furnished to do so,
31 * subject to the following conditions:
33 * The above copyright notice and this permission notice shall be included
34 * in all copies or substantial portions of the Software.
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
38 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
39 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
40 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
41 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
43 * DEALINGS IN THE SOFTWARE.
47 using System.Globalization;
50 using Mono.Security.Protocol.Tls.Handshake;
51 using Mono.Security.Protocol.Tls.Handshake.Server;
53 namespace Mono.Security.Protocol.Tls
55 internal class ServerRecordProtocol : RecordProtocol
59 public ServerRecordProtocol(
61 ServerContext context) : base(innerStream, context)
69 public override void SendRecord(HandshakeType type)
71 // Create the record message
72 HandshakeMessage msg = this.createServerHandshakeMessage(type);
76 this.SendRecord(msg.ContentType, msg.EncodeMessage());
81 // Reset message contents
87 #region Handshake Processing Methods
89 protected override void ProcessChangeCipherSpec()
91 // Reset sequence numbers
92 this.context.ReadSequenceNumber = 0;
94 // Make the pending state to be the current state
95 this.context.IsActual = true;
98 protected override void ProcessHandshakeMessage(TlsStream handMsg)
100 HandshakeType handshakeType = (HandshakeType)handMsg.ReadByte();
101 HandshakeMessage message = null;
103 // Read message length
104 int length = handMsg.ReadInt24();
107 byte[] data = new byte[length];
108 handMsg.Read(data, 0, length);
110 // Create and process the server message
111 message = this.createClientHandshakeMessage(handshakeType, data);
114 // Update the last handshake message
115 this.Context.LastHandshakeMsg = handshakeType;
126 #region Server Handshake Message Factories
128 private HandshakeMessage createClientHandshakeMessage(
129 HandshakeType type, byte[] buffer)
133 case HandshakeType.ClientHello:
134 return new TlsClientHello(this.context, buffer);
136 case HandshakeType.Certificate:
137 return new TlsClientCertificate(this.context, buffer);
139 case HandshakeType.ClientKeyExchange:
140 return new TlsClientKeyExchange(this.context, buffer);
142 case HandshakeType.CertificateVerify:
143 return new TlsClientCertificateVerify(this.context, buffer);
145 case HandshakeType.Finished:
146 return new TlsClientFinished(this.context, buffer);
149 throw new TlsException(
150 AlertDescription.UnexpectedMessage,
151 String.Format(CultureInfo.CurrentUICulture,
152 "Unknown server handshake message received ({0})",
157 private HandshakeMessage createServerHandshakeMessage(
162 case HandshakeType.HelloRequest:
163 this.SendRecord(HandshakeType.ClientHello);
166 case HandshakeType.ServerHello:
167 return new TlsServerHello(this.context);
169 case HandshakeType.Certificate:
170 return new TlsServerCertificate(this.context);
172 case HandshakeType.ServerKeyExchange:
173 return new TlsServerKeyExchange(this.context);
175 case HandshakeType.CertificateRequest:
176 return new TlsServerCertificateRequest(this.context);
178 case HandshakeType.ServerHelloDone:
179 return new TlsServerHelloDone(this.context);
181 case HandshakeType.Finished:
182 return new TlsServerFinished(this.context);
185 throw new InvalidOperationException("Unknown server handshake message type: " + type.ToString() );