comparison common-kex.c @ 771:a389a2a7aa96

Fix zlib for split newkeys
author Matt Johnston <matt@ucc.asn.au>
date Thu, 11 Apr 2013 23:03:58 +0800
parents d63ef1e211ea
children 2f1c199b6e4b
comparison
equal deleted inserted replaced
770:7577a3afc42d 771:a389a2a7aa96
80 static const int DH_G_VAL = 2; 80 static const int DH_G_VAL = 2;
81 81
82 static void kexinitialise(); 82 static void kexinitialise();
83 static void gen_new_keys(); 83 static void gen_new_keys();
84 #ifndef DISABLE_ZLIB 84 #ifndef DISABLE_ZLIB
85 static void gen_new_zstreams(); 85 static void gen_new_zstream_recv();
86 static void gen_new_zstream_trans();
86 #endif 87 #endif
87 static void read_kex_algos(); 88 static void read_kex_algos();
88 /* helper function for gen_new_keys */ 89 /* helper function for gen_new_keys */
89 static void hashkeys(unsigned char *out, int outlen, 90 static void hashkeys(unsigned char *out, int outlen,
90 const hash_state * hs, unsigned const char X); 91 const hash_state * hs, unsigned const char X);
157 TRACE(("DATAALLOWED=0")) 158 TRACE(("DATAALLOWED=0"))
158 TRACE(("-> KEXINIT")) 159 TRACE(("-> KEXINIT"))
159 160
160 } 161 }
161 162
162 void switch_keys() { 163 static void switch_keys() {
163 TRACE2(("enter switch_keys")) 164 TRACE2(("enter switch_keys"))
164 if (!(ses.kexstate.sentkexinit && ses.kexstate.recvkexinit)) { 165 if (!(ses.kexstate.sentkexinit && ses.kexstate.recvkexinit)) {
165 dropbear_exit("Unexpected newkeys message"); 166 dropbear_exit("Unexpected newkeys message");
166 } 167 }
167 168
168 if (!ses.keys) { 169 if (!ses.keys) {
169 ses.keys = m_malloc(sizeof(*ses.newkeys)); 170 ses.keys = m_malloc(sizeof(*ses.newkeys));
170 } 171 }
171 if (ses.kexstate.recvnewkeys && ses.newkeys->recv.valid) { 172 if (ses.kexstate.recvnewkeys && ses.newkeys->recv.valid) {
172 TRACE(("switch_keys recv")) 173 TRACE(("switch_keys recv"))
174 gen_new_zstream_recv();
173 ses.keys->recv = ses.newkeys->recv; 175 ses.keys->recv = ses.newkeys->recv;
174 m_burn(&ses.newkeys->recv, sizeof(ses.newkeys->recv)); 176 m_burn(&ses.newkeys->recv, sizeof(ses.newkeys->recv));
175 ses.newkeys->recv.valid = 0; 177 ses.newkeys->recv.valid = 0;
176 } 178 }
177 if (ses.kexstate.sentnewkeys && ses.newkeys->trans.valid) { 179 if (ses.kexstate.sentnewkeys && ses.newkeys->trans.valid) {
178 TRACE(("switch_keys trans")) 180 TRACE(("switch_keys trans"))
181 gen_new_zstream_trans();
179 ses.keys->trans = ses.newkeys->trans; 182 ses.keys->trans = ses.newkeys->trans;
180 m_burn(&ses.newkeys->trans, sizeof(ses.newkeys->trans)); 183 m_burn(&ses.newkeys->trans, sizeof(ses.newkeys->trans));
181 ses.newkeys->trans.valid = 0; 184 ses.newkeys->trans.valid = 0;
182 } 185 }
183 if (ses.kexstate.sentnewkeys && ses.kexstate.recvnewkeys) 186 if (ses.kexstate.sentnewkeys && ses.kexstate.recvnewkeys)
384 hashkeys(ses.newkeys->recv.mackey, 387 hashkeys(ses.newkeys->recv.mackey,
385 ses.newkeys->recv.algo_mac->keysize, &hs, macrecvletter); 388 ses.newkeys->recv.algo_mac->keysize, &hs, macrecvletter);
386 ses.newkeys->recv.hash_index = find_hash(ses.newkeys->recv.algo_mac->hashdesc->name); 389 ses.newkeys->recv.hash_index = find_hash(ses.newkeys->recv.algo_mac->hashdesc->name);
387 } 390 }
388 391
389 #ifndef DISABLE_ZLIB
390 gen_new_zstreams();
391 #endif
392
393 /* Ready to switch over */ 392 /* Ready to switch over */
394 ses.newkeys->trans.valid = 1; 393 ses.newkeys->trans.valid = 1;
395 ses.newkeys->recv.valid = 1; 394 ses.newkeys->recv.valid = 1;
396 395
397 m_burn(C2S_IV, sizeof(C2S_IV)); 396 m_burn(C2S_IV, sizeof(C2S_IV));
416 && ses.keys->recv.algo_comp == DROPBEAR_COMP_ZLIB_DELAY); 415 && ses.keys->recv.algo_comp == DROPBEAR_COMP_ZLIB_DELAY);
417 } 416 }
418 417
419 /* Set up new zlib compression streams, close the old ones. Only 418 /* Set up new zlib compression streams, close the old ones. Only
420 * called from gen_new_keys() */ 419 * called from gen_new_keys() */
421 static void gen_new_zstreams() { 420 static void gen_new_zstream_recv() {
422 421
423 /* create new zstreams */ 422 /* create new zstreams */
424 if (ses.newkeys->recv.algo_comp == DROPBEAR_COMP_ZLIB 423 if (ses.newkeys->recv.algo_comp == DROPBEAR_COMP_ZLIB
425 || ses.newkeys->recv.algo_comp == DROPBEAR_COMP_ZLIB_DELAY) { 424 || ses.newkeys->recv.algo_comp == DROPBEAR_COMP_ZLIB_DELAY) {
426 ses.newkeys->recv.zstream = (z_streamp)m_malloc(sizeof(z_stream)); 425 ses.newkeys->recv.zstream = (z_streamp)m_malloc(sizeof(z_stream));
431 dropbear_exit("zlib error"); 430 dropbear_exit("zlib error");
432 } 431 }
433 } else { 432 } else {
434 ses.newkeys->recv.zstream = NULL; 433 ses.newkeys->recv.zstream = NULL;
435 } 434 }
435 /* clean up old keys */
436 if (ses.keys->recv.zstream != NULL) {
437 if (inflateEnd(ses.keys->recv.zstream) == Z_STREAM_ERROR) {
438 /* Z_DATA_ERROR is ok, just means that stream isn't ended */
439 dropbear_exit("Crypto error");
440 }
441 m_free(ses.keys->recv.zstream);
442 }
443 }
444
445 static void gen_new_zstream_trans() {
436 446
437 if (ses.newkeys->trans.algo_comp == DROPBEAR_COMP_ZLIB 447 if (ses.newkeys->trans.algo_comp == DROPBEAR_COMP_ZLIB
438 || ses.newkeys->trans.algo_comp == DROPBEAR_COMP_ZLIB_DELAY) { 448 || ses.newkeys->trans.algo_comp == DROPBEAR_COMP_ZLIB_DELAY) {
439 ses.newkeys->trans.zstream = (z_streamp)m_malloc(sizeof(z_stream)); 449 ses.newkeys->trans.zstream = (z_streamp)m_malloc(sizeof(z_stream));
440 ses.newkeys->trans.zstream->zalloc = Z_NULL; 450 ses.newkeys->trans.zstream->zalloc = Z_NULL;
448 } 458 }
449 } else { 459 } else {
450 ses.newkeys->trans.zstream = NULL; 460 ses.newkeys->trans.zstream = NULL;
451 } 461 }
452 462
453 /* clean up old keys */
454 if (ses.keys->recv.zstream != NULL) {
455 if (inflateEnd(ses.keys->recv.zstream) == Z_STREAM_ERROR) {
456 /* Z_DATA_ERROR is ok, just means that stream isn't ended */
457 dropbear_exit("Crypto error");
458 }
459 m_free(ses.keys->recv.zstream);
460 }
461 if (ses.keys->trans.zstream != NULL) { 463 if (ses.keys->trans.zstream != NULL) {
462 if (deflateEnd(ses.keys->trans.zstream) == Z_STREAM_ERROR) { 464 if (deflateEnd(ses.keys->trans.zstream) == Z_STREAM_ERROR) {
463 /* Z_DATA_ERROR is ok, just means that stream isn't ended */ 465 /* Z_DATA_ERROR is ok, just means that stream isn't ended */
464 dropbear_exit("Crypto error"); 466 dropbear_exit("Crypto error");
465 } 467 }