blob: a1ba0fd58df0e13c56fb2136802d8de0e73253dd [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
2
3bufaux.c
4
5Author: Tatu Ylonen <ylo@cs.hut.fi>
6
7Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 All rights reserved
9
10Created: Wed Mar 29 02:24:47 1995 ylo
11
12Auxiliary functions for storing and retrieving various data types to/from
13Buffers.
14
15*/
16
17#include "includes.h"
Damien Millera2d6efe1999-11-13 13:22:46 +110018RCSID("$Id: bufaux.c,v 1.5 1999/11/13 02:22:46 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
20#include "ssh.h"
Damien Miller7f6ea021999-10-28 13:25:17 +100021
22#ifdef HAVE_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +100023#include <openssl/bn.h>
Damien Miller7f6ea021999-10-28 13:25:17 +100024#endif
25#ifdef HAVE_SSL
26#include <ssl/bn.h>
27#endif
28
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029#include "bufaux.h"
30#include "xmalloc.h"
31#include "getput.h"
32
33/* Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed
34 by (bits+7)/8 bytes of binary data, msb first. */
35
36void
37buffer_put_bignum(Buffer *buffer, BIGNUM *value)
38{
39 int bits = BN_num_bits(value);
40 int bin_size = (bits + 7) / 8;
41 char *buf = xmalloc(bin_size);
42 int oi;
43 char msg[2];
44
45 /* Get the value of in binary */
46 oi = BN_bn2bin(value, buf);
Damien Millerfd7c9111999-11-08 16:15:55 +110047 if (oi != bin_size)
48 fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d",
49 oi, bin_size);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
51 /* Store the number of bits in the buffer in two bytes, msb first. */
52 PUT_16BIT(msg, bits);
53 buffer_append(buffer, msg, 2);
54 /* Store the binary data. */
55 buffer_append(buffer, buf, oi);
56 /* Clear the temporary data. */
57 memset(buf, 0, bin_size);
58 xfree(buf);
59}
60
61/* Retrieves an BIGNUM from the buffer. */
62
63int
64buffer_get_bignum(Buffer *buffer, BIGNUM *value)
65{
66 int bits, bytes;
67 unsigned char buf[2], *bin;
68
69 /* Get the number for bits. */
70 buffer_get(buffer, (char *)buf, 2);
71 bits = GET_16BIT(buf);
72 /* Compute the number of binary bytes that follow. */
73 bytes = (bits + 7) / 8;
Damien Millera2d6efe1999-11-13 13:22:46 +110074 if (buffer_len(buffer) < bytes)
75 fatal("buffer_get_bignum: input buffer too small");
76 bin = buffer_ptr(buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077 BN_bin2bn(bin, bytes, value);
Damien Millera2d6efe1999-11-13 13:22:46 +110078 buffer_consume(buffer, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
80 return 2 + bytes;
81}
82
83/* Returns an integer from the buffer (4 bytes, msb first). */
84
85unsigned int buffer_get_int(Buffer *buffer)
86{
87 unsigned char buf[4];
88 buffer_get(buffer, (char *)buf, 4);
89 return GET_32BIT(buf);
90}
91
92/* Stores an integer in the buffer in 4 bytes, msb first. */
93
94void buffer_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/* 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
108char *buffer_get_string(Buffer *buffer, unsigned int *length_ptr)
109{
110 unsigned int len;
111 char *value;
112 /* Get the length. */
113 len = buffer_get_int(buffer);
114 if (len > 256*1024)
115 fatal("Received packet with bad string length %d", len);
116 /* Allocate space for the string. Add one byte for a null character. */
117 value = xmalloc(len + 1);
118 /* Get the string. */
119 buffer_get(buffer, value, len);
120 /* Append a null character to make processing easier. */
121 value[len] = 0;
122 /* Optionally return the length of the string. */
123 if (length_ptr)
124 *length_ptr = len;
125 return value;
126}
127
128/* Stores and arbitrary binary string in the buffer. */
129
130void buffer_put_string(Buffer *buffer, const void *buf, unsigned int len)
131{
132 buffer_put_int(buffer, len);
133 buffer_append(buffer, buf, len);
134}
135
136/* Returns a character from the buffer (0 - 255). */
137
138int buffer_get_char(Buffer *buffer)
139{
140 char ch;
141 buffer_get(buffer, &ch, 1);
142 return (unsigned char)ch;
143}
144
145/* Stores a character in the buffer. */
146
147void buffer_put_char(Buffer *buffer, int value)
148{
149 char ch = value;
150 buffer_append(buffer, &ch, 1);
151}