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
{
43 public class SignatureDescription
{
45 private string _DeformatterAlgorithm
;
46 private string _DigestAlgorithm
;
47 private string _FormatterAlgorithm
;
48 private string _KeyAlgorithm
;
50 public SignatureDescription ()
54 /// LAMESPEC: ArgumentNullException is thrown (not CryptographicException)
55 public SignatureDescription (SecurityElement el
)
58 throw new ArgumentNullException ("el");
60 // thanksfully documented in VS.NET 2005
61 SecurityElement child
= el
.SearchForChildByTag ("Deformatter");
62 _DeformatterAlgorithm
= ((child
== null) ? null : child
.Text
);
64 child
= el
.SearchForChildByTag ("Digest");
65 _DigestAlgorithm
= ((child
== null) ? null : child
.Text
);
67 child
= el
.SearchForChildByTag ("Formatter");
68 _FormatterAlgorithm
= ((child
== null) ? null : child
.Text
);
70 child
= el
.SearchForChildByTag ("Key");
71 _KeyAlgorithm
= ((child
== null) ? null : child
.Text
);
74 // There are no validation of the property
75 public string DeformatterAlgorithm
{
76 get { return _DeformatterAlgorithm; }
77 set { _DeformatterAlgorithm = value; }
80 // There are no validation of the property
81 public string DigestAlgorithm
{
82 get { return _DigestAlgorithm; }
83 set { _DigestAlgorithm = value; }
86 // There are no validation of the property
87 public string FormatterAlgorithm
{
88 get { return _FormatterAlgorithm; }
89 set { _FormatterAlgorithm = value; }
92 // There are no validation of the property
93 public string KeyAlgorithm
{
94 get { return _KeyAlgorithm; }
95 set { _KeyAlgorithm = value; }
98 public virtual AsymmetricSignatureDeformatter
CreateDeformatter (AsymmetricAlgorithm key
)
100 if (_DeformatterAlgorithm
== null)
101 throw new ArgumentNullException ("DeformatterAlgorithm");
103 // this should throw the InvalidCastException if we have an invalid class
104 // (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
105 AsymmetricSignatureDeformatter def
= (AsymmetricSignatureDeformatter
) CryptoConfig
.CreateFromName (_DeformatterAlgorithm
);
107 if (_KeyAlgorithm
== null)
108 throw new NullReferenceException ("KeyAlgorithm");
114 public virtual HashAlgorithm
CreateDigest ()
116 if (_DigestAlgorithm
== null)
117 throw new ArgumentNullException ("DigestAlgorithm");
119 return (HashAlgorithm
) CryptoConfig
.CreateFromName (_DigestAlgorithm
);
122 public virtual AsymmetricSignatureFormatter
CreateFormatter (AsymmetricAlgorithm key
)
124 if (_FormatterAlgorithm
== null)
125 throw new ArgumentNullException ("FormatterAlgorithm");
127 // this should throw the InvalidCastException if we have an invalid class
128 // (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
129 AsymmetricSignatureFormatter fmt
= (AsymmetricSignatureFormatter
) CryptoConfig
.CreateFromName (_FormatterAlgorithm
);
131 if (_KeyAlgorithm
== null)
132 throw new NullReferenceException ("KeyAlgorithm");
139 internal class DSASignatureDescription
: SignatureDescription
{
140 public DSASignatureDescription ()
142 DeformatterAlgorithm
= "System.Security.Cryptography.DSASignatureDeformatter";
143 DigestAlgorithm
= "System.Security.Cryptography.SHA1CryptoServiceProvider";
144 FormatterAlgorithm
= "System.Security.Cryptography.DSASignatureFormatter";
145 KeyAlgorithm
= "System.Security.Cryptography.DSACryptoServiceProvider";
149 internal class RSAPKCS1SHA1SignatureDescription
: SignatureDescription
{
150 public RSAPKCS1SHA1SignatureDescription ()
152 DeformatterAlgorithm
= "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
153 DigestAlgorithm
= "System.Security.Cryptography.SHA1CryptoServiceProvider";
154 FormatterAlgorithm
= "System.Security.Cryptography.RSAPKCS1SignatureFormatter";
155 KeyAlgorithm
= "System.Security.Cryptography.RSACryptoServiceProvider";
158 public override AsymmetricSignatureDeformatter
CreateDeformatter (AsymmetricAlgorithm key
)
160 // just to please corcompare
161 return base.CreateDeformatter (key
);