Initial commit
[mono.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / dsa.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>[....]</OWNER>
7 // 
8
9 //
10 // DSA.cs
11 //
12
13 namespace System.Security.Cryptography {
14     using System.Text;
15     using System.Runtime.Serialization;
16     using System.Security.Util;
17     using System.Globalization;
18     using System.Diagnostics.Contracts;
19
20     // DSAParameters is serializable so that one could pass the public parameters
21     // across a remote call, but we explicitly make the private key X non-serializable
22     // so you cannot accidently send it along with the public parameters.
23     [Serializable]
24 [System.Runtime.InteropServices.ComVisible(true)]
25     public struct DSAParameters {
26         public byte[]      P;
27         public byte[]      Q;
28         public byte[]      G;
29         public byte[]      Y;
30         public byte[]      J;
31         [NonSerialized] public byte[]      X;
32         public byte[]      Seed;
33         public int         Counter;
34     }
35
36 [System.Runtime.InteropServices.ComVisible(true)]
37     public abstract class DSA : AsymmetricAlgorithm
38     {
39         //
40         //  Extending this class allows us to know that you are really implementing
41         //  an DSA key.  This is required for anybody providing a new DSA key value
42         //  implemention.
43         //
44         //  The class provides no methods, fields or anything else.  Its only purpose is
45         //  as a heirarchy member for identification of the algorithm.
46         //
47
48         protected DSA() { }
49
50         //
51         // public methods
52         //
53
54         new static public DSA Create() {
55             return Create("System.Security.Cryptography.DSA");
56         }
57
58         new static public DSA Create(String algName) {
59             return (DSA) CryptoConfig.CreateFromName(algName);
60         }
61
62         abstract public byte[] CreateSignature(byte[] rgbHash);
63
64         abstract public bool VerifySignature(byte[] rgbHash, byte[] rgbSignature); 
65
66         // We can provide a default implementation of FromXmlString because we require 
67         // every DSA implementation to implement ImportParameters
68         // All we have to do here is parse the XML.
69
70         public override void FromXmlString(String xmlString) {
71             if (xmlString == null) throw new ArgumentNullException("xmlString");
72             Contract.EndContractBlock();
73             DSAParameters dsaParams = new DSAParameters();
74             Parser p = new Parser(xmlString);
75             SecurityElement topElement = p.GetTopElement();
76
77             // P is always present
78             String pString = topElement.SearchForTextOfLocalName("P");
79             if (pString == null) {
80                 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","P"));
81             }
82             dsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));
83
84             // Q is always present
85             String qString = topElement.SearchForTextOfLocalName("Q");
86             if (qString == null) {
87                 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","Q"));
88             }
89             dsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString));
90
91             // G is always present
92             String gString = topElement.SearchForTextOfLocalName("G");
93             if (gString == null) {
94                 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","G"));
95             }
96             dsaParams.G = Convert.FromBase64String(Utils.DiscardWhiteSpaces(gString));
97
98             // Y is always present
99             String yString = topElement.SearchForTextOfLocalName("Y");
100             if (yString == null) {
101                 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","Y"));
102             }
103             dsaParams.Y = Convert.FromBase64String(Utils.DiscardWhiteSpaces(yString));
104
105             // J is optional
106             String jString = topElement.SearchForTextOfLocalName("J");
107             if (jString != null) dsaParams.J = Convert.FromBase64String(Utils.DiscardWhiteSpaces(jString));
108
109             // X is optional -- private key if present
110             String xString = topElement.SearchForTextOfLocalName("X");
111             if (xString != null) dsaParams.X = Convert.FromBase64String(Utils.DiscardWhiteSpaces(xString));
112
113             // Seed and PgenCounter are optional as a unit -- both present or both absent
114             String seedString = topElement.SearchForTextOfLocalName("Seed");
115             String pgenCounterString = topElement.SearchForTextOfLocalName("PgenCounter");
116             if ((seedString != null) && (pgenCounterString != null)) {
117                 dsaParams.Seed = Convert.FromBase64String(Utils.DiscardWhiteSpaces(seedString));
118                 dsaParams.Counter = Utils.ConvertByteArrayToInt(Convert.FromBase64String(Utils.DiscardWhiteSpaces(pgenCounterString)));
119             } else if ((seedString != null) || (pgenCounterString != null)) {
120                 if (seedString == null) {
121                     throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","Seed"));
122                 } else {
123                     throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","PgenCounter"));
124                 }
125             }
126
127             ImportParameters(dsaParams);
128         }
129
130         // We can provide a default implementation of ToXmlString because we require 
131         // every DSA implementation to implement ImportParameters
132         // If includePrivateParameters is false, this is just an XMLDSIG DSAKeyValue
133         // clause.  If includePrivateParameters is true, then we extend DSAKeyValue with 
134         // the other (private) elements.
135
136         public override String ToXmlString(bool includePrivateParameters) {
137             // From the XMLDSIG spec, RFC 3075, Section 6.4.1, a DSAKeyValue looks like this:
138             /* 
139                <element name="DSAKeyValue"> 
140                  <complexType> 
141                    <sequence>
142                      <sequence>
143                        <element name="P" type="ds:CryptoBinary"/> 
144                        <element name="Q" type="ds:CryptoBinary"/> 
145                        <element name="G" type="ds:CryptoBinary"/> 
146                        <element name="Y" type="ds:CryptoBinary"/> 
147                        <element name="J" type="ds:CryptoBinary" minOccurs="0"/> 
148                      </sequence>
149                      <sequence minOccurs="0">
150                        <element name="Seed" type="ds:CryptoBinary"/> 
151                        <element name="PgenCounter" type="ds:CryptoBinary"/> 
152                      </sequence>
153                    </sequence>
154                  </complexType>
155                </element>
156             */
157             // we extend appropriately for private component X
158             DSAParameters dsaParams = this.ExportParameters(includePrivateParameters);
159             StringBuilder sb = new StringBuilder();
160             sb.Append("<DSAKeyValue>");
161             // Add P, Q, G and Y
162             sb.Append("<P>"+Convert.ToBase64String(dsaParams.P)+"</P>");
163             sb.Append("<Q>"+Convert.ToBase64String(dsaParams.Q)+"</Q>");
164             sb.Append("<G>"+Convert.ToBase64String(dsaParams.G)+"</G>");
165             sb.Append("<Y>"+Convert.ToBase64String(dsaParams.Y)+"</Y>");
166             // Add optional components if present
167             if (dsaParams.J != null) {
168                 sb.Append("<J>"+Convert.ToBase64String(dsaParams.J)+"</J>");
169             }
170             if ((dsaParams.Seed != null)) {  // note we assume counter is correct if Seed is present
171                 sb.Append("<Seed>"+Convert.ToBase64String(dsaParams.Seed)+"</Seed>");
172                 sb.Append("<PgenCounter>"+Convert.ToBase64String(Utils.ConvertIntToByteArray(dsaParams.Counter))+"</PgenCounter>");
173             }
174
175             if (includePrivateParameters) {
176                 // Add the private component
177                 sb.Append("<X>"+Convert.ToBase64String(dsaParams.X)+"</X>");
178             } 
179             sb.Append("</DSAKeyValue>");
180             return(sb.ToString());
181         }
182
183         abstract public DSAParameters ExportParameters(bool includePrivateParameters);
184
185         abstract public void ImportParameters(DSAParameters parameters);
186     }
187 }