3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>[....]</OWNER>
13 namespace System.Security.Cryptography {
15 using System.Runtime.Serialization;
16 using System.Security.Util;
17 using System.Globalization;
18 using System.Diagnostics.Contracts;
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.
24 [System.Runtime.InteropServices.ComVisible(true)]
25 public struct DSAParameters {
31 [NonSerialized] public byte[] X;
36 [System.Runtime.InteropServices.ComVisible(true)]
37 public abstract class DSA : AsymmetricAlgorithm
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
44 // The class provides no methods, fields or anything else. Its only purpose is
45 // as a heirarchy member for identification of the algorithm.
54 new static public DSA Create() {
55 return Create("System.Security.Cryptography.DSA");
58 new static public DSA Create(String algName) {
59 return (DSA) CryptoConfig.CreateFromName(algName);
62 abstract public byte[] CreateSignature(byte[] rgbHash);
64 abstract public bool VerifySignature(byte[] rgbHash, byte[] rgbSignature);
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.
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();
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"));
82 dsaParams.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(pString));
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"));
89 dsaParams.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(qString));
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"));
96 dsaParams.G = Convert.FromBase64String(Utils.DiscardWhiteSpaces(gString));
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"));
103 dsaParams.Y = Convert.FromBase64String(Utils.DiscardWhiteSpaces(yString));
106 String jString = topElement.SearchForTextOfLocalName("J");
107 if (jString != null) dsaParams.J = Convert.FromBase64String(Utils.DiscardWhiteSpaces(jString));
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));
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"));
123 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString","DSA","PgenCounter"));
127 ImportParameters(dsaParams);
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.
136 public override String ToXmlString(bool includePrivateParameters) {
137 // From the XMLDSIG spec, RFC 3075, Section 6.4.1, a DSAKeyValue looks like this:
139 <element name="DSAKeyValue">
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"/>
149 <sequence minOccurs="0">
150 <element name="Seed" type="ds:CryptoBinary"/>
151 <element name="PgenCounter" type="ds:CryptoBinary"/>
157 // we extend appropriately for private component X
158 DSAParameters dsaParams = this.ExportParameters(includePrivateParameters);
159 StringBuilder sb = new StringBuilder();
160 sb.Append("<DSAKeyValue>");
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>");
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>");
175 if (includePrivateParameters) {
176 // Add the private component
177 sb.Append("<X>"+Convert.ToBase64String(dsaParams.X)+"</X>");
179 sb.Append("</DSAKeyValue>");
180 return(sb.ToString());
183 abstract public DSAParameters ExportParameters(bool includePrivateParameters);
185 abstract public void ImportParameters(DSAParameters parameters);