SslHandshakeHash.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included 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 OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Security.Cryptography;
  25. namespace Mono.Security.Protocol.Tls
  26. {
  27. internal class SslHandshakeHash : System.Security.Cryptography.HashAlgorithm
  28. {
  29. #region Fields
  30. private HashAlgorithm md5;
  31. private HashAlgorithm sha;
  32. private bool hashing;
  33. private byte[] secret;
  34. private byte[] innerPadMD5;
  35. private byte[] outerPadMD5;
  36. private byte[] innerPadSHA;
  37. private byte[] outerPadSHA;
  38. #endregion
  39. #region Constructors
  40. public SslHandshakeHash(byte[] secret)
  41. {
  42. // Create md5 and sha1 hashes
  43. this.md5 = HashAlgorithm.Create("MD5");
  44. this.sha = HashAlgorithm.Create("SHA1");
  45. // Set HashSizeValue
  46. this.HashSizeValue = md5.HashSize + sha.HashSize;
  47. // Update secret
  48. this.secret = secret;
  49. this.Initialize();
  50. }
  51. #endregion
  52. #region Methods
  53. public override void Initialize()
  54. {
  55. this.md5.Initialize();
  56. this.sha.Initialize();
  57. this.initializePad();
  58. this.hashing = false;
  59. }
  60. protected override byte[] HashFinal()
  61. {
  62. if (!this.hashing)
  63. {
  64. this.hashing = true;
  65. }
  66. // Finalize the md5 hash
  67. this.md5.TransformBlock(this.secret, 0, this.secret.Length, this.secret, 0);
  68. this.md5.TransformFinalBlock(this.innerPadMD5, 0, this.innerPadMD5.Length);
  69. byte[] firstResultMD5 = this.md5.Hash;
  70. this.md5.Initialize();
  71. this.md5.TransformBlock(this.secret, 0, this.secret.Length, this.secret, 0);
  72. this.md5.TransformBlock(this.outerPadMD5, 0, this.outerPadMD5.Length, this.outerPadMD5, 0);
  73. this.md5.TransformFinalBlock(firstResultMD5, 0, firstResultMD5.Length);
  74. // Finalize the sha1 hash
  75. this.sha.TransformBlock(this.secret, 0, this.secret.Length, this.secret, 0);
  76. this.sha.TransformFinalBlock(this.innerPadSHA, 0, this.innerPadSHA.Length);
  77. byte[] firstResultSHA = this.sha.Hash;
  78. this.sha.Initialize();
  79. this.sha.TransformBlock(this.secret, 0, this.secret.Length, this.secret, 0);
  80. this.sha.TransformBlock(this.outerPadSHA, 0, this.outerPadSHA.Length, this.outerPadSHA, 0);
  81. this.sha.TransformFinalBlock(firstResultSHA, 0, firstResultSHA.Length);
  82. this.Initialize();
  83. byte[] result = new byte[36];
  84. Buffer.BlockCopy(this.md5.Hash, 0, result, 0, 16);
  85. Buffer.BlockCopy(this.sha.Hash, 0, result, 16, 20);
  86. return result;
  87. }
  88. protected override void HashCore(byte[] array, int ibStart, int cbSize)
  89. {
  90. if (!this.hashing)
  91. {
  92. this.hashing = true;
  93. }
  94. this.md5.TransformBlock(array, ibStart, cbSize, array, ibStart);
  95. this.sha.TransformBlock(array, ibStart, cbSize, array, ibStart);
  96. }
  97. public byte[] CreateSignature(RSA rsa)
  98. {
  99. if (rsa == null)
  100. {
  101. throw new CryptographicUnexpectedOperationException ("missing key");
  102. }
  103. RSASslSignatureFormatter f = new RSASslSignatureFormatter(rsa);
  104. f.SetHashAlgorithm("MD5SHA1");
  105. return f.CreateSignature(this.Hash);
  106. }
  107. public bool VerifySignature(RSA rsa, byte[] rgbSignature)
  108. {
  109. if (rsa == null)
  110. {
  111. throw new CryptographicUnexpectedOperationException ("missing key");
  112. }
  113. if (rgbSignature == null)
  114. {
  115. throw new ArgumentNullException ("rgbSignature");
  116. }
  117. RSASslSignatureDeformatter d = new RSASslSignatureDeformatter(rsa);
  118. d.SetHashAlgorithm("MD5SHA1");
  119. return d.VerifySignature(this.Hash, rgbSignature);
  120. }
  121. #endregion
  122. #region Private Methods
  123. private void initializePad()
  124. {
  125. // Fill md5 arrays
  126. this.innerPadMD5 = new byte[48];
  127. this.outerPadMD5 = new byte[48];
  128. /* Pad the key for inner and outer digest */
  129. for (int i = 0; i < 48; ++i)
  130. {
  131. this.innerPadMD5[i] = 0x36;
  132. this.outerPadMD5[i] = 0x5C;
  133. }
  134. // Fill sha arrays
  135. this.innerPadSHA = new byte[40];
  136. this.outerPadSHA = new byte[40];
  137. /* Pad the key for inner and outer digest */
  138. for (int i = 0; i < 40; ++i)
  139. {
  140. this.innerPadSHA[i] = 0x36;
  141. this.outerPadSHA[i] = 0x5C;
  142. }
  143. }
  144. #endregion
  145. }
  146. }