Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * |
| 3 | * bufaux.c |
| 4 | * |
| 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 6 | * |
| 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
| 9 | * |
| 10 | * Created: Wed Mar 29 02:24:47 1995 ylo |
| 11 | * |
| 12 | * Auxiliary functions for storing and retrieving various data types to/from |
| 13 | * Buffers. |
| 14 | * |
| 15 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
| 17 | #include "includes.h" |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 18 | RCSID("$Id: bufaux.c,v 1.7 1999/11/25 00:54:58 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 19 | |
| 20 | #include "ssh.h" |
Damien Miller | 7f6ea02 | 1999-10-28 13:25:17 +1000 | [diff] [blame] | 21 | |
| 22 | #ifdef HAVE_OPENSSL |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 23 | #include <openssl/bn.h> |
Damien Miller | 7f6ea02 | 1999-10-28 13:25:17 +1000 | [diff] [blame] | 24 | #endif |
| 25 | #ifdef HAVE_SSL |
| 26 | #include <ssl/bn.h> |
| 27 | #endif |
| 28 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 29 | #include "bufaux.h" |
| 30 | #include "xmalloc.h" |
| 31 | #include "getput.h" |
| 32 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 33 | /* |
| 34 | * Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed |
| 35 | * by (bits+7)/8 bytes of binary data, msb first. |
| 36 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 37 | void |
| 38 | buffer_put_bignum(Buffer *buffer, BIGNUM *value) |
| 39 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | int bits = BN_num_bits(value); |
| 41 | int bin_size = (bits + 7) / 8; |
| 42 | char *buf = xmalloc(bin_size); |
| 43 | int oi; |
| 44 | char msg[2]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 46 | /* Get the value of in binary */ |
| 47 | oi = BN_bn2bin(value, buf); |
| 48 | if (oi != bin_size) |
| 49 | fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d", |
| 50 | oi, bin_size); |
| 51 | |
| 52 | /* Store the number of bits in the buffer in two bytes, msb first. */ |
| 53 | PUT_16BIT(msg, bits); |
| 54 | buffer_append(buffer, msg, 2); |
| 55 | /* Store the binary data. */ |
| 56 | buffer_append(buffer, buf, oi); |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 57 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 58 | memset(buf, 0, bin_size); |
| 59 | xfree(buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 60 | } |
| 61 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 62 | /* |
| 63 | * Retrieves an BIGNUM from the buffer. |
| 64 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 65 | int |
| 66 | buffer_get_bignum(Buffer *buffer, BIGNUM *value) |
| 67 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 68 | int bits, bytes; |
| 69 | unsigned char buf[2], *bin; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 70 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 71 | /* Get the number for bits. */ |
| 72 | buffer_get(buffer, (char *) buf, 2); |
| 73 | bits = GET_16BIT(buf); |
| 74 | /* Compute the number of binary bytes that follow. */ |
| 75 | bytes = (bits + 7) / 8; |
| 76 | if (buffer_len(buffer) < bytes) |
| 77 | fatal("buffer_get_bignum: input buffer too small"); |
| 78 | bin = buffer_ptr(buffer); |
| 79 | BN_bin2bn(bin, bytes, value); |
| 80 | buffer_consume(buffer, bytes); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 81 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 82 | return 2 + bytes; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 83 | } |
| 84 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 85 | /* |
| 86 | * Returns an integer from the buffer (4 bytes, msb first). |
| 87 | */ |
| 88 | unsigned int |
| 89 | buffer_get_int(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 90 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 91 | unsigned char buf[4]; |
| 92 | buffer_get(buffer, (char *) buf, 4); |
| 93 | return GET_32BIT(buf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 94 | } |
| 95 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 96 | /* |
| 97 | * Stores an integer in the buffer in 4 bytes, msb first. |
| 98 | */ |
| 99 | void |
| 100 | buffer_put_int(Buffer *buffer, unsigned int value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 101 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 102 | char buf[4]; |
| 103 | PUT_32BIT(buf, value); |
| 104 | buffer_append(buffer, buf, 4); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | } |
| 106 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 107 | /* |
| 108 | * Returns an arbitrary binary string from the buffer. The string cannot |
| 109 | * be longer than 256k. The returned value points to memory allocated |
| 110 | * with xmalloc; it is the responsibility of the calling function to free |
| 111 | * the data. If length_ptr is non-NULL, the length of the returned data |
| 112 | * will be stored there. A null character will be automatically appended |
| 113 | * to the returned string, and is not counted in length. |
| 114 | */ |
| 115 | char * |
| 116 | buffer_get_string(Buffer *buffer, unsigned int *length_ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 117 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 118 | unsigned int len; |
| 119 | char *value; |
| 120 | /* Get the length. */ |
| 121 | len = buffer_get_int(buffer); |
| 122 | if (len > 256 * 1024) |
| 123 | fatal("Received packet with bad string length %d", len); |
| 124 | /* Allocate space for the string. Add one byte for a null character. */ |
| 125 | value = xmalloc(len + 1); |
| 126 | /* Get the string. */ |
| 127 | buffer_get(buffer, value, len); |
| 128 | /* Append a null character to make processing easier. */ |
| 129 | value[len] = 0; |
| 130 | /* Optionally return the length of the string. */ |
| 131 | if (length_ptr) |
| 132 | *length_ptr = len; |
| 133 | return value; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 134 | } |
| 135 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 136 | /* |
| 137 | * Stores and arbitrary binary string in the buffer. |
| 138 | */ |
| 139 | void |
| 140 | buffer_put_string(Buffer *buffer, const void *buf, unsigned int len) |
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 | buffer_put_int(buffer, len); |
| 143 | buffer_append(buffer, buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 144 | } |
| 145 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 146 | /* |
| 147 | * Returns a character from the buffer (0 - 255). |
| 148 | */ |
| 149 | int |
| 150 | buffer_get_char(Buffer *buffer) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 152 | char ch; |
| 153 | buffer_get(buffer, &ch, 1); |
| 154 | return (unsigned char) ch; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 155 | } |
| 156 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 157 | /* |
| 158 | * Stores a character in the buffer. |
| 159 | */ |
| 160 | void |
| 161 | buffer_put_char(Buffer *buffer, int value) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 162 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | char ch = value; |
| 164 | buffer_append(buffer, &ch, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 165 | } |