1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46:
47:
68: public class ServerKeyExchange implements Handshake.Body
69: {
70:
71: protected ByteBuffer buffer;
72: protected final CipherSuite suite;
73:
74: public ServerKeyExchange(final ByteBuffer buffer, final CipherSuite suite)
75: {
76: suite.getClass();
77: this.buffer = buffer.duplicate().order(ByteOrder.BIG_ENDIAN);
78: this.suite = suite;
79: }
80:
81: public int length ()
82: {
83: if (suite.keyExchangeAlgorithm ().equals (KeyExchangeAlgorithm.NONE))
84: return 0;
85: int len = 0;
86: ServerKeyExchangeParams params = params();
87: Signature sig = signature();
88: if (params != null)
89: len += params.length();
90: if (sig != null)
91: len += sig.length();
92: return len;
93: }
94:
95:
101: public ServerKeyExchangeParams params ()
102: {
103: KeyExchangeAlgorithm kex = suite.keyExchangeAlgorithm ();
104: if (kex == KeyExchangeAlgorithm.RSA)
105: return new ServerRSAParams(buffer.duplicate ());
106: else if (kex == KeyExchangeAlgorithm.DHE_DSS
107: || kex == KeyExchangeAlgorithm.DHE_RSA
108: || kex == KeyExchangeAlgorithm.DH_anon)
109: return new ServerDHParams(buffer.duplicate());
110:
111:
112: else if (kex == KeyExchangeAlgorithm.NONE)
113: return null;
114: else if (kex == KeyExchangeAlgorithm.DHE_PSK)
115: return new ServerDHE_PSKParameters(buffer.duplicate());
116: else if (kex == KeyExchangeAlgorithm.PSK)
117: return new ServerPSKParameters(buffer.duplicate());
118: else if (kex == KeyExchangeAlgorithm.RSA_PSK)
119: return new ServerPSKParameters(buffer.duplicate());
120: throw new IllegalArgumentException ("unsupported key exchange: " + kex);
121: }
122:
123:
128: public Signature signature ()
129: {
130: KeyExchangeAlgorithm kex = suite.keyExchangeAlgorithm();
131: if (kex == KeyExchangeAlgorithm.NONE
132: || kex == KeyExchangeAlgorithm.DH_anon
133: || kex == KeyExchangeAlgorithm.DHE_PSK
134: || kex == KeyExchangeAlgorithm.PSK
135: || kex == KeyExchangeAlgorithm.RSA_PSK)
136: return null;
137: ServerKeyExchangeParams params = params();
138: ByteBuffer sigbuf = ((ByteBuffer) buffer.position(params.length ())).slice ();
139: return new Signature (sigbuf, suite.signatureAlgorithm ());
140: }
141:
142: public String toString()
143: {
144: return toString (null);
145: }
146:
147: public String toString (final String prefix)
148: {
149: StringWriter str = new StringWriter();
150: PrintWriter out = new PrintWriter(str);
151: if (prefix != null) out.print (prefix);
152: out.println("struct {");
153: if (prefix != null) out.print (prefix);
154: out.print (" algorithm: ");
155: out.print (suite.keyExchangeAlgorithm ());
156: out.println (";");
157: if (!suite.keyExchangeAlgorithm ().equals (KeyExchangeAlgorithm.NONE))
158: {
159: if (prefix != null) out.print (prefix);
160: out.println (" parameters:");
161: out.println (params ().toString (prefix != null ? prefix+" " : " "));
162: }
163: if (!suite.signatureAlgorithm ().equals (SignatureAlgorithm.ANONYMOUS))
164: {
165: if (prefix != null) out.print (prefix);
166: out.println (" signature:");
167: out.println (signature ().toString (prefix != null ? prefix+" " : " "));
168: }
169: if (prefix != null) out.print (prefix);
170: out.print ("} ServerKeyExchange;");
171: return str.toString();
172: }
173: }