blob: a384cc6853cbff008b3e4c3d85ad0ebf2384d707 [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: bufaux.c,v 1.43 2006/07/22 20:48:22 stevesk 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 Millerd4a8b7e1999-10-27 13:42:43 +100042#include <openssl/bn.h>
Damien Millere3476ed2006-07-24 14:13:33 +100043
44#include <string.h>
45
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046#include "bufaux.h"
47#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "log.h"
Damien Miller3f941882006-03-31 23:13:02 +110049#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
Damien Miller95def091999-11-25 00:26:21 +110051/*
Damien Miller3b235662002-04-23 20:42:36 +100052 * Returns integers from the buffer (msb first).
Damien Miller95def091999-11-25 00:26:21 +110053 */
Damien Miller3b235662002-04-23 20:42:36 +100054
Darren Tucker50dbe832004-11-05 20:41:24 +110055int
56buffer_get_short_ret(u_short *ret, Buffer *buffer)
Damien Miller3b235662002-04-23 20:42:36 +100057{
58 u_char buf[2];
Ben Lindstrome1353632002-06-23 21:29:23 +000059
Darren Tucker50dbe832004-11-05 20:41:24 +110060 if (buffer_get_ret(buffer, (char *) buf, 2) == -1)
61 return (-1);
Damien Miller3f941882006-03-31 23:13:02 +110062 *ret = get_u16(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +110063 return (0);
64}
65
66u_short
67buffer_get_short(Buffer *buffer)
68{
69 u_short ret;
70
71 if (buffer_get_short_ret(&ret, buffer) == -1)
72 fatal("buffer_get_short: buffer error");
73
74 return (ret);
75}
76
77int
78buffer_get_int_ret(u_int *ret, Buffer *buffer)
79{
80 u_char buf[4];
81
82 if (buffer_get_ret(buffer, (char *) buf, 4) == -1)
83 return (-1);
Damien Miller3f941882006-03-31 23:13:02 +110084 *ret = get_u32(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +110085 return (0);
Damien Miller3b235662002-04-23 20:42:36 +100086}
87
Ben Lindstrom46c16222000-12-22 01:43:59 +000088u_int
Damien Miller95def091999-11-25 00:26:21 +110089buffer_get_int(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090{
Darren Tucker50dbe832004-11-05 20:41:24 +110091 u_int ret;
Ben Lindstrome1353632002-06-23 21:29:23 +000092
Darren Tucker50dbe832004-11-05 20:41:24 +110093 if (buffer_get_int_ret(&ret, buffer) == -1)
94 fatal("buffer_get_int: buffer error");
95
96 return (ret);
97}
98
99int
100buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer)
101{
102 u_char buf[8];
103
104 if (buffer_get_ret(buffer, (char *) buf, 8) == -1)
105 return (-1);
Damien Miller3f941882006-03-31 23:13:02 +1100106 *ret = get_u64(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +1100107 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108}
109
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000110u_int64_t
111buffer_get_int64(Buffer *buffer)
112{
Darren Tucker50dbe832004-11-05 20:41:24 +1100113 u_int64_t ret;
Ben Lindstrome1353632002-06-23 21:29:23 +0000114
Darren Tucker50dbe832004-11-05 20:41:24 +1100115 if (buffer_get_int64_ret(&ret, buffer) == -1)
116 fatal("buffer_get_int: buffer error");
117
118 return (ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000119}
120
Damien Miller95def091999-11-25 00:26:21 +1100121/*
Damien Miller3b235662002-04-23 20:42:36 +1000122 * Stores integers in the buffer, msb first.
Damien Miller95def091999-11-25 00:26:21 +1100123 */
Damien Miller5f056372000-04-16 12:31:48 +1000124void
Damien Miller3b235662002-04-23 20:42:36 +1000125buffer_put_short(Buffer *buffer, u_short value)
126{
127 char buf[2];
Ben Lindstrome1353632002-06-23 21:29:23 +0000128
Damien Miller3f941882006-03-31 23:13:02 +1100129 put_u16(buf, value);
Damien Miller3b235662002-04-23 20:42:36 +1000130 buffer_append(buffer, buf, 2);
131}
132
133void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000134buffer_put_int(Buffer *buffer, u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135{
Damien Miller95def091999-11-25 00:26:21 +1100136 char buf[4];
Ben Lindstrome1353632002-06-23 21:29:23 +0000137
Damien Miller3f941882006-03-31 23:13:02 +1100138 put_u32(buf, value);
Damien Miller95def091999-11-25 00:26:21 +1100139 buffer_append(buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140}
141
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000142void
143buffer_put_int64(Buffer *buffer, u_int64_t value)
144{
145 char buf[8];
Ben Lindstrome1353632002-06-23 21:29:23 +0000146
Damien Miller3f941882006-03-31 23:13:02 +1100147 put_u64(buf, value);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000148 buffer_append(buffer, buf, 8);
149}
150
Damien Miller95def091999-11-25 00:26:21 +1100151/*
152 * Returns an arbitrary binary string from the buffer. The string cannot
153 * be longer than 256k. The returned value points to memory allocated
154 * with xmalloc; it is the responsibility of the calling function to free
155 * the data. If length_ptr is non-NULL, the length of the returned data
156 * will be stored there. A null character will be automatically appended
157 * to the returned string, and is not counted in length.
158 */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100159void *
Darren Tucker50dbe832004-11-05 20:41:24 +1100160buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161{
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100162 u_char *value;
Ben Lindstrome1353632002-06-23 21:29:23 +0000163 u_int len;
164
Damien Miller95def091999-11-25 00:26:21 +1100165 /* Get the length. */
166 len = buffer_get_int(buffer);
Darren Tucker50dbe832004-11-05 20:41:24 +1100167 if (len > 256 * 1024) {
168 error("buffer_get_string_ret: bad string length %u", len);
169 return (NULL);
170 }
Damien Miller95def091999-11-25 00:26:21 +1100171 /* Allocate space for the string. Add one byte for a null character. */
172 value = xmalloc(len + 1);
173 /* Get the string. */
Darren Tucker50dbe832004-11-05 20:41:24 +1100174 if (buffer_get_ret(buffer, value, len) == -1) {
175 error("buffer_get_string_ret: buffer_get failed");
176 xfree(value);
177 return (NULL);
178 }
Damien Miller95def091999-11-25 00:26:21 +1100179 /* Append a null character to make processing easier. */
180 value[len] = 0;
181 /* Optionally return the length of the string. */
182 if (length_ptr)
183 *length_ptr = len;
Darren Tucker50dbe832004-11-05 20:41:24 +1100184 return (value);
185}
186
187void *
188buffer_get_string(Buffer *buffer, u_int *length_ptr)
189{
190 void *ret;
191
192 if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
193 fatal("buffer_get_string: buffer error");
194 return (ret);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195}
196
Damien Miller95def091999-11-25 00:26:21 +1100197/*
198 * Stores and arbitrary binary string in the buffer.
199 */
Damien Miller5f056372000-04-16 12:31:48 +1000200void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000201buffer_put_string(Buffer *buffer, const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202{
Damien Miller95def091999-11-25 00:26:21 +1100203 buffer_put_int(buffer, len);
204 buffer_append(buffer, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000205}
Damien Miller5f056372000-04-16 12:31:48 +1000206void
Damien Millerb38eff82000-04-01 11:09:21 +1000207buffer_put_cstring(Buffer *buffer, const char *s)
208{
Ben Lindstrom88aa1b42002-03-22 01:47:52 +0000209 if (s == NULL)
210 fatal("buffer_put_cstring: s == NULL");
Damien Millerb38eff82000-04-01 11:09:21 +1000211 buffer_put_string(buffer, s, strlen(s));
212}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213
Damien Miller95def091999-11-25 00:26:21 +1100214/*
215 * Returns a character from the buffer (0 - 255).
216 */
Damien Miller5f056372000-04-16 12:31:48 +1000217int
Darren Tucker50dbe832004-11-05 20:41:24 +1100218buffer_get_char_ret(char *ret, Buffer *buffer)
219{
220 if (buffer_get_ret(buffer, ret, 1) == -1) {
221 error("buffer_get_char_ret: buffer_get_ret failed");
222 return (-1);
223 }
224 return (0);
225}
226
227int
Damien Miller95def091999-11-25 00:26:21 +1100228buffer_get_char(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000229{
Damien Miller95def091999-11-25 00:26:21 +1100230 char ch;
Ben Lindstrome1353632002-06-23 21:29:23 +0000231
Darren Tucker50dbe832004-11-05 20:41:24 +1100232 if (buffer_get_char_ret(&ch, buffer) == -1)
233 fatal("buffer_get_char: buffer error");
Ben Lindstrom46c16222000-12-22 01:43:59 +0000234 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235}
236
Damien Miller95def091999-11-25 00:26:21 +1100237/*
238 * Stores a character in the buffer.
239 */
Damien Miller5f056372000-04-16 12:31:48 +1000240void
Damien Miller95def091999-11-25 00:26:21 +1100241buffer_put_char(Buffer *buffer, int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242{
Damien Miller95def091999-11-25 00:26:21 +1100243 char ch = value;
Ben Lindstrome1353632002-06-23 21:29:23 +0000244
Damien Miller95def091999-11-25 00:26:21 +1100245 buffer_append(buffer, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246}