//
// System.Security.Cryptography.AsymmetricAlgorithm Class implementation
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
// Sebastien Pouliot (spouliot@motus.com)
//
// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
//
using System;
namespace System.Security.Cryptography {
///
/// Abstract base class for all cryptographic asymmetric algorithms.
/// Available algorithms include:
/// RSA, DSA
///
public abstract class AsymmetricAlgorithm : IDisposable {
protected int KeySizeValue; // The size of the secret key used by the symmetric algorithm in bits.
protected KeySizes[] LegalKeySizesValue; // Specifies the key sizes that are supported by the symmetric algorithm.
///
/// Called from constructor of derived class.
///
protected AsymmetricAlgorithm () {}
///
/// Gets the key exchange algorithm
///
public abstract string KeyExchangeAlgorithm {get;}
///
/// Gets or sets the actual key size
///
public virtual int KeySize {
get {
return this.KeySizeValue;
}
set {
if (!IsLegalKeySize (this.LegalKeySizesValue, value))
throw new CryptographicException("key size not supported by algorithm");
this.KeySizeValue = value;
}
}
///
/// Gets all legal key sizes
///
public virtual KeySizes[] LegalKeySizes {
get {
return this.LegalKeySizesValue;
}
}
///
/// Gets the signature algorithm
///
public abstract string SignatureAlgorithm {get;}
public void Dispose()
{
Dispose (true);
GC.SuppressFinalize (this); // Finalization is now unnecessary
}
public void Clear()
{
Dispose (false);
}
protected abstract void Dispose (bool disposing);
/* Commented to remove cyclic dependency between corlib and System.Xml
// helper function for FromXmlString (used in RSA and DSA)
protected byte[] GetElement (XmlDocument xml, string tag)
{
XmlNodeList xnl = xml.GetElementsByTagName (tag);
if (xnl.Count > 0)
return Convert.FromBase64String (xnl[0].InnerText);
else
return null;
}*/
///
/// Reconstructs the AsymmetricAlgorithm Object from an XML-string
///
public abstract void FromXmlString(string xmlString);
///
/// Returns an XML string representation the current AsymmetricAlgorithm object
///
public abstract string ToXmlString(bool includePrivateParameters);
private bool IsLegalKeySize(KeySizes[] LegalKeys, int Size)
{
foreach (KeySizes LegalKeySize in LegalKeys) {
for (int i=LegalKeySize.MinSize; i<=LegalKeySize.MaxSize; i+=LegalKeySize.SkipSize) {
if (i == Size)
return true;
}
}
return false;
}
///
/// Checks wether the given keyLength is valid for the current algorithm
///
/// the given keyLength
public bool ValidKeySize(int bitLength)
{
return IsLegalKeySize(LegalKeySizesValue, bitLength);
}
///
/// Creates the default implementation of the default asymmetric algorithm (RSA).
///
public static AsymmetricAlgorithm Create ()
{
return Create ("System.Security.Cryptography.AsymmetricAlgorithm");
}
///
/// Creates a specific implementation of the given asymmetric algorithm.
///
/// Specifies which derived class to create
public static AsymmetricAlgorithm Create (string algName)
{
return (AsymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
}
}
}