• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3  * Please refer to the LICENSE.txt for licensing details.
4  */
5 package ch.ethz.ssh2.packets;
6 
7 import java.io.IOException;
8 import java.security.SecureRandom;
9 
10 import ch.ethz.ssh2.crypto.CryptoWishList;
11 import ch.ethz.ssh2.transport.KexParameters;
12 
13 /**
14  * PacketKexInit.
15  *
16  * @author Christian Plattner
17  * @version 2.50, 03/15/10
18  */
19 public class PacketKexInit
20 {
21 	byte[] payload;
22 
23 	KexParameters kp = new KexParameters();
24 
PacketKexInit(CryptoWishList cwl, SecureRandom rnd)25 	public PacketKexInit(CryptoWishList cwl, SecureRandom rnd)
26 	{
27 		kp.cookie = new byte[16];
28 		rnd.nextBytes(kp.cookie);
29 
30 		kp.kex_algorithms = cwl.kexAlgorithms;
31 		kp.server_host_key_algorithms = cwl.serverHostKeyAlgorithms;
32 		kp.encryption_algorithms_client_to_server = cwl.c2s_enc_algos;
33 		kp.encryption_algorithms_server_to_client = cwl.s2c_enc_algos;
34 		kp.mac_algorithms_client_to_server = cwl.c2s_mac_algos;
35 		kp.mac_algorithms_server_to_client = cwl.s2c_mac_algos;
36 		kp.compression_algorithms_client_to_server = new String[] { "none" };
37 		kp.compression_algorithms_server_to_client = new String[] { "none" };
38 		kp.languages_client_to_server = new String[] {};
39 		kp.languages_server_to_client = new String[] {};
40 		kp.first_kex_packet_follows = false;
41 		kp.reserved_field1 = 0;
42 	}
43 
PacketKexInit(byte payload[], int off, int len)44 	public PacketKexInit(byte payload[], int off, int len) throws IOException
45 	{
46 		this.payload = new byte[len];
47 		System.arraycopy(payload, off, this.payload, 0, len);
48 
49 		TypesReader tr = new TypesReader(payload, off, len);
50 
51 		int packet_type = tr.readByte();
52 
53 		if (packet_type != Packets.SSH_MSG_KEXINIT)
54 			throw new IOException("This is not a KexInitPacket! (" + packet_type + ")");
55 
56 		kp.cookie = tr.readBytes(16);
57 		kp.kex_algorithms = tr.readNameList();
58 		kp.server_host_key_algorithms = tr.readNameList();
59 		kp.encryption_algorithms_client_to_server = tr.readNameList();
60 		kp.encryption_algorithms_server_to_client = tr.readNameList();
61 		kp.mac_algorithms_client_to_server = tr.readNameList();
62 		kp.mac_algorithms_server_to_client = tr.readNameList();
63 		kp.compression_algorithms_client_to_server = tr.readNameList();
64 		kp.compression_algorithms_server_to_client = tr.readNameList();
65 		kp.languages_client_to_server = tr.readNameList();
66 		kp.languages_server_to_client = tr.readNameList();
67 		kp.first_kex_packet_follows = tr.readBoolean();
68 		kp.reserved_field1 = tr.readUINT32();
69 
70 		if (tr.remain() != 0)
71 			throw new IOException("Padding in KexInitPacket!");
72 	}
73 
getPayload()74 	public byte[] getPayload()
75 	{
76 		if (payload == null)
77 		{
78 			TypesWriter tw = new TypesWriter();
79 			tw.writeByte(Packets.SSH_MSG_KEXINIT);
80 			tw.writeBytes(kp.cookie, 0, 16);
81 			tw.writeNameList(kp.kex_algorithms);
82 			tw.writeNameList(kp.server_host_key_algorithms);
83 			tw.writeNameList(kp.encryption_algorithms_client_to_server);
84 			tw.writeNameList(kp.encryption_algorithms_server_to_client);
85 			tw.writeNameList(kp.mac_algorithms_client_to_server);
86 			tw.writeNameList(kp.mac_algorithms_server_to_client);
87 			tw.writeNameList(kp.compression_algorithms_client_to_server);
88 			tw.writeNameList(kp.compression_algorithms_server_to_client);
89 			tw.writeNameList(kp.languages_client_to_server);
90 			tw.writeNameList(kp.languages_server_to_client);
91 			tw.writeBoolean(kp.first_kex_packet_follows);
92 			tw.writeUINT32(kp.reserved_field1);
93 			payload = tw.getBytes();
94 		}
95 		return payload;
96 	}
97 
getKexParameters()98 	public KexParameters getKexParameters()
99 	{
100 		return kp;
101 	}
102 
getCompression_algorithms_client_to_server()103 	public String[] getCompression_algorithms_client_to_server()
104 	{
105 		return kp.compression_algorithms_client_to_server;
106 	}
107 
getCompression_algorithms_server_to_client()108 	public String[] getCompression_algorithms_server_to_client()
109 	{
110 		return kp.compression_algorithms_server_to_client;
111 	}
112 
getCookie()113 	public byte[] getCookie()
114 	{
115 		return kp.cookie;
116 	}
117 
getEncryption_algorithms_client_to_server()118 	public String[] getEncryption_algorithms_client_to_server()
119 	{
120 		return kp.encryption_algorithms_client_to_server;
121 	}
122 
getEncryption_algorithms_server_to_client()123 	public String[] getEncryption_algorithms_server_to_client()
124 	{
125 		return kp.encryption_algorithms_server_to_client;
126 	}
127 
isFirst_kex_packet_follows()128 	public boolean isFirst_kex_packet_follows()
129 	{
130 		return kp.first_kex_packet_follows;
131 	}
132 
getKex_algorithms()133 	public String[] getKex_algorithms()
134 	{
135 		return kp.kex_algorithms;
136 	}
137 
getLanguages_client_to_server()138 	public String[] getLanguages_client_to_server()
139 	{
140 		return kp.languages_client_to_server;
141 	}
142 
getLanguages_server_to_client()143 	public String[] getLanguages_server_to_client()
144 	{
145 		return kp.languages_server_to_client;
146 	}
147 
getMac_algorithms_client_to_server()148 	public String[] getMac_algorithms_client_to_server()
149 	{
150 		return kp.mac_algorithms_client_to_server;
151 	}
152 
getMac_algorithms_server_to_client()153 	public String[] getMac_algorithms_server_to_client()
154 	{
155 		return kp.mac_algorithms_server_to_client;
156 	}
157 
getReserved_field1()158 	public int getReserved_field1()
159 	{
160 		return kp.reserved_field1;
161 	}
162 
getServer_host_key_algorithms()163 	public String[] getServer_host_key_algorithms()
164 	{
165 		return kp.server_host_key_algorithms;
166 	}
167 }
168