comparison common-kex.c @ 165:0cfba3034be5

Fixed DEBUG_TRACE macro so that we don't get semicolons left about the place
author Matt Johnston <matt@ucc.asn.au>
date Sun, 02 Jan 2005 20:25:56 +0000
parents 2010f4119c1a
children 161557a9dde8
comparison
equal deleted inserted replaced
161:b9d3f725e00b 165:0cfba3034be5
112 ses.transkexinit = buf_newcopy(ses.writepayload); 112 ses.transkexinit = buf_newcopy(ses.writepayload);
113 113
114 encrypt_packet(); 114 encrypt_packet();
115 ses.dataallowed = 0; /* don't send other packets during kex */ 115 ses.dataallowed = 0; /* don't send other packets during kex */
116 116
117 TRACE(("DATAALLOWED=0")); 117 TRACE(("DATAALLOWED=0"))
118 TRACE(("-> KEXINIT")); 118 TRACE(("-> KEXINIT"))
119 ses.kexstate.sentkexinit = 1; 119 ses.kexstate.sentkexinit = 1;
120 } 120 }
121 121
122 /* *** NOTE regarding (send|recv)_msg_newkeys *** 122 /* *** NOTE regarding (send|recv)_msg_newkeys ***
123 * Changed by mihnea from the original kex.c to set dataallowed after a 123 * Changed by mihnea from the original kex.c to set dataallowed after a
126 */ 126 */
127 127
128 /* Bring new keys into use after a key exchange, and let the client know*/ 128 /* Bring new keys into use after a key exchange, and let the client know*/
129 void send_msg_newkeys() { 129 void send_msg_newkeys() {
130 130
131 TRACE(("enter send_msg_newkeys")); 131 TRACE(("enter send_msg_newkeys"))
132 132
133 /* generate the kexinit request */ 133 /* generate the kexinit request */
134 CHECKCLEARTOWRITE(); 134 CHECKCLEARTOWRITE();
135 buf_putbyte(ses.writepayload, SSH_MSG_NEWKEYS); 135 buf_putbyte(ses.writepayload, SSH_MSG_NEWKEYS);
136 encrypt_packet(); 136 encrypt_packet();
137 137
138 138
139 /* set up our state */ 139 /* set up our state */
140 if (ses.kexstate.recvnewkeys) { 140 if (ses.kexstate.recvnewkeys) {
141 TRACE(("while RECVNEWKEYS=1")); 141 TRACE(("while RECVNEWKEYS=1"))
142 gen_new_keys(); 142 gen_new_keys();
143 kexinitialise(); /* we've finished with this kex */ 143 kexinitialise(); /* we've finished with this kex */
144 TRACE((" -> DATAALLOWED=1")); 144 TRACE((" -> DATAALLOWED=1"))
145 ses.dataallowed = 1; /* we can send other packets again now */ 145 ses.dataallowed = 1; /* we can send other packets again now */
146 ses.kexstate.donefirstkex = 1; 146 ses.kexstate.donefirstkex = 1;
147 } else { 147 } else {
148 ses.kexstate.sentnewkeys = 1; 148 ses.kexstate.sentnewkeys = 1;
149 TRACE(("SENTNEWKEYS=1")); 149 TRACE(("SENTNEWKEYS=1"))
150 } 150 }
151 151
152 TRACE(("-> MSG_NEWKEYS")); 152 TRACE(("-> MSG_NEWKEYS"))
153 TRACE(("leave send_msg_newkeys")); 153 TRACE(("leave send_msg_newkeys"))
154 } 154 }
155 155
156 /* Bring the new keys into use after a key exchange */ 156 /* Bring the new keys into use after a key exchange */
157 void recv_msg_newkeys() { 157 void recv_msg_newkeys() {
158 158
159 TRACE(("<- MSG_NEWKEYS")); 159 TRACE(("<- MSG_NEWKEYS"))
160 TRACE(("enter recv_msg_newkeys")); 160 TRACE(("enter recv_msg_newkeys"))
161 161
162 /* simply check if we've sent SSH_MSG_NEWKEYS, and if so, 162 /* simply check if we've sent SSH_MSG_NEWKEYS, and if so,
163 * switch to the new keys */ 163 * switch to the new keys */
164 if (ses.kexstate.sentnewkeys) { 164 if (ses.kexstate.sentnewkeys) {
165 TRACE(("while SENTNEWKEYS=1")); 165 TRACE(("while SENTNEWKEYS=1"))
166 gen_new_keys(); 166 gen_new_keys();
167 kexinitialise(); /* we've finished with this kex */ 167 kexinitialise(); /* we've finished with this kex */
168 TRACE((" -> DATAALLOWED=1")); 168 TRACE((" -> DATAALLOWED=1"))
169 ses.dataallowed = 1; /* we can send other packets again now */ 169 ses.dataallowed = 1; /* we can send other packets again now */
170 ses.kexstate.donefirstkex = 1; 170 ses.kexstate.donefirstkex = 1;
171 } else { 171 } else {
172 TRACE(("RECVNEWKEYS=1")); 172 TRACE(("RECVNEWKEYS=1"))
173 ses.kexstate.recvnewkeys = 1; 173 ses.kexstate.recvnewkeys = 1;
174 } 174 }
175 175
176 TRACE(("leave recv_msg_newkeys")); 176 TRACE(("leave recv_msg_newkeys"))
177 } 177 }
178 178
179 179
180 /* Set up the kex for the first time */ 180 /* Set up the kex for the first time */
181 void kexfirstinitialise() { 181 void kexfirstinitialise() {
187 /* Reset the kex state, ready for a new negotiation */ 187 /* Reset the kex state, ready for a new negotiation */
188 static void kexinitialise() { 188 static void kexinitialise() {
189 189
190 struct timeval tv; 190 struct timeval tv;
191 191
192 TRACE(("kexinitialise()")); 192 TRACE(("kexinitialise()"))
193 193
194 /* sent/recv'd MSG_KEXINIT */ 194 /* sent/recv'd MSG_KEXINIT */
195 ses.kexstate.sentkexinit = 0; 195 ses.kexstate.sentkexinit = 0;
196 ses.kexstate.recvkexinit = 0; 196 ses.kexstate.recvkexinit = 0;
197 197
260 260
261 hash_state hs; 261 hash_state hs;
262 unsigned int C2S_keysize, S2C_keysize; 262 unsigned int C2S_keysize, S2C_keysize;
263 char mactransletter, macrecvletter; /* Client or server specific */ 263 char mactransletter, macrecvletter; /* Client or server specific */
264 264
265 TRACE(("enter gen_new_keys")); 265 TRACE(("enter gen_new_keys"))
266 /* the dh_K and hash are the start of all hashes, we make use of that */ 266 /* the dh_K and hash are the start of all hashes, we make use of that */
267 267
268 sha1_init(&hs); 268 sha1_init(&hs);
269 sha1_process_mp(&hs, ses.dh_K); 269 sha1_process_mp(&hs, ses.dh_K);
270 mp_clear(ses.dh_K); 270 mp_clear(ses.dh_K);
327 m_burn(ses.keys, sizeof(struct key_context)); 327 m_burn(ses.keys, sizeof(struct key_context));
328 m_free(ses.keys); 328 m_free(ses.keys);
329 ses.keys = ses.newkeys; 329 ses.keys = ses.newkeys;
330 ses.newkeys = NULL; 330 ses.newkeys = NULL;
331 331
332 TRACE(("leave gen_new_keys")); 332 TRACE(("leave gen_new_keys"))
333 } 333 }
334 334
335 #ifndef DISABLE_ZLIB 335 #ifndef DISABLE_ZLIB
336 /* Set up new zlib compression streams, close the old ones. Only 336 /* Set up new zlib compression streams, close the old ones. Only
337 * called from gen_new_keys() */ 337 * called from gen_new_keys() */
391 391
392 /* Originally from kex.c, generalized for cli/svr mode --mihnea */ 392 /* Originally from kex.c, generalized for cli/svr mode --mihnea */
393 /* Belongs in common_kex.c where it should be moved after review */ 393 /* Belongs in common_kex.c where it should be moved after review */
394 void recv_msg_kexinit() { 394 void recv_msg_kexinit() {
395 395
396 TRACE(("<- KEXINIT")); 396 TRACE(("<- KEXINIT"))
397 TRACE(("enter recv_msg_kexinit")); 397 TRACE(("enter recv_msg_kexinit"))
398 398
399 /* start the kex hash */ 399 /* start the kex hash */
400 ses.kexhashbuf = buf_new(MAX_KEXHASHBUF); 400 ses.kexhashbuf = buf_new(MAX_KEXHASHBUF);
401 401
402 if (!ses.kexstate.sentkexinit) { 402 if (!ses.kexstate.sentkexinit) {
403 /* we need to send a kex packet */ 403 /* we need to send a kex packet */
404 send_msg_kexinit(); 404 send_msg_kexinit();
405 TRACE(("continue recv_msg_kexinit: sent kexinit")); 405 TRACE(("continue recv_msg_kexinit: sent kexinit"))
406 } 406 }
407 407
408 408
409 if (IS_DROPBEAR_CLIENT) { 409 if (IS_DROPBEAR_CLIENT) {
410 410
457 /* the rest of ses.kexhashbuf will be done after DH exchange */ 457 /* the rest of ses.kexhashbuf will be done after DH exchange */
458 458
459 ses.kexstate.recvkexinit = 1; 459 ses.kexstate.recvkexinit = 1;
460 // ses.expecting = 0; // client matt 460 // ses.expecting = 0; // client matt
461 461
462 TRACE(("leave recv_msg_kexinit")); 462 TRACE(("leave recv_msg_kexinit"))
463 } 463 }
464 464
465 /* Initialises and generate one side of the diffie-hellman key exchange values. 465 /* Initialises and generate one side of the diffie-hellman key exchange values.
466 * See the ietf-secsh-transport draft, section 6, for details */ 466 * See the ietf-secsh-transport draft, section 6, for details */
467 /* dh_pub and dh_priv MUST be already initialised */ 467 /* dh_pub and dh_priv MUST be already initialised */
471 DEF_MP_INT(dh_q); 471 DEF_MP_INT(dh_q);
472 DEF_MP_INT(dh_g); 472 DEF_MP_INT(dh_g);
473 unsigned char randbuf[DH_P_LEN]; 473 unsigned char randbuf[DH_P_LEN];
474 int dh_q_len; 474 int dh_q_len;
475 475
476 TRACE(("enter send_msg_kexdh_reply")); 476 TRACE(("enter send_msg_kexdh_reply"))
477 477
478 m_mp_init_multi(&dh_g, &dh_p, &dh_q, NULL); 478 m_mp_init_multi(&dh_g, &dh_p, &dh_q, NULL);
479 479
480 /* read the prime and generator*/ 480 /* read the prime and generator*/
481 if (mp_read_unsigned_bin(&dh_p, (unsigned char*)dh_p_val, DH_P_LEN) 481 if (mp_read_unsigned_bin(&dh_p, (unsigned char*)dh_p_val, DH_P_LEN)
617 allgood &= goodguess; 617 allgood &= goodguess;
618 if (algo == NULL) { 618 if (algo == NULL) {
619 erralgo = "kex"; 619 erralgo = "kex";
620 goto error; 620 goto error;
621 } 621 }
622 TRACE(("kex algo %s", algo->name)); 622 TRACE(("kex algo %s", algo->name))
623 ses.newkeys->algo_kex = algo->val; 623 ses.newkeys->algo_kex = algo->val;
624 624
625 /* server_host_key_algorithms */ 625 /* server_host_key_algorithms */
626 algo = ses.buf_match_algo(ses.payload, sshhostkey, &goodguess); 626 algo = ses.buf_match_algo(ses.payload, sshhostkey, &goodguess);
627 allgood &= goodguess; 627 allgood &= goodguess;
628 if (algo == NULL) { 628 if (algo == NULL) {
629 erralgo = "hostkey"; 629 erralgo = "hostkey";
630 goto error; 630 goto error;
631 } 631 }
632 TRACE(("hostkey algo %s", algo->name)); 632 TRACE(("hostkey algo %s", algo->name))
633 ses.newkeys->algo_hostkey = algo->val; 633 ses.newkeys->algo_hostkey = algo->val;
634 634
635 /* encryption_algorithms_client_to_server */ 635 /* encryption_algorithms_client_to_server */
636 c2s_cipher_algo = ses.buf_match_algo(ses.payload, sshciphers, &goodguess); 636 c2s_cipher_algo = ses.buf_match_algo(ses.payload, sshciphers, &goodguess);
637 if (c2s_cipher_algo == NULL) { 637 if (c2s_cipher_algo == NULL) {
638 erralgo = "enc c->s"; 638 erralgo = "enc c->s";
639 goto error; 639 goto error;
640 } 640 }
641 TRACE(("c2s is %s", c2s_cipher_algo->name)); 641 TRACE(("c2s is %s", c2s_cipher_algo->name))
642 642
643 /* encryption_algorithms_server_to_client */ 643 /* encryption_algorithms_server_to_client */
644 s2c_cipher_algo = ses.buf_match_algo(ses.payload, sshciphers, &goodguess); 644 s2c_cipher_algo = ses.buf_match_algo(ses.payload, sshciphers, &goodguess);
645 if (s2c_cipher_algo == NULL) { 645 if (s2c_cipher_algo == NULL) {
646 erralgo = "enc s->c"; 646 erralgo = "enc s->c";
647 goto error; 647 goto error;
648 } 648 }
649 TRACE(("s2c is %s", s2c_cipher_algo->name)); 649 TRACE(("s2c is %s", s2c_cipher_algo->name))
650 650
651 /* mac_algorithms_client_to_server */ 651 /* mac_algorithms_client_to_server */
652 c2s_hash_algo = ses.buf_match_algo(ses.payload, sshhashes, &goodguess); 652 c2s_hash_algo = ses.buf_match_algo(ses.payload, sshhashes, &goodguess);
653 if (c2s_hash_algo == NULL) { 653 if (c2s_hash_algo == NULL) {
654 erralgo = "mac c->s"; 654 erralgo = "mac c->s";
715 (struct dropbear_hash*)s2c_hash_algo->data; 715 (struct dropbear_hash*)s2c_hash_algo->data;
716 ses.newkeys->recv_algo_comp = c2s_comp_algo->val; 716 ses.newkeys->recv_algo_comp = c2s_comp_algo->val;
717 ses.newkeys->trans_algo_comp = s2c_comp_algo->val; 717 ses.newkeys->trans_algo_comp = s2c_comp_algo->val;
718 } 718 }
719 719
720 TRACE(("enc algo recv %s", algo->name)); 720 TRACE(("enc algo recv %s", algo->name))
721 TRACE(("enc algo trans %s", algo->name)); 721 TRACE(("enc algo trans %s", algo->name))
722 TRACE(("mac algo recv %s", algo->name)); 722 TRACE(("mac algo recv %s", algo->name))
723 TRACE(("mac algo trans %s", algo->name)); 723 TRACE(("mac algo trans %s", algo->name))
724 TRACE(("comp algo recv %s", algo->name)); 724 TRACE(("comp algo recv %s", algo->name))
725 TRACE(("comp algo trans %s", algo->name)); 725 TRACE(("comp algo trans %s", algo->name))
726 726
727 /* reserved for future extensions */ 727 /* reserved for future extensions */
728 buf_getint(ses.payload); 728 buf_getint(ses.payload);
729 return; 729 return;
730 730