imported everything from my branch (which is slightly harmless).
[mono.git] / mcs / class / corlib / System.Security.Cryptography / SignatureDescription.cs
1 //
2 // System.Security.Cryptography SignatureDescription Class implementation
3 //
4 // Authors:
5 //      Thomas Neidhart (tome@sbox.tugraz.at)
6 //      Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 // Notes:
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
35
36 using System.Globalization;
37 using System.Runtime.InteropServices;
38 using System.Security;
39
40 namespace System.Security.Cryptography {
41         
42 #if NET_2_0
43 [ComVisible (true)]
44 #endif
45 public class SignatureDescription {
46
47         private string _DeformatterAlgorithm;
48         private string _DigestAlgorithm;                
49         private string _FormatterAlgorithm;             
50         private string _KeyAlgorithm;           
51
52         public SignatureDescription ()
53         {
54         }
55         
56         /// LAMESPEC: ArgumentNullException is thrown (not CryptographicException)
57         public SignatureDescription (SecurityElement el) 
58         {
59                 if (el == null)
60                         throw new ArgumentNullException ("el");
61
62                 // thanksfully documented in VS.NET 2005
63                 SecurityElement child = el.SearchForChildByTag ("Deformatter");
64                 _DeformatterAlgorithm = ((child == null) ? null : child.Text);
65
66                 child = el.SearchForChildByTag ("Digest");
67                 _DigestAlgorithm = ((child == null) ? null : child.Text);
68
69                 child = el.SearchForChildByTag ("Formatter");
70                 _FormatterAlgorithm = ((child == null) ? null : child.Text);
71
72                 child = el.SearchForChildByTag ("Key");
73                 _KeyAlgorithm = ((child == null) ? null : child.Text);
74         }
75
76         // There are no validation of the property
77         public string DeformatterAlgorithm {
78                 get { return _DeformatterAlgorithm; }
79                 set { _DeformatterAlgorithm = value; }
80         }
81
82         // There are no validation of the property
83         public string DigestAlgorithm {
84                 get { return _DigestAlgorithm; }
85                 set { _DigestAlgorithm = value; }
86         }
87
88         // There are no validation of the property
89         public string FormatterAlgorithm {
90                 get { return _FormatterAlgorithm; }
91                 set { _FormatterAlgorithm = value; }
92         }
93
94         // There are no validation of the property
95         public string KeyAlgorithm {
96                 get { return _KeyAlgorithm; }
97                 set { _KeyAlgorithm = value; }
98         }
99
100         public virtual AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key) 
101         {
102                 if (_DeformatterAlgorithm == null)
103                         throw new ArgumentNullException ("DeformatterAlgorithm");
104
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);
108
109                 if (_KeyAlgorithm == null)
110                         throw new NullReferenceException ("KeyAlgorithm");
111
112                 def.SetKey (key);
113                 return def;
114         }
115         
116         public virtual HashAlgorithm CreateDigest ()
117         {
118                 if (_DigestAlgorithm == null)
119                         throw new ArgumentNullException ("DigestAlgorithm");
120
121                 return (HashAlgorithm) CryptoConfig.CreateFromName (_DigestAlgorithm);
122         }
123
124         public virtual AsymmetricSignatureFormatter CreateFormatter (AsymmetricAlgorithm key)
125         {
126                 if (_FormatterAlgorithm == null)
127                         throw new ArgumentNullException ("FormatterAlgorithm");
128
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);
132
133                 if (_KeyAlgorithm == null)
134                         throw new NullReferenceException ("KeyAlgorithm");
135
136                 fmt.SetKey (key);
137                 return fmt;
138         }
139 }
140
141 internal class DSASignatureDescription : SignatureDescription {
142         public DSASignatureDescription () 
143         {
144                 DeformatterAlgorithm = "System.Security.Cryptography.DSASignatureDeformatter";
145                 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
146                 FormatterAlgorithm = "System.Security.Cryptography.DSASignatureFormatter";              
147                 KeyAlgorithm = "System.Security.Cryptography.DSACryptoServiceProvider";         
148         }
149 }
150
151 internal class RSAPKCS1SHA1SignatureDescription : SignatureDescription {
152         public RSAPKCS1SHA1SignatureDescription () 
153         {
154                 DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
155                 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
156                 FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter";         
157                 KeyAlgorithm = "System.Security.Cryptography.RSACryptoServiceProvider";         
158         }
159
160         public override AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key) 
161         {
162                 // just to please corcompare
163                 return base.CreateDeformatter (key);
164         }
165 }
166         
167 }