AsymmetricAlgorithm.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // System.Security.Cryptography.AsymmetricAlgorithm 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. using System;
  11. namespace System.Security.Cryptography {
  12. /// <summary>
  13. /// Abstract base class for all cryptographic asymmetric algorithms.
  14. /// Available algorithms include:
  15. /// RSA, DSA
  16. /// </summary>
  17. public abstract class AsymmetricAlgorithm : IDisposable {
  18. protected int KeySizeValue; // The size of the secret key used by the symmetric algorithm in bits.
  19. protected KeySizes[] LegalKeySizesValue; // Specifies the key sizes that are supported by the symmetric algorithm.
  20. /// <summary>
  21. /// Called from constructor of derived class.
  22. /// </summary>
  23. protected AsymmetricAlgorithm () {}
  24. /// <summary>
  25. /// Gets the key exchange algorithm
  26. /// </summary>
  27. public abstract string KeyExchangeAlgorithm {get;}
  28. /// <summary>
  29. /// Gets or sets the actual key size
  30. /// </summary>
  31. public virtual int KeySize {
  32. get {
  33. return this.KeySizeValue;
  34. }
  35. set {
  36. if (!IsLegalKeySize (this.LegalKeySizesValue, value))
  37. throw new CryptographicException("key size not supported by algorithm");
  38. this.KeySizeValue = value;
  39. }
  40. }
  41. /// <summary>
  42. /// Gets all legal key sizes
  43. /// </summary>
  44. public virtual KeySizes[] LegalKeySizes {
  45. get {
  46. return this.LegalKeySizesValue;
  47. }
  48. }
  49. /// <summary>
  50. /// Gets the signature algorithm
  51. /// </summary>
  52. public abstract string SignatureAlgorithm {get;}
  53. public void Dispose()
  54. {
  55. Dispose (true);
  56. GC.SuppressFinalize (this); // Finalization is now unnecessary
  57. }
  58. public void Clear()
  59. {
  60. Dispose (false);
  61. }
  62. protected abstract void Dispose (bool disposing);
  63. /* Commented to remove cyclic dependency between corlib and System.Xml
  64. // helper function for FromXmlString (used in RSA and DSA)
  65. protected byte[] GetElement (XmlDocument xml, string tag)
  66. {
  67. XmlNodeList xnl = xml.GetElementsByTagName (tag);
  68. if (xnl.Count > 0)
  69. return Convert.FromBase64String (xnl[0].InnerText);
  70. else
  71. return null;
  72. }*/
  73. /// <summary>
  74. /// Reconstructs the AsymmetricAlgorithm Object from an XML-string
  75. /// </summary>
  76. public abstract void FromXmlString(string xmlString);
  77. /// <summary>
  78. /// Returns an XML string representation the current AsymmetricAlgorithm object
  79. /// </summary>
  80. public abstract string ToXmlString(bool includePrivateParameters);
  81. private bool IsLegalKeySize(KeySizes[] LegalKeys, int Size)
  82. {
  83. foreach (KeySizes LegalKeySize in LegalKeys) {
  84. for (int i=LegalKeySize.MinSize; i<=LegalKeySize.MaxSize; i+=LegalKeySize.SkipSize) {
  85. if (i == Size)
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. /// <summary>
  92. /// Checks wether the given keyLength is valid for the current algorithm
  93. /// </summary>
  94. /// <param name="bitLength">the given keyLength</param>
  95. public bool ValidKeySize(int bitLength)
  96. {
  97. return IsLegalKeySize(LegalKeySizesValue, bitLength);
  98. }
  99. /// <summary>
  100. /// Creates the default implementation of the default asymmetric algorithm (RSA).
  101. /// </summary>
  102. public static AsymmetricAlgorithm Create ()
  103. {
  104. return Create ("System.Security.Cryptography.AsymmetricAlgorithm");
  105. }
  106. /// <summary>
  107. /// Creates a specific implementation of the given asymmetric algorithm.
  108. /// </summary>
  109. /// <param name="algo">Specifies which derived class to create</param>
  110. public static AsymmetricAlgorithm Create (string algName)
  111. {
  112. return (AsymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
  113. }
  114. }
  115. }