1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58:
59:
62: public class DSSParameters
63: extends AlgorithmParametersSpi
64: {
65:
70: private BigInteger p;
71:
72:
76: private BigInteger q;
77:
78:
84: private BigInteger g;
85:
86:
87:
88: protected void engineInit(AlgorithmParameterSpec spec)
89: throws InvalidParameterSpecException
90: {
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();
98: }
99:
100:
112: protected void engineInit(byte[] params) throws IOException
113: {
114: DERReader der = new DERReader(params);
115:
116: DERValue derParams = der.read();
117: DerUtil.checkIsConstructed(derParams, "Wrong DSS Parameters field");
118:
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();
128: }
129:
130: protected void engineInit(byte[] params, String format) throws IOException
131: {
132: if (format != null)
133: {
134: format = format.trim();
135: if (format.length() == 0)
136: throw new IOException("Format MUST NOT be an empty string");
137:
138: if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
139: throw new IOException("Unknown or unsupported format: " + format);
140: }
141: engineInit(params);
142: }
143:
144: protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
145: throws InvalidParameterSpecException
146: {
147: if (! paramSpec.isAssignableFrom(DSAParameterSpec.class))
148: throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
149: + paramSpec.getName());
150: return new DSAParameterSpec(p, q, g);
151: }
152:
153:
165: protected byte[] engineGetEncoded() throws IOException
166: {
167: DERValue derP = new DERValue(DER.INTEGER, p);
168: DERValue derQ = new DERValue(DER.INTEGER, q);
169: DERValue derG = new DERValue(DER.INTEGER, g);
170:
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);
176:
177: ByteArrayOutputStream baos = new ByteArrayOutputStream();
178: DERWriter.write(baos, derParams);
179: byte[] result = baos.toByteArray();
180:
181: return result;
182: }
183:
184: protected byte[] engineGetEncoded(String format) throws IOException
185: {
186: if (format != null)
187: {
188: format = format.trim();
189: if (format.length() == 0)
190: throw new IOException("Format MUST NOT be an empty string");
191:
192: if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
193: throw new IOException("Unknown or unsupported format: " + format);
194: }
195: return engineGetEncoded();
196: }
197:
198: protected String engineToString()
199: {
200: CPStringBuilder sb = new CPStringBuilder("p=");
201: if (p == null)
202: sb.append("???");
203: else
204: sb.append("0x").append(p.toString(16));
205:
206: sb.append(", q=");
207: if (q == null)
208: sb.append("???");
209: else
210: sb.append("0x").append(q.toString(16));
211:
212: sb.append(", g=");
213: if (g == null)
214: sb.append("???");
215: else
216: sb.append("0x").append(g.toString(16));
217:
218: return sb.toString();
219: }
220: }