summaryrefslogtreecommitdiff
path: root/opacket.c
blob: a137b5a8a62bc8b6174645009af45d5380ffc4dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* Written by Markus Friedl. Placed in the public domain.  */

#include "includes.h"

#include "ssherr.h"
#include "packet.h"
#include "log.h"

struct ssh *active_state, *backup_state;

/* Map old to new API */

void
ssh_packet_start(struct ssh *ssh, u_char type)
{
	int r;

	if ((r = sshpkt_start(ssh, type)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

void
ssh_packet_put_char(struct ssh *ssh, int value)
{
	u_char ch = value;
	int r;

	if ((r = sshpkt_put_u8(ssh, ch)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

void
ssh_packet_put_int(struct ssh *ssh, u_int value)
{
	int r;

	if ((r = sshpkt_put_u32(ssh, value)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

void
ssh_packet_put_int64(struct ssh *ssh, u_int64_t value)
{
	int r;

	if ((r = sshpkt_put_u64(ssh, value)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

void
ssh_packet_put_string(struct ssh *ssh, const void *buf, u_int len)
{
	int r;

	if ((r = sshpkt_put_string(ssh, buf, len)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

void
ssh_packet_put_cstring(struct ssh *ssh, const char *str)
{
	int r;

	if ((r = sshpkt_put_cstring(ssh, str)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

void
ssh_packet_put_raw(struct ssh *ssh, const void *buf, u_int len)
{
	int r;

	if ((r = sshpkt_put(ssh, buf, len)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

#ifdef WITH_OPENSSL
void
ssh_packet_put_bignum(struct ssh *ssh, BIGNUM * value)
{
	int r;

	if ((r = sshpkt_put_bignum1(ssh, value)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

void
ssh_packet_put_bignum2(struct ssh *ssh, BIGNUM * value)
{
	int r;

	if ((r = sshpkt_put_bignum2(ssh, value)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

void
ssh_packet_put_ecpoint(struct ssh *ssh, const EC_GROUP *curve,
    const EC_POINT *point)
{
	int r;

	if ((r = sshpkt_put_ec(ssh, point, curve)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}
#endif /* WITH_OPENSSL */

void
ssh_packet_send(struct ssh *ssh)
{
	int r;

	if ((r = sshpkt_send(ssh)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

u_int
ssh_packet_get_char(struct ssh *ssh)
{
	u_char ch;
	int r;

	if ((r = sshpkt_get_u8(ssh, &ch)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
	return ch;
}

u_int
ssh_packet_get_int(struct ssh *ssh)
{
	u_int val;
	int r;

	if ((r = sshpkt_get_u32(ssh, &val)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
	return val;
}

u_int64_t
ssh_packet_get_int64(struct ssh *ssh)
{
	u_int64_t val;
	int r;

	if ((r = sshpkt_get_u64(ssh, &val)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
	return val;
}

#ifdef WITH_OPENSSL
void
ssh_packet_get_bignum(struct ssh *ssh, BIGNUM * value)
{
	int r;

	if ((r = sshpkt_get_bignum1(ssh, value)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

void
ssh_packet_get_bignum2(struct ssh *ssh, BIGNUM * value)
{
	int r;

	if ((r = sshpkt_get_bignum2(ssh, value)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}

void
ssh_packet_get_ecpoint(struct ssh *ssh, const EC_GROUP *curve, EC_POINT *point)
{
	int r;

	if ((r = sshpkt_get_ec(ssh, point, curve)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}
#endif /* WITH_OPENSSL */

void *
ssh_packet_get_string(struct ssh *ssh, u_int *length_ptr)
{
	int r;
	size_t len;
	u_char *val;

	if ((r = sshpkt_get_string(ssh, &val, &len)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
	if (length_ptr != NULL)
		*length_ptr = (u_int)len;
	return val;
}

const void *
ssh_packet_get_string_ptr(struct ssh *ssh, u_int *length_ptr)
{
	int r;
	size_t len;
	const u_char *val;

	if ((r = sshpkt_get_string_direct(ssh, &val, &len)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
	if (length_ptr != NULL)
		*length_ptr = (u_int)len;
	return val;
}

char *
ssh_packet_get_cstring(struct ssh *ssh, u_int *length_ptr)
{
	int r;
	size_t len;
	char *val;

	if ((r = sshpkt_get_cstring(ssh, &val, &len)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
	if (length_ptr != NULL)
		*length_ptr = (u_int)len;
	return val;
}

/* Old API, that had to be reimplemented */

void
packet_set_connection(int fd_in, int fd_out)
{
	active_state = ssh_packet_set_connection(active_state, fd_in, fd_out);
}

void
packet_backup_state(void)
{
	ssh_packet_backup_state(active_state, backup_state);
}

void
packet_restore_state(void)
{
	ssh_packet_restore_state(active_state, backup_state);
}

u_int
packet_get_char(void)
{
	return (ssh_packet_get_char(active_state));
}

u_int
packet_get_int(void)
{
	return (ssh_packet_get_int(active_state));
}

int
packet_read_seqnr(u_int32_t *seqnr)
{
	u_char type;
	int r;

	if ((r = ssh_packet_read_seqnr(active_state, &type, seqnr)) != 0) {
		switch (r) {
		case SSH_ERR_CONN_CLOSED:
			logit("Connection closed by %.200s",
			    ssh_remote_ipaddr(active_state));
			cleanup_exit(255);
		case SSH_ERR_CONN_TIMEOUT:
			logit("Connection to %.200s timed out while "
			    "waiting to read", ssh_remote_ipaddr(active_state));
			cleanup_exit(255);
		default:
			fatal("%s: %s", __func__, ssh_err(r));
		}
	}
	return type;
}

int
packet_read_poll_seqnr(u_int32_t *seqnr)
{
	u_char type;
	int r;

	if ((r = ssh_packet_read_poll_seqnr(active_state, &type, seqnr)))
		fatal("%s: %s", __func__, ssh_err(r));
	return type;
}

void
packet_close(void)
{
	ssh_packet_close(active_state);
	active_state = NULL;
}

void
packet_process_incoming(const char *buf, u_int len)
{
	int r;

	if ((r = ssh_packet_process_incoming(active_state, buf, len)) != 0)
		fatal("%s: %s", __func__, ssh_err(r));
}