1 /* $OpenBSD: bufaux.c,v 1.57 2014/04/16 23:22:45 djm Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Auxiliary functions for storing and retrieving various data types to/from
9 * 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".
16 * SSH2 packet format added by Markus Friedl
17 * Copyright (c) 2000 Markus Friedl. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
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.
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.
43 #include <sys/types.h>
45 #include <openssl/bn.h>
57 * Returns integers from the buffer (msb first).
61 buffer_get_short_ret(u_short *ret, Buffer *buffer)
65 if (buffer_get_ret(buffer, (char *) buf, 2) == -1)
72 buffer_get_short(Buffer *buffer)
76 if (buffer_get_short_ret(&ret, buffer) == -1)
77 fatal("buffer_get_short: buffer error");
83 buffer_get_int_ret(u_int *ret, Buffer *buffer)
87 if (buffer_get_ret(buffer, (char *) buf, 4) == -1)
95 buffer_get_int(Buffer *buffer)
99 if (buffer_get_int_ret(&ret, buffer) == -1)
100 fatal("buffer_get_int: buffer error");
106 buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer)
110 if (buffer_get_ret(buffer, (char *) buf, 8) == -1)
118 buffer_get_int64(Buffer *buffer)
122 if (buffer_get_int64_ret(&ret, buffer) == -1)
123 fatal("buffer_get_int: buffer error");
129 * Stores integers in the buffer, msb first.
132 buffer_put_short(Buffer *buffer, u_short value)
137 buffer_append(buffer, buf, 2);
141 buffer_put_int(Buffer *buffer, u_int value)
146 buffer_append(buffer, buf, 4);
150 buffer_put_int64(Buffer *buffer, u_int64_t value)
155 buffer_append(buffer, buf, 8);
159 * Returns an arbitrary binary string from the buffer. The string cannot
160 * be longer than 256k. The returned value points to memory allocated
161 * with xmalloc; it is the responsibility of the calling function to free
162 * the data. If length_ptr is non-NULL, the length of the returned data
163 * will be stored there. A null character will be automatically appended
164 * to the returned string, and is not counted in length.
167 buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
172 /* Get the length. */
173 if (buffer_get_int_ret(&len, buffer) != 0) {
174 error("buffer_get_string_ret: cannot extract length");
177 if (len > 256 * 1024) {
178 error("buffer_get_string_ret: bad string length %u", len);
181 /* Allocate space for the string. Add one byte for a null character. */
182 value = xmalloc(len + 1);
183 /* Get the string. */
184 if (buffer_get_ret(buffer, value, len) == -1) {
185 error("buffer_get_string_ret: buffer_get failed");
189 /* Append a null character to make processing easier. */
191 /* Optionally return the length of the string. */
198 buffer_get_string(Buffer *buffer, u_int *length_ptr)
202 if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
203 fatal("buffer_get_string: buffer error");
208 buffer_get_cstring_ret(Buffer *buffer, u_int *length_ptr)
211 char *cp, *ret = buffer_get_string_ret(buffer, &length);
215 if ((cp = memchr(ret, '\0', length)) != NULL) {
216 /* XXX allow \0 at end-of-string for a while, remove later */
217 if (cp == ret + length - 1)
218 error("buffer_get_cstring_ret: string contains \\0");
220 explicit_bzero(ret, length);
225 if (length_ptr != NULL)
226 *length_ptr = length;
231 buffer_get_cstring(Buffer *buffer, u_int *length_ptr)
235 if ((ret = buffer_get_cstring_ret(buffer, length_ptr)) == NULL)
236 fatal("buffer_get_cstring: buffer error");
241 buffer_get_string_ptr_ret(Buffer *buffer, u_int *length_ptr)
246 if (buffer_get_int_ret(&len, buffer) != 0)
248 if (len > 256 * 1024) {
249 error("buffer_get_string_ptr: bad string length %u", len);
252 ptr = buffer_ptr(buffer);
253 buffer_consume(buffer, len);
260 buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr)
264 if ((ret = buffer_get_string_ptr_ret(buffer, length_ptr)) == NULL)
265 fatal("buffer_get_string_ptr: buffer error");
270 * Stores and arbitrary binary string in the buffer.
273 buffer_put_string(Buffer *buffer, const void *buf, u_int len)
275 buffer_put_int(buffer, len);
276 buffer_append(buffer, buf, len);
279 buffer_put_cstring(Buffer *buffer, const char *s)
282 fatal("buffer_put_cstring: s == NULL");
283 buffer_put_string(buffer, s, strlen(s));
287 * Returns a character from the buffer (0 - 255).
290 buffer_get_char_ret(u_char *ret, Buffer *buffer)
292 if (buffer_get_ret(buffer, ret, 1) == -1) {
293 error("buffer_get_char_ret: buffer_get_ret failed");
300 buffer_get_char(Buffer *buffer)
304 if (buffer_get_char_ret(&ch, buffer) == -1)
305 fatal("buffer_get_char: buffer error");
310 * Stores a character in the buffer.
313 buffer_put_char(Buffer *buffer, int value)
317 buffer_append(buffer, &ch, 1);
320 /* Pseudo bignum functions */
323 buffer_get_bignum2_as_string_ret(Buffer *buffer, u_int *length_ptr)
326 u_char *bin, *p, *ret;
328 if ((p = bin = buffer_get_string_ret(buffer, &len)) == NULL) {
329 error("%s: invalid bignum", __func__);
333 if (len > 0 && (bin[0] & 0x80)) {
334 error("%s: negative numbers not supported", __func__);
338 if (len > 8 * 1024) {
339 error("%s: cannot handle BN of size %d", __func__, len);
343 /* Skip zero prefix on numbers with the MSB set */
344 if (len > 1 && bin[0] == 0x00 && (bin[1] & 0x80) != 0) {
350 explicit_bzero(p, len);
356 buffer_get_bignum2_as_string(Buffer *buffer, u_int *l)
358 void *ret = buffer_get_bignum2_as_string_ret(buffer, l);
361 fatal("%s: buffer error", __func__);
366 * Stores a string using the bignum encoding rules (\0 pad if MSB set).
369 buffer_put_bignum2_from_string(Buffer *buffer, const u_char *s, u_int l)
375 fatal("%s: length %u too long", __func__, l);
376 /* Skip leading zero bytes */
377 for (; l > 0 && *s == 0; l--, s++)
379 p = buf = xmalloc(l + 1);
381 * If most significant bit is set then prepend a zero byte to
382 * avoid interpretation as a negative number.
384 if (l > 0 && (s[0] & 0x80) != 0) {
389 buffer_put_string(buffer, buf, l + pad);
390 explicit_bzero(buf, l + pad);