comparison src/ch/ethz/ssh2/transport/ClientKexManager.java @ 307:071eccdff8ea ganymed

fix java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:16:58 -0700
parents d2b303406d63
children 42b15aaa7ac7
comparison
equal deleted inserted replaced
305:d2b303406d63 307:071eccdff8ea
63 } 63 }
64 64
65 if (kxs.np.server_host_key_algo.equals("ssh-rsa")) { 65 if (kxs.np.server_host_key_algo.equals("ssh-rsa")) {
66 byte[] rs = RSASHA1Verify.decodeSSHRSASignature(sig); 66 byte[] rs = RSASHA1Verify.decodeSSHRSASignature(sig);
67 RSAPublicKey rpk = RSASHA1Verify.decodeSSHRSAPublicKey(hostkey); 67 RSAPublicKey rpk = RSASHA1Verify.decodeSSHRSAPublicKey(hostkey);
68
69 log.debug("Verifying ssh-rsa signature"); 68 log.debug("Verifying ssh-rsa signature");
70
71 return RSASHA1Verify.verifySignature(kxs.H, rs, rpk); 69 return RSASHA1Verify.verifySignature(kxs.H, rs, rpk);
72 } 70 }
73 71
74 if (kxs.np.server_host_key_algo.equals("ssh-dss")) { 72 if (kxs.np.server_host_key_algo.equals("ssh-dss")) {
75 byte[] ds = DSASHA1Verify.decodeSSHDSASignature(sig); 73 byte[] ds = DSASHA1Verify.decodeSSHDSASignature(sig);
76 DSAPublicKey dpk = DSASHA1Verify.decodeSSHDSAPublicKey(hostkey); 74 DSAPublicKey dpk = DSASHA1Verify.decodeSSHDSAPublicKey(hostkey);
77
78 log.debug("Verifying ssh-dss signature"); 75 log.debug("Verifying ssh-dss signature");
79
80 return DSASHA1Verify.verifySignature(kxs.H, ds, dpk); 76 return DSASHA1Verify.verifySignature(kxs.H, ds, dpk);
81 } 77 }
82 78
83 throw new IOException("Unknown server host key algorithm '" + kxs.np.server_host_key_algo + "'"); 79 throw new IOException("Unknown server host key algorithm '" + kxs.np.server_host_key_algo + "'");
84 } 80 }
85 81
86 public void handleFailure(final IOException failure) { 82 public void handleFailure(final IOException failure) {
87 synchronized(accessLock) { 83 synchronized (accessLock) {
88 connectionClosed = true; 84 connectionClosed = true;
89 accessLock.notifyAll(); 85 accessLock.notifyAll();
90 } 86 }
91 } 87 }
92 88
93 public synchronized void handleMessage(byte[] msg) throws IOException { 89 public synchronized void handleMessage(byte[] msg) throws IOException {
94 PacketKexInit kip; 90 PacketKexInit kip;
95 91
96 if((kxs == null) && (msg[0] != Packets.SSH_MSG_KEXINIT)) { 92 if ((kxs == null) && (msg[0] != Packets.SSH_MSG_KEXINIT)) {
97 throw new PacketTypeException(msg[0]); 93 throw new PacketTypeException(msg[0]);
98 } 94 }
99 95
100 if(ignore_next_kex_packet) { 96 if (ignore_next_kex_packet) {
101 ignore_next_kex_packet = false; 97 ignore_next_kex_packet = false;
102 return; 98 return;
103 } 99 }
104 100
105 if(msg[0] == Packets.SSH_MSG_KEXINIT) { 101 if (msg[0] == Packets.SSH_MSG_KEXINIT) {
106 if((kxs != null) && (kxs.state != 0)) { 102 if ((kxs != null) && (kxs.state != 0)) {
107 throw new PacketTypeException(msg[0]); 103 throw new PacketTypeException(msg[0]);
108 } 104 }
109 105
110 if(kxs == null) { 106 if (kxs == null) {
111 /* 107 /*
112 * Ah, OK, peer wants to do KEX. Let's be nice and play 108 * Ah, OK, peer wants to do KEX. Let's be nice and play
113 * together. 109 * together.
114 */ 110 */
115 kxs = new KexState(); 111 kxs = new KexState();
116 kxs.dhgexParameters = nextKEXdhgexParameters; 112 kxs.dhgexParameters = nextKEXdhgexParameters;
117 kip = new PacketKexInit(nextKEXcryptoWishList, rnd); 113 kip = new PacketKexInit(nextKEXcryptoWishList, rnd);
118 kxs.localKEX = kip; 114 kxs.localKEX = kip;
119 tm.sendKexMessage(kip.getPayload()); 115 tm.sendKexMessage(kip.getPayload());
120 } 116 }
121 117
122 kip = new PacketKexInit(msg); 118 kip = new PacketKexInit(msg);
123 kxs.remoteKEX = kip; 119 kxs.remoteKEX = kip;
124
125 kxs.np = mergeKexParameters(kxs.localKEX.getKexParameters(), kxs.remoteKEX.getKexParameters()); 120 kxs.np = mergeKexParameters(kxs.localKEX.getKexParameters(), kxs.remoteKEX.getKexParameters());
126 121
127 if(kxs.remoteKEX.isFirst_kex_packet_follows() && (kxs.np.guessOK == false)) { 122 if (kxs.remoteKEX.isFirst_kex_packet_follows() && (kxs.np.guessOK == false)) {
128 // Guess was wrong, we need to ignore the next kex packet. 123 // Guess was wrong, we need to ignore the next kex packet.
129 ignore_next_kex_packet = true; 124 ignore_next_kex_packet = true;
130 } 125 }
131 126
132 if(kxs.np.kex_algo.equals("diffie-hellman-group-exchange-sha1")) { 127 if (kxs.np.kex_algo.equals("diffie-hellman-group-exchange-sha1")) {
133 if(kxs.dhgexParameters.getMin_group_len() == 0) { 128 if (kxs.dhgexParameters.getMin_group_len() == 0) {
134 PacketKexDhGexRequestOld dhgexreq = new PacketKexDhGexRequestOld(kxs.dhgexParameters); 129 PacketKexDhGexRequestOld dhgexreq = new PacketKexDhGexRequestOld(kxs.dhgexParameters);
135 tm.sendKexMessage(dhgexreq.getPayload()); 130 tm.sendKexMessage(dhgexreq.getPayload());
136
137 } 131 }
138 else { 132 else {
139 PacketKexDhGexRequest dhgexreq = new PacketKexDhGexRequest(kxs.dhgexParameters); 133 PacketKexDhGexRequest dhgexreq = new PacketKexDhGexRequest(kxs.dhgexParameters);
140 tm.sendKexMessage(dhgexreq.getPayload()); 134 tm.sendKexMessage(dhgexreq.getPayload());
141 } 135 }
136
142 kxs.state = 1; 137 kxs.state = 1;
143 return; 138 return;
144 } 139 }
145 140
146 if(kxs.np.kex_algo.equals("diffie-hellman-group1-sha1") 141 if (kxs.np.kex_algo.equals("diffie-hellman-group1-sha1")
147 || kxs.np.kex_algo.equals("diffie-hellman-group14-sha1")) { 142 || kxs.np.kex_algo.equals("diffie-hellman-group14-sha1")) {
148 kxs.dhx = new DhExchange(); 143 kxs.dhx = new DhExchange();
149 144
150 if(kxs.np.kex_algo.equals("diffie-hellman-group1-sha1")) { 145 if (kxs.np.kex_algo.equals("diffie-hellman-group1-sha1")) {
151 kxs.dhx.clientInit(1, rnd); 146 kxs.dhx.clientInit(1, rnd);
152 } 147 }
153 else { 148 else {
154 kxs.dhx.clientInit(14, rnd); 149 kxs.dhx.clientInit(14, rnd);
155 } 150 }
161 } 156 }
162 157
163 throw new IllegalStateException("Unkown KEX method!"); 158 throw new IllegalStateException("Unkown KEX method!");
164 } 159 }
165 160
166 if(msg[0] == Packets.SSH_MSG_NEWKEYS) { 161 if (msg[0] == Packets.SSH_MSG_NEWKEYS) {
167 if(km == null) { 162 if (km == null) {
168 throw new IOException("Peer sent SSH_MSG_NEWKEYS, but I have no key material ready!"); 163 throw new IOException("Peer sent SSH_MSG_NEWKEYS, but I have no key material ready!");
169 } 164 }
170 165
171 BlockCipher cbc; 166 BlockCipher cbc;
172 MAC mac; 167 MAC mac;
173 Compressor comp; 168 Compressor comp;
174 169
175 try { 170 try {
176 cbc = BlockCipherFactory.createCipher(kxs.np.enc_algo_server_to_client, false, 171 cbc = BlockCipherFactory.createCipher(kxs.np.enc_algo_server_to_client, false,
177 km.enc_key_server_to_client, km.initial_iv_server_to_client); 172 km.enc_key_server_to_client, km.initial_iv_server_to_client);
178 173
179 try { 174 try {
180 mac = new MAC(kxs.np.mac_algo_server_to_client, km.integrity_key_server_to_client); 175 mac = new MAC(kxs.np.mac_algo_server_to_client, km.integrity_key_server_to_client);
181 } 176 }
182 catch(DigestException e) { 177 catch (DigestException e) {
183 throw new IOException(e); 178 throw new IOException(e);
184 } 179 }
185 180
186 comp = CompressionFactory.createCompressor(kxs.np.comp_algo_server_to_client); 181 comp = CompressionFactory.createCompressor(kxs.np.comp_algo_server_to_client);
187 } 182 }
188 catch(IllegalArgumentException e) { 183 catch (IllegalArgumentException e) {
189 throw new IOException(e.getMessage()); 184 throw new IOException(e.getMessage());
190 } 185 }
191 186
192 tm.changeRecvCipher(cbc, mac); 187 tm.changeRecvCipher(cbc, mac);
193 tm.changeRecvCompression(comp); 188 tm.changeRecvCompression(comp);
194
195 ConnectionInfo sci = new ConnectionInfo(); 189 ConnectionInfo sci = new ConnectionInfo();
196
197 kexCount++; 190 kexCount++;
198
199 sci.keyExchangeAlgorithm = kxs.np.kex_algo; 191 sci.keyExchangeAlgorithm = kxs.np.kex_algo;
200 sci.keyExchangeCounter = kexCount; 192 sci.keyExchangeCounter = kexCount;
201 sci.clientToServerCryptoAlgorithm = kxs.np.enc_algo_client_to_server; 193 sci.clientToServerCryptoAlgorithm = kxs.np.enc_algo_client_to_server;
202 sci.serverToClientCryptoAlgorithm = kxs.np.enc_algo_server_to_client; 194 sci.serverToClientCryptoAlgorithm = kxs.np.enc_algo_server_to_client;
203 sci.clientToServerMACAlgorithm = kxs.np.mac_algo_client_to_server; 195 sci.clientToServerMACAlgorithm = kxs.np.mac_algo_client_to_server;
204 sci.serverToClientMACAlgorithm = kxs.np.mac_algo_server_to_client; 196 sci.serverToClientMACAlgorithm = kxs.np.mac_algo_server_to_client;
205 sci.serverHostKeyAlgorithm = kxs.np.server_host_key_algo; 197 sci.serverHostKeyAlgorithm = kxs.np.server_host_key_algo;
206 sci.serverHostKey = kxs.remote_hostkey; 198 sci.serverHostKey = kxs.remote_hostkey;
207 199
208 synchronized(accessLock) { 200 synchronized (accessLock) {
209 lastConnInfo = sci; 201 lastConnInfo = sci;
210 accessLock.notifyAll(); 202 accessLock.notifyAll();
211 } 203 }
212 204
213 kxs = null; 205 kxs = null;
214 return; 206 return;
215 } 207 }
216 208
217 if((kxs == null) || (kxs.state == 0)) { 209 if ((kxs == null) || (kxs.state == 0)) {
218 throw new IOException("Unexpected Kex submessage!"); 210 throw new IOException("Unexpected Kex submessage!");
219 } 211 }
220 212
221 if(kxs.np.kex_algo.equals("diffie-hellman-group-exchange-sha1")) { 213 if (kxs.np.kex_algo.equals("diffie-hellman-group-exchange-sha1")) {
222 if(kxs.state == 1) { 214 if (kxs.state == 1) {
223 PacketKexDhGexGroup dhgexgrp = new PacketKexDhGexGroup(msg); 215 PacketKexDhGexGroup dhgexgrp = new PacketKexDhGexGroup(msg);
224 kxs.dhgx = new DhGroupExchange(dhgexgrp.getP(), dhgexgrp.getG()); 216 kxs.dhgx = new DhGroupExchange(dhgexgrp.getP(), dhgexgrp.getG());
225 kxs.dhgx.init(rnd); 217 kxs.dhgx.init(rnd);
226 PacketKexDhGexInit dhgexinit = new PacketKexDhGexInit(kxs.dhgx.getE()); 218 PacketKexDhGexInit dhgexinit = new PacketKexDhGexInit(kxs.dhgx.getE());
227 tm.sendKexMessage(dhgexinit.getPayload()); 219 tm.sendKexMessage(dhgexinit.getPayload());
228 kxs.state = 2; 220 kxs.state = 2;
229 return; 221 return;
230 } 222 }
231 223
232 if(kxs.state == 2) { 224 if (kxs.state == 2) {
233 PacketKexDhGexReply dhgexrpl = new PacketKexDhGexReply(msg); 225 PacketKexDhGexReply dhgexrpl = new PacketKexDhGexReply(msg);
234
235 kxs.remote_hostkey = dhgexrpl.getHostKey(); 226 kxs.remote_hostkey = dhgexrpl.getHostKey();
236 227
237 if(verifier != null) { 228 if (verifier != null) {
238 try { 229 try {
239 if(!verifier.verifyServerHostKey(hostname, port, kxs.np.server_host_key_algo, kxs.remote_hostkey)) { 230 if (!verifier.verifyServerHostKey(hostname, port, kxs.np.server_host_key_algo, kxs.remote_hostkey)) {
240 throw new IOException("The server host key was not accepted by the verifier callback"); 231 throw new IOException("The server host key was not accepted by the verifier callback");
241 } 232 }
242 } 233 }
243 catch(Exception e) { 234 catch (Exception e) {
244 throw new IOException( 235 throw new IOException(
245 "The server host key was not accepted by the verifier callback.", e); 236 "The server host key was not accepted by the verifier callback.", e);
246 } 237 }
247 } 238 }
248 239
249 kxs.dhgx.setF(dhgexrpl.getF()); 240 kxs.dhgx.setF(dhgexrpl.getF());
250 241
251 try { 242 try {
252 kxs.H = kxs.dhgx.calculateH(csh.getClientString(), csh.getServerString(), 243 kxs.H = kxs.dhgx.calculateH(csh.getClientString(), csh.getServerString(),
253 kxs.localKEX.getPayload(), kxs.remoteKEX.getPayload(), dhgexrpl.getHostKey(), 244 kxs.localKEX.getPayload(), kxs.remoteKEX.getPayload(), dhgexrpl.getHostKey(),
254 kxs.dhgexParameters); 245 kxs.dhgexParameters);
255 } 246 }
256 catch(IllegalArgumentException e) { 247 catch (IllegalArgumentException e) {
257 throw new IOException("KEX error.", e); 248 throw new IOException("KEX error.", e);
258 } 249 }
259 if(!verifySignature(dhgexrpl.getSignature(), kxs.remote_hostkey)) { 250
251 if (!verifySignature(dhgexrpl.getSignature(), kxs.remote_hostkey)) {
260 throw new IOException("Invalid remote host key signature"); 252 throw new IOException("Invalid remote host key signature");
261 } 253 }
254
262 kxs.K = kxs.dhgx.getK(); 255 kxs.K = kxs.dhgx.getK();
263 finishKex(true); 256 finishKex(true);
264 kxs.state = -1; 257 kxs.state = -1;
265 return; 258 return;
266 } 259 }
267 260
268 throw new IllegalStateException("Illegal State in KEX Exchange!"); 261 throw new IllegalStateException("Illegal State in KEX Exchange!");
269 } 262 }
270 263
271 if(kxs.np.kex_algo.equals("diffie-hellman-group1-sha1") 264 if (kxs.np.kex_algo.equals("diffie-hellman-group1-sha1")
272 || kxs.np.kex_algo.equals("diffie-hellman-group14-sha1")) { 265 || kxs.np.kex_algo.equals("diffie-hellman-group14-sha1")) {
273 if(kxs.state == 1) { 266 if (kxs.state == 1) {
274
275 PacketKexDHReply dhr = new PacketKexDHReply(msg); 267 PacketKexDHReply dhr = new PacketKexDHReply(msg);
276
277 kxs.remote_hostkey = dhr.getHostKey(); 268 kxs.remote_hostkey = dhr.getHostKey();
278 269
279 if(verifier != null) { 270 if (verifier != null) {
280 try { 271 try {
281 if(!verifier.verifyServerHostKey(hostname, port, kxs.np.server_host_key_algo, kxs.remote_hostkey)) { 272 if (!verifier.verifyServerHostKey(hostname, port, kxs.np.server_host_key_algo, kxs.remote_hostkey)) {
282 throw new IOException("The server host key was not accepted by the verifier callback"); 273 throw new IOException("The server host key was not accepted by the verifier callback");
283 } 274 }
284 } 275 }
285 catch(Exception e) { 276 catch (Exception e) {
286 throw new IOException("The server host key was not accepted by the verifier callback", e); 277 throw new IOException("The server host key was not accepted by the verifier callback", e);
287 } 278 }
288 } 279 }
280
289 kxs.dhx.setF(dhr.getF()); 281 kxs.dhx.setF(dhr.getF());
282
290 try { 283 try {
291 kxs.H = kxs.dhx.calculateH(csh.getClientString(), csh.getServerString(), kxs.localKEX.getPayload(), 284 kxs.H = kxs.dhx.calculateH(csh.getClientString(), csh.getServerString(), kxs.localKEX.getPayload(),
292 kxs.remoteKEX.getPayload(), dhr.getHostKey()); 285 kxs.remoteKEX.getPayload(), dhr.getHostKey());
293 } 286 }
294 catch(IllegalArgumentException e) { 287 catch (IllegalArgumentException e) {
295 throw new IOException("KEX error.", e); 288 throw new IOException("KEX error.", e);
296 } 289 }
297 if(!verifySignature(dhr.getSignature(), kxs.remote_hostkey)) { 290
291 if (!verifySignature(dhr.getSignature(), kxs.remote_hostkey)) {
298 throw new IOException("Invalid remote host key signature"); 292 throw new IOException("Invalid remote host key signature");
299 } 293 }
294
300 kxs.K = kxs.dhx.getK(); 295 kxs.K = kxs.dhx.getK();
301 finishKex(true); 296 finishKex(true);
302 kxs.state = -1; 297 kxs.state = -1;
303 return; 298 return;
304 } 299 }
305 } 300 }
301
306 throw new IllegalStateException(String.format("Unknown KEX method %s", kxs.np.kex_algo)); 302 throw new IllegalStateException(String.format("Unknown KEX method %s", kxs.np.kex_algo));
307 } 303 }
308 } 304 }