freebsd-skq/crypto/openssh/bufaux.c

260 lines
5.1 KiB
C
Raw Normal View History

2015-01-05 16:09:55 +00:00
/* $OpenBSD: bufaux.c,v 1.60 2014/04/30 05:29:56 djm Exp $ */
/*
2015-01-05 16:09:55 +00:00
* Copyright (c) 2012 Damien Miller <djm@mindrot.org>
*
2015-01-05 16:09:55 +00: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.
*
2015-01-05 16:09:55 +00: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.
*/
2015-01-05 16:09:55 +00:00
/* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */
#include "includes.h"
#include <sys/types.h>
#include "buffer.h"
#include "log.h"
2015-01-05 16:09:55 +00:00
#include "ssherr.h"
int
2015-01-05 16:09:55 +00:00
buffer_get_short_ret(u_short *v, Buffer *buffer)
{
2015-01-05 16:09:55 +00:00
int ret;
2015-01-05 16:09:55 +00:00
if ((ret = sshbuf_get_u16(buffer, v)) != 0) {
error("%s: %s", __func__, ssh_err(ret));
return -1;
}
return 0;
}
u_short
buffer_get_short(Buffer *buffer)
{
u_short ret;
if (buffer_get_short_ret(&ret, buffer) == -1)
2015-01-05 16:09:55 +00:00
fatal("%s: buffer error", __func__);
return (ret);
}
int
2015-01-05 16:09:55 +00:00
buffer_get_int_ret(u_int *v, Buffer *buffer)
{
2015-01-05 16:09:55 +00:00
int ret;
2015-01-05 16:09:55 +00:00
if ((ret = sshbuf_get_u32(buffer, v)) != 0) {
error("%s: %s", __func__, ssh_err(ret));
return -1;
}
return 0;
}
u_int
buffer_get_int(Buffer *buffer)
{
u_int ret;
if (buffer_get_int_ret(&ret, buffer) == -1)
2015-01-05 16:09:55 +00:00
fatal("%s: buffer error", __func__);
return (ret);
}
int
2015-01-05 16:09:55 +00:00
buffer_get_int64_ret(u_int64_t *v, Buffer *buffer)
{
2015-01-05 16:09:55 +00:00
int ret;
2015-01-05 16:09:55 +00:00
if ((ret = sshbuf_get_u64(buffer, v)) != 0) {
error("%s: %s", __func__, ssh_err(ret));
return -1;
}
return 0;
}
u_int64_t
buffer_get_int64(Buffer *buffer)
{
u_int64_t ret;
if (buffer_get_int64_ret(&ret, buffer) == -1)
2015-01-05 16:09:55 +00:00
fatal("%s: buffer error", __func__);
return (ret);
}
void
buffer_put_short(Buffer *buffer, u_short value)
{
2015-01-05 16:09:55 +00:00
int ret;
2015-01-05 16:09:55 +00:00
if ((ret = sshbuf_put_u16(buffer, value)) != 0)
fatal("%s: %s", __func__, ssh_err(ret));
}
void
buffer_put_int(Buffer *buffer, u_int value)
{
2015-01-05 16:09:55 +00:00
int ret;
2015-01-05 16:09:55 +00:00
if ((ret = sshbuf_put_u32(buffer, value)) != 0)
fatal("%s: %s", __func__, ssh_err(ret));
}
void
buffer_put_int64(Buffer *buffer, u_int64_t value)
{
2015-01-05 16:09:55 +00:00
int ret;
2015-01-05 16:09:55 +00:00
if ((ret = sshbuf_put_u64(buffer, value)) != 0)
fatal("%s: %s", __func__, ssh_err(ret));
}
void *
buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
{
2015-01-05 16:09:55 +00:00
size_t len;
int ret;
u_char *value;
2015-01-05 16:09:55 +00:00
if ((ret = sshbuf_get_string(buffer, &value, &len)) != 0) {
error("%s: %s", __func__, ssh_err(ret));
return NULL;
}
2015-01-05 16:09:55 +00:00
if (length_ptr != NULL)
*length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
return value;
}
void *
buffer_get_string(Buffer *buffer, u_int *length_ptr)
{
void *ret;
if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
2015-01-05 16:09:55 +00:00
fatal("%s: buffer error", __func__);
return (ret);
}
2011-02-17 11:47:40 +00:00
char *
buffer_get_cstring_ret(Buffer *buffer, u_int *length_ptr)
{
2015-01-05 16:09:55 +00:00
size_t len;
int ret;
char *value;
2011-02-17 11:47:40 +00:00
2015-01-05 16:09:55 +00:00
if ((ret = sshbuf_get_cstring(buffer, &value, &len)) != 0) {
error("%s: %s", __func__, ssh_err(ret));
2011-02-17 11:47:40 +00:00
return NULL;
}
if (length_ptr != NULL)
2015-01-05 16:09:55 +00:00
*length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
return value;
2011-02-17 11:47:40 +00:00
}
char *
buffer_get_cstring(Buffer *buffer, u_int *length_ptr)
{
char *ret;
if ((ret = buffer_get_cstring_ret(buffer, length_ptr)) == NULL)
2015-01-05 16:09:55 +00:00
fatal("%s: buffer error", __func__);
2011-02-17 11:47:40 +00:00
return ret;
}
2015-01-05 16:09:55 +00:00
const void *
2010-03-08 11:19:52 +00:00
buffer_get_string_ptr_ret(Buffer *buffer, u_int *length_ptr)
2008-07-23 09:33:08 +00:00
{
2015-01-05 16:09:55 +00:00
size_t len;
int ret;
const u_char *value;
2008-07-23 09:33:08 +00:00
2015-01-05 16:09:55 +00:00
if ((ret = sshbuf_get_string_direct(buffer, &value, &len)) != 0) {
error("%s: %s", __func__, ssh_err(ret));
2010-03-08 11:19:52 +00:00
return NULL;
}
2015-01-05 16:09:55 +00:00
if (length_ptr != NULL)
*length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
return value;
2008-07-23 09:33:08 +00:00
}
2015-01-05 16:09:55 +00:00
const void *
2010-03-08 11:19:52 +00:00
buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr)
{
2015-01-05 16:09:55 +00:00
const void *ret;
2010-03-08 11:19:52 +00:00
if ((ret = buffer_get_string_ptr_ret(buffer, length_ptr)) == NULL)
2015-01-05 16:09:55 +00:00
fatal("%s: buffer error", __func__);
2010-03-08 11:19:52 +00:00
return (ret);
}
void
buffer_put_string(Buffer *buffer, const void *buf, u_int len)
{
2015-01-05 16:09:55 +00:00
int ret;
if ((ret = sshbuf_put_string(buffer, buf, len)) != 0)
fatal("%s: %s", __func__, ssh_err(ret));
}
2015-01-05 16:09:55 +00:00
void
buffer_put_cstring(Buffer *buffer, const char *s)
{
2015-01-05 16:09:55 +00:00
int ret;
if ((ret = sshbuf_put_cstring(buffer, s)) != 0)
fatal("%s: %s", __func__, ssh_err(ret));
}
int
2015-01-05 16:09:55 +00:00
buffer_get_char_ret(char *v, Buffer *buffer)
{
2015-01-05 16:09:55 +00:00
int ret;
if ((ret = sshbuf_get_u8(buffer, (u_char *)v)) != 0) {
error("%s: %s", __func__, ssh_err(ret));
return -1;
}
2015-01-05 16:09:55 +00:00
return 0;
}
int
buffer_get_char(Buffer *buffer)
{
2015-01-05 16:09:55 +00:00
char ch;
if (buffer_get_char_ret(&ch, buffer) == -1)
2015-01-05 16:09:55 +00:00
fatal("%s: buffer error", __func__);
return (u_char) ch;
}
void
buffer_put_char(Buffer *buffer, int value)
{
2015-01-05 16:09:55 +00:00
int ret;
2015-01-05 16:09:55 +00:00
if ((ret = sshbuf_put_u8(buffer, value)) != 0)
fatal("%s: %s", __func__, ssh_err(ret));
}
2014-01-30 10:56:49 +00:00
void
buffer_put_bignum2_from_string(Buffer *buffer, const u_char *s, u_int l)
{
2015-01-05 16:09:55 +00:00
int ret;
2014-01-30 10:56:49 +00:00
2015-01-05 16:09:55 +00:00
if ((ret = sshbuf_put_bignum2_bytes(buffer, s, l)) != 0)
fatal("%s: %s", __func__, ssh_err(ret));
}
2014-01-30 10:56:49 +00:00