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 | |
| 96 | void |
| 97 | ssh_packet_put_ecpoint(struct ssh *ssh, const EC_GROUP *curve, |
| 98 | const EC_POINT *point) |
| 99 | { |
| 100 | int r; |
| 101 | |
| 102 | if ((r = sshpkt_put_ec(ssh, point, curve)) != 0) |
| 103 | fatal("%s: %s", __func__, ssh_err(r)); |
| 104 | } |
| 105 | #endif /* WITH_OPENSSL */ |
| 106 | |
| 107 | void |
| 108 | ssh_packet_send(struct ssh *ssh) |
| 109 | { |
| 110 | int r; |
| 111 | |
| 112 | if ((r = sshpkt_send(ssh)) != 0) |
| 113 | fatal("%s: %s", __func__, ssh_err(r)); |
| 114 | } |
| 115 | |
| 116 | u_int |
| 117 | ssh_packet_get_char(struct ssh *ssh) |
| 118 | { |
| 119 | u_char ch; |
| 120 | int r; |
| 121 | |
| 122 | if ((r = sshpkt_get_u8(ssh, &ch)) != 0) |
| 123 | fatal("%s: %s", __func__, ssh_err(r)); |
| 124 | return ch; |
| 125 | } |
| 126 | |
| 127 | u_int |
| 128 | ssh_packet_get_int(struct ssh *ssh) |
| 129 | { |
| 130 | u_int val; |
| 131 | int r; |
| 132 | |
| 133 | if ((r = sshpkt_get_u32(ssh, &val)) != 0) |
| 134 | fatal("%s: %s", __func__, ssh_err(r)); |
| 135 | return val; |
| 136 | } |
| 137 | |
| 138 | u_int64_t |
| 139 | ssh_packet_get_int64(struct ssh *ssh) |
| 140 | { |
| 141 | u_int64_t val; |
| 142 | int r; |
| 143 | |
| 144 | if ((r = sshpkt_get_u64(ssh, &val)) != 0) |
| 145 | fatal("%s: %s", __func__, ssh_err(r)); |
| 146 | return val; |
| 147 | } |
| 148 | |
| 149 | #ifdef WITH_OPENSSL |
| 150 | void |
| 151 | ssh_packet_get_bignum(struct ssh *ssh, BIGNUM * value) |
| 152 | { |
| 153 | int r; |
| 154 | |
| 155 | if ((r = sshpkt_get_bignum1(ssh, value)) != 0) |
| 156 | fatal("%s: %s", __func__, ssh_err(r)); |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | ssh_packet_get_bignum2(struct ssh *ssh, BIGNUM * value) |
| 161 | { |
| 162 | int r; |
| 163 | |
| 164 | if ((r = sshpkt_get_bignum2(ssh, value)) != 0) |
| 165 | fatal("%s: %s", __func__, ssh_err(r)); |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | ssh_packet_get_ecpoint(struct ssh *ssh, const EC_GROUP *curve, EC_POINT *point) |
| 170 | { |
| 171 | int r; |
| 172 | |
| 173 | if ((r = sshpkt_get_ec(ssh, point, curve)) != 0) |
| 174 | fatal("%s: %s", __func__, ssh_err(r)); |
| 175 | } |
| 176 | #endif /* WITH_OPENSSL */ |
| 177 | |
| 178 | void * |
| 179 | ssh_packet_get_string(struct ssh *ssh, u_int *length_ptr) |
| 180 | { |
| 181 | int r; |
| 182 | size_t len; |
| 183 | u_char *val; |
| 184 | |
| 185 | if ((r = sshpkt_get_string(ssh, &val, &len)) != 0) |
| 186 | fatal("%s: %s", __func__, ssh_err(r)); |
| 187 | if (length_ptr != NULL) |
| 188 | *length_ptr = (u_int)len; |
| 189 | return val; |
| 190 | } |
| 191 | |
| 192 | const void * |
| 193 | ssh_packet_get_string_ptr(struct ssh *ssh, u_int *length_ptr) |
| 194 | { |
| 195 | int r; |
| 196 | size_t len; |
| 197 | const u_char *val; |
| 198 | |
| 199 | if ((r = sshpkt_get_string_direct(ssh, &val, &len)) != 0) |
| 200 | fatal("%s: %s", __func__, ssh_err(r)); |
| 201 | if (length_ptr != NULL) |
| 202 | *length_ptr = (u_int)len; |
| 203 | return val; |
| 204 | } |
| 205 | |
| 206 | char * |
| 207 | ssh_packet_get_cstring(struct ssh *ssh, u_int *length_ptr) |
| 208 | { |
| 209 | int r; |
| 210 | size_t len; |
| 211 | char *val; |
| 212 | |
| 213 | if ((r = sshpkt_get_cstring(ssh, &val, &len)) != 0) |
| 214 | fatal("%s: %s", __func__, ssh_err(r)); |
| 215 | if (length_ptr != NULL) |
| 216 | *length_ptr = (u_int)len; |
| 217 | return val; |
| 218 | } |
| 219 | |
| 220 | /* Old API, that had to be reimplemented */ |
| 221 | |
| 222 | void |
| 223 | packet_set_connection(int fd_in, int fd_out) |
| 224 | { |
| 225 | active_state = ssh_packet_set_connection(active_state, fd_in, fd_out); |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | packet_backup_state(void) |
| 230 | { |
| 231 | ssh_packet_backup_state(active_state, backup_state); |
| 232 | } |
| 233 | |
| 234 | void |
| 235 | packet_restore_state(void) |
| 236 | { |
| 237 | ssh_packet_restore_state(active_state, backup_state); |
| 238 | } |
| 239 | |
| 240 | u_int |
| 241 | packet_get_char(void) |
| 242 | { |
| 243 | return (ssh_packet_get_char(active_state)); |
| 244 | } |
| 245 | |
| 246 | u_int |
| 247 | packet_get_int(void) |
| 248 | { |
| 249 | return (ssh_packet_get_int(active_state)); |
| 250 | } |
| 251 | |
| 252 | int |
| 253 | packet_read_seqnr(u_int32_t *seqnr) |
| 254 | { |
| 255 | u_char type; |
| 256 | int r; |
| 257 | |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame^] | 258 | if ((r = ssh_packet_read_seqnr(active_state, &type, seqnr)) != 0) { |
| 259 | switch (r) { |
| 260 | case SSH_ERR_CONN_CLOSED: |
| 261 | logit("Connection closed by %.200s", |
| 262 | ssh_remote_ipaddr(active_state)); |
| 263 | cleanup_exit(255); |
| 264 | case SSH_ERR_CONN_TIMEOUT: |
| 265 | logit("Connection to %.200s timed out while " |
| 266 | "waiting to read", ssh_remote_ipaddr(active_state)); |
| 267 | cleanup_exit(255); |
| 268 | default: |
| 269 | fatal("%s: %s", __func__, ssh_err(r)); |
| 270 | } |
| 271 | } |
markus@openbsd.org | 091c302 | 2015-01-19 19:52:16 +0000 | [diff] [blame] | 272 | return type; |
| 273 | } |
| 274 | |
| 275 | int |
| 276 | packet_read_poll_seqnr(u_int32_t *seqnr) |
| 277 | { |
| 278 | u_char type; |
| 279 | int r; |
| 280 | |
| 281 | if ((r = ssh_packet_read_poll_seqnr(active_state, &type, seqnr))) |
| 282 | fatal("%s: %s", __func__, ssh_err(r)); |
| 283 | return type; |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | packet_close(void) |
| 288 | { |
| 289 | ssh_packet_close(active_state); |
| 290 | active_state = NULL; |
| 291 | } |
djm@openbsd.org | fae7bbe | 2015-01-28 21:15:47 +0000 | [diff] [blame^] | 292 | |
| 293 | void |
| 294 | packet_process_incoming(const char *buf, u_int len) |
| 295 | { |
| 296 | int r; |
| 297 | |
| 298 | if ((r = ssh_packet_process_incoming(active_state, buf, len)) != 0) |
| 299 | fatal("%s: %s", __func__, ssh_err(r)); |
| 300 | } |