TlsClientKeyExchange.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.Security.Cryptography.X509Certificates;
  28. namespace Mono.Security.Protocol.Tls.Handshake.Server
  29. {
  30. internal class TlsClientKeyExchange : HandshakeMessage
  31. {
  32. #region Constructors
  33. public TlsClientKeyExchange(Context context, byte[] buffer) :
  34. base(context,
  35. HandshakeType.ClientKeyExchange,
  36. buffer)
  37. {
  38. }
  39. #endregion
  40. #region Protected Methods
  41. protected override void ProcessAsSsl3()
  42. {
  43. AsymmetricAlgorithm privKey = null;
  44. ServerContext context = (ServerContext)this.Context;
  45. // Select the private key information
  46. privKey = context.SslStream.RaisePrivateKeySelection(
  47. new X509Certificate(context.ServerSettings.Certificates[0].RawData),
  48. null);
  49. if (privKey == null)
  50. {
  51. throw new TlsException(AlertDescription.UserCancelled, "Server certificate Private Key unavailable.");
  52. }
  53. // Read client premaster secret
  54. byte[] clientSecret = this.ReadBytes((int)this.Length);
  55. // Decrypt premaster secret
  56. RSAPKCS1KeyExchangeDeformatter deformatter = new RSAPKCS1KeyExchangeDeformatter(privKey);
  57. byte[] preMasterSecret = deformatter.DecryptKeyExchange(clientSecret);
  58. // Create master secret
  59. this.Context.Negotiating.Cipher.ComputeMasterSecret(preMasterSecret);
  60. // Create keys
  61. this.Context.Negotiating.Cipher.ComputeKeys ();
  62. // Initialize Cipher Suite
  63. this.Context.Negotiating.Cipher.InitializeCipher ();
  64. }
  65. protected override void ProcessAsTls1()
  66. {
  67. AsymmetricAlgorithm privKey = null;
  68. ServerContext context = (ServerContext)this.Context;
  69. // Select the private key information
  70. // Select the private key information
  71. privKey = context.SslStream.RaisePrivateKeySelection(
  72. new X509Certificate(context.ServerSettings.Certificates[0].RawData),
  73. null);
  74. if (privKey == null)
  75. {
  76. throw new TlsException(AlertDescription.UserCancelled, "Server certificate Private Key unavailable.");
  77. }
  78. // Read client premaster secret
  79. byte[] clientSecret = this.ReadBytes(this.ReadInt16());
  80. // Decrypt premaster secret
  81. RSAPKCS1KeyExchangeDeformatter deformatter = new RSAPKCS1KeyExchangeDeformatter(privKey);
  82. byte[] preMasterSecret = deformatter.DecryptKeyExchange(clientSecret);
  83. // Create master secret
  84. this.Context.Negotiating.Cipher.ComputeMasterSecret(preMasterSecret);
  85. // Create keys
  86. this.Context.Negotiating.Cipher.ComputeKeys();
  87. // Initialize Cipher Suite
  88. this.Context.Negotiating.Cipher.InitializeCipher();
  89. }
  90. #endregion
  91. }
  92. }