flush
[mcs.git] / class / Mono.Security / Mono.Security.Protocol.Tls.Handshake / HandshakeMessage.cs
blob540712741655c49955078f6230ca4a92c7f41f1b
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.
25 using System;
27 namespace Mono.Security.Protocol.Tls.Handshake
29 internal abstract class HandshakeMessage : TlsStream
31 #region Fields
33 private Context context;
34 private HandshakeType handshakeType;
35 private ContentType contentType;
36 private byte[] cache;
38 #endregion
40 #region Properties
42 public Context Context
44 get { return this.context; }
47 public HandshakeType HandshakeType
49 get { return this.handshakeType; }
52 public ContentType ContentType
54 get { return this.contentType; }
57 #endregion
59 #region Constructors
61 public HandshakeMessage(
62 Context context,
63 HandshakeType handshakeType)
64 : this(context, handshakeType, ContentType.Handshake)
68 public HandshakeMessage(
69 Context context,
70 HandshakeType handshakeType,
71 ContentType contentType) : base()
73 this.context = context;
74 this.handshakeType = handshakeType;
75 this.contentType = contentType;
78 public HandshakeMessage(
79 Context context,
80 HandshakeType handshakeType,
81 byte[] data) : base(data)
83 this.context = context;
84 this.handshakeType = handshakeType;
87 #endregion
89 #region Abstract Methods
91 protected abstract void ProcessAsTls1();
93 protected abstract void ProcessAsSsl3();
95 #endregion
97 #region Methods
99 public void Process()
101 switch (this.Context.SecurityProtocol)
103 case SecurityProtocolType.Tls:
104 case SecurityProtocolType.Default:
105 this.ProcessAsTls1();
106 break;
108 case SecurityProtocolType.Ssl3:
109 this.ProcessAsSsl3();
110 break;
112 case SecurityProtocolType.Ssl2:
113 default:
114 throw new NotSupportedException("Unsupported security protocol type");
118 public virtual void Update()
120 if (this.CanWrite)
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;
131 public virtual byte[] EncodeMessage()
133 cache = null;
135 if (CanWrite)
137 byte[] hs = this.ToArray ();
138 int len = hs.Length;
139 cache = new byte[4 + len];
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);
149 return cache;
152 static public bool Compare (byte[] buffer1, byte[] buffer2)
154 // in our case both null can't exist (or be valid)
155 if ((buffer1 == null) || (buffer2 == null))
156 return false;
158 if (buffer1.Length != buffer2.Length)
159 return false;
161 for (int i = 0; i < buffer1.Length; i++) {
162 if (buffer1[i] != buffer2[i])
163 return false;
165 return true;
168 #endregion