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