1 /* $NetBSD: sshbuf.h,v 1.23 2026/04/08 18:58:41 christos Exp $ */ 2 /* $OpenBSD: sshbuf.h,v 1.35 2026/03/03 09:57:25 dtucker 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 * On success, a pointer to the newly allocated buffer is placed in *bufp. 73 * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 74 */ 75 int sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp); 76 77 /* 78 * Clear and free buf 79 */ 80 void sshbuf_free(struct sshbuf *buf); 81 82 /* 83 * Reset buf, clearing its contents. NB. max_size is preserved. 84 */ 85 void sshbuf_reset(struct sshbuf *buf); 86 87 /* 88 * Return the maximum size of buf 89 */ 90 size_t sshbuf_max_size(const struct sshbuf *buf); 91 92 /* 93 * Set the maximum size of buf 94 * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 95 */ 96 int sshbuf_set_max_size(struct sshbuf *buf, size_t max_size); 97 98 /* 99 * Returns the length of data in buf 100 */ 101 size_t sshbuf_len(const struct sshbuf *buf); 102 103 /* 104 * Returns number of bytes left in buffer before hitting max_size. 105 */ 106 size_t sshbuf_avail(const struct sshbuf *buf); 107 108 /* 109 * Returns a read-only pointer to the start of the data in buf 110 */ 111 const u_char *sshbuf_ptr(const struct sshbuf *buf); 112 113 /* 114 * Returns a mutable pointer to the start of the data in buf, or 115 * NULL if the buffer is read-only. 116 */ 117 u_char *sshbuf_mutable_ptr(const struct sshbuf *buf); 118 119 /* 120 * Check whether a reservation of size len will succeed in buf 121 * Safer to use than direct comparisons again sshbuf_avail as it copes 122 * with unsigned overflows correctly. 123 * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 124 */ 125 int sshbuf_check_reserve(const struct sshbuf *buf, size_t len); 126 127 /* 128 * Preallocates len additional bytes in buf. 129 * Useful for cases where the caller knows how many bytes will ultimately be 130 * required to avoid realloc in the buffer code. 131 * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 132 */ 133 int sshbuf_allocate(struct sshbuf *buf, size_t len); 134 135 /* 136 * Reserve len bytes in buf. 137 * Returns 0 on success and a pointer to the first reserved byte via the 138 * optional dpp parameter or a negative SSH_ERR_* error code on failure. 139 */ 140 int sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp); 141 142 /* 143 * Consume len bytes from the start of buf 144 * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 145 */ 146 int sshbuf_consume(struct sshbuf *buf, size_t len); 147 148 /* 149 * Consume len bytes from the end of buf 150 * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 151 */ 152 int sshbuf_consume_end(struct sshbuf *buf, size_t len); 153 154 /* 155 * Consume data from a parent buffer up to that of a child buffer (i.e. 156 * one created by sshbuf_fromb()). 157 * 158 * Intended to be used in a pattern like: 159 * 160 * b = sshbuf_fromb(parent); 161 * sshbuf_get_string(b, &foo, &foostr); 162 * sshbuf_get_u32(b, &bar); 163 * sshbuf_consume_upto_child(parent, b); 164 * 165 * After which, both "b" and "parent" will point to the same data. 166 * 167 * "child" must be a direct child of "buf" (i.e. neither an unrelated buffer 168 * nor a grandchild) which has consumed data past that of "buf". 169 */ 170 int sshbuf_consume_upto_child(struct sshbuf *buf, const struct sshbuf *child); 171 172 /* Extract or deposit some bytes */ 173 int sshbuf_get(struct sshbuf *buf, void *v, size_t len); 174 int sshbuf_put(struct sshbuf *buf, const void *v, size_t len); 175 int sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v); 176 177 /* Append using a printf(3) format */ 178 int sshbuf_putf(struct sshbuf *buf, const char *fmt, ...) 179 __attribute__((format(printf, 2, 3))); 180 int sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap) 181 __printflike(2, 0); 182 183 /* Functions to extract or store big-endian words of various sizes */ 184 int sshbuf_get_u64(struct sshbuf *buf, uint64_t *valp); 185 int sshbuf_get_u32(struct sshbuf *buf, uint32_t *valp); 186 int sshbuf_get_u16(struct sshbuf *buf, uint16_t *valp); 187 int sshbuf_get_u8(struct sshbuf *buf, u_char *valp); 188 int sshbuf_put_u64(struct sshbuf *buf, uint64_t val); 189 int sshbuf_put_u32(struct sshbuf *buf, uint32_t val); 190 int sshbuf_put_u16(struct sshbuf *buf, uint16_t val); 191 int sshbuf_put_u8(struct sshbuf *buf, u_char val); 192 193 /* Functions to peek at the contents of a buffer without modifying it. */ 194 int sshbuf_peek_u64(const struct sshbuf *buf, size_t offset, 195 uint64_t *valp); 196 int sshbuf_peek_u32(const struct sshbuf *buf, size_t offset, 197 uint32_t *valp); 198 int sshbuf_peek_u16(const struct sshbuf *buf, size_t offset, 199 uint16_t *valp); 200 int sshbuf_peek_u8(const struct sshbuf *buf, size_t offset, 201 u_char *valp); 202 203 /* 204 * Functions to poke values into an existing buffer (e.g. a length header 205 * to a packet). The destination bytes must already exist in the buffer. 206 */ 207 int sshbuf_poke_u64(struct sshbuf *buf, size_t offset, uint64_t val); 208 int sshbuf_poke_u32(struct sshbuf *buf, size_t offset, uint32_t val); 209 int sshbuf_poke_u16(struct sshbuf *buf, size_t offset, uint16_t val); 210 int sshbuf_poke_u8(struct sshbuf *buf, size_t offset, u_char val); 211 int sshbuf_poke(struct sshbuf *buf, size_t offset, void *v, size_t len); 212 213 /* 214 * Functions to extract or store SSH wire encoded strings (u32 len || data) 215 * The "cstring" variants admit no \0 characters in the string contents. 216 * Caller must free *valp. 217 */ 218 int sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp); 219 int sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp); 220 int sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v); 221 int sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len); 222 int sshbuf_put_cstring(struct sshbuf *buf, const char *v); 223 int sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v); 224 225 /* 226 * "Direct" variant of sshbuf_get_string, returns pointer into the sshbuf to 227 * avoid an malloc+memcpy. The pointer is guaranteed to be valid until the 228 * next sshbuf-modifying function call. Caller does not free. 229 */ 230 int sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp, 231 size_t *lenp); 232 233 /* Skip past a string */ 234 #define sshbuf_skip_string(buf) sshbuf_get_string_direct(buf, NULL, NULL) 235 236 /* Another variant: "peeks" into the buffer without modifying it */ 237 int sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp, 238 size_t *lenp); 239 240 /* 241 * Functions to extract or store SSH wire encoded bignums and elliptic 242 * curve points. 243 */ 244 int sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM **valp); 245 int sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf, 246 const u_char **valp, size_t *lenp); 247 int sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v); 248 int sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len); 249 int sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g); 250 int sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v); 251 int sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g); 252 int sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v); 253 int sshbuf_put_ec_pkey(struct sshbuf *buf, EVP_PKEY *pkey); 254 255 /* Functions to extract or store various non-SSH wire encoded values */ 256 int sshbuf_get_nulterminated_string(struct sshbuf *buf, size_t maxlen, 257 char **valp, size_t *lenp); 258 259 /* Dump the contents of the buffer in a human-readable format */ 260 void sshbuf_dump(const struct sshbuf *buf, FILE *f); 261 262 /* Dump specified memory in a human-readable format */ 263 void sshbuf_dump_data(const void *s, size_t len, FILE *f); 264 265 /* Return the hexadecimal representation of the contents of the buffer */ 266 char *sshbuf_dtob16(const struct sshbuf *buf); 267 /* Make a sshbuf from a hex string */ 268 struct sshbuf *sshbuf_b16tod(const char *b16); 269 270 /* Encode the contents of the buffer as base64 */ 271 char *sshbuf_dtob64_string(const struct sshbuf *buf, int wrap); 272 int sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap); 273 /* RFC4648 "base64url" encoding variant */ 274 int sshbuf_dtourlb64(const struct sshbuf *d, struct sshbuf *b64, int wrap); 275 276 /* Decode base64 data and append it to the buffer */ 277 int sshbuf_b64tod(struct sshbuf *buf, const char *b64); 278 279 /* 280 * Tests whether the buffer contains the specified byte sequence at the 281 * specified offset. Returns 0 on successful match, or a ssherr.h code 282 * otherwise. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were 283 * present but the buffer contents did not match those supplied. Zero- 284 * length comparisons are not allowed. 285 * 286 * If sufficient data is present to make a comparison, then it is 287 * performed with timing independent of the value of the data. If 288 * insufficient data is present then the comparison is not attempted at 289 * all. 290 */ 291 int sshbuf_cmp(const struct sshbuf *b, size_t offset, 292 const void *s, size_t len); 293 294 /* 295 * Test whether two buffers have identical contents. 296 * SSH_ERR_MESSAGE_INCOMPLETE indicates the buffers had differing size. 297 * SSH_ERR_INVALID_FORMAT indicates the buffers were the same size but 298 * had differing contents. 299 * Returns 0 on successful compare (comparing two empty buffers returns 0). 300 */ 301 int sshbuf_equals(const struct sshbuf *a, const struct sshbuf *b); 302 303 /* 304 * Searches the buffer for the specified string. Returns 0 on success 305 * and updates *offsetp with the offset of the first match, relative to 306 * the start of the buffer. Otherwise sshbuf_find will return a ssherr.h 307 * error code. SSH_ERR_INVALID_FORMAT indicates sufficient bytes were 308 * present in the buffer for a match to be possible but none was found. 309 * Searches for zero-length data are not allowed. 310 */ 311 int 312 sshbuf_find(const struct sshbuf *b, size_t start_offset, 313 const void *s, size_t len, size_t *offsetp); 314 315 /* 316 * Duplicate the contents of a buffer to a string (caller to free). 317 * Returns NULL on buffer error, or if the buffer contains a premature 318 * nul character. 319 */ 320 char *sshbuf_dup_string(struct sshbuf *buf); 321 322 /* 323 * Fill a buffer from a file descriptor or filename. Both allocate the 324 * buffer for the caller. 325 */ 326 int sshbuf_load_fd(int, struct sshbuf **) 327 __attribute__((__nonnull__ (2))); 328 int sshbuf_load_file(const char *, struct sshbuf **) 329 __attribute__((__nonnull__ (2))); 330 331 /* 332 * Write a buffer to a path, creating/truncating as needed (mode 0644, 333 * subject to umask). The buffer contents are not modified. 334 */ 335 int sshbuf_write_file(const char *path, struct sshbuf *buf) 336 __attribute__((__nonnull__ (2))); 337 338 /* Read up to maxlen bytes from a fd directly to a buffer */ 339 int sshbuf_read(int, struct sshbuf *, size_t, size_t *) 340 __attribute__((__nonnull__ (2))); 341 342 /* Macros for decoding/encoding integers */ 343 #define PEEK_U64(p) \ 344 (((uint64_t)(((const u_char *)(p))[0]) << 56) | \ 345 ((uint64_t)(((const u_char *)(p))[1]) << 48) | \ 346 ((uint64_t)(((const u_char *)(p))[2]) << 40) | \ 347 ((uint64_t)(((const u_char *)(p))[3]) << 32) | \ 348 ((uint64_t)(((const u_char *)(p))[4]) << 24) | \ 349 ((uint64_t)(((const u_char *)(p))[5]) << 16) | \ 350 ((uint64_t)(((const u_char *)(p))[6]) << 8) | \ 351 (uint64_t)(((const u_char *)(p))[7])) 352 #define PEEK_U32(p) \ 353 (((uint32_t)(((const u_char *)(p))[0]) << 24) | \ 354 ((uint32_t)(((const u_char *)(p))[1]) << 16) | \ 355 ((uint32_t)(((const u_char *)(p))[2]) << 8) | \ 356 (uint32_t)(((const u_char *)(p))[3])) 357 #define PEEK_U16(p) \ 358 (((uint16_t)(((const u_char *)(p))[0]) << 8) | \ 359 (uint16_t)(((const u_char *)(p))[1])) 360 361 #define POKE_U64(p, v) \ 362 do { \ 363 const uint64_t __v = (v); \ 364 ((u_char *)(p))[0] = (__v >> 56) & 0xff; \ 365 ((u_char *)(p))[1] = (__v >> 48) & 0xff; \ 366 ((u_char *)(p))[2] = (__v >> 40) & 0xff; \ 367 ((u_char *)(p))[3] = (__v >> 32) & 0xff; \ 368 ((u_char *)(p))[4] = (__v >> 24) & 0xff; \ 369 ((u_char *)(p))[5] = (__v >> 16) & 0xff; \ 370 ((u_char *)(p))[6] = (__v >> 8) & 0xff; \ 371 ((u_char *)(p))[7] = __v & 0xff; \ 372 } while (0) 373 #define POKE_U32(p, v) \ 374 do { \ 375 const uint32_t __v = (v); \ 376 ((u_char *)(p))[0] = (__v >> 24) & 0xff; \ 377 ((u_char *)(p))[1] = (__v >> 16) & 0xff; \ 378 ((u_char *)(p))[2] = (__v >> 8) & 0xff; \ 379 ((u_char *)(p))[3] = __v & 0xff; \ 380 } while (0) 381 #define POKE_U16(p, v) \ 382 do { \ 383 const uint16_t __v = (v); \ 384 ((u_char *)(p))[0] = (__v >> 8) & 0xff; \ 385 ((u_char *)(p))[1] = __v & 0xff; \ 386 } while (0) 387 388 /* Internal definitions follow. Exposed for regress tests */ 389 #ifdef SSHBUF_INTERNAL 390 391 /* 392 * Return the allocation size of buf 393 */ 394 size_t sshbuf_alloc(const struct sshbuf *buf); 395 396 /* 397 * Increment the reference count of buf. 398 */ 399 int sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent); 400 401 /* 402 * Return the parent buffer of buf, or NULL if it has no parent. 403 */ 404 const struct sshbuf *sshbuf_parent(const struct sshbuf *buf); 405 406 /* 407 * Return the reference count of buf 408 */ 409 u_int sshbuf_refcount(const struct sshbuf *buf); 410 411 # define SSHBUF_SIZE_INIT 256 /* Initial allocation */ 412 # define SSHBUF_SIZE_INC 256 /* Preferred increment length */ 413 # define SSHBUF_PACK_MIN 8192 /* Minimum packable offset */ 414 415 /* # define SSHBUF_ABORT abort */ 416 /* # define SSHBUF_DEBUG */ 417 418 # ifndef SSHBUF_ABORT 419 # define SSHBUF_ABORT() 420 # endif 421 422 # ifdef SSHBUF_DEBUG 423 # define SSHBUF_DBG(x) do { \ 424 printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \ 425 printf x; \ 426 printf("\n"); \ 427 fflush(stdout); \ 428 } while (0) 429 # else 430 # define SSHBUF_DBG(x) 431 # endif 432 #endif /* SSHBUF_INTERNAL */ 433 434 #endif /* _SSHBUF_H */ 435