Merge pull request #799 from kebby/master
[mono.git] / mcs / class / Mono.Security / Mono.Security.Protocol.Tls / SslHandshakeHash.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 //
24
25 using System;
26 using System.Security.Cryptography;
27
28 namespace Mono.Security.Protocol.Tls
29 {
30         internal class SslHandshakeHash : System.Security.Cryptography.HashAlgorithm
31         {
32                 #region Fields
33
34                 private HashAlgorithm   md5;
35                 private HashAlgorithm   sha;
36                 private bool                    hashing;
37                 private byte[]                  secret;
38                 private byte[]                  innerPadMD5;
39                 private byte[]                  outerPadMD5;
40                 private byte[]                  innerPadSHA;
41                 private byte[]                  outerPadSHA;
42
43                 #endregion
44
45                 #region Constructors
46
47                 public SslHandshakeHash(byte[] secret)
48                 {
49                         // Create md5 and sha1 hashes
50                         this.md5 = MD5.Create ();
51                         this.sha = SHA1.Create ();
52                         
53                         // Set HashSizeValue
54                         this.HashSizeValue = md5.HashSize + sha.HashSize;
55
56                         // Update secret
57                         this.secret = secret;
58
59                         this.Initialize();
60                 }
61
62                 #endregion
63
64                 #region Methods
65
66                 public override void Initialize()
67                 {
68                         this.md5.Initialize();
69                         this.sha.Initialize();
70                         this.initializePad();
71                         this.hashing = false;
72                 }
73
74                 protected override byte[] HashFinal()
75                 {
76                         if (!this.hashing)
77                         {
78                                 this.hashing = true;
79                         }
80
81                         // Finalize the md5 hash
82                         this.md5.TransformBlock(this.secret, 0, this.secret.Length, this.secret, 0);
83                         this.md5.TransformFinalBlock(this.innerPadMD5, 0, this.innerPadMD5.Length);
84
85                         byte[] firstResultMD5 = this.md5.Hash;
86
87                         this.md5.Initialize();
88                         this.md5.TransformBlock(this.secret, 0, this.secret.Length, this.secret, 0);
89                         this.md5.TransformBlock(this.outerPadMD5, 0, this.outerPadMD5.Length, this.outerPadMD5, 0);
90                         this.md5.TransformFinalBlock(firstResultMD5, 0, firstResultMD5.Length);
91                         
92                         // Finalize the sha1 hash
93                         this.sha.TransformBlock(this.secret, 0, this.secret.Length, this.secret, 0);
94                         this.sha.TransformFinalBlock(this.innerPadSHA, 0, this.innerPadSHA.Length);
95
96                         byte[] firstResultSHA = this.sha.Hash;
97                         
98                         this.sha.Initialize();
99                         this.sha.TransformBlock(this.secret, 0, this.secret.Length, this.secret, 0);
100                         this.sha.TransformBlock(this.outerPadSHA, 0, this.outerPadSHA.Length, this.outerPadSHA, 0);
101                         this.sha.TransformFinalBlock(firstResultSHA, 0, firstResultSHA.Length);
102
103                         this.Initialize();
104
105                         byte[] result = new byte[36];
106
107                         Buffer.BlockCopy(this.md5.Hash, 0, result, 0, 16);
108                         Buffer.BlockCopy(this.sha.Hash, 0, result, 16, 20);
109
110                         return result;
111                 }
112
113                 protected override void HashCore(byte[] array, int ibStart, int cbSize)
114                 {
115                         if (!this.hashing)
116                         {
117                                 this.hashing = true;
118                         }
119
120                         this.md5.TransformBlock(array, ibStart, cbSize, array, ibStart);
121                         this.sha.TransformBlock(array, ibStart, cbSize, array, ibStart);
122                 }
123
124                 public byte[] CreateSignature(RSA rsa) 
125                 {
126                         if (rsa == null)
127                         {
128                                 throw new CryptographicUnexpectedOperationException ("missing key");
129                         }
130
131                         RSASslSignatureFormatter f = new RSASslSignatureFormatter(rsa);
132                         f.SetHashAlgorithm("MD5SHA1");
133
134                         return f.CreateSignature(this.Hash);
135                 }
136
137                 public bool VerifySignature(RSA rsa, byte[] rgbSignature) 
138                 {
139                         if (rsa == null)
140                         {
141                                 throw new CryptographicUnexpectedOperationException ("missing key");
142                         }
143                         if (rgbSignature == null)
144                         {
145                                 throw new ArgumentNullException ("rgbSignature");
146                         }
147
148                         RSASslSignatureDeformatter d = new RSASslSignatureDeformatter(rsa);
149                         d.SetHashAlgorithm("MD5SHA1");
150
151                         return d.VerifySignature(this.Hash, rgbSignature);
152                 }
153
154                 #endregion
155
156                 #region Private Methods
157
158                 private void initializePad()
159                 {
160                         // Fill md5 arrays
161                         this.innerPadMD5 = new byte[48];
162                         this.outerPadMD5 = new byte[48];
163
164                         /* Pad the key for inner and outer digest */
165                         for (int i = 0; i < 48; ++i) 
166                         {
167                                 this.innerPadMD5[i] = 0x36;
168                                 this.outerPadMD5[i] = 0x5C;
169                         }
170
171                         // Fill sha arrays
172                         this.innerPadSHA = new byte[40];
173                         this.outerPadSHA = new byte[40];
174
175                         /* Pad the key for inner and outer digest */
176                         for (int i = 0; i < 40; ++i) 
177                         {
178                                 this.innerPadSHA[i] = 0x36;
179                                 this.outerPadSHA[i] = 0x5C;
180                         }
181                 }
182
183                 #endregion
184         }
185 }