TlsServerKeyExchange.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. using Mono.Security.Cryptography;
  26. using Mono.Security.X509;
  27. namespace Mono.Security.Protocol.Tls.Handshake.Client
  28. {
  29. internal class TlsServerKeyExchange : HandshakeMessage
  30. {
  31. #region Fields
  32. private RSAParameters rsaParams;
  33. private byte[] signedParams;
  34. #endregion
  35. #region Constructors
  36. public TlsServerKeyExchange(Context context, byte[] buffer)
  37. : base(context, HandshakeType.ServerKeyExchange, buffer)
  38. {
  39. this.verifySignature();
  40. }
  41. #endregion
  42. #region Methods
  43. public override void Update()
  44. {
  45. base.Update();
  46. this.Context.ServerSettings.ServerKeyExchange = true;
  47. this.Context.ServerSettings.RsaParameters = this.rsaParams;
  48. this.Context.ServerSettings.SignedParams = this.signedParams;
  49. }
  50. #endregion
  51. #region Protected Methods
  52. protected override void ProcessAsSsl3()
  53. {
  54. this.ProcessAsTls1();
  55. }
  56. protected override void ProcessAsTls1()
  57. {
  58. this.rsaParams = new RSAParameters();
  59. // Read modulus
  60. this.rsaParams.Modulus = this.ReadBytes(this.ReadInt16());
  61. // Read exponent
  62. this.rsaParams.Exponent = this.ReadBytes(this.ReadInt16());
  63. // Read signed params
  64. this.signedParams = this.ReadBytes(this.ReadInt16());
  65. }
  66. #endregion
  67. #region Private Methods
  68. private void verifySignature()
  69. {
  70. MD5SHA1 hash = new MD5SHA1();
  71. // Calculate size of server params
  72. int size = rsaParams.Modulus.Length + rsaParams.Exponent.Length + 4;
  73. // Create server params array
  74. TlsStream stream = new TlsStream();
  75. stream.Write(this.Context.RandomCS);
  76. stream.Write(this.ToArray(), 0, size);
  77. hash.ComputeHash(stream.ToArray());
  78. stream.Reset();
  79. bool isValidSignature = hash.VerifySignature(
  80. this.Context.ServerSettings.CertificateRSA,
  81. this.signedParams);
  82. if (!isValidSignature)
  83. {
  84. throw new TlsException(
  85. AlertDescription.DecodeError,
  86. "Data was not signed with the server certificate.");
  87. }
  88. }
  89. #endregion
  90. }
  91. }