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 // (C) 2004 Novell (http://www.novell.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 // There seems to be some (internal?) class inheriting from SignatureDescription
37 // 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
38 // Those 2 classes are returned by CryptoConfig.CreateFromName and used in XMLDSIG
41 using System.Globalization;
42 using System.Security;
44 namespace System.Security.Cryptography {
46 public class SignatureDescription {
48 private string _DeformatterAlgorithm;
49 private string _DigestAlgorithm;
50 private string _FormatterAlgorithm;
51 private string _KeyAlgorithm;
53 public SignatureDescription ()
57 /// LAMESPEC: ArgumentNullException is thrown (not CryptographicException)
58 public SignatureDescription (SecurityElement el)
61 throw new ArgumentNullException ("el");
63 // thanksfully documented in VS.NET 2005
64 SecurityElement child = el.SearchForChildByTag ("Deformatter");
65 _DeformatterAlgorithm = ((child == null) ? null : child.Text);
67 child = el.SearchForChildByTag ("Digest");
68 _DigestAlgorithm = ((child == null) ? null : child.Text);
70 child = el.SearchForChildByTag ("Formatter");
71 _FormatterAlgorithm = ((child == null) ? null : child.Text);
73 child = el.SearchForChildByTag ("Key");
74 _KeyAlgorithm = ((child == null) ? null : child.Text);
77 // There are no validation of the property
78 public string DeformatterAlgorithm {
79 get { return _DeformatterAlgorithm; }
80 set { _DeformatterAlgorithm = value; }
83 // There are no validation of the property
84 public string DigestAlgorithm {
85 get { return _DigestAlgorithm; }
86 set { _DigestAlgorithm = value; }
89 // There are no validation of the property
90 public string FormatterAlgorithm {
91 get { return _FormatterAlgorithm; }
92 set { _FormatterAlgorithm = value; }
95 // There are no validation of the property
96 public string KeyAlgorithm {
97 get { return _KeyAlgorithm; }
98 set { _KeyAlgorithm = value; }
101 public virtual AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key)
103 if (_DeformatterAlgorithm == null)
104 throw new ArgumentNullException ("DeformatterAlgorithm");
106 // this should throw the InvalidCastException if we have an invalid class
107 // (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
108 AsymmetricSignatureDeformatter def = (AsymmetricSignatureDeformatter) CryptoConfig.CreateFromName (_DeformatterAlgorithm);
110 if (_KeyAlgorithm == null)
111 throw new NullReferenceException ("KeyAlgorithm");
117 public virtual HashAlgorithm CreateDigest ()
119 if (_DigestAlgorithm == null)
120 throw new ArgumentNullException ("DigestAlgorithm");
122 return (HashAlgorithm) CryptoConfig.CreateFromName (_DigestAlgorithm);
125 public virtual AsymmetricSignatureFormatter CreateFormatter (AsymmetricAlgorithm key)
127 if (_FormatterAlgorithm == null)
128 throw new ArgumentNullException ("FormatterAlgorithm");
130 // this should throw the InvalidCastException if we have an invalid class
131 // (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
132 AsymmetricSignatureFormatter fmt = (AsymmetricSignatureFormatter) CryptoConfig.CreateFromName (_FormatterAlgorithm);
134 if (_KeyAlgorithm == null)
135 throw new NullReferenceException ("KeyAlgorithm");
142 internal class DSASignatureDescription : SignatureDescription {
143 public DSASignatureDescription ()
145 DeformatterAlgorithm = "System.Security.Cryptography.DSASignatureDeformatter";
146 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
147 FormatterAlgorithm = "System.Security.Cryptography.DSASignatureFormatter";
148 KeyAlgorithm = "System.Security.Cryptography.DSACryptoServiceProvider";
152 internal class RSAPKCS1SHA1SignatureDescription : SignatureDescription {
153 public RSAPKCS1SHA1SignatureDescription ()
155 DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
156 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
157 FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter";
158 KeyAlgorithm = "System.Security.Cryptography.RSACryptoServiceProvider";
161 public override AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key)
163 // just to please corcompare
164 return base.CreateDeformatter (key);