[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.42

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