blob: 3e70b8d90685d00099db625aad3ccf8b1ff5ed4f [file] [log] [blame]
Damien Miller0600c702013-11-21 13:55:43 +11001/* $OpenBSD: bufaux.c,v 1.53 2013/11/08 11:15:19 dtucker Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Auxiliary functions for storing and retrieving various data types to/from
7 * Buffers.
8 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Millerb38eff82000-04-01 11:09:21 +100014 *
Damien Millere4340be2000-09-16 13:29:08 +110015 *
16 * SSH2 packet format added by Markus Friedl
17 * Copyright (c) 2000 Markus Friedl. All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110038 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
Damien Millerd7834352006-08-05 12:39:39 +100042#include <sys/types.h>
43
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044#include <openssl/bn.h>
Damien Millere3476ed2006-07-24 14:13:33 +100045
46#include <string.h>
Damien Millerd7834352006-08-05 12:39:39 +100047#include <stdarg.h>
Damien Miller0600c702013-11-21 13:55:43 +110048#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100049
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100051#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "log.h"
Damien Miller3f941882006-03-31 23:13:02 +110053#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller95def091999-11-25 00:26:21 +110055/*
Damien Miller3b235662002-04-23 20:42:36 +100056 * Returns integers from the buffer (msb first).
Damien Miller95def091999-11-25 00:26:21 +110057 */
Damien Miller3b235662002-04-23 20:42:36 +100058
Darren Tucker50dbe832004-11-05 20:41:24 +110059int
60buffer_get_short_ret(u_short *ret, Buffer *buffer)
Damien Miller3b235662002-04-23 20:42:36 +100061{
62 u_char buf[2];
Ben Lindstrome1353632002-06-23 21:29:23 +000063
Darren Tucker50dbe832004-11-05 20:41:24 +110064 if (buffer_get_ret(buffer, (char *) buf, 2) == -1)
65 return (-1);
Damien Miller3f941882006-03-31 23:13:02 +110066 *ret = get_u16(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +110067 return (0);
68}
69
70u_short
71buffer_get_short(Buffer *buffer)
72{
73 u_short ret;
74
75 if (buffer_get_short_ret(&ret, buffer) == -1)
76 fatal("buffer_get_short: buffer error");
77
78 return (ret);
79}
80
81int
82buffer_get_int_ret(u_int *ret, Buffer *buffer)
83{
84 u_char buf[4];
85
86 if (buffer_get_ret(buffer, (char *) buf, 4) == -1)
87 return (-1);
Damien Millerd6fc3062010-04-16 15:51:45 +100088 if (ret != NULL)
89 *ret = get_u32(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +110090 return (0);
Damien Miller3b235662002-04-23 20:42:36 +100091}
92
Ben Lindstrom46c16222000-12-22 01:43:59 +000093u_int
Damien Miller95def091999-11-25 00:26:21 +110094buffer_get_int(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095{
Darren Tucker50dbe832004-11-05 20:41:24 +110096 u_int ret;
Ben Lindstrome1353632002-06-23 21:29:23 +000097
Darren Tucker50dbe832004-11-05 20:41:24 +110098 if (buffer_get_int_ret(&ret, buffer) == -1)
99 fatal("buffer_get_int: buffer error");
100
101 return (ret);
102}
103
104int
105buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer)
106{
107 u_char buf[8];
108
109 if (buffer_get_ret(buffer, (char *) buf, 8) == -1)
110 return (-1);
Damien Millerd6fc3062010-04-16 15:51:45 +1000111 if (ret != NULL)
112 *ret = get_u64(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +1100113 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114}
115
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000116u_int64_t
117buffer_get_int64(Buffer *buffer)
118{
Darren Tucker50dbe832004-11-05 20:41:24 +1100119 u_int64_t ret;
Ben Lindstrome1353632002-06-23 21:29:23 +0000120
Darren Tucker50dbe832004-11-05 20:41:24 +1100121 if (buffer_get_int64_ret(&ret, buffer) == -1)
122 fatal("buffer_get_int: buffer error");
123
124 return (ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000125}
126
Damien Miller95def091999-11-25 00:26:21 +1100127/*
Damien Miller3b235662002-04-23 20:42:36 +1000128 * Stores integers in the buffer, msb first.
Damien Miller95def091999-11-25 00:26:21 +1100129 */
Damien Miller5f056372000-04-16 12:31:48 +1000130void
Damien Miller3b235662002-04-23 20:42:36 +1000131buffer_put_short(Buffer *buffer, u_short value)
132{
133 char buf[2];
Ben Lindstrome1353632002-06-23 21:29:23 +0000134
Damien Miller3f941882006-03-31 23:13:02 +1100135 put_u16(buf, value);
Damien Miller3b235662002-04-23 20:42:36 +1000136 buffer_append(buffer, buf, 2);
137}
138
139void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000140buffer_put_int(Buffer *buffer, u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141{
Damien Miller95def091999-11-25 00:26:21 +1100142 char buf[4];
Ben Lindstrome1353632002-06-23 21:29:23 +0000143
Damien Miller3f941882006-03-31 23:13:02 +1100144 put_u32(buf, value);
Damien Miller95def091999-11-25 00:26:21 +1100145 buffer_append(buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146}
147
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000148void
149buffer_put_int64(Buffer *buffer, u_int64_t value)
150{
151 char buf[8];
Ben Lindstrome1353632002-06-23 21:29:23 +0000152
Damien Miller3f941882006-03-31 23:13:02 +1100153 put_u64(buf, value);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000154 buffer_append(buffer, buf, 8);
155}
156
Damien Miller95def091999-11-25 00:26:21 +1100157/*
158 * Returns an arbitrary binary string from the buffer. The string cannot
159 * be longer than 256k. The returned value points to memory allocated
160 * with xmalloc; it is the responsibility of the calling function to free
161 * the data. If length_ptr is non-NULL, the length of the returned data
162 * will be stored there. A null character will be automatically appended
163 * to the returned string, and is not counted in length.
164 */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100165void *
Darren Tucker50dbe832004-11-05 20:41:24 +1100166buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167{
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100168 u_char *value;
Ben Lindstrome1353632002-06-23 21:29:23 +0000169 u_int len;
170
Damien Miller95def091999-11-25 00:26:21 +1100171 /* Get the length. */
Damien Miller17751bc2010-02-12 07:35:08 +1100172 if (buffer_get_int_ret(&len, buffer) != 0) {
173 error("buffer_get_string_ret: cannot extract length");
174 return (NULL);
175 }
Darren Tucker50dbe832004-11-05 20:41:24 +1100176 if (len > 256 * 1024) {
177 error("buffer_get_string_ret: bad string length %u", len);
178 return (NULL);
179 }
Damien Miller95def091999-11-25 00:26:21 +1100180 /* Allocate space for the string. Add one byte for a null character. */
181 value = xmalloc(len + 1);
182 /* Get the string. */
Darren Tucker50dbe832004-11-05 20:41:24 +1100183 if (buffer_get_ret(buffer, value, len) == -1) {
184 error("buffer_get_string_ret: buffer_get failed");
Darren Tuckera627d422013-06-02 07:31:17 +1000185 free(value);
Darren Tucker50dbe832004-11-05 20:41:24 +1100186 return (NULL);
187 }
Damien Miller95def091999-11-25 00:26:21 +1100188 /* Append a null character to make processing easier. */
Darren Tucker2a8b1382008-06-11 09:35:37 +1000189 value[len] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100190 /* Optionally return the length of the string. */
191 if (length_ptr)
192 *length_ptr = len;
Darren Tucker50dbe832004-11-05 20:41:24 +1100193 return (value);
194}
195
196void *
197buffer_get_string(Buffer *buffer, u_int *length_ptr)
198{
199 void *ret;
200
201 if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
202 fatal("buffer_get_string: buffer error");
203 return (ret);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204}
205
Damien Millerda108ec2010-08-31 22:36:39 +1000206char *
207buffer_get_cstring_ret(Buffer *buffer, u_int *length_ptr)
208{
209 u_int length;
210 char *cp, *ret = buffer_get_string_ret(buffer, &length);
211
212 if (ret == NULL)
213 return NULL;
214 if ((cp = memchr(ret, '\0', length)) != NULL) {
215 /* XXX allow \0 at end-of-string for a while, remove later */
216 if (cp == ret + length - 1)
217 error("buffer_get_cstring_ret: string contains \\0");
218 else {
219 bzero(ret, length);
Darren Tuckera627d422013-06-02 07:31:17 +1000220 free(ret);
Damien Millerda108ec2010-08-31 22:36:39 +1000221 return NULL;
222 }
223 }
224 if (length_ptr != NULL)
225 *length_ptr = length;
226 return ret;
227}
228
229char *
230buffer_get_cstring(Buffer *buffer, u_int *length_ptr)
231{
232 char *ret;
233
234 if ((ret = buffer_get_cstring_ret(buffer, length_ptr)) == NULL)
235 fatal("buffer_get_cstring: buffer error");
236 return ret;
237}
238
Damien Millerdb255ca2008-05-19 14:59:37 +1000239void *
Darren Tuckerebc71d92010-01-12 19:45:59 +1100240buffer_get_string_ptr_ret(Buffer *buffer, u_int *length_ptr)
Damien Millerdb255ca2008-05-19 14:59:37 +1000241{
242 void *ptr;
243 u_int len;
244
Darren Tuckerebc71d92010-01-12 19:45:59 +1100245 if (buffer_get_int_ret(&len, buffer) != 0)
246 return NULL;
247 if (len > 256 * 1024) {
248 error("buffer_get_string_ptr: bad string length %u", len);
249 return NULL;
250 }
Damien Millerdb255ca2008-05-19 14:59:37 +1000251 ptr = buffer_ptr(buffer);
252 buffer_consume(buffer, len);
253 if (length_ptr)
254 *length_ptr = len;
255 return (ptr);
256}
257
Darren Tuckerebc71d92010-01-12 19:45:59 +1100258void *
259buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr)
260{
261 void *ret;
262
263 if ((ret = buffer_get_string_ptr_ret(buffer, length_ptr)) == NULL)
264 fatal("buffer_get_string_ptr: buffer error");
265 return (ret);
266}
267
Damien Miller95def091999-11-25 00:26:21 +1100268/*
269 * Stores and arbitrary binary string in the buffer.
270 */
Damien Miller5f056372000-04-16 12:31:48 +1000271void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000272buffer_put_string(Buffer *buffer, const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273{
Damien Miller95def091999-11-25 00:26:21 +1100274 buffer_put_int(buffer, len);
275 buffer_append(buffer, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
Damien Miller5f056372000-04-16 12:31:48 +1000277void
Damien Millerb38eff82000-04-01 11:09:21 +1000278buffer_put_cstring(Buffer *buffer, const char *s)
279{
Ben Lindstrom88aa1b42002-03-22 01:47:52 +0000280 if (s == NULL)
281 fatal("buffer_put_cstring: s == NULL");
Damien Millerb38eff82000-04-01 11:09:21 +1000282 buffer_put_string(buffer, s, strlen(s));
283}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000284
Damien Miller95def091999-11-25 00:26:21 +1100285/*
286 * Returns a character from the buffer (0 - 255).
287 */
Damien Miller5f056372000-04-16 12:31:48 +1000288int
Damien Millerce986542013-07-18 16:12:44 +1000289buffer_get_char_ret(u_char *ret, Buffer *buffer)
Darren Tucker50dbe832004-11-05 20:41:24 +1100290{
291 if (buffer_get_ret(buffer, ret, 1) == -1) {
292 error("buffer_get_char_ret: buffer_get_ret failed");
293 return (-1);
294 }
295 return (0);
296}
297
298int
Damien Miller95def091999-11-25 00:26:21 +1100299buffer_get_char(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300{
Damien Millerce986542013-07-18 16:12:44 +1000301 u_char ch;
Ben Lindstrome1353632002-06-23 21:29:23 +0000302
Darren Tucker50dbe832004-11-05 20:41:24 +1100303 if (buffer_get_char_ret(&ch, buffer) == -1)
304 fatal("buffer_get_char: buffer error");
Damien Millerce986542013-07-18 16:12:44 +1000305 return ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306}
307
Damien Miller95def091999-11-25 00:26:21 +1100308/*
309 * Stores a character in the buffer.
310 */
Damien Miller5f056372000-04-16 12:31:48 +1000311void
Damien Miller95def091999-11-25 00:26:21 +1100312buffer_put_char(Buffer *buffer, int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000313{
Damien Miller95def091999-11-25 00:26:21 +1100314 char ch = value;
Ben Lindstrome1353632002-06-23 21:29:23 +0000315
Damien Miller95def091999-11-25 00:26:21 +1100316 buffer_append(buffer, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317}