Home | History | Annotate | Line # | Download | only in dist
sshbuf.h revision 1.5
      1  1.5  christos /*	$OpenBSD: sshbuf.h,v 1.6 2015/12/10 07:01:35 mmcc 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.1  christos  * Reserve len bytes in buf.
    139  1.1  christos  * Returns 0 on success and a pointer to the first reserved byte via the
    140  1.1  christos  * optional dpp parameter or a negative * SSH_ERR_* error code on failure.
    141  1.1  christos  */
    142  1.1  christos int	sshbuf_reserve(struct sshbuf *buf, size_t len, u_char **dpp);
    143  1.1  christos 
    144  1.1  christos /*
    145  1.1  christos  * Consume len bytes from the start of buf
    146  1.1  christos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
    147  1.1  christos  */
    148  1.1  christos int	sshbuf_consume(struct sshbuf *buf, size_t len);
    149  1.1  christos 
    150  1.1  christos /*
    151  1.1  christos  * Consume len bytes from the end of buf
    152  1.1  christos  * Returns 0 on success, or a negative SSH_ERR_* error code on failure.
    153  1.1  christos  */
    154  1.1  christos int	sshbuf_consume_end(struct sshbuf *buf, size_t len);
    155  1.1  christos 
    156  1.1  christos /* Extract or deposit some bytes */
    157  1.1  christos int	sshbuf_get(struct sshbuf *buf, void *v, size_t len);
    158  1.1  christos int	sshbuf_put(struct sshbuf *buf, const void *v, size_t len);
    159  1.1  christos int	sshbuf_putb(struct sshbuf *buf, const struct sshbuf *v);
    160  1.1  christos 
    161  1.1  christos /* Append using a printf(3) format */
    162  1.1  christos int	sshbuf_putf(struct sshbuf *buf, const char *fmt, ...)
    163  1.1  christos 	    __attribute__((format(printf, 2, 3)));
    164  1.3     joerg int	sshbuf_putfv(struct sshbuf *buf, const char *fmt, va_list ap)
    165  1.3     joerg 	    __printflike(2, 0);
    166  1.1  christos 
    167  1.1  christos /* Functions to extract or store big-endian words of various sizes */
    168  1.1  christos int	sshbuf_get_u64(struct sshbuf *buf, u_int64_t *valp);
    169  1.1  christos int	sshbuf_get_u32(struct sshbuf *buf, u_int32_t *valp);
    170  1.1  christos int	sshbuf_get_u16(struct sshbuf *buf, u_int16_t *valp);
    171  1.1  christos int	sshbuf_get_u8(struct sshbuf *buf, u_char *valp);
    172  1.1  christos int	sshbuf_put_u64(struct sshbuf *buf, u_int64_t val);
    173  1.1  christos int	sshbuf_put_u32(struct sshbuf *buf, u_int32_t val);
    174  1.1  christos int	sshbuf_put_u16(struct sshbuf *buf, u_int16_t val);
    175  1.1  christos int	sshbuf_put_u8(struct sshbuf *buf, u_char val);
    176  1.1  christos 
    177  1.1  christos /*
    178  1.1  christos  * Functions to extract or store SSH wire encoded strings (u32 len || data)
    179  1.1  christos  * The "cstring" variants admit no \0 characters in the string contents.
    180  1.1  christos  * Caller must free *valp.
    181  1.1  christos  */
    182  1.1  christos int	sshbuf_get_string(struct sshbuf *buf, u_char **valp, size_t *lenp);
    183  1.1  christos int	sshbuf_get_cstring(struct sshbuf *buf, char **valp, size_t *lenp);
    184  1.1  christos int	sshbuf_get_stringb(struct sshbuf *buf, struct sshbuf *v);
    185  1.1  christos int	sshbuf_put_string(struct sshbuf *buf, const void *v, size_t len);
    186  1.1  christos int	sshbuf_put_cstring(struct sshbuf *buf, const char *v);
    187  1.1  christos int	sshbuf_put_stringb(struct sshbuf *buf, const struct sshbuf *v);
    188  1.1  christos 
    189  1.1  christos /*
    190  1.1  christos  * "Direct" variant of sshbuf_get_string, returns pointer into the sshbuf to
    191  1.1  christos  * avoid an malloc+memcpy. The pointer is guaranteed to be valid until the
    192  1.1  christos  * next sshbuf-modifying function call. Caller does not free.
    193  1.1  christos  */
    194  1.1  christos int	sshbuf_get_string_direct(struct sshbuf *buf, const u_char **valp,
    195  1.1  christos 	    size_t *lenp);
    196  1.1  christos 
    197  1.1  christos /* Skip past a string */
    198  1.1  christos #define sshbuf_skip_string(buf) sshbuf_get_string_direct(buf, NULL, NULL)
    199  1.1  christos 
    200  1.1  christos /* Another variant: "peeks" into the buffer without modifying it */
    201  1.1  christos int	sshbuf_peek_string_direct(const struct sshbuf *buf, const u_char **valp,
    202  1.1  christos 	    size_t *lenp);
    203  1.1  christos 
    204  1.1  christos /*
    205  1.1  christos  * Functions to extract or store SSH wire encoded bignums and elliptic
    206  1.1  christos  * curve points.
    207  1.1  christos  */
    208  1.1  christos int	sshbuf_get_bignum2(struct sshbuf *buf, BIGNUM *v);
    209  1.1  christos int	sshbuf_get_bignum1(struct sshbuf *buf, BIGNUM *v);
    210  1.4  christos int	sshbuf_get_bignum2_bytes_direct(struct sshbuf *buf,
    211  1.4  christos 	    const u_char **valp, size_t *lenp);
    212  1.1  christos int	sshbuf_put_bignum2(struct sshbuf *buf, const BIGNUM *v);
    213  1.1  christos int	sshbuf_put_bignum1(struct sshbuf *buf, const BIGNUM *v);
    214  1.1  christos int	sshbuf_put_bignum2_bytes(struct sshbuf *buf, const void *v, size_t len);
    215  1.1  christos int	sshbuf_get_ec(struct sshbuf *buf, EC_POINT *v, const EC_GROUP *g);
    216  1.1  christos int	sshbuf_get_eckey(struct sshbuf *buf, EC_KEY *v);
    217  1.1  christos int	sshbuf_put_ec(struct sshbuf *buf, const EC_POINT *v, const EC_GROUP *g);
    218  1.1  christos int	sshbuf_put_eckey(struct sshbuf *buf, const EC_KEY *v);
    219  1.1  christos 
    220  1.1  christos /* Dump the contents of the buffer in a human-readable format */
    221  1.1  christos void	sshbuf_dump(struct sshbuf *buf, FILE *f);
    222  1.1  christos 
    223  1.1  christos /* Dump specified memory in a human-readable format */
    224  1.1  christos void	sshbuf_dump_data(const void *s, size_t len, FILE *f);
    225  1.1  christos 
    226  1.1  christos /* Return the hexadecimal representation of the contents of the buffer */
    227  1.1  christos char	*sshbuf_dtob16(struct sshbuf *buf);
    228  1.1  christos 
    229  1.1  christos /* Encode the contents of the buffer as base64 */
    230  1.1  christos char	*sshbuf_dtob64(struct sshbuf *buf);
    231  1.1  christos 
    232  1.1  christos /* Decode base64 data and append it to the buffer */
    233  1.1  christos int	sshbuf_b64tod(struct sshbuf *buf, const char *b64);
    234  1.1  christos 
    235  1.1  christos /* Macros for decoding/encoding integers */
    236  1.1  christos #define PEEK_U64(p) \
    237  1.2  christos 	(((u_int64_t)(((const u_char *)(p))[0]) << 56) | \
    238  1.2  christos 	 ((u_int64_t)(((const u_char *)(p))[1]) << 48) | \
    239  1.2  christos 	 ((u_int64_t)(((const u_char *)(p))[2]) << 40) | \
    240  1.2  christos 	 ((u_int64_t)(((const u_char *)(p))[3]) << 32) | \
    241  1.2  christos 	 ((u_int64_t)(((const u_char *)(p))[4]) << 24) | \
    242  1.2  christos 	 ((u_int64_t)(((const u_char *)(p))[5]) << 16) | \
    243  1.2  christos 	 ((u_int64_t)(((const u_char *)(p))[6]) << 8) | \
    244  1.2  christos 	  (u_int64_t)(((const u_char *)(p))[7]))
    245  1.1  christos #define PEEK_U32(p) \
    246  1.2  christos 	(((u_int32_t)(((const u_char *)(p))[0]) << 24) | \
    247  1.2  christos 	 ((u_int32_t)(((const u_char *)(p))[1]) << 16) | \
    248  1.2  christos 	 ((u_int32_t)(((const u_char *)(p))[2]) << 8) | \
    249  1.2  christos 	  (u_int32_t)(((const u_char *)(p))[3]))
    250  1.1  christos #define PEEK_U16(p) \
    251  1.2  christos 	(((u_int16_t)(((const u_char *)(p))[0]) << 8) | \
    252  1.2  christos 	  (u_int16_t)(((const u_char *)(p))[1]))
    253  1.1  christos 
    254  1.1  christos #define POKE_U64(p, v) \
    255  1.1  christos 	do { \
    256  1.5  christos 		const u_int64_t __v = (v); \
    257  1.5  christos 		((u_char *)(p))[0] = (__v >> 56) & 0xff; \
    258  1.5  christos 		((u_char *)(p))[1] = (__v >> 48) & 0xff; \
    259  1.5  christos 		((u_char *)(p))[2] = (__v >> 40) & 0xff; \
    260  1.5  christos 		((u_char *)(p))[3] = (__v >> 32) & 0xff; \
    261  1.5  christos 		((u_char *)(p))[4] = (__v >> 24) & 0xff; \
    262  1.5  christos 		((u_char *)(p))[5] = (__v >> 16) & 0xff; \
    263  1.5  christos 		((u_char *)(p))[6] = (__v >> 8) & 0xff; \
    264  1.5  christos 		((u_char *)(p))[7] = __v & 0xff; \
    265  1.1  christos 	} while (0)
    266  1.1  christos #define POKE_U32(p, v) \
    267  1.1  christos 	do { \
    268  1.5  christos 		const u_int32_t __v = (v); \
    269  1.5  christos 		((u_char *)(p))[0] = (__v >> 24) & 0xff; \
    270  1.5  christos 		((u_char *)(p))[1] = (__v >> 16) & 0xff; \
    271  1.5  christos 		((u_char *)(p))[2] = (__v >> 8) & 0xff; \
    272  1.5  christos 		((u_char *)(p))[3] = __v & 0xff; \
    273  1.1  christos 	} while (0)
    274  1.1  christos #define POKE_U16(p, v) \
    275  1.1  christos 	do { \
    276  1.5  christos 		const u_int16_t __v = (v); \
    277  1.5  christos 		((u_char *)(p))[0] = (__v >> 8) & 0xff; \
    278  1.5  christos 		((u_char *)(p))[1] = __v & 0xff; \
    279  1.1  christos 	} while (0)
    280  1.1  christos 
    281  1.1  christos /* Internal definitions follow. Exposed for regress tests */
    282  1.1  christos #ifdef SSHBUF_INTERNAL
    283  1.1  christos 
    284  1.1  christos /*
    285  1.1  christos  * Return the allocation size of buf
    286  1.1  christos  */
    287  1.1  christos size_t	sshbuf_alloc(const struct sshbuf *buf);
    288  1.1  christos 
    289  1.1  christos /*
    290  1.1  christos  * Increment the reference count of buf.
    291  1.1  christos  */
    292  1.1  christos int	sshbuf_set_parent(struct sshbuf *child, struct sshbuf *parent);
    293  1.1  christos 
    294  1.1  christos /*
    295  1.1  christos  * Return the parent buffer of buf, or NULL if it has no parent.
    296  1.1  christos  */
    297  1.1  christos const struct sshbuf *sshbuf_parent(const struct sshbuf *buf);
    298  1.1  christos 
    299  1.1  christos /*
    300  1.1  christos  * Return the reference count of buf
    301  1.1  christos  */
    302  1.1  christos u_int	sshbuf_refcount(const struct sshbuf *buf);
    303  1.1  christos 
    304  1.1  christos # define SSHBUF_SIZE_INIT	256		/* Initial allocation */
    305  1.1  christos # define SSHBUF_SIZE_INC	256		/* Preferred increment length */
    306  1.1  christos # define SSHBUF_PACK_MIN	8192		/* Minimim packable offset */
    307  1.1  christos 
    308  1.1  christos /* # define SSHBUF_ABORT abort */
    309  1.1  christos /* # define SSHBUF_DEBUG */
    310  1.1  christos 
    311  1.1  christos # ifndef SSHBUF_ABORT
    312  1.1  christos #  define SSHBUF_ABORT()
    313  1.1  christos # endif
    314  1.1  christos 
    315  1.1  christos # ifdef SSHBUF_DEBUG
    316  1.1  christos #  define SSHBUF_TELL(what) do { \
    317  1.1  christos 		printf("%s:%d %s: %s size %zu alloc %zu off %zu max %zu\n", \
    318  1.1  christos 		    __FILE__, __LINE__, __func__, what, \
    319  1.1  christos 		    buf->size, buf->alloc, buf->off, buf->max_size); \
    320  1.1  christos 		fflush(stdout); \
    321  1.1  christos 	} while (0)
    322  1.1  christos #  define SSHBUF_DBG(x) do { \
    323  1.1  christos 		printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \
    324  1.1  christos 		printf x; \
    325  1.1  christos 		printf("\n"); \
    326  1.1  christos 		fflush(stdout); \
    327  1.1  christos 	} while (0)
    328  1.1  christos # else
    329  1.1  christos #  define SSHBUF_TELL(what)
    330  1.1  christos #  define SSHBUF_DBG(x)
    331  1.1  christos # endif
    332  1.1  christos #endif /* SSHBUF_INTERNAL */
    333  1.1  christos 
    334  1.1  christos #endif /* _SSHBUF_H */
    335