libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / gnu / java / security / jce / sig / DSSParameters.java
blob8a1cb4ca50204eb18c925189ca2580fa6c1c7c6d
1 /* DSSParameters.java -- DSS parameters DAO
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.security.jce.sig;
41 import gnu.java.lang.CPStringBuilder;
43 import gnu.java.security.Registry;
44 import gnu.java.security.der.DER;
45 import gnu.java.security.der.DERReader;
46 import gnu.java.security.der.DERValue;
47 import gnu.java.security.der.DERWriter;
48 import gnu.java.security.util.DerUtil;
50 import java.io.ByteArrayOutputStream;
51 import java.io.IOException;
52 import java.math.BigInteger;
53 import java.security.AlgorithmParametersSpi;
54 import java.security.spec.AlgorithmParameterSpec;
55 import java.security.spec.DSAParameterSpec;
56 import java.security.spec.InvalidParameterSpecException;
57 import java.util.ArrayList;
59 /**
60 * A JCE-specific Data Access Object (DAO) for DSS parameters.
62 public class DSSParameters
63 extends AlgorithmParametersSpi
65 /**
66 * A prime modulus, where <code>2<sup>L-1</sup> &lt; p &lt; 2<sup>L</sup></code>
67 * for <code>512 &lt;= L &lt;= 1024</code> and <code>L</code> a multiple of
68 * <code>64</code>.
70 private BigInteger p;
72 /**
73 * A prime divisor of <code>p - 1</code>, where <code>2<sup>159</sup> &lt; q
74 * &lt; 2<sup>160</sup></code>.
76 private BigInteger q;
78 /**
79 * <code>g = h<sup>(p-1)</sup>/q mod p</code>, where <code>h</code> is any
80 * integer with <code>1 &lt; h &lt; p - 1</code> such that <code>h<sup>
81 * (p-1)</sup>/q mod p > 1</code> (<code>g</code> has order <code>q mod p
82 * </code>).
84 private BigInteger g;
86 // default 0-arguments constructor
88 protected void engineInit(AlgorithmParameterSpec spec)
89 throws InvalidParameterSpecException
91 if (! (spec instanceof DSAParameterSpec))
92 throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
93 + spec.getClass().getName());
94 DSAParameterSpec dsaSpec = (DSAParameterSpec) spec;
95 p = dsaSpec.getP();
96 q = dsaSpec.getQ();
97 g = dsaSpec.getG();
101 * Decodes the set of DSS parameters as per RFC-2459; i.e. the DER-encoded
102 * form of the following ASN.1 construct:
104 * <pre>
105 * DssParams ::= SEQUENCE {
106 * p INTEGER,
107 * q INTEGER,
108 * g INTEGER
110 * </pre>
112 protected void engineInit(byte[] params) throws IOException
114 DERReader der = new DERReader(params);
116 DERValue derParams = der.read();
117 DerUtil.checkIsConstructed(derParams, "Wrong DSS Parameters field");
119 DERValue val = der.read();
120 DerUtil.checkIsBigInteger(val, "Wrong P field");
121 p = (BigInteger) val.getValue();
122 val = der.read();
123 DerUtil.checkIsBigInteger(val, "Wrong Q field");
124 q = (BigInteger) val.getValue();
125 val = der.read();
126 DerUtil.checkIsBigInteger(val, "Wrong G field");
127 g = (BigInteger) val.getValue();
130 protected void engineInit(byte[] params, String format) throws IOException
132 if (format != null)
134 format = format.trim();
135 if (format.length() == 0)
136 throw new IOException("Format MUST NOT be an empty string");
138 if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
139 throw new IOException("Unknown or unsupported format: " + format);
141 engineInit(params);
144 protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
145 throws InvalidParameterSpecException
147 if (! paramSpec.isAssignableFrom(DSAParameterSpec.class))
148 throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
149 + paramSpec.getName());
150 return new DSAParameterSpec(p, q, g);
154 * Encodes the set of DSS parameters as per RFC-2459; i.e. as the DER-encoded
155 * form of the following ASN.1 construct:
157 * <pre>
158 * DssParams ::= SEQUENCE {
159 * p INTEGER,
160 * q INTEGER,
161 * g INTEGER
163 * </pre>
165 protected byte[] engineGetEncoded() throws IOException
167 DERValue derP = new DERValue(DER.INTEGER, p);
168 DERValue derQ = new DERValue(DER.INTEGER, q);
169 DERValue derG = new DERValue(DER.INTEGER, g);
171 ArrayList params = new ArrayList(3);
172 params.add(derP);
173 params.add(derQ);
174 params.add(derG);
175 DERValue derParams = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, params);
177 ByteArrayOutputStream baos = new ByteArrayOutputStream();
178 DERWriter.write(baos, derParams);
179 byte[] result = baos.toByteArray();
181 return result;
184 protected byte[] engineGetEncoded(String format) throws IOException
186 if (format != null)
188 format = format.trim();
189 if (format.length() == 0)
190 throw new IOException("Format MUST NOT be an empty string");
192 if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
193 throw new IOException("Unknown or unsupported format: " + format);
195 return engineGetEncoded();
198 protected String engineToString()
200 CPStringBuilder sb = new CPStringBuilder("p=");
201 if (p == null)
202 sb.append("???");
203 else
204 sb.append("0x").append(p.toString(16));
206 sb.append(", q=");
207 if (q == null)
208 sb.append("???");
209 else
210 sb.append("0x").append(q.toString(16));
212 sb.append(", g=");
213 if (g == null)
214 sb.append("???");
215 else
216 sb.append("0x").append(g.toString(16));
218 return sb.toString();