Merge pull request #487 from mayerwin/patch-1
[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, 2008 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         [ComVisible (true)]
45         public abstract class DSA : AsymmetricAlgorithm {
46
47                 // Constructor visibility fixed in Fx 2.0
48                 protected DSA ()
49                 {
50                 }
51
52                 public static new DSA Create ()
53                 {
54 #if FULL_AOT_RUNTIME
55                         return new System.Security.Cryptography.DSACryptoServiceProvider ();
56 #else
57                         return Create ("System.Security.Cryptography.DSA");
58 #endif
59                 }
60
61                 public static new DSA Create (string algName) 
62                 {
63                         return (DSA) CryptoConfig.CreateFromName (algName);
64                 }
65                 
66                 public abstract byte[] CreateSignature (byte[] rgbHash);
67                 
68                 public abstract DSAParameters ExportParameters (bool includePrivateParameters);
69
70                 internal void ZeroizePrivateKey (DSAParameters parameters)
71                 {
72                         if (parameters.X != null)
73                                 Array.Clear (parameters.X, 0, parameters.X.Length);
74                 }
75
76                 public override void FromXmlString (string xmlString) 
77                 {
78                         if (xmlString == null)
79                                 throw new ArgumentNullException ("xmlString");
80                         
81                         DSAParameters dsaParams = new DSAParameters ();
82                         try {
83                                 dsaParams.P = GetNamedParam (xmlString, "P");
84                                 dsaParams.Q = GetNamedParam (xmlString, "Q");
85                                 dsaParams.G = GetNamedParam (xmlString, "G");
86                                 dsaParams.J = GetNamedParam (xmlString, "J");
87                                 dsaParams.Y = GetNamedParam (xmlString, "Y");
88                                 dsaParams.X = GetNamedParam (xmlString, "X");
89                                 dsaParams.Seed = GetNamedParam (xmlString, "Seed");
90                                 byte[] counter = GetNamedParam (xmlString, "PgenCounter");
91                                 if (counter != null) {
92                                         byte[] counter4b = new byte [4]; // always 4 bytes
93                                         Buffer.BlockCopy (counter, 0, counter4b, 0, counter.Length);
94                                         dsaParams.Counter = BitConverterLE.ToInt32 (counter4b, 0);
95                                 }
96                                 ImportParameters (dsaParams);
97                         }
98                         catch {
99                                 ZeroizePrivateKey (dsaParams);
100                                 throw;
101                         }
102                         finally {
103                                 ZeroizePrivateKey (dsaParams);
104                         }
105                 }
106                 
107                 public abstract void ImportParameters (DSAParameters parameters);
108
109                 // note: using SecurityElement.ToXml wouldn't generate the same string as the MS implementation
110                 public override string ToXmlString (bool includePrivateParameters)
111                 {
112                         StringBuilder sb = new StringBuilder ();
113                         DSAParameters dsaParams = ExportParameters (includePrivateParameters);
114                         try {
115                                 sb.Append ("<DSAKeyValue>");
116                                 
117                                 sb.Append ("<P>");
118                                 sb.Append (Convert.ToBase64String (dsaParams.P));
119                                 sb.Append ("</P>");
120                                 
121                                 sb.Append ("<Q>");
122                                 sb.Append (Convert.ToBase64String (dsaParams.Q));
123                                 sb.Append ("</Q>");
124
125                                 sb.Append ("<G>");
126                                 sb.Append (Convert.ToBase64String (dsaParams.G));
127                                 sb.Append ("</G>");
128
129                                 sb.Append ("<Y>");
130                                 sb.Append (Convert.ToBase64String (dsaParams.Y));
131                                 sb.Append( "</Y>");
132
133                                 if (dsaParams.J != null) {
134                                         // if J wasn't imported then it's not exported and neither 
135                                         // is part of the XML output
136                                         sb.Append ("<J>");
137                                         sb.Append (Convert.ToBase64String (dsaParams.J));
138                                         sb.Append ("</J>");
139                                 }
140
141                                 if (dsaParams.Seed != null) {
142                                         sb.Append ("<Seed>");
143                                         sb.Append (Convert.ToBase64String (dsaParams.Seed));
144                                         sb.Append ("</Seed>");
145
146                                         sb.Append ("<PgenCounter>");
147                                         // the number of bytes is important (no matter == 0x00)
148                                         if (dsaParams.Counter != 0) {
149                                                 byte[] inArr = BitConverterLE.GetBytes (dsaParams.Counter);
150                                                 int l = inArr.Length;
151                                                 while (inArr[l-1] == 0x00)
152                                                         l--;
153
154                                                 sb.Append (Convert.ToBase64String (inArr, 0, l));
155                                         } else {
156                                                 sb.Append ("AA==");     // base64 encoded 0
157                                         }
158                                         sb.Append ("</PgenCounter>");
159                                 }
160
161                                 if (dsaParams.X != null) {
162                                         sb.Append ("<X>");
163                                         sb.Append (Convert.ToBase64String (dsaParams.X));
164                                         sb.Append ("</X>");
165                                 }
166                                 else if (includePrivateParameters) {
167                                         throw new ArgumentNullException ("X");
168                                 }
169
170                                 sb.Append ("</DSAKeyValue>");
171                         }
172                         catch {
173                                 ZeroizePrivateKey (dsaParams);
174                                 throw;
175                         }
176                                                 
177                         return sb.ToString ();
178                 }
179                 
180                 public abstract bool VerifySignature (byte[] rgbHash, byte[] rgbSignature);
181         }
182 }