djm@openbsd.org | b011edb | 2017-10-20 01:56:39 +0000 | [diff] [blame] | 1 | /* $OpenBSD: opacket.c,v 1.7 2017/10/20 01:56:39 djm Exp $ */ |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 2 | /* 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 | |
| 10 | struct ssh *active_state, *backup_state; |
| 11 | |
| 12 | /* Map old to new API */ |
| 13 | |
| 14 | void |
| 15 | ssh_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 | |
| 23 | void |
| 24 | ssh_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 | |
| 33 | void |
| 34 | ssh_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 | |
| 42 | void |
| 43 | ssh_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 | |
| 51 | void |
| 52 | ssh_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 | |
| 60 | void |
| 61 | ssh_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 | |
| 69 | void |
| 70 | ssh_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.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 78 | |
Damien Miller | bbffb23 | 2015-03-03 13:50:27 -0800 | [diff] [blame] | 79 | #ifdef WITH_OPENSSL |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 80 | void |
| 81 | ssh_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 Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 89 | # ifdef OPENSSL_HAS_ECC |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 90 | void |
| 91 | ssh_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 Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 99 | # endif |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 100 | #endif /* WITH_OPENSSL */ |
| 101 | |
| 102 | void |
| 103 | ssh_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 | |
| 111 | u_int |
| 112 | ssh_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 | |
| 122 | u_int |
| 123 | ssh_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 | |
| 133 | u_int64_t |
| 134 | ssh_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.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 144 | |
Damien Miller | bbffb23 | 2015-03-03 13:50:27 -0800 | [diff] [blame] | 145 | #ifdef WITH_OPENSSL |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 146 | void |
| 147 | ssh_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 Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 155 | # ifdef OPENSSL_HAS_ECC |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 156 | void |
| 157 | ssh_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 Tucker | f2004cd | 2015-02-23 05:04:21 +1100 | [diff] [blame] | 164 | # endif |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 165 | #endif /* WITH_OPENSSL */ |
| 166 | |
| 167 | void * |
| 168 | ssh_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 | |
| 181 | const void * |
| 182 | ssh_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 | |
| 195 | char * |
| 196 | ssh_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 | |
| 211 | void |
| 212 | packet_set_connection(int fd_in, int fd_out) |
| 213 | { |
| 214 | 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] | 215 | if (active_state == NULL) |
| 216 | fatal("%s: ssh_packet_set_connection failed", __func__); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 217 | } |
| 218 | |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 219 | u_int |
| 220 | packet_get_char(void) |
| 221 | { |
| 222 | return (ssh_packet_get_char(active_state)); |
| 223 | } |
| 224 | |
| 225 | u_int |
| 226 | packet_get_int(void) |
| 227 | { |
| 228 | return (ssh_packet_get_int(active_state)); |
| 229 | } |
| 230 | |
| 231 | int |
| 232 | packet_read_seqnr(u_int32_t *seqnr) |
| 233 | { |
| 234 | u_char type; |
| 235 | int r; |
| 236 | |
djm@openbsd.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 237 | if ((r = ssh_packet_read_seqnr(active_state, &type, seqnr)) != 0) |
| 238 | sshpkt_fatal(active_state, __func__, r); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 239 | return type; |
| 240 | } |
| 241 | |
| 242 | int |
| 243 | packet_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.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 249 | sshpkt_fatal(active_state, __func__, r); |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 250 | return type; |
| 251 | } |
| 252 | |
| 253 | void |
| 254 | packet_close(void) |
| 255 | { |
| 256 | ssh_packet_close(active_state); |
| 257 | active_state = NULL; |
| 258 | } |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 259 | |
| 260 | void |
| 261 | packet_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.org | 4509b5d | 2015-01-30 01:13:33 +0000 | [diff] [blame] | 266 | sshpkt_fatal(active_state, __func__, r); |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | packet_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 | |
| 278 | void |
| 279 | packet_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 | |
| 287 | void |
| 288 | packet_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.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame] | 294 | } |
Darren Tucker | dcc8997 | 2015-02-24 12:30:59 +1100 | [diff] [blame] | 295 | |
| 296 | void |
| 297 | packet_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 | |
| 308 | void |
| 309 | packet_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 | } |