2005-06-05 Peter Bartok <pbartok@novell.com>
[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 // (C) 2004 Novell (http://www.novell.com)
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
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:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
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.
33 //
34
35 // Notes:
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
39
40 using System;
41 using System.Globalization;
42 using System.Security;
43
44 namespace System.Security.Cryptography {
45         
46 public class SignatureDescription {
47
48         private string _DeformatterAlgorithm;
49         private string _DigestAlgorithm;                
50         private string _FormatterAlgorithm;             
51         private string _KeyAlgorithm;           
52
53         public SignatureDescription ()
54         {
55         }
56         
57         /// LAMESPEC: ArgumentNullException is thrown (not CryptographicException)
58         public SignatureDescription (SecurityElement el) 
59         {
60                 if (el == null)
61                         throw new ArgumentNullException ("el");
62
63                 // thanksfully documented in VS.NET 2005
64                 SecurityElement child = el.SearchForChildByTag ("Deformatter");
65                 _DeformatterAlgorithm = ((child == null) ? null : child.Text);
66
67                 child = el.SearchForChildByTag ("Digest");
68                 _DigestAlgorithm = ((child == null) ? null : child.Text);
69
70                 child = el.SearchForChildByTag ("Formatter");
71                 _FormatterAlgorithm = ((child == null) ? null : child.Text);
72
73                 child = el.SearchForChildByTag ("Key");
74                 _KeyAlgorithm = ((child == null) ? null : child.Text);
75         }
76
77         // There are no validation of the property
78         public string DeformatterAlgorithm {
79                 get { return _DeformatterAlgorithm; }
80                 set { _DeformatterAlgorithm = value; }
81         }
82
83         // There are no validation of the property
84         public string DigestAlgorithm {
85                 get { return _DigestAlgorithm; }
86                 set { _DigestAlgorithm = value; }
87         }
88
89         // There are no validation of the property
90         public string FormatterAlgorithm {
91                 get { return _FormatterAlgorithm; }
92                 set { _FormatterAlgorithm = value; }
93         }
94
95         // There are no validation of the property
96         public string KeyAlgorithm {
97                 get { return _KeyAlgorithm; }
98                 set { _KeyAlgorithm = value; }
99         }
100
101         public virtual AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key) 
102         {
103                 if (_DeformatterAlgorithm == null)
104                         throw new ArgumentNullException ("DeformatterAlgorithm");
105
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);
109
110                 if (_KeyAlgorithm == null)
111                         throw new NullReferenceException ("KeyAlgorithm");
112
113                 def.SetKey (key);
114                 return def;
115         }
116         
117         public virtual HashAlgorithm CreateDigest ()
118         {
119                 if (_DigestAlgorithm == null)
120                         throw new ArgumentNullException ("DigestAlgorithm");
121
122                 return (HashAlgorithm) CryptoConfig.CreateFromName (_DigestAlgorithm);
123         }
124
125         public virtual AsymmetricSignatureFormatter CreateFormatter (AsymmetricAlgorithm key)
126         {
127                 if (_FormatterAlgorithm == null)
128                         throw new ArgumentNullException ("FormatterAlgorithm");
129
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);
133
134                 if (_KeyAlgorithm == null)
135                         throw new NullReferenceException ("KeyAlgorithm");
136
137                 fmt.SetKey (key);
138                 return fmt;
139         }
140 }
141
142 internal class DSASignatureDescription : SignatureDescription {
143         public DSASignatureDescription () 
144         {
145                 DeformatterAlgorithm = "System.Security.Cryptography.DSASignatureDeformatter";
146                 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
147                 FormatterAlgorithm = "System.Security.Cryptography.DSASignatureFormatter";              
148                 KeyAlgorithm = "System.Security.Cryptography.DSACryptoServiceProvider";         
149         }
150 }
151
152 internal class RSAPKCS1SHA1SignatureDescription : SignatureDescription {
153         public RSAPKCS1SHA1SignatureDescription () 
154         {
155                 DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
156                 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
157                 FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter";         
158                 KeyAlgorithm = "System.Security.Cryptography.RSACryptoServiceProvider";         
159         }
160
161         public override AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key) 
162         {
163                 // just to please corcompare
164                 return base.CreateDeformatter (key);
165         }
166 }
167         
168 }