1323124Sdes/* $OpenBSD: sshbuf.h,v 1.7 2016/05/02 08:49:03 djm Exp $ */ 2276707Sdes/* 3276707Sdes * Copyright (c) 2011 Damien Miller 4276707Sdes * 5276707Sdes * Permission to use, copy, modify, and distribute this software for any 6276707Sdes * purpose with or without fee is hereby granted, provided that the above 7276707Sdes * copyright notice and this permission notice appear in all copies. 8276707Sdes * 9276707Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10276707Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11276707Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12276707Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13276707Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14276707Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15276707Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16276707Sdes */ 17276707Sdes 18276707Sdes#ifndef _SSHBUF_H 19276707Sdes#define _SSHBUF_H 20276707Sdes 21276707Sdes#include <sys/types.h> 22276707Sdes#include <stdarg.h> 23276707Sdes#include <stdio.h> 24276707Sdes#ifdef WITH_OPENSSL 25276707Sdes# include <openssl/bn.h> 26276707Sdes# ifdef OPENSSL_HAS_ECC 27276707Sdes# include <openssl/ec.h> 28276707Sdes# endif /* OPENSSL_HAS_ECC */ 29276707Sdes#endif /* WITH_OPENSSL */ 30276707Sdes 31276707Sdes#define SSHBUF_SIZE_MAX 0x8000000 /* Hard maximum size */ 32276707Sdes#define SSHBUF_REFS_MAX 0x100000 /* Max child buffers */ 33276707Sdes#define SSHBUF_MAX_BIGNUM (16384 / 8) /* Max bignum *bytes* */ 34276707Sdes#define SSHBUF_MAX_ECPOINT ((528 * 2 / 8) + 1) /* Max EC point *bytes* */ 35276707Sdes 36276707Sdes/* 37276707Sdes * NB. do not depend on the internals of this. It will be made opaque 38276707Sdes * one day. 39276707Sdes */ 40276707Sdesstruct sshbuf { 41276707Sdes u_char *d; /* Data */ 42276707Sdes const u_char *cd; /* Const data */ 43276707Sdes size_t off; /* First available byte is buf->d + buf->off */ 44276707Sdes size_t size; /* Last byte is buf->d + buf->size - 1 */ 45276707Sdes size_t max_size; /* Maximum size of buffer */ 46276707Sdes size_t alloc; /* Total bytes allocated to buf->d */ 47276707Sdes int readonly; /* Refers to external, const data */ 48276707Sdes int dont_free; /* Kludge to support sshbuf_init */ 49276707Sdes u_int refcount; /* Tracks self and number of child buffers */ 50276707Sdes struct sshbuf *parent; /* If child, pointer to parent */ 51276707Sdes}; 52276707Sdes 53276707Sdes#ifndef SSHBUF_NO_DEPREACTED 54276707Sdes/* 55276707Sdes * NB. Please do not use sshbuf_init() in new code. Please use sshbuf_new() 56276707Sdes * instead. sshbuf_init() is deprectated and will go away soon (it is 57276707Sdes * only included to allow compat with buffer_* in OpenSSH) 58276707Sdes */ 59276707Sdesvoid sshbuf_init(struct sshbuf *buf); 60276707Sdes#endif 61276707Sdes 62276707Sdes/* 63276707Sdes * Create a new sshbuf buffer. 64276707Sdes * Returns pointer to buffer on success, or NULL on allocation failure. 65276707Sdes */ 66276707Sdesstruct sshbuf *sshbuf_new(void); 67276707Sdes 68276707Sdes/* 69276707Sdes * Create a new, read-only sshbuf buffer from existing data. 70276707Sdes * Returns pointer to buffer on success, or NULL on allocation failure. 71276707Sdes */ 72276707Sdesstruct sshbuf *sshbuf_from(const void *blob, size_t len); 73276707Sdes 74276707Sdes/* 75276707Sdes * Create a new, read-only sshbuf buffer from the contents of an existing 76276707Sdes * buffer. The contents of "buf" must not change in the lifetime of the 77276707Sdes * resultant buffer. 78276707Sdes * Returns pointer to buffer on success, or NULL on allocation failure. 79276707Sdes */ 80276707Sdesstruct sshbuf *sshbuf_fromb(struct sshbuf *buf); 81276707Sdes 82276707Sdes/* 83276707Sdes * Create a new, read-only sshbuf buffer from the contents of a string in 84276707Sdes * an existing buffer (the string is consumed in the process). 85276707Sdes * The contents of "buf" must not change in the lifetime of the resultant 86276707Sdes * buffer. 87276707Sdes * Returns pointer to buffer on success, or NULL on allocation failure. 88276707Sdes */ 89276707Sdesint sshbuf_froms(struct sshbuf *buf, struct sshbuf **bufp); 90276707Sdes 91276707Sdes/* 92276707Sdes * Clear and free buf 93276707Sdes */ 94276707Sdesvoid sshbuf_free(struct sshbuf *buf); 95276707Sdes 96276707Sdes/* 97276707Sdes * Reset buf, clearing its contents. NB. max_size is preserved. 98276707Sdes */ 99276707Sdesvoid sshbuf_reset(struct sshbuf *buf); 100276707Sdes 101276707Sdes/* 102276707Sdes * Return the maximum size of buf 103276707Sdes */ 104276707Sdessize_t sshbuf_max_size(const struct sshbuf *buf); 105276707Sdes 106276707Sdes/* 107276707Sdes * Set the maximum size of buf 108276707Sdes * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 109276707Sdes */ 110276707Sdesint sshbuf_set_max_size(struct sshbuf *buf, size_t max_size); 111276707Sdes 112276707Sdes/* 113276707Sdes * Returns the length of data in buf 114276707Sdes */ 115276707Sdessize_t sshbuf_len(const struct sshbuf *buf); 116276707Sdes 117276707Sdes/* 118276707Sdes * Returns number of bytes left in buffer before hitting max_size. 119276707Sdes */ 120276707Sdessize_t sshbuf_avail(const struct sshbuf *buf); 121276707Sdes 122276707Sdes/* 123296781Sdes * Returns a read-only pointer to the start of the data in buf 124276707Sdes */ 125276707Sdesconst u_char *sshbuf_ptr(const struct sshbuf *buf); 126276707Sdes 127276707Sdes/* 128296781Sdes * Returns a mutable pointer to the start of the data in buf, or 129276707Sdes * NULL if the buffer is read-only. 130276707Sdes */ 131276707Sdesu_char *sshbuf_mutable_ptr(const struct sshbuf *buf); 132276707Sdes 133276707Sdes/* 134276707Sdes * Check whether a reservation of size len will succeed in buf 135276707Sdes * Safer to use than direct comparisons again sshbuf_avail as it copes 136276707Sdes * with unsigned overflows correctly. 137276707Sdes * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 138276707Sdes */ 139276707Sdesint sshbuf_check_reserve(const struct sshbuf *buf, size_t len); 140276707Sdes 141276707Sdes/* 142276707Sdes * Reserve len bytes in buf. 143276707Sdes * Returns 0 on success and a pointer to the first reserved byte via the 144276707Sdes * optional dpp parameter or a negative * SSH_ERR_* error code on failure. 145276707Sdes */ 146276707Sdesint sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp); 147276707Sdes 148276707Sdes/* 149276707Sdes * Consume len bytes from the start of buf 150276707Sdes * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 151276707Sdes */ 152276707Sdesint sshbuf_consume(struct sshbuf *buf, size_t len); 153276707Sdes 154276707Sdes/* 155276707Sdes * Consume len bytes from the end of buf 156276707Sdes * Returns 0 on success, or a negative SSH_ERR_* error code on failure. 157276707Sdes */ 158276707Sdesint sshbuf_consume_end(struct sshbuf *buf, size_t len); 159276707Sdes 160276707Sdes/* Extract or deposit some bytes */ 161276707Sdesint sshbuf_get(struct sshbuf *buf, void *v, size_t len); 162276707Sdesint sshbuf_put(struct sshbuf *buf, const void *v, size_t len); 163276707Sdesint sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v); 164276707Sdes 165276707Sdes/* Append using a printf(3) format */ 166276707Sdesint sshbuf_putf(struct sshbuf *buf, const char *fmt, ...) 167276707Sdes __attribute__((format(printf, 2, 3))); 168276707Sdesint sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap); 169276707Sdes 170276707Sdes/* Functions to extract or store big-endian words of various sizes */ 171276707Sdesint sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp); 172276707Sdesint sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp); 173276707Sdesint sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp); 174276707Sdesint sshbuf_get_u8(struct sshbuf *buf, u_char *valp); 175276707Sdesint sshbuf_put_u64(struct sshbuf *buf, u_int64_t val); 176276707Sdesint sshbuf_put_u32(struct sshbuf *buf, u_int32_t val); 177276707Sdesint sshbuf_put_u16(struct sshbuf *buf, u_int16_t val); 178276707Sdesint sshbuf_put_u8(struct sshbuf *buf, u_char val); 179276707Sdes 180276707Sdes/* 181276707Sdes * Functions to extract or store SSH wire encoded strings (u32 len || data) 182276707Sdes * The "cstring" variants admit no \0 characters in the string contents. 183276707Sdes * Caller must free *valp. 184276707Sdes */ 185276707Sdesint sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp); 186276707Sdesint sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp); 187276707Sdesint sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v); 188276707Sdesint sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len); 189276707Sdesint sshbuf_put_cstring(struct sshbuf *buf, const char *v); 190276707Sdesint sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v); 191276707Sdes 192276707Sdes/* 193276707Sdes * "Direct" variant of sshbuf_get_string, returns pointer into the sshbuf to 194276707Sdes * avoid an malloc+memcpy. The pointer is guaranteed to be valid until the 195276707Sdes * next sshbuf-modifying function call. Caller does not free. 196276707Sdes */ 197276707Sdesint sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp, 198276707Sdes size_t *lenp); 199276707Sdes 200276707Sdes/* Skip past a string */ 201276707Sdes#define sshbuf_skip_string(buf) sshbuf_get_string_direct(buf, NULL, NULL) 202276707Sdes 203276707Sdes/* Another variant: "peeks" into the buffer without modifying it */ 204276707Sdesint sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp, 205276707Sdes size_t *lenp); 206276707Sdes 207276707Sdes/* 208276707Sdes * Functions to extract or store SSH wire encoded bignums and elliptic 209276707Sdes * curve points. 210276707Sdes */ 211276707Sdesint sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len); 212295367Sdesint sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf, 213295367Sdes const u_char **valp, size_t *lenp); 214276707Sdes#ifdef WITH_OPENSSL 215276707Sdesint sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM *v); 216276707Sdesint sshbuf_get_bignum1(struct sshbuf *buf, BIGNUM *v); 217276707Sdesint sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v); 218276707Sdesint sshbuf_put_bignum1(struct sshbuf *buf, const BIGNUM *v); 219276707Sdes# ifdef OPENSSL_HAS_ECC 220276707Sdesint sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g); 221276707Sdesint sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v); 222276707Sdesint sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g); 223276707Sdesint sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v); 224276707Sdes# endif /* OPENSSL_HAS_ECC */ 225276707Sdes#endif /* WITH_OPENSSL */ 226276707Sdes 227276707Sdes/* Dump the contents of the buffer in a human-readable format */ 228276707Sdesvoid sshbuf_dump(struct sshbuf *buf, FILE *f); 229276707Sdes 230276707Sdes/* Dump specified memory in a human-readable format */ 231276707Sdesvoid sshbuf_dump_data(const void *s, size_t len, FILE *f); 232276707Sdes 233276707Sdes/* Return the hexadecimal representation of the contents of the buffer */ 234276707Sdeschar *sshbuf_dtob16(struct sshbuf *buf); 235276707Sdes 236276707Sdes/* Encode the contents of the buffer as base64 */ 237276707Sdeschar *sshbuf_dtob64(struct sshbuf *buf); 238276707Sdes 239276707Sdes/* Decode base64 data and append it to the buffer */ 240276707Sdesint sshbuf_b64tod(struct sshbuf *buf, const char *b64); 241276707Sdes 242323124Sdes/* 243323124Sdes * Duplicate the contents of a buffer to a string (caller to free). 244323124Sdes * Returns NULL on buffer error, or if the buffer contains a premature 245323124Sdes * nul character. 246323124Sdes */ 247323124Sdeschar *sshbuf_dup_string(struct sshbuf *buf); 248323124Sdes 249276707Sdes/* Macros for decoding/encoding integers */ 250276707Sdes#define PEEK_U64(p) \ 251296781Sdes (((u_int64_t)(((const u_char *)(p))[0]) << 56) | \ 252296781Sdes ((u_int64_t)(((const u_char *)(p))[1]) << 48) | \ 253296781Sdes ((u_int64_t)(((const u_char *)(p))[2]) << 40) | \ 254296781Sdes ((u_int64_t)(((const u_char *)(p))[3]) << 32) | \ 255296781Sdes ((u_int64_t)(((const u_char *)(p))[4]) << 24) | \ 256296781Sdes ((u_int64_t)(((const u_char *)(p))[5]) << 16) | \ 257296781Sdes ((u_int64_t)(((const u_char *)(p))[6]) << 8) | \ 258296781Sdes (u_int64_t)(((const u_char *)(p))[7])) 259276707Sdes#define PEEK_U32(p) \ 260296781Sdes (((u_int32_t)(((const u_char *)(p))[0]) << 24) | \ 261296781Sdes ((u_int32_t)(((const u_char *)(p))[1]) << 16) | \ 262296781Sdes ((u_int32_t)(((const u_char *)(p))[2]) << 8) | \ 263296781Sdes (u_int32_t)(((const u_char *)(p))[3])) 264276707Sdes#define PEEK_U16(p) \ 265296781Sdes (((u_int16_t)(((const u_char *)(p))[0]) << 8) | \ 266296781Sdes (u_int16_t)(((const u_char *)(p))[1])) 267276707Sdes 268276707Sdes#define POKE_U64(p, v) \ 269276707Sdes do { \ 270296781Sdes const u_int64_t __v = (v); \ 271296781Sdes ((u_char *)(p))[0] = (__v >> 56) & 0xff; \ 272296781Sdes ((u_char *)(p))[1] = (__v >> 48) & 0xff; \ 273296781Sdes ((u_char *)(p))[2] = (__v >> 40) & 0xff; \ 274296781Sdes ((u_char *)(p))[3] = (__v >> 32) & 0xff; \ 275296781Sdes ((u_char *)(p))[4] = (__v >> 24) & 0xff; \ 276296781Sdes ((u_char *)(p))[5] = (__v >> 16) & 0xff; \ 277296781Sdes ((u_char *)(p))[6] = (__v >> 8) & 0xff; \ 278296781Sdes ((u_char *)(p))[7] = __v & 0xff; \ 279276707Sdes } while (0) 280276707Sdes#define POKE_U32(p, v) \ 281276707Sdes do { \ 282296781Sdes const u_int32_t __v = (v); \ 283296781Sdes ((u_char *)(p))[0] = (__v >> 24) & 0xff; \ 284296781Sdes ((u_char *)(p))[1] = (__v >> 16) & 0xff; \ 285296781Sdes ((u_char *)(p))[2] = (__v >> 8) & 0xff; \ 286296781Sdes ((u_char *)(p))[3] = __v & 0xff; \ 287276707Sdes } while (0) 288276707Sdes#define POKE_U16(p, v) \ 289276707Sdes do { \ 290296781Sdes const u_int16_t __v = (v); \ 291296781Sdes ((u_char *)(p))[0] = (__v >> 8) & 0xff; \ 292296781Sdes ((u_char *)(p))[1] = __v & 0xff; \ 293276707Sdes } while (0) 294276707Sdes 295276707Sdes/* Internal definitions follow. Exposed for regress tests */ 296276707Sdes#ifdef SSHBUF_INTERNAL 297276707Sdes 298276707Sdes/* 299276707Sdes * Return the allocation size of buf 300276707Sdes */ 301276707Sdessize_t sshbuf_alloc(const struct sshbuf *buf); 302276707Sdes 303276707Sdes/* 304276707Sdes * Increment the reference count of buf. 305276707Sdes */ 306276707Sdesint sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent); 307276707Sdes 308276707Sdes/* 309276707Sdes * Return the parent buffer of buf, or NULL if it has no parent. 310276707Sdes */ 311276707Sdesconst struct sshbuf *sshbuf_parent(const struct sshbuf *buf); 312276707Sdes 313276707Sdes/* 314276707Sdes * Return the reference count of buf 315276707Sdes */ 316276707Sdesu_int sshbuf_refcount(const struct sshbuf *buf); 317276707Sdes 318276707Sdes# define SSHBUF_SIZE_INIT 256 /* Initial allocation */ 319276707Sdes# define SSHBUF_SIZE_INC 256 /* Preferred increment length */ 320276707Sdes# define SSHBUF_PACK_MIN 8192 /* Minimim packable offset */ 321276707Sdes 322276707Sdes/* # define SSHBUF_ABORT abort */ 323276707Sdes/* # define SSHBUF_DEBUG */ 324276707Sdes 325276707Sdes# ifndef SSHBUF_ABORT 326276707Sdes# define SSHBUF_ABORT() 327276707Sdes# endif 328276707Sdes 329276707Sdes# ifdef SSHBUF_DEBUG 330276707Sdes# define SSHBUF_TELL(what) do { \ 331276707Sdes printf("%s:%d %s: %s size %zu alloc %zu off %zu max %zu\n", \ 332276707Sdes __FILE__, __LINE__, __func__, what, \ 333276707Sdes buf->size, buf->alloc, buf->off, buf->max_size); \ 334276707Sdes fflush(stdout); \ 335276707Sdes } while (0) 336276707Sdes# define SSHBUF_DBG(x) do { \ 337276707Sdes printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \ 338276707Sdes printf x; \ 339276707Sdes printf("\n"); \ 340276707Sdes fflush(stdout); \ 341276707Sdes } while (0) 342276707Sdes# else 343276707Sdes# define SSHBUF_TELL(what) 344276707Sdes# define SSHBUF_DBG(x) 345276707Sdes# endif 346276707Sdes#endif /* SSHBUF_INTERNAL */ 347276707Sdes 348276707Sdes#endif /* _SSHBUF_H */ 349