blob: fca48e5f5193b8c0870aba313eaa4ef333051f27 [file] [log] [blame]
djm@openbsd.orgb011edb2017-10-20 01:56:39 +00001/* $OpenBSD: opacket.c,v 1.7 2017/10/20 01:56:39 djm Exp $ */
markus@openbsd.org091c3022015-01-19 19:52:16 +00002/* Written by Markus Friedl. Placed in the public domain. */
3
4#include "includes.h"
5
6#include "ssherr.h"
7#include "packet.h"
8#include "log.h"
9
10struct ssh *active_state, *backup_state;
11
12/* Map old to new API */
13
14void
15ssh_packet_start(struct ssh *ssh, u_char type)
16{
17 int r;
18
19 if ((r = sshpkt_start(ssh, type)) != 0)
20 fatal("%s: %s", __func__, ssh_err(r));
21}
22
23void
24ssh_packet_put_char(struct ssh *ssh, int value)
25{
26 u_char ch = value;
27 int r;
28
29 if ((r = sshpkt_put_u8(ssh, ch)) != 0)
30 fatal("%s: %s", __func__, ssh_err(r));
31}
32
33void
34ssh_packet_put_int(struct ssh *ssh, u_int value)
35{
36 int r;
37
38 if ((r = sshpkt_put_u32(ssh, value)) != 0)
39 fatal("%s: %s", __func__, ssh_err(r));
40}
41
42void
43ssh_packet_put_int64(struct ssh *ssh, u_int64_t value)
44{
45 int r;
46
47 if ((r = sshpkt_put_u64(ssh, value)) != 0)
48 fatal("%s: %s", __func__, ssh_err(r));
49}
50
51void
52ssh_packet_put_string(struct ssh *ssh, const void *buf, u_int len)
53{
54 int r;
55
56 if ((r = sshpkt_put_string(ssh, buf, len)) != 0)
57 fatal("%s: %s", __func__, ssh_err(r));
58}
59
60void
61ssh_packet_put_cstring(struct ssh *ssh, const char *str)
62{
63 int r;
64
65 if ((r = sshpkt_put_cstring(ssh, str)) != 0)
66 fatal("%s: %s", __func__, ssh_err(r));
67}
68
69void
70ssh_packet_put_raw(struct ssh *ssh, const void *buf, u_int len)
71{
72 int r;
73
74 if ((r = sshpkt_put(ssh, buf, len)) != 0)
75 fatal("%s: %s", __func__, ssh_err(r));
76}
77
markus@openbsd.org091c3022015-01-19 19:52:16 +000078
Damien Millerbbffb232015-03-03 13:50:27 -080079#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +000080void
81ssh_packet_put_bignum2(struct ssh *ssh, BIGNUM * value)
82{
83 int r;
84
85 if ((r = sshpkt_put_bignum2(ssh, value)) != 0)
86 fatal("%s: %s", __func__, ssh_err(r));
87}
88
Darren Tuckerf2004cd2015-02-23 05:04:21 +110089# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +000090void
91ssh_packet_put_ecpoint(struct ssh *ssh, const EC_GROUP *curve,
92 const EC_POINT *point)
93{
94 int r;
95
96 if ((r = sshpkt_put_ec(ssh, point, curve)) != 0)
97 fatal("%s: %s", __func__, ssh_err(r));
98}
Darren Tuckerf2004cd2015-02-23 05:04:21 +110099# endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000100#endif /* WITH_OPENSSL */
101
102void
103ssh_packet_send(struct ssh *ssh)
104{
105 int r;
106
107 if ((r = sshpkt_send(ssh)) != 0)
108 fatal("%s: %s", __func__, ssh_err(r));
109}
110
111u_int
112ssh_packet_get_char(struct ssh *ssh)
113{
114 u_char ch;
115 int r;
116
117 if ((r = sshpkt_get_u8(ssh, &ch)) != 0)
118 fatal("%s: %s", __func__, ssh_err(r));
119 return ch;
120}
121
122u_int
123ssh_packet_get_int(struct ssh *ssh)
124{
125 u_int val;
126 int r;
127
128 if ((r = sshpkt_get_u32(ssh, &val)) != 0)
129 fatal("%s: %s", __func__, ssh_err(r));
130 return val;
131}
132
133u_int64_t
134ssh_packet_get_int64(struct ssh *ssh)
135{
136 u_int64_t val;
137 int r;
138
139 if ((r = sshpkt_get_u64(ssh, &val)) != 0)
140 fatal("%s: %s", __func__, ssh_err(r));
141 return val;
142}
143
markus@openbsd.org091c3022015-01-19 19:52:16 +0000144
Damien Millerbbffb232015-03-03 13:50:27 -0800145#ifdef WITH_OPENSSL
markus@openbsd.org091c3022015-01-19 19:52:16 +0000146void
147ssh_packet_get_bignum2(struct ssh *ssh, BIGNUM * value)
148{
149 int r;
150
151 if ((r = sshpkt_get_bignum2(ssh, value)) != 0)
152 fatal("%s: %s", __func__, ssh_err(r));
153}
154
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100155# ifdef OPENSSL_HAS_ECC
markus@openbsd.org091c3022015-01-19 19:52:16 +0000156void
157ssh_packet_get_ecpoint(struct ssh *ssh, const EC_GROUP *curve, EC_POINT *point)
158{
159 int r;
160
161 if ((r = sshpkt_get_ec(ssh, point, curve)) != 0)
162 fatal("%s: %s", __func__, ssh_err(r));
163}
Darren Tuckerf2004cd2015-02-23 05:04:21 +1100164# endif
markus@openbsd.org091c3022015-01-19 19:52:16 +0000165#endif /* WITH_OPENSSL */
166
167void *
168ssh_packet_get_string(struct ssh *ssh, u_int *length_ptr)
169{
170 int r;
171 size_t len;
172 u_char *val;
173
174 if ((r = sshpkt_get_string(ssh, &val, &len)) != 0)
175 fatal("%s: %s", __func__, ssh_err(r));
176 if (length_ptr != NULL)
177 *length_ptr = (u_int)len;
178 return val;
179}
180
181const void *
182ssh_packet_get_string_ptr(struct ssh *ssh, u_int *length_ptr)
183{
184 int r;
185 size_t len;
186 const u_char *val;
187
188 if ((r = sshpkt_get_string_direct(ssh, &val, &len)) != 0)
189 fatal("%s: %s", __func__, ssh_err(r));
190 if (length_ptr != NULL)
191 *length_ptr = (u_int)len;
192 return val;
193}
194
195char *
196ssh_packet_get_cstring(struct ssh *ssh, u_int *length_ptr)
197{
198 int r;
199 size_t len;
200 char *val;
201
202 if ((r = sshpkt_get_cstring(ssh, &val, &len)) != 0)
203 fatal("%s: %s", __func__, ssh_err(r));
204 if (length_ptr != NULL)
205 *length_ptr = (u_int)len;
206 return val;
207}
208
209/* Old API, that had to be reimplemented */
210
211void
212packet_set_connection(int fd_in, int fd_out)
213{
214 active_state = ssh_packet_set_connection(active_state, fd_in, fd_out);
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000215 if (active_state == NULL)
216 fatal("%s: ssh_packet_set_connection failed", __func__);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000217}
218
markus@openbsd.org091c3022015-01-19 19:52:16 +0000219u_int
220packet_get_char(void)
221{
222 return (ssh_packet_get_char(active_state));
223}
224
225u_int
226packet_get_int(void)
227{
228 return (ssh_packet_get_int(active_state));
229}
230
231int
232packet_read_seqnr(u_int32_t *seqnr)
233{
234 u_char type;
235 int r;
236
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000237 if ((r = ssh_packet_read_seqnr(active_state, &type, seqnr)) != 0)
238 sshpkt_fatal(active_state, __func__, r);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000239 return type;
240}
241
242int
243packet_read_poll_seqnr(u_int32_t *seqnr)
244{
245 u_char type;
246 int r;
247
248 if ((r = ssh_packet_read_poll_seqnr(active_state, &type, seqnr)))
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000249 sshpkt_fatal(active_state, __func__, r);
markus@openbsd.org091c3022015-01-19 19:52:16 +0000250 return type;
251}
252
253void
254packet_close(void)
255{
256 ssh_packet_close(active_state);
257 active_state = NULL;
258}
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +0000259
260void
261packet_process_incoming(const char *buf, u_int len)
262{
263 int r;
264
265 if ((r = ssh_packet_process_incoming(active_state, buf, len)) != 0)
djm@openbsd.org4509b5d2015-01-30 01:13:33 +0000266 sshpkt_fatal(active_state, __func__, r);
267}
268
269void
270packet_write_wait(void)
271{
272 int r;
273
274 if ((r = ssh_packet_write_wait(active_state)) != 0)
275 sshpkt_fatal(active_state, __func__, r);
276}
277
278void
279packet_write_poll(void)
280{
281 int r;
282
283 if ((r = ssh_packet_write_poll(active_state)) != 0)
284 sshpkt_fatal(active_state, __func__, r);
285}
286
287void
288packet_read_expect(int expected_type)
289{
290 int r;
291
292 if ((r = ssh_packet_read_expect(active_state, expected_type)) != 0)
293 sshpkt_fatal(active_state, __func__, r);
djm@openbsd.orgfae7bbe2015-01-28 21:15:47 +0000294}
Darren Tuckerdcc89972015-02-24 12:30:59 +1100295
296void
297packet_disconnect(const char *fmt, ...)
298{
299 char buf[1024];
300 va_list args;
301
302 va_start(args, fmt);
303 vsnprintf(buf, sizeof(buf), fmt, args);
304 va_end(args);
305 ssh_packet_disconnect(active_state, "%s", buf);
306}
307
308void
309packet_send_debug(const char *fmt, ...)
310{
311 char buf[1024];
312 va_list args;
313
314 va_start(args, fmt);
315 vsnprintf(buf, sizeof(buf), fmt, args);
316 va_end(args);
317 ssh_packet_send_debug(active_state, "%s", buf);
318}