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

Annotation of src/usr.bin/ssh/bufaux.c, Revision 1.47

1.47    ! djm         1: /* $OpenBSD: bufaux.c,v 1.46 2008/06/10 23:21:34 dtucker Exp $ */
1.1       deraadt     2: /*
1.6       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.13      deraadt     6:  * Auxiliary functions for storing and retrieving various data types to/from
                      7:  * Buffers.
1.11      markus      8:  *
1.13      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.11      markus     14:  *
1.6       deraadt    15:  *
1.9       markus     16:  * SSH2 packet format added by Markus Friedl
1.13      deraadt    17:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1.9       markus     18:  *
1.13      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.6       deraadt    38:  */
1.1       deraadt    39:
1.44      deraadt    40: #include <sys/types.h>
1.1       deraadt    41:
1.10      markus     42: #include <openssl/bn.h>
1.43      stevesk    43:
                     44: #include <string.h>
1.44      deraadt    45: #include <stdarg.h>
1.43      stevesk    46:
1.1       deraadt    47: #include "xmalloc.h"
1.44      deraadt    48: #include "buffer.h"
1.17      markus     49: #include "log.h"
1.41      djm        50: #include "misc.h"
1.32      markus     51:
1.9       markus     52: /*
1.25      markus     53:  * Returns integers from the buffer (msb first).
1.6       deraadt    54:  */
1.25      markus     55:
1.33      djm        56: int
                     57: buffer_get_short_ret(u_short *ret, Buffer *buffer)
                     58: {
                     59:        u_char buf[2];
                     60:
                     61:        if (buffer_get_ret(buffer, (char *) buf, 2) == -1)
                     62:                return (-1);
1.41      djm        63:        *ret = get_u16(buf);
1.33      djm        64:        return (0);
                     65: }
                     66:
1.25      markus     67: u_short
                     68: buffer_get_short(Buffer *buffer)
                     69: {
1.33      djm        70:        u_short ret;
                     71:
                     72:        if (buffer_get_short_ret(&ret, buffer) == -1)
                     73:                fatal("buffer_get_short: buffer error");
                     74:
                     75:        return (ret);
                     76: }
                     77:
                     78: int
                     79: buffer_get_int_ret(u_int *ret, Buffer *buffer)
                     80: {
                     81:        u_char buf[4];
1.26      deraadt    82:
1.33      djm        83:        if (buffer_get_ret(buffer, (char *) buf, 4) == -1)
                     84:                return (-1);
1.41      djm        85:        *ret = get_u32(buf);
1.33      djm        86:        return (0);
1.25      markus     87: }
                     88:
1.14      markus     89: u_int
1.5       markus     90: buffer_get_int(Buffer *buffer)
1.1       deraadt    91: {
1.33      djm        92:        u_int ret;
                     93:
                     94:        if (buffer_get_int_ret(&ret, buffer) == -1)
                     95:                fatal("buffer_get_int: buffer error");
                     96:
                     97:        return (ret);
                     98: }
                     99:
                    100: int
                    101: buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer)
                    102: {
                    103:        u_char buf[8];
1.26      deraadt   104:
1.33      djm       105:        if (buffer_get_ret(buffer, (char *) buf, 8) == -1)
                    106:                return (-1);
1.41      djm       107:        *ret = get_u64(buf);
1.33      djm       108:        return (0);
1.1       deraadt   109: }
                    110:
1.15      markus    111: u_int64_t
                    112: buffer_get_int64(Buffer *buffer)
                    113: {
1.33      djm       114:        u_int64_t ret;
                    115:
                    116:        if (buffer_get_int64_ret(&ret, buffer) == -1)
                    117:                fatal("buffer_get_int: buffer error");
1.26      deraadt   118:
1.33      djm       119:        return (ret);
1.15      markus    120: }
                    121:
1.6       deraadt   122: /*
1.25      markus    123:  * Stores integers in the buffer, msb first.
1.6       deraadt   124:  */
1.25      markus    125: void
                    126: buffer_put_short(Buffer *buffer, u_short value)
                    127: {
                    128:        char buf[2];
1.26      deraadt   129:
1.41      djm       130:        put_u16(buf, value);
1.25      markus    131:        buffer_append(buffer, buf, 2);
                    132: }
                    133:
1.11      markus    134: void
1.14      markus    135: buffer_put_int(Buffer *buffer, u_int value)
1.1       deraadt   136: {
1.5       markus    137:        char buf[4];
1.26      deraadt   138:
1.41      djm       139:        put_u32(buf, value);
1.5       markus    140:        buffer_append(buffer, buf, 4);
1.15      markus    141: }
                    142:
                    143: void
                    144: buffer_put_int64(Buffer *buffer, u_int64_t value)
                    145: {
                    146:        char buf[8];
1.26      deraadt   147:
1.41      djm       148:        put_u64(buf, value);
1.15      markus    149:        buffer_append(buffer, buf, 8);
1.1       deraadt   150: }
                    151:
1.6       deraadt   152: /*
                    153:  * Returns an arbitrary binary string from the buffer.  The string cannot
                    154:  * be longer than 256k.  The returned value points to memory allocated
                    155:  * with xmalloc; it is the responsibility of the calling function to free
                    156:  * the data.  If length_ptr is non-NULL, the length of the returned data
                    157:  * will be stored there.  A null character will be automatically appended
                    158:  * to the returned string, and is not counted in length.
                    159:  */
1.20      stevesk   160: void *
1.33      djm       161: buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
1.1       deraadt   162: {
1.26      deraadt   163:        u_char *value;
1.14      markus    164:        u_int len;
1.26      deraadt   165:
1.5       markus    166:        /* Get the length. */
                    167:        len = buffer_get_int(buffer);
1.33      djm       168:        if (len > 256 * 1024) {
                    169:                error("buffer_get_string_ret: bad string length %u", len);
                    170:                return (NULL);
                    171:        }
1.5       markus    172:        /* Allocate space for the string.  Add one byte for a null character. */
                    173:        value = xmalloc(len + 1);
                    174:        /* Get the string. */
1.33      djm       175:        if (buffer_get_ret(buffer, value, len) == -1) {
                    176:                error("buffer_get_string_ret: buffer_get failed");
                    177:                xfree(value);
                    178:                return (NULL);
                    179:        }
1.5       markus    180:        /* Append a null character to make processing easier. */
1.46      dtucker   181:        value[len] = '\0';
1.5       markus    182:        /* Optionally return the length of the string. */
                    183:        if (length_ptr)
                    184:                *length_ptr = len;
1.33      djm       185:        return (value);
                    186: }
                    187:
                    188: void *
                    189: buffer_get_string(Buffer *buffer, u_int *length_ptr)
                    190: {
                    191:        void *ret;
                    192:
                    193:        if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
                    194:                fatal("buffer_get_string: buffer error");
                    195:        return (ret);
1.45      markus    196: }
                    197:
                    198: void *
1.47    ! djm       199: buffer_get_string_ptr_ret(Buffer *buffer, u_int *length_ptr)
1.45      markus    200: {
                    201:        void *ptr;
                    202:        u_int len;
                    203:
1.47    ! djm       204:        if (buffer_get_int_ret(&len, buffer) != 0)
        !           205:                return NULL;
        !           206:        if (len > 256 * 1024) {
        !           207:                error("buffer_get_string_ptr: bad string length %u", len);
        !           208:                return NULL;
        !           209:        }
1.45      markus    210:        ptr = buffer_ptr(buffer);
                    211:        buffer_consume(buffer, len);
                    212:        if (length_ptr)
                    213:                *length_ptr = len;
                    214:        return (ptr);
1.47    ! djm       215: }
        !           216:
        !           217: void *
        !           218: buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr)
        !           219: {
        !           220:        void *ret;
        !           221:
        !           222:        if ((ret = buffer_get_string_ptr_ret(buffer, length_ptr)) == NULL)
        !           223:                fatal("buffer_get_string_ptr: buffer error");
        !           224:        return (ret);
1.1       deraadt   225: }
                    226:
1.6       deraadt   227: /*
                    228:  * Stores and arbitrary binary string in the buffer.
                    229:  */
1.11      markus    230: void
1.14      markus    231: buffer_put_string(Buffer *buffer, const void *buf, u_int len)
1.1       deraadt   232: {
1.5       markus    233:        buffer_put_int(buffer, len);
                    234:        buffer_append(buffer, buf, len);
1.9       markus    235: }
1.11      markus    236: void
1.9       markus    237: buffer_put_cstring(Buffer *buffer, const char *s)
                    238: {
1.23      provos    239:        if (s == NULL)
                    240:                fatal("buffer_put_cstring: s == NULL");
1.9       markus    241:        buffer_put_string(buffer, s, strlen(s));
1.1       deraadt   242: }
                    243:
1.6       deraadt   244: /*
                    245:  * Returns a character from the buffer (0 - 255).
                    246:  */
1.11      markus    247: int
1.33      djm       248: buffer_get_char_ret(char *ret, Buffer *buffer)
                    249: {
                    250:        if (buffer_get_ret(buffer, ret, 1) == -1) {
                    251:                error("buffer_get_char_ret: buffer_get_ret failed");
                    252:                return (-1);
                    253:        }
                    254:        return (0);
                    255: }
                    256:
                    257: int
1.5       markus    258: buffer_get_char(Buffer *buffer)
1.1       deraadt   259: {
1.5       markus    260:        char ch;
1.26      deraadt   261:
1.33      djm       262:        if (buffer_get_char_ret(&ch, buffer) == -1)
                    263:                fatal("buffer_get_char: buffer error");
1.14      markus    264:        return (u_char) ch;
1.1       deraadt   265: }
                    266:
1.6       deraadt   267: /*
                    268:  * Stores a character in the buffer.
                    269:  */
1.11      markus    270: void
1.5       markus    271: buffer_put_char(Buffer *buffer, int value)
1.1       deraadt   272: {
1.5       markus    273:        char ch = value;
1.26      deraadt   274:
1.5       markus    275:        buffer_append(buffer, &ch, 1);
1.1       deraadt   276: }