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