1*beea8b97Schristos /* $NetBSD: buffer.c,v 1.2 2011/02/12 23:21:32 christos Exp $ */
219c14409Schristos
319c14409Schristos /* Copyright (c) 2010 The NetBSD Foundation, Inc.
419c14409Schristos * All rights reserved.
519c14409Schristos *
619c14409Schristos * Redistribution and use in source and binary forms, with or without
719c14409Schristos * modification, are permitted provided that the following conditions
819c14409Schristos * are met:
919c14409Schristos * 1. Redistributions of source code must retain the above copyright
1019c14409Schristos * notice, this list of conditions and the following disclaimer.
1119c14409Schristos * 2. Redistributions in binary form must reproduce the above copyright
1219c14409Schristos * notice, this list of conditions and the following disclaimer in the
1319c14409Schristos * documentation and/or other materials provided with the distribution.
1419c14409Schristos * 3. All advertising materials mentioning features or use of this software
1519c14409Schristos * must display the following acknowledgement:
1619c14409Schristos * This product includes software developed by the NetBSD
1719c14409Schristos * Foundation, Inc. and its contributors.
1819c14409Schristos * 4. Neither the name of The NetBSD Foundation nor the names of its
1919c14409Schristos * contributors may be used to endorse or promote products derived
2019c14409Schristos * from this software without specific prior written permission.
2119c14409Schristos *
2219c14409Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2319c14409Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2419c14409Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2519c14409Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2619c14409Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2719c14409Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2819c14409Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2919c14409Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3019c14409Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3119c14409Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3219c14409Schristos * POSSIBILITY OF SUCH DAMAGE.
3319c14409Schristos */
3419c14409Schristos #include <sys/cdefs.h>
35*beea8b97Schristos __RCSID("$NetBSD: buffer.c,v 1.2 2011/02/12 23:21:32 christos Exp $");
3619c14409Schristos
3719c14409Schristos #include <sys/param.h> /* for MIN() */
38*beea8b97Schristos
3919c14409Schristos #include <assert.h>
4019c14409Schristos #include <saslc.h>
4119c14409Schristos #include <stdio.h>
42*beea8b97Schristos #include <stdlib.h>
4319c14409Schristos #include <string.h>
4419c14409Schristos
4519c14409Schristos #include "buffer.h"
4619c14409Schristos #include "error.h"
4719c14409Schristos #include "saslc_private.h"
4819c14409Schristos
4919c14409Schristos /*
5019c14409Schristos * XXX: Should we rename saslc__buffer_* and saslc__buffer32_* to
5119c14409Schristos * something reflecting their encode and decode, resp, context?
5219c14409Schristos */
5319c14409Schristos
5419c14409Schristos /**
5519c14409Schristos * encode buffer context
5619c14409Schristos */
5719c14409Schristos struct saslc__buffer_context_t {
5819c14409Schristos saslc_sess_t *sess; /* session pointer (for error messages) */
5919c14409Schristos size_t maxbuf; /* allocated length of payload buffer (maxbuf) */
6019c14409Schristos size_t bufneed; /* bytes needed in payload buffer */
6119c14409Schristos
6219c14409Schristos /* XXX: must be at end */
6319c14409Schristos uint8_t buf[1]; /* payload buffer */
6419c14409Schristos };
6519c14409Schristos
6619c14409Schristos /**
6719c14409Schristos * decode buffer context
6819c14409Schristos *
6919c14409Schristos * the actual packet looks like:
7019c14409Schristos *
7119c14409Schristos * struct {
7219c14409Schristos * uint8_t size[4]; // length of packet following this (big endian order)
7319c14409Schristos * uint8_t payload[]; // variable length payload area
7419c14409Schristos * struct {
7519c14409Schristos * uint8_t mac_0_9[10]; // truncated MD5_HMAC hash of size and payload
7619c14409Schristos * uint8_t version[2]; // always 1 (big endian order)
7719c14409Schristos * uint8_t seqnum[4]; // sequence number (big endian order)
7819c14409Schristos * } mac __packed;
7919c14409Schristos * } __packed
8019c14409Schristos */
8119c14409Schristos struct saslc__buffer32_context_t {
8219c14409Schristos saslc_sess_t *sess; /* session pointer (for error messages) */
8319c14409Schristos size_t szneed; /* bytes needed in size buffer */
8419c14409Schristos size_t bufsize; /* size of payload buffer */
8519c14409Schristos size_t maxbuf; /* allocated length of payload buffer */
8619c14409Schristos size_t bufneed; /* bytes needed in payload buffer */
8719c14409Schristos
8819c14409Schristos /* XXX: these must be sequential and at the end! */
8919c14409Schristos uint8_t szbuf[4]; /* size buffer */
9019c14409Schristos uint8_t buf[1]; /* payload buffer */
9119c14409Schristos } __packed;
9219c14409Schristos
9319c14409Schristos /****************************************
9419c14409Schristos * saslc__buffer_* routines.
9519c14409Schristos * For fetching unencoded data.
9619c14409Schristos */
9719c14409Schristos
9819c14409Schristos /**
9919c14409Schristos * @brief destroy a buffer context
10019c14409Schristos * @param ctx context to destroy
10119c14409Schristos * @return nothing
10219c14409Schristos */
10319c14409Schristos void
saslc__buffer_destroy(saslc__buffer_context_t * ctx)10419c14409Schristos saslc__buffer_destroy(saslc__buffer_context_t *ctx)
10519c14409Schristos {
10619c14409Schristos
10719c14409Schristos free(ctx);
10819c14409Schristos }
10919c14409Schristos
11019c14409Schristos /**
11119c14409Schristos * @brief create a buffer context
11219c14409Schristos * @param sess saslc session
11319c14409Schristos * @param maxbuf maximum buffer size
11419c14409Schristos * @return buffer context
11519c14409Schristos */
11619c14409Schristos saslc__buffer_context_t *
saslc__buffer_create(saslc_sess_t * sess,size_t maxbuf)11719c14409Schristos saslc__buffer_create(saslc_sess_t *sess, size_t maxbuf)
11819c14409Schristos {
11919c14409Schristos saslc__buffer_context_t *ctx;
12019c14409Schristos size_t buflen;
12119c14409Schristos
12219c14409Schristos buflen = sizeof(*ctx) - sizeof(ctx->buf) + maxbuf;
12319c14409Schristos ctx = malloc(buflen);
12419c14409Schristos if (ctx == NULL) {
12519c14409Schristos saslc__error_set_errno(ERR(sess), ERROR_NOMEM);
12619c14409Schristos return NULL;
12719c14409Schristos }
12819c14409Schristos memset(ctx, 0, sizeof(*ctx) - sizeof(ctx->buf));
12919c14409Schristos
13019c14409Schristos ctx->maxbuf = maxbuf;
13119c14409Schristos ctx->bufneed = ctx->maxbuf;
13219c14409Schristos ctx->sess = sess;
13319c14409Schristos return ctx;
13419c14409Schristos }
13519c14409Schristos
13619c14409Schristos /**
13719c14409Schristos * @brief fetch a block of data from the input stream.
13819c14409Schristos * @param ctx context
13919c14409Schristos * @param in input buffer
14019c14409Schristos * @param inlen input buffer length
14119c14409Schristos * @param out pointer to output buffer
14219c14409Schristos * @param outlen pointer to output buffer length
14319c14409Schristos * @return number of bytes consumed by the current call, or -1 on
14419c14409Schristos * failure.
14519c14409Schristos *
14619c14409Schristos * NOTE: Output is buffered, so if the return is success and outlen is
14719c14409Schristos * zero, then more data is needed to fill the packet. The internal
14819c14409Schristos * buffer can be flushed by calling with inlen = 0.
14919c14409Schristos */
15019c14409Schristos ssize_t
saslc__buffer_fetch(saslc__buffer_context_t * ctx,const uint8_t * in,size_t inlen,uint8_t ** out,size_t * outlen)15119c14409Schristos saslc__buffer_fetch(saslc__buffer_context_t *ctx, const uint8_t *in,
15219c14409Schristos size_t inlen, uint8_t **out, size_t *outlen)
15319c14409Schristos {
15419c14409Schristos uint8_t *p;
15519c14409Schristos size_t len;
15619c14409Schristos
15719c14409Schristos if (inlen == 0) { /* flush internal buffer */
15819c14409Schristos *outlen = ctx->maxbuf - ctx->bufneed;
15919c14409Schristos *out = *outlen != 0 ? ctx->buf : NULL;
16019c14409Schristos ctx->bufneed = ctx->maxbuf; /* for next call */
16119c14409Schristos return 0;
16219c14409Schristos }
16319c14409Schristos
16419c14409Schristos len = 0;
16519c14409Schristos if (ctx->bufneed > 0) {
16619c14409Schristos p = ctx->buf + ctx->maxbuf - ctx->bufneed;
16719c14409Schristos len = MIN(inlen, ctx->bufneed);
16819c14409Schristos memcpy(p, in, len);
16919c14409Schristos ctx->bufneed -= len;
17019c14409Schristos if (ctx->bufneed > 0) {
17119c14409Schristos *out = NULL;
17219c14409Schristos *outlen = 0;
17319c14409Schristos return len;
17419c14409Schristos }
17519c14409Schristos *out = ctx->buf;
17619c14409Schristos *outlen = ctx->maxbuf;
17719c14409Schristos ctx->bufneed = ctx->maxbuf; /* for next call */
17819c14409Schristos return len;
17919c14409Schristos }
18019c14409Schristos assert(/*CONSTCOND*/0); /* should not happen! */
18119c14409Schristos saslc__error_set(ERR(ctx->sess), ERROR_MECH, "buffer coding error");
18219c14409Schristos *out = NULL;
18319c14409Schristos *outlen = 0;
18419c14409Schristos ctx->bufneed = ctx->maxbuf; /* for next call */
18519c14409Schristos return -1;
18619c14409Schristos }
18719c14409Schristos
18819c14409Schristos /****************************************
18919c14409Schristos * saslc__buffer32_* routines.
19019c14409Schristos * For fetching an encoded packet.
19119c14409Schristos * The packet is of the form:
19219c14409Schristos * struct {
19319c14409Schristos * uint8_t size[4]; // bytes in payload
19419c14409Schristos * uint8_t payload[]; // packet payload (including any trailing HMAC)
19519c14409Schristos * } __packed;
19619c14409Schristos */
19719c14409Schristos
19819c14409Schristos /**
19919c14409Schristos * @brief destroy a buffer32 context
20019c14409Schristos * @param ctx context to destroy
20119c14409Schristos * @return nothing
20219c14409Schristos */
20319c14409Schristos void
saslc__buffer32_destroy(saslc__buffer32_context_t * ctx)20419c14409Schristos saslc__buffer32_destroy(saslc__buffer32_context_t *ctx)
20519c14409Schristos {
20619c14409Schristos
20719c14409Schristos free(ctx);
20819c14409Schristos }
20919c14409Schristos
21019c14409Schristos /**
21119c14409Schristos * @brief create a buffer32 context
21219c14409Schristos * @param sess saslc session
21319c14409Schristos * @param maxbuf maximum buffer size
21419c14409Schristos * @return buffer context
21519c14409Schristos */
21619c14409Schristos saslc__buffer32_context_t *
saslc__buffer32_create(saslc_sess_t * sess,size_t maxbuf)21719c14409Schristos saslc__buffer32_create(saslc_sess_t *sess, size_t maxbuf)
21819c14409Schristos {
21919c14409Schristos saslc__buffer32_context_t *ctx;
22019c14409Schristos size_t buflen;
22119c14409Schristos
22219c14409Schristos buflen = sizeof(*ctx) - sizeof(ctx->buf) + maxbuf;
22319c14409Schristos ctx = malloc(buflen);
22419c14409Schristos if (ctx == NULL) {
22519c14409Schristos saslc__error_set_errno(ERR(sess), ERROR_NOMEM);
22619c14409Schristos return NULL;
22719c14409Schristos }
22819c14409Schristos memset(ctx, 0, sizeof(*ctx) - sizeof(ctx->buf));
22919c14409Schristos
23019c14409Schristos ctx->maxbuf = maxbuf;
23119c14409Schristos ctx->szneed = sizeof(ctx->szbuf);
23219c14409Schristos ctx->sess = sess;
23319c14409Schristos return ctx;
23419c14409Schristos }
23519c14409Schristos
23619c14409Schristos /**
23719c14409Schristos * @brief fetch a block of data from the input stream. The block is
23819c14409Schristos * prefixed in the stream by a 4 byte length field (in network byte
23919c14409Schristos * order).
24019c14409Schristos * @param ctx context
24119c14409Schristos * @param in input buffer
24219c14409Schristos * @param inlen input buffer length
24319c14409Schristos * @param out pointer to output buffer
24419c14409Schristos * @param outlen pointer to output buffer length
24519c14409Schristos * @return number of bytes consumed by the current call on success, 0
24619c14409Schristos * if more data is needed, or -1 on failure.
24719c14409Schristos */
24819c14409Schristos ssize_t
saslc__buffer32_fetch(saslc__buffer32_context_t * ctx,const uint8_t * in,size_t inlen,uint8_t ** out,size_t * outlen)24919c14409Schristos saslc__buffer32_fetch(saslc__buffer32_context_t *ctx, const uint8_t *in,
25019c14409Schristos size_t inlen, uint8_t **out, size_t *outlen)
25119c14409Schristos {
25219c14409Schristos uint8_t *p;
25319c14409Schristos size_t ate, len;
25419c14409Schristos
25519c14409Schristos if (inlen == 0) { /* we cannot flush the decode buffer */
25619c14409Schristos saslc__error_set(ERR(ctx->sess), ERROR_BADARG,
25719c14409Schristos "bad inlen: cannot flush decode buffer");
25819c14409Schristos return -1;
25919c14409Schristos }
26019c14409Schristos ate = 0;
26119c14409Schristos if (ctx->szneed) {
26219c14409Schristos p = ctx->szbuf + sizeof(ctx->szbuf) - ctx->szneed;
26319c14409Schristos len = MIN(inlen, ctx->szneed);
26419c14409Schristos memcpy(p, in, len);
26519c14409Schristos ctx->szneed -= len;
26619c14409Schristos ate += len;
26719c14409Schristos if (ctx->szneed > 0)
26819c14409Schristos goto need_more;
26919c14409Schristos
27019c14409Schristos ctx->bufsize = be32dec(ctx->szbuf);
27119c14409Schristos if (ctx->bufsize == 0) {
27219c14409Schristos saslc__error_set(ERR(ctx->sess), ERROR_MECH,
27319c14409Schristos "pack with no payload");
27419c14409Schristos return -1;
27519c14409Schristos }
27619c14409Schristos if (ctx->bufsize > ctx->maxbuf) {
27719c14409Schristos saslc__error_set(ERR(ctx->sess), ERROR_MECH,
27819c14409Schristos "payload longer than maxbuf");
27919c14409Schristos return -1;
28019c14409Schristos }
28119c14409Schristos in += len;
28219c14409Schristos inlen -= len;
28319c14409Schristos ctx->bufneed = ctx->bufsize;
28419c14409Schristos }
28519c14409Schristos if (ctx->bufneed) {
28619c14409Schristos p = ctx->buf + ctx->bufsize - ctx->bufneed;
28719c14409Schristos len = MIN(inlen, ctx->bufneed);
28819c14409Schristos memcpy(p, in, len);
28919c14409Schristos ctx->bufneed -= len;
29019c14409Schristos ate += len;
29119c14409Schristos if (ctx->bufneed > 0)
29219c14409Schristos goto need_more;
29319c14409Schristos }
29419c14409Schristos ctx->szneed = sizeof(ctx->szbuf); /* for next call */
29519c14409Schristos *out = ctx->szbuf;
29619c14409Schristos *outlen = sizeof(ctx->szbuf) + ctx->bufsize;
29719c14409Schristos return ate;
29819c14409Schristos need_more:
29919c14409Schristos *out = NULL;
30019c14409Schristos *outlen = 0;
30119c14409Schristos return ate;
30219c14409Schristos }
303