001: package ch.ethz.ssh2.packets;
002:
003: import java.io.IOException;
004: import java.security.SecureRandom;
005:
006: import ch.ethz.ssh2.crypto.CryptoWishList;
007: import ch.ethz.ssh2.transport.KexParameters;
008:
009: /**
010: * PacketKexInit.
011: *
012: * @author Christian Plattner, plattner@inf.ethz.ch
013: * @version $Id: PacketKexInit.java,v 1.4 2006/02/14 19:43:15 cplattne Exp $
014: */
015: public class PacketKexInit {
016: byte[] payload;
017:
018: KexParameters kp = new KexParameters();
019:
020: public PacketKexInit(CryptoWishList cwl, SecureRandom rnd) {
021: kp.cookie = new byte[16];
022: rnd.nextBytes(kp.cookie);
023:
024: kp.kex_algorithms = cwl.kexAlgorithms;
025: kp.server_host_key_algorithms = cwl.serverHostKeyAlgorithms;
026: kp.encryption_algorithms_client_to_server = cwl.c2s_enc_algos;
027: kp.encryption_algorithms_server_to_client = cwl.s2c_enc_algos;
028: kp.mac_algorithms_client_to_server = cwl.c2s_mac_algos;
029: kp.mac_algorithms_server_to_client = cwl.s2c_mac_algos;
030: kp.compression_algorithms_client_to_server = new String[] { "none" };
031: kp.compression_algorithms_server_to_client = new String[] { "none" };
032: kp.languages_client_to_server = new String[] {};
033: kp.languages_server_to_client = new String[] {};
034: kp.first_kex_packet_follows = false;
035: kp.reserved_field1 = 0;
036: }
037:
038: public PacketKexInit(byte payload[], int off, int len)
039: throws IOException {
040: this .payload = new byte[len];
041: System.arraycopy(payload, off, this .payload, 0, len);
042:
043: TypesReader tr = new TypesReader(payload, off, len);
044:
045: int packet_type = tr.readByte();
046:
047: if (packet_type != Packets.SSH_MSG_KEXINIT)
048: throw new IOException("This is not a KexInitPacket! ("
049: + packet_type + ")");
050:
051: kp.cookie = tr.readBytes(16);
052: kp.kex_algorithms = tr.readNameList();
053: kp.server_host_key_algorithms = tr.readNameList();
054: kp.encryption_algorithms_client_to_server = tr.readNameList();
055: kp.encryption_algorithms_server_to_client = tr.readNameList();
056: kp.mac_algorithms_client_to_server = tr.readNameList();
057: kp.mac_algorithms_server_to_client = tr.readNameList();
058: kp.compression_algorithms_client_to_server = tr.readNameList();
059: kp.compression_algorithms_server_to_client = tr.readNameList();
060: kp.languages_client_to_server = tr.readNameList();
061: kp.languages_server_to_client = tr.readNameList();
062: kp.first_kex_packet_follows = tr.readBoolean();
063: kp.reserved_field1 = tr.readUINT32();
064:
065: if (tr.remain() != 0)
066: throw new IOException("Padding in KexInitPacket!");
067: }
068:
069: public byte[] getPayload() {
070: if (payload == null) {
071: TypesWriter tw = new TypesWriter();
072: tw.writeByte(Packets.SSH_MSG_KEXINIT);
073: tw.writeBytes(kp.cookie, 0, 16);
074: tw.writeNameList(kp.kex_algorithms);
075: tw.writeNameList(kp.server_host_key_algorithms);
076: tw.writeNameList(kp.encryption_algorithms_client_to_server);
077: tw.writeNameList(kp.encryption_algorithms_server_to_client);
078: tw.writeNameList(kp.mac_algorithms_client_to_server);
079: tw.writeNameList(kp.mac_algorithms_server_to_client);
080: tw
081: .writeNameList(kp.compression_algorithms_client_to_server);
082: tw
083: .writeNameList(kp.compression_algorithms_server_to_client);
084: tw.writeNameList(kp.languages_client_to_server);
085: tw.writeNameList(kp.languages_server_to_client);
086: tw.writeBoolean(kp.first_kex_packet_follows);
087: tw.writeUINT32(kp.reserved_field1);
088: payload = tw.getBytes();
089: }
090: return payload;
091: }
092:
093: public KexParameters getKexParameters() {
094: return kp;
095: }
096:
097: public String[] getCompression_algorithms_client_to_server() {
098: return kp.compression_algorithms_client_to_server;
099: }
100:
101: public String[] getCompression_algorithms_server_to_client() {
102: return kp.compression_algorithms_server_to_client;
103: }
104:
105: public byte[] getCookie() {
106: return kp.cookie;
107: }
108:
109: public String[] getEncryption_algorithms_client_to_server() {
110: return kp.encryption_algorithms_client_to_server;
111: }
112:
113: public String[] getEncryption_algorithms_server_to_client() {
114: return kp.encryption_algorithms_server_to_client;
115: }
116:
117: public boolean isFirst_kex_packet_follows() {
118: return kp.first_kex_packet_follows;
119: }
120:
121: public String[] getKex_algorithms() {
122: return kp.kex_algorithms;
123: }
124:
125: public String[] getLanguages_client_to_server() {
126: return kp.languages_client_to_server;
127: }
128:
129: public String[] getLanguages_server_to_client() {
130: return kp.languages_server_to_client;
131: }
132:
133: public String[] getMac_algorithms_client_to_server() {
134: return kp.mac_algorithms_client_to_server;
135: }
136:
137: public String[] getMac_algorithms_server_to_client() {
138: return kp.mac_algorithms_server_to_client;
139: }
140:
141: public int getReserved_field1() {
142: return kp.reserved_field1;
143: }
144:
145: public String[] getServer_host_key_algorithms() {
146: return kp.server_host_key_algorithms;
147: }
148: }
|