SslCipherSuite.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.
  23. //
  24. using System;
  25. using System.IO;
  26. using System.Security.Cryptography;
  27. using System.Text;
  28. namespace Mono.Security.Protocol.Tls
  29. {
  30. internal class SslCipherSuite : CipherSuite
  31. {
  32. #region Fields
  33. private byte[] pad1;
  34. private byte[] pad2;
  35. private const int MacHeaderLength = 11;
  36. private byte[] header;
  37. #endregion
  38. #region Constructors
  39. public SslCipherSuite(
  40. short code, string name, CipherAlgorithmType cipherAlgorithmType,
  41. HashAlgorithmType hashAlgorithmType, ExchangeAlgorithmType exchangeAlgorithmType,
  42. bool exportable, bool blockMode, byte keyMaterialSize,
  43. byte expandedKeyMaterialSize, short effectiveKeyBytes,
  44. byte ivSize, byte blockSize) :
  45. base(code, name, cipherAlgorithmType, hashAlgorithmType,
  46. exchangeAlgorithmType, exportable, blockMode, keyMaterialSize,
  47. expandedKeyMaterialSize, effectiveKeyBytes, ivSize, blockSize)
  48. {
  49. int padLength = (hashAlgorithmType == HashAlgorithmType.Md5) ? 48 : 40;
  50. // Fill pad arrays
  51. this.pad1 = new byte[padLength];
  52. this.pad2 = new byte[padLength];
  53. /* Pad the key for inner and outer digest */
  54. for (int i = 0; i < padLength; ++i)
  55. {
  56. this.pad1[i] = 0x36;
  57. this.pad2[i] = 0x5C;
  58. }
  59. }
  60. #endregion
  61. #region MAC Generation Methods
  62. public override byte[] ComputeServerRecordMAC(ContentType contentType, byte[] fragment)
  63. {
  64. HashAlgorithm hash = HashAlgorithm.Create(this.HashAlgorithmName);
  65. byte[] smac = this.Context.Read.ServerWriteMAC;
  66. hash.TransformBlock (smac, 0, smac.Length, smac, 0);
  67. hash.TransformBlock (pad1, 0, pad1.Length, pad1, 0);
  68. if (header == null)
  69. header = new byte [MacHeaderLength];
  70. ulong seqnum = (Context is ClientContext) ? Context.ReadSequenceNumber : Context.WriteSequenceNumber;
  71. Write (header, 0, seqnum);
  72. header [8] = (byte) contentType;
  73. Write (header, 9, (short)fragment.Length);
  74. hash.TransformBlock (header, 0, header.Length, header, 0);
  75. hash.TransformBlock (fragment, 0, fragment.Length, fragment, 0);
  76. // hack, else the method will allocate a new buffer of the same length (negative half the optimization)
  77. hash.TransformFinalBlock (CipherSuite.EmptyArray, 0, 0);
  78. byte[] blockHash = hash.Hash;
  79. hash.Initialize ();
  80. hash.TransformBlock (smac, 0, smac.Length, smac, 0);
  81. hash.TransformBlock (pad2, 0, pad2.Length, pad2, 0);
  82. hash.TransformBlock (blockHash, 0, blockHash.Length, blockHash, 0);
  83. // hack again
  84. hash.TransformFinalBlock (CipherSuite.EmptyArray, 0, 0);
  85. return hash.Hash;
  86. }
  87. public override byte[] ComputeClientRecordMAC(ContentType contentType, byte[] fragment)
  88. {
  89. HashAlgorithm hash = HashAlgorithm.Create(this.HashAlgorithmName);
  90. byte[] cmac = this.Context.Current.ClientWriteMAC;
  91. hash.TransformBlock (cmac, 0, cmac.Length, cmac, 0);
  92. hash.TransformBlock (pad1, 0, pad1.Length, pad1, 0);
  93. if (header == null)
  94. header = new byte [MacHeaderLength];
  95. ulong seqnum = (Context is ClientContext) ? Context.WriteSequenceNumber : Context.ReadSequenceNumber;
  96. Write (header, 0, seqnum);
  97. header [8] = (byte) contentType;
  98. Write (header, 9, (short)fragment.Length);
  99. hash.TransformBlock (header, 0, header.Length, header, 0);
  100. hash.TransformBlock (fragment, 0, fragment.Length, fragment, 0);
  101. // hack, else the method will allocate a new buffer of the same length (negative half the optimization)
  102. hash.TransformFinalBlock (CipherSuite.EmptyArray, 0, 0);
  103. byte[] blockHash = hash.Hash;
  104. hash.Initialize ();
  105. hash.TransformBlock (cmac, 0, cmac.Length, cmac, 0);
  106. hash.TransformBlock (pad2, 0, pad2.Length, pad2, 0);
  107. hash.TransformBlock (blockHash, 0, blockHash.Length, blockHash, 0);
  108. // hack again
  109. hash.TransformFinalBlock (CipherSuite.EmptyArray, 0, 0);
  110. return hash.Hash;
  111. }
  112. #endregion
  113. #region Key Generation Methods
  114. public override void ComputeMasterSecret(byte[] preMasterSecret)
  115. {
  116. TlsStream masterSecret = new TlsStream();
  117. masterSecret.Write(this.prf(preMasterSecret, "A", this.Context.RandomCS));
  118. masterSecret.Write(this.prf(preMasterSecret, "BB", this.Context.RandomCS));
  119. masterSecret.Write(this.prf(preMasterSecret, "CCC", this.Context.RandomCS));
  120. this.Context.MasterSecret = masterSecret.ToArray();
  121. DebugHelper.WriteLine(">>>> MasterSecret", this.Context.MasterSecret);
  122. }
  123. public override void ComputeKeys()
  124. {
  125. // Compute KeyBlock
  126. TlsStream tmp = new TlsStream();
  127. char labelChar = 'A';
  128. int count = 1;
  129. while (tmp.Length < this.KeyBlockSize)
  130. {
  131. string label = String.Empty;
  132. for (int i = 0; i < count; i++)
  133. {
  134. label += labelChar.ToString();
  135. }
  136. byte[] block = this.prf(this.Context.MasterSecret, label.ToString(), this.Context.RandomSC);
  137. int size = (tmp.Length + block.Length) > this.KeyBlockSize ? (this.KeyBlockSize - (int)tmp.Length) : block.Length;
  138. tmp.Write(block, 0, size);
  139. labelChar++;
  140. count++;
  141. }
  142. // Create keyblock
  143. TlsStream keyBlock = new TlsStream(tmp.ToArray());
  144. this.Context.Negotiating.ClientWriteMAC = keyBlock.ReadBytes(this.HashSize);
  145. this.Context.Negotiating.ServerWriteMAC = keyBlock.ReadBytes(this.HashSize);
  146. this.Context.ClientWriteKey = keyBlock.ReadBytes(this.KeyMaterialSize);
  147. this.Context.ServerWriteKey = keyBlock.ReadBytes(this.KeyMaterialSize);
  148. if (!this.IsExportable)
  149. {
  150. if (this.IvSize != 0)
  151. {
  152. this.Context.ClientWriteIV = keyBlock.ReadBytes(this.IvSize);
  153. this.Context.ServerWriteIV = keyBlock.ReadBytes(this.IvSize);
  154. }
  155. else
  156. {
  157. this.Context.ClientWriteIV = CipherSuite.EmptyArray;
  158. this.Context.ServerWriteIV = CipherSuite.EmptyArray;
  159. }
  160. }
  161. else
  162. {
  163. HashAlgorithm md5 = MD5.Create();
  164. int keySize = (md5.HashSize >> 3); //in bytes not bits
  165. byte[] temp = new byte [keySize];
  166. // Generate final write keys
  167. md5.TransformBlock(this.Context.ClientWriteKey, 0, this.Context.ClientWriteKey.Length, temp, 0);
  168. md5.TransformFinalBlock(this.Context.RandomCS, 0, this.Context.RandomCS.Length);
  169. byte[] finalClientWriteKey = new byte[this.ExpandedKeyMaterialSize];
  170. Buffer.BlockCopy(md5.Hash, 0, finalClientWriteKey, 0, this.ExpandedKeyMaterialSize);
  171. md5.Initialize();
  172. md5.TransformBlock(this.Context.ServerWriteKey, 0, this.Context.ServerWriteKey.Length, temp, 0);
  173. md5.TransformFinalBlock(this.Context.RandomSC, 0, this.Context.RandomSC.Length);
  174. byte[] finalServerWriteKey = new byte[this.ExpandedKeyMaterialSize];
  175. Buffer.BlockCopy(md5.Hash, 0, finalServerWriteKey, 0, this.ExpandedKeyMaterialSize);
  176. this.Context.ClientWriteKey = finalClientWriteKey;
  177. this.Context.ServerWriteKey = finalServerWriteKey;
  178. // Generate IV keys
  179. if (this.IvSize > 0)
  180. {
  181. md5.Initialize();
  182. temp = md5.ComputeHash(this.Context.RandomCS, 0, this.Context.RandomCS.Length);
  183. this.Context.ClientWriteIV = new byte[this.IvSize];
  184. Buffer.BlockCopy(temp, 0, this.Context.ClientWriteIV, 0, this.IvSize);
  185. md5.Initialize();
  186. temp = md5.ComputeHash(this.Context.RandomSC, 0, this.Context.RandomSC.Length);
  187. this.Context.ServerWriteIV = new byte[this.IvSize];
  188. Buffer.BlockCopy(temp, 0, this.Context.ServerWriteIV, 0, this.IvSize);
  189. }
  190. else
  191. {
  192. this.Context.ClientWriteIV = CipherSuite.EmptyArray;
  193. this.Context.ServerWriteIV = CipherSuite.EmptyArray;
  194. }
  195. }
  196. DebugHelper.WriteLine(">>>> KeyBlock", keyBlock.ToArray());
  197. DebugHelper.WriteLine(">>>> ClientWriteKey", this.Context.ClientWriteKey);
  198. DebugHelper.WriteLine(">>>> ClientWriteIV", this.Context.ClientWriteIV);
  199. DebugHelper.WriteLine(">>>> ClientWriteMAC", this.Context.Negotiating.ClientWriteMAC);
  200. DebugHelper.WriteLine(">>>> ServerWriteKey", this.Context.ServerWriteKey);
  201. DebugHelper.WriteLine(">>>> ServerWriteIV", this.Context.ServerWriteIV);
  202. DebugHelper.WriteLine(">>>> ServerWriteMAC", this.Context.Negotiating.ServerWriteMAC);
  203. ClientSessionCache.SetContextInCache (this.Context);
  204. // Clear no more needed data
  205. keyBlock.Reset();
  206. tmp.Reset();
  207. }
  208. #endregion
  209. #region Private Methods
  210. private byte[] prf(byte[] secret, string label, byte[] random)
  211. {
  212. HashAlgorithm md5 = MD5.Create();
  213. HashAlgorithm sha = SHA1.Create();
  214. // Compute SHA hash
  215. TlsStream block = new TlsStream();
  216. block.Write(Encoding.ASCII.GetBytes(label));
  217. block.Write(secret);
  218. block.Write(random);
  219. byte[] shaHash = sha.ComputeHash(block.ToArray(), 0, (int)block.Length);
  220. block.Reset();
  221. // Compute MD5 hash
  222. block.Write(secret);
  223. block.Write(shaHash);
  224. byte[] result = md5.ComputeHash(block.ToArray(), 0, (int)block.Length);
  225. // Free resources
  226. block.Reset();
  227. return result;
  228. }
  229. #endregion
  230. }
  231. }