sshbuf.h revision 1.21 1 1.17 mlelstv /* $NetBSD: sshbuf.h,v 1.21 2024/09/24 21:32:19 christos Exp $ */
2 1.21 christos /* $OpenBSD: sshbuf.h,v 1.29 2024/08/15 00:51:51 djm Exp $ */
3 1.21 christos
4 1.1 christos /*
5 1.1 christos * Copyright (c) 2011 Damien Miller
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos #ifndef _SSHBUF_H
21 1.1 christos #define _SSHBUF_H
22 1.1 christos
23 1.1 christos #include <sys/types.h>
24 1.1 christos #include <stdarg.h>
25 1.1 christos #include <stdio.h>
26 1.12 christos
27 1.12 christos #ifdef WITH_OPENSSL
28 1.1 christos #include <openssl/bn.h>
29 1.1 christos #include <openssl/ec.h>
30 1.12 christos #include <openssl/ecdsa.h>
31 1.21 christos #include <openssl/evp.h>
32 1.12 christos #else /* OPENSSL */
33 1.12 christos #define BIGNUM void
34 1.12 christos #define EC_KEY void
35 1.12 christos #define EC_GROUP void
36 1.12 christos #define EC_POINT void
37 1.21 christos #define EVP_PKEY void
38 1.12 christos #endif /* WITH_OPENSSL */
39 1.1 christos
40 1.17 mlelstv #define SSHBUF_SIZE_MAX 0x10000000 /* Hard maximum size 256MB */
41 1.1 christos #define SSHBUF_REFS_MAX 0x100000 /* Max child buffers */
42 1.1 christos #define SSHBUF_MAX_BIGNUM (16384 / 8) /* Max bignum *bytes* */
43 1.1 christos #define SSHBUF_MAX_ECPOINT ((528 * 2 / 8) + 1) /* Max EC point *bytes* */
44 1.1 christos
45 1.19 christos struct sshbuf;
46 1.1 christos
47 1.1 christos /*
48 1.1 christos * Create a new sshbuf buffer.
49 1.1 christos * Returns pointer to buffer on success, or NULL on allocation failure.
50 1.1 christos */
51 1.1 christos struct sshbuf *sshbuf_new(void);
52 1.1 christos
53 1.1 christos /*
54 1.1 christos * Create a new, read-only sshbuf buffer from existing data.
55 1.1 christos * Returns pointer to buffer on success, or NULL on allocation failure.
56 1.1 christos */
57 1.1 christos struct sshbuf *sshbuf_from(const void *blob, size_t len);
58 1.1 christos
59 1.1 christos /*
60 1.1 christos * Create a new, read-only sshbuf buffer from the contents of an existing
61 1.1 christos * buffer. The contents of "buf" must not change in the lifetime of the
62 1.1 christos * resultant buffer.
63 1.1 christos * Returns pointer to buffer on success, or NULL on allocation failure.
64 1.1 christos */
65 1.1 christos struct sshbuf *sshbuf_fromb(struct sshbuf *buf);
66 1.1 christos
67 1.1 christos /*
68 1.1 christos * Create a new, read-only sshbuf buffer from the contents of a string in
69 1.1 christos * an existing buffer (the string is consumed in the process).
70 1.1 christos * The contents of "buf" must not change in the lifetime of the resultant
71 1.1 christos * buffer.
72 1.1 christos * Returns pointer to buffer on success, or NULL on allocation failure.
73 1.1 christos */
74 1.1 christos int sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp);
75 1.1 christos
76 1.1 christos /*
77 1.1 christos * Clear and free buf
78 1.1 christos */
79 1.1 christos void sshbuf_free(struct sshbuf *buf);
80 1.1 christos
81 1.1 christos /*
82 1.1 christos * Reset buf, clearing its contents. NB. max_size is preserved.
83 1.1 christos */
84 1.1 christos void sshbuf_reset(struct sshbuf *buf);
85 1.1 christos
86 1.1 christos /*
87 1.1 christos * Return the maximum size of buf
88 1.1 christos */
89 1.1 christos size_t sshbuf_max_size(const struct sshbuf *buf);
90 1.1 christos
91 1.1 christos /*
92 1.1 christos * Set the maximum size of buf
93 1.1 christos * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
94 1.1 christos */
95 1.1 christos int sshbuf_set_max_size(struct sshbuf *buf, size_t max_size);
96 1.1 christos
97 1.1 christos /*
98 1.1 christos * Returns the length of data in buf
99 1.1 christos */
100 1.1 christos size_t sshbuf_len(const struct sshbuf *buf);
101 1.1 christos
102 1.1 christos /*
103 1.1 christos * Returns number of bytes left in buffer before hitting max_size.
104 1.1 christos */
105 1.1 christos size_t sshbuf_avail(const struct sshbuf *buf);
106 1.1 christos
107 1.1 christos /*
108 1.5 christos * Returns a read-only pointer to the start of the data in buf
109 1.1 christos */
110 1.1 christos const u_char *sshbuf_ptr(const struct sshbuf *buf);
111 1.1 christos
112 1.1 christos /*
113 1.5 christos * Returns a mutable pointer to the start of the data in buf, or
114 1.1 christos * NULL if the buffer is read-only.
115 1.1 christos */
116 1.1 christos u_char *sshbuf_mutable_ptr(const struct sshbuf *buf);
117 1.1 christos
118 1.1 christos /*
119 1.1 christos * Check whether a reservation of size len will succeed in buf
120 1.1 christos * Safer to use than direct comparisons again sshbuf_avail as it copes
121 1.1 christos * with unsigned overflows correctly.
122 1.1 christos * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
123 1.1 christos */
124 1.1 christos int sshbuf_check_reserve(const struct sshbuf *buf, size_t len);
125 1.1 christos
126 1.1 christos /*
127 1.7 christos * Preallocates len additional bytes in buf.
128 1.7 christos * Useful for cases where the caller knows how many bytes will ultimately be
129 1.7 christos * required to avoid realloc in the buffer code.
130 1.7 christos * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
131 1.7 christos */
132 1.7 christos int sshbuf_allocate(struct sshbuf *buf, size_t len);
133 1.7 christos
134 1.7 christos /*
135 1.1 christos * Reserve len bytes in buf.
136 1.1 christos * Returns 0 on success and a pointer to the first reserved byte via the
137 1.14 christos * optional dpp parameter or a negative SSH_ERR_* error code on failure.
138 1.1 christos */
139 1.1 christos int sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp);
140 1.1 christos
141 1.1 christos /*
142 1.1 christos * Consume len bytes from the start of buf
143 1.1 christos * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
144 1.1 christos */
145 1.1 christos int sshbuf_consume(struct sshbuf *buf, size_t len);
146 1.1 christos
147 1.1 christos /*
148 1.1 christos * Consume len bytes from the end of buf
149 1.1 christos * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
150 1.1 christos */
151 1.1 christos int sshbuf_consume_end(struct sshbuf *buf, size_t len);
152 1.1 christos
153 1.1 christos /* Extract or deposit some bytes */
154 1.1 christos int sshbuf_get(struct sshbuf *buf, void *v, size_t len);
155 1.1 christos int sshbuf_put(struct sshbuf *buf, const void *v, size_t len);
156 1.1 christos int sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v);
157 1.1 christos
158 1.1 christos /* Append using a printf(3) format */
159 1.1 christos int sshbuf_putf(struct sshbuf *buf, const char *fmt, ...)
160 1.1 christos __attribute__((format(printf, 2, 3)));
161 1.3 joerg int sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap)
162 1.3 joerg __printflike(2, 0);
163 1.1 christos
164 1.1 christos /* Functions to extract or store big-endian words of various sizes */
165 1.1 christos int sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp);
166 1.1 christos int sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp);
167 1.1 christos int sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp);
168 1.1 christos int sshbuf_get_u8(struct sshbuf *buf, u_char *valp);
169 1.1 christos int sshbuf_put_u64(struct sshbuf *buf, u_int64_t val);
170 1.1 christos int sshbuf_put_u32(struct sshbuf *buf, u_int32_t val);
171 1.1 christos int sshbuf_put_u16(struct sshbuf *buf, u_int16_t val);
172 1.1 christos int sshbuf_put_u8(struct sshbuf *buf, u_char val);
173 1.1 christos
174 1.12 christos /* Functions to peek at the contents of a buffer without modifying it. */
175 1.12 christos int sshbuf_peek_u64(const struct sshbuf *buf, size_t offset,
176 1.12 christos u_int64_t *valp);
177 1.12 christos int sshbuf_peek_u32(const struct sshbuf *buf, size_t offset,
178 1.12 christos u_int32_t *valp);
179 1.12 christos int sshbuf_peek_u16(const struct sshbuf *buf, size_t offset,
180 1.12 christos u_int16_t *valp);
181 1.12 christos int sshbuf_peek_u8(const struct sshbuf *buf, size_t offset,
182 1.12 christos u_char *valp);
183 1.12 christos
184 1.12 christos /*
185 1.14 christos * Functions to poke values into an existing buffer (e.g. a length header
186 1.12 christos * to a packet). The destination bytes must already exist in the buffer.
187 1.12 christos */
188 1.12 christos int sshbuf_poke_u64(struct sshbuf *buf, size_t offset, u_int64_t val);
189 1.12 christos int sshbuf_poke_u32(struct sshbuf *buf, size_t offset, u_int32_t val);
190 1.12 christos int sshbuf_poke_u16(struct sshbuf *buf, size_t offset, u_int16_t val);
191 1.12 christos int sshbuf_poke_u8(struct sshbuf *buf, size_t offset, u_char val);
192 1.12 christos int sshbuf_poke(struct sshbuf *buf, size_t offset, void *v, size_t len);
193 1.12 christos
194 1.1 christos /*
195 1.1 christos * Functions to extract or store SSH wire encoded strings (u32 len || data)
196 1.1 christos * The "cstring" variants admit no \0 characters in the string contents.
197 1.1 christos * Caller must free *valp.
198 1.1 christos */
199 1.1 christos int sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp);
200 1.1 christos int sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp);
201 1.1 christos int sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v);
202 1.1 christos int sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len);
203 1.1 christos int sshbuf_put_cstring(struct sshbuf *buf, const char *v);
204 1.1 christos int sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v);
205 1.1 christos
206 1.1 christos /*
207 1.1 christos * "Direct" variant of sshbuf_get_string, returns pointer into the sshbuf to
208 1.1 christos * avoid an malloc+memcpy. The pointer is guaranteed to be valid until the
209 1.1 christos * next sshbuf-modifying function call. Caller does not free.
210 1.1 christos */
211 1.1 christos int sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp,
212 1.1 christos size_t *lenp);
213 1.1 christos
214 1.1 christos /* Skip past a string */
215 1.1 christos #define sshbuf_skip_string(buf) sshbuf_get_string_direct(buf, NULL, NULL)
216 1.1 christos
217 1.1 christos /* Another variant: "peeks" into the buffer without modifying it */
218 1.1 christos int sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp,
219 1.1 christos size_t *lenp);
220 1.1 christos
221 1.1 christos /*
222 1.1 christos * Functions to extract or store SSH wire encoded bignums and elliptic
223 1.1 christos * curve points.
224 1.1 christos */
225 1.11 christos int sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM **valp);
226 1.4 christos int sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
227 1.4 christos const u_char **valp, size_t *lenp);
228 1.1 christos int sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v);
229 1.1 christos int sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len);
230 1.1 christos int sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g);
231 1.1 christos int sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v);
232 1.1 christos int sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g);
233 1.1 christos int sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v);
234 1.21 christos int sshbuf_put_ec_pkey(struct sshbuf *buf, EVP_PKEY *pkey);
235 1.1 christos
236 1.1 christos /* Dump the contents of the buffer in a human-readable format */
237 1.15 christos void sshbuf_dump(const struct sshbuf *buf, FILE *f);
238 1.1 christos
239 1.1 christos /* Dump specified memory in a human-readable format */
240 1.1 christos void sshbuf_dump_data(const void *s, size_t len, FILE *f);
241 1.1 christos
242 1.1 christos /* Return the hexadecimal representation of the contents of the buffer */
243 1.1 christos char *sshbuf_dtob16(struct sshbuf *buf);
244 1.1 christos
245 1.1 christos /* Encode the contents of the buffer as base64 */
246 1.12 christos char *sshbuf_dtob64_string(const struct sshbuf *buf, int wrap);
247 1.12 christos int sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap);
248 1.15 christos /* RFC4648 "base64url" encoding variant */
249 1.15 christos int sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap);
250 1.1 christos
251 1.1 christos /* Decode base64 data and append it to the buffer */
252 1.1 christos int sshbuf_b64tod(struct sshbuf *buf, const char *b64);
253 1.1 christos
254 1.6 christos /*
255 1.12 christos * Tests whether the buffer contains the specified byte sequence at the
256 1.12 christos * specified offset. Returns 0 on successful match, or a ssherr.h code
257 1.12 christos * otherwise. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were
258 1.12 christos * present but the buffer contents did not match those supplied. Zero-
259 1.12 christos * length comparisons are not allowed.
260 1.12 christos *
261 1.12 christos * If sufficient data is present to make a comparison, then it is
262 1.12 christos * performed with timing independent of the value of the data. If
263 1.12 christos * insufficient data is present then the comparison is not attempted at
264 1.12 christos * all.
265 1.12 christos */
266 1.12 christos int sshbuf_cmp(const struct sshbuf *b, size_t offset,
267 1.12 christos const void *s, size_t len);
268 1.12 christos
269 1.12 christos /*
270 1.12 christos * Searches the buffer for the specified string. Returns 0 on success
271 1.12 christos * and updates *offsetp with the offset of the first match, relative to
272 1.12 christos * the start of the buffer. Otherwise sshbuf_find will return a ssherr.h
273 1.12 christos * error code. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were
274 1.12 christos * present in the buffer for a match to be possible but none was found.
275 1.12 christos * Searches for zero-length data are not allowed.
276 1.12 christos */
277 1.12 christos int
278 1.12 christos sshbuf_find(const struct sshbuf *b, size_t start_offset,
279 1.12 christos const void *s, size_t len, size_t *offsetp);
280 1.12 christos
281 1.12 christos /*
282 1.6 christos * Duplicate the contents of a buffer to a string (caller to free).
283 1.6 christos * Returns NULL on buffer error, or if the buffer contains a premature
284 1.6 christos * nul character.
285 1.6 christos */
286 1.6 christos char *sshbuf_dup_string(struct sshbuf *buf);
287 1.6 christos
288 1.13 christos /*
289 1.13 christos * Fill a buffer from a file descriptor or filename. Both allocate the
290 1.13 christos * buffer for the caller.
291 1.13 christos */
292 1.13 christos int sshbuf_load_fd(int, struct sshbuf **)
293 1.13 christos __attribute__((__nonnull__ (2)));
294 1.13 christos int sshbuf_load_file(const char *, struct sshbuf **)
295 1.13 christos __attribute__((__nonnull__ (2)));
296 1.13 christos
297 1.13 christos /*
298 1.13 christos * Write a buffer to a path, creating/truncating as needed (mode 0644,
299 1.13 christos * subject to umask). The buffer contents are not modified.
300 1.13 christos */
301 1.13 christos int sshbuf_write_file(const char *path, struct sshbuf *buf)
302 1.13 christos __attribute__((__nonnull__ (2)));
303 1.13 christos
304 1.16 christos /* Read up to maxlen bytes from a fd directly to a buffer */
305 1.16 christos int sshbuf_read(int, struct sshbuf *, size_t, size_t *)
306 1.16 christos __attribute__((__nonnull__ (2)));
307 1.16 christos
308 1.1 christos /* Macros for decoding/encoding integers */
309 1.1 christos #define PEEK_U64(p) \
310 1.2 christos (((u_int64_t)(((const u_char *)(p))[0]) << 56) | \
311 1.2 christos ((u_int64_t)(((const u_char *)(p))[1]) << 48) | \
312 1.2 christos ((u_int64_t)(((const u_char *)(p))[2]) << 40) | \
313 1.2 christos ((u_int64_t)(((const u_char *)(p))[3]) << 32) | \
314 1.2 christos ((u_int64_t)(((const u_char *)(p))[4]) << 24) | \
315 1.2 christos ((u_int64_t)(((const u_char *)(p))[5]) << 16) | \
316 1.2 christos ((u_int64_t)(((const u_char *)(p))[6]) << 8) | \
317 1.2 christos (u_int64_t)(((const u_char *)(p))[7]))
318 1.1 christos #define PEEK_U32(p) \
319 1.2 christos (((u_int32_t)(((const u_char *)(p))[0]) << 24) | \
320 1.2 christos ((u_int32_t)(((const u_char *)(p))[1]) << 16) | \
321 1.2 christos ((u_int32_t)(((const u_char *)(p))[2]) << 8) | \
322 1.2 christos (u_int32_t)(((const u_char *)(p))[3]))
323 1.1 christos #define PEEK_U16(p) \
324 1.2 christos (((u_int16_t)(((const u_char *)(p))[0]) << 8) | \
325 1.2 christos (u_int16_t)(((const u_char *)(p))[1]))
326 1.1 christos
327 1.1 christos #define POKE_U64(p, v) \
328 1.1 christos do { \
329 1.5 christos const u_int64_t __v = (v); \
330 1.5 christos ((u_char *)(p))[0] = (__v >> 56) & 0xff; \
331 1.5 christos ((u_char *)(p))[1] = (__v >> 48) & 0xff; \
332 1.5 christos ((u_char *)(p))[2] = (__v >> 40) & 0xff; \
333 1.5 christos ((u_char *)(p))[3] = (__v >> 32) & 0xff; \
334 1.5 christos ((u_char *)(p))[4] = (__v >> 24) & 0xff; \
335 1.5 christos ((u_char *)(p))[5] = (__v >> 16) & 0xff; \
336 1.5 christos ((u_char *)(p))[6] = (__v >> 8) & 0xff; \
337 1.5 christos ((u_char *)(p))[7] = __v & 0xff; \
338 1.1 christos } while (0)
339 1.1 christos #define POKE_U32(p, v) \
340 1.1 christos do { \
341 1.5 christos const u_int32_t __v = (v); \
342 1.5 christos ((u_char *)(p))[0] = (__v >> 24) & 0xff; \
343 1.5 christos ((u_char *)(p))[1] = (__v >> 16) & 0xff; \
344 1.5 christos ((u_char *)(p))[2] = (__v >> 8) & 0xff; \
345 1.5 christos ((u_char *)(p))[3] = __v & 0xff; \
346 1.1 christos } while (0)
347 1.1 christos #define POKE_U16(p, v) \
348 1.1 christos do { \
349 1.5 christos const u_int16_t __v = (v); \
350 1.5 christos ((u_char *)(p))[0] = (__v >> 8) & 0xff; \
351 1.5 christos ((u_char *)(p))[1] = __v & 0xff; \
352 1.1 christos } while (0)
353 1.1 christos
354 1.1 christos /* Internal definitions follow. Exposed for regress tests */
355 1.1 christos #ifdef SSHBUF_INTERNAL
356 1.1 christos
357 1.1 christos /*
358 1.1 christos * Return the allocation size of buf
359 1.1 christos */
360 1.1 christos size_t sshbuf_alloc(const struct sshbuf *buf);
361 1.1 christos
362 1.1 christos /*
363 1.1 christos * Increment the reference count of buf.
364 1.1 christos */
365 1.1 christos int sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent);
366 1.1 christos
367 1.1 christos /*
368 1.1 christos * Return the parent buffer of buf, or NULL if it has no parent.
369 1.1 christos */
370 1.1 christos const struct sshbuf *sshbuf_parent(const struct sshbuf *buf);
371 1.1 christos
372 1.1 christos /*
373 1.1 christos * Return the reference count of buf
374 1.1 christos */
375 1.1 christos u_int sshbuf_refcount(const struct sshbuf *buf);
376 1.1 christos
377 1.1 christos # define SSHBUF_SIZE_INIT 256 /* Initial allocation */
378 1.1 christos # define SSHBUF_SIZE_INC 256 /* Preferred increment length */
379 1.16 christos # define SSHBUF_PACK_MIN 8192 /* Minimum packable offset */
380 1.1 christos
381 1.1 christos /* # define SSHBUF_ABORT abort */
382 1.1 christos /* # define SSHBUF_DEBUG */
383 1.1 christos
384 1.1 christos # ifndef SSHBUF_ABORT
385 1.1 christos # define SSHBUF_ABORT()
386 1.1 christos # endif
387 1.1 christos
388 1.1 christos # ifdef SSHBUF_DEBUG
389 1.1 christos # define SSHBUF_DBG(x) do { \
390 1.1 christos printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \
391 1.1 christos printf x; \
392 1.1 christos printf("\n"); \
393 1.1 christos fflush(stdout); \
394 1.1 christos } while (0)
395 1.1 christos # else
396 1.1 christos # define SSHBUF_DBG(x)
397 1.1 christos # endif
398 1.1 christos #endif /* SSHBUF_INTERNAL */
399 1.1 christos
400 1.1 christos #endif /* _SSHBUF_H */
401