summaryrefslogtreecommitdiff
path: root/packet.c
diff options
context:
space:
mode:
authorBen Lindstrom <mouring@eviladmin.org>2002-03-22 01:42:04 +0000
committerBen Lindstrom <mouring@eviladmin.org>2002-03-22 01:42:04 +0000
commitf6027d3407f8665cc64321bcee8786311a58fa0c (patch)
tree96a397097bfbf2b1dcf73f6bdbdfb6c7d9e577bb /packet.c
parent212facacdeae1b5c004860500b03ee33dc5a2240 (diff)
- markus@cvs.openbsd.org 2002/03/18 17:16:38
[packet.c packet.h] export/import cipher state, iv and ssh2 seqnr; needed by ssh-privsep
Diffstat (limited to 'packet.c')
-rw-r--r--packet.c124
1 files changed, 109 insertions, 15 deletions
diff --git a/packet.c b/packet.c
index 045d5a105..25de34951 100644
--- a/packet.c
+++ b/packet.c
@@ -37,7 +37,7 @@
37 */ 37 */
38 38
39#include "includes.h" 39#include "includes.h"
40RCSID("$OpenBSD: packet.c,v 1.90 2002/02/27 21:23:13 stevesk Exp $"); 40RCSID("$OpenBSD: packet.c,v 1.91 2002/03/18 17:16:38 markus Exp $");
41 41
42#include "xmalloc.h" 42#include "xmalloc.h"
43#include "buffer.h" 43#include "buffer.h"
@@ -86,10 +86,10 @@ static CipherContext receive_context;
86static CipherContext send_context; 86static CipherContext send_context;
87 87
88/* Buffer for raw input data from the socket. */ 88/* Buffer for raw input data from the socket. */
89static Buffer input; 89Buffer input;
90 90
91/* Buffer for raw output data going to the socket. */ 91/* Buffer for raw output data going to the socket. */
92static Buffer output; 92Buffer output;
93 93
94/* Buffer for the partial outgoing packet being constructed. */ 94/* Buffer for the partial outgoing packet being constructed. */
95static Buffer outgoing_packet; 95static Buffer outgoing_packet;
@@ -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,99 @@ 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 CipherContext *cc;
199
200 if (mode == MODE_OUT)
201 cc = &send_context;
202 else
203 cc = &receive_context;
204
205 return (cipher_get_keycontext(cc, dat));
206}
207
208void
209packet_set_keycontext(int mode, u_char *dat)
210{
211 CipherContext *cc;
212
213 if (mode == MODE_OUT)
214 cc = &send_context;
215 else
216 cc = &receive_context;
217
218 cipher_set_keycontext(cc, dat);
219}
220
221int
222packet_get_keyiv_len(int mode)
223{
224 CipherContext *cc;
225
226 if (mode == MODE_OUT)
227 cc = &send_context;
228 else
229 cc = &receive_context;
230
231 return (cipher_get_keyiv_len(cc));
232}
233void
234packet_set_iv(int mode, u_char *dat)
235{
236 CipherContext *cc;
237
238 if (mode == MODE_OUT)
239 cc = &send_context;
240 else
241 cc = &receive_context;
242
243 cipher_set_keyiv(cc, dat);
244}
245int
246packet_get_ssh1_cipher()
247{
248 return (cipher_get_number(receive_context.cipher));
249}
250
251
252u_int32_t
253packet_get_seqnr(int mode)
254{
255 return (mode == MODE_IN ? read_seqnr : send_seqnr);
256}
257
258void
259packet_set_seqnr(int mode, u_int32_t seqnr)
260{
261 if (mode == MODE_IN)
262 read_seqnr = seqnr;
263 else if (mode == MODE_OUT)
264 send_seqnr = seqnr;
265 else
266 fatal("%s: bad mode %d", __FUNCTION__, mode);
267}
268
174/* returns 1 if connection is via ipv4 */ 269/* returns 1 if connection is via ipv4 */
175 270
176int 271int
@@ -433,7 +528,7 @@ packet_send1(void)
433 */ 528 */
434} 529}
435 530
436static void 531void
437set_newkeys(int mode) 532set_newkeys(int mode)
438{ 533{
439 Enc *enc; 534 Enc *enc;
@@ -477,8 +572,9 @@ set_newkeys(int mode)
477 DBG(debug("cipher_init_context: %d", mode)); 572 DBG(debug("cipher_init_context: %d", mode));
478 cipher_init(cc, enc->cipher, enc->key, enc->key_len, 573 cipher_init(cc, enc->cipher, enc->key, enc->key_len,
479 enc->iv, enc->block_size, encrypt); 574 enc->iv, enc->block_size, encrypt);
480 memset(enc->iv, 0, enc->block_size); 575 /* Deleting the keys does not gain extra security */
481 memset(enc->key, 0, enc->key_len); 576 /* memset(enc->iv, 0, enc->block_size);
577 memset(enc->key, 0, enc->key_len); */
482 if (comp->type != 0 && comp->enabled == 0) { 578 if (comp->type != 0 && comp->enabled == 0) {
483 packet_init_compression(); 579 packet_init_compression();
484 if (mode == MODE_OUT) 580 if (mode == MODE_OUT)
@@ -495,7 +591,6 @@ set_newkeys(int mode)
495static void 591static void
496packet_send2(void) 592packet_send2(void)
497{ 593{
498 static u_int32_t seqnr = 0;
499 u_char type, *cp, *macbuf = NULL; 594 u_char type, *cp, *macbuf = NULL;
500 u_char padlen, pad; 595 u_char padlen, pad;
501 u_int packet_length = 0; 596 u_int packet_length = 0;
@@ -576,10 +671,10 @@ packet_send2(void)
576 671
577 /* compute MAC over seqnr and packet(length fields, payload, padding) */ 672 /* compute MAC over seqnr and packet(length fields, payload, padding) */
578 if (mac && mac->enabled) { 673 if (mac && mac->enabled) {
579 macbuf = mac_compute(mac, seqnr, 674 macbuf = mac_compute(mac, send_seqnr,
580 buffer_ptr(&outgoing_packet), 675 buffer_ptr(&outgoing_packet),
581 buffer_len(&outgoing_packet)); 676 buffer_len(&outgoing_packet));
582 DBG(debug("done calc MAC out #%d", seqnr)); 677 DBG(debug("done calc MAC out #%d", send_seqnr));
583 } 678 }
584 /* encrypt packet and append to output buffer. */ 679 /* encrypt packet and append to output buffer. */
585 cp = buffer_append_space(&output, buffer_len(&outgoing_packet)); 680 cp = buffer_append_space(&output, buffer_len(&outgoing_packet));
@@ -593,7 +688,7 @@ packet_send2(void)
593 buffer_dump(&output); 688 buffer_dump(&output);
594#endif 689#endif
595 /* increment sequence number for outgoing packets */ 690 /* increment sequence number for outgoing packets */
596 if (++seqnr == 0) 691 if (++send_seqnr == 0)
597 log("outgoing seqnr wraps around"); 692 log("outgoing seqnr wraps around");
598 buffer_clear(&outgoing_packet); 693 buffer_clear(&outgoing_packet);
599 694
@@ -783,7 +878,6 @@ packet_read_poll1(void)
783static int 878static int
784packet_read_poll2(u_int32_t *seqnr_p) 879packet_read_poll2(u_int32_t *seqnr_p)
785{ 880{
786 static u_int32_t seqnr = 0;
787 static u_int packet_length = 0; 881 static u_int packet_length = 0;
788 u_int padlen, need; 882 u_int padlen, need;
789 u_char *macbuf, *cp, type; 883 u_char *macbuf, *cp, type;
@@ -845,17 +939,17 @@ packet_read_poll2(u_int32_t *seqnr_p)
845 * increment sequence number for incoming packet 939 * increment sequence number for incoming packet
846 */ 940 */
847 if (mac && mac->enabled) { 941 if (mac && mac->enabled) {
848 macbuf = mac_compute(mac, seqnr, 942 macbuf = mac_compute(mac, read_seqnr,
849 buffer_ptr(&incoming_packet), 943 buffer_ptr(&incoming_packet),
850 buffer_len(&incoming_packet)); 944 buffer_len(&incoming_packet));
851 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0) 945 if (memcmp(macbuf, buffer_ptr(&input), mac->mac_len) != 0)
852 packet_disconnect("Corrupted MAC on input."); 946 packet_disconnect("Corrupted MAC on input.");
853 DBG(debug("MAC #%d ok", seqnr)); 947 DBG(debug("MAC #%d ok", read_seqnr));
854 buffer_consume(&input, mac->mac_len); 948 buffer_consume(&input, mac->mac_len);
855 } 949 }
856 if (seqnr_p != NULL) 950 if (seqnr_p != NULL)
857 *seqnr_p = seqnr; 951 *seqnr_p = read_seqnr;
858 if (++seqnr == 0) 952 if (++read_seqnr == 0)
859 log("incoming seqnr wraps around"); 953 log("incoming seqnr wraps around");
860 954
861 /* get padlen */ 955 /* get padlen */