This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls.Handshake / HandshakeMessage.cs
1 /* Transport Security Layer (TLS)
2  * Copyright (c) 2003-2004 Carlos Guzman Alvarez
3  * 
4  * Permission is hereby granted, free of charge, to any person 
5  * obtaining a copy of this software and associated documentation 
6  * files (the "Software"), to deal in the Software without restriction, 
7  * including without limitation the rights to use, copy, modify, merge, 
8  * publish, distribute, sublicense, and/or sell copies of the Software, 
9  * and to permit persons to whom the Software is furnished to do so, 
10  * subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice shall be included 
13  * in all copies or substantial portions of the Software.
14  * 
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
22  * 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
37                 #endregion
38
39                 #region Properties
40
41                 public Context Context
42                 {
43                         get { return this.context; }
44                 }
45
46                 public HandshakeType HandshakeType
47                 {
48                         get { return this.handshakeType; }
49                 }
50
51                 public ContentType ContentType
52                 {
53                         get { return this.contentType; }
54                 }
55
56                 #endregion
57
58                 #region Constructors
59
60                 public HandshakeMessage(
61                         Context                 context,
62                         HandshakeType   handshakeType) 
63                         : this(context, handshakeType, ContentType.Handshake)
64                 {
65                 }
66
67                 public HandshakeMessage(
68                         Context                 context,
69                         HandshakeType   handshakeType,
70                         ContentType             contentType) : base()
71                 {
72                         this.context            = context;
73                         this.handshakeType      = handshakeType;
74                         this.contentType        = contentType;
75                 }
76
77                 public HandshakeMessage(
78                         Context                 context, 
79                         HandshakeType   handshakeType, 
80                         byte[]                  data) : base(data)
81                 {
82                         this.context            = context;
83                         this.handshakeType      = handshakeType;                                                
84                 }
85
86                 #endregion
87
88                 #region Abstract Methods
89
90                 protected abstract void ProcessAsTls1();
91
92                 protected abstract void ProcessAsSsl3();
93
94                 #endregion
95
96                 #region Methods
97
98                 public void Process()
99                 {
100                         switch (this.Context.SecurityProtocol)
101                         {
102                                 case SecurityProtocolType.Tls:
103                                 case SecurityProtocolType.Default:
104                                         this.ProcessAsTls1();
105                                         break;
106
107                                 case SecurityProtocolType.Ssl3:
108                                         this.ProcessAsSsl3();
109                                         break;
110
111                                 case SecurityProtocolType.Ssl2:
112                                 default:
113                                         throw new NotSupportedException("Unsupported security protocol type");
114                         }
115                 }
116
117                 public virtual void Update()
118                 {
119                         if (this.CanWrite)
120                         {
121                                 this.context.HandshakeMessages.Write(this.EncodeMessage());
122                                 this.Reset();
123                         }
124                 }
125
126                 public virtual byte[] EncodeMessage()
127                 {
128                         byte[] result = null;
129
130                         if (CanWrite)
131                         {
132                                 TlsStream c = new TlsStream();
133
134                                 c.Write((byte)HandshakeType);
135                                 c.WriteInt24((int)this.Length);
136                                 c.Write(this.ToArray());
137
138                                 result = c.ToArray();
139                         }
140
141                         return result;
142                 }
143
144                 #endregion
145         }
146 }