SSH.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.io.IOException;
  3. import java.math.BigInteger;
  4. import java.nio.ByteBuffer;
  5. import java.security.SecureRandom;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  9. import de.tudarmstadt.informatik.hostage.ssh.crypto.KeyMaterial;
  10. import de.tudarmstadt.informatik.hostage.ssh.crypto.PEMDecoder;
  11. import de.tudarmstadt.informatik.hostage.ssh.crypto.cipher.CBCMode;
  12. import de.tudarmstadt.informatik.hostage.ssh.crypto.cipher.DESede;
  13. import de.tudarmstadt.informatik.hostage.ssh.crypto.dh.DhExchange;
  14. import de.tudarmstadt.informatik.hostage.ssh.crypto.digest.MAC;
  15. import de.tudarmstadt.informatik.hostage.ssh.signature.DSAPrivateKey;
  16. import de.tudarmstadt.informatik.hostage.ssh.signature.DSASHA1Verify;
  17. import de.tudarmstadt.informatik.hostage.ssh.signature.DSASignature;
  18. import de.tudarmstadt.informatik.hostage.ssh.util.TypesReader;
  19. import de.tudarmstadt.informatik.hostage.ssh.util.TypesWriter;
  20. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  21. /**
  22. * SSH protocol. Implementation of RFC documents 4250, 4251, 4252, 4253, 4254.
  23. * It can handle the following requests: Server Protocol, Key Exchange Init,
  24. * Diffie-Hellman Key Exchange Init, New Keys, Service Request, Connection
  25. * Request, Channel Open Request, Channel Request.
  26. *
  27. * @author Wulf Pfeiffer
  28. */
  29. public class SSH implements Protocol {
  30. /**
  31. * Represents the states of the protocol.
  32. */
  33. private enum STATE {
  34. NONE, SERVER_VERSION, CLIENT_VERSION, KEX_INIT, NEW_KEYS, USERAUTH, CONNECTION, CHANNEL, TERMINAL_CMD, TERMINAL_ENTER, CLOSED
  35. }
  36. /**
  37. * Denotes in which state the protocol is right now.
  38. */
  39. private STATE state = STATE.NONE;
  40. private boolean useEncryption = false;
  41. // version stuff
  42. private String[][][] possibleSshTypes = {
  43. { { "3." }, { "4", "5", "6", "7", "8", "9" } },
  44. { { "4." }, { "0", "1", "2", "3", "4", "5", "6", "7", "9" } },
  45. { { "5." }, { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" } },
  46. { { "6." }, { "0", "1", "2", "3", "4" } } };
  47. private String initSshType() {
  48. SecureRandom rnd = new SecureRandom();
  49. int majorVersion = rnd.nextInt(possibleSshTypes.length);
  50. return "OpenSSH_"
  51. + possibleSshTypes[majorVersion][0][0]
  52. + possibleSshTypes[majorVersion][1][rnd
  53. .nextInt(possibleSshTypes[majorVersion][1].length)];
  54. }
  55. // server infos
  56. private static final String serverVersion = "SSH-2.0-";
  57. private String serverType = initSshType();
  58. private String serverName = HelperUtils.getRandomString(16, false);
  59. private int packetNumber = 0;
  60. private int recipientChannel;
  61. private String userName;
  62. private String terminalPrefix;
  63. private StringBuffer command = new StringBuffer();
  64. private SecureRandom random = new SecureRandom();
  65. // SSH Parameters for Kex etc.
  66. private byte[] V_S = (serverVersion + serverType).getBytes();
  67. private byte[] V_C;
  68. private byte[] I_S;
  69. private byte[] I_C;
  70. private byte[] e;
  71. private BigInteger f;
  72. private byte[] h;
  73. private BigInteger k;
  74. private byte[] K_S;
  75. private byte[] signature;
  76. // allowed algorithms for kexinit
  77. private static final String KEX_ALG = "diffie-hellman-group1-sha1";
  78. private static final String SERVER_ALG = "ssh-dss";
  79. private static final String ENCRYPT_ALG_C = "3des-cbc";
  80. private static final String ENCRYPT_ALG_S = "3des-cbc";
  81. private static final String MAC_ALG_C = "hmac-sha1";
  82. private static final String MAC_ALG_S = "hmac-sha1";
  83. private static final String COMP_ALG_C = "none";
  84. private static final String COMP_ALG_S = "none";
  85. private int cipherBlockSize = 16;
  86. // for en- and decryption
  87. private DESede desEncryption;
  88. private DESede desDecryption;
  89. private CBCMode cbcEncryption;
  90. private CBCMode cbcDecryption;
  91. private MAC macEncryption;
  92. // private MAC macDec;
  93. // dsa private key
  94. private static final char[] dsaPem = ("-----BEGIN DSA PRIVATE KEY-----\n"
  95. + "MIIBugIBAAKBgQCDZ9R2vfCPwjv5vKF1igIv9drrZ7G0dhMkGT9AZTjgI34Qm4w0\n"
  96. + "0iWeCqO7SmqiaMIjbRIm91MeDed4ObAq4sAkqRE/2P4mTbzFx5KhEczRRiDoqQBX\n"
  97. + "xYa0yWKJpeZ94SGM6DEPuBTxKo0T4uMjbq2FzHL2FXT1/WoNCmRU6gFSiwIVAMK4\n"
  98. + "Epz3JiwDUbkSpLOjIqtEhJmVAoGAL6zlXRI4Q8iwvSDh0vDf1j9a5Aaaq+93LTjK\n"
  99. + "SwL4nvUWBl2Aa0vqu05ZS5rOD1I+/naLMg0fNgFJRhA03sl+12MI3a2HXJWXRSdj\n"
  100. + "m1Vq9cUXqiYrX6+iGfEaA/y9UO4ZPF6if6eLypXB8VuqjtjDCiMMsM6+qQki7L71\n"
  101. + "yN4M75ICgYAcFXUhN2zRug3JvwmGxW8gMgHquSiBnbx1582KGh2B/ukE/kOrbKYD\n"
  102. + "HUkBzolcm4x1Odq5apowlriFxY6zMQP615plIK4x9NaU6dvc/HoTkjzT5EYSMN39\n"
  103. + "eAGufJ0jrtIpKL4lP8o8yrAHfmbR7bjecWc0viTH0+OWlyVsex/bZAIUEKn310Li\n"
  104. + "v62Zs4hlDvhwvx8MQ+A=\n" + "-----END DSA PRIVATE KEY-----")
  105. .toCharArray();
  106. @Override
  107. public int getPort() {
  108. return 22;
  109. }
  110. @Override
  111. public TALK_FIRST whoTalksFirst() {
  112. return TALK_FIRST.CLIENT;
  113. }
  114. @Override
  115. public List<Packet> processMessage(Packet requestPacket) {
  116. List<Packet> responsePackets = new ArrayList<Packet>();
  117. byte[] request = null;
  118. if (requestPacket != null) {
  119. request = requestPacket.getBytes();
  120. if (useEncryption) {
  121. request = decryptBytes(request);
  122. }
  123. }
  124. switch (state) {
  125. case NONE:
  126. extractType(request);
  127. responsePackets.add(new Packet(serverVersion + serverType + "\r\n",
  128. toString()));
  129. state = STATE.SERVER_VERSION;
  130. break;
  131. case SERVER_VERSION:
  132. extractPayload(request);
  133. responsePackets.add(kexInit());
  134. state = STATE.CLIENT_VERSION;
  135. break;
  136. case CLIENT_VERSION:
  137. extractPubKey(request);
  138. responsePackets.add(dhKexReply());
  139. state = STATE.KEX_INIT;
  140. break;
  141. case KEX_INIT:
  142. responsePackets.add(newKeys());
  143. useEncryption = true;
  144. state = STATE.NEW_KEYS;
  145. break;
  146. case NEW_KEYS:
  147. responsePackets.add(serviceReply(request));
  148. state = STATE.USERAUTH;
  149. break;
  150. case USERAUTH:
  151. responsePackets.add(connectionReply(request));
  152. state = STATE.CONNECTION;
  153. break;
  154. case CONNECTION:
  155. responsePackets.add(channelOpenReply(request));
  156. state = STATE.CHANNEL;
  157. break;
  158. case CHANNEL:
  159. responsePackets.add(channelSuccessReply(request));
  160. responsePackets.add(terminalPrefix());
  161. state = STATE.TERMINAL_CMD;
  162. break;
  163. case TERMINAL_CMD:
  164. responsePackets.add(terminalReply(request));
  165. break;
  166. case CLOSED:
  167. break;
  168. default:
  169. state = STATE.CLOSED;
  170. break;
  171. }
  172. return responsePackets;
  173. }
  174. @Override
  175. public boolean isClosed() {
  176. return (state == STATE.CLOSED);
  177. }
  178. @Override
  179. public boolean isSecure() {
  180. return false;
  181. }
  182. @Override
  183. public String toString() {
  184. return "SSH";
  185. }
  186. /**
  187. * Wraps the packets with packet length and padding.
  188. *
  189. * @param response
  190. * content that is wrapped.
  191. * @return wrapped packet.
  192. */
  193. private Packet wrapPacket(byte[] response) {
  194. // 4 byte packet length, 1 byte padding length, payload length
  195. int packetLength = 5 + response.length;
  196. int paddingLengthCBS = cipherBlockSize
  197. - (packetLength % cipherBlockSize);
  198. int paddingLength8 = 8 - (packetLength % 8);
  199. int paddingLength = paddingLengthCBS > paddingLength8 ? paddingLengthCBS
  200. : paddingLength8;
  201. if (paddingLength < 4)
  202. paddingLength += cipherBlockSize;
  203. // add padding string length to packet length
  204. packetLength = packetLength + paddingLength - 4;
  205. byte[] packetLen = ByteBuffer.allocate(4).putInt(packetLength).array();
  206. byte[] paddingLen = { (byte) paddingLength };
  207. byte[] paddingString = HelperUtils.randomBytes(paddingLength);
  208. byte[] wrappedResponse = HelperUtils.concat(packetLen, paddingLen,
  209. response, paddingString);
  210. if (useEncryption) {
  211. byte[] mac = createMac(wrappedResponse);
  212. byte[] responseEnc = encryptBytes(wrappedResponse);
  213. wrappedResponse = HelperUtils.concat(responseEnc, mac);
  214. }
  215. packetNumber++;
  216. return new Packet(wrappedResponse, toString());
  217. }
  218. /**
  219. * Encrypts a request with triple DES.
  220. *
  221. * @param request
  222. * that is encrypted.
  223. * @return encrypted request.
  224. */
  225. private byte[] encryptBytes(byte[] bytes) {
  226. byte[] responseEncrypted = new byte[bytes.length];
  227. for (int i = 0; i < bytes.length; i += 8) {
  228. cbcEncryption.transformBlock(bytes, i, responseEncrypted, i);
  229. }
  230. return responseEncrypted;
  231. }
  232. /**
  233. * Decrypts a request with triple DES.
  234. *
  235. * @param request
  236. * that is decrypted.
  237. * @return decrypted request.
  238. */
  239. private byte[] decryptBytes(byte[] request) {
  240. byte[] decryptedRequest = new byte[request.length
  241. - ((request.length % 8 == 0) ? 0 : 20)];
  242. for (int i = 0; i < decryptedRequest.length; i += 8) {
  243. cbcDecryption.transformBlock(request, i, decryptedRequest, i);
  244. }
  245. return decryptedRequest;
  246. }
  247. /**
  248. * Creates the SHA1 Mac with the given bytes.
  249. *
  250. * @param bytes
  251. * that are used for the Mac.
  252. * @return Mac.
  253. */
  254. private byte[] createMac(byte[] bytes) {
  255. byte[] mac = new byte[20];
  256. macEncryption.initMac(packetNumber);
  257. macEncryption.update(bytes, 0, bytes.length);
  258. macEncryption.getMac(mac, 0);
  259. return mac;
  260. }
  261. /**
  262. * Builds the Kex Init packet that contains all the allowed algorithms by
  263. * the server.
  264. *
  265. * @return Kex Init packet.
  266. */
  267. private Packet kexInit() {
  268. TypesWriter tw = new TypesWriter();
  269. tw.writeByte(0x14);
  270. // cookie
  271. tw.writeBytes(HelperUtils.randomBytes(16));
  272. tw.writeString(KEX_ALG);
  273. tw.writeString(SERVER_ALG);
  274. tw.writeString(ENCRYPT_ALG_C);
  275. tw.writeString(ENCRYPT_ALG_S);
  276. tw.writeString(MAC_ALG_C);
  277. tw.writeString(MAC_ALG_S);
  278. tw.writeString(COMP_ALG_C);
  279. tw.writeString(COMP_ALG_S);
  280. // language client to server
  281. tw.writeBytes(new byte[] { 0x00, 0x00, 0x00, 0x00 });
  282. // language server to client
  283. tw.writeBytes(new byte[] { 0x00, 0x00, 0x00, 0x00 });
  284. // no guess from server
  285. tw.writeByte(0x00);
  286. // reserved
  287. tw.writeBytes(new byte[] { 0x00, 0x00, 0x00, 0x00 });
  288. byte[] response = tw.getBytes();
  289. I_S = response;
  290. return wrapPacket(response);
  291. }
  292. /**
  293. * Builds the Diffie-Hellman Kex Reply, containing the host key,f and the
  294. * signature.
  295. *
  296. * @return Diffie-Hellman Kex Reply packet.
  297. */
  298. private Packet dhKexReply() {
  299. byte[] response = null;
  300. try {
  301. DhExchange dhx = new DhExchange();
  302. dhx.serverInit(1, random);
  303. dhx.setE(new BigInteger(e));
  304. f = dhx.getF();
  305. DSAPrivateKey dsa = (DSAPrivateKey) PEMDecoder.decode(dsaPem, null);
  306. K_S = DSASHA1Verify.encodeSSHDSAPublicKey(dsa.getPublicKey());
  307. h = dhx.calculateH(V_C, V_S, I_C, I_S, K_S);
  308. k = dhx.getK();
  309. DSASignature ds = DSASHA1Verify.generateSignature(h, dsa, random);
  310. signature = DSASHA1Verify.encodeSSHDSASignature(ds);
  311. TypesWriter tw = new TypesWriter();
  312. tw.writeByte(31);
  313. tw.writeString(K_S, 0, K_S.length);
  314. tw.writeMPInt(f);
  315. tw.writeString(signature, 0, signature.length);
  316. response = tw.getBytes();
  317. // init for decryption and encryption
  318. // KeyMaterial: alg, h, k, keylength, blocklength, maclength,
  319. // keylength, blocklength, maclength
  320. KeyMaterial km = KeyMaterial.create("SHA1", h, k, h, 24, 8, 20, 24,
  321. 8, 20);
  322. desEncryption = new DESede();
  323. desDecryption = new DESede();
  324. desEncryption.init(true, km.enc_key_server_to_client);
  325. desDecryption.init(false, km.enc_key_client_to_server);
  326. cbcEncryption = new CBCMode(desEncryption,
  327. km.initial_iv_server_to_client, true);
  328. cbcDecryption = new CBCMode(desDecryption,
  329. km.initial_iv_client_to_server, false);
  330. macEncryption = new MAC("hmac-sha1",
  331. km.integrity_key_server_to_client);
  332. } catch (Exception e) {
  333. e.printStackTrace();
  334. }
  335. return wrapPacket(response);
  336. }
  337. /**
  338. * New Keys response.
  339. *
  340. * @return New Keys response.
  341. */
  342. private Packet newKeys() {
  343. byte[] msgCode = { 0x15 };
  344. return wrapPacket(msgCode);
  345. }
  346. /**
  347. * Service ssh-userauth reply.
  348. *
  349. * @param request
  350. * from the client.
  351. * @return Service reply.
  352. */
  353. private Packet serviceReply(byte[] request) {
  354. byte[] message;
  355. // if newkeys request is included in the same packet remove it
  356. if (request[5] == 0x15) {
  357. message = new byte[request.length - 16];
  358. System.arraycopy(request, 16, message, 0, request.length - 16);
  359. } else {
  360. message = request;
  361. }
  362. if (message[5] != 0x05
  363. && !(HelperUtils.byteToStr(message).contains("ssh-userauth"))) {
  364. // disconnect because its not servicerequest ssh-userauth
  365. return disconnectReply(7);
  366. }
  367. TypesWriter tw = new TypesWriter();
  368. tw.writeByte(0x06);
  369. tw.writeString("ssh-userauth");
  370. return wrapPacket(tw.getBytes());
  371. }
  372. /**
  373. * Userauth ssh-connection reply.
  374. *
  375. * @param request
  376. * from the client.
  377. * @return ssh-connection reply.
  378. */
  379. private Packet connectionReply(byte[] request) {
  380. if (request[5] != 0x32
  381. && !(HelperUtils.byteToStr(request).contains("ssh-connection"))) {
  382. // disconnect because its not servicerequest ssh-connect
  383. return disconnectReply(14);
  384. }
  385. try {
  386. TypesReader tr = new TypesReader(request, 6);
  387. userName = tr.readString();
  388. terminalPrefix = "[" + userName + "@" + serverName + " ~]$ ";
  389. } catch (IOException e) {
  390. e.printStackTrace();
  391. }
  392. byte[] msgcode = { 0x34 };
  393. return wrapPacket(msgcode);
  394. }
  395. /**
  396. * Channel Open Reply.
  397. *
  398. * @param request
  399. * from client.
  400. * @return Channel Open Reply.
  401. */
  402. private Packet channelOpenReply(byte[] request) {
  403. if (!(HelperUtils.byteToStr(request).contains("session"))) {
  404. // if contains "session" ok else disc
  405. return disconnectReply(2);
  406. }
  407. TypesReader tr = new TypesReader(request, 6);
  408. TypesWriter tw = new TypesWriter();
  409. try {
  410. tr.readString();
  411. recipientChannel = tr.readUINT32();
  412. int senderChannel = recipientChannel;
  413. int initialWindowSize = tr.readUINT32();
  414. int maximumPacketSize = tr.readUINT32();
  415. // msgcode
  416. tw.writeByte(0x5b);
  417. tw.writeUINT32(recipientChannel);
  418. tw.writeUINT32(senderChannel);
  419. tw.writeUINT32(initialWindowSize);
  420. tw.writeUINT32(maximumPacketSize);
  421. } catch (IOException e) {
  422. e.printStackTrace();
  423. }
  424. return wrapPacket(tw.getBytes());
  425. }
  426. /**
  427. * Channel Success Reply.
  428. *
  429. * @param request
  430. * from client.
  431. * @return Channel Success Reply.
  432. */
  433. private Packet channelSuccessReply(byte[] request) {
  434. if (!(HelperUtils.byteToStr(request)).contains("pty-req")) {
  435. return disconnectReply(2);
  436. }
  437. TypesWriter tw = new TypesWriter();
  438. // msgcode
  439. tw.writeByte(0x63);
  440. tw.writeUINT32(recipientChannel);
  441. return wrapPacket(tw.getBytes());
  442. }
  443. /**
  444. * Returns the terminal prefix for the client.
  445. *
  446. * @return terminal prefix.
  447. */
  448. private Packet terminalPrefix() {
  449. TypesWriter tw = new TypesWriter();
  450. tw.writeByte(0x5e);
  451. tw.writeUINT32(recipientChannel);
  452. tw.writeString(terminalPrefix);
  453. return wrapPacket(tw.getBytes());
  454. }
  455. /**
  456. * Computes the reply for the client input.
  457. *
  458. * @param request
  459. * client input.
  460. * @return input reply.
  461. */
  462. private Packet terminalReply(byte[] request) {
  463. TypesReader tr = new TypesReader(request, 6);
  464. String message = "";
  465. try {
  466. tr.readUINT32();
  467. message = tr.readString();
  468. if (message.contains("\r")) {
  469. if (command.toString().contains("exit")) {
  470. state = STATE.CLOSED;
  471. return disconnectReply(2);
  472. }
  473. message = "\r\nbash: " + command + " :command not found\r\n"
  474. + terminalPrefix;
  475. command = new StringBuffer();
  476. } else if (message.contains(new String(new char[] { '\u007F' }))
  477. && command.length() > 0) {
  478. command = command
  479. .delete(command.length() - 1, command.length());
  480. } else {
  481. command.append(message);
  482. }
  483. } catch (IOException e) {
  484. e.printStackTrace();
  485. }
  486. TypesWriter tw = new TypesWriter();
  487. // msgcode
  488. tw.writeByte(0x5e);
  489. tw.writeUINT32(recipientChannel);
  490. tw.writeString(message);
  491. return wrapPacket(tw.getBytes());
  492. }
  493. /**
  494. * Disconnect Reply using the given number as reason code.
  495. *
  496. * @param reasonCode
  497. * for disconnect reply. Must be between 1 and 15, default is 2.
  498. * @return Disconnect Reply.
  499. */
  500. private Packet disconnectReply(int reasonCode) {
  501. TypesWriter tw = new TypesWriter();
  502. tw.writeByte(0x01);
  503. switch (reasonCode) {
  504. case 1:
  505. tw.writeUINT32(1);
  506. tw.writeString("SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT");
  507. break;
  508. case 7:
  509. tw.writeUINT32(7);
  510. tw.writeString("SSH_DISCONNECT_SERVICE_NOT_AVAILABLE");
  511. break;
  512. case 14:
  513. tw.writeUINT32(14);
  514. tw.writeString("SSH_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE");
  515. break;
  516. default:
  517. tw.writeUINT32(2);
  518. tw.writeString("SSH_DISCONNECT_PROTOCOL_ERROR");
  519. break;
  520. }
  521. return wrapPacket(tw.getBytes());
  522. }
  523. /**
  524. * Extracts the type of the client
  525. *
  526. * @param request
  527. * containing the clients type
  528. */
  529. private void extractType(byte[] request) {
  530. int length = 0;
  531. for (int i = 0; i < request.length; i++, length++) {
  532. // find the end of the type: '\r'
  533. if (request[i] == 0x0d)
  534. break;
  535. }
  536. V_C = new byte[length];
  537. System.arraycopy(request, 0, V_C, 0, length);
  538. }
  539. /**
  540. * Extracts the payload of a packet and writes it in I_C.
  541. *
  542. * @param request
  543. * packet of which the payload is extracted.
  544. */
  545. private void extractPayload(byte[] request) {
  546. int position = 0;
  547. if (request[5] != 0x14) {
  548. position = 1;
  549. for (int i = 0; i < request.length; i++, position++) {
  550. if (request[i] == 0x0a)
  551. break;
  552. }
  553. }
  554. int packetLength = byteToInt(new byte[] { request[position],
  555. request[1 + position], request[2 + position],
  556. request[3 + position] });
  557. int paddingLength = byteToInt(new byte[] { request[4 + position] });
  558. byte[] payload = new byte[packetLength - paddingLength - 1];
  559. for (int i = 5; i < packetLength - paddingLength - 1; i++) {
  560. payload[i - 5] = request[i + position];
  561. }
  562. I_C = payload;
  563. }
  564. /**
  565. * Extracts the public key from the DH Kex Request
  566. *
  567. * @param request
  568. * containing the clients public key
  569. */
  570. private void extractPubKey(byte[] request) {
  571. e = new byte[byteToInt(new byte[] { request[6], request[7], request[8],
  572. request[9] })];
  573. for (int i = 0; i < e.length; i++) {
  574. e[i] = request[i + 10];
  575. }
  576. }
  577. /**
  578. * Converts a byte[] to int
  579. *
  580. * @param bytes
  581. * that are converted
  582. * @return converted byte[] as int
  583. */
  584. private static int byteToInt(byte[] bytes) {
  585. int convertedInteger = 0;
  586. for (int i = 0; i < bytes.length; i++) {
  587. convertedInteger <<= 8;
  588. convertedInteger |= bytes[i] & 0xFF;
  589. }
  590. return convertedInteger;
  591. }
  592. }