2 // System.Security.Cryptography SignatureDescription Class implementation
5 // Thomas Neidhart (tome@sbox.tugraz.at)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 // There seems to be some (internal?) class inheriting from SignatureDescription
33 // 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
34 // Those 2 classes are returned by CryptoConfig.CreateFromName and used in XMLDSIG
36 using System.Globalization;
37 using System.Runtime.InteropServices;
38 using System.Security;
40 namespace System.Security.Cryptography {
45 public class SignatureDescription {
47 private string _DeformatterAlgorithm;
48 private string _DigestAlgorithm;
49 private string _FormatterAlgorithm;
50 private string _KeyAlgorithm;
52 public SignatureDescription ()
56 /// LAMESPEC: ArgumentNullException is thrown (not CryptographicException)
57 public SignatureDescription (SecurityElement el)
60 throw new ArgumentNullException ("el");
62 // thanksfully documented in VS.NET 2005
63 SecurityElement child = el.SearchForChildByTag ("Deformatter");
64 _DeformatterAlgorithm = ((child == null) ? null : child.Text);
66 child = el.SearchForChildByTag ("Digest");
67 _DigestAlgorithm = ((child == null) ? null : child.Text);
69 child = el.SearchForChildByTag ("Formatter");
70 _FormatterAlgorithm = ((child == null) ? null : child.Text);
72 child = el.SearchForChildByTag ("Key");
73 _KeyAlgorithm = ((child == null) ? null : child.Text);
76 // There are no validation of the property
77 public string DeformatterAlgorithm {
78 get { return _DeformatterAlgorithm; }
79 set { _DeformatterAlgorithm = value; }
82 // There are no validation of the property
83 public string DigestAlgorithm {
84 get { return _DigestAlgorithm; }
85 set { _DigestAlgorithm = value; }
88 // There are no validation of the property
89 public string FormatterAlgorithm {
90 get { return _FormatterAlgorithm; }
91 set { _FormatterAlgorithm = value; }
94 // There are no validation of the property
95 public string KeyAlgorithm {
96 get { return _KeyAlgorithm; }
97 set { _KeyAlgorithm = value; }
100 public virtual AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key)
102 if (_DeformatterAlgorithm == null)
103 throw new ArgumentNullException ("DeformatterAlgorithm");
105 // this should throw the InvalidCastException if we have an invalid class
106 // (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
107 AsymmetricSignatureDeformatter def = (AsymmetricSignatureDeformatter) CryptoConfig.CreateFromName (_DeformatterAlgorithm);
109 if (_KeyAlgorithm == null)
110 throw new NullReferenceException ("KeyAlgorithm");
116 public virtual HashAlgorithm CreateDigest ()
118 if (_DigestAlgorithm == null)
119 throw new ArgumentNullException ("DigestAlgorithm");
121 return (HashAlgorithm) CryptoConfig.CreateFromName (_DigestAlgorithm);
124 public virtual AsymmetricSignatureFormatter CreateFormatter (AsymmetricAlgorithm key)
126 if (_FormatterAlgorithm == null)
127 throw new ArgumentNullException ("FormatterAlgorithm");
129 // this should throw the InvalidCastException if we have an invalid class
130 // (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
131 AsymmetricSignatureFormatter fmt = (AsymmetricSignatureFormatter) CryptoConfig.CreateFromName (_FormatterAlgorithm);
133 if (_KeyAlgorithm == null)
134 throw new NullReferenceException ("KeyAlgorithm");
141 internal class DSASignatureDescription : SignatureDescription {
142 public DSASignatureDescription ()
144 DeformatterAlgorithm = "System.Security.Cryptography.DSASignatureDeformatter";
145 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
146 FormatterAlgorithm = "System.Security.Cryptography.DSASignatureFormatter";
147 KeyAlgorithm = "System.Security.Cryptography.DSACryptoServiceProvider";
151 internal class RSAPKCS1SHA1SignatureDescription : SignatureDescription {
152 public RSAPKCS1SHA1SignatureDescription ()
154 DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
155 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
156 FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter";
157 KeyAlgorithm = "System.Security.Cryptography.RSACryptoServiceProvider";
160 public override AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key)
162 // just to please corcompare
163 return base.CreateDeformatter (key);