blob: 4c9cb662c3610c09e3ce804b211d97f51d1504a8 [file] [log] [blame]
Damien Miller58629fa2006-04-23 12:08:19 +10001/* $OpenBSD: bufaux.c,v 1.42 2006/04/18 10:44:28 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 Millerd4a8b7e1999-10-27 13:42:43 +100042#include <openssl/bn.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043#include "bufaux.h"
44#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "log.h"
Damien Miller3f941882006-03-31 23:13:02 +110046#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
Damien Miller95def091999-11-25 00:26:21 +110048/*
Damien Miller3b235662002-04-23 20:42:36 +100049 * Returns integers from the buffer (msb first).
Damien Miller95def091999-11-25 00:26:21 +110050 */
Damien Miller3b235662002-04-23 20:42:36 +100051
Darren Tucker50dbe832004-11-05 20:41:24 +110052int
53buffer_get_short_ret(u_short *ret, Buffer *buffer)
Damien Miller3b235662002-04-23 20:42:36 +100054{
55 u_char buf[2];
Ben Lindstrome1353632002-06-23 21:29:23 +000056
Darren Tucker50dbe832004-11-05 20:41:24 +110057 if (buffer_get_ret(buffer, (char *) buf, 2) == -1)
58 return (-1);
Damien Miller3f941882006-03-31 23:13:02 +110059 *ret = get_u16(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +110060 return (0);
61}
62
63u_short
64buffer_get_short(Buffer *buffer)
65{
66 u_short ret;
67
68 if (buffer_get_short_ret(&ret, buffer) == -1)
69 fatal("buffer_get_short: buffer error");
70
71 return (ret);
72}
73
74int
75buffer_get_int_ret(u_int *ret, Buffer *buffer)
76{
77 u_char buf[4];
78
79 if (buffer_get_ret(buffer, (char *) buf, 4) == -1)
80 return (-1);
Damien Miller3f941882006-03-31 23:13:02 +110081 *ret = get_u32(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +110082 return (0);
Damien Miller3b235662002-04-23 20:42:36 +100083}
84
Ben Lindstrom46c16222000-12-22 01:43:59 +000085u_int
Damien Miller95def091999-11-25 00:26:21 +110086buffer_get_int(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087{
Darren Tucker50dbe832004-11-05 20:41:24 +110088 u_int ret;
Ben Lindstrome1353632002-06-23 21:29:23 +000089
Darren Tucker50dbe832004-11-05 20:41:24 +110090 if (buffer_get_int_ret(&ret, buffer) == -1)
91 fatal("buffer_get_int: buffer error");
92
93 return (ret);
94}
95
96int
97buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer)
98{
99 u_char buf[8];
100
101 if (buffer_get_ret(buffer, (char *) buf, 8) == -1)
102 return (-1);
Damien Miller3f941882006-03-31 23:13:02 +1100103 *ret = get_u64(buf);
Darren Tucker50dbe832004-11-05 20:41:24 +1100104 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105}
106
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000107u_int64_t
108buffer_get_int64(Buffer *buffer)
109{
Darren Tucker50dbe832004-11-05 20:41:24 +1100110 u_int64_t ret;
Ben Lindstrome1353632002-06-23 21:29:23 +0000111
Darren Tucker50dbe832004-11-05 20:41:24 +1100112 if (buffer_get_int64_ret(&ret, buffer) == -1)
113 fatal("buffer_get_int: buffer error");
114
115 return (ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000116}
117
Damien Miller95def091999-11-25 00:26:21 +1100118/*
Damien Miller3b235662002-04-23 20:42:36 +1000119 * Stores integers in the buffer, msb first.
Damien Miller95def091999-11-25 00:26:21 +1100120 */
Damien Miller5f056372000-04-16 12:31:48 +1000121void
Damien Miller3b235662002-04-23 20:42:36 +1000122buffer_put_short(Buffer *buffer, u_short value)
123{
124 char buf[2];
Ben Lindstrome1353632002-06-23 21:29:23 +0000125
Damien Miller3f941882006-03-31 23:13:02 +1100126 put_u16(buf, value);
Damien Miller3b235662002-04-23 20:42:36 +1000127 buffer_append(buffer, buf, 2);
128}
129
130void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000131buffer_put_int(Buffer *buffer, u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132{
Damien Miller95def091999-11-25 00:26:21 +1100133 char buf[4];
Ben Lindstrome1353632002-06-23 21:29:23 +0000134
Damien Miller3f941882006-03-31 23:13:02 +1100135 put_u32(buf, value);
Damien Miller95def091999-11-25 00:26:21 +1100136 buffer_append(buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137}
138
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000139void
140buffer_put_int64(Buffer *buffer, u_int64_t value)
141{
142 char buf[8];
Ben Lindstrome1353632002-06-23 21:29:23 +0000143
Damien Miller3f941882006-03-31 23:13:02 +1100144 put_u64(buf, value);
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000145 buffer_append(buffer, buf, 8);
146}
147
Damien Miller95def091999-11-25 00:26:21 +1100148/*
149 * Returns an arbitrary binary string from the buffer. The string cannot
150 * be longer than 256k. The returned value points to memory allocated
151 * with xmalloc; it is the responsibility of the calling function to free
152 * the data. If length_ptr is non-NULL, the length of the returned data
153 * will be stored there. A null character will be automatically appended
154 * to the returned string, and is not counted in length.
155 */
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100156void *
Darren Tucker50dbe832004-11-05 20:41:24 +1100157buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158{
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100159 u_char *value;
Ben Lindstrome1353632002-06-23 21:29:23 +0000160 u_int len;
161
Damien Miller95def091999-11-25 00:26:21 +1100162 /* Get the length. */
163 len = buffer_get_int(buffer);
Darren Tucker50dbe832004-11-05 20:41:24 +1100164 if (len > 256 * 1024) {
165 error("buffer_get_string_ret: bad string length %u", len);
166 return (NULL);
167 }
Damien Miller95def091999-11-25 00:26:21 +1100168 /* Allocate space for the string. Add one byte for a null character. */
169 value = xmalloc(len + 1);
170 /* Get the string. */
Darren Tucker50dbe832004-11-05 20:41:24 +1100171 if (buffer_get_ret(buffer, value, len) == -1) {
172 error("buffer_get_string_ret: buffer_get failed");
173 xfree(value);
174 return (NULL);
175 }
Damien Miller95def091999-11-25 00:26:21 +1100176 /* Append a null character to make processing easier. */
177 value[len] = 0;
178 /* Optionally return the length of the string. */
179 if (length_ptr)
180 *length_ptr = len;
Darren Tucker50dbe832004-11-05 20:41:24 +1100181 return (value);
182}
183
184void *
185buffer_get_string(Buffer *buffer, u_int *length_ptr)
186{
187 void *ret;
188
189 if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
190 fatal("buffer_get_string: buffer error");
191 return (ret);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192}
193
Damien Miller95def091999-11-25 00:26:21 +1100194/*
195 * Stores and arbitrary binary string in the buffer.
196 */
Damien Miller5f056372000-04-16 12:31:48 +1000197void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000198buffer_put_string(Buffer *buffer, const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199{
Damien Miller95def091999-11-25 00:26:21 +1100200 buffer_put_int(buffer, len);
201 buffer_append(buffer, buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202}
Damien Miller5f056372000-04-16 12:31:48 +1000203void
Damien Millerb38eff82000-04-01 11:09:21 +1000204buffer_put_cstring(Buffer *buffer, const char *s)
205{
Ben Lindstrom88aa1b42002-03-22 01:47:52 +0000206 if (s == NULL)
207 fatal("buffer_put_cstring: s == NULL");
Damien Millerb38eff82000-04-01 11:09:21 +1000208 buffer_put_string(buffer, s, strlen(s));
209}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210
Damien Miller95def091999-11-25 00:26:21 +1100211/*
212 * Returns a character from the buffer (0 - 255).
213 */
Damien Miller5f056372000-04-16 12:31:48 +1000214int
Darren Tucker50dbe832004-11-05 20:41:24 +1100215buffer_get_char_ret(char *ret, Buffer *buffer)
216{
217 if (buffer_get_ret(buffer, ret, 1) == -1) {
218 error("buffer_get_char_ret: buffer_get_ret failed");
219 return (-1);
220 }
221 return (0);
222}
223
224int
Damien Miller95def091999-11-25 00:26:21 +1100225buffer_get_char(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000226{
Damien Miller95def091999-11-25 00:26:21 +1100227 char ch;
Ben Lindstrome1353632002-06-23 21:29:23 +0000228
Darren Tucker50dbe832004-11-05 20:41:24 +1100229 if (buffer_get_char_ret(&ch, buffer) == -1)
230 fatal("buffer_get_char: buffer error");
Ben Lindstrom46c16222000-12-22 01:43:59 +0000231 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232}
233
Damien Miller95def091999-11-25 00:26:21 +1100234/*
235 * Stores a character in the buffer.
236 */
Damien Miller5f056372000-04-16 12:31:48 +1000237void
Damien Miller95def091999-11-25 00:26:21 +1100238buffer_put_char(Buffer *buffer, int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239{
Damien Miller95def091999-11-25 00:26:21 +1100240 char ch = value;
Ben Lindstrome1353632002-06-23 21:29:23 +0000241
Damien Miller95def091999-11-25 00:26:21 +1100242 buffer_append(buffer, &ch, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000243}