TlsCipherSuite.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. namespace Mono.Security.Protocol.Tls
  28. {
  29. internal class TlsCipherSuite : CipherSuite
  30. {
  31. private const int MacHeaderLength = 13;
  32. private byte[] header;
  33. #region Constructors
  34. public TlsCipherSuite(
  35. short code, string name, CipherAlgorithmType cipherAlgorithmType,
  36. HashAlgorithmType hashAlgorithmType, ExchangeAlgorithmType exchangeAlgorithmType,
  37. bool exportable, bool blockMode, byte keyMaterialSize,
  38. byte expandedKeyMaterialSize, short effectiveKeyBytes,
  39. byte ivSize, byte blockSize)
  40. :base(code, name, cipherAlgorithmType, hashAlgorithmType,
  41. exchangeAlgorithmType, exportable, blockMode, keyMaterialSize,
  42. expandedKeyMaterialSize, effectiveKeyBytes, ivSize, blockSize)
  43. {
  44. }
  45. #endregion
  46. #region MAC Generation Methods
  47. public override byte[] ComputeServerRecordMAC(ContentType contentType, byte[] fragment)
  48. {
  49. if (header == null)
  50. header = new byte [MacHeaderLength];
  51. ulong seqnum = (Context is ClientContext) ? Context.ReadSequenceNumber : Context.WriteSequenceNumber;
  52. Write (header, 0, seqnum);
  53. header [8] = (byte) contentType;
  54. Write (header, 9, this.Context.Protocol);
  55. Write (header, 11, (short)fragment.Length);
  56. HashAlgorithm mac = this.ServerHMAC;
  57. mac.TransformBlock (header, 0, header.Length, header, 0);
  58. mac.TransformBlock (fragment, 0, fragment.Length, fragment, 0);
  59. // hack, else the method will allocate a new buffer of the same length (negative half the optimization)
  60. mac.TransformFinalBlock (CipherSuite.EmptyArray, 0, 0);
  61. return mac.Hash;
  62. }
  63. public override byte[] ComputeClientRecordMAC(ContentType contentType, byte[] fragment)
  64. {
  65. if (header == null)
  66. header = new byte [MacHeaderLength];
  67. ulong seqnum = (Context is ClientContext) ? Context.WriteSequenceNumber : Context.ReadSequenceNumber;
  68. Write (header, 0, seqnum);
  69. header [8] = (byte) contentType;
  70. Write (header, 9, this.Context.Protocol);
  71. Write (header, 11, (short)fragment.Length);
  72. HashAlgorithm mac = this.ClientHMAC;
  73. mac.TransformBlock (header, 0, header.Length, header, 0);
  74. mac.TransformBlock (fragment, 0, fragment.Length, fragment, 0);
  75. // hack, else the method will allocate a new buffer of the same length (negative half the optimization)
  76. mac.TransformFinalBlock (CipherSuite.EmptyArray, 0, 0);
  77. return mac.Hash;
  78. }
  79. #endregion
  80. #region Key Generation Methods
  81. public override void ComputeMasterSecret(byte[] preMasterSecret)
  82. {
  83. // Create master secret
  84. this.Context.MasterSecret = new byte[preMasterSecret.Length];
  85. this.Context.MasterSecret = this.PRF(
  86. preMasterSecret, "master secret", this.Context.RandomCS, 48);
  87. DebugHelper.WriteLine(">>>> MasterSecret", this.Context.MasterSecret);
  88. }
  89. public override void ComputeKeys()
  90. {
  91. // Create keyblock
  92. TlsStream keyBlock = new TlsStream(
  93. this.PRF(
  94. this.Context.MasterSecret,
  95. "key expansion",
  96. this.Context.RandomSC,
  97. this.KeyBlockSize));
  98. this.Context.Negotiating.ClientWriteMAC = keyBlock.ReadBytes(this.HashSize);
  99. this.Context.Negotiating.ServerWriteMAC = keyBlock.ReadBytes(this.HashSize);
  100. this.Context.ClientWriteKey = keyBlock.ReadBytes(this.KeyMaterialSize);
  101. this.Context.ServerWriteKey = keyBlock.ReadBytes(this.KeyMaterialSize);
  102. if (!this.IsExportable)
  103. {
  104. if (this.IvSize != 0)
  105. {
  106. this.Context.ClientWriteIV = keyBlock.ReadBytes(this.IvSize);
  107. this.Context.ServerWriteIV = keyBlock.ReadBytes(this.IvSize);
  108. }
  109. else
  110. {
  111. this.Context.ClientWriteIV = CipherSuite.EmptyArray;
  112. this.Context.ServerWriteIV = CipherSuite.EmptyArray;
  113. }
  114. }
  115. else
  116. {
  117. // Generate final write keys
  118. byte[] finalClientWriteKey = PRF(this.Context.ClientWriteKey, "client write key", this.Context.RandomCS, this.ExpandedKeyMaterialSize);
  119. byte[] finalServerWriteKey = PRF(this.Context.ServerWriteKey, "server write key", this.Context.RandomCS, this.ExpandedKeyMaterialSize);
  120. this.Context.ClientWriteKey = finalClientWriteKey;
  121. this.Context.ServerWriteKey = finalServerWriteKey;
  122. if (this.IvSize > 0)
  123. {
  124. // Generate IV block
  125. byte[] ivBlock = PRF(CipherSuite.EmptyArray, "IV block", this.Context.RandomCS, this.IvSize*2);
  126. // Generate IV keys
  127. this.Context.ClientWriteIV = new byte[this.IvSize];
  128. Buffer.BlockCopy(ivBlock, 0, this.Context.ClientWriteIV, 0, this.Context.ClientWriteIV.Length);
  129. this.Context.ServerWriteIV = new byte[this.IvSize];
  130. Buffer.BlockCopy(ivBlock, this.IvSize, this.Context.ServerWriteIV, 0, this.Context.ServerWriteIV.Length);
  131. }
  132. else
  133. {
  134. this.Context.ClientWriteIV = CipherSuite.EmptyArray;
  135. this.Context.ServerWriteIV = CipherSuite.EmptyArray;
  136. }
  137. }
  138. DebugHelper.WriteLine(">>>> KeyBlock", keyBlock.ToArray());
  139. DebugHelper.WriteLine(">>>> ClientWriteKey", this.Context.ClientWriteKey);
  140. DebugHelper.WriteLine(">>>> ClientWriteIV", this.Context.ClientWriteIV);
  141. DebugHelper.WriteLine(">>>> ClientWriteMAC", this.Context.Negotiating.ClientWriteMAC);
  142. DebugHelper.WriteLine(">>>> ServerWriteKey", this.Context.ServerWriteKey);
  143. DebugHelper.WriteLine(">>>> ServerWriteIV", this.Context.ServerWriteIV);
  144. DebugHelper.WriteLine(">>>> ServerWriteMAC", this.Context.Negotiating.ServerWriteMAC);
  145. ClientSessionCache.SetContextInCache (this.Context);
  146. // Clear no more needed data
  147. keyBlock.Reset();
  148. }
  149. #endregion
  150. }
  151. }