Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame^] | 1 | /* $OpenBSD: bufaux.c,v 1.58 2014/04/28 03:09:18 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Auxiliary functions for storing and retrieving various data types to/from |
| 7 | * Buffers. |
| 8 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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". |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 14 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 15 | * |
| 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 38 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | |
| 40 | #include "includes.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 41 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 42 | #include <sys/types.h> |
| 43 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | #include <openssl/bn.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 45 | |
| 46 | #include <string.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 47 | #include <stdarg.h> |
Damien Miller | 0600c70 | 2013-11-21 13:55:43 +1100 | [diff] [blame] | 48 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 49 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | #include "xmalloc.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 51 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 52 | #include "log.h" |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 53 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 54 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 55 | /* |
Damien Miller | 3b23566 | 2002-04-23 20:42:36 +1000 | [diff] [blame] | 56 | * Returns integers from the buffer (msb first). |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 57 | */ |
Damien Miller | 3b23566 | 2002-04-23 20:42:36 +1000 | [diff] [blame] | 58 | |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 59 | int |
| 60 | buffer_get_short_ret(u_short *ret, Buffer *buffer) |
Damien Miller | 3b23566 | 2002-04-23 20:42:36 +1000 | [diff] [blame] | 61 | { |
| 62 | u_char buf[2]; |
Ben Lindstrom | e135363 | 2002-06-23 21:29:23 +0000 | [diff] [blame] | 63 | |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 64 | if (buffer_get_ret(buffer, (char *) buf, 2) == -1) |
| 65 | return (-1); |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 66 | *ret = get_u16(buf); |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 67 | return (0); |
| 68 | } |
| 69 | |
| 70 | u_short |
| 71 | buffer_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 | |
| 81 | int |
| 82 | buffer_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 Miller | d6fc306 | 2010-04-16 15:51:45 +1000 | [diff] [blame] | 88 | if (ret != NULL) |
| 89 | *ret = get_u32(buf); |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 90 | return (0); |
Damien Miller | 3b23566 | 2002-04-23 20:42:36 +1000 | [diff] [blame] | 91 | } |
| 92 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 93 | u_int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | buffer_get_int(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 95 | { |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 96 | u_int ret; |
Ben Lindstrom | e135363 | 2002-06-23 21:29:23 +0000 | [diff] [blame] | 97 | |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 98 | if (buffer_get_int_ret(&ret, buffer) == -1) |
| 99 | fatal("buffer_get_int: buffer error"); |
| 100 | |
| 101 | return (ret); |
| 102 | } |
| 103 | |
| 104 | int |
| 105 | buffer_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 Miller | d6fc306 | 2010-04-16 15:51:45 +1000 | [diff] [blame] | 111 | if (ret != NULL) |
| 112 | *ret = get_u64(buf); |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 113 | return (0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 114 | } |
| 115 | |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 116 | u_int64_t |
| 117 | buffer_get_int64(Buffer *buffer) |
| 118 | { |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 119 | u_int64_t ret; |
Ben Lindstrom | e135363 | 2002-06-23 21:29:23 +0000 | [diff] [blame] | 120 | |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 121 | if (buffer_get_int64_ret(&ret, buffer) == -1) |
| 122 | fatal("buffer_get_int: buffer error"); |
| 123 | |
| 124 | return (ret); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 127 | /* |
Damien Miller | 3b23566 | 2002-04-23 20:42:36 +1000 | [diff] [blame] | 128 | * Stores integers in the buffer, msb first. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 129 | */ |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 130 | void |
Damien Miller | 3b23566 | 2002-04-23 20:42:36 +1000 | [diff] [blame] | 131 | buffer_put_short(Buffer *buffer, u_short value) |
| 132 | { |
| 133 | char buf[2]; |
Ben Lindstrom | e135363 | 2002-06-23 21:29:23 +0000 | [diff] [blame] | 134 | |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 135 | put_u16(buf, value); |
Damien Miller | 3b23566 | 2002-04-23 20:42:36 +1000 | [diff] [blame] | 136 | buffer_append(buffer, buf, 2); |
| 137 | } |
| 138 | |
| 139 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 140 | buffer_put_int(Buffer *buffer, u_int value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 141 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 142 | char buf[4]; |
Ben Lindstrom | e135363 | 2002-06-23 21:29:23 +0000 | [diff] [blame] | 143 | |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 144 | put_u32(buf, value); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 145 | buffer_append(buffer, buf, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 146 | } |
| 147 | |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 148 | void |
| 149 | buffer_put_int64(Buffer *buffer, u_int64_t value) |
| 150 | { |
| 151 | char buf[8]; |
Ben Lindstrom | e135363 | 2002-06-23 21:29:23 +0000 | [diff] [blame] | 152 | |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 153 | put_u64(buf, value); |
Ben Lindstrom | 2f959b4 | 2001-01-11 06:20:23 +0000 | [diff] [blame] | 154 | buffer_append(buffer, buf, 8); |
| 155 | } |
| 156 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 157 | /* |
| 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 Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 165 | void * |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 166 | buffer_get_string_ret(Buffer *buffer, u_int *length_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 167 | { |
Damien Miller | 5a6b4fe | 2001-12-21 14:56:54 +1100 | [diff] [blame] | 168 | u_char *value; |
Ben Lindstrom | e135363 | 2002-06-23 21:29:23 +0000 | [diff] [blame] | 169 | u_int len; |
| 170 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 171 | /* Get the length. */ |
Damien Miller | 17751bc | 2010-02-12 07:35:08 +1100 | [diff] [blame] | 172 | if (buffer_get_int_ret(&len, buffer) != 0) { |
| 173 | error("buffer_get_string_ret: cannot extract length"); |
| 174 | return (NULL); |
| 175 | } |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 176 | if (len > 256 * 1024) { |
| 177 | error("buffer_get_string_ret: bad string length %u", len); |
| 178 | return (NULL); |
| 179 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 180 | /* Allocate space for the string. Add one byte for a null character. */ |
| 181 | value = xmalloc(len + 1); |
| 182 | /* Get the string. */ |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 183 | if (buffer_get_ret(buffer, value, len) == -1) { |
| 184 | error("buffer_get_string_ret: buffer_get failed"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 185 | free(value); |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 186 | return (NULL); |
| 187 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 188 | /* Append a null character to make processing easier. */ |
Darren Tucker | 2a8b138 | 2008-06-11 09:35:37 +1000 | [diff] [blame] | 189 | value[len] = '\0'; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 190 | /* Optionally return the length of the string. */ |
| 191 | if (length_ptr) |
| 192 | *length_ptr = len; |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 193 | return (value); |
| 194 | } |
| 195 | |
| 196 | void * |
| 197 | buffer_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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 204 | } |
| 205 | |
Damien Miller | da108ec | 2010-08-31 22:36:39 +1000 | [diff] [blame] | 206 | char * |
| 207 | buffer_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 { |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 219 | explicit_bzero(ret, length); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 220 | free(ret); |
Damien Miller | da108ec | 2010-08-31 22:36:39 +1000 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
| 223 | } |
| 224 | if (length_ptr != NULL) |
| 225 | *length_ptr = length; |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | char * |
| 230 | buffer_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 Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame^] | 239 | const void * |
Darren Tucker | ebc71d9 | 2010-01-12 19:45:59 +1100 | [diff] [blame] | 240 | buffer_get_string_ptr_ret(Buffer *buffer, u_int *length_ptr) |
Damien Miller | db255ca | 2008-05-19 14:59:37 +1000 | [diff] [blame] | 241 | { |
| 242 | void *ptr; |
| 243 | u_int len; |
| 244 | |
Darren Tucker | ebc71d9 | 2010-01-12 19:45:59 +1100 | [diff] [blame] | 245 | 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 Miller | db255ca | 2008-05-19 14:59:37 +1000 | [diff] [blame] | 251 | ptr = buffer_ptr(buffer); |
| 252 | buffer_consume(buffer, len); |
| 253 | if (length_ptr) |
| 254 | *length_ptr = len; |
| 255 | return (ptr); |
| 256 | } |
| 257 | |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame^] | 258 | const void * |
Darren Tucker | ebc71d9 | 2010-01-12 19:45:59 +1100 | [diff] [blame] | 259 | buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr) |
| 260 | { |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame^] | 261 | const void *ret; |
Darren Tucker | ebc71d9 | 2010-01-12 19:45:59 +1100 | [diff] [blame] | 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 268 | /* |
| 269 | * Stores and arbitrary binary string in the buffer. |
| 270 | */ |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 271 | void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 272 | buffer_put_string(Buffer *buffer, const void *buf, u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 273 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 274 | buffer_put_int(buffer, len); |
| 275 | buffer_append(buffer, buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 276 | } |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 277 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 278 | buffer_put_cstring(Buffer *buffer, const char *s) |
| 279 | { |
Ben Lindstrom | 88aa1b4 | 2002-03-22 01:47:52 +0000 | [diff] [blame] | 280 | if (s == NULL) |
| 281 | fatal("buffer_put_cstring: s == NULL"); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 282 | buffer_put_string(buffer, s, strlen(s)); |
| 283 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 284 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 285 | /* |
| 286 | * Returns a character from the buffer (0 - 255). |
| 287 | */ |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 288 | int |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 289 | buffer_get_char_ret(u_char *ret, Buffer *buffer) |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 290 | { |
| 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 | |
| 298 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 299 | buffer_get_char(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 300 | { |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 301 | u_char ch; |
Ben Lindstrom | e135363 | 2002-06-23 21:29:23 +0000 | [diff] [blame] | 302 | |
Darren Tucker | 50dbe83 | 2004-11-05 20:41:24 +1100 | [diff] [blame] | 303 | if (buffer_get_char_ret(&ch, buffer) == -1) |
| 304 | fatal("buffer_get_char: buffer error"); |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 305 | return ch; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 306 | } |
| 307 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 308 | /* |
| 309 | * Stores a character in the buffer. |
| 310 | */ |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 311 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 312 | buffer_put_char(Buffer *buffer, int value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 313 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 314 | char ch = value; |
Ben Lindstrom | e135363 | 2002-06-23 21:29:23 +0000 | [diff] [blame] | 315 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 316 | buffer_append(buffer, &ch, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 317 | } |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 318 | |
| 319 | /* Pseudo bignum functions */ |
| 320 | |
| 321 | void * |
| 322 | buffer_get_bignum2_as_string_ret(Buffer *buffer, u_int *length_ptr) |
| 323 | { |
| 324 | u_int len; |
| 325 | u_char *bin, *p, *ret; |
| 326 | |
| 327 | if ((p = bin = buffer_get_string_ret(buffer, &len)) == NULL) { |
| 328 | error("%s: invalid bignum", __func__); |
| 329 | return NULL; |
| 330 | } |
| 331 | |
| 332 | if (len > 0 && (bin[0] & 0x80)) { |
| 333 | error("%s: negative numbers not supported", __func__); |
| 334 | free(bin); |
| 335 | return NULL; |
| 336 | } |
| 337 | if (len > 8 * 1024) { |
| 338 | error("%s: cannot handle BN of size %d", __func__, len); |
| 339 | free(bin); |
| 340 | return NULL; |
| 341 | } |
| 342 | /* Skip zero prefix on numbers with the MSB set */ |
| 343 | if (len > 1 && bin[0] == 0x00 && (bin[1] & 0x80) != 0) { |
| 344 | p++; |
| 345 | len--; |
| 346 | } |
| 347 | ret = xmalloc(len); |
| 348 | memcpy(ret, p, len); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 349 | explicit_bzero(p, len); |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 350 | free(bin); |
| 351 | return ret; |
| 352 | } |
| 353 | |
| 354 | void * |
| 355 | buffer_get_bignum2_as_string(Buffer *buffer, u_int *l) |
| 356 | { |
| 357 | void *ret = buffer_get_bignum2_as_string_ret(buffer, l); |
| 358 | |
| 359 | if (ret == NULL) |
| 360 | fatal("%s: buffer error", __func__); |
| 361 | return ret; |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Stores a string using the bignum encoding rules (\0 pad if MSB set). |
| 366 | */ |
| 367 | void |
| 368 | buffer_put_bignum2_from_string(Buffer *buffer, const u_char *s, u_int l) |
| 369 | { |
| 370 | u_char *buf, *p; |
| 371 | int pad = 0; |
| 372 | |
| 373 | if (l > 8 * 1024) |
| 374 | fatal("%s: length %u too long", __func__, l); |
Damien Miller | adbfdbb | 2014-04-20 13:24:49 +1000 | [diff] [blame] | 375 | /* Skip leading zero bytes */ |
| 376 | for (; l > 0 && *s == 0; l--, s++) |
| 377 | ; |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 378 | p = buf = xmalloc(l + 1); |
| 379 | /* |
| 380 | * If most significant bit is set then prepend a zero byte to |
| 381 | * avoid interpretation as a negative number. |
| 382 | */ |
| 383 | if (l > 0 && (s[0] & 0x80) != 0) { |
| 384 | *p++ = '\0'; |
| 385 | pad = 1; |
| 386 | } |
| 387 | memcpy(p, s, l); |
| 388 | buffer_put_string(buffer, buf, l + pad); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 389 | explicit_bzero(buf, l + pad); |
Damien Miller | 91b580e | 2014-01-12 19:21:22 +1100 | [diff] [blame] | 390 | free(buf); |
| 391 | } |
| 392 | |
| 393 | |