No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / external / bsd / openssh / dist / bufbn.c
blob09791086187debb260cf4bf6b924bc1dfd89d81e
1 /* $NetBSD$ */
2 /* $OpenBSD: bufbn.c,v 1.6 2007/06/02 09:04:58 djm Exp $*/
3 /*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved
7 * Auxiliary functions for storing and retrieving various data types to/from
8 * Buffers.
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
22 * are met:
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.
41 #include "includes.h"
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>
47 #include <string.h>
48 #include <stdarg.h>
49 #include <time.h>
51 #include "xmalloc.h"
52 #include "buffer.h"
53 #include "log.h"
54 #include "misc.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.
60 int
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);
66 int oi;
67 char msg[2];
69 /* Get the value of in binary */
70 oi = BN_bn2bin(value, buf);
71 if (oi != bin_size) {
72 error("buffer_put_bignum_ret: BN_bn2bin() failed: oi %d != bin_size %d",
73 oi, bin_size);
74 xfree(buf);
75 return (-1);
78 /* Store the number of bits in the buffer in two bytes, msb first. */
79 put_u16(msg, bits);
80 buffer_append(buffer, msg, 2);
81 /* Store the binary data. */
82 buffer_append(buffer, buf, oi);
84 memset(buf, 0, bin_size);
85 xfree(buf);
87 return (0);
90 void
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)
103 u_int bits, bytes;
104 u_char buf[2], *bin;
106 /* Get the number of bits. */
107 if (buffer_get_ret(buffer, (char *) buf, 2) == -1) {
108 error("buffer_get_bignum_ret: invalid length");
109 return (-1);
111 bits = get_u16(buf);
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);
116 return (-1);
118 if (buffer_len(buffer) < bytes) {
119 error("buffer_get_bignum_ret: input buffer too small");
120 return (-1);
122 bin = buffer_ptr(buffer);
123 if (BN_bin2bn(bin, bytes, value) == NULL) {
124 error("buffer_get_bignum_ret: BN_bin2bn failed");
125 return (-1);
127 if (buffer_consume_ret(buffer, bytes) == -1) {
128 error("buffer_get_bignum_ret: buffer_consume failed");
129 return (-1);
131 return (0);
134 void
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)
147 u_int bytes;
148 u_char *buf;
149 int oi;
150 u_int hasnohigh = 0;
152 if (BN_is_zero(value)) {
153 buffer_put_int(buffer, 0);
154 return 0;
156 if (value->neg) {
157 error("buffer_put_bignum2_ret: negative numbers not supported");
158 return (-1);
160 bytes = BN_num_bytes(value) + 1; /* extra padding byte */
161 if (bytes < 2) {
162 error("buffer_put_bignum2_ret: BN too small");
163 return (-1);
165 buf = xmalloc(bytes);
166 buf[0] = 0x00;
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);
172 xfree(buf);
173 return (-1);
175 hasnohigh = (buf[1] & 0x80) ? 0 : 1;
176 buffer_put_string(buffer, buf+hasnohigh, bytes-hasnohigh);
177 memset(buf, 0, bytes);
178 xfree(buf);
179 return (0);
182 void
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)
192 u_int len;
193 u_char *bin;
195 if ((bin = buffer_get_string_ret(buffer, &len)) == NULL) {
196 error("buffer_get_bignum2_ret: invalid bignum");
197 return (-1);
200 if (len > 0 && (bin[0] & 0x80)) {
201 error("buffer_get_bignum2_ret: negative numbers not supported");
202 xfree(bin);
203 return (-1);
205 if (len > 8 * 1024) {
206 error("buffer_get_bignum2_ret: cannot handle BN of size %d",
207 len);
208 xfree(bin);
209 return (-1);
211 if (BN_bin2bn(bin, len, value) == NULL) {
212 error("buffer_get_bignum2_ret: BN_bin2bn failed");
213 xfree(bin);
214 return (-1);
216 xfree(bin);
217 return (0);
220 void
221 buffer_get_bignum2(Buffer *buffer, BIGNUM *value)
223 if (buffer_get_bignum2_ret(buffer, value) == -1)
224 fatal("buffer_get_bignum2: buffer error");