[runtime] Overwrite stacktrace for exception on re-throw. Fixes #1856.
[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 [ComVisible (true)]
43 public class SignatureDescription {
44
45         private string _DeformatterAlgorithm;
46         private string _DigestAlgorithm;                
47         private string _FormatterAlgorithm;             
48         private string _KeyAlgorithm;           
49
50         public SignatureDescription ()
51         {
52         }
53         
54         /// LAMESPEC: ArgumentNullException is thrown (not CryptographicException)
55         public SignatureDescription (SecurityElement el) 
56         {
57                 if (el == null)
58                         throw new ArgumentNullException ("el");
59
60                 // thanksfully documented in VS.NET 2005
61                 SecurityElement child = el.SearchForChildByTag ("Deformatter");
62                 _DeformatterAlgorithm = ((child == null) ? null : child.Text);
63
64                 child = el.SearchForChildByTag ("Digest");
65                 _DigestAlgorithm = ((child == null) ? null : child.Text);
66
67                 child = el.SearchForChildByTag ("Formatter");
68                 _FormatterAlgorithm = ((child == null) ? null : child.Text);
69
70                 child = el.SearchForChildByTag ("Key");
71                 _KeyAlgorithm = ((child == null) ? null : child.Text);
72         }
73
74         // There are no validation of the property
75         public string DeformatterAlgorithm {
76                 get { return _DeformatterAlgorithm; }
77                 set { _DeformatterAlgorithm = value; }
78         }
79
80         // There are no validation of the property
81         public string DigestAlgorithm {
82                 get { return _DigestAlgorithm; }
83                 set { _DigestAlgorithm = value; }
84         }
85
86         // There are no validation of the property
87         public string FormatterAlgorithm {
88                 get { return _FormatterAlgorithm; }
89                 set { _FormatterAlgorithm = value; }
90         }
91
92         // There are no validation of the property
93         public string KeyAlgorithm {
94                 get { return _KeyAlgorithm; }
95                 set { _KeyAlgorithm = value; }
96         }
97
98         public virtual AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key) 
99         {
100                 if (_DeformatterAlgorithm == null)
101                         throw new ArgumentNullException ("DeformatterAlgorithm");
102
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);
106
107                 if (_KeyAlgorithm == null)
108                         throw new NullReferenceException ("KeyAlgorithm");
109
110                 def.SetKey (key);
111                 return def;
112         }
113         
114         public virtual HashAlgorithm CreateDigest ()
115         {
116                 if (_DigestAlgorithm == null)
117                         throw new ArgumentNullException ("DigestAlgorithm");
118
119                 return (HashAlgorithm) CryptoConfig.CreateFromName (_DigestAlgorithm);
120         }
121
122         public virtual AsymmetricSignatureFormatter CreateFormatter (AsymmetricAlgorithm key)
123         {
124                 if (_FormatterAlgorithm == null)
125                         throw new ArgumentNullException ("FormatterAlgorithm");
126
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);
130
131                 if (_KeyAlgorithm == null)
132                         throw new NullReferenceException ("KeyAlgorithm");
133
134                 fmt.SetKey (key);
135                 return fmt;
136         }
137 }
138
139 internal class DSASignatureDescription : SignatureDescription {
140         public DSASignatureDescription () 
141         {
142                 DeformatterAlgorithm = "System.Security.Cryptography.DSASignatureDeformatter";
143                 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
144                 FormatterAlgorithm = "System.Security.Cryptography.DSASignatureFormatter";              
145                 KeyAlgorithm = "System.Security.Cryptography.DSACryptoServiceProvider";         
146         }
147 }
148
149 internal class RSAPKCS1SHA1SignatureDescription : SignatureDescription {
150         public RSAPKCS1SHA1SignatureDescription () 
151         {
152                 DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
153                 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
154                 FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter";         
155                 KeyAlgorithm = "System.Security.Cryptography.RSACryptoServiceProvider";         
156         }
157
158         public override AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key) 
159         {
160                 // just to please corcompare
161                 return base.CreateDeformatter (key);
162         }
163 }
164         
165 }