2007-04-24 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / corlib / System.Security.Cryptography / AsymmetricAlgorithm.cs
old mode 100755 (executable)
new mode 100644 (file)
index 651906b..9447908
-//\r
-// System.Security.Cryptography.AsymmetricAlgorithm Class implementation\r
-//\r
-// Authors:\r
-//   Thomas Neidhart (tome@sbox.tugraz.at)\r
-//   Sebastien Pouliot (spouliot@motus.com)\r
-//\r
-// Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)\r
-//\r
-\r
-using System;\r
-using System.Xml;\r
-\r
-namespace System.Security.Cryptography {\r
-\r
-       /// <summary>\r
-       /// Abstract base class for all cryptographic asymmetric algorithms.\r
-       /// Available algorithms include:\r
-       /// RSA, DSA\r
-       /// </summary>\r
-       public abstract class AsymmetricAlgorithm : IDisposable {\r
-\r
-               protected int KeySizeValue; // The size of the secret key used by the symmetric algorithm in bits. \r
-               protected KeySizes[] LegalKeySizesValue; // Specifies the key sizes that are supported by the symmetric algorithm. \r
-\r
-               /// <summary>\r
-               /// Called from constructor of derived class.\r
-               /// </summary>\r
-               protected AsymmetricAlgorithm () {}\r
-               \r
-               /// <summary>\r
-               /// Gets the key exchange algorithm\r
-               /// </summary>\r
-               public abstract string KeyExchangeAlgorithm {get;}\r
-               \r
-               /// <summary>\r
-               /// Gets or sets the actual key size\r
-               /// </summary>\r
-               public virtual int KeySize {\r
-                       get {\r
-                               return this.KeySizeValue;\r
-                       }\r
-                       set {\r
-                               if (!IsLegalKeySize(this.LegalKeySizesValue, value))\r
-                                       throw new CryptographicException("key size not supported by algorithm");\r
-                               \r
-                               this.KeySizeValue = value;\r
-                       }\r
-               }\r
-\r
-               /// <summary>\r
-               /// Gets all legal key sizes\r
-               /// </summary>\r
-               public virtual KeySizes[] LegalKeySizes {\r
-                       get {\r
-                               return this.LegalKeySizesValue;\r
-                       }\r
-               }\r
-\r
-               /// <summary>\r
-               /// Gets the signature algorithm\r
-               /// </summary>\r
-               public abstract string SignatureAlgorithm {get;}\r
-\r
-               void System.IDisposable.Dispose() \r
-               {\r
-               }\r
-\r
-               public void Clear() \r
-               {\r
-//                     Dispose();\r
-               }\r
-\r
-               protected abstract void Dispose (bool disposing);\r
-\r
-               // helper function for FromXmlString (used in RSA and DSA)\r
-               protected byte[] GetElement (XmlDocument xml, string tag) \r
-               {\r
-                       XmlNodeList xnl = xml.GetElementsByTagName (tag);\r
-                       if (xnl.Count > 0)\r
-                               return Convert.FromBase64String (xnl[0].InnerText);\r
-                       else\r
-                               return null;\r
-               }\r
-\r
-               /// <summary>\r
-               /// Reconstructs the AsymmetricAlgorithm Object from an XML-string\r
-               /// </summary>\r
-               public abstract void FromXmlString(string xmlString);\r
-               \r
-               /// <summary>\r
-               /// Returns an XML string representation the current AsymmetricAlgorithm object\r
-               /// </summary>\r
-               public abstract string ToXmlString(bool includePrivateParameters);              \r
-               \r
-               private bool IsLegalKeySize(KeySizes[] LegalKeys, int Size) \r
-               {\r
-                       foreach (KeySizes LegalKeySize in LegalKeys) {\r
-                               for (int i=LegalKeySize.MinSize; i<=LegalKeySize.MaxSize; i+=LegalKeySize.SkipSize) {\r
-                                       if (i == Size)\r
-                                               return true;\r
-                               }\r
-                       }\r
-                       return false;\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Checks wether the given keyLength is valid for the current algorithm\r
-               /// </summary>\r
-               /// <param name="bitLength">the given keyLength</param>\r
-               public bool ValidKeySize(int bitLength) \r
-               {\r
-                       return IsLegalKeySize(LegalKeySizesValue, bitLength);\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Creates the default implementation of the default asymmetric algorithm (RSA).\r
-               /// </summary>\r
-               public static AsymmetricAlgorithm Create () \r
-               {\r
-                       return Create ("System.Security.Cryptography.AsymmetricAlgorithm");\r
-               }\r
-       \r
-               /// <summary>\r
-               /// Creates a specific implementation of the given asymmetric algorithm.\r
-               /// </summary>\r
-               /// <param name="algo">Specifies which derived class to create</param>\r
-               public static AsymmetricAlgorithm Create (string algName) \r
-               {\r
-                       return (AsymmetricAlgorithm) CryptoConfig.CreateFromName (algName);\r
-               }\r
-       }\r
-}\r
-\r
+//
+// System.Security.Cryptography.AsymmetricAlgorithm Class implementation
+//
+// Authors:
+//     Thomas Neidhart (tome@sbox.tugraz.at)
+//     Sebastien Pouliot (sebastien@ximian.com)
+//
+// Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System.Globalization;
+using System.Runtime.InteropServices;
+
+namespace System.Security.Cryptography {
+
+#if NET_2_0
+       [ComVisible (true)]
+#endif
+       public abstract class AsymmetricAlgorithm : IDisposable {
+
+               protected int KeySizeValue;
+               protected KeySizes[] LegalKeySizesValue; 
+
+               protected AsymmetricAlgorithm ()
+               {
+               }
+               
+               public abstract string KeyExchangeAlgorithm {
+                       get;
+               }
+               
+               public virtual int KeySize {
+                       get { return this.KeySizeValue; }
+                       set {
+                               if (!KeySizes.IsLegalKeySize (this.LegalKeySizesValue, value))
+                                       throw new CryptographicException (Locale.GetText ("Key size not supported by algorithm."));
+                               
+                               this.KeySizeValue = value;
+                       }
+               }
+
+               public virtual KeySizes[] LegalKeySizes {
+                       get { return this.LegalKeySizesValue; }
+               }
+
+               public abstract string SignatureAlgorithm {
+                       get;
+               }
+
+               void IDisposable.Dispose () 
+               {
+                       Dispose (true);
+                       GC.SuppressFinalize (this);  // Finalization is now unnecessary
+               }
+
+               public void Clear () 
+               {
+                       Dispose (false);
+               }
+
+               protected abstract void Dispose (bool disposing);
+
+               public abstract void FromXmlString (string xmlString);
+               
+               public abstract string ToXmlString (bool includePrivateParameters);             
+               
+               public static AsymmetricAlgorithm Create () 
+               {
+                       return Create ("System.Security.Cryptography.AsymmetricAlgorithm");
+               }
+       
+               public static AsymmetricAlgorithm Create (string algName) 
+               {
+                       return (AsymmetricAlgorithm) CryptoConfig.CreateFromName (algName);
+               }
+       }
+}