1 // Transport Security Layer (TLS)
2 // Copyright (c) 2003-2004 Carlos Guzman Alvarez
3 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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.
27 namespace Mono
.Security
.Protocol
.Tls
.Handshake
29 internal abstract class HandshakeMessage
: TlsStream
33 private Context context
;
34 private HandshakeType handshakeType
;
35 private ContentType contentType
;
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; }
61 public HandshakeMessage(
63 HandshakeType handshakeType
)
64 : this(context
, handshakeType
, ContentType
.Handshake
)
68 public HandshakeMessage(
70 HandshakeType handshakeType
,
71 ContentType contentType
) : base()
73 this.context
= context
;
74 this.handshakeType
= handshakeType
;
75 this.contentType
= contentType
;
78 public HandshakeMessage(
80 HandshakeType handshakeType
,
81 byte[] data
) : base(data
)
83 this.context
= context
;
84 this.handshakeType
= handshakeType
;
89 #region Abstract Methods
91 protected abstract void ProcessAsTls1();
93 protected abstract void ProcessAsSsl3();
101 switch (this.Context
.SecurityProtocol
)
103 case SecurityProtocolType
.Tls
:
104 case SecurityProtocolType
.Default
:
105 this.ProcessAsTls1();
108 case SecurityProtocolType
.Ssl3
:
109 this.ProcessAsSsl3();
112 case SecurityProtocolType
.Ssl2
:
114 throw new NotSupportedException("Unsupported security protocol type");
118 public virtual void Update()
122 // result may (should) be available from a previous call to EncodeMessage
124 cache
= this.EncodeMessage ();
125 this.context
.HandshakeMessages
.Write (cache
);
131 public virtual byte[] EncodeMessage()
137 byte[] hs
= this.ToArray ();
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
);
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))
158 if (buffer1
.Length
!= buffer2
.Length
)
161 for (int i
= 0; i
< buffer1
.Length
; i
++) {
162 if (buffer1
[i
] != buffer2
[i
])