2014-05-15 06:33:43 +02:00
|
|
|
/* $OpenBSD: bufaux.c,v 1.60 2014/04/30 05:29:56 djm Exp $ */
|
1999-10-27 05:42:43 +02:00
|
|
|
/*
|
2014-05-15 06:33:43 +02:00
|
|
|
* Copyright (c) 2012 Damien Miller <djm@mindrot.org>
|
1999-11-24 14:26:21 +01:00
|
|
|
*
|
2014-05-15 06:33:43 +02:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-09-16 04:29:08 +02:00
|
|
|
*
|
2014-05-15 06:33:43 +02:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1999-11-24 14:26:21 +01:00
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
/* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2014-06-11 05:39:24 +02:00
|
|
|
#include "includes.h"
|
|
|
|
|
2006-08-05 04:39:39 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "buffer.h"
|
2001-01-22 06:34:40 +01:00
|
|
|
#include "log.h"
|
2014-05-15 06:33:43 +02:00
|
|
|
#include "ssherr.h"
|
2002-04-23 12:42:36 +02:00
|
|
|
|
2004-11-05 10:41:24 +01:00
|
|
|
int
|
2014-05-15 06:33:43 +02:00
|
|
|
buffer_get_short_ret(u_short *v, Buffer *buffer)
|
2004-11-05 10:41:24 +01:00
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
int ret;
|
2004-11-05 10:41:24 +01:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
if ((ret = sshbuf_get_u16(buffer, v)) != 0) {
|
|
|
|
error("%s: %s", __func__, ssh_err(ret));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2004-11-05 10:41:24 +01:00
|
|
|
}
|
|
|
|
|
2002-04-23 12:42:36 +02:00
|
|
|
u_short
|
|
|
|
buffer_get_short(Buffer *buffer)
|
|
|
|
{
|
2004-11-05 10:41:24 +01:00
|
|
|
u_short ret;
|
|
|
|
|
|
|
|
if (buffer_get_short_ret(&ret, buffer) == -1)
|
2014-05-15 06:33:43 +02:00
|
|
|
fatal("%s: buffer error", __func__);
|
2002-06-23 23:29:23 +02:00
|
|
|
|
2004-11-05 10:41:24 +01:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-05-15 06:33:43 +02:00
|
|
|
buffer_get_int_ret(u_int *v, Buffer *buffer)
|
2004-11-05 10:41:24 +01:00
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
int ret;
|
2004-11-05 10:41:24 +01:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
if ((ret = sshbuf_get_u32(buffer, v)) != 0) {
|
|
|
|
error("%s: %s", __func__, ssh_err(ret));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2002-04-23 12:42:36 +02:00
|
|
|
}
|
|
|
|
|
2000-12-22 02:43:59 +01:00
|
|
|
u_int
|
1999-11-24 14:26:21 +01:00
|
|
|
buffer_get_int(Buffer *buffer)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2004-11-05 10:41:24 +01:00
|
|
|
u_int ret;
|
|
|
|
|
|
|
|
if (buffer_get_int_ret(&ret, buffer) == -1)
|
2014-05-15 06:33:43 +02:00
|
|
|
fatal("%s: buffer error", __func__);
|
2004-11-05 10:41:24 +01:00
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
2002-06-23 23:29:23 +02:00
|
|
|
|
2004-11-05 10:41:24 +01:00
|
|
|
int
|
2014-05-15 06:33:43 +02:00
|
|
|
buffer_get_int64_ret(u_int64_t *v, Buffer *buffer)
|
2004-11-05 10:41:24 +01:00
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
int ret;
|
2004-11-05 10:41:24 +01:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
if ((ret = sshbuf_get_u64(buffer, v)) != 0) {
|
|
|
|
error("%s: %s", __func__, ssh_err(ret));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 07:20:23 +01:00
|
|
|
u_int64_t
|
|
|
|
buffer_get_int64(Buffer *buffer)
|
|
|
|
{
|
2004-11-05 10:41:24 +01:00
|
|
|
u_int64_t ret;
|
2002-06-23 23:29:23 +02:00
|
|
|
|
2004-11-05 10:41:24 +01:00
|
|
|
if (buffer_get_int64_ret(&ret, buffer) == -1)
|
2014-05-15 06:33:43 +02:00
|
|
|
fatal("%s: buffer error", __func__);
|
2004-11-05 10:41:24 +01:00
|
|
|
|
|
|
|
return (ret);
|
2001-01-11 07:20:23 +01:00
|
|
|
}
|
|
|
|
|
2002-04-23 12:42:36 +02:00
|
|
|
void
|
|
|
|
buffer_put_short(Buffer *buffer, u_short value)
|
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
int ret;
|
2002-06-23 23:29:23 +02:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
if ((ret = sshbuf_put_u16(buffer, value)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(ret));
|
2002-04-23 12:42:36 +02:00
|
|
|
}
|
|
|
|
|
2000-04-16 04:31:48 +02:00
|
|
|
void
|
2000-12-22 02:43:59 +01:00
|
|
|
buffer_put_int(Buffer *buffer, u_int value)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
int ret;
|
2002-06-23 23:29:23 +02:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
if ((ret = sshbuf_put_u32(buffer, value)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(ret));
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 07:20:23 +01:00
|
|
|
void
|
|
|
|
buffer_put_int64(Buffer *buffer, u_int64_t value)
|
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
int ret;
|
2002-06-23 23:29:23 +02:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
if ((ret = sshbuf_put_u64(buffer, value)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(ret));
|
2001-01-11 07:20:23 +01:00
|
|
|
}
|
|
|
|
|
2001-12-21 04:56:54 +01:00
|
|
|
void *
|
2004-11-05 10:41:24 +01:00
|
|
|
buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
size_t len;
|
|
|
|
int ret;
|
2001-12-21 04:56:54 +01:00
|
|
|
u_char *value;
|
2002-06-23 23:29:23 +02:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
if ((ret = sshbuf_get_string(buffer, &value, &len)) != 0) {
|
|
|
|
error("%s: %s", __func__, ssh_err(ret));
|
|
|
|
return NULL;
|
2004-11-05 10:41:24 +01:00
|
|
|
}
|
2014-05-15 06:33:43 +02:00
|
|
|
if (length_ptr != NULL)
|
|
|
|
*length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
|
|
|
|
return value;
|
2004-11-05 10:41:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
buffer_get_string(Buffer *buffer, u_int *length_ptr)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
|
2014-05-15 06:33:43 +02:00
|
|
|
fatal("%s: buffer error", __func__);
|
2004-11-05 10:41:24 +01:00
|
|
|
return (ret);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2010-08-31 14:36:39 +02:00
|
|
|
char *
|
|
|
|
buffer_get_cstring_ret(Buffer *buffer, u_int *length_ptr)
|
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
size_t len;
|
|
|
|
int ret;
|
|
|
|
char *value;
|
2010-08-31 14:36:39 +02:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
if ((ret = sshbuf_get_cstring(buffer, &value, &len)) != 0) {
|
|
|
|
error("%s: %s", __func__, ssh_err(ret));
|
2010-08-31 14:36:39 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (length_ptr != NULL)
|
2014-05-15 06:33:43 +02:00
|
|
|
*length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
|
|
|
|
return value;
|
2010-08-31 14:36:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
buffer_get_cstring(Buffer *buffer, u_int *length_ptr)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
if ((ret = buffer_get_cstring_ret(buffer, length_ptr)) == NULL)
|
2014-05-15 06:33:43 +02:00
|
|
|
fatal("%s: buffer error", __func__);
|
2010-08-31 14:36:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-15 05:48:26 +02:00
|
|
|
const void *
|
2010-01-12 09:45:59 +01:00
|
|
|
buffer_get_string_ptr_ret(Buffer *buffer, u_int *length_ptr)
|
2008-05-19 06:59:37 +02:00
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
size_t len;
|
|
|
|
int ret;
|
|
|
|
const u_char *value;
|
2008-05-19 06:59:37 +02:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
if ((ret = sshbuf_get_string_direct(buffer, &value, &len)) != 0) {
|
|
|
|
error("%s: %s", __func__, ssh_err(ret));
|
2010-01-12 09:45:59 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-15 06:33:43 +02:00
|
|
|
if (length_ptr != NULL)
|
|
|
|
*length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
|
|
|
|
return value;
|
2008-05-19 06:59:37 +02:00
|
|
|
}
|
|
|
|
|
2014-05-15 05:48:26 +02:00
|
|
|
const void *
|
2010-01-12 09:45:59 +01:00
|
|
|
buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr)
|
|
|
|
{
|
2014-05-15 05:48:26 +02:00
|
|
|
const void *ret;
|
2010-01-12 09:45:59 +01:00
|
|
|
|
|
|
|
if ((ret = buffer_get_string_ptr_ret(buffer, length_ptr)) == NULL)
|
2014-05-15 06:33:43 +02:00
|
|
|
fatal("%s: buffer error", __func__);
|
2010-01-12 09:45:59 +01:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
2000-04-16 04:31:48 +02:00
|
|
|
void
|
2000-12-22 02:43:59 +01:00
|
|
|
buffer_put_string(Buffer *buffer, const void *buf, u_int len)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = sshbuf_put_string(buffer, buf, len)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(ret));
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
2014-05-15 06:33:43 +02:00
|
|
|
|
2000-04-16 04:31:48 +02:00
|
|
|
void
|
2000-04-01 03:09:21 +02:00
|
|
|
buffer_put_cstring(Buffer *buffer, const char *s)
|
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = sshbuf_put_cstring(buffer, s)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(ret));
|
2000-04-01 03:09:21 +02:00
|
|
|
}
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2004-11-05 10:41:24 +01:00
|
|
|
int
|
2014-05-15 06:33:43 +02:00
|
|
|
buffer_get_char_ret(char *v, Buffer *buffer)
|
2004-11-05 10:41:24 +01:00
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = sshbuf_get_u8(buffer, (u_char *)v)) != 0) {
|
|
|
|
error("%s: %s", __func__, ssh_err(ret));
|
|
|
|
return -1;
|
2004-11-05 10:41:24 +01:00
|
|
|
}
|
2014-05-15 06:33:43 +02:00
|
|
|
return 0;
|
2004-11-05 10:41:24 +01:00
|
|
|
}
|
|
|
|
|
2000-04-16 04:31:48 +02:00
|
|
|
int
|
1999-11-24 14:26:21 +01:00
|
|
|
buffer_get_char(Buffer *buffer)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
char ch;
|
2002-06-23 23:29:23 +02:00
|
|
|
|
2004-11-05 10:41:24 +01:00
|
|
|
if (buffer_get_char_ret(&ch, buffer) == -1)
|
2014-05-15 06:33:43 +02:00
|
|
|
fatal("%s: buffer error", __func__);
|
|
|
|
return (u_char) ch;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2000-04-16 04:31:48 +02:00
|
|
|
void
|
1999-11-24 14:26:21 +01:00
|
|
|
buffer_put_char(Buffer *buffer, int value)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
int ret;
|
2014-01-12 09:21:22 +01:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
if ((ret = sshbuf_put_u8(buffer, value)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(ret));
|
2014-01-12 09:21:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
buffer_put_bignum2_from_string(Buffer *buffer, const u_char *s, u_int l)
|
|
|
|
{
|
2014-05-15 06:33:43 +02:00
|
|
|
int ret;
|
2014-01-12 09:21:22 +01:00
|
|
|
|
2014-05-15 06:33:43 +02:00
|
|
|
if ((ret = sshbuf_put_bignum2_bytes(buffer, s, l)) != 0)
|
|
|
|
fatal("%s: %s", __func__, ssh_err(ret));
|
|
|
|
}
|
2014-01-12 09:21:22 +01:00
|
|
|
|