1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46:
47:
59: public class ServerDHParams implements Builder, ServerKeyExchangeParams
60: {
61: private final ByteBuffer buffer;
62:
63: public ServerDHParams (final ByteBuffer buffer)
64: {
65: this.buffer = buffer.duplicate().order(ByteOrder.BIG_ENDIAN);
66: }
67:
68: public ServerDHParams (final BigInteger p, final BigInteger g,
69: final BigInteger y)
70: {
71: byte[] p_bytes = p.toByteArray();
72: byte[] g_bytes = g.toByteArray();
73: byte[] y_bytes = y.toByteArray();
74: int len = p_bytes.length + g_bytes.length + y_bytes.length + 6;
75:
76: int p_off = 0;
77: if (p_bytes[0] == 0x00)
78: {
79: p_off = 1;
80: len--;
81: }
82: int g_off = 0;
83: if (g_bytes[0] == 0x00)
84: {
85: g_off = 1;
86: len--;
87: }
88: int y_off = 0;
89: if (y_bytes[0] == 0x00)
90: {
91: y_off = 1;
92: len--;
93: }
94: int p_len = p_bytes.length - p_off;
95: int g_len = g_bytes.length - g_off;
96: int y_len = y_bytes.length - y_off;
97:
98: buffer = ByteBuffer.allocate(len);
99: buffer.putShort((short) p_len);
100: buffer.put(p_bytes, p_off, p_len);
101: buffer.putShort((short) g_len);
102: buffer.put(g_bytes, g_off, g_len);
103: buffer.putShort((short) y_len);
104: buffer.put(y_bytes, y_off, y_len);
105: }
106:
107: @Deprecated public KeyExchangeAlgorithm algorithm ()
108: {
109: return null;
110: }
111:
112: public int length ()
113: {
114: int offset1 = buffer.getShort (0) & 0xFFFF;
115: int offset2 = buffer.getShort (offset1 + 2) & 0xFFFF;
116: return ((buffer.getShort (offset1 + offset2 + 4) & 0xFFFF)
117: + offset1 + offset2 + 6);
118: }
119:
120: public ByteBuffer buffer()
121: {
122: return (ByteBuffer) buffer.duplicate().position(0).limit(length());
123: }
124:
125:
130: public BigInteger p ()
131: {
132: int len = buffer.getShort (0) & 0xFFFF;
133: byte[] buf = new byte[len];
134: buffer.position (2);
135: buffer.get (buf);
136: return new BigInteger (1, buf);
137: }
138:
139:
144: public BigInteger g ()
145: {
146: int off = (buffer.getShort (0) & 0xFFFF) + 2;
147: int len = buffer.getShort (off) & 0xFFFF;
148: byte[] buf = new byte[len];
149: buffer.position (off + 2);
150: buffer.get (buf);
151: return new BigInteger (1, buf);
152: }
153:
154:
159: public BigInteger y ()
160: {
161: int offset1 = (buffer.getShort (0) & 0xFFFF) + 2;
162: int offset2 = (buffer.getShort (offset1) & 0xFFFF) + offset1 + 2;
163: int len = buffer.getShort (offset2) & 0xFFFF;
164: byte[] buf = new byte[len];
165: buffer.position (offset2 + 2);
166: buffer.get (buf);
167: return new BigInteger (1, buf);
168: }
169:
170:
177: public void setP (final BigInteger p)
178: {
179: byte[] buf = p.toByteArray ();
180: int length = (buf[0] == 0x00 ? buf.length - 1 : buf.length);
181: int offset = (buf[0] == 0x00 ? 1 : 0);
182: buffer.putShort (0, (short) length);
183: buffer.position (2);
184: buffer.put (buf, offset, length);
185: }
186:
187:
194: public void setG (final BigInteger g)
195: {
196: byte[] buf = g.toByteArray ();
197: int length = (buf[0] == 0x00 ? buf.length -1 : buf.length);
198: int offset = (buf[0] == 0x00 ? 1 : 0);
199: int where = (buffer.getShort (0) & 0xFFFF) + 2;
200: buffer.putShort (where, (short) length);
201: buffer.position (where + 2);
202: buffer.put (buf, offset, length);
203: }
204:
205:
212: public void setY (final BigInteger y)
213: {
214: int offset1 = (buffer.getShort (0) & 0xFFFF) + 2;
215: int offset2 = (buffer.getShort (offset1) & 0xFFFF) + offset1 + 2;
216: byte[] buf = y.toByteArray ();
217: int length = (buf[0] == 0x00 ? buf.length -1 : buf.length);
218: int offset = (buf[0] == 0x00 ? 1 : 0);
219: buffer.putShort (offset2, (short) length);
220: buffer.position (offset2 + 2);
221: buffer.put (buf, offset, length);
222: }
223:
224: public String toString ()
225: {
226: return toString (null);
227: }
228:
229: public String toString (final String prefix)
230: {
231: StringWriter str = new StringWriter ();
232: PrintWriter out = new PrintWriter (str);
233: if (prefix != null) out.print (prefix);
234: out.println ("struct {");
235: if (prefix != null) out.print (prefix);
236: out.print (" dh_p: ");
237: out.println (p ().toString (16));
238: if (prefix != null) out.print (prefix);
239: out.print (" dh_g: ");
240: out.println (g ().toString (16));
241: if (prefix != null) out.print (prefix);
242: out.print (" dh_Ys: ");
243: out.println (y ().toString (16));
244: if (prefix != null) out.print (prefix);
245: out.print ("} ServerDHParams;");
246: return str.toString ();
247: }
248: }