summaryrefslogtreecommitdiff
path: root/packet.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2002-03-13 12:47:54 +1100
committerDamien Miller <djm@mindrot.org>2002-03-13 12:47:54 +1100
commit646e7cf3d7e7d4231c2d97d27c09fe5fe1d749e2 (patch)
treea693368c47d2d044514878fbb1516f87b487f78b /packet.c
parent29bdd2c9bca2737e7a246ed50fd827a6ccba0c61 (diff)
Import of Niels Provos' 20020312 ssh-complete.diff
PAM, Cygwin and OSF SIA will not work for sure
Diffstat (limited to 'packet.c')
-rw-r--r--packet.c106
1 files changed, 94 insertions, 12 deletions
diff --git a/packet.c b/packet.c
index 045d5a105..1c80af128 100644
--- a/packet.c
+++ b/packet.c
@@ -115,6 +115,8 @@ static int interactive_mode = 0;
115 115
116/* Session key information for Encryption and MAC */ 116/* Session key information for Encryption and MAC */
117Newkeys *newkeys[MODE_MAX]; 117Newkeys *newkeys[MODE_MAX];
118static u_int32_t read_seqnr = 0;
119static u_int32_t send_seqnr = 0;
118 120
119/* roundup current message to extra_pad bytes */ 121/* roundup current message to extra_pad bytes */
120static u_char extra_pad = 0; 122static u_char extra_pad = 0;
@@ -171,6 +173,87 @@ packet_connection_is_on_socket(void)
171 return 1; 173 return 1;
172} 174}
173 175
176/*
177 * Exports an IV from the CipherContext required to export the key
178 * state back from the unprivileged child to the privileged parent
179 * process.
180 */
181
182void
183packet_get_keyiv(int mode, u_char *iv, u_int len)
184{
185 CipherContext *cc;
186
187 if (mode == MODE_OUT)
188 cc = &send_context;
189 else
190 cc = &receive_context;
191
192 cipher_get_keyiv(cc, iv, len);
193}
194
195int
196packet_get_keycontext(int mode, u_char *dat)
197{
198 int plen;
199 CipherContext *cc;
200
201 if (mode == MODE_OUT)
202 cc = &send_context;
203 else
204 cc = &receive_context;
205
206#if OPENSSL_VERSION_NUMBER < 0x00907000L
207 plen = sizeof(cc->evp.c);
208#else
209 plen = cc->evp.cipher->ctx_size;
210#endif
211
212 if (dat == NULL)
213 return (plen);
214
215#if OPENSSL_VERSION_NUMBER < 0x00907000L
216 memcpy(dat, &cc->evp.c, sizeof(cc->evp.c));
217#else
218 memcpy(dat, &cc->evp.cipher_data, plen);
219#endif
220 return (plen);
221}
222
223void
224packet_set_keycontext(int mode, u_char *dat)
225{
226 CipherContext *cc;
227
228 if (mode == MODE_OUT)
229 cc = &send_context;
230 else
231 cc = &receive_context;
232
233#if OPENSSL_VERSION_NUMBER < 0x00907000L
234 memcpy(&cc->evp.c, dat, sizeof(cc->evp.c));
235#else
236 memcpy(&cc->evp.cipher_data, dat, cc->evp.cipher->ctx_size);
237#endif
238}
239
240u_int32_t
241packet_get_seqnr(int mode)
242{
243 return (mode == MODE_IN ? read_seqnr : send_seqnr);
244}
245
246void
247packet_set_seqnr(int mode, u_int32_t seqnr)
248{
249 if (mode == MODE_IN)
250 read_seqnr = seqnr;
251 else if (mode == MODE_OUT)
252 send_seqnr = seqnr;
253 else
254 fatal("%s: bad mode %d", __FUNCTION__, mode);
255}
256
174/* returns 1 if connection is via ipv4 */ 257/* returns 1 if connection is via ipv4 */
175 258
176int 259int
@@ -433,7 +516,7 @@ packet_send1(void)
433 */ 516 */
434} 517}
435 518
436static void 519void
437set_newkeys(int mode) 520set_newkeys(int mode)
438{ 521{
439 Enc *enc; 522 Enc *enc;
@@ -477,8 +560,9 @@ set_newkeys(int mode)
477 DBG(debug("cipher_init_context: %d", mode)); 560 DBG(debug("cipher_init_context: %d", mode));
478 cipher_init(cc, enc->cipher, enc->key, enc->key_len, 561 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
479 enc->iv, enc->block_size, encrypt); 562 enc->iv, enc->block_size, encrypt);
480 memset(enc->iv, 0, enc->block_size); 563 /* Deleting the keys does not gain extra security */
481 memset(enc->key, 0, enc->key_len); 564 /* memset(enc->iv, 0, enc->block_size);
565 memset(enc->key, 0, enc->key_len); */
482 if (comp->type != 0 && comp->enabled == 0) { 566 if (comp->type != 0 && comp->enabled == 0) {
483 packet_init_compression(); 567 packet_init_compression();
484 if (mode == MODE_OUT) 568 if (mode == MODE_OUT)
@@ -495,7 +579,6 @@ set_newkeys(int mode)
495static void 579static void
496packet_send2(void) 580packet_send2(void)
497{ 581{
498 static u_int32_t seqnr = 0;
499 u_char type, *cp, *macbuf = NULL; 582 u_char type, *cp, *macbuf = NULL;
500 u_char padlen, pad; 583 u_char padlen, pad;
501 u_int packet_length = 0; 584 u_int packet_length = 0;
@@ -576,10 +659,10 @@ packet_send2(void)
576 659
577 /* compute MAC over seqnr and packet(length fields, payload, padding) */ 660 /* compute MAC over seqnr and packet(length fields, payload, padding) */
578 if (mac && mac->enabled) { 661 if (mac && mac->enabled) {
579 macbuf = mac_compute(mac, seqnr, 662 macbuf = mac_compute(mac, send_seqnr,
580 buffer_ptr(&outgoing_packet), 663 buffer_ptr(&outgoing_packet),
581 buffer_len(&outgoing_packet)); 664 buffer_len(&outgoing_packet));
582 DBG(debug("done calc MAC out #%d", seqnr)); 665 DBG(debug("done calc MAC out #%d", send_seqnr));
583 } 666 }
584 /* encrypt packet and append to output buffer. */ 667 /* encrypt packet and append to output buffer. */
585 cp = buffer_append_space(&output, buffer_len(&outgoing_packet)); 668 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
@@ -593,7 +676,7 @@ packet_send2(void)
593 buffer_dump(&output); 676 buffer_dump(&output);
594#endif 677#endif
595 /* increment sequence number for outgoing packets */ 678 /* increment sequence number for outgoing packets */
596 if (++seqnr == 0) 679 if (++send_seqnr == 0)
597 log("outgoing seqnr wraps around"); 680 log("outgoing seqnr wraps around");
598 buffer_clear(&outgoing_packet); 681 buffer_clear(&outgoing_packet);
599 682
@@ -783,7 +866,6 @@ packet_read_poll1(void)
783static int 866static int
784packet_read_poll2(u_int32_t *seqnr_p) 867packet_read_poll2(u_int32_t *seqnr_p)
785{ 868{
786 static u_int32_t seqnr = 0;
787 static u_int packet_length = 0; 869 static u_int packet_length = 0;
788 u_int padlen, need; 870 u_int padlen, need;
789 u_char *macbuf, *cp, type; 871 u_char *macbuf, *cp, type;
@@ -845,17 +927,17 @@ packet_read_poll2(u_int32_t *seqnr_p)
845 * increment sequence number for incoming packet 927 * increment sequence number for incoming packet
846 */ 928 */
847 if (mac && mac->enabled) { 929 if (mac && mac->enabled) {
848 macbuf = mac_compute(mac, seqnr, 930 macbuf = mac_compute(mac, read_seqnr,
849 buffer_ptr(&incoming_packet), 931 buffer_ptr(&incoming_packet),
850 buffer_len(&incoming_packet)); 932 buffer_len(&incoming_packet));
851 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0) 933 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
852 packet_disconnect("Corrupted MAC on input."); 934 packet_disconnect("Corrupted MAC on input.");
853 DBG(debug("MAC #%d ok", seqnr)); 935 DBG(debug("MAC #%d ok", read_seqnr));
854 buffer_consume(&input, mac->mac_len); 936 buffer_consume(&input, mac->mac_len);
855 } 937 }
856 if (seqnr_p != NULL) 938 if (seqnr_p != NULL)
857 *seqnr_p = seqnr; 939 *seqnr_p = read_seqnr;
858 if (++seqnr == 0) 940 if (++read_seqnr == 0)
859 log("incoming seqnr wraps around"); 941 log("incoming seqnr wraps around");
860 942
861 /* get padlen */ 943 /* get padlen */