2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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 //
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 //\r
24 \r
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 }