summaryrefslogtreecommitdiff
path: root/bufaux.c
diff options
context:
space:
mode:
Diffstat (limited to 'bufaux.c')
-rw-r--r--bufaux.c53
1 files changed, 52 insertions, 1 deletions
diff --git a/bufaux.c b/bufaux.c
index 0c1381e36..4ab45a2f8 100644
--- a/bufaux.c
+++ b/bufaux.c
@@ -12,10 +12,12 @@
12 * Auxiliary functions for storing and retrieving various data types to/from 12 * Auxiliary functions for storing and retrieving various data types to/from
13 * Buffers. 13 * Buffers.
14 * 14 *
15 * SSH2 packet format added by Markus Friedl
16 *
15 */ 17 */
16 18
17#include "includes.h" 19#include "includes.h"
18RCSID("$Id: bufaux.c,v 1.8 2000/03/17 12:40:15 damien Exp $"); 20RCSID("$Id: bufaux.c,v 1.9 2000/04/01 01:09:23 damien Exp $");
19 21
20#include "ssh.h" 22#include "ssh.h"
21 23
@@ -83,6 +85,50 @@ buffer_get_bignum(Buffer *buffer, BIGNUM *value)
83} 85}
84 86
85/* 87/*
88 * Stores an BIGNUM in the buffer in SSH2 format.
89 */
90void
91buffer_put_bignum2(Buffer *buffer, BIGNUM *value)
92{
93 int bytes = BN_num_bytes(value) + 1;
94 unsigned char *buf = xmalloc(bytes);
95 int oi;
96 int hasnohigh = 0;
97 buf[0] = '\0';
98 /* Get the value of in binary */
99 oi = BN_bn2bin(value, buf+1);
100 if (oi != bytes-1)
101 fatal("buffer_put_bignum: BN_bn2bin() failed: oi %d != bin_size %d",
102 oi, bytes);
103 hasnohigh = (buf[1] & 0x80) ? 0 : 1;
104 if (value->neg) {
105 /**XXX should be two's-complement */
106 int i, carry;
107 unsigned char *uc = buf;
108 log("negativ!");
109 for(i = bytes-1, carry = 1; i>=0; i--) {
110 uc[i] ^= 0xff;
111 if(carry)
112 carry = !++uc[i];
113 }
114 }
115 buffer_put_string(buffer, buf+hasnohigh, bytes-hasnohigh);
116 memset(buf, 0, bytes);
117 xfree(buf);
118}
119
120int
121buffer_get_bignum2(Buffer *buffer, BIGNUM *value)
122{
123 /**XXX should be two's-complement */
124 int len;
125 unsigned char *bin = (unsigned char *)buffer_get_string(buffer, (unsigned int *)&len);
126 BN_bin2bn(bin, len, value);
127 xfree(bin);
128 return len;
129}
130
131/*
86 * Returns an integer from the buffer (4 bytes, msb first). 132 * Returns an integer from the buffer (4 bytes, msb first).
87 */ 133 */
88unsigned int 134unsigned int
@@ -142,6 +188,11 @@ buffer_put_string(Buffer *buffer, const void *buf, unsigned int len)
142 buffer_put_int(buffer, len); 188 buffer_put_int(buffer, len);
143 buffer_append(buffer, buf, len); 189 buffer_append(buffer, buf, len);
144} 190}
191void
192buffer_put_cstring(Buffer *buffer, const char *s)
193{
194 buffer_put_string(buffer, s, strlen(s));
195}
145 196
146/* 197/*
147 * Returns a character from the buffer (0 - 255). 198 * Returns a character from the buffer (0 - 255).