sshbuf.h revision 1.7 1 1.7 christos /* $OpenBSD: sshbuf.h,v 1.8 2016/11/25 23:22:04 djm Exp $ */
2 1.1 christos /*
3 1.1 christos * Copyright (c) 2011 Damien Miller
4 1.1 christos *
5 1.1 christos * Permission to use, copy, modify, and distribute this software for any
6 1.1 christos * purpose with or without fee is hereby granted, provided that the above
7 1.1 christos * copyright notice and this permission notice appear in all copies.
8 1.1 christos *
9 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 1.1 christos */
17 1.1 christos
18 1.1 christos #ifndef _SSHBUF_H
19 1.1 christos #define _SSHBUF_H
20 1.1 christos
21 1.1 christos #include <sys/types.h>
22 1.1 christos #include <stdarg.h>
23 1.1 christos #include <stdio.h>
24 1.1 christos #include <openssl/bn.h>
25 1.1 christos #include <openssl/ec.h>
26 1.1 christos
27 1.1 christos #define SSHBUF_SIZE_MAX 0x8000000 /* Hard maximum size */
28 1.1 christos #define SSHBUF_REFS_MAX 0x100000 /* Max child buffers */
29 1.1 christos #define SSHBUF_MAX_BIGNUM (16384 / 8) /* Max bignum *bytes* */
30 1.1 christos #define SSHBUF_MAX_ECPOINT ((528 * 2 / 8) + 1) /* Max EC point *bytes* */
31 1.1 christos
32 1.1 christos /*
33 1.1 christos * NB. do not depend on the internals of this. It will be made opaque
34 1.1 christos * one day.
35 1.1 christos */
36 1.1 christos struct sshbuf {
37 1.1 christos u_char *d; /* Data */
38 1.1 christos const u_char *cd; /* Const data */
39 1.1 christos size_t off; /* First available byte is buf->d + buf->off */
40 1.1 christos size_t size; /* Last byte is buf->d + buf->size - 1 */
41 1.1 christos size_t max_size; /* Maximum size of buffer */
42 1.1 christos size_t alloc; /* Total bytes allocated to buf->d */
43 1.1 christos int readonly; /* Refers to external, const data */
44 1.1 christos int dont_free; /* Kludge to support sshbuf_init */
45 1.1 christos u_int refcount; /* Tracks self and number of child buffers */
46 1.1 christos struct sshbuf *parent; /* If child, pointer to parent */
47 1.1 christos };
48 1.1 christos
49 1.1 christos #ifndef SSHBUF_NO_DEPREACTED
50 1.1 christos /*
51 1.1 christos * NB. Please do not use sshbuf_init() in new code. Please use sshbuf_new()
52 1.1 christos * instead. sshbuf_init() is deprectated and will go away soon (it is
53 1.1 christos * only included to allow compat with buffer_* in OpenSSH)
54 1.1 christos */
55 1.1 christos void sshbuf_init(struct sshbuf *buf);
56 1.1 christos #endif
57 1.1 christos
58 1.1 christos /*
59 1.1 christos * Create a new sshbuf buffer.
60 1.1 christos * Returns pointer to buffer on success, or NULL on allocation failure.
61 1.1 christos */
62 1.1 christos struct sshbuf *sshbuf_new(void);
63 1.1 christos
64 1.1 christos /*
65 1.1 christos * Create a new, read-only sshbuf buffer from existing data.
66 1.1 christos * Returns pointer to buffer on success, or NULL on allocation failure.
67 1.1 christos */
68 1.1 christos struct sshbuf *sshbuf_from(const void *blob, size_t len);
69 1.1 christos
70 1.1 christos /*
71 1.1 christos * Create a new, read-only sshbuf buffer from the contents of an existing
72 1.1 christos * buffer. The contents of "buf" must not change in the lifetime of the
73 1.1 christos * resultant buffer.
74 1.1 christos * Returns pointer to buffer on success, or NULL on allocation failure.
75 1.1 christos */
76 1.1 christos struct sshbuf *sshbuf_fromb(struct sshbuf *buf);
77 1.1 christos
78 1.1 christos /*
79 1.1 christos * Create a new, read-only sshbuf buffer from the contents of a string in
80 1.1 christos * an existing buffer (the string is consumed in the process).
81 1.1 christos * The contents of "buf" must not change in the lifetime of the resultant
82 1.1 christos * buffer.
83 1.1 christos * Returns pointer to buffer on success, or NULL on allocation failure.
84 1.1 christos */
85 1.1 christos int sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp);
86 1.1 christos
87 1.1 christos /*
88 1.1 christos * Clear and free buf
89 1.1 christos */
90 1.1 christos void sshbuf_free(struct sshbuf *buf);
91 1.1 christos
92 1.1 christos /*
93 1.1 christos * Reset buf, clearing its contents. NB. max_size is preserved.
94 1.1 christos */
95 1.1 christos void sshbuf_reset(struct sshbuf *buf);
96 1.1 christos
97 1.1 christos /*
98 1.1 christos * Return the maximum size of buf
99 1.1 christos */
100 1.1 christos size_t sshbuf_max_size(const struct sshbuf *buf);
101 1.1 christos
102 1.1 christos /*
103 1.1 christos * Set the maximum size of buf
104 1.1 christos * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
105 1.1 christos */
106 1.1 christos int sshbuf_set_max_size(struct sshbuf *buf, size_t max_size);
107 1.1 christos
108 1.1 christos /*
109 1.1 christos * Returns the length of data in buf
110 1.1 christos */
111 1.1 christos size_t sshbuf_len(const struct sshbuf *buf);
112 1.1 christos
113 1.1 christos /*
114 1.1 christos * Returns number of bytes left in buffer before hitting max_size.
115 1.1 christos */
116 1.1 christos size_t sshbuf_avail(const struct sshbuf *buf);
117 1.1 christos
118 1.1 christos /*
119 1.5 christos * Returns a read-only pointer to the start of the data in buf
120 1.1 christos */
121 1.1 christos const u_char *sshbuf_ptr(const struct sshbuf *buf);
122 1.1 christos
123 1.1 christos /*
124 1.5 christos * Returns a mutable pointer to the start of the data in buf, or
125 1.1 christos * NULL if the buffer is read-only.
126 1.1 christos */
127 1.1 christos u_char *sshbuf_mutable_ptr(const struct sshbuf *buf);
128 1.1 christos
129 1.1 christos /*
130 1.1 christos * Check whether a reservation of size len will succeed in buf
131 1.1 christos * Safer to use than direct comparisons again sshbuf_avail as it copes
132 1.1 christos * with unsigned overflows correctly.
133 1.1 christos * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
134 1.1 christos */
135 1.1 christos int sshbuf_check_reserve(const struct sshbuf *buf, size_t len);
136 1.1 christos
137 1.1 christos /*
138 1.7 christos * Preallocates len additional bytes in buf.
139 1.7 christos * Useful for cases where the caller knows how many bytes will ultimately be
140 1.7 christos * required to avoid realloc in the buffer code.
141 1.7 christos * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
142 1.7 christos */
143 1.7 christos int sshbuf_allocate(struct sshbuf *buf, size_t len);
144 1.7 christos
145 1.7 christos /*
146 1.1 christos * Reserve len bytes in buf.
147 1.1 christos * Returns 0 on success and a pointer to the first reserved byte via the
148 1.1 christos * optional dpp parameter or a negative * SSH_ERR_* error code on failure.
149 1.1 christos */
150 1.1 christos int sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp);
151 1.1 christos
152 1.1 christos /*
153 1.1 christos * Consume len bytes from the start of buf
154 1.1 christos * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
155 1.1 christos */
156 1.1 christos int sshbuf_consume(struct sshbuf *buf, size_t len);
157 1.1 christos
158 1.1 christos /*
159 1.1 christos * Consume len bytes from the end of buf
160 1.1 christos * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
161 1.1 christos */
162 1.1 christos int sshbuf_consume_end(struct sshbuf *buf, size_t len);
163 1.1 christos
164 1.1 christos /* Extract or deposit some bytes */
165 1.1 christos int sshbuf_get(struct sshbuf *buf, void *v, size_t len);
166 1.1 christos int sshbuf_put(struct sshbuf *buf, const void *v, size_t len);
167 1.1 christos int sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v);
168 1.1 christos
169 1.1 christos /* Append using a printf(3) format */
170 1.1 christos int sshbuf_putf(struct sshbuf *buf, const char *fmt, ...)
171 1.1 christos __attribute__((format(printf, 2, 3)));
172 1.3 joerg int sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap)
173 1.3 joerg __printflike(2, 0);
174 1.1 christos
175 1.1 christos /* Functions to extract or store big-endian words of various sizes */
176 1.1 christos int sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp);
177 1.1 christos int sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp);
178 1.1 christos int sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp);
179 1.1 christos int sshbuf_get_u8(struct sshbuf *buf, u_char *valp);
180 1.1 christos int sshbuf_put_u64(struct sshbuf *buf, u_int64_t val);
181 1.1 christos int sshbuf_put_u32(struct sshbuf *buf, u_int32_t val);
182 1.1 christos int sshbuf_put_u16(struct sshbuf *buf, u_int16_t val);
183 1.1 christos int sshbuf_put_u8(struct sshbuf *buf, u_char val);
184 1.1 christos
185 1.1 christos /*
186 1.1 christos * Functions to extract or store SSH wire encoded strings (u32 len || data)
187 1.1 christos * The "cstring" variants admit no \0 characters in the string contents.
188 1.1 christos * Caller must free *valp.
189 1.1 christos */
190 1.1 christos int sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp);
191 1.1 christos int sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp);
192 1.1 christos int sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v);
193 1.1 christos int sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len);
194 1.1 christos int sshbuf_put_cstring(struct sshbuf *buf, const char *v);
195 1.1 christos int sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v);
196 1.1 christos
197 1.1 christos /*
198 1.1 christos * "Direct" variant of sshbuf_get_string, returns pointer into the sshbuf to
199 1.1 christos * avoid an malloc+memcpy. The pointer is guaranteed to be valid until the
200 1.1 christos * next sshbuf-modifying function call. Caller does not free.
201 1.1 christos */
202 1.1 christos int sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp,
203 1.1 christos size_t *lenp);
204 1.1 christos
205 1.1 christos /* Skip past a string */
206 1.1 christos #define sshbuf_skip_string(buf) sshbuf_get_string_direct(buf, NULL, NULL)
207 1.1 christos
208 1.1 christos /* Another variant: "peeks" into the buffer without modifying it */
209 1.1 christos int sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp,
210 1.1 christos size_t *lenp);
211 1.1 christos
212 1.1 christos /*
213 1.1 christos * Functions to extract or store SSH wire encoded bignums and elliptic
214 1.1 christos * curve points.
215 1.1 christos */
216 1.1 christos int sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM *v);
217 1.1 christos int sshbuf_get_bignum1(struct sshbuf *buf, BIGNUM *v);
218 1.4 christos int sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
219 1.4 christos const u_char **valp, size_t *lenp);
220 1.1 christos int sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v);
221 1.1 christos int sshbuf_put_bignum1(struct sshbuf *buf, const BIGNUM *v);
222 1.1 christos int sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len);
223 1.1 christos int sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g);
224 1.1 christos int sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v);
225 1.1 christos int sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g);
226 1.1 christos int sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v);
227 1.1 christos
228 1.1 christos /* Dump the contents of the buffer in a human-readable format */
229 1.1 christos void sshbuf_dump(struct sshbuf *buf, FILE *f);
230 1.1 christos
231 1.1 christos /* Dump specified memory in a human-readable format */
232 1.1 christos void sshbuf_dump_data(const void *s, size_t len, FILE *f);
233 1.1 christos
234 1.1 christos /* Return the hexadecimal representation of the contents of the buffer */
235 1.1 christos char *sshbuf_dtob16(struct sshbuf *buf);
236 1.1 christos
237 1.1 christos /* Encode the contents of the buffer as base64 */
238 1.1 christos char *sshbuf_dtob64(struct sshbuf *buf);
239 1.1 christos
240 1.1 christos /* Decode base64 data and append it to the buffer */
241 1.1 christos int sshbuf_b64tod(struct sshbuf *buf, const char *b64);
242 1.1 christos
243 1.6 christos /*
244 1.6 christos * Duplicate the contents of a buffer to a string (caller to free).
245 1.6 christos * Returns NULL on buffer error, or if the buffer contains a premature
246 1.6 christos * nul character.
247 1.6 christos */
248 1.6 christos char *sshbuf_dup_string(struct sshbuf *buf);
249 1.6 christos
250 1.1 christos /* Macros for decoding/encoding integers */
251 1.1 christos #define PEEK_U64(p) \
252 1.2 christos (((u_int64_t)(((const u_char *)(p))[0]) << 56) | \
253 1.2 christos ((u_int64_t)(((const u_char *)(p))[1]) << 48) | \
254 1.2 christos ((u_int64_t)(((const u_char *)(p))[2]) << 40) | \
255 1.2 christos ((u_int64_t)(((const u_char *)(p))[3]) << 32) | \
256 1.2 christos ((u_int64_t)(((const u_char *)(p))[4]) << 24) | \
257 1.2 christos ((u_int64_t)(((const u_char *)(p))[5]) << 16) | \
258 1.2 christos ((u_int64_t)(((const u_char *)(p))[6]) << 8) | \
259 1.2 christos (u_int64_t)(((const u_char *)(p))[7]))
260 1.1 christos #define PEEK_U32(p) \
261 1.2 christos (((u_int32_t)(((const u_char *)(p))[0]) << 24) | \
262 1.2 christos ((u_int32_t)(((const u_char *)(p))[1]) << 16) | \
263 1.2 christos ((u_int32_t)(((const u_char *)(p))[2]) << 8) | \
264 1.2 christos (u_int32_t)(((const u_char *)(p))[3]))
265 1.1 christos #define PEEK_U16(p) \
266 1.2 christos (((u_int16_t)(((const u_char *)(p))[0]) << 8) | \
267 1.2 christos (u_int16_t)(((const u_char *)(p))[1]))
268 1.1 christos
269 1.1 christos #define POKE_U64(p, v) \
270 1.1 christos do { \
271 1.5 christos const u_int64_t __v = (v); \
272 1.5 christos ((u_char *)(p))[0] = (__v >> 56) & 0xff; \
273 1.5 christos ((u_char *)(p))[1] = (__v >> 48) & 0xff; \
274 1.5 christos ((u_char *)(p))[2] = (__v >> 40) & 0xff; \
275 1.5 christos ((u_char *)(p))[3] = (__v >> 32) & 0xff; \
276 1.5 christos ((u_char *)(p))[4] = (__v >> 24) & 0xff; \
277 1.5 christos ((u_char *)(p))[5] = (__v >> 16) & 0xff; \
278 1.5 christos ((u_char *)(p))[6] = (__v >> 8) & 0xff; \
279 1.5 christos ((u_char *)(p))[7] = __v & 0xff; \
280 1.1 christos } while (0)
281 1.1 christos #define POKE_U32(p, v) \
282 1.1 christos do { \
283 1.5 christos const u_int32_t __v = (v); \
284 1.5 christos ((u_char *)(p))[0] = (__v >> 24) & 0xff; \
285 1.5 christos ((u_char *)(p))[1] = (__v >> 16) & 0xff; \
286 1.5 christos ((u_char *)(p))[2] = (__v >> 8) & 0xff; \
287 1.5 christos ((u_char *)(p))[3] = __v & 0xff; \
288 1.1 christos } while (0)
289 1.1 christos #define POKE_U16(p, v) \
290 1.1 christos do { \
291 1.5 christos const u_int16_t __v = (v); \
292 1.5 christos ((u_char *)(p))[0] = (__v >> 8) & 0xff; \
293 1.5 christos ((u_char *)(p))[1] = __v & 0xff; \
294 1.1 christos } while (0)
295 1.1 christos
296 1.1 christos /* Internal definitions follow. Exposed for regress tests */
297 1.1 christos #ifdef SSHBUF_INTERNAL
298 1.1 christos
299 1.1 christos /*
300 1.1 christos * Return the allocation size of buf
301 1.1 christos */
302 1.1 christos size_t sshbuf_alloc(const struct sshbuf *buf);
303 1.1 christos
304 1.1 christos /*
305 1.1 christos * Increment the reference count of buf.
306 1.1 christos */
307 1.1 christos int sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent);
308 1.1 christos
309 1.1 christos /*
310 1.1 christos * Return the parent buffer of buf, or NULL if it has no parent.
311 1.1 christos */
312 1.1 christos const struct sshbuf *sshbuf_parent(const struct sshbuf *buf);
313 1.1 christos
314 1.1 christos /*
315 1.1 christos * Return the reference count of buf
316 1.1 christos */
317 1.1 christos u_int sshbuf_refcount(const struct sshbuf *buf);
318 1.1 christos
319 1.1 christos # define SSHBUF_SIZE_INIT 256 /* Initial allocation */
320 1.1 christos # define SSHBUF_SIZE_INC 256 /* Preferred increment length */
321 1.1 christos # define SSHBUF_PACK_MIN 8192 /* Minimim packable offset */
322 1.1 christos
323 1.1 christos /* # define SSHBUF_ABORT abort */
324 1.1 christos /* # define SSHBUF_DEBUG */
325 1.1 christos
326 1.1 christos # ifndef SSHBUF_ABORT
327 1.1 christos # define SSHBUF_ABORT()
328 1.1 christos # endif
329 1.1 christos
330 1.1 christos # ifdef SSHBUF_DEBUG
331 1.1 christos # define SSHBUF_TELL(what) do { \
332 1.1 christos printf("%s:%d %s: %s size %zu alloc %zu off %zu max %zu\n", \
333 1.1 christos __FILE__, __LINE__, __func__, what, \
334 1.1 christos buf->size, buf->alloc, buf->off, buf->max_size); \
335 1.1 christos fflush(stdout); \
336 1.1 christos } while (0)
337 1.1 christos # define SSHBUF_DBG(x) do { \
338 1.1 christos printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \
339 1.1 christos printf x; \
340 1.1 christos printf("\n"); \
341 1.1 christos fflush(stdout); \
342 1.1 christos } while (0)
343 1.1 christos # else
344 1.1 christos # define SSHBUF_TELL(what)
345 1.1 christos # define SSHBUF_DBG(x)
346 1.1 christos # endif
347 1.1 christos #endif /* SSHBUF_INTERNAL */
348 1.1 christos
349 1.1 christos #endif /* _SSHBUF_H */
350