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:
56: import ;
57: import ;
58:
59:
62: public class DHParameters
63: extends AlgorithmParametersSpi
64: {
65:
66: private BigInteger p;
67:
68:
69: private BigInteger g;
70:
71:
72: private BigInteger q;
73:
74:
75: private int l;
76:
77:
78:
79: protected void engineInit(AlgorithmParameterSpec spec)
80: throws InvalidParameterSpecException
81: {
82: if (! (spec instanceof DHParameterSpec))
83: throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
84: + spec.getClass().getName());
85: DHParameterSpec dhSpec = (DHParameterSpec) spec;
86: p = dhSpec.getP();
87: g = dhSpec.getG();
88: l = dhSpec.getL();
89: }
90:
91:
103: protected void engineInit(byte[] params) throws IOException
104: {
105: DERReader der = new DERReader(params);
106:
107: DERValue derParams = der.read();
108: DerUtil.checkIsConstructed(derParams, "Wrong DH Parameters field");
109:
110: DERValue val = der.read();
111: DerUtil.checkIsBigInteger(val, "Wrong P field");
112: p = (BigInteger) val.getValue();
113: val = der.read();
114: DerUtil.checkIsBigInteger(val, "Wrong G field");
115: g = (BigInteger) val.getValue();
116: val = der.read();
117: DerUtil.checkIsBigInteger(val, "Wrong Q field");
118: q = (BigInteger) val.getValue();
119: l = q.bitLength();
120: }
121:
122: protected void engineInit(byte[] params, String format) throws IOException
123: {
124: if (format != null)
125: {
126: format = format.trim();
127: if (format.length() == 0)
128: throw new IOException("Format MUST NOT be an empty string");
129:
130: if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
131: throw new IOException("Unknown or unsupported format: " + format);
132: }
133:
134: engineInit(params);
135: }
136:
137: protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec)
138: throws InvalidParameterSpecException
139: {
140: if (paramSpec.isAssignableFrom(DHParameterSpec.class))
141: return new DHParameterSpec(p, g, l);
142:
143: if (paramSpec.isAssignableFrom(DHGenParameterSpec.class))
144: return new DHGenParameterSpec(p.bitLength(), l);
145:
146: throw new InvalidParameterSpecException("Wrong AlgorithmParameterSpec type: "
147: + paramSpec.getName());
148: }
149:
150:
162: protected byte[] engineGetEncoded() throws IOException
163: {
164: DERValue derP = new DERValue(DER.INTEGER, p);
165: DERValue derG = new DERValue(DER.INTEGER, g);
166: DERValue derQ = new DERValue(DER.INTEGER, q);
167:
168: ArrayList params = new ArrayList(3);
169: params.add(derP);
170: params.add(derG);
171: params.add(derQ);
172: DERValue derParams = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, params);
173:
174: ByteArrayOutputStream baos = new ByteArrayOutputStream();
175: DERWriter.write(baos, derParams);
176: byte[] result = baos.toByteArray();
177:
178: return result;
179: }
180:
181: protected byte[] engineGetEncoded(String format) throws IOException
182: {
183: if (format != null)
184: {
185: format = format.trim();
186: if (format.length() == 0)
187: throw new IOException("Format MUST NOT be an empty string");
188:
189: if (! format.equalsIgnoreCase(Registry.ASN1_ENCODING_SHORT_NAME))
190: throw new IOException("Unknown or unsupported format: " + format);
191: }
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(", g=");
205: if (g == null)
206: sb.append("???");
207: else
208: sb.append("0x").append(g.toString(16));
209:
210: sb.append(", q=");
211: if (q == null)
212: sb.append("???");
213: else
214: sb.append("0x").append(q.toString(16));
215:
216: sb.append(", l=").append(l);
217:
218: return sb.toString();
219: }
220: }