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