New test.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / DSA.cs
1 //
2 // System.Security.Cryptography.DSA.cs class implementation
3 //
4 // Authors:
5 //      Thomas Neidhart (tome@sbox.tugraz.at)
6 //      Sebastien Pouliot (sebastien@ximian.com)
7 //
8 // Portions (C) 2002, 2003 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 using System.Globalization;
32 using System.Runtime.InteropServices;
33 using System.Text;
34
35 using Mono.Xml;
36 using Mono.Security;
37
38 // References:
39 // a.   FIPS PUB 186-2: Digital Signature Standard (DSS) 
40 //      http://csrc.nist.gov/publications/fips/fips186-2/fips186-2-change1.pdf
41
42 namespace System.Security.Cryptography {
43
44 #if NET_2_0
45         [ComVisible (true)]
46 #endif
47         public abstract class DSA : AsymmetricAlgorithm {
48
49 #if NET_2_0
50                 // Constructor visibility fixed in Fx 2.0
51                 protected DSA ()
52 #else
53                 // LAMESPEC: It says to derive new DSA implemenation from DSA class.
54                 // Well it's aint gonna be easy this way.
55                 // RSA constructor is public
56                 internal DSA ()
57 #endif
58                 {
59                 }
60
61                 public static new DSA Create ()
62                 {
63                         return Create ("System.Security.Cryptography.DSA");
64                 }
65
66                 public static new DSA Create (string algName) 
67                 {
68                         return (DSA) CryptoConfig.CreateFromName (algName);
69                 }
70                 
71                 public abstract byte[] CreateSignature (byte[] rgbHash);
72                 
73                 public abstract DSAParameters ExportParameters (bool includePrivateParameters);
74
75                 internal void ZeroizePrivateKey (DSAParameters parameters)
76                 {
77                         if (parameters.X != null)
78                                 Array.Clear (parameters.X, 0, parameters.X.Length);
79                 }
80
81                 private byte[] GetNamedParam (SecurityElement se, string param) 
82                 {
83                         SecurityElement sep = se.SearchForChildByTag (param);
84                         if (sep == null)
85                                 return null;
86                         return Convert.FromBase64String (sep.Text);
87                 }
88
89                 public override void FromXmlString (string xmlString) 
90                 {
91                         if (xmlString == null)
92                                 throw new ArgumentNullException ("xmlString");
93                         
94                         DSAParameters dsaParams = new DSAParameters ();
95                         try {
96                                 SecurityParser sp = new SecurityParser ();
97                                 sp.LoadXml (xmlString);
98                                 SecurityElement se = sp.ToXml ();
99                                 if (se.Tag != "DSAKeyValue")
100                                         throw new Exception ();
101                                 dsaParams.P = GetNamedParam (se, "P");
102                                 dsaParams.Q = GetNamedParam (se, "Q");
103                                 dsaParams.G = GetNamedParam (se, "G");
104                                 dsaParams.J = GetNamedParam (se, "J");
105                                 dsaParams.Y = GetNamedParam (se, "Y");
106                                 dsaParams.X = GetNamedParam (se, "X");
107                                 dsaParams.Seed = GetNamedParam (se, "Seed");
108                                 byte[] counter = GetNamedParam (se, "PgenCounter");
109                                 if (counter != null) {
110                                         byte[] counter4b = new byte [4]; // always 4 bytes
111                                         Buffer.BlockCopy (counter, 0, counter4b, 0, counter.Length);
112                                         dsaParams.Counter = BitConverterLE.ToInt32 (counter4b, 0);
113                                 }
114                                 ImportParameters (dsaParams);
115                         }
116                         catch {
117                                 ZeroizePrivateKey (dsaParams);
118                                 throw;
119                         }
120                         finally {
121                                 ZeroizePrivateKey (dsaParams);
122                         }
123                 }
124                 
125                 public abstract void ImportParameters (DSAParameters parameters);
126
127                 // note: using SecurityElement.ToXml wouldn't generate the same string as the MS implementation
128                 public override string ToXmlString (bool includePrivateParameters)
129                 {
130                         StringBuilder sb = new StringBuilder ();
131                         DSAParameters dsaParams = ExportParameters (includePrivateParameters);
132                         try {
133                                 sb.Append ("<DSAKeyValue>");
134                                 
135                                 sb.Append ("<P>");
136                                 sb.Append (Convert.ToBase64String (dsaParams.P));
137                                 sb.Append ("</P>");
138                                 
139                                 sb.Append ("<Q>");
140                                 sb.Append (Convert.ToBase64String (dsaParams.Q));
141                                 sb.Append ("</Q>");
142
143                                 sb.Append ("<G>");
144                                 sb.Append (Convert.ToBase64String (dsaParams.G));
145                                 sb.Append ("</G>");
146
147                                 sb.Append ("<Y>");
148                                 sb.Append (Convert.ToBase64String (dsaParams.Y));
149                                 sb.Append( "</Y>");
150
151                                 if (dsaParams.J != null) {
152                                         // if J wasn't imported then it's not exported and neither 
153                                         // is part of the XML output
154                                         sb.Append ("<J>");
155                                         sb.Append (Convert.ToBase64String (dsaParams.J));
156                                         sb.Append ("</J>");
157                                 }
158
159                                 if (dsaParams.Seed != null) {
160                                         sb.Append ("<Seed>");
161                                         sb.Append (Convert.ToBase64String (dsaParams.Seed));
162                                         sb.Append ("</Seed>");
163
164                                         sb.Append ("<PgenCounter>");
165                                         // the number of bytes is important (no matter == 0x00)
166                                         if (dsaParams.Counter != 0) {
167                                                 byte[] inArr = BitConverterLE.GetBytes (dsaParams.Counter);
168                                                 int l = inArr.Length;
169                                                 while (inArr[l-1] == 0x00)
170                                                         l--;
171
172                                                 sb.Append (Convert.ToBase64String (inArr, 0, l));
173                                         } else {
174                                                 sb.Append ("AA==");     // base64 encoded 0
175                                         }
176                                         sb.Append ("</PgenCounter>");
177                                 }
178
179                                 if (dsaParams.X != null) {
180                                         sb.Append ("<X>");
181                                         sb.Append (Convert.ToBase64String (dsaParams.X));
182                                         sb.Append ("</X>");
183                                 }
184                                 else if (includePrivateParameters) {
185 #if NET_2_0
186                                         throw new ArgumentNullException ("X");
187 #else
188                                         throw new CryptographicException (
189                                                 Locale.GetText ("Missing private key informations"));
190 #endif
191                                 }
192
193                                 sb.Append ("</DSAKeyValue>");
194                         }
195                         catch {
196                                 ZeroizePrivateKey (dsaParams);
197                                 throw;
198                         }
199                                                 
200                         return sb.ToString ();
201                 }
202                 
203                 public abstract bool VerifySignature (byte[] rgbHash, byte[] rgbSignature);
204         }
205 }