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