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