AsymmetricAlgorithm.cs 3.3 KB

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