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

1.1       deraadt     1: /*
1.6       deraadt     2:  *
                      3:  * bufaux.c
                      4:  *
                      5:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      6:  *
                      7:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      8:  *                    All rights reserved
                      9:  *
                     10:  * Created: Wed Mar 29 02:24:47 1995 ylo
                     11:  *
                     12:  * Auxiliary functions for storing and retrieving various data types to/from
                     13:  * Buffers.
                     14:  *
                     15:  */
1.1       deraadt    16:
                     17: #include "includes.h"
1.7     ! markus     18: RCSID("$Id: bufaux.c,v 1.6 1999/11/24 00:26:01 deraadt Exp $");
1.1       deraadt    19:
                     20: #include "ssh.h"
1.2       provos     21: #include <ssl/bn.h>
1.1       deraadt    22: #include "bufaux.h"
                     23: #include "xmalloc.h"
                     24: #include "getput.h"
                     25:
1.6       deraadt    26: /*
                     27:  * Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed
                     28:  * by (bits+7)/8 bytes of binary data, msb first.
                     29:  */
1.2       provos     30: void
                     31: buffer_put_bignum(Buffer *buffer, BIGNUM *value)
1.1       deraadt    32: {
1.5       markus     33:        int bits = BN_num_bits(value);
                     34:        int bin_size = (bits + 7) / 8;
                     35:        char *buf = xmalloc(bin_size);
                     36:        int oi;
                     37:        char msg[2];
                     38:
                     39:        /* Get the value of in binary */
                     40:        oi = BN_bn2bin(value, buf);
                     41:        if (oi != bin_size)
                     42:                fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d",
                     43:                      oi, bin_size);
                     44:
                     45:        /* Store the number of bits in the buffer in two bytes, msb first. */
                     46:        PUT_16BIT(msg, bits);
                     47:        buffer_append(buffer, msg, 2);
                     48:        /* Store the binary data. */
                     49:        buffer_append(buffer, buf, oi);
1.7     ! markus     50:
1.5       markus     51:        memset(buf, 0, bin_size);
                     52:        xfree(buf);
1.1       deraadt    53: }
                     54:
1.6       deraadt    55: /*
                     56:  * Retrieves an BIGNUM from the buffer.
                     57:  */
1.2       provos     58: int
                     59: buffer_get_bignum(Buffer *buffer, BIGNUM *value)
1.1       deraadt    60: {
1.5       markus     61:        int bits, bytes;
                     62:        unsigned char buf[2], *bin;
1.1       deraadt    63:
1.5       markus     64:        /* Get the number for bits. */
                     65:        buffer_get(buffer, (char *) buf, 2);
                     66:        bits = GET_16BIT(buf);
                     67:        /* Compute the number of binary bytes that follow. */
                     68:        bytes = (bits + 7) / 8;
                     69:        if (buffer_len(buffer) < bytes)
                     70:                fatal("buffer_get_bignum: input buffer too small");
                     71:        bin = buffer_ptr(buffer);
                     72:        BN_bin2bn(bin, bytes, value);
                     73:        buffer_consume(buffer, bytes);
1.2       provos     74:
1.5       markus     75:        return 2 + bytes;
1.1       deraadt    76: }
                     77:
1.6       deraadt    78: /*
                     79:  * Returns an integer from the buffer (4 bytes, msb first).
                     80:  */
1.5       markus     81: unsigned int
                     82: buffer_get_int(Buffer *buffer)
1.1       deraadt    83: {
1.5       markus     84:        unsigned char buf[4];
                     85:        buffer_get(buffer, (char *) buf, 4);
                     86:        return GET_32BIT(buf);
1.1       deraadt    87: }
                     88:
1.6       deraadt    89: /*
                     90:  * Stores an integer in the buffer in 4 bytes, msb first.
                     91:  */
1.5       markus     92: void
                     93: buffer_put_int(Buffer *buffer, unsigned int value)
1.1       deraadt    94: {
1.5       markus     95:        char buf[4];
                     96:        PUT_32BIT(buf, value);
                     97:        buffer_append(buffer, buf, 4);
1.1       deraadt    98: }
                     99:
1.6       deraadt   100: /*
                    101:  * Returns an arbitrary binary string from the buffer.  The string cannot
                    102:  * be longer than 256k.  The returned value points to memory allocated
                    103:  * with xmalloc; it is the responsibility of the calling function to free
                    104:  * the data.  If length_ptr is non-NULL, the length of the returned data
                    105:  * will be stored there.  A null character will be automatically appended
                    106:  * to the returned string, and is not counted in length.
                    107:  */
1.5       markus    108: char *
                    109: buffer_get_string(Buffer *buffer, unsigned int *length_ptr)
1.1       deraadt   110: {
1.5       markus    111:        unsigned int len;
                    112:        char *value;
                    113:        /* Get the length. */
                    114:        len = buffer_get_int(buffer);
                    115:        if (len > 256 * 1024)
                    116:                fatal("Received packet with bad string length %d", len);
                    117:        /* Allocate space for the string.  Add one byte for a null character. */
                    118:        value = xmalloc(len + 1);
                    119:        /* Get the string. */
                    120:        buffer_get(buffer, value, len);
                    121:        /* Append a null character to make processing easier. */
                    122:        value[len] = 0;
                    123:        /* Optionally return the length of the string. */
                    124:        if (length_ptr)
                    125:                *length_ptr = len;
                    126:        return value;
1.1       deraadt   127: }
                    128:
1.6       deraadt   129: /*
                    130:  * Stores and arbitrary binary string in the buffer.
                    131:  */
1.5       markus    132: void
                    133: buffer_put_string(Buffer *buffer, const void *buf, unsigned int len)
1.1       deraadt   134: {
1.5       markus    135:        buffer_put_int(buffer, len);
                    136:        buffer_append(buffer, buf, len);
1.1       deraadt   137: }
                    138:
1.6       deraadt   139: /*
                    140:  * Returns a character from the buffer (0 - 255).
                    141:  */
1.5       markus    142: int
                    143: buffer_get_char(Buffer *buffer)
1.1       deraadt   144: {
1.5       markus    145:        char ch;
                    146:        buffer_get(buffer, &ch, 1);
                    147:        return (unsigned char) ch;
1.1       deraadt   148: }
                    149:
1.6       deraadt   150: /*
                    151:  * Stores a character in the buffer.
                    152:  */
1.5       markus    153: void
                    154: buffer_put_char(Buffer *buffer, int value)
1.1       deraadt   155: {
1.5       markus    156:        char ch = value;
                    157:        buffer_append(buffer, &ch, 1);
1.1       deraadt   158: }