bufaux.c (157019) | bufaux.c (162856) |
---|---|
1/* $OpenBSD: bufaux.c,v 1.44 2006/08/03 03:34:41 deraadt Exp $ */ |
|
1/* 2 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * All rights reserved 5 * Auxiliary functions for storing and retrieving various data types to/from 6 * Buffers. 7 * 8 * As far as I am concerned, the code I have written for this software --- 23 unchanged lines hidden (view full) --- 32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39#include "includes.h" | 2/* 3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5 * All rights reserved 6 * Auxiliary functions for storing and retrieving various data types to/from 7 * Buffers. 8 * 9 * As far as I am concerned, the code I have written for this software --- 23 unchanged lines hidden (view full) --- 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. 38 */ 39 40#include "includes.h" |
40RCSID("$OpenBSD: bufaux.c,v 1.37 2005/11/05 05:01:15 djm Exp $"); 41RCSID("$FreeBSD: head/crypto/openssh/bufaux.c 157019 2006-03-22 20:41:37Z des $"); | 41__RCSID("$FreeBSD: head/crypto/openssh/bufaux.c 162856 2006-09-30 13:38:06Z des $"); |
42 | 42 |
43#include <sys/types.h> 44 |
|
43#include <openssl/bn.h> | 45#include <openssl/bn.h> |
44#include "bufaux.h" | 46 47#include <string.h> 48#include <stdarg.h> 49 |
45#include "xmalloc.h" | 50#include "xmalloc.h" |
46#include "getput.h" | 51#include "buffer.h" |
47#include "log.h" | 52#include "log.h" |
53#include "misc.h" |
|
48 49/* | 54 55/* |
50 * Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed 51 * by (bits+7)/8 bytes of binary data, msb first. 52 */ 53int 54buffer_put_bignum_ret(Buffer *buffer, const BIGNUM *value) 55{ 56 int bits = BN_num_bits(value); 57 int bin_size = (bits + 7) / 8; 58 u_char *buf = xmalloc(bin_size); 59 int oi; 60 char msg[2]; 61 62 /* Get the value of in binary */ 63 oi = BN_bn2bin(value, buf); 64 if (oi != bin_size) { 65 error("buffer_put_bignum_ret: BN_bn2bin() failed: oi %d != bin_size %d", 66 oi, bin_size); 67 xfree(buf); 68 return (-1); 69 } 70 71 /* Store the number of bits in the buffer in two bytes, msb first. */ 72 PUT_16BIT(msg, bits); 73 buffer_append(buffer, msg, 2); 74 /* Store the binary data. */ 75 buffer_append(buffer, (char *)buf, oi); 76 77 memset(buf, 0, bin_size); 78 xfree(buf); 79 80 return (0); 81} 82 83void 84buffer_put_bignum(Buffer *buffer, const BIGNUM *value) 85{ 86 if (buffer_put_bignum_ret(buffer, value) == -1) 87 fatal("buffer_put_bignum: buffer error"); 88} 89 90/* 91 * Retrieves an BIGNUM from the buffer. 92 */ 93int 94buffer_get_bignum_ret(Buffer *buffer, BIGNUM *value) 95{ 96 u_int bits, bytes; 97 u_char buf[2], *bin; 98 99 /* Get the number for bits. */ 100 if (buffer_get_ret(buffer, (char *) buf, 2) == -1) { 101 error("buffer_get_bignum_ret: invalid length"); 102 return (-1); 103 } 104 bits = GET_16BIT(buf); 105 /* Compute the number of binary bytes that follow. */ 106 bytes = (bits + 7) / 8; 107 if (bytes > 8 * 1024) { 108 error("buffer_get_bignum_ret: cannot handle BN of size %d", bytes); 109 return (-1); 110 } 111 if (buffer_len(buffer) < bytes) { 112 error("buffer_get_bignum_ret: input buffer too small"); 113 return (-1); 114 } 115 bin = buffer_ptr(buffer); 116 BN_bin2bn(bin, bytes, value); 117 if (buffer_consume_ret(buffer, bytes) == -1) { 118 error("buffer_get_bignum_ret: buffer_consume failed"); 119 return (-1); 120 } 121 return (0); 122} 123 124void 125buffer_get_bignum(Buffer *buffer, BIGNUM *value) 126{ 127 if (buffer_get_bignum_ret(buffer, value) == -1) 128 fatal("buffer_get_bignum: buffer error"); 129} 130 131/* 132 * Stores an BIGNUM in the buffer in SSH2 format. 133 */ 134int 135buffer_put_bignum2_ret(Buffer *buffer, const BIGNUM *value) 136{ 137 u_int bytes; 138 u_char *buf; 139 int oi; 140 u_int hasnohigh = 0; 141 142 if (BN_is_zero(value)) { 143 buffer_put_int(buffer, 0); 144 return 0; 145 } 146 if (value->neg) { 147 error("buffer_put_bignum2_ret: negative numbers not supported"); 148 return (-1); 149 } 150 bytes = BN_num_bytes(value) + 1; /* extra padding byte */ 151 if (bytes < 2) { 152 error("buffer_put_bignum2_ret: BN too small"); 153 return (-1); 154 } 155 buf = xmalloc(bytes); 156 buf[0] = 0x00; 157 /* Get the value of in binary */ 158 oi = BN_bn2bin(value, buf+1); 159 if (oi < 0 || (u_int)oi != bytes - 1) { 160 error("buffer_put_bignum2_ret: BN_bn2bin() failed: " 161 "oi %d != bin_size %d", oi, bytes); 162 xfree(buf); 163 return (-1); 164 } 165 hasnohigh = (buf[1] & 0x80) ? 0 : 1; 166 buffer_put_string(buffer, buf+hasnohigh, bytes-hasnohigh); 167 memset(buf, 0, bytes); 168 xfree(buf); 169 return (0); 170} 171 172void 173buffer_put_bignum2(Buffer *buffer, const BIGNUM *value) 174{ 175 if (buffer_put_bignum2_ret(buffer, value) == -1) 176 fatal("buffer_put_bignum2: buffer error"); 177} 178 179int 180buffer_get_bignum2_ret(Buffer *buffer, BIGNUM *value) 181{ 182 u_int len; 183 u_char *bin; 184 185 if ((bin = buffer_get_string_ret(buffer, &len)) == NULL) { 186 error("buffer_get_bignum2_ret: invalid bignum"); 187 return (-1); 188 } 189 190 if (len > 0 && (bin[0] & 0x80)) { 191 error("buffer_get_bignum2_ret: negative numbers not supported"); 192 xfree(bin); 193 return (-1); 194 } 195 if (len > 8 * 1024) { 196 error("buffer_get_bignum2_ret: cannot handle BN of size %d", len); 197 xfree(bin); 198 return (-1); 199 } 200 BN_bin2bn(bin, len, value); 201 xfree(bin); 202 return (0); 203} 204 205void 206buffer_get_bignum2(Buffer *buffer, BIGNUM *value) 207{ 208 if (buffer_get_bignum2_ret(buffer, value) == -1) 209 fatal("buffer_get_bignum2: buffer error"); 210} 211 212/* | |
213 * Returns integers from the buffer (msb first). 214 */ 215 216int 217buffer_get_short_ret(u_short *ret, Buffer *buffer) 218{ 219 u_char buf[2]; 220 221 if (buffer_get_ret(buffer, (char *) buf, 2) == -1) 222 return (-1); | 56 * Returns integers from the buffer (msb first). 57 */ 58 59int 60buffer_get_short_ret(u_short *ret, Buffer *buffer) 61{ 62 u_char buf[2]; 63 64 if (buffer_get_ret(buffer, (char *) buf, 2) == -1) 65 return (-1); |
223 *ret = GET_16BIT(buf); | 66 *ret = get_u16(buf); |
224 return (0); 225} 226 227u_short 228buffer_get_short(Buffer *buffer) 229{ 230 u_short ret; 231 --- 5 unchanged lines hidden (view full) --- 237 238int 239buffer_get_int_ret(u_int *ret, Buffer *buffer) 240{ 241 u_char buf[4]; 242 243 if (buffer_get_ret(buffer, (char *) buf, 4) == -1) 244 return (-1); | 67 return (0); 68} 69 70u_short 71buffer_get_short(Buffer *buffer) 72{ 73 u_short ret; 74 --- 5 unchanged lines hidden (view full) --- 80 81int 82buffer_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); |
245 *ret = GET_32BIT(buf); | 88 *ret = get_u32(buf); |
246 return (0); 247} 248 249u_int 250buffer_get_int(Buffer *buffer) 251{ 252 u_int ret; 253 --- 5 unchanged lines hidden (view full) --- 259 260int 261buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer) 262{ 263 u_char buf[8]; 264 265 if (buffer_get_ret(buffer, (char *) buf, 8) == -1) 266 return (-1); | 89 return (0); 90} 91 92u_int 93buffer_get_int(Buffer *buffer) 94{ 95 u_int ret; 96 --- 5 unchanged lines hidden (view full) --- 102 103int 104buffer_get_int64_ret(u_int64_t *ret, Buffer *buffer) 105{ 106 u_char buf[8]; 107 108 if (buffer_get_ret(buffer, (char *) buf, 8) == -1) 109 return (-1); |
267 *ret = GET_64BIT(buf); | 110 *ret = get_u64(buf); |
268 return (0); 269} 270 271u_int64_t 272buffer_get_int64(Buffer *buffer) 273{ 274 u_int64_t ret; 275 --- 6 unchanged lines hidden (view full) --- 282/* 283 * Stores integers in the buffer, msb first. 284 */ 285void 286buffer_put_short(Buffer *buffer, u_short value) 287{ 288 char buf[2]; 289 | 111 return (0); 112} 113 114u_int64_t 115buffer_get_int64(Buffer *buffer) 116{ 117 u_int64_t ret; 118 --- 6 unchanged lines hidden (view full) --- 125/* 126 * Stores integers in the buffer, msb first. 127 */ 128void 129buffer_put_short(Buffer *buffer, u_short value) 130{ 131 char buf[2]; 132 |
290 PUT_16BIT(buf, value); | 133 put_u16(buf, value); |
291 buffer_append(buffer, buf, 2); 292} 293 294void 295buffer_put_int(Buffer *buffer, u_int value) 296{ 297 char buf[4]; 298 | 134 buffer_append(buffer, buf, 2); 135} 136 137void 138buffer_put_int(Buffer *buffer, u_int value) 139{ 140 char buf[4]; 141 |
299 PUT_32BIT(buf, value); | 142 put_u32(buf, value); |
300 buffer_append(buffer, buf, 4); 301} 302 303void 304buffer_put_int64(Buffer *buffer, u_int64_t value) 305{ 306 char buf[8]; 307 | 143 buffer_append(buffer, buf, 4); 144} 145 146void 147buffer_put_int64(Buffer *buffer, u_int64_t value) 148{ 149 char buf[8]; 150 |
308 PUT_64BIT(buf, value); | 151 put_u64(buf, value); |
309 buffer_append(buffer, buf, 8); 310} 311 312/* 313 * Returns an arbitrary binary string from the buffer. The string cannot 314 * be longer than 256k. The returned value points to memory allocated 315 * with xmalloc; it is the responsibility of the calling function to free 316 * the data. If length_ptr is non-NULL, the length of the returned data --- 91 unchanged lines hidden --- | 152 buffer_append(buffer, buf, 8); 153} 154 155/* 156 * Returns an arbitrary binary string from the buffer. The string cannot 157 * be longer than 256k. The returned value points to memory allocated 158 * with xmalloc; it is the responsibility of the calling function to free 159 * the data. If length_ptr is non-NULL, the length of the returned data --- 91 unchanged lines hidden --- |