RSASslSignatureDeformatter.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 RSASslSignatureDeformatter : AsymmetricSignatureDeformatter
  28. {
  29. #region Fields
  30. private RSA key;
  31. private HashAlgorithm hash;
  32. #endregion
  33. #region Constructors
  34. public RSASslSignatureDeformatter()
  35. {
  36. }
  37. public RSASslSignatureDeformatter(AsymmetricAlgorithm key)
  38. {
  39. this.SetKey(key);
  40. }
  41. #endregion
  42. #region Methods
  43. public override bool VerifySignature(
  44. byte[] rgbHash,
  45. byte[] rgbSignature)
  46. {
  47. if (this.key == null)
  48. {
  49. throw new CryptographicUnexpectedOperationException("The key is a null reference");
  50. }
  51. if (hash == null)
  52. {
  53. throw new CryptographicUnexpectedOperationException("The hash algorithm is a null reference.");
  54. }
  55. if (rgbHash == null)
  56. {
  57. throw new ArgumentNullException("The rgbHash parameter is a null reference.");
  58. }
  59. return Mono.Security.Cryptography.PKCS1.Verify_v15(
  60. this.key,
  61. this.hash,
  62. rgbHash,
  63. rgbSignature);
  64. }
  65. public override void SetHashAlgorithm(string strName)
  66. {
  67. switch (strName)
  68. {
  69. case "MD5SHA1":
  70. this.hash = new Mono.Security.Cryptography.MD5SHA1();
  71. break;
  72. default:
  73. this.hash = HashAlgorithm.Create(strName);
  74. break;
  75. }
  76. }
  77. public override void SetKey(AsymmetricAlgorithm key)
  78. {
  79. if (!(key is RSA))
  80. {
  81. throw new ArgumentException("Specfied key is not an RSA key");
  82. }
  83. this.key = key as RSA;
  84. }
  85. #endregion
  86. }
  87. }