Merge pull request #960 from ermshiperete/ShowHelp
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / ServerRecordProtocol.cs
1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
3
4 //
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:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
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.
23 //
24
25 using System;
26 using System.Globalization;
27 using System.IO;
28
29 using Mono.Security.Protocol.Tls.Handshake;
30 using Mono.Security.Protocol.Tls.Handshake.Server;
31
32 namespace Mono.Security.Protocol.Tls
33 {
34         internal class ServerRecordProtocol : RecordProtocol
35         {
36                 #region Constructors
37
38                 public ServerRecordProtocol(
39                         Stream                  innerStream, 
40                         ServerContext   context) : base(innerStream, context)
41                 {
42                 }
43
44                 #endregion
45
46                 #region Send Messages
47
48                 public override HandshakeMessage GetMessage(HandshakeType type)
49                 {
50                         // Create and process the record message
51                         HandshakeMessage msg = this.createServerHandshakeMessage(type);
52
53                         return msg;
54                 }
55
56                 #endregion
57
58                 #region Handshake Processing Methods
59
60                 protected override void ProcessHandshakeMessage(TlsStream handMsg)
61                 {
62                         HandshakeType           handshakeType   = (HandshakeType)handMsg.ReadByte();
63                         HandshakeMessage        message                 = null;
64
65                         // Read message length
66                         int length = handMsg.ReadInt24();
67
68                         // Read message data
69                         byte[] data = new byte[length];
70                         handMsg.Read(data, 0, length);
71
72                         // Create and process the server message
73                         message = this.createClientHandshakeMessage(handshakeType, data);
74                         message.Process();
75
76                         // Update the last handshake message
77                         this.Context.LastHandshakeMsg = handshakeType;
78
79                         // Update session
80                         if (message != null)
81                         {
82                                 message.Update();
83                                 this.Context.HandshakeMessages.WriteByte ((byte) handshakeType);
84                                 this.Context.HandshakeMessages.WriteInt24 (length);
85                                 this.Context.HandshakeMessages.Write (data, 0, data.Length);
86                         }
87                 }
88
89                 #endregion
90
91                 #region Server Handshake Message Factories
92
93                 private HandshakeMessage createClientHandshakeMessage(
94                         HandshakeType type, byte[] buffer)
95                 {
96                         switch (type)
97                         {
98                                 case HandshakeType.ClientHello:
99                                         return new TlsClientHello(this.context, buffer);
100
101                                 case HandshakeType.Certificate:
102                                         return new TlsClientCertificate(this.context, buffer);
103
104                                 case HandshakeType.ClientKeyExchange:
105                                         return new TlsClientKeyExchange(this.context, buffer);
106
107                                 case HandshakeType.CertificateVerify:
108                                         return new TlsClientCertificateVerify(this.context, buffer);
109
110                                 case HandshakeType.Finished:
111                                         return new TlsClientFinished(this.context, buffer);
112
113                                 default:
114                                         throw new TlsException(
115                                                 AlertDescription.UnexpectedMessage,
116                                                 String.Format(CultureInfo.CurrentUICulture,
117                                                         "Unknown server handshake message received ({0})", 
118                                                         type.ToString()));
119                         }
120                 }
121
122                 private HandshakeMessage createServerHandshakeMessage(
123                         HandshakeType type)
124                 {
125                         switch (type)
126                         {
127                                 case HandshakeType.HelloRequest:
128                                         this.SendRecord(HandshakeType.ClientHello);
129                                         return null;
130
131                                 case HandshakeType.ServerHello:
132                                         return new TlsServerHello(this.context);
133
134                                 case HandshakeType.Certificate:
135                                         return new TlsServerCertificate(this.context);
136
137                                 case HandshakeType.ServerKeyExchange:
138                                         return new TlsServerKeyExchange(this.context);
139
140                                 case HandshakeType.CertificateRequest:
141                                         return new TlsServerCertificateRequest(this.context);
142
143                                 case HandshakeType.ServerHelloDone:
144                                         return new TlsServerHelloDone(this.context);
145
146                                 case HandshakeType.Finished:
147                                         return new TlsServerFinished(this.context);
148
149                                 default:
150                                         throw new InvalidOperationException("Unknown server handshake message type: " + type.ToString() );                                      
151                         }
152                 }
153
154                 #endregion
155         }
156 }