1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47:
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57:
60: public class DSSParameters
61: extends AlgorithmParametersSpi
62: {
63:
68: private BigInteger p;
69:
70:
74: private BigInteger q;
75:
76:
82: private BigInteger g;
83:
84:
85:
86: protected void engineInit(AlgorithmParameterSpec spec)
87: throws InvalidParameterSpecException
88: {
89: if (! (spec instanceof DSAParameterSpec))
90: throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
91: + spec.getClass().getName());
92: DSAParameterSpec dsaSpec = (DSAParameterSpec) spec;
93: p = dsaSpec.getP();
94: q = dsaSpec.getQ();
95: g = dsaSpec.getG();
96: }
97:
98:
110: protected void engineInit(byte[] params) throws IOException
111: {
112: DERReader der = new DERReader(params);
113:
114: DERValue derParams = der.read();
115: DerUtil.checkIsConstructed(derParams, "Wrong DSS Parameters field");
116:
117: DERValue val = der.read();
118: DerUtil.checkIsBigInteger(val, "Wrong P field");
119: p = (BigInteger) val.getValue();
120: val = der.read();
121: DerUtil.checkIsBigInteger(val, "Wrong Q field");
122: q = (BigInteger) val.getValue();
123: val = der.read();
124: DerUtil.checkIsBigInteger(val, "Wrong G field");
125: g = (BigInteger) val.getValue();
126: }
127:
128: protected void engineInit(byte[] params, String format) throws IOException
129: {
130: if (format != null)
131: {
132: format = format.trim();
133: if (format.length() == 0)
134: throw new IOException("Format MUST NOT be an empty string");
135:
136: if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
137: throw new IOException("Unknown or unsupported format: " + format);
138: }
139: engineInit(params);
140: }
141:
142: protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
143: throws InvalidParameterSpecException
144: {
145: if (! paramSpec.isAssignableFrom(DSAParameterSpec.class))
146: throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
147: + paramSpec.getName());
148: return new DSAParameterSpec(p, q, g);
149: }
150:
151:
163: protected byte[] engineGetEncoded() throws IOException
164: {
165: DERValue derP = new DERValue(DER.INTEGER, p);
166: DERValue derQ = new DERValue(DER.INTEGER, q);
167: DERValue derG = new DERValue(DER.INTEGER, g);
168:
169: ArrayList params = new ArrayList(3);
170: params.add(derP);
171: params.add(derQ);
172: params.add(derG);
173: DERValue derParams = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, params);
174:
175: ByteArrayOutputStream baos = new ByteArrayOutputStream();
176: DERWriter.write(baos, derParams);
177: byte[] result = baos.toByteArray();
178:
179: return result;
180: }
181:
182: protected byte[] engineGetEncoded(String format) throws IOException
183: {
184: if (format != null)
185: {
186: format = format.trim();
187: if (format.length() == 0)
188: throw new IOException("Format MUST NOT be an empty string");
189:
190: if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
191: throw new IOException("Unknown or unsupported format: " + format);
192: }
193: return engineGetEncoded();
194: }
195:
196: protected String engineToString()
197: {
198: StringBuffer sb = new StringBuffer("p=");
199: if (p == null)
200: sb.append("???");
201: else
202: sb.append("0x").append(p.toString(16));
203:
204: sb.append(", q=");
205: if (q == null)
206: sb.append("???");
207: else
208: sb.append("0x").append(q.toString(16));
209:
210: sb.append(", g=");
211: if (g == null)
212: sb.append("???");
213: else
214: sb.append("0x").append(g.toString(16));
215:
216: return sb.toString();
217: }
218: }