summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2017-09-20 09:46:34 +0200
committerNikos Mavrogiannopoulos <nmav@redhat.com>2017-09-20 10:55:53 +0200
commit3f602849b8dc653d8c444d001e0e8932f9cb1b5f (patch)
tree603db5fd36fb005f831601e1f5839f65911fe2cf
parentd8b063d52d8eb88e41ae914411a084fafeba6ea0 (diff)
downloadgnutls-3f602849b8dc653d8c444d001e0e8932f9cb1b5f.tar.gz
extv: introduced a low-level extension parsing code
This will simplify the parsing and handling of extensions throughout the TLS 1.3 message contents. Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
-rw-r--r--lib/Makefile.am2
-rw-r--r--lib/extv.c127
-rw-r--r--lib/extv.h72
-rw-r--r--lib/gnutls_int.h4
4 files changed, 203 insertions, 2 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 3442a6c443..49dd89bd73 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -79,7 +79,7 @@ COBJECTS = range.c record.c compress.c debug.c cipher.c \
safe-memfuncs.c system/inet_pton.c atfork.c atfork.h randomart.c \
system-keys.h urls.c urls.h prf.c auto-verify.c dh-session.c \
cert-session.c handshake-checks.c dtls-sw.c dh-primes.c openpgp_compat.c \
- crypto-selftests.c crypto-selftests-pk.c secrets.c
+ crypto-selftests.c crypto-selftests-pk.c secrets.c extv.c extv.h
if WINDOWS
COBJECTS += system/keys-win.c
diff --git a/lib/extv.c b/lib/extv.c
new file mode 100644
index 0000000000..d68f852769
--- /dev/null
+++ b/lib/extv.c
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2017 Red Hat, Inc.
+ *
+ * Author: Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS.
+ *
+ * The GnuTLS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#include "gnutls_int.h"
+#include "extensions.h"
+#include "errors.h"
+#include "extv.h"
+
+/* Iterates through all extensions found, and calls the cb()
+ * function with their data */
+int _gnutls_extv_parse(void *ctx,
+ int (*cb)(void *ctx, uint16_t tls_id, const uint8_t *data, int data_size),
+ const uint8_t * data, int data_size)
+{
+ int next, ret;
+ int pos = 0;
+ uint16_t tls_id;
+ const uint8_t *sdata;
+ uint16_t size;
+
+ if (data_size == 0)
+ return 0;
+
+ DECR_LENGTH_RET(data_size, 2, GNUTLS_E_UNEXPECTED_EXTENSIONS_LENGTH);
+ next = _gnutls_read_uint16(data);
+ pos += 2;
+
+ DECR_LENGTH_RET(data_size, next, GNUTLS_E_UNEXPECTED_EXTENSIONS_LENGTH);
+
+ if (next == 0 && data_size == 0) /* field is present, but has zero length? Ignore it. */
+ return 0;
+ else if (data_size > 0) /* forbid unaccounted data */
+ return gnutls_assert_val(GNUTLS_E_UNEXPECTED_EXTENSIONS_LENGTH);
+
+ do {
+ DECR_LENGTH_RET(next, 2, GNUTLS_E_UNEXPECTED_EXTENSIONS_LENGTH);
+ tls_id = _gnutls_read_uint16(&data[pos]);
+ pos += 2;
+
+ DECR_LENGTH_RET(next, 2, GNUTLS_E_UNEXPECTED_EXTENSIONS_LENGTH);
+ size = _gnutls_read_uint16(&data[pos]);
+ pos += 2;
+
+ DECR_LENGTH_RET(next, size, GNUTLS_E_UNEXPECTED_EXTENSIONS_LENGTH);
+ sdata = &data[pos];
+ pos += size;
+
+ ret = cb(ctx, tls_id, sdata, size);
+ if (ret < 0)
+ return gnutls_assert_val(ret);
+ }
+ while (next > 2);
+
+ /* forbid leftovers */
+ if (next > 0)
+ return gnutls_assert_val(GNUTLS_E_UNEXPECTED_EXTENSIONS_LENGTH);
+
+ return 0;
+
+}
+
+/* Returns:
+ * * On success the number of bytes appended (always positive), or zero if not sent
+ * * On failure, a negative error code.
+ */
+int _gnutls_extv_append(gnutls_buffer_st *buf,
+ uint16_t tls_id,
+ void *ctx,
+ int (*cb)(void *ctx, gnutls_buffer_st *buf))
+{
+ int size_pos, appended, ret;
+ size_t size_prev;
+
+ ret = _gnutls_buffer_append_prefix(buf, 16, tls_id);
+ if (ret < 0)
+ return gnutls_assert_val(ret);
+
+ size_pos = buf->length;
+ ret = _gnutls_buffer_append_prefix(buf, 16, 0);
+ if (ret < 0)
+ return gnutls_assert_val(ret);
+
+ size_prev = buf->length;
+ ret = cb(ctx, buf);
+ if (ret < 0 && ret != GNUTLS_E_INT_RET_0) {
+ return gnutls_assert_val(ret);
+ }
+
+ /* returning GNUTLS_E_INT_RET_0 means to send an empty
+ * extension of this type.
+ */
+ appended = buf->length - size_prev;
+
+ if (appended > 0 || ret == GNUTLS_E_INT_RET_0) {
+ if (ret == GNUTLS_E_INT_RET_0)
+ appended = 0;
+
+ /* write the real size */
+ _gnutls_write_uint16(appended,
+ &buf->data[size_pos]);
+ } else if (appended == 0) {
+ buf->length -= 4; /* reset type and size */
+ return 0;
+ }
+
+ return appended + 4;
+}
+
diff --git a/lib/extv.h b/lib/extv.h
new file mode 100644
index 0000000000..b16ad3cc65
--- /dev/null
+++ b/lib/extv.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2017 Red Hat, Inc.
+ *
+ * Author: Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS.
+ *
+ * The GnuTLS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#ifndef GNUTLS_EXTV_H
+#define GNUTLS_EXTV_H
+
+#include <gnutls/gnutls.h>
+#include "str.h"
+
+/* Iterates through all TLS-type extensions in data, and
+ * calls the callback function for each of them. The ctx, flags
+ * and parse_type are passed verbatim to callback. */
+int _gnutls_extv_parse(void *ctx,
+ int (*cb)(void *ctx, uint16_t tls_id, const uint8_t *data, int data_size),
+ const uint8_t * data, int data_size);
+
+inline static
+int _gnutls_extv_append_init(gnutls_buffer_st *buf)
+{
+ unsigned pos;
+ int ret;
+
+ pos = buf->length;
+
+ ret = _gnutls_buffer_append_prefix(buf, 16, 0);
+ if (ret < 0)
+ return gnutls_assert_val(ret);
+
+ return pos;
+}
+
+/* its input is the buffer and the return value of _gnutls_extv_append_init() */
+inline static
+int _gnutls_extv_append_final(gnutls_buffer_st *buf, unsigned init)
+{
+ unsigned size = buf->length - init - 2;
+
+ if (size > UINT16_MAX) /* sent too many extensions */
+ return gnutls_assert_val(GNUTLS_E_HANDSHAKE_TOO_LARGE);
+
+ if (size > 0)
+ _gnutls_write_uint16(size, &buf->data[init]);
+
+ return 0;
+}
+
+int _gnutls_extv_append(gnutls_buffer_st *buf,
+ uint16_t tls_id,
+ void *ctx,
+ int (*cb)(void *ctx, gnutls_buffer_st *buf));
+
+
+#endif
diff --git a/lib/gnutls_int.h b/lib/gnutls_int.h
index 8978276aeb..e91b4e72fc 100644
--- a/lib/gnutls_int.h
+++ b/lib/gnutls_int.h
@@ -292,6 +292,8 @@ typedef enum extensions_t {
} extensions_t;
#define GNUTLS_EXTENSION_MAX_VALUE 31
+#define ext_track_t uint32_t
+
#if GNUTLS_EXTENSION_MAX >= GNUTLS_EXTENSION_MAX_VALUE
# error over limit
#endif
@@ -1151,7 +1153,7 @@ typedef struct {
* otherwise the extensions we received from the client. This is
* an OR of (1<<extensions_t values).
*/
- uint32_t used_exts;
+ ext_track_t used_exts;
/* this is not the negotiated max_record_recv_size, but the actual maximum
* receive size */