blob: 3976896a99e1cd0cbc73d5bb22749ade971e0069 [file] [log] [blame]
Damien Miller05e82c32014-05-15 14:33:43 +10001/* $OpenBSD: bufaux.c,v 1.60 2014/04/30 05:29:56 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller05e82c32014-05-15 14:33:43 +10003 * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
Damien Miller95def091999-11-25 00:26:21 +11004 *
Damien Miller05e82c32014-05-15 14:33:43 +10005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Millerb38eff82000-04-01 11:09:21 +10008 *
Damien Miller05e82c32014-05-15 14:33:43 +10009 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Damien Miller95def091999-11-25 00:26:21 +110016 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
Damien Miller05e82c32014-05-15 14:33:43 +100018/* Emulation wrappers for legacy OpenSSH buffer API atop sshbuf */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
Darren Tucker58538d72014-06-11 13:39:24 +100020#include "includes.h"
21
Damien Millerd7834352006-08-05 12:39:39 +100022#include <sys/types.h>
23
Damien Millerd7834352006-08-05 12:39:39 +100024#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000025#include "log.h"
Damien Miller05e82c32014-05-15 14:33:43 +100026#include "ssherr.h"
Damien Miller3b235662002-04-23 20:42:36 +100027
Darren Tucker50dbe832004-11-05 20:41:24 +110028int
Damien Miller05e82c32014-05-15 14:33:43 +100029buffer_get_short_ret(u_short *v, Buffer *buffer)
Damien Miller3b235662002-04-23 20:42:36 +100030{
Damien Miller05e82c32014-05-15 14:33:43 +100031 int ret;
Ben Lindstrome1353632002-06-23 21:29:23 +000032
Damien Miller05e82c32014-05-15 14:33:43 +100033 if ((ret = sshbuf_get_u16(buffer, v)) != 0) {
34 error("%s: %s", __func__, ssh_err(ret));
35 return -1;
36 }
37 return 0;
Darren Tucker50dbe832004-11-05 20:41:24 +110038}
39
40u_short
41buffer_get_short(Buffer *buffer)
42{
43 u_short ret;
44
45 if (buffer_get_short_ret(&ret, buffer) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +100046 fatal("%s: buffer error", __func__);
Darren Tucker50dbe832004-11-05 20:41:24 +110047
48 return (ret);
49}
50
51int
Damien Miller05e82c32014-05-15 14:33:43 +100052buffer_get_int_ret(u_int *v, Buffer *buffer)
Darren Tucker50dbe832004-11-05 20:41:24 +110053{
Damien Miller05e82c32014-05-15 14:33:43 +100054 int ret;
Darren Tucker50dbe832004-11-05 20:41:24 +110055
Damien Miller05e82c32014-05-15 14:33:43 +100056 if ((ret = sshbuf_get_u32(buffer, v)) != 0) {
57 error("%s: %s", __func__, ssh_err(ret));
58 return -1;
59 }
60 return 0;
Damien Miller3b235662002-04-23 20:42:36 +100061}
62
Ben Lindstrom46c16222000-12-22 01:43:59 +000063u_int
Damien Miller95def091999-11-25 00:26:21 +110064buffer_get_int(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065{
Darren Tucker50dbe832004-11-05 20:41:24 +110066 u_int ret;
Ben Lindstrome1353632002-06-23 21:29:23 +000067
Darren Tucker50dbe832004-11-05 20:41:24 +110068 if (buffer_get_int_ret(&ret, buffer) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +100069 fatal("%s: buffer error", __func__);
Darren Tucker50dbe832004-11-05 20:41:24 +110070
71 return (ret);
72}
73
74int
Damien Miller05e82c32014-05-15 14:33:43 +100075buffer_get_int64_ret(u_int64_t *v, Buffer *buffer)
Darren Tucker50dbe832004-11-05 20:41:24 +110076{
Damien Miller05e82c32014-05-15 14:33:43 +100077 int ret;
Darren Tucker50dbe832004-11-05 20:41:24 +110078
Damien Miller05e82c32014-05-15 14:33:43 +100079 if ((ret = sshbuf_get_u64(buffer, v)) != 0) {
80 error("%s: %s", __func__, ssh_err(ret));
81 return -1;
82 }
83 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084}
85
Ben Lindstrom2f959b42001-01-11 06:20:23 +000086u_int64_t
87buffer_get_int64(Buffer *buffer)
88{
Darren Tucker50dbe832004-11-05 20:41:24 +110089 u_int64_t ret;
Ben Lindstrome1353632002-06-23 21:29:23 +000090
Darren Tucker50dbe832004-11-05 20:41:24 +110091 if (buffer_get_int64_ret(&ret, buffer) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +100092 fatal("%s: buffer error", __func__);
Darren Tucker50dbe832004-11-05 20:41:24 +110093
94 return (ret);
Ben Lindstrom2f959b42001-01-11 06:20:23 +000095}
96
Damien Miller5f056372000-04-16 12:31:48 +100097void
Damien Miller3b235662002-04-23 20:42:36 +100098buffer_put_short(Buffer *buffer, u_short value)
99{
Damien Miller05e82c32014-05-15 14:33:43 +1000100 int ret;
Ben Lindstrome1353632002-06-23 21:29:23 +0000101
Damien Miller05e82c32014-05-15 14:33:43 +1000102 if ((ret = sshbuf_put_u16(buffer, value)) != 0)
103 fatal("%s: %s", __func__, ssh_err(ret));
Damien Miller3b235662002-04-23 20:42:36 +1000104}
105
106void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000107buffer_put_int(Buffer *buffer, u_int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108{
Damien Miller05e82c32014-05-15 14:33:43 +1000109 int ret;
Ben Lindstrome1353632002-06-23 21:29:23 +0000110
Damien Miller05e82c32014-05-15 14:33:43 +1000111 if ((ret = sshbuf_put_u32(buffer, value)) != 0)
112 fatal("%s: %s", __func__, ssh_err(ret));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113}
114
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000115void
116buffer_put_int64(Buffer *buffer, u_int64_t value)
117{
Damien Miller05e82c32014-05-15 14:33:43 +1000118 int ret;
Ben Lindstrome1353632002-06-23 21:29:23 +0000119
Damien Miller05e82c32014-05-15 14:33:43 +1000120 if ((ret = sshbuf_put_u64(buffer, value)) != 0)
121 fatal("%s: %s", __func__, ssh_err(ret));
Ben Lindstrom2f959b42001-01-11 06:20:23 +0000122}
123
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100124void *
Darren Tucker50dbe832004-11-05 20:41:24 +1100125buffer_get_string_ret(Buffer *buffer, u_int *length_ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126{
Damien Miller05e82c32014-05-15 14:33:43 +1000127 size_t len;
128 int ret;
Damien Miller5a6b4fe2001-12-21 14:56:54 +1100129 u_char *value;
Ben Lindstrome1353632002-06-23 21:29:23 +0000130
Damien Miller05e82c32014-05-15 14:33:43 +1000131 if ((ret = sshbuf_get_string(buffer, &value, &len)) != 0) {
132 error("%s: %s", __func__, ssh_err(ret));
133 return NULL;
Damien Miller17751bc2010-02-12 07:35:08 +1100134 }
Damien Miller05e82c32014-05-15 14:33:43 +1000135 if (length_ptr != NULL)
136 *length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
137 return value;
Darren Tucker50dbe832004-11-05 20:41:24 +1100138}
139
140void *
141buffer_get_string(Buffer *buffer, u_int *length_ptr)
142{
143 void *ret;
144
145 if ((ret = buffer_get_string_ret(buffer, length_ptr)) == NULL)
Damien Miller05e82c32014-05-15 14:33:43 +1000146 fatal("%s: buffer error", __func__);
Darren Tucker50dbe832004-11-05 20:41:24 +1100147 return (ret);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148}
149
Damien Millerda108ec2010-08-31 22:36:39 +1000150char *
151buffer_get_cstring_ret(Buffer *buffer, u_int *length_ptr)
152{
Damien Miller05e82c32014-05-15 14:33:43 +1000153 size_t len;
154 int ret;
155 char *value;
Damien Millerda108ec2010-08-31 22:36:39 +1000156
Damien Miller05e82c32014-05-15 14:33:43 +1000157 if ((ret = sshbuf_get_cstring(buffer, &value, &len)) != 0) {
158 error("%s: %s", __func__, ssh_err(ret));
Damien Millerda108ec2010-08-31 22:36:39 +1000159 return NULL;
Damien Millerda108ec2010-08-31 22:36:39 +1000160 }
161 if (length_ptr != NULL)
Damien Miller05e82c32014-05-15 14:33:43 +1000162 *length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
163 return value;
Damien Millerda108ec2010-08-31 22:36:39 +1000164}
165
166char *
167buffer_get_cstring(Buffer *buffer, u_int *length_ptr)
168{
169 char *ret;
170
171 if ((ret = buffer_get_cstring_ret(buffer, length_ptr)) == NULL)
Damien Miller05e82c32014-05-15 14:33:43 +1000172 fatal("%s: buffer error", __func__);
Damien Millerda108ec2010-08-31 22:36:39 +1000173 return ret;
174}
175
Damien Miller633de332014-05-15 13:48:26 +1000176const void *
Darren Tuckerebc71d92010-01-12 19:45:59 +1100177buffer_get_string_ptr_ret(Buffer *buffer, u_int *length_ptr)
Damien Millerdb255ca2008-05-19 14:59:37 +1000178{
Damien Miller05e82c32014-05-15 14:33:43 +1000179 size_t len;
180 int ret;
181 const u_char *value;
Damien Millerdb255ca2008-05-19 14:59:37 +1000182
Damien Miller05e82c32014-05-15 14:33:43 +1000183 if ((ret = sshbuf_get_string_direct(buffer, &value, &len)) != 0) {
184 error("%s: %s", __func__, ssh_err(ret));
Darren Tuckerebc71d92010-01-12 19:45:59 +1100185 return NULL;
186 }
Damien Miller05e82c32014-05-15 14:33:43 +1000187 if (length_ptr != NULL)
188 *length_ptr = len; /* Safe: sshbuf never stores len > 2^31 */
189 return value;
Damien Millerdb255ca2008-05-19 14:59:37 +1000190}
191
Damien Miller633de332014-05-15 13:48:26 +1000192const void *
Darren Tuckerebc71d92010-01-12 19:45:59 +1100193buffer_get_string_ptr(Buffer *buffer, u_int *length_ptr)
194{
Damien Miller633de332014-05-15 13:48:26 +1000195 const void *ret;
Darren Tuckerebc71d92010-01-12 19:45:59 +1100196
197 if ((ret = buffer_get_string_ptr_ret(buffer, length_ptr)) == NULL)
Damien Miller05e82c32014-05-15 14:33:43 +1000198 fatal("%s: buffer error", __func__);
Darren Tuckerebc71d92010-01-12 19:45:59 +1100199 return (ret);
200}
201
Damien Miller5f056372000-04-16 12:31:48 +1000202void
Ben Lindstrom46c16222000-12-22 01:43:59 +0000203buffer_put_string(Buffer *buffer, const void *buf, u_int len)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204{
Damien Miller05e82c32014-05-15 14:33:43 +1000205 int ret;
206
207 if ((ret = sshbuf_put_string(buffer, buf, len)) != 0)
208 fatal("%s: %s", __func__, ssh_err(ret));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209}
Damien Miller05e82c32014-05-15 14:33:43 +1000210
Damien Miller5f056372000-04-16 12:31:48 +1000211void
Damien Millerb38eff82000-04-01 11:09:21 +1000212buffer_put_cstring(Buffer *buffer, const char *s)
213{
Damien Miller05e82c32014-05-15 14:33:43 +1000214 int ret;
215
216 if ((ret = sshbuf_put_cstring(buffer, s)) != 0)
217 fatal("%s: %s", __func__, ssh_err(ret));
Damien Millerb38eff82000-04-01 11:09:21 +1000218}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000219
Damien Miller5f056372000-04-16 12:31:48 +1000220int
Damien Miller05e82c32014-05-15 14:33:43 +1000221buffer_get_char_ret(char *v, Buffer *buffer)
Darren Tucker50dbe832004-11-05 20:41:24 +1100222{
Damien Miller05e82c32014-05-15 14:33:43 +1000223 int ret;
224
225 if ((ret = sshbuf_get_u8(buffer, (u_char *)v)) != 0) {
226 error("%s: %s", __func__, ssh_err(ret));
227 return -1;
Darren Tucker50dbe832004-11-05 20:41:24 +1100228 }
Damien Miller05e82c32014-05-15 14:33:43 +1000229 return 0;
Darren Tucker50dbe832004-11-05 20:41:24 +1100230}
231
232int
Damien Miller95def091999-11-25 00:26:21 +1100233buffer_get_char(Buffer *buffer)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000234{
Damien Miller05e82c32014-05-15 14:33:43 +1000235 char ch;
Ben Lindstrome1353632002-06-23 21:29:23 +0000236
Darren Tucker50dbe832004-11-05 20:41:24 +1100237 if (buffer_get_char_ret(&ch, buffer) == -1)
Damien Miller05e82c32014-05-15 14:33:43 +1000238 fatal("%s: buffer error", __func__);
239 return (u_char) ch;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
241
Damien Miller5f056372000-04-16 12:31:48 +1000242void
Damien Miller95def091999-11-25 00:26:21 +1100243buffer_put_char(Buffer *buffer, int value)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244{
Damien Miller05e82c32014-05-15 14:33:43 +1000245 int ret;
Ben Lindstrome1353632002-06-23 21:29:23 +0000246
Damien Miller05e82c32014-05-15 14:33:43 +1000247 if ((ret = sshbuf_put_u8(buffer, value)) != 0)
248 fatal("%s: %s", __func__, ssh_err(ret));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249}
Damien Miller91b580e2014-01-12 19:21:22 +1100250
Damien Miller91b580e2014-01-12 19:21:22 +1100251void
252buffer_put_bignum2_from_string(Buffer *buffer, const u_char *s, u_int l)
253{
Damien Miller05e82c32014-05-15 14:33:43 +1000254 int ret;
Damien Miller91b580e2014-01-12 19:21:22 +1100255
Damien Miller05e82c32014-05-15 14:33:43 +1000256 if ((ret = sshbuf_put_bignum2_bytes(buffer, s, l)) != 0)
257 fatal("%s: %s", __func__, ssh_err(ret));
Damien Miller91b580e2014-01-12 19:21:22 +1100258}
259