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 | |
| 77 | #ifdef WITH_OPENSSL |
| 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 | } |
| 86 | |
| 87 | void |
| 88 | ssh_packet_put_bignum2(struct ssh *ssh, BIGNUM * value) |
| 89 | { |
| 90 | int r; |
| 91 | |
| 92 | if ((r = sshpkt_put_bignum2(ssh, value)) != 0) |
| 93 | fatal("%s: %s", __func__, ssh_err(r)); |
| 94 | } |
| 95 | |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 96 | # ifdef OPENSSL_HAS_ECC |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 97 | void |
| 98 | ssh_packet_put_ecpoint(struct ssh *ssh, const EC_GROUP *curve, |
| 99 | const EC_POINT *point) |
| 100 | { |
| 101 | int r; |
| 102 | |
| 103 | if ((r = sshpkt_put_ec(ssh, point, curve)) != 0) |
| 104 | fatal("%s: %s", __func__, ssh_err(r)); |
| 105 | } |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 106 | # endif |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 107 | #endif /* WITH_OPENSSL */ |
| 108 | |
| 109 | void |
| 110 | ssh_packet_send(struct ssh *ssh) |
| 111 | { |
| 112 | int r; |
| 113 | |
| 114 | if ((r = sshpkt_send(ssh)) != 0) |
| 115 | fatal("%s: %s", __func__, ssh_err(r)); |
| 116 | } |
| 117 | |
| 118 | u_int |
| 119 | ssh_packet_get_char(struct ssh *ssh) |
| 120 | { |
| 121 | u_char ch; |
| 122 | int r; |
| 123 | |
| 124 | if ((r = sshpkt_get_u8(ssh, &ch)) != 0) |
| 125 | fatal("%s: %s", __func__, ssh_err(r)); |
| 126 | return ch; |
| 127 | } |
| 128 | |
| 129 | u_int |
| 130 | ssh_packet_get_int(struct ssh *ssh) |
| 131 | { |
| 132 | u_int val; |
| 133 | int r; |
| 134 | |
| 135 | if ((r = sshpkt_get_u32(ssh, &val)) != 0) |
| 136 | fatal("%s: %s", __func__, ssh_err(r)); |
| 137 | return val; |
| 138 | } |
| 139 | |
| 140 | u_int64_t |
| 141 | ssh_packet_get_int64(struct ssh *ssh) |
| 142 | { |
| 143 | u_int64_t val; |
| 144 | int r; |
| 145 | |
| 146 | if ((r = sshpkt_get_u64(ssh, &val)) != 0) |
| 147 | fatal("%s: %s", __func__, ssh_err(r)); |
| 148 | return val; |
| 149 | } |
| 150 | |
| 151 | #ifdef WITH_OPENSSL |
| 152 | void |
| 153 | ssh_packet_get_bignum(struct ssh *ssh, BIGNUM * value) |
| 154 | { |
| 155 | int r; |
| 156 | |
| 157 | if ((r = sshpkt_get_bignum1(ssh, value)) != 0) |
| 158 | fatal("%s: %s", __func__, ssh_err(r)); |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | ssh_packet_get_bignum2(struct ssh *ssh, BIGNUM * value) |
| 163 | { |
| 164 | int r; |
| 165 | |
| 166 | if ((r = sshpkt_get_bignum2(ssh, value)) != 0) |
| 167 | fatal("%s: %s", __func__, ssh_err(r)); |
| 168 | } |
| 169 | |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 170 | # ifdef OPENSSL_HAS_ECC |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 171 | void |
| 172 | ssh_packet_get_ecpoint(struct ssh *ssh, const EC_GROUP *curve, EC_POINT *point) |
| 173 | { |
| 174 | int r; |
| 175 | |
| 176 | if ((r = sshpkt_get_ec(ssh, point, curve)) != 0) |
| 177 | fatal("%s: %s", __func__, ssh_err(r)); |
| 178 | } |
Darren Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 179 | # endif |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 180 | #endif /* WITH_OPENSSL */ |
| 181 | |
| 182 | void * |
| 183 | ssh_packet_get_string(struct ssh *ssh, u_int *length_ptr) |
| 184 | { |
| 185 | int r; |
| 186 | size_t len; |
| 187 | u_char *val; |
| 188 | |
| 189 | if ((r = sshpkt_get_string(ssh, &val, &len)) != 0) |
| 190 | fatal("%s: %s", __func__, ssh_err(r)); |
| 191 | if (length_ptr != NULL) |
| 192 | *length_ptr = (u_int)len; |
| 193 | return val; |
| 194 | } |
| 195 | |
| 196 | const void * |
| 197 | ssh_packet_get_string_ptr(struct ssh *ssh, u_int *length_ptr) |
| 198 | { |
| 199 | int r; |
| 200 | size_t len; |
| 201 | const u_char *val; |
| 202 | |
| 203 | if ((r = sshpkt_get_string_direct(ssh, &val, &len)) != 0) |
| 204 | fatal("%s: %s", __func__, ssh_err(r)); |
| 205 | if (length_ptr != NULL) |
| 206 | *length_ptr = (u_int)len; |
| 207 | return val; |
| 208 | } |
| 209 | |
| 210 | char * |
| 211 | ssh_packet_get_cstring(struct ssh *ssh, u_int *length_ptr) |
| 212 | { |
| 213 | int r; |
| 214 | size_t len; |
| 215 | char *val; |
| 216 | |
| 217 | if ((r = sshpkt_get_cstring(ssh, &val, &len)) != 0) |
| 218 | fatal("%s: %s", __func__, ssh_err(r)); |
| 219 | if (length_ptr != NULL) |
| 220 | *length_ptr = (u_int)len; |
| 221 | return val; |
| 222 | } |
| 223 | |
| 224 | /* Old API, that had to be reimplemented */ |
| 225 | |
| 226 | void |
| 227 | packet_set_connection(int fd_in, int fd_out) |
| 228 | { |
| 229 | 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] | 230 | if (active_state == NULL) |
| 231 | fatal("%s: ssh_packet_set_connection failed", __func__); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void |
| 235 | packet_backup_state(void) |
| 236 | { |
| 237 | ssh_packet_backup_state(active_state, backup_state); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | packet_restore_state(void) |
| 242 | { |
| 243 | ssh_packet_restore_state(active_state, backup_state); |
| 244 | } |
| 245 | |
| 246 | u_int |
| 247 | packet_get_char(void) |
| 248 | { |
| 249 | return (ssh_packet_get_char(active_state)); |
| 250 | } |
| 251 | |
| 252 | u_int |
| 253 | packet_get_int(void) |
| 254 | { |
| 255 | return (ssh_packet_get_int(active_state)); |
| 256 | } |
| 257 | |
| 258 | int |
| 259 | packet_read_seqnr(u_int32_t *seqnr) |
| 260 | { |
| 261 | u_char type; |
| 262 | int r; |
| 263 | |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 264 | if ((r = ssh_packet_read_seqnr(active_state, &type, seqnr)) != 0) |
| 265 | sshpkt_fatal(active_state, __func__, r); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 266 | return type; |
| 267 | } |
| 268 | |
| 269 | int |
| 270 | packet_read_poll_seqnr(u_int32_t *seqnr) |
| 271 | { |
| 272 | u_char type; |
| 273 | int r; |
| 274 | |
| 275 | if ((r = ssh_packet_read_poll_seqnr(active_state, &type, seqnr))) |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 276 | sshpkt_fatal(active_state, __func__, r); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 277 | return type; |
| 278 | } |
| 279 | |
| 280 | void |
| 281 | packet_close(void) |
| 282 | { |
| 283 | ssh_packet_close(active_state); |
| 284 | active_state = NULL; |
| 285 | } |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 286 | |
| 287 | void |
| 288 | packet_process_incoming(const char *buf, u_int len) |
| 289 | { |
| 290 | int r; |
| 291 | |
| 292 | if ((r = ssh_packet_process_incoming(active_state, buf, len)) != 0) |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 293 | sshpkt_fatal(active_state, __func__, r); |
| 294 | } |
| 295 | |
| 296 | void |
| 297 | packet_write_wait(void) |
| 298 | { |
| 299 | int r; |
| 300 | |
| 301 | if ((r = ssh_packet_write_wait(active_state)) != 0) |
| 302 | sshpkt_fatal(active_state, __func__, r); |
| 303 | } |
| 304 | |
| 305 | void |
| 306 | packet_write_poll(void) |
| 307 | { |
| 308 | int r; |
| 309 | |
| 310 | if ((r = ssh_packet_write_poll(active_state)) != 0) |
| 311 | sshpkt_fatal(active_state, __func__, r); |
| 312 | } |
| 313 | |
| 314 | void |
| 315 | packet_read_expect(int expected_type) |
| 316 | { |
| 317 | int r; |
| 318 | |
| 319 | if ((r = ssh_packet_read_expect(active_state, expected_type)) != 0) |
| 320 | sshpkt_fatal(active_state, __func__, r); |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 321 | } |
Darren Tucker | dcc8997 | 2015-02-24 12:30:59 +1100 | [diff] [blame^] | 322 | |
| 323 | void |
| 324 | packet_disconnect(const char *fmt, ...) |
| 325 | { |
| 326 | char buf[1024]; |
| 327 | va_list args; |
| 328 | |
| 329 | va_start(args, fmt); |
| 330 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 331 | va_end(args); |
| 332 | ssh_packet_disconnect(active_state, "%s", buf); |
| 333 | } |
| 334 | |
| 335 | void |
| 336 | packet_send_debug(const char *fmt, ...) |
| 337 | { |
| 338 | char buf[1024]; |
| 339 | va_list args; |
| 340 | |
| 341 | va_start(args, fmt); |
| 342 | vsnprintf(buf, sizeof(buf), fmt, args); |
| 343 | va_end(args); |
| 344 | ssh_packet_send_debug(active_state, "%s", buf); |
| 345 | } |