TlsClientCertificateVerify.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.X509Certificates;
  25. using System.Security.Cryptography;
  26. using Mono.Security.Cryptography;
  27. namespace Mono.Security.Protocol.Tls.Handshake.Server
  28. {
  29. internal class TlsClientCertificateVerify : HandshakeMessage
  30. {
  31. #region Constructors
  32. public TlsClientCertificateVerify(Context context, byte[] buffer)
  33. : base(context, HandshakeType.CertificateVerify, buffer)
  34. {
  35. }
  36. #endregion
  37. #region Protected Methods
  38. protected override void ProcessAsSsl3()
  39. {
  40. ServerContext context = (ServerContext)this.Context;
  41. int length = this.ReadInt16 ();
  42. byte[] signature = this.ReadBytes (length);
  43. // Verify signature
  44. SslHandshakeHash hash = new SslHandshakeHash(context.MasterSecret);
  45. hash.TransformFinalBlock(
  46. context.HandshakeMessages.ToArray(),
  47. 0,
  48. (int)context.HandshakeMessages.Length);
  49. if (!hash.VerifySignature(context.ClientSettings.CertificateRSA, signature))
  50. {
  51. throw new TlsException(AlertDescription.HandshakeFailiure, "Handshake Failure.");
  52. }
  53. }
  54. protected override void ProcessAsTls1()
  55. {
  56. ServerContext context = (ServerContext)this.Context;
  57. int length = this.ReadInt16 ();
  58. byte[] signature = this.ReadBytes (length);
  59. // Verify signature
  60. MD5SHA1 hash = new MD5SHA1();
  61. hash.ComputeHash(
  62. context.HandshakeMessages.ToArray(),
  63. 0,
  64. (int)context.HandshakeMessages.Length);
  65. if (!hash.VerifySignature(context.ClientSettings.CertificateRSA, signature))
  66. {
  67. throw new TlsException (AlertDescription.HandshakeFailiure, "Handshake Failure.");
  68. }
  69. }
  70. #endregion
  71. }
  72. }