1 |
|
|
/* Written by Markus Friedl. Placed in the public domain. */ |
2 |
|
|
|
3 |
|
|
#include "ssherr.h" |
4 |
|
|
#include "packet.h" |
5 |
|
|
#include "log.h" |
6 |
|
|
|
7 |
|
|
struct ssh *active_state, *backup_state; |
8 |
|
|
|
9 |
|
|
/* Map old to new API */ |
10 |
|
|
|
11 |
|
|
void |
12 |
|
|
ssh_packet_start(struct ssh *ssh, u_char type) |
13 |
|
|
{ |
14 |
|
|
int r; |
15 |
|
|
|
16 |
✗✓ |
8 |
if ((r = sshpkt_start(ssh, type)) != 0) |
17 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
18 |
|
4 |
} |
19 |
|
|
|
20 |
|
|
void |
21 |
|
|
ssh_packet_put_char(struct ssh *ssh, int value) |
22 |
|
|
{ |
23 |
|
4 |
u_char ch = value; |
24 |
|
|
int r; |
25 |
|
|
|
26 |
✗✓ |
2 |
if ((r = sshpkt_put_u8(ssh, ch)) != 0) |
27 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
28 |
|
2 |
} |
29 |
|
|
|
30 |
|
|
void |
31 |
|
|
ssh_packet_put_int(struct ssh *ssh, u_int value) |
32 |
|
|
{ |
33 |
|
|
int r; |
34 |
|
|
|
35 |
✗✓ |
2 |
if ((r = sshpkt_put_u32(ssh, value)) != 0) |
36 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
37 |
|
1 |
} |
38 |
|
|
|
39 |
|
|
void |
40 |
|
|
ssh_packet_put_int64(struct ssh *ssh, u_int64_t value) |
41 |
|
|
{ |
42 |
|
|
int r; |
43 |
|
|
|
44 |
|
|
if ((r = sshpkt_put_u64(ssh, value)) != 0) |
45 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
void |
49 |
|
|
ssh_packet_put_string(struct ssh *ssh, const void *buf, u_int len) |
50 |
|
|
{ |
51 |
|
|
int r; |
52 |
|
|
|
53 |
✗✓ |
2 |
if ((r = sshpkt_put_string(ssh, buf, len)) != 0) |
54 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
55 |
|
1 |
} |
56 |
|
|
|
57 |
|
|
void |
58 |
|
|
ssh_packet_put_cstring(struct ssh *ssh, const char *str) |
59 |
|
|
{ |
60 |
|
|
int r; |
61 |
|
|
|
62 |
✗✓ |
20 |
if ((r = sshpkt_put_cstring(ssh, str)) != 0) |
63 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
64 |
|
10 |
} |
65 |
|
|
|
66 |
|
|
void |
67 |
|
|
ssh_packet_put_raw(struct ssh *ssh, const void *buf, u_int len) |
68 |
|
|
{ |
69 |
|
|
int r; |
70 |
|
|
|
71 |
|
|
if ((r = sshpkt_put(ssh, buf, len)) != 0) |
72 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
|
76 |
|
|
#ifdef WITH_OPENSSL |
77 |
|
|
void |
78 |
|
|
ssh_packet_put_bignum2(struct ssh *ssh, BIGNUM * value) |
79 |
|
|
{ |
80 |
|
|
int r; |
81 |
|
|
|
82 |
|
|
if ((r = sshpkt_put_bignum2(ssh, value)) != 0) |
83 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
void |
87 |
|
|
ssh_packet_put_ecpoint(struct ssh *ssh, const EC_GROUP *curve, |
88 |
|
|
const EC_POINT *point) |
89 |
|
|
{ |
90 |
|
|
int r; |
91 |
|
|
|
92 |
|
|
if ((r = sshpkt_put_ec(ssh, point, curve)) != 0) |
93 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
94 |
|
|
} |
95 |
|
|
#endif /* WITH_OPENSSL */ |
96 |
|
|
|
97 |
|
|
void |
98 |
|
|
ssh_packet_send(struct ssh *ssh) |
99 |
|
|
{ |
100 |
|
|
int r; |
101 |
|
|
|
102 |
✗✓ |
10 |
if ((r = sshpkt_send(ssh)) != 0) |
103 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
104 |
|
5 |
} |
105 |
|
|
|
106 |
|
|
u_int |
107 |
|
|
ssh_packet_get_char(struct ssh *ssh) |
108 |
|
|
{ |
109 |
|
6 |
u_char ch; |
110 |
|
|
int r; |
111 |
|
|
|
112 |
✗✓ |
3 |
if ((r = sshpkt_get_u8(ssh, &ch)) != 0) |
113 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
114 |
|
6 |
return ch; |
115 |
|
3 |
} |
116 |
|
|
|
117 |
|
|
u_int |
118 |
|
|
ssh_packet_get_int(struct ssh *ssh) |
119 |
|
|
{ |
120 |
|
4 |
u_int val; |
121 |
|
|
int r; |
122 |
|
|
|
123 |
✗✓ |
2 |
if ((r = sshpkt_get_u32(ssh, &val)) != 0) |
124 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
125 |
|
4 |
return val; |
126 |
|
2 |
} |
127 |
|
|
|
128 |
|
|
u_int64_t |
129 |
|
|
ssh_packet_get_int64(struct ssh *ssh) |
130 |
|
|
{ |
131 |
|
|
u_int64_t val; |
132 |
|
|
int r; |
133 |
|
|
|
134 |
|
|
if ((r = sshpkt_get_u64(ssh, &val)) != 0) |
135 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
136 |
|
|
return val; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
|
140 |
|
|
#ifdef WITH_OPENSSL |
141 |
|
|
void |
142 |
|
|
ssh_packet_get_bignum2(struct ssh *ssh, BIGNUM * value) |
143 |
|
|
{ |
144 |
|
|
int r; |
145 |
|
|
|
146 |
|
|
if ((r = sshpkt_get_bignum2(ssh, value)) != 0) |
147 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
void |
151 |
|
|
ssh_packet_get_ecpoint(struct ssh *ssh, const EC_GROUP *curve, EC_POINT *point) |
152 |
|
|
{ |
153 |
|
|
int r; |
154 |
|
|
|
155 |
|
|
if ((r = sshpkt_get_ec(ssh, point, curve)) != 0) |
156 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
157 |
|
|
} |
158 |
|
|
#endif /* WITH_OPENSSL */ |
159 |
|
|
|
160 |
|
|
void * |
161 |
|
|
ssh_packet_get_string(struct ssh *ssh, u_int *length_ptr) |
162 |
|
|
{ |
163 |
|
|
int r; |
164 |
|
4 |
size_t len; |
165 |
|
2 |
u_char *val; |
166 |
|
|
|
167 |
✗✓ |
2 |
if ((r = sshpkt_get_string(ssh, &val, &len)) != 0) |
168 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
169 |
✗✓ |
2 |
if (length_ptr != NULL) |
170 |
|
|
*length_ptr = (u_int)len; |
171 |
|
4 |
return val; |
172 |
|
2 |
} |
173 |
|
|
|
174 |
|
|
const void * |
175 |
|
|
ssh_packet_get_string_ptr(struct ssh *ssh, u_int *length_ptr) |
176 |
|
|
{ |
177 |
|
|
int r; |
178 |
|
|
size_t len; |
179 |
|
|
const u_char *val; |
180 |
|
|
|
181 |
|
|
if ((r = sshpkt_get_string_direct(ssh, &val, &len)) != 0) |
182 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
183 |
|
|
if (length_ptr != NULL) |
184 |
|
|
*length_ptr = (u_int)len; |
185 |
|
|
return val; |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
char * |
189 |
|
|
ssh_packet_get_cstring(struct ssh *ssh, u_int *length_ptr) |
190 |
|
|
{ |
191 |
|
|
int r; |
192 |
|
2 |
size_t len; |
193 |
|
1 |
char *val; |
194 |
|
|
|
195 |
✗✓ |
1 |
if ((r = sshpkt_get_cstring(ssh, &val, &len)) != 0) |
196 |
|
|
fatal("%s: %s", __func__, ssh_err(r)); |
197 |
✗✓ |
1 |
if (length_ptr != NULL) |
198 |
|
|
*length_ptr = (u_int)len; |
199 |
|
2 |
return val; |
200 |
|
1 |
} |
201 |
|
|
|
202 |
|
|
/* Old API, that had to be reimplemented */ |
203 |
|
|
|
204 |
|
|
void |
205 |
|
|
packet_set_connection(int fd_in, int fd_out) |
206 |
|
|
{ |
207 |
|
|
active_state = ssh_packet_set_connection(active_state, fd_in, fd_out); |
208 |
|
|
if (active_state == NULL) |
209 |
|
|
fatal("%s: ssh_packet_set_connection failed", __func__); |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
u_int |
213 |
|
|
packet_get_char(void) |
214 |
|
|
{ |
215 |
|
6 |
return (ssh_packet_get_char(active_state)); |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
u_int |
219 |
|
|
packet_get_int(void) |
220 |
|
|
{ |
221 |
|
4 |
return (ssh_packet_get_int(active_state)); |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
int |
225 |
|
|
packet_read_seqnr(u_int32_t *seqnr) |
226 |
|
|
{ |
227 |
|
|
u_char type; |
228 |
|
|
int r; |
229 |
|
|
|
230 |
|
|
if ((r = ssh_packet_read_seqnr(active_state, &type, seqnr)) != 0) |
231 |
|
|
sshpkt_fatal(active_state, __func__, r); |
232 |
|
|
return type; |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
int |
236 |
|
|
packet_read_poll_seqnr(u_int32_t *seqnr) |
237 |
|
|
{ |
238 |
|
|
u_char type; |
239 |
|
|
int r; |
240 |
|
|
|
241 |
|
|
if ((r = ssh_packet_read_poll_seqnr(active_state, &type, seqnr))) |
242 |
|
|
sshpkt_fatal(active_state, __func__, r); |
243 |
|
|
return type; |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
void |
247 |
|
|
packet_close(void) |
248 |
|
|
{ |
249 |
|
2 |
ssh_packet_close(active_state); |
250 |
|
1 |
active_state = NULL; |
251 |
|
1 |
} |
252 |
|
|
|
253 |
|
|
void |
254 |
|
|
packet_process_incoming(const char *buf, u_int len) |
255 |
|
|
{ |
256 |
|
|
int r; |
257 |
|
|
|
258 |
✗✓ |
28800 |
if ((r = ssh_packet_process_incoming(active_state, buf, len)) != 0) |
259 |
|
|
sshpkt_fatal(active_state, __func__, r); |
260 |
|
14400 |
} |
261 |
|
|
|
262 |
|
|
void |
263 |
|
|
packet_write_wait(void) |
264 |
|
|
{ |
265 |
|
|
int r; |
266 |
|
|
|
267 |
✗✓ |
2 |
if ((r = ssh_packet_write_wait(active_state)) != 0) |
268 |
|
|
sshpkt_fatal(active_state, __func__, r); |
269 |
|
1 |
} |
270 |
|
|
|
271 |
|
|
void |
272 |
|
|
packet_write_poll(void) |
273 |
|
|
{ |
274 |
|
|
int r; |
275 |
|
|
|
276 |
✗✓ |
84276 |
if ((r = ssh_packet_write_poll(active_state)) != 0) |
277 |
|
|
sshpkt_fatal(active_state, __func__, r); |
278 |
|
42138 |
} |
279 |
|
|
|
280 |
|
|
void |
281 |
|
|
packet_read_expect(int expected_type) |
282 |
|
|
{ |
283 |
|
|
int r; |
284 |
|
|
|
285 |
|
|
if ((r = ssh_packet_read_expect(active_state, expected_type)) != 0) |
286 |
|
|
sshpkt_fatal(active_state, __func__, r); |
287 |
|
|
} |