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