SignatureDescription.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // System.Security.Cryptography SignatureDescription Class implementation
  3. //
  4. // Authors:
  5. // Thomas Neidhart ([email protected])
  6. // Sebastien Pouliot ([email protected])
  7. //
  8. // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  9. //
  10. // Notes:
  11. // There seems to be some (internal?) class inheriting from SignatureDescription
  12. // http://www.csharpfriends.com/Members/Main/Classes/get_class.aspx?assembly=mscorlib,%20Version=1.0.3300.0,%20Culture=neutral,%20PublicKeyToken=b77a5c561934e089&namespace=System.Security.Cryptography&class=SignatureDescription
  13. // Those 2 classes are returned by CryptoConfig.CreateFromName and used in XMLDSIG
  14. using System;
  15. using System.Security;
  16. namespace System.Security.Cryptography {
  17. public class SignatureDescription {
  18. private string _DeformatterAlgorithm;
  19. private string _DigestAlgorithm;
  20. private string _FormatterAlgorithm;
  21. private string _KeyAlgorithm;
  22. public SignatureDescription () {}
  23. /// LAMESPEC: ArgumentNullException is thrown (not CryptographicException)
  24. [MonoTODO("Parse SecurityElement")]
  25. public SignatureDescription (SecurityElement el)
  26. {
  27. if (el == null)
  28. throw new ArgumentNullException ();
  29. // TODO: Parse the SecurityElement
  30. // Clearly it must contains Deformatter, Digest,
  31. // Formatter and KeyAlgorithm...
  32. // But what do the SecurityElement looks like ?
  33. }
  34. // There are no validation of the property
  35. public string DeformatterAlgorithm {
  36. get { return _DeformatterAlgorithm; }
  37. set { _DeformatterAlgorithm = value; }
  38. }
  39. // There are no validation of the property
  40. public string DigestAlgorithm {
  41. get { return _DigestAlgorithm; }
  42. set { _DigestAlgorithm = value; }
  43. }
  44. // There are no validation of the property
  45. public string FormatterAlgorithm {
  46. get { return _FormatterAlgorithm; }
  47. set { _FormatterAlgorithm = value; }
  48. }
  49. // There are no validation of the property
  50. public string KeyAlgorithm {
  51. get { return _KeyAlgorithm; }
  52. set { _KeyAlgorithm = value; }
  53. }
  54. public virtual AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key)
  55. {
  56. if (_DeformatterAlgorithm == null)
  57. throw new ArgumentNullException ("DeformatterAlgorithm");
  58. // this should throw the InvalidCastException if we have an invalid class
  59. // (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
  60. AsymmetricSignatureDeformatter def = (AsymmetricSignatureDeformatter) CryptoConfig.CreateFromName (_DeformatterAlgorithm);
  61. if (_KeyAlgorithm == null)
  62. throw new NullReferenceException ("KeyAlgorithm");
  63. def.SetKey (key);
  64. return def;
  65. }
  66. /// <summary>
  67. /// Create the hash algorithm assigned with this object
  68. /// </summary>
  69. public virtual HashAlgorithm CreateDigest ()
  70. {
  71. if (_DigestAlgorithm == null)
  72. throw new ArgumentNullException ("DigestAlgorithm");
  73. return (HashAlgorithm) CryptoConfig.CreateFromName (_DigestAlgorithm);
  74. }
  75. public virtual AsymmetricSignatureFormatter CreateFormatter (AsymmetricAlgorithm key)
  76. {
  77. if (_FormatterAlgorithm == null)
  78. throw new ArgumentNullException ("FormatterAlgorithm");
  79. // this should throw the InvalidCastException if we have an invalid class
  80. // (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
  81. AsymmetricSignatureFormatter fmt = (AsymmetricSignatureFormatter) CryptoConfig.CreateFromName (_FormatterAlgorithm);
  82. if (_KeyAlgorithm == null)
  83. throw new NullReferenceException ("KeyAlgorithm");
  84. fmt.SetKey (key);
  85. return fmt;
  86. }
  87. } // SignatureDescription
  88. internal class DSASignatureDescription : SignatureDescription {
  89. public DSASignatureDescription ()
  90. {
  91. DeformatterAlgorithm = "System.Security.Cryptography.DSASignatureDeformatter";
  92. DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
  93. FormatterAlgorithm = "System.Security.Cryptography.DSASignatureFormatter";
  94. KeyAlgorithm = "System.Security.Cryptography.DSACryptoServiceProvider";
  95. }
  96. }
  97. internal class RSAPKCS1SHA1SignatureDescription : SignatureDescription {
  98. public RSAPKCS1SHA1SignatureDescription ()
  99. {
  100. DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
  101. DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
  102. FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter";
  103. KeyAlgorithm = "System.Security.Cryptography.RSACryptoServiceProvider";
  104. }
  105. public override AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key)
  106. {
  107. // just to please corcompare
  108. return base.CreateDeformatter (key);
  109. }
  110. }
  111. } // System.Security.Cryptography