5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Wed Mar 29 02:24:47 1995 ylo
12 * Auxiliary functions for storing and retrieving various data types to/from
15 * SSH2 packet format added by Markus Friedl
25 #include <openssl/bn.h>
36 * Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed
37 * by (bits+7)/8 bytes of binary data, msb first.
40 buffer_put_bignum(Buffer *buffer, BIGNUM *value)
42 int bits = BN_num_bits(value);
43 int bin_size = (bits + 7) / 8;
44 char unsigned *buf = xmalloc(bin_size);
48 /* Get the value of in binary */
49 oi = BN_bn2bin(value, buf);
51 fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d",
54 /* Store the number of bits in the buffer in two bytes, msb first. */
56 buffer_append(buffer, msg, 2);
57 /* Store the binary data. */
58 buffer_append(buffer, (char *)buf, oi);
60 memset(buf, 0, bin_size);
65 * Retrieves an BIGNUM from the buffer.
68 buffer_get_bignum(Buffer *buffer, BIGNUM *value)
71 unsigned char buf[2], *bin;
73 /* Get the number for bits. */
74 buffer_get(buffer, (char *) buf, 2);
75 bits = GET_16BIT(buf);
76 /* Compute the number of binary bytes that follow. */
77 bytes = (bits + 7) / 8;
78 if (buffer_len(buffer) < bytes)
79 fatal("buffer_get_bignum: input buffer too small");
80 bin = (unsigned char*) buffer_ptr(buffer);
81 BN_bin2bn(bin, bytes, value);
82 buffer_consume(buffer, bytes);
88 * Stores an BIGNUM in the buffer in SSH2 format.
91 buffer_put_bignum2(Buffer *buffer, BIGNUM *value)
93 int bytes = BN_num_bytes(value) + 1;
94 unsigned char *buf = xmalloc(bytes);
98 /* Get the value of in binary */
99 oi = BN_bn2bin(value, buf+1);
101 fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d",
103 hasnohigh = (buf[1] & 0x80) ? 0 : 1;
105 /**XXX should be two's-complement */
107 unsigned char *uc = buf;
109 for(i = bytes-1, carry = 1; i>=0; i--) {
115 buffer_put_string(buffer, buf+hasnohigh, bytes-hasnohigh);
116 memset(buf, 0, bytes);
121 buffer_get_bignum2(Buffer *buffer, BIGNUM *value)
123 /**XXX should be two's-complement */
125 unsigned char *bin = (unsigned char *)buffer_get_string(buffer, (unsigned int *)&len);
126 BN_bin2bn(bin, len, value);
132 * Returns an integer from the buffer (4 bytes, msb first).
135 buffer_get_int(Buffer *buffer)
137 unsigned char buf[4];
138 buffer_get(buffer, (char *) buf, 4);
139 return GET_32BIT(buf);
143 * Stores an integer in the buffer in 4 bytes, msb first.
146 buffer_put_int(Buffer *buffer, unsigned int value)
149 PUT_32BIT(buf, value);
150 buffer_append(buffer, buf, 4);
154 * Returns an arbitrary binary string from the buffer. The string cannot
155 * be longer than 256k. The returned value points to memory allocated
156 * with xmalloc; it is the responsibility of the calling function to free
157 * the data. If length_ptr is non-NULL, the length of the returned data
158 * will be stored there. A null character will be automatically appended
159 * to the returned string, and is not counted in length.
162 buffer_get_string(Buffer *buffer, unsigned int *length_ptr)
166 /* Get the length. */
167 len = buffer_get_int(buffer);
168 if (len > 256 * 1024)
169 fatal("Received packet with bad string length %d", len);
170 /* Allocate space for the string. Add one byte for a null character. */
171 value = xmalloc(len + 1);
172 /* Get the string. */
173 buffer_get(buffer, value, len);
174 /* Append a null character to make processing easier. */
176 /* Optionally return the length of the string. */
183 * Stores and arbitrary binary string in the buffer.
186 buffer_put_string(Buffer *buffer, const void *buf, unsigned int len)
188 buffer_put_int(buffer, len);
189 buffer_append(buffer, buf, len);
192 buffer_put_cstring(Buffer *buffer, const char *s)
194 buffer_put_string(buffer, s, strlen(s));
198 * Returns a character from the buffer (0 - 255).
201 buffer_get_char(Buffer *buffer)
204 buffer_get(buffer, &ch, 1);
205 return (unsigned char) ch;
209 * Stores a character in the buffer.
212 buffer_put_char(Buffer *buffer, int value)
215 buffer_append(buffer, &ch, 1);