markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 1 | /* Written by Markus Friedl. Placed in the public domain. */ |
| 2 | |
| 3 | #include "includes.h" |
| 4 | |
| 5 | #include "ssherr.h" |
| 6 | #include "packet.h" |
| 7 | #include "log.h" |
| 8 | |
| 9 | struct ssh *active_state, *backup_state; |
| 10 | |
| 11 | /* Map old to new API */ |
| 12 | |
| 13 | void |
| 14 | ssh_packet_start(struct ssh *ssh, u_char type) |
| 15 | { |
| 16 | int r; |
| 17 | |
| 18 | if ((r = sshpkt_start(ssh, type)) != 0) |
| 19 | fatal("%s: %s", __func__, ssh_err(r)); |
| 20 | } |
| 21 | |
| 22 | void |
| 23 | ssh_packet_put_char(struct ssh *ssh, int value) |
| 24 | { |
| 25 | u_char ch = value; |
| 26 | int r; |
| 27 | |
| 28 | if ((r = sshpkt_put_u8(ssh, ch)) != 0) |
| 29 | fatal("%s: %s", __func__, ssh_err(r)); |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | ssh_packet_put_int(struct ssh *ssh, u_int value) |
| 34 | { |
| 35 | int r; |
| 36 | |
| 37 | if ((r = sshpkt_put_u32(ssh, value)) != 0) |
| 38 | fatal("%s: %s", __func__, ssh_err(r)); |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | ssh_packet_put_int64(struct ssh *ssh, u_int64_t value) |
| 43 | { |
| 44 | int r; |
| 45 | |
| 46 | if ((r = sshpkt_put_u64(ssh, value)) != 0) |
| 47 | fatal("%s: %s", __func__, ssh_err(r)); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | ssh_packet_put_string(struct ssh *ssh, const void *buf, u_int len) |
| 52 | { |
| 53 | int r; |
| 54 | |
| 55 | if ((r = sshpkt_put_string(ssh, buf, len)) != 0) |
| 56 | fatal("%s: %s", __func__, ssh_err(r)); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | ssh_packet_put_cstring(struct ssh *ssh, const char *str) |
| 61 | { |
| 62 | int r; |
| 63 | |
| 64 | if ((r = sshpkt_put_cstring(ssh, str)) != 0) |
| 65 | fatal("%s: %s", __func__, ssh_err(r)); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | ssh_packet_put_raw(struct ssh *ssh, const void *buf, u_int len) |
| 70 | { |
| 71 | int r; |
| 72 | |
| 73 | if ((r = sshpkt_put(ssh, buf, len)) != 0) |
| 74 | fatal("%s: %s", __func__, ssh_err(r)); |
| 75 | } |
| 76 | |
Damien Miller | bbffb23 | 2015-03-03 13:50:27 -0800 | [diff] [blame] | 77 | #ifdef WITH_SSH1 |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 78 | void |
| 79 | ssh_packet_put_bignum(struct ssh *ssh, BIGNUM * value) |
| 80 | { |
| 81 | int r; |
| 82 | |
| 83 | if ((r = sshpkt_put_bignum1(ssh, value)) != 0) |
| 84 | fatal("%s: %s", __func__, ssh_err(r)); |
| 85 | } |
Damien Miller | bbffb23 | 2015-03-03 13:50:27 -0800 | [diff] [blame] | 86 | #endif |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 87 | |
Damien Miller | bbffb23 | 2015-03-03 13:50:27 -0800 | [diff] [blame] | 88 | #ifdef WITH_OPENSSL |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 89 | void |
| 90 | ssh_packet_put_bignum2(struct ssh *ssh, BIGNUM * value) |
| 91 | { |
| 92 | int r; |
| 93 | |
| 94 | if ((r = sshpkt_put_bignum2(ssh, value)) != 0) |
| 95 | fatal("%s: %s", __func__, ssh_err(r)); |
| 96 | } |
| 97 | |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 98 | # ifdef OPENSSL_HAS_ECC |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 99 | void |
| 100 | ssh_packet_put_ecpoint(struct ssh *ssh, const EC_GROUP *curve, |
| 101 | const EC_POINT *point) |
| 102 | { |
| 103 | int r; |
| 104 | |
| 105 | if ((r = sshpkt_put_ec(ssh, point, curve)) != 0) |
| 106 | fatal("%s: %s", __func__, ssh_err(r)); |
| 107 | } |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 108 | # endif |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 109 | #endif /* WITH_OPENSSL */ |
| 110 | |
| 111 | void |
| 112 | ssh_packet_send(struct ssh *ssh) |
| 113 | { |
| 114 | int r; |
| 115 | |
| 116 | if ((r = sshpkt_send(ssh)) != 0) |
| 117 | fatal("%s: %s", __func__, ssh_err(r)); |
| 118 | } |
| 119 | |
| 120 | u_int |
| 121 | ssh_packet_get_char(struct ssh *ssh) |
| 122 | { |
| 123 | u_char ch; |
| 124 | int r; |
| 125 | |
| 126 | if ((r = sshpkt_get_u8(ssh, &ch)) != 0) |
| 127 | fatal("%s: %s", __func__, ssh_err(r)); |
| 128 | return ch; |
| 129 | } |
| 130 | |
| 131 | u_int |
| 132 | ssh_packet_get_int(struct ssh *ssh) |
| 133 | { |
| 134 | u_int val; |
| 135 | int r; |
| 136 | |
| 137 | if ((r = sshpkt_get_u32(ssh, &val)) != 0) |
| 138 | fatal("%s: %s", __func__, ssh_err(r)); |
| 139 | return val; |
| 140 | } |
| 141 | |
| 142 | u_int64_t |
| 143 | ssh_packet_get_int64(struct ssh *ssh) |
| 144 | { |
| 145 | u_int64_t val; |
| 146 | int r; |
| 147 | |
| 148 | if ((r = sshpkt_get_u64(ssh, &val)) != 0) |
| 149 | fatal("%s: %s", __func__, ssh_err(r)); |
| 150 | return val; |
| 151 | } |
| 152 | |
Damien Miller | bbffb23 | 2015-03-03 13:50:27 -0800 | [diff] [blame] | 153 | #ifdef WITH_SSH1 |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 154 | void |
| 155 | ssh_packet_get_bignum(struct ssh *ssh, BIGNUM * value) |
| 156 | { |
| 157 | int r; |
| 158 | |
| 159 | if ((r = sshpkt_get_bignum1(ssh, value)) != 0) |
| 160 | fatal("%s: %s", __func__, ssh_err(r)); |
| 161 | } |
Damien Miller | bbffb23 | 2015-03-03 13:50:27 -0800 | [diff] [blame] | 162 | #endif |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 163 | |
Damien Miller | bbffb23 | 2015-03-03 13:50:27 -0800 | [diff] [blame] | 164 | #ifdef WITH_OPENSSL |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 165 | void |
| 166 | ssh_packet_get_bignum2(struct ssh *ssh, BIGNUM * value) |
| 167 | { |
| 168 | int r; |
| 169 | |
| 170 | if ((r = sshpkt_get_bignum2(ssh, value)) != 0) |
| 171 | fatal("%s: %s", __func__, ssh_err(r)); |
| 172 | } |
| 173 | |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 174 | # ifdef OPENSSL_HAS_ECC |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 175 | void |
| 176 | ssh_packet_get_ecpoint(struct ssh *ssh, const EC_GROUP *curve, EC_POINT *point) |
| 177 | { |
| 178 | int r; |
| 179 | |
| 180 | if ((r = sshpkt_get_ec(ssh, point, curve)) != 0) |
| 181 | fatal("%s: %s", __func__, ssh_err(r)); |
| 182 | } |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 183 | # endif |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 184 | #endif /* WITH_OPENSSL */ |
| 185 | |
| 186 | void * |
| 187 | ssh_packet_get_string(struct ssh *ssh, u_int *length_ptr) |
| 188 | { |
| 189 | int r; |
| 190 | size_t len; |
| 191 | u_char *val; |
| 192 | |
| 193 | if ((r = sshpkt_get_string(ssh, &val, &len)) != 0) |
| 194 | fatal("%s: %s", __func__, ssh_err(r)); |
| 195 | if (length_ptr != NULL) |
| 196 | *length_ptr = (u_int)len; |
| 197 | return val; |
| 198 | } |
| 199 | |
| 200 | const void * |
| 201 | ssh_packet_get_string_ptr(struct ssh *ssh, u_int *length_ptr) |
| 202 | { |
| 203 | int r; |
| 204 | size_t len; |
| 205 | const u_char *val; |
| 206 | |
| 207 | if ((r = sshpkt_get_string_direct(ssh, &val, &len)) != 0) |
| 208 | fatal("%s: %s", __func__, ssh_err(r)); |
| 209 | if (length_ptr != NULL) |
| 210 | *length_ptr = (u_int)len; |
| 211 | return val; |
| 212 | } |
| 213 | |
| 214 | char * |
| 215 | ssh_packet_get_cstring(struct ssh *ssh, u_int *length_ptr) |
| 216 | { |
| 217 | int r; |
| 218 | size_t len; |
| 219 | char *val; |
| 220 | |
| 221 | if ((r = sshpkt_get_cstring(ssh, &val, &len)) != 0) |
| 222 | fatal("%s: %s", __func__, ssh_err(r)); |
| 223 | if (length_ptr != NULL) |
| 224 | *length_ptr = (u_int)len; |
| 225 | return val; |
| 226 | } |
| 227 | |
| 228 | /* Old API, that had to be reimplemented */ |
| 229 | |
| 230 | void |
| 231 | packet_set_connection(int fd_in, int fd_out) |
| 232 | { |
| 233 | active_state = ssh_packet_set_connection(active_state, fd_in, fd_out); |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 234 | if (active_state == NULL) |
| 235 | fatal("%s: ssh_packet_set_connection failed", __func__); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void |
| 239 | packet_backup_state(void) |
| 240 | { |
| 241 | ssh_packet_backup_state(active_state, backup_state); |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | packet_restore_state(void) |
| 246 | { |
| 247 | ssh_packet_restore_state(active_state, backup_state); |
| 248 | } |
| 249 | |
| 250 | u_int |
| 251 | packet_get_char(void) |
| 252 | { |
| 253 | return (ssh_packet_get_char(active_state)); |
| 254 | } |
| 255 | |
| 256 | u_int |
| 257 | packet_get_int(void) |
| 258 | { |
| 259 | return (ssh_packet_get_int(active_state)); |
| 260 | } |
| 261 | |
| 262 | int |
| 263 | packet_read_seqnr(u_int32_t *seqnr) |
| 264 | { |
| 265 | u_char type; |
| 266 | int r; |
| 267 | |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 268 | if ((r = ssh_packet_read_seqnr(active_state, &type, seqnr)) != 0) |
| 269 | sshpkt_fatal(active_state, __func__, r); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 270 | return type; |
| 271 | } |
| 272 | |
| 273 | int |
| 274 | packet_read_poll_seqnr(u_int32_t *seqnr) |
| 275 | { |
| 276 | u_char type; |
| 277 | int r; |
| 278 | |
| 279 | if ((r = ssh_packet_read_poll_seqnr(active_state, &type, seqnr))) |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 280 | sshpkt_fatal(active_state, __func__, r); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 281 | return type; |
| 282 | } |
| 283 | |
| 284 | void |
| 285 | packet_close(void) |
| 286 | { |
| 287 | ssh_packet_close(active_state); |
| 288 | active_state = NULL; |
| 289 | } |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 290 | |
| 291 | void |
| 292 | packet_process_incoming(const char *buf, u_int len) |
| 293 | { |
| 294 | int r; |
| 295 | |
| 296 | if ((r = ssh_packet_process_incoming(active_state, buf, len)) != 0) |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 297 | sshpkt_fatal(active_state, __func__, r); |
| 298 | } |
| 299 | |
| 300 | void |
| 301 | packet_write_wait(void) |
| 302 | { |
| 303 | int r; |
| 304 | |
| 305 | if ((r = ssh_packet_write_wait(active_state)) != 0) |
| 306 | sshpkt_fatal(active_state, __func__, r); |
| 307 | } |
| 308 | |
| 309 | void |
| 310 | packet_write_poll(void) |
| 311 | { |
| 312 | int r; |
| 313 | |
| 314 | if ((r = ssh_packet_write_poll(active_state)) != 0) |
| 315 | sshpkt_fatal(active_state, __func__, r); |
| 316 | } |
| 317 | |
| 318 | void |
| 319 | packet_read_expect(int expected_type) |
| 320 | { |
| 321 | int r; |
| 322 | |
| 323 | if ((r = ssh_packet_read_expect(active_state, expected_type)) != 0) |
| 324 | sshpkt_fatal(active_state, __func__, r); |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 325 | } |
Darren Tucker | dcc8997 | 2015-02-24 12:30:59 +1100 | [diff] [blame] | 326 | |
| 327 | void |
| 328 | packet_disconnect(const char *fmt, ...) |
| 329 | { |
| 330 | char buf[1024]; |
| 331 | va_list args; |
| 332 | |
| 333 | va_start(args, fmt); |
| 334 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 335 | va_end(args); |
| 336 | ssh_packet_disconnect(active_state, "%s", buf); |
| 337 | } |
| 338 | |
| 339 | void |
| 340 | packet_send_debug(const char *fmt, ...) |
| 341 | { |
| 342 | char buf[1024]; |
| 343 | va_list args; |
| 344 | |
| 345 | va_start(args, fmt); |
| 346 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 347 | va_end(args); |
| 348 | ssh_packet_send_debug(active_state, "%s", buf); |
| 349 | } |