blob: ecf529ff6961a1f378e6b0938463574de6a01489 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * bufaux.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Wed Mar 29 02:24:47 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * Auxiliary functions for storing and retrieving various data types to/from
13 * Buffers.
14 *
Damien Millerb38eff82000-04-01 11:09:21 +100015 * SSH2 packet format added by Markus Friedl
16 *
Damien Miller95def091999-11-25 00:26:21 +110017 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "includes.h"
Damien Miller6536c7d2000-06-22 21:32:31 +100020RCSID("$OpenBSD: bufaux.c,v 1.12 2000/06/20 01:39:39 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
22#include "ssh.h"
23#include <openssl/bn.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024#include "bufaux.h"
25#include "xmalloc.h"
26#include "getput.h"
27
Damien Miller95def091999-11-25 00:26:21 +110028/*
29 * Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed
30 * by (bits+7)/8 bytes of binary data, msb first.
31 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032void
33buffer_put_bignum(Buffer *buffer, BIGNUM *value)
34{
Damien Miller95def091999-11-25 00:26:21 +110035 int bits = BN_num_bits(value);
36 int bin_size = (bits + 7) / 8;
Damien Miller7684ee12000-03-17 23:40:15 +110037 char unsigned *buf = xmalloc(bin_size);
Damien Miller95def091999-11-25 00:26:21 +110038 int oi;
39 char msg[2];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
Damien Miller95def091999-11-25 00:26:21 +110041 /* Get the value of in binary */
42 oi = BN_bn2bin(value, buf);
43 if (oi != bin_size)
44 fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d",
45 oi, bin_size);
46
47 /* Store the number of bits in the buffer in two bytes, msb first. */
48 PUT_16BIT(msg, bits);
49 buffer_append(buffer, msg, 2);
50 /* Store the binary data. */
Damien Miller7684ee12000-03-17 23:40:15 +110051 buffer_append(buffer, (char *)buf, oi);
Damien Miller5428f641999-11-25 11:54:57 +110052
Damien Miller95def091999-11-25 00:26:21 +110053 memset(buf, 0, bin_size);
54 xfree(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055}
56
Damien Miller95def091999-11-25 00:26:21 +110057/*
58 * Retrieves an BIGNUM from the buffer.
59 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060int
61buffer_get_bignum(Buffer *buffer, BIGNUM *value)
62{
Damien Miller95def091999-11-25 00:26:21 +110063 int bits, bytes;
64 unsigned char buf[2], *bin;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065
Damien Miller95def091999-11-25 00:26:21 +110066 /* Get the number for bits. */
67 buffer_get(buffer, (char *) buf, 2);
68 bits = GET_16BIT(buf);
69 /* Compute the number of binary bytes that follow. */
70 bytes = (bits + 7) / 8;
71 if (buffer_len(buffer) < bytes)
72 fatal("buffer_get_bignum: input buffer too small");
Damien Miller7684ee12000-03-17 23:40:15 +110073 bin = (unsigned char*) buffer_ptr(buffer);
Damien Miller95def091999-11-25 00:26:21 +110074 BN_bin2bn(bin, bytes, value);
75 buffer_consume(buffer, bytes);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
Damien Miller95def091999-11-25 00:26:21 +110077 return 2 + bytes;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078}
79
Damien Miller95def091999-11-25 00:26:21 +110080/*
Damien Millerb38eff82000-04-01 11:09:21 +100081 * Stores an BIGNUM in the buffer in SSH2 format.
82 */
83void
84buffer_put_bignum2(Buffer *buffer, BIGNUM *value)
85{
86 int bytes = BN_num_bytes(value) + 1;
87 unsigned char *buf = xmalloc(bytes);
88 int oi;
89 int hasnohigh = 0;
90 buf[0] = '\0';
91 /* Get the value of in binary */
92 oi = BN_bn2bin(value, buf+1);
93 if (oi != bytes-1)
94 fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d",
95 oi, bytes);
96 hasnohigh = (buf[1] & 0x80) ? 0 : 1;
97 if (value->neg) {
98 /**XXX should be two's-complement */
99 int i, carry;
100 unsigned char *uc = buf;
101 log("negativ!");
102 for(i = bytes-1, carry = 1; i>=0; i--) {
103 uc[i] ^= 0xff;
104 if(carry)
105 carry = !++uc[i];
106 }
107 }
108 buffer_put_string(buffer, buf+hasnohigh, bytes-hasnohigh);
109 memset(buf, 0, bytes);
110 xfree(buf);
111}
112
113int
114buffer_get_bignum2(Buffer *buffer, BIGNUM *value)
115{
116 /**XXX should be two's-complement */
117 int len;
118 unsigned char *bin = (unsigned char *)buffer_get_string(buffer, (unsigned int *)&len);
119 BN_bin2bn(bin, len, value);
120 xfree(bin);
121 return len;
122}
123
124/*
Damien Miller95def091999-11-25 00:26:21 +1100125 * Returns an integer from the buffer (4 bytes, msb first).
126 */
Damien Miller5f056372000-04-16 12:31:48 +1000127unsigned int
Damien Miller95def091999-11-25 00:26:21 +1100128buffer_get_int(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129{
Damien Miller95def091999-11-25 00:26:21 +1100130 unsigned char buf[4];
131 buffer_get(buffer, (char *) buf, 4);
132 return GET_32BIT(buf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000133}
134
Damien Miller95def091999-11-25 00:26:21 +1100135/*
136 * Stores an integer in the buffer in 4 bytes, msb first.
137 */
Damien Miller5f056372000-04-16 12:31:48 +1000138void
Damien Miller95def091999-11-25 00:26:21 +1100139buffer_put_int(Buffer *buffer, unsigned int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140{
Damien Miller95def091999-11-25 00:26:21 +1100141 char buf[4];
142 PUT_32BIT(buf, value);
143 buffer_append(buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144}
145
Damien Miller95def091999-11-25 00:26:21 +1100146/*
147 * Returns an arbitrary binary string from the buffer. The string cannot
148 * be longer than 256k. The returned value points to memory allocated
149 * with xmalloc; it is the responsibility of the calling function to free
150 * the data. If length_ptr is non-NULL, the length of the returned data
151 * will be stored there. A null character will be automatically appended
152 * to the returned string, and is not counted in length.
153 */
154char *
155buffer_get_string(Buffer *buffer, unsigned int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156{
Damien Miller95def091999-11-25 00:26:21 +1100157 unsigned int len;
158 char *value;
159 /* Get the length. */
160 len = buffer_get_int(buffer);
161 if (len > 256 * 1024)
162 fatal("Received packet with bad string length %d", len);
163 /* Allocate space for the string. Add one byte for a null character. */
164 value = xmalloc(len + 1);
165 /* Get the string. */
166 buffer_get(buffer, value, len);
167 /* Append a null character to make processing easier. */
168 value[len] = 0;
169 /* Optionally return the length of the string. */
170 if (length_ptr)
171 *length_ptr = len;
172 return value;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173}
174
Damien Miller95def091999-11-25 00:26:21 +1100175/*
176 * Stores and arbitrary binary string in the buffer.
177 */
Damien Miller5f056372000-04-16 12:31:48 +1000178void
Damien Miller95def091999-11-25 00:26:21 +1100179buffer_put_string(Buffer *buffer, const void *buf, unsigned int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180{
Damien Miller95def091999-11-25 00:26:21 +1100181 buffer_put_int(buffer, len);
182 buffer_append(buffer, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183}
Damien Miller5f056372000-04-16 12:31:48 +1000184void
Damien Millerb38eff82000-04-01 11:09:21 +1000185buffer_put_cstring(Buffer *buffer, const char *s)
186{
187 buffer_put_string(buffer, s, strlen(s));
188}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189
Damien Miller95def091999-11-25 00:26:21 +1100190/*
191 * Returns a character from the buffer (0 - 255).
192 */
Damien Miller5f056372000-04-16 12:31:48 +1000193int
Damien Miller95def091999-11-25 00:26:21 +1100194buffer_get_char(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195{
Damien Miller95def091999-11-25 00:26:21 +1100196 char ch;
197 buffer_get(buffer, &ch, 1);
198 return (unsigned char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199}
200
Damien Miller95def091999-11-25 00:26:21 +1100201/*
202 * Stores a character in the buffer.
203 */
Damien Miller5f056372000-04-16 12:31:48 +1000204void
Damien Miller95def091999-11-25 00:26:21 +1100205buffer_put_char(Buffer *buffer, int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206{
Damien Miller95def091999-11-25 00:26:21 +1100207 char ch = value;
208 buffer_append(buffer, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}