2003-07-31 Sebastien Pouliot <spouliot@videotron.ca>
[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 (spouliot@motus.com)
7 //
8 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
9 //
10
11 // Notes:
12 // There seems to be some (internal?) class inheriting from SignatureDescription
13 // 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
14 // Those 2 classes are returned by CryptoConfig.CreateFromName and used in XMLDSIG
15
16 using System;
17 using System.Security;
18
19 namespace System.Security.Cryptography {
20         
21 public class SignatureDescription {
22         private string _DeformatterAlgorithm;
23         private string _DigestAlgorithm;                
24         private string _FormatterAlgorithm;             
25         private string _KeyAlgorithm;           
26
27         public SignatureDescription () {}
28         
29         /// LAMESPEC: ArgumentNullException is thrown (not CryptographicException)
30         [MonoTODO("Parse SecurityElement")]
31         public SignatureDescription (SecurityElement el) 
32         {
33                 if (el == null)
34                         throw new ArgumentNullException ();
35                 // TODO: Parse the SecurityElement 
36                 // Clearly it must contains Deformatter, Digest, 
37                 // Formatter and KeyAlgorithm... 
38                 // But what do the SecurityElement looks like ?
39         }
40
41         // There are no validation of the property
42         public string DeformatterAlgorithm {
43                 get { return _DeformatterAlgorithm; }
44                 set { _DeformatterAlgorithm = value; }
45         }
46
47         // There are no validation of the property
48         public string DigestAlgorithm {
49                 get { return _DigestAlgorithm; }
50                 set { _DigestAlgorithm = value; }
51         }
52
53         // There are no validation of the property
54         public string FormatterAlgorithm {
55                 get { return _FormatterAlgorithm; }
56                 set { _FormatterAlgorithm = value; }
57         }
58
59         // There are no validation of the property
60         public string KeyAlgorithm {
61                 get { return _KeyAlgorithm; }
62                 set { _KeyAlgorithm = value; }
63         }
64
65         public virtual AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key) 
66         {
67                 if (_DeformatterAlgorithm == null)
68                         throw new ArgumentNullException ("DeformatterAlgorithm");
69
70                 // this should throw the InvalidCastException if we have an invalid class
71                 // (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
72                 AsymmetricSignatureDeformatter def = (AsymmetricSignatureDeformatter) CryptoConfig.CreateFromName (_DeformatterAlgorithm);
73
74                 if (_KeyAlgorithm == null)
75                         throw new NullReferenceException ("KeyAlgorithm");
76
77                 def.SetKey (key);
78                 return def;
79         }
80         
81         /// <summary>
82         /// Create the hash algorithm assigned with this object
83         /// </summary>
84         public virtual HashAlgorithm CreateDigest ()
85         {
86                 if (_DigestAlgorithm == null)
87                         throw new ArgumentNullException ("DigestAlgorithm");
88                 return (HashAlgorithm) CryptoConfig.CreateFromName (_DigestAlgorithm);
89         }
90
91         public virtual AsymmetricSignatureFormatter CreateFormatter (AsymmetricAlgorithm key)
92         {
93                 if (_FormatterAlgorithm == null)
94                         throw new ArgumentNullException ("FormatterAlgorithm");
95
96                 // this should throw the InvalidCastException if we have an invalid class
97                 // (but not if the class doesn't exist - as null is valid for AsymmetricSignatureDeformatter)
98                 AsymmetricSignatureFormatter fmt = (AsymmetricSignatureFormatter) CryptoConfig.CreateFromName (_FormatterAlgorithm);
99
100                 if (_KeyAlgorithm == null)
101                         throw new NullReferenceException ("KeyAlgorithm");
102
103                 fmt.SetKey (key);
104                 return fmt;
105         }
106         
107 } // SignatureDescription
108
109 internal class DSASignatureDescription : SignatureDescription {
110         public DSASignatureDescription () 
111         {
112                 DeformatterAlgorithm = "System.Security.Cryptography.DSASignatureDeformatter";
113                 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
114                 FormatterAlgorithm = "System.Security.Cryptography.DSASignatureFormatter";              
115                 KeyAlgorithm = "System.Security.Cryptography.DSACryptoServiceProvider";         
116         }
117 }
118
119 internal class RSAPKCS1SHA1SignatureDescription : SignatureDescription {
120         public RSAPKCS1SHA1SignatureDescription () 
121         {
122                 DeformatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureDeformatter";
123                 DigestAlgorithm = "System.Security.Cryptography.SHA1CryptoServiceProvider";
124                 FormatterAlgorithm = "System.Security.Cryptography.RSAPKCS1SignatureFormatter";         
125                 KeyAlgorithm = "System.Security.Cryptography.RSACryptoServiceProvider";         
126         }
127
128         public override AsymmetricSignatureDeformatter CreateDeformatter (AsymmetricAlgorithm key) 
129         {
130                 // just to please corcompare
131                 return base.CreateDeformatter (key);
132         }
133 }
134         
135 } // System.Security.Cryptography