Mercurial > dropbear
comparison svr-kex.c @ 26:0969767bca0d
snapshot of stuff
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 26 Jul 2004 02:44:20 +0000 |
parents | 469950e86d0f |
children | e3adf4cf5465 |
comparison
equal
deleted
inserted
replaced
25:e4b6e2d569b2 | 26:0969767bca0d |
---|---|
68 * result is sent to the client. | 68 * result is sent to the client. |
69 * | 69 * |
70 * See the ietf-secsh-transport draft, section 6, for details */ | 70 * See the ietf-secsh-transport draft, section 6, for details */ |
71 static void send_msg_kexdh_reply(mp_int *dh_e) { | 71 static void send_msg_kexdh_reply(mp_int *dh_e) { |
72 | 72 |
73 mp_int dh_p, dh_q, dh_g, dh_y, dh_f; | 73 mp_int dh_y, dh_f; |
74 unsigned char randbuf[DH_P_LEN]; | |
75 int dh_q_len; | |
76 hash_state hs; | |
77 | 74 |
78 TRACE(("enter send_msg_kexdh_reply")); | 75 TRACE(("enter send_msg_kexdh_reply")); |
79 | 76 |
80 m_mp_init_multi(&dh_g, &dh_p, &dh_q, &dh_y, &dh_f, NULL); | 77 gen_kexdh_vals(&dh_f, &dh_y); |
81 | 78 |
82 /* read the prime and generator*/ | 79 kexdh_comb_key(&dh_f, &dh_y, dh_e, svr_opts.hostkey); |
83 if (mp_read_unsigned_bin(&dh_p, (unsigned char*)dh_p_val, DH_P_LEN) | 80 mp_clear(&dh_y); |
84 != MP_OKAY) { | |
85 dropbear_exit("Diffie-Hellman error"); | |
86 } | |
87 | |
88 if (mp_set_int(&dh_g, DH_G_VAL) != MP_OKAY) { | |
89 dropbear_exit("Diffie-Hellman error"); | |
90 } | |
91 | 81 |
92 /* calculate q = (p-1)/2 */ | |
93 if (mp_sub_d(&dh_p, 1, &dh_y) != MP_OKAY) { /*dh_y is just a temp var here*/ | |
94 dropbear_exit("Diffie-Hellman error"); | |
95 } | |
96 if (mp_div_2(&dh_y, &dh_q) != MP_OKAY) { | |
97 dropbear_exit("Diffie-Hellman error"); | |
98 } | |
99 | |
100 dh_q_len = mp_unsigned_bin_size(&dh_q); | |
101 | |
102 /* calculate our random value dh_y */ | |
103 do { | |
104 assert((unsigned int)dh_q_len <= sizeof(randbuf)); | |
105 genrandom(randbuf, dh_q_len); | |
106 if (mp_read_unsigned_bin(&dh_y, randbuf, dh_q_len) != MP_OKAY) { | |
107 dropbear_exit("Diffie-Hellman error"); | |
108 } | |
109 } while (mp_cmp(&dh_y, &dh_q) == MP_GT || mp_cmp_d(&dh_y, 0) != MP_GT); | |
110 | |
111 /* f = g^y mod p */ | |
112 if (mp_exptmod(&dh_g, &dh_y, &dh_p, &dh_f) != MP_OKAY) { | |
113 dropbear_exit("Diffie-Hellman error"); | |
114 } | |
115 mp_clear(&dh_g); | |
116 | |
117 /* K = e^y mod p */ | |
118 ses.dh_K = (mp_int*)m_malloc(sizeof(mp_int)); | |
119 m_mp_init(ses.dh_K); | |
120 if (mp_exptmod(dh_e, &dh_y, &dh_p, ses.dh_K) != MP_OKAY) { | |
121 dropbear_exit("Diffie-Hellman error"); | |
122 } | |
123 | |
124 /* clear no longer needed vars */ | |
125 mp_clear_multi(&dh_y, &dh_p, &dh_q, NULL); | |
126 | |
127 /* Create the remainder of the hash buffer, to generate the exchange hash */ | |
128 /* K_S, the host key */ | |
129 buf_put_pub_key(ses.kexhashbuf, svr_opts.hostkey, | |
130 ses.newkeys->algo_hostkey); | |
131 /* e, exchange value sent by the client */ | |
132 buf_putmpint(ses.kexhashbuf, dh_e); | |
133 /* f, exchange value sent by the server */ | |
134 buf_putmpint(ses.kexhashbuf, &dh_f); | |
135 /* K, the shared secret */ | |
136 buf_putmpint(ses.kexhashbuf, ses.dh_K); | |
137 | |
138 /* calculate the hash H to sign */ | |
139 sha1_init(&hs); | |
140 buf_setpos(ses.kexhashbuf, 0); | |
141 sha1_process(&hs, buf_getptr(ses.kexhashbuf, ses.kexhashbuf->len), | |
142 ses.kexhashbuf->len); | |
143 sha1_done(&hs, ses.hash); | |
144 buf_free(ses.kexhashbuf); | |
145 ses.kexhashbuf = NULL; | |
146 | |
147 /* first time around, we set the session_id to H */ | |
148 if (ses.session_id == NULL) { | |
149 /* create the session_id, this never needs freeing */ | |
150 ses.session_id = (unsigned char*)m_malloc(SHA1_HASH_SIZE); | |
151 memcpy(ses.session_id, ses.hash, SHA1_HASH_SIZE); | |
152 } | |
153 | |
154 /* we can start creating the kexdh_reply packet */ | 82 /* we can start creating the kexdh_reply packet */ |
155 CHECKCLEARTOWRITE(); | 83 CHECKCLEARTOWRITE(); |
156 buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_REPLY); | 84 buf_putbyte(ses.writepayload, SSH_MSG_KEXDH_REPLY); |
157 buf_put_pub_key(ses.writepayload, svr_opts.hostkey, | 85 buf_put_pub_key(ses.writepayload, svr_opts.hostkey, |
158 ses.newkeys->algo_hostkey); | 86 ses.newkeys->algo_hostkey); |
169 encrypt_packet(); | 97 encrypt_packet(); |
170 | 98 |
171 TRACE(("leave send_msg_kexdh_reply")); | 99 TRACE(("leave send_msg_kexdh_reply")); |
172 } | 100 } |
173 | 101 |
174 /* read the client's choice of algorithms */ | |
175 void svr_read_kex() { | |
176 | |
177 algo_type * algo; | |
178 char * erralgo = NULL; | |
179 | |
180 int goodguess = 0; | |
181 int allgood = 1; /* we AND this with each goodguess and see if its still | |
182 true after */ | |
183 | |
184 buf_incrpos(ses.payload, 16); /* start after the cookie */ | |
185 | |
186 ses.newkeys = (struct key_context*)m_malloc(sizeof(struct key_context)); | |
187 | |
188 /* kex_algorithms */ | |
189 algo = svr_buf_match_algo(ses.payload, sshkex, &goodguess); | |
190 allgood &= goodguess; | |
191 if (algo == NULL) { | |
192 erralgo = "kex"; | |
193 goto error; | |
194 } | |
195 ses.newkeys->algo_kex = algo->val; | |
196 | |
197 /* server_host_key_algorithms */ | |
198 algo = svr_buf_match_algo(ses.payload, sshhostkey, &goodguess); | |
199 allgood &= goodguess; | |
200 if (algo == NULL) { | |
201 erralgo = "hostkey"; | |
202 goto error; | |
203 } | |
204 ses.newkeys->algo_hostkey = algo->val; | |
205 | |
206 /* encryption_algorithms_client_to_server */ | |
207 algo = svr_buf_match_algo(ses.payload, sshciphers, &goodguess); | |
208 if (algo == NULL) { | |
209 erralgo = "enc c->s"; | |
210 goto error; | |
211 } | |
212 ses.newkeys->recv_algo_crypt = (struct dropbear_cipher*)algo->data; | |
213 | |
214 /* encryption_algorithms_server_to_client */ | |
215 algo = svr_buf_match_algo(ses.payload, sshciphers, &goodguess); | |
216 if (algo == NULL) { | |
217 erralgo = "enc s->c"; | |
218 goto error; | |
219 } | |
220 ses.newkeys->trans_algo_crypt = (struct dropbear_cipher*)algo->data; | |
221 | |
222 /* mac_algorithms_client_to_server */ | |
223 algo = svr_buf_match_algo(ses.payload, sshhashes, &goodguess); | |
224 if (algo == NULL) { | |
225 erralgo = "mac c->s"; | |
226 goto error; | |
227 } | |
228 ses.newkeys->recv_algo_mac = (struct dropbear_hash*)algo->data; | |
229 | |
230 /* mac_algorithms_server_to_client */ | |
231 algo = svr_buf_match_algo(ses.payload, sshhashes, &goodguess); | |
232 if (algo == NULL) { | |
233 erralgo = "mac s->c"; | |
234 goto error; | |
235 } | |
236 ses.newkeys->trans_algo_mac = (struct dropbear_hash*)algo->data; | |
237 | |
238 /* compression_algorithms_client_to_server */ | |
239 algo = svr_buf_match_algo(ses.payload, sshcompress, &goodguess); | |
240 if (algo == NULL) { | |
241 erralgo = "comp c->s"; | |
242 goto error; | |
243 } | |
244 ses.newkeys->recv_algo_comp = algo->val; | |
245 | |
246 /* compression_algorithms_server_to_client */ | |
247 algo = svr_buf_match_algo(ses.payload, sshcompress, &goodguess); | |
248 if (algo == NULL) { | |
249 erralgo = "comp s->c"; | |
250 goto error; | |
251 } | |
252 ses.newkeys->trans_algo_comp = algo->val; | |
253 | |
254 /* languages_client_to_server */ | |
255 buf_eatstring(ses.payload); | |
256 | |
257 /* languages_server_to_client */ | |
258 buf_eatstring(ses.payload); | |
259 | |
260 /* first_kex_packet_follows */ | |
261 if (buf_getbyte(ses.payload)) { | |
262 ses.kexstate.firstfollows = 1; | |
263 /* if the guess wasn't good, we ignore the packet sent */ | |
264 if (!allgood) { | |
265 ses.ignorenext = 1; | |
266 } | |
267 } | |
268 | |
269 /* reserved for future extensions */ | |
270 buf_getint(ses.payload); | |
271 return; | |
272 | |
273 error: | |
274 dropbear_exit("no matching algo %s", erralgo); | |
275 } |