2 /* $OpenBSD: bufbn.c,v 1.6 2007/06/02 09:04:58 djm Exp $*/
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * Auxiliary functions for storing and retrieving various data types to/from
10 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
17 * SSH2 packet format added by Markus Friedl
18 * Copyright (c) 2000 Markus Friedl. All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 __RCSID("$NetBSD: bufbn.c,v 1.5 2009/02/16 20:53:54 christos Exp $");
43 #include <sys/types.h>
45 #include <openssl/bn.h>
57 * Stores an BIGNUM in the buffer with a 2-byte msb first bit count, followed
58 * by (bits+7)/8 bytes of binary data, msb first.
61 buffer_put_bignum_ret(Buffer
*buffer
, const BIGNUM
*value
)
63 int bits
= BN_num_bits(value
);
64 int bin_size
= (bits
+ 7) / 8;
65 u_char
*buf
= xmalloc(bin_size
);
69 /* Get the value of in binary */
70 oi
= BN_bn2bin(value
, buf
);
72 error("buffer_put_bignum_ret: BN_bn2bin() failed: oi %d != bin_size %d",
78 /* Store the number of bits in the buffer in two bytes, msb first. */
80 buffer_append(buffer
, msg
, 2);
81 /* Store the binary data. */
82 buffer_append(buffer
, buf
, oi
);
84 memset(buf
, 0, bin_size
);
91 buffer_put_bignum(Buffer
*buffer
, const BIGNUM
*value
)
93 if (buffer_put_bignum_ret(buffer
, value
) == -1)
94 fatal("buffer_put_bignum: buffer error");
98 * Retrieves a BIGNUM from the buffer.
101 buffer_get_bignum_ret(Buffer
*buffer
, BIGNUM
*value
)
106 /* Get the number of bits. */
107 if (buffer_get_ret(buffer
, (char *) buf
, 2) == -1) {
108 error("buffer_get_bignum_ret: invalid length");
112 /* Compute the number of binary bytes that follow. */
113 bytes
= (bits
+ 7) / 8;
114 if (bytes
> 8 * 1024) {
115 error("buffer_get_bignum_ret: cannot handle BN of size %d", bytes
);
118 if (buffer_len(buffer
) < bytes
) {
119 error("buffer_get_bignum_ret: input buffer too small");
122 bin
= buffer_ptr(buffer
);
123 if (BN_bin2bn(bin
, bytes
, value
) == NULL
) {
124 error("buffer_get_bignum_ret: BN_bin2bn failed");
127 if (buffer_consume_ret(buffer
, bytes
) == -1) {
128 error("buffer_get_bignum_ret: buffer_consume failed");
135 buffer_get_bignum(Buffer
*buffer
, BIGNUM
*value
)
137 if (buffer_get_bignum_ret(buffer
, value
) == -1)
138 fatal("buffer_get_bignum: buffer error");
142 * Stores a BIGNUM in the buffer in SSH2 format.
145 buffer_put_bignum2_ret(Buffer
*buffer
, const BIGNUM
*value
)
152 if (BN_is_zero(value
)) {
153 buffer_put_int(buffer
, 0);
157 error("buffer_put_bignum2_ret: negative numbers not supported");
160 bytes
= BN_num_bytes(value
) + 1; /* extra padding byte */
162 error("buffer_put_bignum2_ret: BN too small");
165 buf
= xmalloc(bytes
);
167 /* Get the value of in binary */
168 oi
= BN_bn2bin(value
, buf
+1);
169 if (oi
< 0 || (u_int
)oi
!= bytes
- 1) {
170 error("buffer_put_bignum2_ret: BN_bn2bin() failed: "
171 "oi %d != bin_size %d", oi
, bytes
);
175 hasnohigh
= (buf
[1] & 0x80) ? 0 : 1;
176 buffer_put_string(buffer
, buf
+hasnohigh
, bytes
-hasnohigh
);
177 memset(buf
, 0, bytes
);
183 buffer_put_bignum2(Buffer
*buffer
, const BIGNUM
*value
)
185 if (buffer_put_bignum2_ret(buffer
, value
) == -1)
186 fatal("buffer_put_bignum2: buffer error");
190 buffer_get_bignum2_ret(Buffer
*buffer
, BIGNUM
*value
)
195 if ((bin
= buffer_get_string_ret(buffer
, &len
)) == NULL
) {
196 error("buffer_get_bignum2_ret: invalid bignum");
200 if (len
> 0 && (bin
[0] & 0x80)) {
201 error("buffer_get_bignum2_ret: negative numbers not supported");
205 if (len
> 8 * 1024) {
206 error("buffer_get_bignum2_ret: cannot handle BN of size %d",
211 if (BN_bin2bn(bin
, len
, value
) == NULL
) {
212 error("buffer_get_bignum2_ret: BN_bin2bn failed");
221 buffer_get_bignum2(Buffer
*buffer
, BIGNUM
*value
)
223 if (buffer_get_bignum2_ret(buffer
, value
) == -1)
224 fatal("buffer_get_bignum2: buffer error");