7cece5060e6fb8785a1779cbdfc3b5edad5a9cd5
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / ClientRecordProtocol.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.Client;
31
32 namespace Mono.Security.Protocol.Tls
33 {
34         internal class ClientRecordProtocol : RecordProtocol
35         {
36                 #region Constructors
37
38                 public ClientRecordProtocol(
39                         Stream                  innerStream, 
40                         ClientContext   context) : base(innerStream, context)
41                 {
42                 }
43
44                 #endregion
45
46                 #region Send Messages
47
48                 public override HandshakeMessage GetMessage(HandshakeType type)
49                 {
50                         HandshakeMessage msg = this.createClientHandshakeMessage(type);
51
52                         return msg;
53                 }
54
55                 #endregion
56
57                 #region Handshake Processing Methods
58
59                 protected override void ProcessHandshakeMessage(TlsStream handMsg)
60                 {
61                         HandshakeType           handshakeType   = (HandshakeType)handMsg.ReadByte();
62                         HandshakeMessage        message                 = null;
63
64                         DebugHelper.WriteLine(">>>> Processing Handshake record ({0})", handshakeType);
65
66                         // Read message length
67                         int length = handMsg.ReadInt24();
68
69                         // Read message data
70                         byte[] data = null;
71                         if (length > 0)
72                         {
73                                 data = new byte[length];
74                                 handMsg.Read (data, 0, length);
75                         }
76
77                         // Create and process the server message
78                         message = this.createServerHandshakeMessage(handshakeType, data);
79                         if (message != null)
80                         {
81                                 message.Process();
82                         }
83
84                         // Update the last handshake message
85                         this.Context.LastHandshakeMsg = handshakeType;
86
87                         // Update session
88                         if (message != null)
89                         {
90                                 message.Update();
91                                 this.Context.HandshakeMessages.WriteByte ((byte) handshakeType);
92                                 this.Context.HandshakeMessages.WriteInt24 (length);
93                                 if (length > 0) 
94                                 {
95                                         this.Context.HandshakeMessages.Write (data, 0, data.Length);
96                                 }
97                         }
98                 }
99
100                 #endregion
101
102                 #region Client Handshake Message Factories
103
104                 private HandshakeMessage createClientHandshakeMessage(HandshakeType type)
105                 {
106                         switch (type)
107                         {
108                                 case HandshakeType.ClientHello:
109                                         return new TlsClientHello(this.context);
110
111                                 case HandshakeType.Certificate:
112                                         return new TlsClientCertificate(this.context);
113
114                                 case HandshakeType.ClientKeyExchange:
115                                         return new TlsClientKeyExchange(this.context);
116
117                                 case HandshakeType.CertificateVerify:
118                                         return new TlsClientCertificateVerify(this.context);
119
120                                 case HandshakeType.Finished:
121                                         return new TlsClientFinished(this.context);
122
123                                 default:
124                                         throw new InvalidOperationException("Unknown client handshake message type: " + type.ToString() );
125                         }
126                 }
127
128                 private HandshakeMessage createServerHandshakeMessage(
129                         HandshakeType type, byte[] buffer)
130                 {
131                         ClientContext context = (ClientContext)this.context;
132
133                         switch (type)
134                         {
135                                 case HandshakeType.HelloRequest:
136                                         if (context.HandshakeState != HandshakeState.Started)
137                                         {
138                                                 context.HandshakeState = HandshakeState.None;
139                                                 // re-negotiation will occur at next read/write
140                                                 // (i.e. not during an existing encode/decode op)
141                                         }
142                                         else
143                                         {
144                                                 this.SendAlert(
145                                                         AlertLevel.Warning,
146                                                         AlertDescription.NoRenegotiation);
147                                         }
148                                         return null;
149
150                                 case HandshakeType.ServerHello:
151                                         return new TlsServerHello(this.context, buffer);
152
153                                 case HandshakeType.Certificate:
154                                         return new TlsServerCertificate(this.context, buffer);
155
156                                 case HandshakeType.ServerKeyExchange:
157                                         return new TlsServerKeyExchange(this.context, buffer);
158
159                                 case HandshakeType.CertificateRequest:
160                                         return new TlsServerCertificateRequest(this.context, buffer);
161
162                                 case HandshakeType.ServerHelloDone:
163                                         return new TlsServerHelloDone(this.context, buffer);
164
165                                 case HandshakeType.Finished:
166                                         return new TlsServerFinished(this.context, buffer);
167
168                                 default:
169                                         throw new TlsException(
170                                                 AlertDescription.UnexpectedMessage,
171                                                 String.Format(CultureInfo.CurrentUICulture,
172                                                         "Unknown server handshake message received ({0})", 
173                                                         type.ToString()));
174                         }
175                 }
176
177                 #endregion
178         }
179 }