This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / ServerRecordProtocol.cs
1
2 //
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:
10 // 
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 // 
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.
21 //
22 /* Transport Security Layer (TLS)
23  * Copyright (c) 2003-2004 Carlos Guzman Alvarez
24  * 
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:
32  * 
33  * The above copyright notice and this permission notice shall be included 
34  * in all copies or substantial portions of the Software.
35  * 
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.
44  */
45
46 using System;
47 using System.Globalization;
48 using System.IO;
49
50 using Mono.Security.Protocol.Tls.Handshake;
51 using Mono.Security.Protocol.Tls.Handshake.Server;
52
53 namespace Mono.Security.Protocol.Tls
54 {
55         internal class ServerRecordProtocol : RecordProtocol
56         {
57                 #region Constructors
58
59                 public ServerRecordProtocol(
60                         Stream                  innerStream, 
61                         ServerContext   context) : base(innerStream, context)
62                 {
63                 }
64
65                 #endregion
66
67                 #region Send Messages
68
69                 public override void SendRecord(HandshakeType type)
70                 {
71                         // Create the record message
72                         HandshakeMessage msg = this.createServerHandshakeMessage(type);
73                         msg.Process();
74                         
75                         // Write record
76                         this.SendRecord(msg.ContentType, msg.EncodeMessage());
77
78                         // Update session
79                         msg.Update();
80
81                         // Reset message contents
82                         msg.Reset();
83                 }
84
85                 #endregion
86
87                 #region Handshake Processing Methods
88
89                 protected override void ProcessChangeCipherSpec()
90                 {
91                         // Reset sequence numbers
92                         this.context.ReadSequenceNumber = 0;
93
94                         // Make the pending state to be the current state
95                         this.context.IsActual = true;
96                 }
97
98                 protected override void ProcessHandshakeMessage(TlsStream handMsg)
99                 {
100                         HandshakeType           handshakeType   = (HandshakeType)handMsg.ReadByte();
101                         HandshakeMessage        message                 = null;
102
103                         // Read message length
104                         int length = handMsg.ReadInt24();
105
106                         // Read message data
107                         byte[] data = new byte[length];
108                         handMsg.Read(data, 0, length);
109
110                         // Create and process the server message
111                         message = this.createClientHandshakeMessage(handshakeType, data);
112                         message.Process();
113
114                         // Update the last handshake message
115                         this.Context.LastHandshakeMsg = handshakeType;
116
117                         // Update session
118                         if (message != null)
119                         {
120                                 message.Update();
121                         }
122                 }
123
124                 #endregion
125
126                 #region Server Handshake Message Factories
127
128                 private HandshakeMessage createClientHandshakeMessage(
129                         HandshakeType type, byte[] buffer)
130                 {
131                         switch (type)
132                         {
133                                 case HandshakeType.ClientHello:
134                                         return new TlsClientHello(this.context, buffer);
135
136                                 case HandshakeType.Certificate:
137                                         return new TlsClientCertificate(this.context, buffer);
138
139                                 case HandshakeType.ClientKeyExchange:
140                                         return new TlsClientKeyExchange(this.context, buffer);
141
142                                 case HandshakeType.CertificateVerify:
143                                         return new TlsClientCertificateVerify(this.context, buffer);
144
145                                 case HandshakeType.Finished:
146                                         return new TlsClientFinished(this.context, buffer);
147
148                                 default:
149                                         throw new TlsException(
150                                                 AlertDescription.UnexpectedMessage,
151                                                 String.Format(CultureInfo.CurrentUICulture,
152                                                         "Unknown server handshake message received ({0})", 
153                                                         type.ToString()));
154                         }
155                 }
156
157                 private HandshakeMessage createServerHandshakeMessage(
158                         HandshakeType type)
159                 {
160                         switch (type)
161                         {
162                                 case HandshakeType.HelloRequest:
163                                         this.SendRecord(HandshakeType.ClientHello);
164                                         return null;
165
166                                 case HandshakeType.ServerHello:
167                                         return new TlsServerHello(this.context);
168
169                                 case HandshakeType.Certificate:
170                                         return new TlsServerCertificate(this.context);
171
172                                 case HandshakeType.ServerKeyExchange:
173                                         return new TlsServerKeyExchange(this.context);
174
175                                 case HandshakeType.CertificateRequest:
176                                         return new TlsServerCertificateRequest(this.context);
177
178                                 case HandshakeType.ServerHelloDone:
179                                         return new TlsServerHelloDone(this.context);
180
181                                 case HandshakeType.Finished:
182                                         return new TlsServerFinished(this.context);
183
184                                 default:
185                                         throw new InvalidOperationException("Unknown server handshake message type: " + type.ToString() );                                      
186                         }
187                 }
188
189                 #endregion
190         }
191 }