2010-05-21 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.ServiceModel / Mono.Security.Protocol.Tls.Handshake / HandshakeMessage.cs
1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
3 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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
27 namespace Mono.Security.Protocol.Tls.Handshake
28 {
29         internal abstract class HandshakeMessage : TlsStream
30         {
31                 #region Fields
32
33                 private Context                 context;
34                 private HandshakeType   handshakeType;
35                 private ContentType     contentType;
36                 private byte[]          cache;
37
38                 #endregion
39
40                 #region Properties
41
42                 public Context Context
43                 {
44                         get { return this.context; }
45                 }
46
47                 public HandshakeType HandshakeType
48                 {
49                         get { return this.handshakeType; }
50                 }
51
52                 public ContentType ContentType
53                 {
54                         get { return this.contentType; }
55                 }
56
57                 #endregion
58
59                 #region Constructors
60
61                 public HandshakeMessage(
62                         Context                 context,
63                         HandshakeType   handshakeType) 
64                         : this(context, handshakeType, ContentType.Handshake)
65                 {
66                 }
67
68                 public HandshakeMessage(
69                         Context                 context,
70                         HandshakeType   handshakeType,
71                         ContentType             contentType) : base()
72                 {
73                         this.context            = context;
74                         this.handshakeType      = handshakeType;
75                         this.contentType        = contentType;
76                 }
77
78                 public HandshakeMessage(
79                         Context                 context, 
80                         HandshakeType   handshakeType, 
81                         byte[]                  data) : base(data)
82                 {
83                         this.context            = context;
84                         this.handshakeType      = handshakeType;                                                
85                 }
86
87                 #endregion
88
89                 #region Abstract Methods
90
91                 protected abstract void ProcessAsTls1();
92
93                 protected abstract void ProcessAsSsl3();
94
95                 #endregion
96
97                 #region Methods
98
99                 public void Process()
100                 {
101                         switch (this.Context.SecurityProtocol)
102                         {
103                                 case SecurityProtocolType.Tls:
104                                 case SecurityProtocolType.Default:
105                                         this.ProcessAsTls1();
106                                         break;
107
108                                 case SecurityProtocolType.Ssl3:
109                                         this.ProcessAsSsl3();
110                                         break;
111
112                                 case SecurityProtocolType.Ssl2:
113                                 default:
114                                         throw new NotSupportedException("Unsupported security protocol type");
115                         }
116                 }
117
118                 public virtual void Update()
119                 {
120                         if (this.CanWrite)
121                         {
122                                 // result may (should) be available from a previous call to EncodeMessage
123                                 if (cache == null)
124                                         cache = this.EncodeMessage ();
125                                 this.context.HandshakeMessages.Write (cache);
126                                 this.Reset();
127                                 cache = null;
128                         }
129                 }
130
131                 public virtual byte[] EncodeMessage()
132                 {
133                         cache = null;
134
135                         if (CanWrite)
136                         {
137                                 byte[] hs = this.ToArray ();
138                                 int len = hs.Length;
139                                 cache = new byte[4 + len];
140
141                                 cache[0] = (byte) HandshakeType;
142                                 // Length as an Int24 in Network Order
143                                 cache[1] = (byte) (len >> 16);
144                                 cache[2] = (byte) (len >> 8);
145                                 cache[3] = (byte) len;
146                                 Buffer.BlockCopy (hs, 0, cache, 4, len);
147                         }
148
149                         return cache;
150                 }
151
152                 static public bool Compare (byte[] buffer1, byte[] buffer2)
153                 {
154                         // in our case both null can't exist (or be valid)
155                         if ((buffer1 == null) || (buffer2 == null))
156                                 return false;
157
158                         if (buffer1.Length != buffer2.Length)
159                                 return false;
160
161                         for (int i = 0; i < buffer1.Length; i++) {
162                                 if (buffer1[i] != buffer2[i])
163                                         return false;
164                         }
165                         return true;
166                 }
167
168                 #endregion
169         }
170 }