blob: 4ef19c454b16f26c4b85c721779154ebce6ace79 [file] [log] [blame]
Damien Miller17751bc2010-02-12 07:35:08 +11001/* $OpenBSD: bufaux.c,v 1.48 2010/02/02 22:49:34 djm 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 Millere3476ed2006-07-24 14:13:33 +100048
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "xmalloc.h"
Damien Millerd7834352006-08-05 12:39:39 +100050#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "log.h"
Damien Miller3f941882006-03-31 23:13:02 +110052#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Damien Miller95def091999-11-25 00:26:21 +110054/*
Damien Miller3b235662002-04-23 20:42:36 +100055 * Returns integers from the buffer (msb first).
Damien Miller95def091999-11-25 00:26:21 +110056 */
Damien Miller3b235662002-04-23 20:42:36 +100057
Darren Tucker50dbe832004-11-05 20:41:24 +110058int
59buffer_get_short_ret(u_short *ret, Buffer *buffer)
Damien Miller3b235662002-04-23 20:42:36 +100060{
61 u_char buf[2];
Ben Lindstrome1353632002-06-23 21:29:23 +000062
Darren Tucker50dbe832004-11-05 20:41:24 +110063 if (buffer_get_ret(buffer, (char *) buf, 2) == -1)
64 return (-1);
Damien Miller3f941882006-03-31 23:13:02 +110065 *ret = get_u16(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +110066 return (0);
67}
68
69u_short
70buffer_get_short(Buffer *buffer)
71{
72 u_short ret;
73
74 if (buffer_get_short_ret(&ret, buffer) == -1)
75 fatal("buffer_get_short: buffer error");
76
77 return (ret);
78}
79
80int
81buffer_get_int_ret(u_int *ret, Buffer *buffer)
82{
83 u_char buf[4];
84
85 if (buffer_get_ret(buffer, (char *) buf, 4) == -1)
86 return (-1);
Damien Miller3f941882006-03-31 23:13:02 +110087 *ret = get_u32(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +110088 return (0);
Damien Miller3b235662002-04-23 20:42:36 +100089}
90
Ben Lindstrom46c16222000-12-22 01:43:59 +000091u_int
Damien Miller95def091999-11-25 00:26:21 +110092buffer_get_int(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093{
Darren Tucker50dbe832004-11-05 20:41:24 +110094 u_int ret;
Ben Lindstrome1353632002-06-23 21:29:23 +000095
Darren Tucker50dbe832004-11-05 20:41:24 +110096 if (buffer_get_int_ret(&ret, buffer) == -1)
97 fatal("buffer_get_int: buffer error");
98
99 return (ret);
100}
101
102int
103buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer)
104{
105 u_char buf[8];
106
107 if (buffer_get_ret(buffer, (char *) buf, 8) == -1)
108 return (-1);
Damien Miller3f941882006-03-31 23:13:02 +1100109 *ret = get_u64(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +1100110 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111}
112
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000113u_int64_t
114buffer_get_int64(Buffer *buffer)
115{
Darren Tucker50dbe832004-11-05 20:41:24 +1100116 u_int64_t ret;
Ben Lindstrome1353632002-06-23 21:29:23 +0000117
Darren Tucker50dbe832004-11-05 20:41:24 +1100118 if (buffer_get_int64_ret(&ret, buffer) == -1)
119 fatal("buffer_get_int: buffer error");
120
121 return (ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000122}
123
Damien Miller95def091999-11-25 00:26:21 +1100124/*
Damien Miller3b235662002-04-23 20:42:36 +1000125 * Stores integers in the buffer, msb first.
Damien Miller95def091999-11-25 00:26:21 +1100126 */
Damien Miller5f056372000-04-16 12:31:48 +1000127void
Damien Miller3b235662002-04-23 20:42:36 +1000128buffer_put_short(Buffer *buffer, u_short value)
129{
130 char buf[2];
Ben Lindstrome1353632002-06-23 21:29:23 +0000131
Damien Miller3f941882006-03-31 23:13:02 +1100132 put_u16(buf, value);
Damien Miller3b235662002-04-23 20:42:36 +1000133 buffer_append(buffer, buf, 2);
134}
135
136void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000137buffer_put_int(Buffer *buffer, u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138{
Damien Miller95def091999-11-25 00:26:21 +1100139 char buf[4];
Ben Lindstrome1353632002-06-23 21:29:23 +0000140
Damien Miller3f941882006-03-31 23:13:02 +1100141 put_u32(buf, value);
Damien Miller95def091999-11-25 00:26:21 +1100142 buffer_append(buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143}
144
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000145void
146buffer_put_int64(Buffer *buffer, u_int64_t value)
147{
148 char buf[8];
Ben Lindstrome1353632002-06-23 21:29:23 +0000149
Damien Miller3f941882006-03-31 23:13:02 +1100150 put_u64(buf, value);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000151 buffer_append(buffer, buf, 8);
152}
153
Damien Miller95def091999-11-25 00:26:21 +1100154/*
155 * Returns an arbitrary binary string from the buffer. The string cannot
156 * be longer than 256k. The returned value points to memory allocated
157 * with xmalloc; it is the responsibility of the calling function to free
158 * the data. If length_ptr is non-NULL, the length of the returned data
159 * will be stored there. A null character will be automatically appended
160 * to the returned string, and is not counted in length.
161 */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100162void *
Darren Tucker50dbe832004-11-05 20:41:24 +1100163buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164{
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100165 u_char *value;
Ben Lindstrome1353632002-06-23 21:29:23 +0000166 u_int len;
167
Damien Miller95def091999-11-25 00:26:21 +1100168 /* Get the length. */
Damien Miller17751bc2010-02-12 07:35:08 +1100169 if (buffer_get_int_ret(&len, buffer) != 0) {
170 error("buffer_get_string_ret: cannot extract length");
171 return (NULL);
172 }
Darren Tucker50dbe832004-11-05 20:41:24 +1100173 if (len > 256 * 1024) {
174 error("buffer_get_string_ret: bad string length %u", len);
175 return (NULL);
176 }
Damien Miller95def091999-11-25 00:26:21 +1100177 /* Allocate space for the string. Add one byte for a null character. */
178 value = xmalloc(len + 1);
179 /* Get the string. */
Darren Tucker50dbe832004-11-05 20:41:24 +1100180 if (buffer_get_ret(buffer, value, len) == -1) {
181 error("buffer_get_string_ret: buffer_get failed");
182 xfree(value);
183 return (NULL);
184 }
Damien Miller95def091999-11-25 00:26:21 +1100185 /* Append a null character to make processing easier. */
Darren Tucker2a8b1382008-06-11 09:35:37 +1000186 value[len] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100187 /* Optionally return the length of the string. */
188 if (length_ptr)
189 *length_ptr = len;
Darren Tucker50dbe832004-11-05 20:41:24 +1100190 return (value);
191}
192
193void *
194buffer_get_string(Buffer *buffer, u_int *length_ptr)
195{
196 void *ret;
197
198 if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
199 fatal("buffer_get_string: buffer error");
200 return (ret);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201}
202
Damien Millerdb255ca2008-05-19 14:59:37 +1000203void *
Darren Tuckerebc71d92010-01-12 19:45:59 +1100204buffer_get_string_ptr_ret(Buffer *buffer, u_int *length_ptr)
Damien Millerdb255ca2008-05-19 14:59:37 +1000205{
206 void *ptr;
207 u_int len;
208
Darren Tuckerebc71d92010-01-12 19:45:59 +1100209 if (buffer_get_int_ret(&len, buffer) != 0)
210 return NULL;
211 if (len > 256 * 1024) {
212 error("buffer_get_string_ptr: bad string length %u", len);
213 return NULL;
214 }
Damien Millerdb255ca2008-05-19 14:59:37 +1000215 ptr = buffer_ptr(buffer);
216 buffer_consume(buffer, len);
217 if (length_ptr)
218 *length_ptr = len;
219 return (ptr);
220}
221
Darren Tuckerebc71d92010-01-12 19:45:59 +1100222void *
223buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr)
224{
225 void *ret;
226
227 if ((ret = buffer_get_string_ptr_ret(buffer, length_ptr)) == NULL)
228 fatal("buffer_get_string_ptr: buffer error");
229 return (ret);
230}
231
Damien Miller95def091999-11-25 00:26:21 +1100232/*
233 * Stores and arbitrary binary string in the buffer.
234 */
Damien Miller5f056372000-04-16 12:31:48 +1000235void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000236buffer_put_string(Buffer *buffer, const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000237{
Damien Miller95def091999-11-25 00:26:21 +1100238 buffer_put_int(buffer, len);
239 buffer_append(buffer, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
Damien Miller5f056372000-04-16 12:31:48 +1000241void
Damien Millerb38eff82000-04-01 11:09:21 +1000242buffer_put_cstring(Buffer *buffer, const char *s)
243{
Ben Lindstrom88aa1b42002-03-22 01:47:52 +0000244 if (s == NULL)
245 fatal("buffer_put_cstring: s == NULL");
Damien Millerb38eff82000-04-01 11:09:21 +1000246 buffer_put_string(buffer, s, strlen(s));
247}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000248
Damien Miller95def091999-11-25 00:26:21 +1100249/*
250 * Returns a character from the buffer (0 - 255).
251 */
Damien Miller5f056372000-04-16 12:31:48 +1000252int
Darren Tucker50dbe832004-11-05 20:41:24 +1100253buffer_get_char_ret(char *ret, Buffer *buffer)
254{
255 if (buffer_get_ret(buffer, ret, 1) == -1) {
256 error("buffer_get_char_ret: buffer_get_ret failed");
257 return (-1);
258 }
259 return (0);
260}
261
262int
Damien Miller95def091999-11-25 00:26:21 +1100263buffer_get_char(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264{
Damien Miller95def091999-11-25 00:26:21 +1100265 char ch;
Ben Lindstrome1353632002-06-23 21:29:23 +0000266
Darren Tucker50dbe832004-11-05 20:41:24 +1100267 if (buffer_get_char_ret(&ch, buffer) == -1)
268 fatal("buffer_get_char: buffer error");
Ben Lindstrom46c16222000-12-22 01:43:59 +0000269 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270}
271
Damien Miller95def091999-11-25 00:26:21 +1100272/*
273 * Stores a character in the buffer.
274 */
Damien Miller5f056372000-04-16 12:31:48 +1000275void
Damien Miller95def091999-11-25 00:26:21 +1100276buffer_put_char(Buffer *buffer, int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000277{
Damien Miller95def091999-11-25 00:26:21 +1100278 char ch = value;
Ben Lindstrome1353632002-06-23 21:29:23 +0000279
Damien Miller95def091999-11-25 00:26:21 +1100280 buffer_append(buffer, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000281}