[BACK]Return to packet.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/packet.c, Revision 1.227

1.227   ! djm         1: /* $OpenBSD: packet.c,v 1.226 2016/01/29 05:46:01 djm Exp $ */
1.1       deraadt     2: /*
1.15      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
1.35      deraadt     6:  * This file contains code implementing the packet protocol and communication
                      7:  * with the other side.  This same code is used both on client and server side.
1.29      markus      8:  *
1.35      deraadt     9:  * As far as I am concerned, the code I have written for this software
                     10:  * can be used freely for any purpose.  Any derived versions of this
                     11:  * software must be clearly marked as such, and if the derived work is
                     12:  * incompatible with the protocol description in the RFC file, it must be
                     13:  * called by a name other than "ssh" or "Secure Shell".
1.29      markus     14:  *
1.25      markus     15:  *
                     16:  * SSH2 packet format added by Markus Friedl.
1.69      markus     17:  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
1.25      markus     18:  *
1.35      deraadt    19:  * Redistribution and use in source and binary forms, with or without
                     20:  * modification, are permitted provided that the following conditions
                     21:  * are met:
                     22:  * 1. Redistributions of source code must retain the above copyright
                     23:  *    notice, this list of conditions and the following disclaimer.
                     24:  * 2. Redistributions in binary form must reproduce the above copyright
                     25:  *    notice, this list of conditions and the following disclaimer in the
                     26:  *    documentation and/or other materials provided with the distribution.
                     27:  *
                     28:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     29:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     30:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     31:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     32:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     33:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     34:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     35:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     36:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     37:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.15      deraadt    38:  */
1.1       deraadt    39:
1.203     deraadt    40: #include <sys/param.h> /* MIN roundup */
1.142     deraadt    41: #include <sys/types.h>
1.105     markus     42: #include <sys/queue.h>
1.132     stevesk    43: #include <sys/socket.h>
1.138     stevesk    44: #include <sys/time.h>
1.132     stevesk    45: #include <netinet/in.h>
1.121     stevesk    46: #include <netinet/ip.h>
1.134     stevesk    47:
1.135     stevesk    48: #include <errno.h>
1.134     stevesk    49: #include <stdarg.h>
1.141     stevesk    50: #include <stdio.h>
1.140     stevesk    51: #include <stdlib.h>
1.137     stevesk    52: #include <string.h>
1.136     stevesk    53: #include <unistd.h>
1.203     deraadt    54: #include <limits.h>
1.142     deraadt    55: #include <signal.h>
1.184     dtucker    56: #include <time.h>
1.1       deraadt    57:
1.201     markus     58: #include <zlib.h>
                     59:
                     60: #include "buffer.h"    /* typedefs XXX */
                     61: #include "key.h"       /* typedefs XXX */
                     62:
1.1       deraadt    63: #include "xmalloc.h"
                     64: #include "crc32.h"
1.9       dugsong    65: #include "deattack.h"
1.25      markus     66: #include "compat.h"
1.45      markus     67: #include "ssh1.h"
1.25      markus     68: #include "ssh2.h"
1.37      markus     69: #include "cipher.h"
1.201     markus     70: #include "sshkey.h"
1.25      markus     71: #include "kex.h"
1.200     markus     72: #include "digest.h"
1.50      markus     73: #include "mac.h"
1.46      markus     74: #include "log.h"
                     75: #include "canohost.h"
1.87      stevesk    76: #include "misc.h"
1.198     millert    77: #include "channels.h"
1.95      markus     78: #include "ssh.h"
1.201     markus     79: #include "packet.h"
1.197     djm        80: #include "ssherr.h"
1.201     markus     81: #include "sshbuf.h"
1.25      markus     82:
                     83: #ifdef PACKET_DEBUG
                     84: #define DBG(x) x
                     85: #else
                     86: #define DBG(x)
                     87: #endif
                     88:
1.159     markus     89: #define PACKET_MAX_SIZE (256 * 1024)
                     90:
1.161     andreas    91: struct packet_state {
                     92:        u_int32_t seqnr;
                     93:        u_int32_t packets;
                     94:        u_int64_t blocks;
                     95:        u_int64_t bytes;
                     96: };
                     97:
                     98: struct packet {
                     99:        TAILQ_ENTRY(packet) next;
                    100:        u_char type;
1.201     markus    101:        struct sshbuf *payload;
1.161     andreas   102: };
                    103:
                    104: struct session_state {
                    105:        /*
                    106:         * This variable contains the file descriptors used for
                    107:         * communicating with the other side.  connection_in is used for
                    108:         * reading; connection_out for writing.  These can be the same
                    109:         * descriptor, in which case it is assumed to be a socket.
                    110:         */
                    111:        int connection_in;
                    112:        int connection_out;
                    113:
                    114:        /* Protocol flags for the remote side. */
                    115:        u_int remote_protocol_flags;
1.1       deraadt   116:
1.161     andreas   117:        /* Encryption context for receiving data.  Only used for decryption. */
1.201     markus    118:        struct sshcipher_ctx receive_context;
1.1       deraadt   119:
1.161     andreas   120:        /* Encryption context for sending data.  Only used for encryption. */
1.201     markus    121:        struct sshcipher_ctx send_context;
1.14      markus    122:
1.161     andreas   123:        /* Buffer for raw input data from the socket. */
1.201     markus    124:        struct sshbuf *input;
1.1       deraadt   125:
1.161     andreas   126:        /* Buffer for raw output data going to the socket. */
1.201     markus    127:        struct sshbuf *output;
1.1       deraadt   128:
1.161     andreas   129:        /* Buffer for the partial outgoing packet being constructed. */
1.201     markus    130:        struct sshbuf *outgoing_packet;
1.1       deraadt   131:
1.161     andreas   132:        /* Buffer for the incoming packet currently being processed. */
1.201     markus    133:        struct sshbuf *incoming_packet;
1.1       deraadt   134:
1.161     andreas   135:        /* Scratch buffer for packet compression/decompression. */
1.201     markus    136:        struct sshbuf *compression_buffer;
                    137:
                    138:        /* Incoming/outgoing compression dictionaries */
                    139:        z_stream compression_in_stream;
                    140:        z_stream compression_out_stream;
                    141:        int compression_in_started;
                    142:        int compression_out_started;
                    143:        int compression_in_failures;
                    144:        int compression_out_failures;
1.1       deraadt   145:
1.161     andreas   146:        /*
                    147:         * Flag indicating whether packet compression/decompression is
                    148:         * enabled.
                    149:         */
                    150:        int packet_compression;
1.1       deraadt   151:
1.161     andreas   152:        /* default maximum packet size */
                    153:        u_int max_packet_size;
1.1       deraadt   154:
1.161     andreas   155:        /* Flag indicating whether this module has been initialized. */
                    156:        int initialized;
1.12      markus    157:
1.161     andreas   158:        /* Set to true if the connection is interactive. */
                    159:        int interactive_mode;
1.1       deraadt   160:
1.161     andreas   161:        /* Set to true if we are the server side. */
                    162:        int server_side;
1.1       deraadt   163:
1.161     andreas   164:        /* Set to true if we are authenticated. */
                    165:        int after_authentication;
1.118     markus    166:
1.161     andreas   167:        int keep_alive_timeouts;
1.118     markus    168:
1.161     andreas   169:        /* The maximum time that we will wait to send or receive a packet */
                    170:        int packet_timeout_ms;
1.151     dtucker   171:
1.161     andreas   172:        /* Session key information for Encryption and MAC */
1.201     markus    173:        struct newkeys *newkeys[MODE_MAX];
1.161     andreas   174:        struct packet_state p_read, p_send;
1.154     dtucker   175:
1.184     dtucker   176:        /* Volume-based rekeying */
1.224     dtucker   177:        u_int64_t max_blocks_in, max_blocks_out, rekey_limit;
1.105     markus    178:
1.184     dtucker   179:        /* Time-based rekeying */
1.208     markus    180:        u_int32_t rekey_interval;       /* how often in seconds */
1.184     dtucker   181:        time_t rekey_time;      /* time of last rekeying */
                    182:
1.161     andreas   183:        /* Session key for protocol v1 */
                    184:        u_char ssh1_key[SSH_SESSION_KEY_LENGTH];
                    185:        u_int ssh1_keylen;
1.25      markus    186:
1.161     andreas   187:        /* roundup current message to extra_pad bytes */
                    188:        u_char extra_pad;
1.95      markus    189:
1.161     andreas   190:        /* XXX discard incoming data after MAC error */
                    191:        u_int packet_discard;
1.201     markus    192:        struct sshmac *packet_discard_mac;
1.71      markus    193:
1.161     andreas   194:        /* Used in packet_read_poll2() */
                    195:        u_int packlen;
1.159     markus    196:
1.165     andreas   197:        /* Used in packet_send2 */
                    198:        int rekeying;
                    199:
                    200:        /* Used in packet_set_interactive */
                    201:        int set_interactive_called;
                    202:
                    203:        /* Used in packet_set_maxsize */
                    204:        int set_maxsize_called;
                    205:
1.201     markus    206:        /* One-off warning about weak ciphers */
                    207:        int cipher_warning_done;
                    208:
                    209:        /* SSH1 CRC compensation attack detector */
                    210:        struct deattack_ctx deattack;
                    211:
1.161     andreas   212:        TAILQ_HEAD(, packet) outgoing;
1.105     markus    213: };
1.161     andreas   214:
1.201     markus    215: struct ssh *
                    216: ssh_alloc_session_state(void)
1.161     andreas   217: {
1.201     markus    218:        struct ssh *ssh = NULL;
                    219:        struct session_state *state = NULL;
1.161     andreas   220:
1.201     markus    221:        if ((ssh = calloc(1, sizeof(*ssh))) == NULL ||
                    222:            (state = calloc(1, sizeof(*state))) == NULL ||
                    223:            (state->input = sshbuf_new()) == NULL ||
                    224:            (state->output = sshbuf_new()) == NULL ||
                    225:            (state->outgoing_packet = sshbuf_new()) == NULL ||
                    226:            (state->incoming_packet = sshbuf_new()) == NULL)
                    227:                goto fail;
                    228:        TAILQ_INIT(&state->outgoing);
1.202     markus    229:        TAILQ_INIT(&ssh->private_keys);
                    230:        TAILQ_INIT(&ssh->public_keys);
1.201     markus    231:        state->connection_in = -1;
                    232:        state->connection_out = -1;
                    233:        state->max_packet_size = 32768;
                    234:        state->packet_timeout_ms = -1;
                    235:        state->p_send.packets = state->p_read.packets = 0;
                    236:        state->initialized = 1;
                    237:        /*
                    238:         * ssh_packet_send2() needs to queue packets until
                    239:         * we've done the initial key exchange.
                    240:         */
                    241:        state->rekeying = 1;
                    242:        ssh->state = state;
                    243:        return ssh;
                    244:  fail:
                    245:        if (state) {
                    246:                sshbuf_free(state->input);
                    247:                sshbuf_free(state->output);
                    248:                sshbuf_free(state->incoming_packet);
                    249:                sshbuf_free(state->outgoing_packet);
                    250:                free(state);
                    251:        }
                    252:        free(ssh);
                    253:        return NULL;
1.161     andreas   254: }
1.105     markus    255:
1.16      markus    256: /*
                    257:  * Sets the descriptors used for communication.  Disables encryption until
                    258:  * packet_set_encryption_key is called.
                    259:  */
1.201     markus    260: struct ssh *
                    261: ssh_packet_set_connection(struct ssh *ssh, int fd_in, int fd_out)
1.1       deraadt   262: {
1.201     markus    263:        struct session_state *state;
                    264:        const struct sshcipher *none = cipher_by_name("none");
1.197     djm       265:        int r;
1.97      deraadt   266:
1.205     djm       267:        if (none == NULL) {
                    268:                error("%s: cannot load cipher 'none'", __func__);
                    269:                return NULL;
                    270:        }
1.201     markus    271:        if (ssh == NULL)
                    272:                ssh = ssh_alloc_session_state();
1.205     djm       273:        if (ssh == NULL) {
                    274:                error("%s: cound not allocate state", __func__);
                    275:                return NULL;
                    276:        }
1.201     markus    277:        state = ssh->state;
                    278:        state->connection_in = fd_in;
                    279:        state->connection_out = fd_out;
                    280:        if ((r = cipher_init(&state->send_context, none,
1.197     djm       281:            (const u_char *)"", 0, NULL, 0, CIPHER_ENCRYPT)) != 0 ||
1.201     markus    282:            (r = cipher_init(&state->receive_context, none,
1.205     djm       283:            (const u_char *)"", 0, NULL, 0, CIPHER_DECRYPT)) != 0) {
                    284:                error("%s: cipher_init failed: %s", __func__, ssh_err(r));
1.209     jsg       285:                free(ssh);
1.205     djm       286:                return NULL;
                    287:        }
1.201     markus    288:        state->newkeys[MODE_IN] = state->newkeys[MODE_OUT] = NULL;
                    289:        deattack_init(&state->deattack);
1.207     djm       290:        /*
                    291:         * Cache the IP address of the remote connection for use in error
                    292:         * messages that might be generated after the connection has closed.
                    293:         */
                    294:        (void)ssh_remote_ipaddr(ssh);
1.201     markus    295:        return ssh;
1.1       deraadt   296: }
                    297:
1.154     dtucker   298: void
1.201     markus    299: ssh_packet_set_timeout(struct ssh *ssh, int timeout, int count)
1.154     dtucker   300: {
1.201     markus    301:        struct session_state *state = ssh->state;
                    302:
1.174     djm       303:        if (timeout <= 0 || count <= 0) {
1.201     markus    304:                state->packet_timeout_ms = -1;
1.154     dtucker   305:                return;
                    306:        }
                    307:        if ((INT_MAX / 1000) / count < timeout)
1.201     markus    308:                state->packet_timeout_ms = INT_MAX;
1.154     dtucker   309:        else
1.201     markus    310:                state->packet_timeout_ms = timeout * count * 1000;
1.154     dtucker   311: }
                    312:
1.201     markus    313: int
                    314: ssh_packet_stop_discard(struct ssh *ssh)
1.159     markus    315: {
1.201     markus    316:        struct session_state *state = ssh->state;
                    317:        int r;
                    318:
                    319:        if (state->packet_discard_mac) {
1.159     markus    320:                char buf[1024];
1.201     markus    321:
1.159     markus    322:                memset(buf, 'a', sizeof(buf));
1.201     markus    323:                while (sshbuf_len(state->incoming_packet) <
1.161     andreas   324:                    PACKET_MAX_SIZE)
1.201     markus    325:                        if ((r = sshbuf_put(state->incoming_packet, buf,
                    326:                            sizeof(buf))) != 0)
                    327:                                return r;
                    328:                (void) mac_compute(state->packet_discard_mac,
                    329:                    state->p_read.seqnr,
                    330:                    sshbuf_ptr(state->incoming_packet), PACKET_MAX_SIZE,
                    331:                    NULL, 0);
1.159     markus    332:        }
1.220     djm       333:        logit("Finished discarding for %.200s port %d",
                    334:            ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.201     markus    335:        return SSH_ERR_MAC_INVALID;
1.159     markus    336: }
                    337:
1.201     markus    338: static int
                    339: ssh_packet_start_discard(struct ssh *ssh, struct sshenc *enc,
                    340:     struct sshmac *mac, u_int packet_length, u_int discard)
1.159     markus    341: {
1.201     markus    342:        struct session_state *state = ssh->state;
                    343:        int r;
                    344:
                    345:        if (enc == NULL || !cipher_is_cbc(enc->cipher) || (mac && mac->etm)) {
                    346:                if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
                    347:                        return r;
                    348:                return SSH_ERR_MAC_INVALID;
                    349:        }
1.159     markus    350:        if (packet_length != PACKET_MAX_SIZE && mac && mac->enabled)
1.201     markus    351:                state->packet_discard_mac = mac;
                    352:        if (sshbuf_len(state->input) >= discard &&
                    353:           (r = ssh_packet_stop_discard(ssh)) != 0)
                    354:                return r;
                    355:        state->packet_discard = discard - sshbuf_len(state->input);
                    356:        return 0;
1.159     markus    357: }
                    358:
1.19      markus    359: /* Returns 1 if remote host is connected via socket, 0 if not. */
                    360:
                    361: int
1.201     markus    362: ssh_packet_connection_is_on_socket(struct ssh *ssh)
1.19      markus    363: {
1.201     markus    364:        struct session_state *state = ssh->state;
1.19      markus    365:        struct sockaddr_storage from, to;
                    366:        socklen_t fromlen, tolen;
                    367:
                    368:        /* filedescriptors in and out are the same, so it's a socket */
1.201     markus    369:        if (state->connection_in == state->connection_out)
1.19      markus    370:                return 1;
                    371:        fromlen = sizeof(from);
                    372:        memset(&from, 0, sizeof(from));
1.201     markus    373:        if (getpeername(state->connection_in, (struct sockaddr *)&from,
1.161     andreas   374:            &fromlen) < 0)
1.19      markus    375:                return 0;
                    376:        tolen = sizeof(to);
                    377:        memset(&to, 0, sizeof(to));
1.201     markus    378:        if (getpeername(state->connection_out, (struct sockaddr *)&to,
1.161     andreas   379:            &tolen) < 0)
1.19      markus    380:                return 0;
                    381:        if (fromlen != tolen || memcmp(&from, &to, fromlen) != 0)
                    382:                return 0;
                    383:        if (from.ss_family != AF_INET && from.ss_family != AF_INET6)
                    384:                return 0;
                    385:        return 1;
                    386: }
                    387:
1.91      markus    388: void
1.201     markus    389: ssh_packet_get_bytes(struct ssh *ssh, u_int64_t *ibytes, u_int64_t *obytes)
1.91      markus    390: {
1.201     markus    391:        if (ibytes)
                    392:                *ibytes = ssh->state->p_read.bytes;
                    393:        if (obytes)
                    394:                *obytes = ssh->state->p_send.bytes;
1.91      markus    395: }
1.125     deraadt   396:
1.91      markus    397: int
1.201     markus    398: ssh_packet_connection_af(struct ssh *ssh)
1.19      markus    399: {
                    400:        struct sockaddr_storage to;
1.21      deraadt   401:        socklen_t tolen = sizeof(to);
1.19      markus    402:
                    403:        memset(&to, 0, sizeof(to));
1.201     markus    404:        if (getsockname(ssh->state->connection_out, (struct sockaddr *)&to,
1.161     andreas   405:            &tolen) < 0)
1.19      markus    406:                return 0;
1.173     djm       407:        return to.ss_family;
1.19      markus    408: }
                    409:
1.1       deraadt   410: /* Sets the connection into non-blocking mode. */
                    411:
1.2       provos    412: void
1.201     markus    413: ssh_packet_set_nonblocking(struct ssh *ssh)
1.1       deraadt   414: {
1.14      markus    415:        /* Set the socket into non-blocking mode. */
1.201     markus    416:        set_nonblock(ssh->state->connection_in);
1.14      markus    417:
1.201     markus    418:        if (ssh->state->connection_out != ssh->state->connection_in)
                    419:                set_nonblock(ssh->state->connection_out);
1.1       deraadt   420: }
                    421:
                    422: /* Returns the socket used for reading. */
                    423:
1.2       provos    424: int
1.201     markus    425: ssh_packet_get_connection_in(struct ssh *ssh)
1.1       deraadt   426: {
1.201     markus    427:        return ssh->state->connection_in;
1.1       deraadt   428: }
                    429:
                    430: /* Returns the descriptor used for writing. */
                    431:
1.2       provos    432: int
1.201     markus    433: ssh_packet_get_connection_out(struct ssh *ssh)
1.1       deraadt   434: {
1.201     markus    435:        return ssh->state->connection_out;
                    436: }
                    437:
                    438: /*
                    439:  * Returns the IP-address of the remote host as a string.  The returned
                    440:  * string must not be freed.
                    441:  */
                    442:
                    443: const char *
                    444: ssh_remote_ipaddr(struct ssh *ssh)
                    445: {
1.220     djm       446:        const int sock = ssh->state->connection_in;
                    447:
1.201     markus    448:        /* Check whether we have cached the ipaddr. */
1.220     djm       449:        if (ssh->remote_ipaddr == NULL) {
                    450:                if (ssh_packet_connection_is_on_socket(ssh)) {
                    451:                        ssh->remote_ipaddr = get_peer_ipaddr(sock);
                    452:                        ssh->remote_port = get_sock_port(sock, 0);
                    453:                } else {
                    454:                        ssh->remote_ipaddr = strdup("UNKNOWN");
                    455:                        ssh->remote_port = 0;
                    456:                }
                    457:        }
1.201     markus    458:        return ssh->remote_ipaddr;
1.1       deraadt   459: }
                    460:
1.220     djm       461: /* Returns the port number of the remote host. */
                    462:
                    463: int
                    464: ssh_remote_port(struct ssh *ssh)
                    465: {
                    466:        (void)ssh_remote_ipaddr(ssh); /* Will lookup and cache. */
                    467:        return ssh->remote_port;
                    468: }
                    469:
1.1       deraadt   470: /* Closes the connection and clears and frees internal data structures. */
                    471:
1.2       provos    472: void
1.201     markus    473: ssh_packet_close(struct ssh *ssh)
1.1       deraadt   474: {
1.201     markus    475:        struct session_state *state = ssh->state;
                    476:        int r;
                    477:        u_int mode;
                    478:
                    479:        if (!state->initialized)
1.14      markus    480:                return;
1.201     markus    481:        state->initialized = 0;
                    482:        if (state->connection_in == state->connection_out) {
                    483:                shutdown(state->connection_out, SHUT_RDWR);
                    484:                close(state->connection_out);
1.14      markus    485:        } else {
1.201     markus    486:                close(state->connection_in);
                    487:                close(state->connection_out);
1.14      markus    488:        }
1.201     markus    489:        sshbuf_free(state->input);
                    490:        sshbuf_free(state->output);
                    491:        sshbuf_free(state->outgoing_packet);
                    492:        sshbuf_free(state->incoming_packet);
                    493:        for (mode = 0; mode < MODE_MAX; mode++)
                    494:                kex_free_newkeys(state->newkeys[mode]);
                    495:        if (state->compression_buffer) {
                    496:                sshbuf_free(state->compression_buffer);
                    497:                if (state->compression_out_started) {
                    498:                        z_streamp stream = &state->compression_out_stream;
                    499:                        debug("compress outgoing: "
                    500:                            "raw data %llu, compressed %llu, factor %.2f",
                    501:                                (unsigned long long)stream->total_in,
                    502:                                (unsigned long long)stream->total_out,
                    503:                                stream->total_in == 0 ? 0.0 :
                    504:                                (double) stream->total_out / stream->total_in);
                    505:                        if (state->compression_out_failures == 0)
                    506:                                deflateEnd(stream);
                    507:                }
                    508:                if (state->compression_in_started) {
                    509:                        z_streamp stream = &state->compression_out_stream;
                    510:                        debug("compress incoming: "
                    511:                            "raw data %llu, compressed %llu, factor %.2f",
                    512:                            (unsigned long long)stream->total_out,
                    513:                            (unsigned long long)stream->total_in,
                    514:                            stream->total_out == 0 ? 0.0 :
                    515:                            (double) stream->total_in / stream->total_out);
                    516:                        if (state->compression_in_failures == 0)
                    517:                                inflateEnd(stream);
                    518:                }
1.14      markus    519:        }
1.201     markus    520:        if ((r = cipher_cleanup(&state->send_context)) != 0)
                    521:                error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
                    522:        if ((r = cipher_cleanup(&state->receive_context)) != 0)
                    523:                error("%s: cipher_cleanup failed: %s", __func__, ssh_err(r));
1.219     mmcc      524:        free(ssh->remote_ipaddr);
                    525:        ssh->remote_ipaddr = NULL;
1.201     markus    526:        free(ssh->state);
                    527:        ssh->state = NULL;
1.1       deraadt   528: }
                    529:
                    530: /* Sets remote side protocol flags. */
                    531:
1.2       provos    532: void
1.201     markus    533: ssh_packet_set_protocol_flags(struct ssh *ssh, u_int protocol_flags)
1.1       deraadt   534: {
1.201     markus    535:        ssh->state->remote_protocol_flags = protocol_flags;
1.1       deraadt   536: }
                    537:
                    538: /* Returns the remote protocol flags set earlier by the above function. */
                    539:
1.40      markus    540: u_int
1.201     markus    541: ssh_packet_get_protocol_flags(struct ssh *ssh)
1.1       deraadt   542: {
1.201     markus    543:        return ssh->state->remote_protocol_flags;
1.1       deraadt   544: }
                    545:
1.16      markus    546: /*
                    547:  * Starts packet compression from the next packet on in both directions.
                    548:  * Level is compression level 1 (fastest) - 9 (slow, best) as in gzip.
                    549:  */
1.1       deraadt   550:
1.201     markus    551: static int
                    552: ssh_packet_init_compression(struct ssh *ssh)
1.60      markus    553: {
1.201     markus    554:        if (!ssh->state->compression_buffer &&
                    555:           ((ssh->state->compression_buffer = sshbuf_new()) == NULL))
                    556:                return SSH_ERR_ALLOC_FAIL;
                    557:        return 0;
1.60      markus    558: }
                    559:
1.201     markus    560: static int
                    561: start_compression_out(struct ssh *ssh, int level)
1.1       deraadt   562: {
1.201     markus    563:        if (level < 1 || level > 9)
                    564:                return SSH_ERR_INVALID_ARGUMENT;
                    565:        debug("Enabling compression at level %d.", level);
                    566:        if (ssh->state->compression_out_started == 1)
                    567:                deflateEnd(&ssh->state->compression_out_stream);
                    568:        switch (deflateInit(&ssh->state->compression_out_stream, level)) {
                    569:        case Z_OK:
                    570:                ssh->state->compression_out_started = 1;
                    571:                break;
                    572:        case Z_MEM_ERROR:
                    573:                return SSH_ERR_ALLOC_FAIL;
                    574:        default:
                    575:                return SSH_ERR_INTERNAL_ERROR;
                    576:        }
                    577:        return 0;
1.1       deraadt   578: }
                    579:
1.201     markus    580: static int
                    581: start_compression_in(struct ssh *ssh)
                    582: {
                    583:        if (ssh->state->compression_in_started == 1)
                    584:                inflateEnd(&ssh->state->compression_in_stream);
                    585:        switch (inflateInit(&ssh->state->compression_in_stream)) {
                    586:        case Z_OK:
                    587:                ssh->state->compression_in_started = 1;
                    588:                break;
                    589:        case Z_MEM_ERROR:
                    590:                return SSH_ERR_ALLOC_FAIL;
                    591:        default:
                    592:                return SSH_ERR_INTERNAL_ERROR;
                    593:        }
                    594:        return 0;
                    595: }
1.95      markus    596:
1.201     markus    597: int
                    598: ssh_packet_start_compression(struct ssh *ssh, int level)
1.1       deraadt   599: {
1.197     djm       600:        int r;
1.97      deraadt   601:
1.201     markus    602:        if (ssh->state->packet_compression && !compat20)
                    603:                return SSH_ERR_INTERNAL_ERROR;
                    604:        ssh->state->packet_compression = 1;
                    605:        if ((r = ssh_packet_init_compression(ssh)) != 0 ||
                    606:            (r = start_compression_in(ssh)) != 0 ||
                    607:            (r = start_compression_out(ssh, level)) != 0)
                    608:                return r;
                    609:        return 0;
1.95      markus    610: }
                    611:
1.201     markus    612: /* XXX remove need for separate compression buffer */
                    613: static int
                    614: compress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
1.95      markus    615: {
1.201     markus    616:        u_char buf[4096];
                    617:        int r, status;
                    618:
                    619:        if (ssh->state->compression_out_started != 1)
                    620:                return SSH_ERR_INTERNAL_ERROR;
                    621:
                    622:        /* This case is not handled below. */
                    623:        if (sshbuf_len(in) == 0)
                    624:                return 0;
                    625:
                    626:        /* Input is the contents of the input buffer. */
                    627:        if ((ssh->state->compression_out_stream.next_in =
                    628:            sshbuf_mutable_ptr(in)) == NULL)
                    629:                return SSH_ERR_INTERNAL_ERROR;
                    630:        ssh->state->compression_out_stream.avail_in = sshbuf_len(in);
                    631:
                    632:        /* Loop compressing until deflate() returns with avail_out != 0. */
                    633:        do {
                    634:                /* Set up fixed-size output buffer. */
                    635:                ssh->state->compression_out_stream.next_out = buf;
                    636:                ssh->state->compression_out_stream.avail_out = sizeof(buf);
                    637:
                    638:                /* Compress as much data into the buffer as possible. */
                    639:                status = deflate(&ssh->state->compression_out_stream,
                    640:                    Z_PARTIAL_FLUSH);
                    641:                switch (status) {
                    642:                case Z_MEM_ERROR:
                    643:                        return SSH_ERR_ALLOC_FAIL;
                    644:                case Z_OK:
                    645:                        /* Append compressed data to output_buffer. */
                    646:                        if ((r = sshbuf_put(out, buf, sizeof(buf) -
                    647:                            ssh->state->compression_out_stream.avail_out)) != 0)
                    648:                                return r;
                    649:                        break;
                    650:                case Z_STREAM_ERROR:
                    651:                default:
                    652:                        ssh->state->compression_out_failures++;
                    653:                        return SSH_ERR_INVALID_FORMAT;
                    654:                }
                    655:        } while (ssh->state->compression_out_stream.avail_out == 0);
                    656:        return 0;
1.1       deraadt   657: }
                    658:
1.201     markus    659: static int
                    660: uncompress_buffer(struct ssh *ssh, struct sshbuf *in, struct sshbuf *out)
1.1       deraadt   661: {
1.201     markus    662:        u_char buf[4096];
                    663:        int r, status;
1.1       deraadt   664:
1.201     markus    665:        if (ssh->state->compression_in_started != 1)
                    666:                return SSH_ERR_INTERNAL_ERROR;
1.25      markus    667:
1.201     markus    668:        if ((ssh->state->compression_in_stream.next_in =
                    669:            sshbuf_mutable_ptr(in)) == NULL)
                    670:                return SSH_ERR_INTERNAL_ERROR;
                    671:        ssh->state->compression_in_stream.avail_in = sshbuf_len(in);
1.97      deraadt   672:
1.201     markus    673:        for (;;) {
                    674:                /* Set up fixed-size output buffer. */
                    675:                ssh->state->compression_in_stream.next_out = buf;
                    676:                ssh->state->compression_in_stream.avail_out = sizeof(buf);
                    677:
                    678:                status = inflate(&ssh->state->compression_in_stream,
                    679:                    Z_PARTIAL_FLUSH);
                    680:                switch (status) {
                    681:                case Z_OK:
                    682:                        if ((r = sshbuf_put(out, buf, sizeof(buf) -
                    683:                            ssh->state->compression_in_stream.avail_out)) != 0)
                    684:                                return r;
                    685:                        break;
                    686:                case Z_BUF_ERROR:
                    687:                        /*
                    688:                         * Comments in zlib.h say that we should keep calling
                    689:                         * inflate() until we get an error.  This appears to
                    690:                         * be the error that we get.
                    691:                         */
                    692:                        return 0;
                    693:                case Z_DATA_ERROR:
                    694:                        return SSH_ERR_INVALID_FORMAT;
                    695:                case Z_MEM_ERROR:
                    696:                        return SSH_ERR_ALLOC_FAIL;
                    697:                case Z_STREAM_ERROR:
                    698:                default:
                    699:                        ssh->state->compression_in_failures++;
                    700:                        return SSH_ERR_INTERNAL_ERROR;
                    701:                }
                    702:        }
                    703:        /* NOTREACHED */
1.1       deraadt   704: }
1.125     deraadt   705:
1.201     markus    706: /* Serialise compression state into a blob for privsep */
                    707: static int
                    708: ssh_packet_get_compress_state(struct sshbuf *m, struct ssh *ssh)
1.1       deraadt   709: {
1.201     markus    710:        struct session_state *state = ssh->state;
                    711:        struct sshbuf *b;
                    712:        int r;
                    713:
                    714:        if ((b = sshbuf_new()) == NULL)
                    715:                return SSH_ERR_ALLOC_FAIL;
                    716:        if (state->compression_in_started) {
                    717:                if ((r = sshbuf_put_string(b, &state->compression_in_stream,
                    718:                    sizeof(state->compression_in_stream))) != 0)
                    719:                        goto out;
                    720:        } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
                    721:                goto out;
                    722:        if (state->compression_out_started) {
                    723:                if ((r = sshbuf_put_string(b, &state->compression_out_stream,
                    724:                    sizeof(state->compression_out_stream))) != 0)
                    725:                        goto out;
                    726:        } else if ((r = sshbuf_put_string(b, NULL, 0)) != 0)
                    727:                goto out;
                    728:        r = sshbuf_put_stringb(m, b);
                    729:  out:
                    730:        sshbuf_free(b);
                    731:        return r;
1.1       deraadt   732: }
1.125     deraadt   733:
1.201     markus    734: /* Deserialise compression state from a blob for privsep */
                    735: static int
                    736: ssh_packet_set_compress_state(struct ssh *ssh, struct sshbuf *m)
1.162     andreas   737: {
1.201     markus    738:        struct session_state *state = ssh->state;
                    739:        struct sshbuf *b = NULL;
                    740:        int r;
                    741:        const u_char *inblob, *outblob;
                    742:        size_t inl, outl;
1.162     andreas   743:
1.201     markus    744:        if ((r = sshbuf_froms(m, &b)) != 0)
                    745:                goto out;
                    746:        if ((r = sshbuf_get_string_direct(b, &inblob, &inl)) != 0 ||
                    747:            (r = sshbuf_get_string_direct(b, &outblob, &outl)) != 0)
                    748:                goto out;
                    749:        if (inl == 0)
                    750:                state->compression_in_started = 0;
                    751:        else if (inl != sizeof(state->compression_in_stream)) {
                    752:                r = SSH_ERR_INTERNAL_ERROR;
                    753:                goto out;
                    754:        } else {
                    755:                state->compression_in_started = 1;
                    756:                memcpy(&state->compression_in_stream, inblob, inl);
                    757:        }
                    758:        if (outl == 0)
                    759:                state->compression_out_started = 0;
                    760:        else if (outl != sizeof(state->compression_out_stream)) {
                    761:                r = SSH_ERR_INTERNAL_ERROR;
                    762:                goto out;
                    763:        } else {
                    764:                state->compression_out_started = 1;
                    765:                memcpy(&state->compression_out_stream, outblob, outl);
                    766:        }
                    767:        r = 0;
                    768:  out:
                    769:        sshbuf_free(b);
                    770:        return r;
1.1       deraadt   771: }
1.125     deraadt   772:
1.24      markus    773: void
1.201     markus    774: ssh_packet_set_compress_hooks(struct ssh *ssh, void *ctx,
                    775:     void *(*allocfunc)(void *, u_int, u_int),
                    776:     void (*freefunc)(void *, void *))
                    777: {
                    778:        ssh->state->compression_out_stream.zalloc = (alloc_func)allocfunc;
                    779:        ssh->state->compression_out_stream.zfree = (free_func)freefunc;
                    780:        ssh->state->compression_out_stream.opaque = ctx;
                    781:        ssh->state->compression_in_stream.zalloc = (alloc_func)allocfunc;
                    782:        ssh->state->compression_in_stream.zfree = (free_func)freefunc;
                    783:        ssh->state->compression_in_stream.opaque = ctx;
1.24      markus    784: }
1.125     deraadt   785:
1.201     markus    786: /*
                    787:  * Causes any further packets to be encrypted using the given key.  The same
                    788:  * key is used for both sending and reception.  However, both directions are
                    789:  * encrypted independently of each other.
                    790:  */
1.125     deraadt   791:
1.2       provos    792: void
1.201     markus    793: ssh_packet_set_encryption_key(struct ssh *ssh, const u_char *key, u_int keylen, int number)
1.1       deraadt   794: {
1.211     djm       795: #ifndef WITH_SSH1
                    796:        fatal("no SSH protocol 1 support");
                    797: #else /* WITH_SSH1 */
1.201     markus    798:        struct session_state *state = ssh->state;
                    799:        const struct sshcipher *cipher = cipher_by_number(number);
                    800:        int r;
                    801:        const char *wmsg;
1.125     deraadt   802:
1.201     markus    803:        if (cipher == NULL)
                    804:                fatal("%s: unknown cipher number %d", __func__, number);
                    805:        if (keylen < 20)
                    806:                fatal("%s: keylen too small: %d", __func__, keylen);
                    807:        if (keylen > SSH_SESSION_KEY_LENGTH)
                    808:                fatal("%s: keylen too big: %d", __func__, keylen);
                    809:        memcpy(state->ssh1_key, key, keylen);
                    810:        state->ssh1_keylen = keylen;
                    811:        if ((r = cipher_init(&state->send_context, cipher, key, keylen,
                    812:            NULL, 0, CIPHER_ENCRYPT)) != 0 ||
                    813:            (r = cipher_init(&state->receive_context, cipher, key, keylen,
                    814:            NULL, 0, CIPHER_DECRYPT) != 0))
                    815:                fatal("%s: cipher_init failed: %s", __func__, ssh_err(r));
                    816:        if (!state->cipher_warning_done &&
                    817:            ((wmsg = cipher_warning_message(&state->send_context)) != NULL ||
                    818:            (wmsg = cipher_warning_message(&state->send_context)) != NULL)) {
                    819:                error("Warning: %s", wmsg);
                    820:                state->cipher_warning_done = 1;
                    821:        }
1.211     djm       822: #endif /* WITH_SSH1 */
1.170     djm       823: }
                    824:
1.16      markus    825: /*
                    826:  * Finalizes and sends the packet.  If the encryption key has been set,
                    827:  * encrypts the packet before sending.
                    828:  */
1.14      markus    829:
1.201     markus    830: int
                    831: ssh_packet_send1(struct ssh *ssh)
1.1       deraadt   832: {
1.201     markus    833:        struct session_state *state = ssh->state;
1.89      markus    834:        u_char buf[8], *cp;
1.201     markus    835:        int r, padding, len;
1.40      markus    836:        u_int checksum;
1.14      markus    837:
1.16      markus    838:        /*
                    839:         * If using packet compression, compress the payload of the outgoing
                    840:         * packet.
                    841:         */
1.201     markus    842:        if (state->packet_compression) {
                    843:                sshbuf_reset(state->compression_buffer);
1.14      markus    844:                /* Skip padding. */
1.201     markus    845:                if ((r = sshbuf_consume(state->outgoing_packet, 8)) != 0)
                    846:                        goto out;
1.14      markus    847:                /* padding */
1.201     markus    848:                if ((r = sshbuf_put(state->compression_buffer,
                    849:                    "\0\0\0\0\0\0\0\0", 8)) != 0)
                    850:                        goto out;
                    851:                if ((r = compress_buffer(ssh, state->outgoing_packet,
                    852:                    state->compression_buffer)) != 0)
                    853:                        goto out;
                    854:                sshbuf_reset(state->outgoing_packet);
                    855:                 if ((r = sshbuf_putb(state->outgoing_packet,
                    856:                     state->compression_buffer)) != 0)
                    857:                        goto out;
1.14      markus    858:        }
                    859:        /* Compute packet length without padding (add checksum, remove padding). */
1.201     markus    860:        len = sshbuf_len(state->outgoing_packet) + 4 - 8;
1.14      markus    861:
1.32      markus    862:        /* Insert padding. Initialized to zero in packet_start1() */
1.14      markus    863:        padding = 8 - len % 8;
1.201     markus    864:        if (!state->send_context.plaintext) {
                    865:                cp = sshbuf_mutable_ptr(state->outgoing_packet);
                    866:                if (cp == NULL) {
                    867:                        r = SSH_ERR_INTERNAL_ERROR;
                    868:                        goto out;
1.14      markus    869:                }
1.201     markus    870:                arc4random_buf(cp + 8 - padding, padding);
1.14      markus    871:        }
1.201     markus    872:        if ((r = sshbuf_consume(state->outgoing_packet, 8 - padding)) != 0)
                    873:                goto out;
1.14      markus    874:
                    875:        /* Add check bytes. */
1.201     markus    876:        checksum = ssh_crc32(sshbuf_ptr(state->outgoing_packet),
                    877:            sshbuf_len(state->outgoing_packet));
                    878:        POKE_U32(buf, checksum);
                    879:        if ((r = sshbuf_put(state->outgoing_packet, buf, 4)) != 0)
                    880:                goto out;
1.1       deraadt   881:
                    882: #ifdef PACKET_DEBUG
1.14      markus    883:        fprintf(stderr, "packet_send plain: ");
1.201     markus    884:        sshbuf_dump(state->outgoing_packet, stderr);
1.1       deraadt   885: #endif
                    886:
1.14      markus    887:        /* Append to output. */
1.201     markus    888:        POKE_U32(buf, len);
                    889:        if ((r = sshbuf_put(state->output, buf, 4)) != 0)
                    890:                goto out;
                    891:        if ((r = sshbuf_reserve(state->output,
                    892:            sshbuf_len(state->outgoing_packet), &cp)) != 0)
                    893:                goto out;
                    894:        if ((r = cipher_crypt(&state->send_context, 0, cp,
                    895:            sshbuf_ptr(state->outgoing_packet),
                    896:            sshbuf_len(state->outgoing_packet), 0, 0)) != 0)
                    897:                goto out;
1.14      markus    898:
1.1       deraadt   899: #ifdef PACKET_DEBUG
1.14      markus    900:        fprintf(stderr, "encrypted: ");
1.201     markus    901:        sshbuf_dump(state->output, stderr);
1.1       deraadt   902: #endif
1.201     markus    903:        state->p_send.packets++;
                    904:        state->p_send.bytes += len +
                    905:            sshbuf_len(state->outgoing_packet);
                    906:        sshbuf_reset(state->outgoing_packet);
1.1       deraadt   907:
1.16      markus    908:        /*
1.120     djm       909:         * Note that the packet is now only buffered in output.  It won't be
1.205     djm       910:         * actually sent until ssh_packet_write_wait or ssh_packet_write_poll
                    911:         * is called.
1.16      markus    912:         */
1.201     markus    913:        r = 0;
                    914:  out:
                    915:        return r;
1.1       deraadt   916: }
                    917:
1.201     markus    918: int
                    919: ssh_set_newkeys(struct ssh *ssh, int mode)
1.57      markus    920: {
1.201     markus    921:        struct session_state *state = ssh->state;
                    922:        struct sshenc *enc;
                    923:        struct sshmac *mac;
                    924:        struct sshcomp *comp;
                    925:        struct sshcipher_ctx *cc;
1.105     markus    926:        u_int64_t *max_blocks;
1.201     markus    927:        const char *wmsg;
1.197     djm       928:        int r, crypt_type;
1.57      markus    929:
1.100     markus    930:        debug2("set_newkeys: mode %d", mode);
1.57      markus    931:
1.88      markus    932:        if (mode == MODE_OUT) {
1.201     markus    933:                cc = &state->send_context;
1.115     avsm      934:                crypt_type = CIPHER_ENCRYPT;
1.201     markus    935:                state->p_send.packets = state->p_send.blocks = 0;
                    936:                max_blocks = &state->max_blocks_out;
1.88      markus    937:        } else {
1.201     markus    938:                cc = &state->receive_context;
1.115     avsm      939:                crypt_type = CIPHER_DECRYPT;
1.201     markus    940:                state->p_read.packets = state->p_read.blocks = 0;
                    941:                max_blocks = &state->max_blocks_in;
1.88      markus    942:        }
1.201     markus    943:        if (state->newkeys[mode] != NULL) {
1.224     dtucker   944:                debug("set_newkeys: rekeying, input %llu bytes %llu blocks, "
                    945:                   "output %llu bytes %llu blocks",
1.227   ! djm       946:                   (unsigned long long)state->p_read.bytes,
        !           947:                   (unsigned long long)state->p_read.blocks,
        !           948:                   (unsigned long long)state->p_send.bytes,
        !           949:                   (unsigned long long)state->p_send.blocks);
1.201     markus    950:                if ((r = cipher_cleanup(cc)) != 0)
                    951:                        return r;
                    952:                enc  = &state->newkeys[mode]->enc;
                    953:                mac  = &state->newkeys[mode]->mac;
                    954:                comp = &state->newkeys[mode]->comp;
1.148     pvalchev  955:                mac_clear(mac);
1.192     djm       956:                explicit_bzero(enc->iv,  enc->iv_len);
                    957:                explicit_bzero(enc->key, enc->key_len);
                    958:                explicit_bzero(mac->key, mac->key_len);
1.186     djm       959:                free(enc->name);
                    960:                free(enc->iv);
                    961:                free(enc->key);
                    962:                free(mac->name);
                    963:                free(mac->key);
                    964:                free(comp->name);
1.201     markus    965:                free(state->newkeys[mode]);
1.57      markus    966:        }
1.201     markus    967:        /* move newkeys from kex to state */
                    968:        if ((state->newkeys[mode] = ssh->kex->newkeys[mode]) == NULL)
                    969:                return SSH_ERR_INTERNAL_ERROR;
                    970:        ssh->kex->newkeys[mode] = NULL;
                    971:        enc  = &state->newkeys[mode]->enc;
                    972:        mac  = &state->newkeys[mode]->mac;
                    973:        comp = &state->newkeys[mode]->comp;
                    974:        if (cipher_authlen(enc->cipher) == 0) {
                    975:                if ((r = mac_init(mac)) != 0)
                    976:                        return r;
                    977:        }
                    978:        mac->enabled = 1;
1.57      markus    979:        DBG(debug("cipher_init_context: %d", mode));
1.197     djm       980:        if ((r = cipher_init(cc, enc->cipher, enc->key, enc->key_len,
                    981:            enc->iv, enc->iv_len, crypt_type)) != 0)
1.201     markus    982:                return r;
                    983:        if (!state->cipher_warning_done &&
                    984:            (wmsg = cipher_warning_message(cc)) != NULL) {
                    985:                error("Warning: %s", wmsg);
                    986:                state->cipher_warning_done = 1;
                    987:        }
1.91      markus    988:        /* Deleting the keys does not gain extra security */
1.192     djm       989:        /* explicit_bzero(enc->iv,  enc->block_size);
                    990:           explicit_bzero(enc->key, enc->key_len);
                    991:           explicit_bzero(mac->key, mac->key_len); */
1.118     markus    992:        if ((comp->type == COMP_ZLIB ||
1.161     andreas   993:            (comp->type == COMP_DELAYED &&
1.201     markus    994:             state->after_authentication)) && comp->enabled == 0) {
                    995:                if ((r = ssh_packet_init_compression(ssh)) < 0)
                    996:                        return r;
                    997:                if (mode == MODE_OUT) {
                    998:                        if ((r = start_compression_out(ssh, 6)) != 0)
                    999:                                return r;
                   1000:                } else {
                   1001:                        if ((r = start_compression_in(ssh)) != 0)
                   1002:                                return r;
                   1003:                }
1.57      markus   1004:                comp->enabled = 1;
                   1005:        }
1.109     markus   1006:        /*
                   1007:         * The 2^(blocksize*2) limit is too expensive for 3DES,
                   1008:         * blowfish, etc, so enforce a 1GB limit for small blocksizes.
                   1009:         */
                   1010:        if (enc->block_size >= 16)
                   1011:                *max_blocks = (u_int64_t)1 << (enc->block_size*2);
                   1012:        else
                   1013:                *max_blocks = ((u_int64_t)1 << 30) / enc->block_size;
1.201     markus   1014:        if (state->rekey_limit)
1.161     andreas  1015:                *max_blocks = MIN(*max_blocks,
1.201     markus   1016:                    state->rekey_limit / enc->block_size);
1.227   ! djm      1017:        debug("rekey after %llu blocks", (unsigned long long)*max_blocks);
1.201     markus   1018:        return 0;
1.57      markus   1019: }
                   1020:
1.16      markus   1021: /*
1.118     markus   1022:  * Delayed compression for SSH2 is enabled after authentication:
1.143     dtucker  1023:  * This happens on the server side after a SSH2_MSG_USERAUTH_SUCCESS is sent,
1.118     markus   1024:  * and on the client side after a SSH2_MSG_USERAUTH_SUCCESS is received.
                   1025:  */
1.201     markus   1026: static int
                   1027: ssh_packet_enable_delayed_compress(struct ssh *ssh)
1.118     markus   1028: {
1.201     markus   1029:        struct session_state *state = ssh->state;
                   1030:        struct sshcomp *comp = NULL;
                   1031:        int r, mode;
1.118     markus   1032:
                   1033:        /*
                   1034:         * Remember that we are past the authentication step, so rekeying
                   1035:         * with COMP_DELAYED will turn on compression immediately.
                   1036:         */
1.201     markus   1037:        state->after_authentication = 1;
1.118     markus   1038:        for (mode = 0; mode < MODE_MAX; mode++) {
1.145     markus   1039:                /* protocol error: USERAUTH_SUCCESS received before NEWKEYS */
1.201     markus   1040:                if (state->newkeys[mode] == NULL)
1.145     markus   1041:                        continue;
1.201     markus   1042:                comp = &state->newkeys[mode]->comp;
1.118     markus   1043:                if (comp && !comp->enabled && comp->type == COMP_DELAYED) {
1.201     markus   1044:                        if ((r = ssh_packet_init_compression(ssh)) != 0)
                   1045:                                return r;
                   1046:                        if (mode == MODE_OUT) {
                   1047:                                if ((r = start_compression_out(ssh, 6)) != 0)
                   1048:                                        return r;
                   1049:                        } else {
                   1050:                                if ((r = start_compression_in(ssh)) != 0)
                   1051:                                        return r;
                   1052:                        }
1.118     markus   1053:                        comp->enabled = 1;
                   1054:                }
                   1055:        }
1.201     markus   1056:        return 0;
1.118     markus   1057: }
                   1058:
1.226     djm      1059: /* Used to mute debug logging for noisy packet types */
                   1060: static int
                   1061: ssh_packet_log_type(u_char type)
                   1062: {
                   1063:        switch (type) {
                   1064:        case SSH2_MSG_CHANNEL_DATA:
                   1065:        case SSH2_MSG_CHANNEL_EXTENDED_DATA:
                   1066:        case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
                   1067:                return 0;
                   1068:        default:
                   1069:                return 1;
                   1070:        }
                   1071: }
                   1072:
1.118     markus   1073: /*
1.25      markus   1074:  * Finalize packet in SSH2 format (compress, mac, encrypt, enqueue)
                   1075:  */
1.201     markus   1076: int
                   1077: ssh_packet_send2_wrapped(struct ssh *ssh)
1.25      markus   1078: {
1.201     markus   1079:        struct session_state *state = ssh->state;
1.200     markus   1080:        u_char type, *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
1.178     markus   1081:        u_char padlen, pad = 0;
1.201     markus   1082:        u_int authlen = 0, aadlen = 0;
                   1083:        u_int len;
                   1084:        struct sshenc *enc   = NULL;
                   1085:        struct sshmac *mac   = NULL;
                   1086:        struct sshcomp *comp = NULL;
                   1087:        int r, block_size;
                   1088:
                   1089:        if (state->newkeys[MODE_OUT] != NULL) {
                   1090:                enc  = &state->newkeys[MODE_OUT]->enc;
                   1091:                mac  = &state->newkeys[MODE_OUT]->mac;
                   1092:                comp = &state->newkeys[MODE_OUT]->comp;
1.180     markus   1093:                /* disable mac for authenticated encryption */
                   1094:                if ((authlen = cipher_authlen(enc->cipher)) != 0)
                   1095:                        mac = NULL;
1.25      markus   1096:        }
1.88      markus   1097:        block_size = enc ? enc->block_size : 8;
1.180     markus   1098:        aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
1.25      markus   1099:
1.201     markus   1100:        type = (sshbuf_ptr(state->outgoing_packet))[5];
1.226     djm      1101:        if (ssh_packet_log_type(type))
                   1102:                debug3("send packet: type %u", type);
1.25      markus   1103: #ifdef PACKET_DEBUG
                   1104:        fprintf(stderr, "plain:     ");
1.201     markus   1105:        sshbuf_dump(state->outgoing_packet, stderr);
1.25      markus   1106: #endif
                   1107:
                   1108:        if (comp && comp->enabled) {
1.201     markus   1109:                len = sshbuf_len(state->outgoing_packet);
1.25      markus   1110:                /* skip header, compress only payload */
1.201     markus   1111:                if ((r = sshbuf_consume(state->outgoing_packet, 5)) != 0)
                   1112:                        goto out;
                   1113:                sshbuf_reset(state->compression_buffer);
                   1114:                if ((r = compress_buffer(ssh, state->outgoing_packet,
                   1115:                    state->compression_buffer)) != 0)
                   1116:                        goto out;
                   1117:                sshbuf_reset(state->outgoing_packet);
                   1118:                if ((r = sshbuf_put(state->outgoing_packet,
                   1119:                    "\0\0\0\0\0", 5)) != 0 ||
                   1120:                    (r = sshbuf_putb(state->outgoing_packet,
                   1121:                    state->compression_buffer)) != 0)
                   1122:                        goto out;
                   1123:                DBG(debug("compression: raw %d compressed %zd", len,
                   1124:                    sshbuf_len(state->outgoing_packet)));
1.25      markus   1125:        }
                   1126:
                   1127:        /* sizeof (packet_len + pad_len + payload) */
1.201     markus   1128:        len = sshbuf_len(state->outgoing_packet);
1.25      markus   1129:
                   1130:        /*
                   1131:         * calc size of padding, alloc space, get random data,
                   1132:         * minimum padding is 4 bytes
                   1133:         */
1.178     markus   1134:        len -= aadlen; /* packet length is not encrypted for EtM modes */
1.25      markus   1135:        padlen = block_size - (len % block_size);
                   1136:        if (padlen < 4)
                   1137:                padlen += block_size;
1.201     markus   1138:        if (state->extra_pad) {
1.71      markus   1139:                /* will wrap if extra_pad+padlen > 255 */
1.201     markus   1140:                state->extra_pad =
                   1141:                    roundup(state->extra_pad, block_size);
                   1142:                pad = state->extra_pad -
                   1143:                    ((len + padlen) % state->extra_pad);
1.193     djm      1144:                DBG(debug3("%s: adding %d (len %d padlen %d extra_pad %d)",
1.201     markus   1145:                    __func__, pad, len, padlen, state->extra_pad));
1.71      markus   1146:                padlen += pad;
1.201     markus   1147:                state->extra_pad = 0;
1.71      markus   1148:        }
1.201     markus   1149:        if ((r = sshbuf_reserve(state->outgoing_packet, padlen, &cp)) != 0)
                   1150:                goto out;
                   1151:        if (enc && !state->send_context.plaintext) {
1.32      markus   1152:                /* random padding */
1.201     markus   1153:                arc4random_buf(cp, padlen);
1.32      markus   1154:        } else {
                   1155:                /* clear padding */
1.192     djm      1156:                explicit_bzero(cp, padlen);
1.25      markus   1157:        }
1.178     markus   1158:        /* sizeof (packet_len + pad_len + payload + padding) */
1.201     markus   1159:        len = sshbuf_len(state->outgoing_packet);
                   1160:        cp = sshbuf_mutable_ptr(state->outgoing_packet);
                   1161:        if (cp == NULL) {
                   1162:                r = SSH_ERR_INTERNAL_ERROR;
                   1163:                goto out;
                   1164:        }
1.25      markus   1165:        /* packet_length includes payload, padding and padding length field */
1.201     markus   1166:        POKE_U32(cp, len - 4);
1.89      markus   1167:        cp[4] = padlen;
1.178     markus   1168:        DBG(debug("send: len %d (includes padlen %d, aadlen %d)",
                   1169:            len, padlen, aadlen));
1.25      markus   1170:
                   1171:        /* compute MAC over seqnr and packet(length fields, payload, padding) */
1.178     markus   1172:        if (mac && mac->enabled && !mac->etm) {
1.201     markus   1173:                if ((r = mac_compute(mac, state->p_send.seqnr,
                   1174:                    sshbuf_ptr(state->outgoing_packet), len,
1.200     markus   1175:                    macbuf, sizeof(macbuf))) != 0)
1.201     markus   1176:                        goto out;
                   1177:                DBG(debug("done calc MAC out #%d", state->p_send.seqnr));
1.25      markus   1178:        }
                   1179:        /* encrypt packet and append to output buffer. */
1.201     markus   1180:        if ((r = sshbuf_reserve(state->output,
                   1181:            sshbuf_len(state->outgoing_packet) + authlen, &cp)) != 0)
                   1182:                goto out;
                   1183:        if ((r = cipher_crypt(&state->send_context, state->p_send.seqnr, cp,
                   1184:            sshbuf_ptr(state->outgoing_packet),
                   1185:            len - aadlen, aadlen, authlen)) != 0)
                   1186:                goto out;
1.25      markus   1187:        /* append unencrypted MAC */
1.178     markus   1188:        if (mac && mac->enabled) {
                   1189:                if (mac->etm) {
                   1190:                        /* EtM: compute mac over aadlen + cipher text */
1.201     markus   1191:                        if ((r = mac_compute(mac, state->p_send.seqnr,
                   1192:                            cp, len, macbuf, sizeof(macbuf))) != 0)
                   1193:                                goto out;
1.178     markus   1194:                        DBG(debug("done calc MAC(EtM) out #%d",
1.201     markus   1195:                            state->p_send.seqnr));
1.178     markus   1196:                }
1.201     markus   1197:                if ((r = sshbuf_put(state->output, macbuf, mac->mac_len)) != 0)
                   1198:                        goto out;
1.178     markus   1199:        }
1.25      markus   1200: #ifdef PACKET_DEBUG
                   1201:        fprintf(stderr, "encrypted: ");
1.201     markus   1202:        sshbuf_dump(state->output, stderr);
1.25      markus   1203: #endif
1.29      markus   1204:        /* increment sequence number for outgoing packets */
1.201     markus   1205:        if (++state->p_send.seqnr == 0)
1.106     itojun   1206:                logit("outgoing seqnr wraps around");
1.201     markus   1207:        if (++state->p_send.packets == 0)
                   1208:                if (!(ssh->compat & SSH_BUG_NOREKEY))
                   1209:                        return SSH_ERR_NEED_REKEY;
                   1210:        state->p_send.blocks += len / block_size;
                   1211:        state->p_send.bytes += len;
                   1212:        sshbuf_reset(state->outgoing_packet);
1.25      markus   1213:
1.57      markus   1214:        if (type == SSH2_MSG_NEWKEYS)
1.201     markus   1215:                r = ssh_set_newkeys(ssh, MODE_OUT);
                   1216:        else if (type == SSH2_MSG_USERAUTH_SUCCESS && state->server_side)
                   1217:                r = ssh_packet_enable_delayed_compress(ssh);
                   1218:        else
                   1219:                r = 0;
                   1220:  out:
                   1221:        return r;
1.25      markus   1222: }
                   1223:
1.201     markus   1224: int
                   1225: ssh_packet_send2(struct ssh *ssh)
1.105     markus   1226: {
1.201     markus   1227:        struct session_state *state = ssh->state;
1.105     markus   1228:        struct packet *p;
1.201     markus   1229:        u_char type;
                   1230:        int r;
1.105     markus   1231:
1.201     markus   1232:        type = sshbuf_ptr(state->outgoing_packet)[5];
1.105     markus   1233:
                   1234:        /* during rekeying we can only send key exchange messages */
1.201     markus   1235:        if (state->rekeying) {
1.175     markus   1236:                if ((type < SSH2_MSG_TRANSPORT_MIN) ||
                   1237:                    (type > SSH2_MSG_TRANSPORT_MAX) ||
                   1238:                    (type == SSH2_MSG_SERVICE_REQUEST) ||
1.218     markus   1239:                    (type == SSH2_MSG_SERVICE_ACCEPT) ||
                   1240:                    (type == SSH2_MSG_EXT_INFO)) {
1.105     markus   1241:                        debug("enqueue packet: %u", type);
1.201     markus   1242:                        p = calloc(1, sizeof(*p));
                   1243:                        if (p == NULL)
                   1244:                                return SSH_ERR_ALLOC_FAIL;
1.105     markus   1245:                        p->type = type;
1.201     markus   1246:                        p->payload = state->outgoing_packet;
                   1247:                        TAILQ_INSERT_TAIL(&state->outgoing, p, next);
                   1248:                        state->outgoing_packet = sshbuf_new();
                   1249:                        if (state->outgoing_packet == NULL)
                   1250:                                return SSH_ERR_ALLOC_FAIL;
                   1251:                        return 0;
1.105     markus   1252:                }
                   1253:        }
                   1254:
                   1255:        /* rekeying starts with sending KEXINIT */
                   1256:        if (type == SSH2_MSG_KEXINIT)
1.201     markus   1257:                state->rekeying = 1;
1.105     markus   1258:
1.201     markus   1259:        if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
                   1260:                return r;
1.105     markus   1261:
                   1262:        /* after a NEWKEYS message we can send the complete queue */
                   1263:        if (type == SSH2_MSG_NEWKEYS) {
1.201     markus   1264:                state->rekeying = 0;
                   1265:                state->rekey_time = monotime();
                   1266:                while ((p = TAILQ_FIRST(&state->outgoing))) {
1.105     markus   1267:                        type = p->type;
                   1268:                        debug("dequeue packet: %u", type);
1.201     markus   1269:                        sshbuf_free(state->outgoing_packet);
                   1270:                        state->outgoing_packet = p->payload;
                   1271:                        TAILQ_REMOVE(&state->outgoing, p, next);
1.186     djm      1272:                        free(p);
1.201     markus   1273:                        if ((r = ssh_packet_send2_wrapped(ssh)) != 0)
                   1274:                                return r;
1.105     markus   1275:                }
                   1276:        }
1.201     markus   1277:        return 0;
1.25      markus   1278: }
                   1279:
                   1280: /*
1.16      markus   1281:  * Waits until a packet has been received, and returns its type.  Note that
                   1282:  * no other data is processed until this returns, so this function should not
                   1283:  * be used during the interactive session.
                   1284:  */
1.1       deraadt  1285:
1.2       provos   1286: int
1.201     markus   1287: ssh_packet_read_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
1.1       deraadt  1288: {
1.201     markus   1289:        struct session_state *state = ssh->state;
1.222     markus   1290:        int len, r, ms_remain;
1.56      millert  1291:        fd_set *setp;
1.14      markus   1292:        char buf[8192];
1.155     deraadt  1293:        struct timeval timeout, start, *timeoutp = NULL;
                   1294:
1.25      markus   1295:        DBG(debug("packet_read()"));
1.14      markus   1296:
1.214     deraadt  1297:        setp = calloc(howmany(state->connection_in + 1,
1.161     andreas  1298:            NFDBITS), sizeof(fd_mask));
1.201     markus   1299:        if (setp == NULL)
                   1300:                return SSH_ERR_ALLOC_FAIL;
1.56      millert  1301:
1.205     djm      1302:        /*
                   1303:         * Since we are blocking, ensure that all written packets have
                   1304:         * been sent.
                   1305:         */
1.210     markus   1306:        if ((r = ssh_packet_write_wait(ssh)) != 0)
                   1307:                goto out;
1.14      markus   1308:
                   1309:        /* Stay in the loop until we have received a complete packet. */
                   1310:        for (;;) {
                   1311:                /* Try to read a packet from the buffer. */
1.201     markus   1312:                r = ssh_packet_read_poll_seqnr(ssh, typep, seqnr_p);
                   1313:                if (r != 0)
                   1314:                        break;
1.62      markus   1315:                if (!compat20 && (
1.201     markus   1316:                    *typep == SSH_SMSG_SUCCESS
                   1317:                    || *typep == SSH_SMSG_FAILURE
                   1318:                    || *typep == SSH_CMSG_EOF
                   1319:                    || *typep == SSH_CMSG_EXIT_CONFIRMATION))
                   1320:                        if ((r = sshpkt_get_end(ssh)) != 0)
                   1321:                                break;
1.14      markus   1322:                /* If we got a packet, return it. */
1.201     markus   1323:                if (*typep != SSH_MSG_NONE)
                   1324:                        break;
1.16      markus   1325:                /*
                   1326:                 * Otherwise, wait for some data to arrive, add it to the
                   1327:                 * buffer, and try again.
                   1328:                 */
1.201     markus   1329:                memset(setp, 0, howmany(state->connection_in + 1,
1.161     andreas  1330:                    NFDBITS) * sizeof(fd_mask));
1.201     markus   1331:                FD_SET(state->connection_in, setp);
1.16      markus   1332:
1.201     markus   1333:                if (state->packet_timeout_ms > 0) {
                   1334:                        ms_remain = state->packet_timeout_ms;
1.154     dtucker  1335:                        timeoutp = &timeout;
                   1336:                }
1.14      markus   1337:                /* Wait for some data to arrive. */
1.154     dtucker  1338:                for (;;) {
1.201     markus   1339:                        if (state->packet_timeout_ms != -1) {
1.154     dtucker  1340:                                ms_to_timeval(&timeout, ms_remain);
                   1341:                                gettimeofday(&start, NULL);
                   1342:                        }
1.201     markus   1343:                        if ((r = select(state->connection_in + 1, setp,
1.161     andreas  1344:                            NULL, NULL, timeoutp)) >= 0)
1.154     dtucker  1345:                                break;
1.161     andreas  1346:                        if (errno != EAGAIN && errno != EINTR)
1.154     dtucker  1347:                                break;
1.201     markus   1348:                        if (state->packet_timeout_ms == -1)
1.154     dtucker  1349:                                continue;
                   1350:                        ms_subtract_diff(&start, &ms_remain);
                   1351:                        if (ms_remain <= 0) {
1.201     markus   1352:                                r = 0;
1.154     dtucker  1353:                                break;
                   1354:                        }
                   1355:                }
1.204     djm      1356:                if (r == 0)
                   1357:                        return SSH_ERR_CONN_TIMEOUT;
1.14      markus   1358:                /* Read data from the socket. */
1.222     markus   1359:                len = read(state->connection_in, buf, sizeof(buf));
1.210     markus   1360:                if (len == 0) {
                   1361:                        r = SSH_ERR_CONN_CLOSED;
                   1362:                        goto out;
                   1363:                }
                   1364:                if (len < 0) {
                   1365:                        r = SSH_ERR_SYSTEM_ERROR;
                   1366:                        goto out;
                   1367:                }
1.204     djm      1368:
1.14      markus   1369:                /* Append it to the buffer. */
1.204     djm      1370:                if ((r = ssh_packet_process_incoming(ssh, buf, len)) != 0)
1.210     markus   1371:                        goto out;
1.14      markus   1372:        }
1.210     markus   1373:  out:
1.201     markus   1374:        free(setp);
                   1375:        return r;
1.1       deraadt  1376: }
                   1377:
1.77      djm      1378: int
1.201     markus   1379: ssh_packet_read(struct ssh *ssh)
1.77      djm      1380: {
1.201     markus   1381:        u_char type;
                   1382:        int r;
                   1383:
                   1384:        if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
                   1385:                fatal("%s: %s", __func__, ssh_err(r));
                   1386:        return type;
1.77      djm      1387: }
                   1388:
1.16      markus   1389: /*
                   1390:  * Waits until a packet has been received, verifies that its type matches
                   1391:  * that given, and gives a fatal error and exits if there is a mismatch.
                   1392:  */
1.1       deraadt  1393:
1.205     djm      1394: int
                   1395: ssh_packet_read_expect(struct ssh *ssh, u_int expected_type)
1.1       deraadt  1396: {
1.205     djm      1397:        int r;
                   1398:        u_char type;
1.1       deraadt  1399:
1.205     djm      1400:        if ((r = ssh_packet_read_seqnr(ssh, &type, NULL)) != 0)
                   1401:                return r;
                   1402:        if (type != expected_type) {
                   1403:                if ((r = sshpkt_disconnect(ssh,
1.201     markus   1404:                    "Protocol error: expected packet type %d, got %d",
1.205     djm      1405:                    expected_type, type)) != 0)
                   1406:                        return r;
                   1407:                return SSH_ERR_PROTOCOL_ERROR;
                   1408:        }
                   1409:        return 0;
1.1       deraadt  1410: }
                   1411:
                   1412: /* Checks if a full packet is available in the data received so far via
1.14      markus   1413:  * packet_process_incoming.  If so, reads the packet; otherwise returns
                   1414:  * SSH_MSG_NONE.  This does not wait for data from the connection.
                   1415:  *
1.150     dtucker  1416:  * SSH_MSG_DISCONNECT is handled specially here.  Also,
                   1417:  * SSH_MSG_IGNORE messages are skipped by this function and are never returned
                   1418:  * to higher levels.
1.14      markus   1419:  */
1.1       deraadt  1420:
1.201     markus   1421: int
                   1422: ssh_packet_read_poll1(struct ssh *ssh, u_char *typep)
1.1       deraadt  1423: {
1.201     markus   1424:        struct session_state *state = ssh->state;
1.40      markus   1425:        u_int len, padded_len;
1.205     djm      1426:        const char *emsg;
1.201     markus   1427:        const u_char *cp;
                   1428:        u_char *p;
1.40      markus   1429:        u_int checksum, stored_checksum;
1.201     markus   1430:        int r;
                   1431:
                   1432:        *typep = SSH_MSG_NONE;
1.14      markus   1433:
                   1434:        /* Check if input size is less than minimum packet size. */
1.201     markus   1435:        if (sshbuf_len(state->input) < 4 + 8)
                   1436:                return 0;
1.14      markus   1437:        /* Get length of incoming packet. */
1.201     markus   1438:        len = PEEK_U32(sshbuf_ptr(state->input));
1.205     djm      1439:        if (len < 1 + 2 + 2 || len > 256 * 1024) {
                   1440:                if ((r = sshpkt_disconnect(ssh, "Bad packet length %u",
                   1441:                    len)) != 0)
                   1442:                        return r;
                   1443:                return SSH_ERR_CONN_CORRUPT;
                   1444:        }
1.14      markus   1445:        padded_len = (len + 8) & ~7;
                   1446:
                   1447:        /* Check if the packet has been entirely received. */
1.201     markus   1448:        if (sshbuf_len(state->input) < 4 + padded_len)
                   1449:                return 0;
1.14      markus   1450:
                   1451:        /* The entire packet is in buffer. */
                   1452:
                   1453:        /* Consume packet length. */
1.201     markus   1454:        if ((r = sshbuf_consume(state->input, 4)) != 0)
                   1455:                goto out;
1.14      markus   1456:
1.62      markus   1457:        /*
                   1458:         * Cryptographic attack detector for ssh
                   1459:         * (C)1998 CORE-SDI, Buenos Aires Argentina
                   1460:         * Ariel Futoransky(futo@core-sdi.com)
                   1461:         */
1.201     markus   1462:        if (!state->receive_context.plaintext) {
1.205     djm      1463:                emsg = NULL;
1.201     markus   1464:                switch (detect_attack(&state->deattack,
                   1465:                    sshbuf_ptr(state->input), padded_len)) {
                   1466:                case DEATTACK_OK:
                   1467:                        break;
1.144     djm      1468:                case DEATTACK_DETECTED:
1.205     djm      1469:                        emsg = "crc32 compensation attack detected";
                   1470:                        break;
1.144     djm      1471:                case DEATTACK_DOS_DETECTED:
1.205     djm      1472:                        emsg = "deattack denial of service detected";
                   1473:                        break;
1.201     markus   1474:                default:
1.205     djm      1475:                        emsg = "deattack error";
                   1476:                        break;
                   1477:                }
                   1478:                if (emsg != NULL) {
                   1479:                        error("%s", emsg);
                   1480:                        if ((r = sshpkt_disconnect(ssh, "%s", emsg)) != 0 ||
                   1481:                            (r = ssh_packet_write_wait(ssh)) != 0)
                   1482:                                        return r;
                   1483:                        return SSH_ERR_CONN_CORRUPT;
1.144     djm      1484:                }
                   1485:        }
1.62      markus   1486:
                   1487:        /* Decrypt data to incoming_packet. */
1.201     markus   1488:        sshbuf_reset(state->incoming_packet);
                   1489:        if ((r = sshbuf_reserve(state->incoming_packet, padded_len, &p)) != 0)
                   1490:                goto out;
                   1491:        if ((r = cipher_crypt(&state->receive_context, 0, p,
                   1492:            sshbuf_ptr(state->input), padded_len, 0, 0)) != 0)
                   1493:                goto out;
1.62      markus   1494:
1.201     markus   1495:        if ((r = sshbuf_consume(state->input, padded_len)) != 0)
                   1496:                goto out;
1.1       deraadt  1497:
                   1498: #ifdef PACKET_DEBUG
1.14      markus   1499:        fprintf(stderr, "read_poll plain: ");
1.201     markus   1500:        sshbuf_dump(state->incoming_packet, stderr);
1.1       deraadt  1501: #endif
                   1502:
1.14      markus   1503:        /* Compute packet checksum. */
1.201     markus   1504:        checksum = ssh_crc32(sshbuf_ptr(state->incoming_packet),
                   1505:            sshbuf_len(state->incoming_packet) - 4);
1.14      markus   1506:
                   1507:        /* Skip padding. */
1.201     markus   1508:        if ((r = sshbuf_consume(state->incoming_packet, 8 - len % 8)) != 0)
                   1509:                goto out;
1.14      markus   1510:
                   1511:        /* Test check bytes. */
1.205     djm      1512:        if (len != sshbuf_len(state->incoming_packet)) {
                   1513:                error("%s: len %d != sshbuf_len %zd", __func__,
1.201     markus   1514:                    len, sshbuf_len(state->incoming_packet));
1.205     djm      1515:                if ((r = sshpkt_disconnect(ssh, "invalid packet length")) != 0 ||
                   1516:                    (r = ssh_packet_write_wait(ssh)) != 0)
                   1517:                        return r;
                   1518:                return SSH_ERR_CONN_CORRUPT;
                   1519:        }
1.14      markus   1520:
1.201     markus   1521:        cp = sshbuf_ptr(state->incoming_packet) + len - 4;
                   1522:        stored_checksum = PEEK_U32(cp);
1.205     djm      1523:        if (checksum != stored_checksum) {
                   1524:                error("Corrupted check bytes on input");
                   1525:                if ((r = sshpkt_disconnect(ssh, "connection corrupted")) != 0 ||
                   1526:                    (r = ssh_packet_write_wait(ssh)) != 0)
                   1527:                        return r;
                   1528:                return SSH_ERR_CONN_CORRUPT;
                   1529:        }
1.201     markus   1530:        if ((r = sshbuf_consume_end(state->incoming_packet, 4)) < 0)
                   1531:                goto out;
                   1532:
                   1533:        if (state->packet_compression) {
                   1534:                sshbuf_reset(state->compression_buffer);
                   1535:                if ((r = uncompress_buffer(ssh, state->incoming_packet,
                   1536:                    state->compression_buffer)) != 0)
                   1537:                        goto out;
                   1538:                sshbuf_reset(state->incoming_packet);
                   1539:                if ((r = sshbuf_putb(state->incoming_packet,
                   1540:                    state->compression_buffer)) != 0)
                   1541:                        goto out;
                   1542:        }
                   1543:        state->p_read.packets++;
                   1544:        state->p_read.bytes += padded_len + 4;
                   1545:        if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
                   1546:                goto out;
1.205     djm      1547:        if (*typep < SSH_MSG_MIN || *typep > SSH_MSG_MAX) {
                   1548:                error("Invalid ssh1 packet type: %d", *typep);
                   1549:                if ((r = sshpkt_disconnect(ssh, "invalid packet type")) != 0 ||
                   1550:                    (r = ssh_packet_write_wait(ssh)) != 0)
                   1551:                        return r;
                   1552:                return SSH_ERR_PROTOCOL_ERROR;
                   1553:        }
1.201     markus   1554:        r = 0;
                   1555:  out:
                   1556:        return r;
1.1       deraadt  1557: }
1.14      markus   1558:
1.201     markus   1559: int
                   1560: ssh_packet_read_poll2(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
1.25      markus   1561: {
1.201     markus   1562:        struct session_state *state = ssh->state;
1.40      markus   1563:        u_int padlen, need;
1.201     markus   1564:        u_char *cp, macbuf[SSH_DIGEST_MAX_LENGTH];
                   1565:        u_int maclen, aadlen = 0, authlen = 0, block_size;
                   1566:        struct sshenc *enc   = NULL;
                   1567:        struct sshmac *mac   = NULL;
                   1568:        struct sshcomp *comp = NULL;
1.200     markus   1569:        int r;
1.201     markus   1570:
                   1571:        *typep = SSH_MSG_NONE;
                   1572:
                   1573:        if (state->packet_discard)
                   1574:                return 0;
                   1575:
                   1576:        if (state->newkeys[MODE_IN] != NULL) {
                   1577:                enc  = &state->newkeys[MODE_IN]->enc;
                   1578:                mac  = &state->newkeys[MODE_IN]->mac;
                   1579:                comp = &state->newkeys[MODE_IN]->comp;
1.180     markus   1580:                /* disable mac for authenticated encryption */
                   1581:                if ((authlen = cipher_authlen(enc->cipher)) != 0)
                   1582:                        mac = NULL;
1.25      markus   1583:        }
                   1584:        maclen = mac && mac->enabled ? mac->mac_len : 0;
1.88      markus   1585:        block_size = enc ? enc->block_size : 8;
1.180     markus   1586:        aadlen = (mac && mac->enabled && mac->etm) || authlen ? 4 : 0;
1.25      markus   1587:
1.201     markus   1588:        if (aadlen && state->packlen == 0) {
                   1589:                if (cipher_get_length(&state->receive_context,
                   1590:                    &state->packlen, state->p_read.seqnr,
                   1591:                    sshbuf_ptr(state->input), sshbuf_len(state->input)) != 0)
                   1592:                        return 0;
                   1593:                if (state->packlen < 1 + 4 ||
                   1594:                    state->packlen > PACKET_MAX_SIZE) {
1.178     markus   1595: #ifdef PACKET_DEBUG
1.201     markus   1596:                        sshbuf_dump(state->input, stderr);
1.178     markus   1597: #endif
1.201     markus   1598:                        logit("Bad packet length %u.", state->packlen);
                   1599:                        if ((r = sshpkt_disconnect(ssh, "Packet corrupt")) != 0)
                   1600:                                return r;
1.217     djm      1601:                        return SSH_ERR_CONN_CORRUPT;
1.178     markus   1602:                }
1.201     markus   1603:                sshbuf_reset(state->incoming_packet);
                   1604:        } else if (state->packlen == 0) {
1.25      markus   1605:                /*
                   1606:                 * check if input size is less than the cipher block size,
                   1607:                 * decrypt first block and extract length of incoming packet
                   1608:                 */
1.201     markus   1609:                if (sshbuf_len(state->input) < block_size)
                   1610:                        return 0;
                   1611:                sshbuf_reset(state->incoming_packet);
                   1612:                if ((r = sshbuf_reserve(state->incoming_packet, block_size,
                   1613:                    &cp)) != 0)
                   1614:                        goto out;
                   1615:                if ((r = cipher_crypt(&state->receive_context,
                   1616:                    state->p_send.seqnr, cp, sshbuf_ptr(state->input),
                   1617:                    block_size, 0, 0)) != 0)
                   1618:                        goto out;
                   1619:                state->packlen = PEEK_U32(sshbuf_ptr(state->incoming_packet));
                   1620:                if (state->packlen < 1 + 4 ||
                   1621:                    state->packlen > PACKET_MAX_SIZE) {
1.110     markus   1622: #ifdef PACKET_DEBUG
1.201     markus   1623:                        fprintf(stderr, "input: \n");
                   1624:                        sshbuf_dump(state->input, stderr);
                   1625:                        fprintf(stderr, "incoming_packet: \n");
                   1626:                        sshbuf_dump(state->incoming_packet, stderr);
1.110     markus   1627: #endif
1.201     markus   1628:                        logit("Bad packet length %u.", state->packlen);
                   1629:                        return ssh_packet_start_discard(ssh, enc, mac,
                   1630:                            state->packlen, PACKET_MAX_SIZE);
1.25      markus   1631:                }
1.201     markus   1632:                if ((r = sshbuf_consume(state->input, block_size)) != 0)
                   1633:                        goto out;
1.25      markus   1634:        }
1.201     markus   1635:        DBG(debug("input: packet len %u", state->packlen+4));
                   1636:
1.178     markus   1637:        if (aadlen) {
                   1638:                /* only the payload is encrypted */
1.201     markus   1639:                need = state->packlen;
1.178     markus   1640:        } else {
                   1641:                /*
                   1642:                 * the payload size and the payload are encrypted, but we
                   1643:                 * have a partial packet of block_size bytes
                   1644:                 */
1.201     markus   1645:                need = 4 + state->packlen - block_size;
1.178     markus   1646:        }
1.180     markus   1647:        DBG(debug("partial packet: block %d, need %d, maclen %d, authlen %d,"
                   1648:            " aadlen %d", block_size, need, maclen, authlen, aadlen));
1.158     markus   1649:        if (need % block_size != 0) {
                   1650:                logit("padding error: need %d block %d mod %d",
1.25      markus   1651:                    need, block_size, need % block_size);
1.201     markus   1652:                return ssh_packet_start_discard(ssh, enc, mac,
                   1653:                    state->packlen, PACKET_MAX_SIZE - block_size);
1.158     markus   1654:        }
1.25      markus   1655:        /*
                   1656:         * check if the entire packet has been received and
1.178     markus   1657:         * decrypt into incoming_packet:
                   1658:         * 'aadlen' bytes are unencrypted, but authenticated.
1.180     markus   1659:         * 'need' bytes are encrypted, followed by either
                   1660:         * 'authlen' bytes of authentication tag or
1.178     markus   1661:         * 'maclen' bytes of message authentication code.
1.25      markus   1662:         */
1.201     markus   1663:        if (sshbuf_len(state->input) < aadlen + need + authlen + maclen)
                   1664:                return 0;
1.25      markus   1665: #ifdef PACKET_DEBUG
                   1666:        fprintf(stderr, "read_poll enc/full: ");
1.201     markus   1667:        sshbuf_dump(state->input, stderr);
1.25      markus   1668: #endif
1.178     markus   1669:        /* EtM: compute mac over encrypted input */
1.201     markus   1670:        if (mac && mac->enabled && mac->etm) {
                   1671:                if ((r = mac_compute(mac, state->p_read.seqnr,
                   1672:                    sshbuf_ptr(state->input), aadlen + need,
1.200     markus   1673:                    macbuf, sizeof(macbuf))) != 0)
1.201     markus   1674:                        goto out;
                   1675:        }
                   1676:        if ((r = sshbuf_reserve(state->incoming_packet, aadlen + need,
                   1677:            &cp)) != 0)
                   1678:                goto out;
                   1679:        if ((r = cipher_crypt(&state->receive_context, state->p_read.seqnr, cp,
                   1680:            sshbuf_ptr(state->input), need, aadlen, authlen)) != 0)
                   1681:                goto out;
                   1682:        if ((r = sshbuf_consume(state->input, aadlen + need + authlen)) != 0)
                   1683:                goto out;
1.25      markus   1684:        /*
                   1685:         * compute MAC over seqnr and packet,
                   1686:         * increment sequence number for incoming packet
                   1687:         */
1.29      markus   1688:        if (mac && mac->enabled) {
1.178     markus   1689:                if (!mac->etm)
1.201     markus   1690:                        if ((r = mac_compute(mac, state->p_read.seqnr,
                   1691:                            sshbuf_ptr(state->incoming_packet),
                   1692:                            sshbuf_len(state->incoming_packet),
1.200     markus   1693:                            macbuf, sizeof(macbuf))) != 0)
1.201     markus   1694:                                goto out;
                   1695:                if (timingsafe_bcmp(macbuf, sshbuf_ptr(state->input),
1.161     andreas  1696:                    mac->mac_len) != 0) {
1.159     markus   1697:                        logit("Corrupted MAC on input.");
                   1698:                        if (need > PACKET_MAX_SIZE)
1.201     markus   1699:                                return SSH_ERR_INTERNAL_ERROR;
                   1700:                        return ssh_packet_start_discard(ssh, enc, mac,
                   1701:                            state->packlen, PACKET_MAX_SIZE - need);
                   1702:                }
                   1703:
                   1704:                DBG(debug("MAC #%d ok", state->p_read.seqnr));
                   1705:                if ((r = sshbuf_consume(state->input, mac->mac_len)) != 0)
                   1706:                        goto out;
1.25      markus   1707:        }
1.77      djm      1708:        if (seqnr_p != NULL)
1.201     markus   1709:                *seqnr_p = state->p_read.seqnr;
                   1710:        if (++state->p_read.seqnr == 0)
1.106     itojun   1711:                logit("incoming seqnr wraps around");
1.201     markus   1712:        if (++state->p_read.packets == 0)
                   1713:                if (!(ssh->compat & SSH_BUG_NOREKEY))
                   1714:                        return SSH_ERR_NEED_REKEY;
                   1715:        state->p_read.blocks += (state->packlen + 4) / block_size;
                   1716:        state->p_read.bytes += state->packlen + 4;
1.25      markus   1717:
                   1718:        /* get padlen */
1.201     markus   1719:        padlen = sshbuf_ptr(state->incoming_packet)[4];
1.25      markus   1720:        DBG(debug("input: padlen %d", padlen));
1.205     djm      1721:        if (padlen < 4) {
                   1722:                if ((r = sshpkt_disconnect(ssh,
                   1723:                    "Corrupted padlen %d on input.", padlen)) != 0 ||
                   1724:                    (r = ssh_packet_write_wait(ssh)) != 0)
                   1725:                        return r;
                   1726:                return SSH_ERR_CONN_CORRUPT;
                   1727:        }
1.25      markus   1728:
                   1729:        /* skip packet size + padlen, discard padding */
1.201     markus   1730:        if ((r = sshbuf_consume(state->incoming_packet, 4 + 1)) != 0 ||
                   1731:            ((r = sshbuf_consume_end(state->incoming_packet, padlen)) != 0))
                   1732:                goto out;
1.25      markus   1733:
1.201     markus   1734:        DBG(debug("input: len before de-compress %zd",
                   1735:            sshbuf_len(state->incoming_packet)));
1.25      markus   1736:        if (comp && comp->enabled) {
1.201     markus   1737:                sshbuf_reset(state->compression_buffer);
                   1738:                if ((r = uncompress_buffer(ssh, state->incoming_packet,
                   1739:                    state->compression_buffer)) != 0)
                   1740:                        goto out;
                   1741:                sshbuf_reset(state->incoming_packet);
                   1742:                if ((r = sshbuf_putb(state->incoming_packet,
                   1743:                    state->compression_buffer)) != 0)
                   1744:                        goto out;
                   1745:                DBG(debug("input: len after de-compress %zd",
                   1746:                    sshbuf_len(state->incoming_packet)));
1.25      markus   1747:        }
                   1748:        /*
                   1749:         * get packet type, implies consume.
                   1750:         * return length of payload (without type field)
                   1751:         */
1.201     markus   1752:        if ((r = sshbuf_get_u8(state->incoming_packet, typep)) != 0)
                   1753:                goto out;
1.226     djm      1754:        if (ssh_packet_log_type(*typep))
                   1755:                debug3("receive packet: type %u", *typep);
1.205     djm      1756:        if (*typep < SSH2_MSG_MIN || *typep >= SSH2_MSG_LOCAL_MIN) {
                   1757:                if ((r = sshpkt_disconnect(ssh,
                   1758:                    "Invalid ssh2 packet type: %d", *typep)) != 0 ||
                   1759:                    (r = ssh_packet_write_wait(ssh)) != 0)
                   1760:                        return r;
                   1761:                return SSH_ERR_PROTOCOL_ERROR;
                   1762:        }
1.201     markus   1763:        if (*typep == SSH2_MSG_NEWKEYS)
                   1764:                r = ssh_set_newkeys(ssh, MODE_IN);
                   1765:        else if (*typep == SSH2_MSG_USERAUTH_SUCCESS && !state->server_side)
                   1766:                r = ssh_packet_enable_delayed_compress(ssh);
                   1767:        else
                   1768:                r = 0;
1.25      markus   1769: #ifdef PACKET_DEBUG
1.201     markus   1770:        fprintf(stderr, "read/plain[%d]:\r\n", *typep);
                   1771:        sshbuf_dump(state->incoming_packet, stderr);
1.25      markus   1772: #endif
1.62      markus   1773:        /* reset for next packet */
1.201     markus   1774:        state->packlen = 0;
                   1775:  out:
                   1776:        return r;
1.25      markus   1777: }
                   1778:
                   1779: int
1.201     markus   1780: ssh_packet_read_poll_seqnr(struct ssh *ssh, u_char *typep, u_int32_t *seqnr_p)
1.25      markus   1781: {
1.201     markus   1782:        struct session_state *state = ssh->state;
1.96      deraadt  1783:        u_int reason, seqnr;
1.201     markus   1784:        int r;
                   1785:        u_char *msg;
1.62      markus   1786:
1.25      markus   1787:        for (;;) {
1.201     markus   1788:                msg = NULL;
1.62      markus   1789:                if (compat20) {
1.201     markus   1790:                        r = ssh_packet_read_poll2(ssh, typep, seqnr_p);
                   1791:                        if (r != 0)
                   1792:                                return r;
                   1793:                        if (*typep) {
                   1794:                                state->keep_alive_timeouts = 0;
                   1795:                                DBG(debug("received packet type %d", *typep));
1.153     djm      1796:                        }
1.201     markus   1797:                        switch (*typep) {
1.150     dtucker  1798:                        case SSH2_MSG_IGNORE:
1.151     dtucker  1799:                                debug3("Received SSH2_MSG_IGNORE");
1.150     dtucker  1800:                                break;
1.25      markus   1801:                        case SSH2_MSG_DEBUG:
1.201     markus   1802:                                if ((r = sshpkt_get_u8(ssh, NULL)) != 0 ||
                   1803:                                    (r = sshpkt_get_string(ssh, &msg, NULL)) != 0 ||
                   1804:                                    (r = sshpkt_get_string(ssh, NULL, NULL)) != 0) {
1.219     mmcc     1805:                                        free(msg);
1.201     markus   1806:                                        return r;
                   1807:                                }
1.25      markus   1808:                                debug("Remote: %.900s", msg);
1.186     djm      1809:                                free(msg);
1.25      markus   1810:                                break;
                   1811:                        case SSH2_MSG_DISCONNECT:
1.201     markus   1812:                                if ((r = sshpkt_get_u32(ssh, &reason)) != 0 ||
                   1813:                                    (r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
                   1814:                                        return r;
1.182     djm      1815:                                /* Ignore normal client exit notifications */
1.201     markus   1816:                                do_log2(ssh->state->server_side &&
1.182     djm      1817:                                    reason == SSH2_DISCONNECT_BY_APPLICATION ?
                   1818:                                    SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_ERROR,
1.220     djm      1819:                                    "Received disconnect from %s port %d:"
                   1820:                                    "%u: %.400s", ssh_remote_ipaddr(ssh),
                   1821:                                    ssh_remote_port(ssh), reason, msg);
1.186     djm      1822:                                free(msg);
1.201     markus   1823:                                return SSH_ERR_DISCONNECTED;
1.84      markus   1824:                        case SSH2_MSG_UNIMPLEMENTED:
1.201     markus   1825:                                if ((r = sshpkt_get_u32(ssh, &seqnr)) != 0)
                   1826:                                        return r;
1.96      deraadt  1827:                                debug("Received SSH2_MSG_UNIMPLEMENTED for %u",
                   1828:                                    seqnr);
1.150     dtucker  1829:                                break;
1.25      markus   1830:                        default:
1.201     markus   1831:                                return 0;
1.48      stevesk  1832:                        }
1.25      markus   1833:                } else {
1.201     markus   1834:                        r = ssh_packet_read_poll1(ssh, typep);
                   1835:                        switch (*typep) {
1.188     djm      1836:                        case SSH_MSG_NONE:
                   1837:                                return SSH_MSG_NONE;
1.25      markus   1838:                        case SSH_MSG_IGNORE:
                   1839:                                break;
                   1840:                        case SSH_MSG_DEBUG:
1.201     markus   1841:                                if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
                   1842:                                        return r;
1.25      markus   1843:                                debug("Remote: %.900s", msg);
1.186     djm      1844:                                free(msg);
1.25      markus   1845:                                break;
                   1846:                        case SSH_MSG_DISCONNECT:
1.201     markus   1847:                                if ((r = sshpkt_get_string(ssh, &msg, NULL)) != 0)
                   1848:                                        return r;
1.220     djm      1849:                                error("Received disconnect from %s port %d: "
                   1850:                                    "%.400s", ssh_remote_ipaddr(ssh),
                   1851:                                    ssh_remote_port(ssh), msg);
1.201     markus   1852:                                free(msg);
                   1853:                                return SSH_ERR_DISCONNECTED;
1.25      markus   1854:                        default:
1.201     markus   1855:                                DBG(debug("received packet type %d", *typep));
                   1856:                                return 0;
1.48      stevesk  1857:                        }
1.25      markus   1858:                }
                   1859:        }
                   1860: }
                   1861:
1.16      markus   1862: /*
                   1863:  * Buffers the given amount of input characters.  This is intended to be used
                   1864:  * together with packet_read_poll.
                   1865:  */
1.1       deraadt  1866:
1.204     djm      1867: int
1.201     markus   1868: ssh_packet_process_incoming(struct ssh *ssh, const char *buf, u_int len)
1.1       deraadt  1869: {
1.201     markus   1870:        struct session_state *state = ssh->state;
                   1871:        int r;
                   1872:
                   1873:        if (state->packet_discard) {
                   1874:                state->keep_alive_timeouts = 0; /* ?? */
                   1875:                if (len >= state->packet_discard) {
                   1876:                        if ((r = ssh_packet_stop_discard(ssh)) != 0)
1.204     djm      1877:                                return r;
1.201     markus   1878:                }
                   1879:                state->packet_discard -= len;
1.204     djm      1880:                return 0;
1.159     markus   1881:        }
1.201     markus   1882:        if ((r = sshbuf_put(ssh->state->input, buf, len)) != 0)
1.204     djm      1883:                return r;
                   1884:
                   1885:        return 0;
1.28      markus   1886: }
                   1887:
                   1888: int
1.201     markus   1889: ssh_packet_remaining(struct ssh *ssh)
1.28      markus   1890: {
1.201     markus   1891:        return sshbuf_len(ssh->state->incoming_packet);
1.1       deraadt  1892: }
                   1893:
1.16      markus   1894: /*
                   1895:  * Sends a diagnostic message from the server to the client.  This message
                   1896:  * can be sent at any time (but not while constructing another message). The
                   1897:  * message is printed immediately, but only if the client is being executed
                   1898:  * in verbose mode.  These messages are primarily intended to ease debugging
                   1899:  * authentication problems.   The length of the formatted message must not
1.205     djm      1900:  * exceed 1024 bytes.  This will automatically call ssh_packet_write_wait.
1.16      markus   1901:  */
1.2       provos   1902: void
1.201     markus   1903: ssh_packet_send_debug(struct ssh *ssh, const char *fmt,...)
1.1       deraadt  1904: {
1.14      markus   1905:        char buf[1024];
                   1906:        va_list args;
1.201     markus   1907:        int r;
1.39      markus   1908:
1.201     markus   1909:        if (compat20 && (ssh->compat & SSH_BUG_DEBUG))
1.39      markus   1910:                return;
1.14      markus   1911:
                   1912:        va_start(args, fmt);
                   1913:        vsnprintf(buf, sizeof(buf), fmt, args);
                   1914:        va_end(args);
                   1915:
1.30      markus   1916:        if (compat20) {
1.201     markus   1917:                if ((r = sshpkt_start(ssh, SSH2_MSG_DEBUG)) != 0 ||
                   1918:                    (r = sshpkt_put_u8(ssh, 0)) != 0 || /* always display */
                   1919:                    (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
                   1920:                    (r = sshpkt_put_cstring(ssh, "")) != 0 ||
                   1921:                    (r = sshpkt_send(ssh)) != 0)
                   1922:                        fatal("%s: %s", __func__, ssh_err(r));
1.30      markus   1923:        } else {
1.201     markus   1924:                if ((r = sshpkt_start(ssh, SSH_MSG_DEBUG)) != 0 ||
                   1925:                    (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
                   1926:                    (r = sshpkt_send(ssh)) != 0)
                   1927:                        fatal("%s: %s", __func__, ssh_err(r));
1.30      markus   1928:        }
1.205     djm      1929:        if ((r = ssh_packet_write_wait(ssh)) != 0)
                   1930:                fatal("%s: %s", __func__, ssh_err(r));
                   1931: }
                   1932:
                   1933: /*
                   1934:  * Pretty-print connection-terminating errors and exit.
                   1935:  */
                   1936: void
                   1937: sshpkt_fatal(struct ssh *ssh, const char *tag, int r)
                   1938: {
                   1939:        switch (r) {
                   1940:        case SSH_ERR_CONN_CLOSED:
1.220     djm      1941:                logit("Connection closed by %.200s port %d",
                   1942:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.205     djm      1943:                cleanup_exit(255);
                   1944:        case SSH_ERR_CONN_TIMEOUT:
1.220     djm      1945:                logit("Connection %s %.200s port %d timed out",
                   1946:                    ssh->state->server_side ? "from" : "to",
                   1947:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.205     djm      1948:                cleanup_exit(255);
1.212     djm      1949:        case SSH_ERR_DISCONNECTED:
1.220     djm      1950:                logit("Disconnected from %.200s port %d",
                   1951:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.212     djm      1952:                cleanup_exit(255);
                   1953:        case SSH_ERR_SYSTEM_ERROR:
                   1954:                if (errno == ECONNRESET) {
1.220     djm      1955:                        logit("Connection reset by %.200s port %d",
                   1956:                            ssh_remote_ipaddr(ssh), ssh_remote_port(ssh));
1.212     djm      1957:                        cleanup_exit(255);
1.213     djm      1958:                }
                   1959:                /* FALLTHROUGH */
                   1960:        case SSH_ERR_NO_CIPHER_ALG_MATCH:
                   1961:        case SSH_ERR_NO_MAC_ALG_MATCH:
                   1962:        case SSH_ERR_NO_COMPRESS_ALG_MATCH:
                   1963:        case SSH_ERR_NO_KEX_ALG_MATCH:
                   1964:        case SSH_ERR_NO_HOSTKEY_ALG_MATCH:
                   1965:                if (ssh && ssh->kex && ssh->kex->failed_choice) {
1.220     djm      1966:                        fatal("Unable to negotiate with %.200s port %d: %s. "
1.213     djm      1967:                            "Their offer: %s", ssh_remote_ipaddr(ssh),
1.220     djm      1968:                            ssh_remote_port(ssh), ssh_err(r),
                   1969:                            ssh->kex->failed_choice);
1.212     djm      1970:                }
                   1971:                /* FALLTHROUGH */
1.205     djm      1972:        default:
1.220     djm      1973:                fatal("%s%sConnection %s %.200s port %d: %s",
1.205     djm      1974:                    tag != NULL ? tag : "", tag != NULL ? ": " : "",
1.220     djm      1975:                    ssh->state->server_side ? "from" : "to",
                   1976:                    ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), ssh_err(r));
1.205     djm      1977:        }
1.1       deraadt  1978: }
                   1979:
1.16      markus   1980: /*
                   1981:  * Logs the error plus constructs and sends a disconnect packet, closes the
                   1982:  * connection, and exits.  This function never returns. The error message
                   1983:  * should not contain a newline.  The length of the formatted message must
                   1984:  * not exceed 1024 bytes.
                   1985:  */
1.2       provos   1986: void
1.201     markus   1987: ssh_packet_disconnect(struct ssh *ssh, const char *fmt,...)
1.1       deraadt  1988: {
1.14      markus   1989:        char buf[1024];
                   1990:        va_list args;
                   1991:        static int disconnecting = 0;
1.201     markus   1992:        int r;
1.97      deraadt  1993:
1.14      markus   1994:        if (disconnecting)      /* Guard against recursive invocations. */
                   1995:                fatal("packet_disconnect called recursively.");
                   1996:        disconnecting = 1;
                   1997:
1.16      markus   1998:        /*
                   1999:         * Format the message.  Note that the caller must make sure the
                   2000:         * message is of limited size.
                   2001:         */
1.14      markus   2002:        va_start(args, fmt);
                   2003:        vsnprintf(buf, sizeof(buf), fmt, args);
                   2004:        va_end(args);
                   2005:
1.99      markus   2006:        /* Display the error locally */
1.106     itojun   2007:        logit("Disconnecting: %.100s", buf);
1.99      markus   2008:
1.205     djm      2009:        /*
                   2010:         * Send the disconnect message to the other side, and wait
                   2011:         * for it to get sent.
                   2012:         */
                   2013:        if ((r = sshpkt_disconnect(ssh, "%s", buf)) != 0)
                   2014:                sshpkt_fatal(ssh, __func__, r);
                   2015:
                   2016:        if ((r = ssh_packet_write_wait(ssh)) != 0)
                   2017:                sshpkt_fatal(ssh, __func__, r);
1.14      markus   2018:
                   2019:        /* Close the connection. */
1.201     markus   2020:        ssh_packet_close(ssh);
1.112     markus   2021:        cleanup_exit(255);
1.1       deraadt  2022: }
                   2023:
1.205     djm      2024: /*
                   2025:  * Checks if there is any buffered output, and tries to write some of
                   2026:  * the output.
                   2027:  */
                   2028: int
1.201     markus   2029: ssh_packet_write_poll(struct ssh *ssh)
1.1       deraadt  2030: {
1.201     markus   2031:        struct session_state *state = ssh->state;
                   2032:        int len = sshbuf_len(state->output);
1.222     markus   2033:        int r;
1.97      deraadt  2034:
1.14      markus   2035:        if (len > 0) {
1.222     markus   2036:                len = write(state->connection_out,
                   2037:                    sshbuf_ptr(state->output), len);
1.156     djm      2038:                if (len == -1) {
                   2039:                        if (errno == EINTR || errno == EAGAIN)
1.205     djm      2040:                                return 0;
                   2041:                        return SSH_ERR_SYSTEM_ERROR;
1.14      markus   2042:                }
1.222     markus   2043:                if (len == 0)
1.205     djm      2044:                        return SSH_ERR_CONN_CLOSED;
1.201     markus   2045:                if ((r = sshbuf_consume(state->output, len)) != 0)
1.205     djm      2046:                        return r;
1.14      markus   2047:        }
1.205     djm      2048:        return 0;
1.1       deraadt  2049: }
                   2050:
1.16      markus   2051: /*
                   2052:  * Calls packet_write_poll repeatedly until all pending output data has been
                   2053:  * written.
                   2054:  */
1.205     djm      2055: int
1.201     markus   2056: ssh_packet_write_wait(struct ssh *ssh)
1.1       deraadt  2057: {
1.56      millert  2058:        fd_set *setp;
1.205     djm      2059:        int ret, r, ms_remain = 0;
1.154     dtucker  2060:        struct timeval start, timeout, *timeoutp = NULL;
1.201     markus   2061:        struct session_state *state = ssh->state;
1.56      millert  2062:
1.214     deraadt  2063:        setp = calloc(howmany(state->connection_out + 1,
1.161     andreas  2064:            NFDBITS), sizeof(fd_mask));
1.201     markus   2065:        if (setp == NULL)
1.205     djm      2066:                return SSH_ERR_ALLOC_FAIL;
1.216     gsoares  2067:        if ((r = ssh_packet_write_poll(ssh)) != 0) {
                   2068:                free(setp);
1.215     djm      2069:                return r;
1.216     gsoares  2070:        }
1.201     markus   2071:        while (ssh_packet_have_data_to_write(ssh)) {
                   2072:                memset(setp, 0, howmany(state->connection_out + 1,
1.161     andreas  2073:                    NFDBITS) * sizeof(fd_mask));
1.201     markus   2074:                FD_SET(state->connection_out, setp);
1.154     dtucker  2075:
1.201     markus   2076:                if (state->packet_timeout_ms > 0) {
                   2077:                        ms_remain = state->packet_timeout_ms;
1.154     dtucker  2078:                        timeoutp = &timeout;
                   2079:                }
                   2080:                for (;;) {
1.201     markus   2081:                        if (state->packet_timeout_ms != -1) {
1.154     dtucker  2082:                                ms_to_timeval(&timeout, ms_remain);
                   2083:                                gettimeofday(&start, NULL);
                   2084:                        }
1.201     markus   2085:                        if ((ret = select(state->connection_out + 1,
1.161     andreas  2086:                            NULL, setp, NULL, timeoutp)) >= 0)
1.154     dtucker  2087:                                break;
1.161     andreas  2088:                        if (errno != EAGAIN && errno != EINTR)
1.154     dtucker  2089:                                break;
1.201     markus   2090:                        if (state->packet_timeout_ms == -1)
1.154     dtucker  2091:                                continue;
                   2092:                        ms_subtract_diff(&start, &ms_remain);
                   2093:                        if (ms_remain <= 0) {
                   2094:                                ret = 0;
                   2095:                                break;
                   2096:                        }
                   2097:                }
                   2098:                if (ret == 0) {
1.205     djm      2099:                        free(setp);
                   2100:                        return SSH_ERR_CONN_TIMEOUT;
                   2101:                }
                   2102:                if ((r = ssh_packet_write_poll(ssh)) != 0) {
                   2103:                        free(setp);
                   2104:                        return r;
1.154     dtucker  2105:                }
1.14      markus   2106:        }
1.186     djm      2107:        free(setp);
1.205     djm      2108:        return 0;
1.1       deraadt  2109: }
                   2110:
                   2111: /* Returns true if there is buffered data to write to the connection. */
                   2112:
1.2       provos   2113: int
1.201     markus   2114: ssh_packet_have_data_to_write(struct ssh *ssh)
1.1       deraadt  2115: {
1.201     markus   2116:        return sshbuf_len(ssh->state->output) != 0;
1.1       deraadt  2117: }
                   2118:
                   2119: /* Returns true if there is not too much data to write to the connection. */
                   2120:
1.2       provos   2121: int
1.201     markus   2122: ssh_packet_not_very_much_data_to_write(struct ssh *ssh)
1.1       deraadt  2123: {
1.201     markus   2124:        if (ssh->state->interactive_mode)
                   2125:                return sshbuf_len(ssh->state->output) < 16384;
1.14      markus   2126:        else
1.201     markus   2127:                return sshbuf_len(ssh->state->output) < 128 * 1024;
1.1       deraadt  2128: }
                   2129:
1.201     markus   2130: void
                   2131: ssh_packet_set_tos(struct ssh *ssh, int tos)
1.101     markus   2132: {
1.201     markus   2133:        if (!ssh_packet_connection_is_on_socket(ssh))
1.101     markus   2134:                return;
1.201     markus   2135:        switch (ssh_packet_connection_af(ssh)) {
1.173     djm      2136:        case AF_INET:
                   2137:                debug3("%s: set IP_TOS 0x%02x", __func__, tos);
1.201     markus   2138:                if (setsockopt(ssh->state->connection_in,
1.173     djm      2139:                    IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0)
                   2140:                        error("setsockopt IP_TOS %d: %.100s:",
                   2141:                            tos, strerror(errno));
                   2142:                break;
                   2143:        case AF_INET6:
                   2144:                debug3("%s: set IPV6_TCLASS 0x%02x", __func__, tos);
1.201     markus   2145:                if (setsockopt(ssh->state->connection_in,
1.173     djm      2146:                    IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos)) < 0)
                   2147:                        error("setsockopt IPV6_TCLASS %d: %.100s:",
                   2148:                            tos, strerror(errno));
                   2149:                break;
                   2150:        }
1.101     markus   2151: }
                   2152:
1.1       deraadt  2153: /* Informs that the current session is interactive.  Sets IP flags for that. */
                   2154:
1.2       provos   2155: void
1.201     markus   2156: ssh_packet_set_interactive(struct ssh *ssh, int interactive, int qos_interactive, int qos_bulk)
1.1       deraadt  2157: {
1.201     markus   2158:        struct session_state *state = ssh->state;
                   2159:
                   2160:        if (state->set_interactive_called)
1.43      markus   2161:                return;
1.201     markus   2162:        state->set_interactive_called = 1;
1.1       deraadt  2163:
1.14      markus   2164:        /* Record that we are in interactive mode. */
1.201     markus   2165:        state->interactive_mode = interactive;
1.1       deraadt  2166:
1.19      markus   2167:        /* Only set socket options if using a socket.  */
1.201     markus   2168:        if (!ssh_packet_connection_is_on_socket(ssh))
1.14      markus   2169:                return;
1.201     markus   2170:        set_nodelay(state->connection_in);
                   2171:        ssh_packet_set_tos(ssh, interactive ? qos_interactive :
                   2172:            qos_bulk);
1.1       deraadt  2173: }
                   2174:
                   2175: /* Returns true if the current connection is interactive. */
                   2176:
1.2       provos   2177: int
1.201     markus   2178: ssh_packet_is_interactive(struct ssh *ssh)
1.1       deraadt  2179: {
1.201     markus   2180:        return ssh->state->interactive_mode;
1.12      markus   2181: }
                   2182:
1.113     deraadt  2183: int
1.201     markus   2184: ssh_packet_set_maxsize(struct ssh *ssh, u_int s)
1.12      markus   2185: {
1.201     markus   2186:        struct session_state *state = ssh->state;
                   2187:
                   2188:        if (state->set_maxsize_called) {
1.106     itojun   2189:                logit("packet_set_maxsize: called twice: old %d new %d",
1.201     markus   2190:                    state->max_packet_size, s);
1.14      markus   2191:                return -1;
                   2192:        }
                   2193:        if (s < 4 * 1024 || s > 1024 * 1024) {
1.106     itojun   2194:                logit("packet_set_maxsize: bad size %d", s);
1.14      markus   2195:                return -1;
                   2196:        }
1.201     markus   2197:        state->set_maxsize_called = 1;
1.66      markus   2198:        debug("packet_set_maxsize: setting to %d", s);
1.201     markus   2199:        state->max_packet_size = s;
1.14      markus   2200:        return s;
1.53      markus   2201: }
                   2202:
1.161     andreas  2203: int
1.201     markus   2204: ssh_packet_inc_alive_timeouts(struct ssh *ssh)
1.161     andreas  2205: {
1.201     markus   2206:        return ++ssh->state->keep_alive_timeouts;
1.161     andreas  2207: }
                   2208:
                   2209: void
1.201     markus   2210: ssh_packet_set_alive_timeouts(struct ssh *ssh, int ka)
1.161     andreas  2211: {
1.201     markus   2212:        ssh->state->keep_alive_timeouts = ka;
1.161     andreas  2213: }
                   2214:
                   2215: u_int
1.201     markus   2216: ssh_packet_get_maxsize(struct ssh *ssh)
1.71      markus   2217: {
1.201     markus   2218:        return ssh->state->max_packet_size;
1.71      markus   2219: }
                   2220:
1.53      markus   2221: /*
                   2222:  * 9.2.  Ignored Data Message
1.61      markus   2223:  *
1.53      markus   2224:  *   byte      SSH_MSG_IGNORE
                   2225:  *   string    data
1.61      markus   2226:  *
1.53      markus   2227:  * All implementations MUST understand (and ignore) this message at any
                   2228:  * time (after receiving the protocol version). No implementation is
                   2229:  * required to send them. This message can be used as an additional
                   2230:  * protection measure against advanced traffic analysis techniques.
                   2231:  */
1.54      markus   2232: void
1.201     markus   2233: ssh_packet_send_ignore(struct ssh *ssh, int nbytes)
1.54      markus   2234: {
1.115     avsm     2235:        u_int32_t rnd = 0;
1.201     markus   2236:        int r, i;
1.54      markus   2237:
1.201     markus   2238:        if ((r = sshpkt_start(ssh, compat20 ?
                   2239:            SSH2_MSG_IGNORE : SSH_MSG_IGNORE)) != 0 ||
                   2240:            (r = sshpkt_put_u32(ssh, nbytes)) != 0)
                   2241:                fatal("%s: %s", __func__, ssh_err(r));
1.75      deraadt  2242:        for (i = 0; i < nbytes; i++) {
1.53      markus   2243:                if (i % 4 == 0)
1.115     avsm     2244:                        rnd = arc4random();
1.201     markus   2245:                if ((r = sshpkt_put_u8(ssh, (u_char)rnd & 0xff)) != 0)
                   2246:                        fatal("%s: %s", __func__, ssh_err(r));
1.115     avsm     2247:                rnd >>= 8;
1.53      markus   2248:        }
1.105     markus   2249: }
                   2250:
1.113     deraadt  2251: #define MAX_PACKETS    (1U<<31)
1.105     markus   2252: int
1.201     markus   2253: ssh_packet_need_rekeying(struct ssh *ssh)
1.105     markus   2254: {
1.201     markus   2255:        struct session_state *state = ssh->state;
                   2256:
                   2257:        if (ssh->compat & SSH_BUG_NOREKEY)
1.105     markus   2258:                return 0;
                   2259:        return
1.201     markus   2260:            (state->p_send.packets > MAX_PACKETS) ||
                   2261:            (state->p_read.packets > MAX_PACKETS) ||
                   2262:            (state->max_blocks_out &&
1.225     dtucker  2263:                (state->p_send.blocks > state->max_blocks_out)) ||
1.201     markus   2264:            (state->max_blocks_in &&
1.225     dtucker  2265:                (state->p_read.blocks > state->max_blocks_in)) ||
1.201     markus   2266:            (state->rekey_interval != 0 && state->rekey_time +
                   2267:                 state->rekey_interval <= monotime());
1.105     markus   2268: }
                   2269:
                   2270: void
1.224     dtucker  2271: ssh_packet_set_rekey_limits(struct ssh *ssh, u_int64_t bytes, time_t seconds)
1.105     markus   2272: {
1.224     dtucker  2273:        debug3("rekey after %llu bytes, %d seconds", (unsigned long long)bytes,
1.184     dtucker  2274:            (int)seconds);
1.201     markus   2275:        ssh->state->rekey_limit = bytes;
                   2276:        ssh->state->rekey_interval = seconds;
1.184     dtucker  2277: }
                   2278:
                   2279: time_t
1.201     markus   2280: ssh_packet_get_rekey_timeout(struct ssh *ssh)
1.184     dtucker  2281: {
                   2282:        time_t seconds;
                   2283:
1.201     markus   2284:        seconds = ssh->state->rekey_time + ssh->state->rekey_interval -
1.187     dtucker  2285:            monotime();
1.185     dtucker  2286:        return (seconds <= 0 ? 1 : seconds);
1.118     markus   2287: }
                   2288:
                   2289: void
1.201     markus   2290: ssh_packet_set_server(struct ssh *ssh)
1.118     markus   2291: {
1.201     markus   2292:        ssh->state->server_side = 1;
1.118     markus   2293: }
                   2294:
                   2295: void
1.201     markus   2296: ssh_packet_set_authenticated(struct ssh *ssh)
1.118     markus   2297: {
1.201     markus   2298:        ssh->state->after_authentication = 1;
1.161     andreas  2299: }
                   2300:
                   2301: void *
1.201     markus   2302: ssh_packet_get_input(struct ssh *ssh)
1.161     andreas  2303: {
1.201     markus   2304:        return (void *)ssh->state->input;
1.161     andreas  2305: }
                   2306:
                   2307: void *
1.201     markus   2308: ssh_packet_get_output(struct ssh *ssh)
1.161     andreas  2309: {
1.201     markus   2310:        return (void *)ssh->state->output;
1.166     andreas  2311: }
                   2312:
1.196     markus   2313: /* Reset after_authentication and reset compression in post-auth privsep */
1.201     markus   2314: static int
                   2315: ssh_packet_set_postauth(struct ssh *ssh)
1.196     markus   2316: {
1.201     markus   2317:        struct sshcomp *comp;
                   2318:        int r, mode;
1.196     markus   2319:
                   2320:        debug("%s: called", __func__);
                   2321:        /* This was set in net child, but is not visible in user child */
1.201     markus   2322:        ssh->state->after_authentication = 1;
                   2323:        ssh->state->rekeying = 0;
1.196     markus   2324:        for (mode = 0; mode < MODE_MAX; mode++) {
1.201     markus   2325:                if (ssh->state->newkeys[mode] == NULL)
1.196     markus   2326:                        continue;
1.201     markus   2327:                comp = &ssh->state->newkeys[mode]->comp;
                   2328:                if (comp && comp->enabled &&
                   2329:                    (r = ssh_packet_init_compression(ssh)) != 0)
                   2330:                        return r;
                   2331:        }
                   2332:        return 0;
                   2333: }
                   2334:
                   2335: /* Packet state (de-)serialization for privsep */
                   2336:
                   2337: /* turn kex into a blob for packet state serialization */
                   2338: static int
                   2339: kex_to_blob(struct sshbuf *m, struct kex *kex)
                   2340: {
                   2341:        int r;
                   2342:
                   2343:        if ((r = sshbuf_put_string(m, kex->session_id,
                   2344:            kex->session_id_len)) != 0 ||
                   2345:            (r = sshbuf_put_u32(m, kex->we_need)) != 0 ||
                   2346:            (r = sshbuf_put_u32(m, kex->hostkey_type)) != 0 ||
                   2347:            (r = sshbuf_put_u32(m, kex->kex_type)) != 0 ||
                   2348:            (r = sshbuf_put_stringb(m, kex->my)) != 0 ||
                   2349:            (r = sshbuf_put_stringb(m, kex->peer)) != 0 ||
                   2350:            (r = sshbuf_put_u32(m, kex->flags)) != 0 ||
                   2351:            (r = sshbuf_put_cstring(m, kex->client_version_string)) != 0 ||
                   2352:            (r = sshbuf_put_cstring(m, kex->server_version_string)) != 0)
                   2353:                return r;
                   2354:        return 0;
                   2355: }
                   2356:
                   2357: /* turn key exchange results into a blob for packet state serialization */
                   2358: static int
                   2359: newkeys_to_blob(struct sshbuf *m, struct ssh *ssh, int mode)
                   2360: {
                   2361:        struct sshbuf *b;
                   2362:        struct sshcipher_ctx *cc;
                   2363:        struct sshcomp *comp;
                   2364:        struct sshenc *enc;
                   2365:        struct sshmac *mac;
                   2366:        struct newkeys *newkey;
                   2367:        int r;
                   2368:
                   2369:        if ((newkey = ssh->state->newkeys[mode]) == NULL)
                   2370:                return SSH_ERR_INTERNAL_ERROR;
                   2371:        enc = &newkey->enc;
                   2372:        mac = &newkey->mac;
                   2373:        comp = &newkey->comp;
                   2374:        cc = (mode == MODE_OUT) ? &ssh->state->send_context :
                   2375:            &ssh->state->receive_context;
                   2376:        if ((r = cipher_get_keyiv(cc, enc->iv, enc->iv_len)) != 0)
                   2377:                return r;
                   2378:        if ((b = sshbuf_new()) == NULL)
                   2379:                return SSH_ERR_ALLOC_FAIL;
                   2380:        /* The cipher struct is constant and shared, you export pointer */
                   2381:        if ((r = sshbuf_put_cstring(b, enc->name)) != 0 ||
                   2382:            (r = sshbuf_put(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
                   2383:            (r = sshbuf_put_u32(b, enc->enabled)) != 0 ||
                   2384:            (r = sshbuf_put_u32(b, enc->block_size)) != 0 ||
                   2385:            (r = sshbuf_put_string(b, enc->key, enc->key_len)) != 0 ||
                   2386:            (r = sshbuf_put_string(b, enc->iv, enc->iv_len)) != 0)
                   2387:                goto out;
                   2388:        if (cipher_authlen(enc->cipher) == 0) {
                   2389:                if ((r = sshbuf_put_cstring(b, mac->name)) != 0 ||
                   2390:                    (r = sshbuf_put_u32(b, mac->enabled)) != 0 ||
                   2391:                    (r = sshbuf_put_string(b, mac->key, mac->key_len)) != 0)
                   2392:                        goto out;
                   2393:        }
                   2394:        if ((r = sshbuf_put_u32(b, comp->type)) != 0 ||
                   2395:            (r = sshbuf_put_u32(b, comp->enabled)) != 0 ||
                   2396:            (r = sshbuf_put_cstring(b, comp->name)) != 0)
                   2397:                goto out;
                   2398:        r = sshbuf_put_stringb(m, b);
                   2399:  out:
1.221     mmcc     2400:        sshbuf_free(b);
1.201     markus   2401:        return r;
                   2402: }
                   2403:
                   2404: /* serialize packet state into a blob */
                   2405: int
                   2406: ssh_packet_get_state(struct ssh *ssh, struct sshbuf *m)
                   2407: {
                   2408:        struct session_state *state = ssh->state;
                   2409:        u_char *p;
                   2410:        size_t slen, rlen;
                   2411:        int r, ssh1cipher;
                   2412:
                   2413:        if (!compat20) {
                   2414:                ssh1cipher = cipher_get_number(state->receive_context.cipher);
                   2415:                slen = cipher_get_keyiv_len(&state->send_context);
                   2416:                rlen = cipher_get_keyiv_len(&state->receive_context);
                   2417:                if ((r = sshbuf_put_u32(m, state->remote_protocol_flags)) != 0 ||
                   2418:                    (r = sshbuf_put_u32(m, ssh1cipher)) != 0 ||
                   2419:                    (r = sshbuf_put_string(m, state->ssh1_key, state->ssh1_keylen)) != 0 ||
                   2420:                    (r = sshbuf_put_u32(m, slen)) != 0 ||
                   2421:                    (r = sshbuf_reserve(m, slen, &p)) != 0 ||
                   2422:                    (r = cipher_get_keyiv(&state->send_context, p, slen)) != 0 ||
                   2423:                    (r = sshbuf_put_u32(m, rlen)) != 0 ||
                   2424:                    (r = sshbuf_reserve(m, rlen, &p)) != 0 ||
                   2425:                    (r = cipher_get_keyiv(&state->receive_context, p, rlen)) != 0)
                   2426:                        return r;
                   2427:        } else {
                   2428:                if ((r = kex_to_blob(m, ssh->kex)) != 0 ||
                   2429:                    (r = newkeys_to_blob(m, ssh, MODE_OUT)) != 0 ||
                   2430:                    (r = newkeys_to_blob(m, ssh, MODE_IN)) != 0 ||
1.224     dtucker  2431:                    (r = sshbuf_put_u64(m, state->rekey_limit)) != 0 ||
1.208     markus   2432:                    (r = sshbuf_put_u32(m, state->rekey_interval)) != 0 ||
1.201     markus   2433:                    (r = sshbuf_put_u32(m, state->p_send.seqnr)) != 0 ||
                   2434:                    (r = sshbuf_put_u64(m, state->p_send.blocks)) != 0 ||
                   2435:                    (r = sshbuf_put_u32(m, state->p_send.packets)) != 0 ||
                   2436:                    (r = sshbuf_put_u64(m, state->p_send.bytes)) != 0 ||
                   2437:                    (r = sshbuf_put_u32(m, state->p_read.seqnr)) != 0 ||
                   2438:                    (r = sshbuf_put_u64(m, state->p_read.blocks)) != 0 ||
                   2439:                    (r = sshbuf_put_u32(m, state->p_read.packets)) != 0 ||
                   2440:                    (r = sshbuf_put_u64(m, state->p_read.bytes)) != 0)
                   2441:                        return r;
                   2442:        }
                   2443:
                   2444:        slen = cipher_get_keycontext(&state->send_context, NULL);
                   2445:        rlen = cipher_get_keycontext(&state->receive_context, NULL);
                   2446:        if ((r = sshbuf_put_u32(m, slen)) != 0 ||
                   2447:            (r = sshbuf_reserve(m, slen, &p)) != 0)
                   2448:                return r;
                   2449:        if (cipher_get_keycontext(&state->send_context, p) != (int)slen)
                   2450:                return SSH_ERR_INTERNAL_ERROR;
                   2451:        if ((r = sshbuf_put_u32(m, rlen)) != 0 ||
                   2452:            (r = sshbuf_reserve(m, rlen, &p)) != 0)
                   2453:                return r;
                   2454:        if (cipher_get_keycontext(&state->receive_context, p) != (int)rlen)
                   2455:                return SSH_ERR_INTERNAL_ERROR;
                   2456:
                   2457:        if ((r = ssh_packet_get_compress_state(m, ssh)) != 0 ||
                   2458:            (r = sshbuf_put_stringb(m, state->input)) != 0 ||
                   2459:            (r = sshbuf_put_stringb(m, state->output)) != 0)
                   2460:                return r;
                   2461:
                   2462:        return 0;
                   2463: }
                   2464:
                   2465: /* restore key exchange results from blob for packet state de-serialization */
                   2466: static int
                   2467: newkeys_from_blob(struct sshbuf *m, struct ssh *ssh, int mode)
                   2468: {
                   2469:        struct sshbuf *b = NULL;
                   2470:        struct sshcomp *comp;
                   2471:        struct sshenc *enc;
                   2472:        struct sshmac *mac;
                   2473:        struct newkeys *newkey = NULL;
                   2474:        size_t keylen, ivlen, maclen;
                   2475:        int r;
                   2476:
                   2477:        if ((newkey = calloc(1, sizeof(*newkey))) == NULL) {
                   2478:                r = SSH_ERR_ALLOC_FAIL;
                   2479:                goto out;
                   2480:        }
                   2481:        if ((r = sshbuf_froms(m, &b)) != 0)
                   2482:                goto out;
                   2483: #ifdef DEBUG_PK
                   2484:        sshbuf_dump(b, stderr);
                   2485: #endif
                   2486:        enc = &newkey->enc;
                   2487:        mac = &newkey->mac;
                   2488:        comp = &newkey->comp;
                   2489:
                   2490:        if ((r = sshbuf_get_cstring(b, &enc->name, NULL)) != 0 ||
                   2491:            (r = sshbuf_get(b, &enc->cipher, sizeof(enc->cipher))) != 0 ||
                   2492:            (r = sshbuf_get_u32(b, (u_int *)&enc->enabled)) != 0 ||
                   2493:            (r = sshbuf_get_u32(b, &enc->block_size)) != 0 ||
                   2494:            (r = sshbuf_get_string(b, &enc->key, &keylen)) != 0 ||
                   2495:            (r = sshbuf_get_string(b, &enc->iv, &ivlen)) != 0)
                   2496:                goto out;
                   2497:        if (cipher_authlen(enc->cipher) == 0) {
                   2498:                if ((r = sshbuf_get_cstring(b, &mac->name, NULL)) != 0)
                   2499:                        goto out;
                   2500:                if ((r = mac_setup(mac, mac->name)) != 0)
                   2501:                        goto out;
                   2502:                if ((r = sshbuf_get_u32(b, (u_int *)&mac->enabled)) != 0 ||
                   2503:                    (r = sshbuf_get_string(b, &mac->key, &maclen)) != 0)
                   2504:                        goto out;
                   2505:                if (maclen > mac->key_len) {
                   2506:                        r = SSH_ERR_INVALID_FORMAT;
                   2507:                        goto out;
                   2508:                }
                   2509:                mac->key_len = maclen;
                   2510:        }
                   2511:        if ((r = sshbuf_get_u32(b, &comp->type)) != 0 ||
                   2512:            (r = sshbuf_get_u32(b, (u_int *)&comp->enabled)) != 0 ||
                   2513:            (r = sshbuf_get_cstring(b, &comp->name, NULL)) != 0)
                   2514:                goto out;
                   2515:        if (enc->name == NULL ||
                   2516:            cipher_by_name(enc->name) != enc->cipher) {
                   2517:                r = SSH_ERR_INVALID_FORMAT;
                   2518:                goto out;
                   2519:        }
                   2520:        if (sshbuf_len(b) != 0) {
                   2521:                r = SSH_ERR_INVALID_FORMAT;
                   2522:                goto out;
                   2523:        }
                   2524:        enc->key_len = keylen;
                   2525:        enc->iv_len = ivlen;
                   2526:        ssh->kex->newkeys[mode] = newkey;
                   2527:        newkey = NULL;
                   2528:        r = 0;
                   2529:  out:
1.219     mmcc     2530:        free(newkey);
1.221     mmcc     2531:        sshbuf_free(b);
1.201     markus   2532:        return r;
                   2533: }
                   2534:
                   2535: /* restore kex from blob for packet state de-serialization */
                   2536: static int
                   2537: kex_from_blob(struct sshbuf *m, struct kex **kexp)
                   2538: {
                   2539:        struct kex *kex;
                   2540:        int r;
                   2541:
                   2542:        if ((kex = calloc(1, sizeof(struct kex))) == NULL ||
                   2543:            (kex->my = sshbuf_new()) == NULL ||
                   2544:            (kex->peer = sshbuf_new()) == NULL) {
                   2545:                r = SSH_ERR_ALLOC_FAIL;
                   2546:                goto out;
                   2547:        }
                   2548:        if ((r = sshbuf_get_string(m, &kex->session_id, &kex->session_id_len)) != 0 ||
                   2549:            (r = sshbuf_get_u32(m, &kex->we_need)) != 0 ||
                   2550:            (r = sshbuf_get_u32(m, (u_int *)&kex->hostkey_type)) != 0 ||
                   2551:            (r = sshbuf_get_u32(m, &kex->kex_type)) != 0 ||
                   2552:            (r = sshbuf_get_stringb(m, kex->my)) != 0 ||
                   2553:            (r = sshbuf_get_stringb(m, kex->peer)) != 0 ||
                   2554:            (r = sshbuf_get_u32(m, &kex->flags)) != 0 ||
                   2555:            (r = sshbuf_get_cstring(m, &kex->client_version_string, NULL)) != 0 ||
                   2556:            (r = sshbuf_get_cstring(m, &kex->server_version_string, NULL)) != 0)
                   2557:                goto out;
                   2558:        kex->server = 1;
                   2559:        kex->done = 1;
                   2560:        r = 0;
                   2561:  out:
                   2562:        if (r != 0 || kexp == NULL) {
                   2563:                if (kex != NULL) {
1.221     mmcc     2564:                        sshbuf_free(kex->my);
                   2565:                        sshbuf_free(kex->peer);
1.201     markus   2566:                        free(kex);
                   2567:                }
                   2568:                if (kexp != NULL)
                   2569:                        *kexp = NULL;
                   2570:        } else {
                   2571:                *kexp = kex;
                   2572:        }
                   2573:        return r;
                   2574: }
                   2575:
                   2576: /*
                   2577:  * Restore packet state from content of blob 'm' (de-serialization).
                   2578:  * Note that 'm' will be partially consumed on parsing or any other errors.
                   2579:  */
                   2580: int
                   2581: ssh_packet_set_state(struct ssh *ssh, struct sshbuf *m)
                   2582: {
                   2583:        struct session_state *state = ssh->state;
                   2584:        const u_char *ssh1key, *ivin, *ivout, *keyin, *keyout, *input, *output;
                   2585:        size_t ssh1keylen, rlen, slen, ilen, olen;
                   2586:        int r;
                   2587:        u_int ssh1cipher = 0;
                   2588:
                   2589:        if (!compat20) {
                   2590:                if ((r = sshbuf_get_u32(m, &state->remote_protocol_flags)) != 0 ||
                   2591:                    (r = sshbuf_get_u32(m, &ssh1cipher)) != 0 ||
                   2592:                    (r = sshbuf_get_string_direct(m, &ssh1key, &ssh1keylen)) != 0 ||
                   2593:                    (r = sshbuf_get_string_direct(m, &ivout, &slen)) != 0 ||
                   2594:                    (r = sshbuf_get_string_direct(m, &ivin, &rlen)) != 0)
                   2595:                        return r;
                   2596:                if (ssh1cipher > INT_MAX)
                   2597:                        return SSH_ERR_KEY_UNKNOWN_CIPHER;
                   2598:                ssh_packet_set_encryption_key(ssh, ssh1key, ssh1keylen,
                   2599:                    (int)ssh1cipher);
                   2600:                if (cipher_get_keyiv_len(&state->send_context) != (int)slen ||
                   2601:                    cipher_get_keyiv_len(&state->receive_context) != (int)rlen)
                   2602:                        return SSH_ERR_INVALID_FORMAT;
                   2603:                if ((r = cipher_set_keyiv(&state->send_context, ivout)) != 0 ||
                   2604:                    (r = cipher_set_keyiv(&state->receive_context, ivin)) != 0)
                   2605:                        return r;
                   2606:        } else {
                   2607:                if ((r = kex_from_blob(m, &ssh->kex)) != 0 ||
                   2608:                    (r = newkeys_from_blob(m, ssh, MODE_OUT)) != 0 ||
                   2609:                    (r = newkeys_from_blob(m, ssh, MODE_IN)) != 0 ||
1.224     dtucker  2610:                    (r = sshbuf_get_u64(m, &state->rekey_limit)) != 0 ||
1.208     markus   2611:                    (r = sshbuf_get_u32(m, &state->rekey_interval)) != 0 ||
1.201     markus   2612:                    (r = sshbuf_get_u32(m, &state->p_send.seqnr)) != 0 ||
                   2613:                    (r = sshbuf_get_u64(m, &state->p_send.blocks)) != 0 ||
                   2614:                    (r = sshbuf_get_u32(m, &state->p_send.packets)) != 0 ||
                   2615:                    (r = sshbuf_get_u64(m, &state->p_send.bytes)) != 0 ||
                   2616:                    (r = sshbuf_get_u32(m, &state->p_read.seqnr)) != 0 ||
                   2617:                    (r = sshbuf_get_u64(m, &state->p_read.blocks)) != 0 ||
                   2618:                    (r = sshbuf_get_u32(m, &state->p_read.packets)) != 0 ||
                   2619:                    (r = sshbuf_get_u64(m, &state->p_read.bytes)) != 0)
                   2620:                        return r;
1.208     markus   2621:                /*
                   2622:                 * We set the time here so that in post-auth privsep slave we
                   2623:                 * count from the completion of the authentication.
                   2624:                 */
                   2625:                state->rekey_time = monotime();
1.201     markus   2626:                /* XXX ssh_set_newkeys overrides p_read.packets? XXX */
                   2627:                if ((r = ssh_set_newkeys(ssh, MODE_IN)) != 0 ||
                   2628:                    (r = ssh_set_newkeys(ssh, MODE_OUT)) != 0)
                   2629:                        return r;
                   2630:        }
                   2631:        if ((r = sshbuf_get_string_direct(m, &keyout, &slen)) != 0 ||
                   2632:            (r = sshbuf_get_string_direct(m, &keyin, &rlen)) != 0)
                   2633:                return r;
                   2634:        if (cipher_get_keycontext(&state->send_context, NULL) != (int)slen ||
                   2635:            cipher_get_keycontext(&state->receive_context, NULL) != (int)rlen)
                   2636:                return SSH_ERR_INVALID_FORMAT;
                   2637:        cipher_set_keycontext(&state->send_context, keyout);
                   2638:        cipher_set_keycontext(&state->receive_context, keyin);
                   2639:
                   2640:        if ((r = ssh_packet_set_compress_state(ssh, m)) != 0 ||
                   2641:            (r = ssh_packet_set_postauth(ssh)) != 0)
                   2642:                return r;
                   2643:
                   2644:        sshbuf_reset(state->input);
                   2645:        sshbuf_reset(state->output);
                   2646:        if ((r = sshbuf_get_string_direct(m, &input, &ilen)) != 0 ||
                   2647:            (r = sshbuf_get_string_direct(m, &output, &olen)) != 0 ||
                   2648:            (r = sshbuf_put(state->input, input, ilen)) != 0 ||
                   2649:            (r = sshbuf_put(state->output, output, olen)) != 0)
                   2650:                return r;
                   2651:
                   2652:        if (sshbuf_len(m))
                   2653:                return SSH_ERR_INVALID_FORMAT;
                   2654:        debug3("%s: done", __func__);
                   2655:        return 0;
                   2656: }
                   2657:
                   2658: /* NEW API */
                   2659:
                   2660: /* put data to the outgoing packet */
                   2661:
                   2662: int
                   2663: sshpkt_put(struct ssh *ssh, const void *v, size_t len)
                   2664: {
                   2665:        return sshbuf_put(ssh->state->outgoing_packet, v, len);
                   2666: }
                   2667:
                   2668: int
                   2669: sshpkt_putb(struct ssh *ssh, const struct sshbuf *b)
                   2670: {
                   2671:        return sshbuf_putb(ssh->state->outgoing_packet, b);
                   2672: }
                   2673:
                   2674: int
                   2675: sshpkt_put_u8(struct ssh *ssh, u_char val)
                   2676: {
                   2677:        return sshbuf_put_u8(ssh->state->outgoing_packet, val);
                   2678: }
                   2679:
                   2680: int
                   2681: sshpkt_put_u32(struct ssh *ssh, u_int32_t val)
                   2682: {
                   2683:        return sshbuf_put_u32(ssh->state->outgoing_packet, val);
                   2684: }
                   2685:
                   2686: int
                   2687: sshpkt_put_u64(struct ssh *ssh, u_int64_t val)
                   2688: {
                   2689:        return sshbuf_put_u64(ssh->state->outgoing_packet, val);
                   2690: }
                   2691:
                   2692: int
                   2693: sshpkt_put_string(struct ssh *ssh, const void *v, size_t len)
                   2694: {
                   2695:        return sshbuf_put_string(ssh->state->outgoing_packet, v, len);
                   2696: }
                   2697:
                   2698: int
                   2699: sshpkt_put_cstring(struct ssh *ssh, const void *v)
                   2700: {
                   2701:        return sshbuf_put_cstring(ssh->state->outgoing_packet, v);
                   2702: }
                   2703:
                   2704: int
                   2705: sshpkt_put_stringb(struct ssh *ssh, const struct sshbuf *v)
                   2706: {
                   2707:        return sshbuf_put_stringb(ssh->state->outgoing_packet, v);
                   2708: }
                   2709:
1.211     djm      2710: #ifdef WITH_OPENSSL
1.201     markus   2711: int
                   2712: sshpkt_put_ec(struct ssh *ssh, const EC_POINT *v, const EC_GROUP *g)
                   2713: {
                   2714:        return sshbuf_put_ec(ssh->state->outgoing_packet, v, g);
                   2715: }
                   2716:
1.211     djm      2717: #ifdef WITH_SSH1
1.201     markus   2718: int
                   2719: sshpkt_put_bignum1(struct ssh *ssh, const BIGNUM *v)
                   2720: {
                   2721:        return sshbuf_put_bignum1(ssh->state->outgoing_packet, v);
                   2722: }
1.211     djm      2723: #endif /* WITH_SSH1 */
1.201     markus   2724:
                   2725: int
                   2726: sshpkt_put_bignum2(struct ssh *ssh, const BIGNUM *v)
                   2727: {
                   2728:        return sshbuf_put_bignum2(ssh->state->outgoing_packet, v);
                   2729: }
1.211     djm      2730: #endif /* WITH_OPENSSL */
1.201     markus   2731:
                   2732: /* fetch data from the incoming packet */
                   2733:
                   2734: int
                   2735: sshpkt_get(struct ssh *ssh, void *valp, size_t len)
                   2736: {
                   2737:        return sshbuf_get(ssh->state->incoming_packet, valp, len);
                   2738: }
                   2739:
                   2740: int
                   2741: sshpkt_get_u8(struct ssh *ssh, u_char *valp)
                   2742: {
                   2743:        return sshbuf_get_u8(ssh->state->incoming_packet, valp);
                   2744: }
                   2745:
                   2746: int
                   2747: sshpkt_get_u32(struct ssh *ssh, u_int32_t *valp)
                   2748: {
                   2749:        return sshbuf_get_u32(ssh->state->incoming_packet, valp);
                   2750: }
                   2751:
                   2752: int
                   2753: sshpkt_get_u64(struct ssh *ssh, u_int64_t *valp)
                   2754: {
                   2755:        return sshbuf_get_u64(ssh->state->incoming_packet, valp);
                   2756: }
                   2757:
                   2758: int
                   2759: sshpkt_get_string(struct ssh *ssh, u_char **valp, size_t *lenp)
                   2760: {
                   2761:        return sshbuf_get_string(ssh->state->incoming_packet, valp, lenp);
                   2762: }
                   2763:
                   2764: int
                   2765: sshpkt_get_string_direct(struct ssh *ssh, const u_char **valp, size_t *lenp)
                   2766: {
                   2767:        return sshbuf_get_string_direct(ssh->state->incoming_packet, valp, lenp);
                   2768: }
                   2769:
                   2770: int
                   2771: sshpkt_get_cstring(struct ssh *ssh, char **valp, size_t *lenp)
                   2772: {
                   2773:        return sshbuf_get_cstring(ssh->state->incoming_packet, valp, lenp);
                   2774: }
                   2775:
1.211     djm      2776: #ifdef WITH_OPENSSL
1.201     markus   2777: int
                   2778: sshpkt_get_ec(struct ssh *ssh, EC_POINT *v, const EC_GROUP *g)
                   2779: {
                   2780:        return sshbuf_get_ec(ssh->state->incoming_packet, v, g);
                   2781: }
                   2782:
1.211     djm      2783: #ifdef WITH_SSH1
1.201     markus   2784: int
                   2785: sshpkt_get_bignum1(struct ssh *ssh, BIGNUM *v)
                   2786: {
                   2787:        return sshbuf_get_bignum1(ssh->state->incoming_packet, v);
                   2788: }
1.211     djm      2789: #endif /* WITH_SSH1 */
1.201     markus   2790:
                   2791: int
                   2792: sshpkt_get_bignum2(struct ssh *ssh, BIGNUM *v)
                   2793: {
                   2794:        return sshbuf_get_bignum2(ssh->state->incoming_packet, v);
                   2795: }
1.211     djm      2796: #endif /* WITH_OPENSSL */
1.201     markus   2797:
                   2798: int
                   2799: sshpkt_get_end(struct ssh *ssh)
                   2800: {
                   2801:        if (sshbuf_len(ssh->state->incoming_packet) > 0)
                   2802:                return SSH_ERR_UNEXPECTED_TRAILING_DATA;
                   2803:        return 0;
                   2804: }
                   2805:
                   2806: const u_char *
                   2807: sshpkt_ptr(struct ssh *ssh, size_t *lenp)
                   2808: {
                   2809:        if (lenp != NULL)
                   2810:                *lenp = sshbuf_len(ssh->state->incoming_packet);
                   2811:        return sshbuf_ptr(ssh->state->incoming_packet);
                   2812: }
                   2813:
                   2814: /* start a new packet */
                   2815:
                   2816: int
                   2817: sshpkt_start(struct ssh *ssh, u_char type)
                   2818: {
                   2819:        u_char buf[9];
                   2820:        int len;
                   2821:
                   2822:        DBG(debug("packet_start[%d]", type));
                   2823:        len = compat20 ? 6 : 9;
                   2824:        memset(buf, 0, len - 1);
                   2825:        buf[len - 1] = type;
                   2826:        sshbuf_reset(ssh->state->outgoing_packet);
                   2827:        return sshbuf_put(ssh->state->outgoing_packet, buf, len);
                   2828: }
                   2829:
                   2830: /* send it */
                   2831:
                   2832: int
                   2833: sshpkt_send(struct ssh *ssh)
                   2834: {
                   2835:        if (compat20)
                   2836:                return ssh_packet_send2(ssh);
                   2837:        else
                   2838:                return ssh_packet_send1(ssh);
                   2839: }
                   2840:
                   2841: int
                   2842: sshpkt_disconnect(struct ssh *ssh, const char *fmt,...)
                   2843: {
                   2844:        char buf[1024];
                   2845:        va_list args;
                   2846:        int r;
                   2847:
                   2848:        va_start(args, fmt);
                   2849:        vsnprintf(buf, sizeof(buf), fmt, args);
                   2850:        va_end(args);
                   2851:
                   2852:        if (compat20) {
                   2853:                if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
                   2854:                    (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_PROTOCOL_ERROR)) != 0 ||
                   2855:                    (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
                   2856:                    (r = sshpkt_put_cstring(ssh, "")) != 0 ||
                   2857:                    (r = sshpkt_send(ssh)) != 0)
                   2858:                        return r;
                   2859:        } else {
                   2860:                if ((r = sshpkt_start(ssh, SSH_MSG_DISCONNECT)) != 0 ||
                   2861:                    (r = sshpkt_put_cstring(ssh, buf)) != 0 ||
                   2862:                    (r = sshpkt_send(ssh)) != 0)
                   2863:                        return r;
1.166     andreas  2864:        }
1.201     markus   2865:        return 0;
                   2866: }
                   2867:
                   2868: /* roundup current message to pad bytes */
                   2869: int
                   2870: sshpkt_add_padding(struct ssh *ssh, u_char pad)
                   2871: {
                   2872:        ssh->state->extra_pad = pad;
                   2873:        return 0;
1.1       deraadt  2874: }