summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@redhat.com>2018-02-23 09:55:50 +0100
committerNikos Mavrogiannopoulos <nmav@redhat.com>2018-02-26 08:45:08 +0100
commit8c0e18585f96d4cfd046598a9d40c94eabff0b33 (patch)
tree9875594d70f09e63f881a82fa71487c4fe1c3208
parent3684ce08e4dd419a3d42c66b4042d8d3fd767c35 (diff)
downloadgnutls-8c0e18585f96d4cfd046598a9d40c94eabff0b33.tar.gz
gnutls_ext_raw_parse: introduced function
That function can be combined with callbacks like gnutls_handshake_set_hook_function() for applications to be able to process messages when necessary. Resolves #382 Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
-rw-r--r--doc/cha-gtls-app.texi18
-rw-r--r--lib/extv.c75
-rw-r--r--lib/extv.h2
-rw-r--r--lib/hello_ext.c2
-rw-r--r--lib/includes/gnutls/gnutls.h.in4
-rw-r--r--lib/libgnutls.map1
-rw-r--r--lib/tls13/certificate.c4
-rw-r--r--lib/tls13/certificate_request.c13
-rw-r--r--lib/tls13/session_ticket.c4
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/gnutls_ext_raw_parse.c317
11 files changed, 427 insertions, 15 deletions
diff --git a/doc/cha-gtls-app.texi b/doc/cha-gtls-app.texi
index e401713814..6575120756 100644
--- a/doc/cha-gtls-app.texi
+++ b/doc/cha-gtls-app.texi
@@ -1583,6 +1583,20 @@ continue in the handshake process. A brief usage example is shown
below.
@example
+static int ext_hook_func(void *ctx, unsigned tls_id,
+ const unsigned char *data, unsigned size)
+@{
+ if (tls_id == 0) @{ /* server name */
+ /* figure the advertized name - the following hack
+ * relies on the fact that this extension only supports
+ * DNS names, and due to a protocol bug cannot be extended
+ * to support anything else. */
+ if (name < 5) return 0;
+ name = data+5;
+ name_size = size-5;
+ @}
+@}
+
static int
handshake_hook_func(gnutls_session_t session, unsigned int htype,
unsigned when, unsigned int incoming, const gnutls_datum_t *msg)
@@ -1590,6 +1604,10 @@ handshake_hook_func(gnutls_session_t session, unsigned int htype,
assert(htype == GNUTLS_HANDSHAKE_CLIENT_HELLO);
assert(when == GNUTLS_HOOK_PRE);
+ ret = gnutls_ext_raw_parse(NULL, ext_hook_func, msg,
+ GNUTLS_EXT_RAW_FLAG_CLIENT_HELLO);
+ assert(ret >= 0);
+
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cred);
@}
diff --git a/lib/extv.c b/lib/extv.c
index 32dbc942fe..0abfd370fb 100644
--- a/lib/extv.c
+++ b/lib/extv.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2017 Red Hat, Inc.
+ * Copyright (C) 2017-2018 Red Hat, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
@@ -28,7 +28,7 @@
/* 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),
+ gnutls_ext_raw_process_func cb,
const uint8_t * data, int data_size)
{
int next, ret;
@@ -75,7 +75,78 @@ int _gnutls_extv_parse(void *ctx,
return gnutls_assert_val(GNUTLS_E_UNEXPECTED_EXTENSIONS_LENGTH);
return 0;
+}
+
+#define HANDSHAKE_SESSION_ID_POS (34)
+/**
+ * gnutls_ext_raw_parse:
+ * @ctx: a pointer to pass to callback function
+ * @cb: callback function to process each extension found
+ * @data: TLS extension data
+ * @flags: should be zero or %GNUTLS_EXT_RAW_FLAG_CLIENT_HELLO
+ *
+ * This function iterates through the TLS extensions as passed in
+ * @data, passing the individual extension data to callback. The
+ * @data must conform to Extension extensions<0..2^16-1> format.
+ *
+ * If flags is %GNUTLS_EXT_RAW_FLAG_CLIENT_HELLO then this function
+ * will parse the extension data from the position, as if the packet in
+ * @data is a client hello (without record or handshake headers) -
+ * as provided by gnutls_handshake_set_hook_function().
+ *
+ * The return value of the callback will be propagated.
+ *
+ * Returns: %GNUTLS_E_SUCCESS on success, or an error code. On unknown
+ * flags it returns %GNUTLS_E_INVALID_REQUEST.
+ *
+ * Since: 3.6.3
+ **/
+int gnutls_ext_raw_parse(void *ctx, gnutls_ext_raw_process_func cb,
+ const gnutls_datum_t *data, unsigned int flags)
+{
+ if (flags & GNUTLS_EXT_RAW_FLAG_CLIENT_HELLO) {
+ ssize_t size = data->size;
+ size_t len;
+ uint8_t *p = data->data;
+
+ DECR_LEN(size, HANDSHAKE_SESSION_ID_POS);
+
+ if (p[0] != 0x03)
+ return gnutls_assert_val(GNUTLS_E_UNSUPPORTED_VERSION_PACKET);
+
+ p += HANDSHAKE_SESSION_ID_POS;
+
+ /* skip session id */
+ DECR_LEN(size, 1);
+ len = p[0];
+ p++;
+ DECR_LEN(size, len);
+ p += len;
+
+ /* CipherSuites */
+ DECR_LEN(size, 2);
+ len = _gnutls_read_uint16(p);
+ p += 2;
+ DECR_LEN(size, len);
+ p += len;
+
+ /* legacy_compression_methods */
+ DECR_LEN(size, 1);
+ len = p[0];
+ p++;
+ DECR_LEN(size, len);
+ p += len;
+
+ if (size <= 0)
+ return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
+
+ return _gnutls_extv_parse(ctx, cb, p, size);
+ }
+
+ if (flags != 0)
+ return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
+ return _gnutls_extv_parse(ctx, cb, data->data, data->size);
}
/* Returns:
diff --git a/lib/extv.h b/lib/extv.h
index b5089efed5..9f13f7a2ce 100644
--- a/lib/extv.h
+++ b/lib/extv.h
@@ -30,7 +30,7 @@
* 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),
+ gnutls_ext_raw_process_func cb,
const uint8_t * data, int data_size);
inline static
diff --git a/lib/hello_ext.c b/lib/hello_ext.c
index 4a4b2cc675..844b570061 100644
--- a/lib/hello_ext.c
+++ b/lib/hello_ext.c
@@ -195,7 +195,7 @@ typedef struct hello_ext_ctx_st {
} hello_ext_ctx_st;
static
-int hello_ext_parse(void *_ctx, uint16_t tls_id, const uint8_t *data, int data_size)
+int hello_ext_parse(void *_ctx, unsigned tls_id, const uint8_t *data, unsigned data_size)
{
hello_ext_ctx_st *ctx = _ctx;
gnutls_session_t session = ctx->session;
diff --git a/lib/includes/gnutls/gnutls.h.in b/lib/includes/gnutls/gnutls.h.in
index dd123aaf4e..b9142519aa 100644
--- a/lib/includes/gnutls/gnutls.h.in
+++ b/lib/includes/gnutls/gnutls.h.in
@@ -2708,6 +2708,10 @@ typedef int (*gnutls_ext_pack_func) (gnutls_ext_priv_data_t data,
typedef int (*gnutls_ext_unpack_func) (gnutls_buffer_t packed_data,
gnutls_ext_priv_data_t *data);
+#define GNUTLS_EXT_RAW_FLAG_CLIENT_HELLO 1
+typedef int (*gnutls_ext_raw_process_func)(void *ctx, unsigned tls_id, const unsigned char *data, unsigned data_size);
+int gnutls_ext_raw_parse(void *ctx, gnutls_ext_raw_process_func cb,
+ const gnutls_datum_t *data, unsigned int flags);
/**
* gnutls_ext_parse_type_t:
diff --git a/lib/libgnutls.map b/lib/libgnutls.map
index 5d7d7a5ace..07aaf714cc 100644
--- a/lib/libgnutls.map
+++ b/lib/libgnutls.map
@@ -1214,6 +1214,7 @@ GNUTLS_3_6_3
gnutls_certificate_set_ocsp_status_request_mem;
gnutls_certificate_get_ocsp_expiration;
gnutls_record_send2;
+ gnutls_ext_raw_parse;
} GNUTLS_3_6_2;
GNUTLS_FIPS140_3_4 {
diff --git a/lib/tls13/certificate.c b/lib/tls13/certificate.c
index 1d688de0b2..ad05f372c5 100644
--- a/lib/tls13/certificate.c
+++ b/lib/tls13/certificate.c
@@ -29,7 +29,7 @@
#include "mbuffers.h"
#include "ext/status_request.h"
-static int parse_cert_extension(void *ctx, uint16_t tls_id, const uint8_t *data, int data_size);
+static int parse_cert_extension(void *ctx, unsigned tls_id, const uint8_t *data, unsigned data_size);
static int parse_cert_list(gnutls_session_t session, uint8_t * data, size_t data_size);
int _gnutls13_recv_certificate(gnutls_session_t session)
@@ -309,7 +309,7 @@ typedef struct crt_cert_ctx_st {
unsigned idx;
} crt_cert_ctx_st;
-static int parse_cert_extension(void *_ctx, uint16_t tls_id, const uint8_t *data, int data_size)
+static int parse_cert_extension(void *_ctx, unsigned tls_id, const uint8_t *data, unsigned data_size)
{
crt_cert_ctx_st *ctx = _ctx;
gnutls_session_t session = ctx->session;
diff --git a/lib/tls13/certificate_request.c b/lib/tls13/certificate_request.c
index 959603f477..4e7c104afb 100644
--- a/lib/tls13/certificate_request.c
+++ b/lib/tls13/certificate_request.c
@@ -55,10 +55,11 @@ static unsigned is_algo_in_list(gnutls_pk_algorithm_t algo, gnutls_pk_algorithm_
}
static
-int parse_cert_extension(void *_ctx, uint16_t tls_id, const uint8_t *data, int data_size)
+int parse_cert_extension(void *_ctx, unsigned tls_id, const uint8_t *data, unsigned data_size)
{
crt_req_ctx_st *ctx = _ctx;
gnutls_session_t session = ctx->session;
+ unsigned v;
int ret;
/* Decide which certificate to use if the signature algorithms extension
@@ -78,8 +79,8 @@ int parse_cert_extension(void *_ctx, uint16_t tls_id, const uint8_t *data, int d
if (data_size < 2)
return gnutls_assert_val(GNUTLS_E_TLS_PACKET_DECODING_ERROR);
- ret = _gnutls_read_uint16(data);
- if (ret != data_size-2)
+ v = _gnutls_read_uint16(data);
+ if (v != data_size-2)
return gnutls_assert_val(GNUTLS_E_TLS_PACKET_DECODING_ERROR);
data += 2;
@@ -111,12 +112,12 @@ int parse_cert_extension(void *_ctx, uint16_t tls_id, const uint8_t *data, int d
return gnutls_assert_val(GNUTLS_E_TLS_PACKET_DECODING_ERROR);
}
- ret = _gnutls_read_uint16(data);
- if (ret != data_size-2)
+ v = _gnutls_read_uint16(data);
+ if (v != data_size-2)
return gnutls_assert_val(GNUTLS_E_TLS_PACKET_DECODING_ERROR);
ctx->rdn = data+2;
- ctx->rdn_size = ret;
+ ctx->rdn_size = v;
}
return 0;
diff --git a/lib/tls13/session_ticket.c b/lib/tls13/session_ticket.c
index 3dbec9260f..d5d62f433f 100644
--- a/lib/tls13/session_ticket.c
+++ b/lib/tls13/session_ticket.c
@@ -27,7 +27,7 @@
#include "tls13/session_ticket.h"
#include "auth/cert.h"
-static int parse_nst_extension(void *ctx, uint16_t tls_id, const uint8_t *data, int data_size);
+static int parse_nst_extension(void *ctx, unsigned tls_id, const uint8_t *data, unsigned data_size);
int _gnutls13_recv_session_ticket(gnutls_session_t session, gnutls_buffer_st *buf)
{
@@ -76,7 +76,7 @@ cleanup:
return ret;
}
-static int parse_nst_extension(void *ctx, uint16_t tls_id, const uint8_t *data, int data_size)
+static int parse_nst_extension(void *ctx, unsigned tls_id, const uint8_t *data, unsigned data_size)
{
/* ignore all extensions */
return 0;
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c3c74f5451..27d971232b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -121,7 +121,7 @@ ctests += mini-record-2 simple gnutls_hmac_fast set_pkcs12_cred cert certuniquei
crq_apis init_roundtrip pkcs12_s2k_pem dn2 mini-eagain tls12-rehandshake-cert-3 \
nul-in-x509-names x509_altname pkcs12_encode mini-x509 \
tls12-rehandshake-cert rng-fork mini-eagain-dtls resume-dtls \
- tls13-rehandshake-cert \
+ tls13-rehandshake-cert gnutls_ext_raw_parse \
x509cert x509cert-tl infoaccess mini-dtls-hello-verify sign-verify-ed25519-rfc8080 \
trustdb-tofu dtls-rehandshake-anon mini-alpn mini-dtls-large \
mini-termination mini-x509-cas mini-x509-2 pkcs12_simple \
diff --git a/tests/gnutls_ext_raw_parse.c b/tests/gnutls_ext_raw_parse.c
new file mode 100644
index 0000000000..1402cdf16e
--- /dev/null
+++ b/tests/gnutls_ext_raw_parse.c
@@ -0,0 +1,317 @@
+/*
+ * Copyright (C) 2018 Red Hat, Inc.
+ *
+ * Author: Nikos Mavrogiannopoulos
+ *
+ * This file is part of GnuTLS.
+ *
+ * GnuTLS is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GnuTLS 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
+ * 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/>
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#if defined(_WIN32)
+
+int main()
+{
+ exit(77);
+}
+
+#else
+
+#include <string.h>
+#include <sys/types.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <arpa/inet.h>
+#include <unistd.h>
+#include <time.h>
+#include <gnutls/gnutls.h>
+#include <signal.h>
+#include <assert.h>
+
+#include "utils.h"
+#include "cert-common.h"
+#include "tls13/ext-parse.h"
+
+/* This program tests gnutls_ext_raw_parse with GNUTLS_EXT_RAW_FLAG_CLIENT_HELLO
+ * flag.
+ */
+
+#define HOSTNAME "example.com"
+
+static void server_log_func(int level, const char *str)
+{
+ fprintf(stderr, "server|<%d>| %s", level, str);
+}
+
+static void client_log_func(int level, const char *str)
+{
+ fprintf(stderr, "client|<%d>| %s", level, str);
+}
+
+static unsigned found_server_name = 0;
+static unsigned found_status_req = 0;
+static unsigned bare_version = 0;
+
+static int ext_callback(void *ctx, unsigned tls_id, const unsigned char *data, unsigned size)
+{
+ if (tls_id == 0) { /* server name */
+ /* very interesting extension, 4 bytes of sizes
+ * and 1 byte of type. */
+ unsigned esize = (data[0] << 8) | data[1];
+ assert(esize == strlen(HOSTNAME)+3);
+
+ size -= 2;
+ data += 2;
+
+ assert(data[0] == 0);
+ data++;
+ size--;
+
+ esize = (data[0] << 8) | data[1];
+
+ assert(esize == strlen(HOSTNAME));
+ data += 2;
+ size -= 2;
+
+ assert(memcmp(data, HOSTNAME, strlen(HOSTNAME)) == 0);
+ found_server_name = 1;
+ } else if (tls_id == 5) {
+ found_status_req = 1;
+ } else {
+ if (debug)
+ success("found extension: %u\n", tls_id);
+ }
+ return 0;
+}
+
+static int handshake_callback(gnutls_session_t session, unsigned int htype,
+ unsigned post, unsigned int incoming, const gnutls_datum_t *msg)
+{
+ int ret;
+
+ if (htype == GNUTLS_HANDSHAKE_CLIENT_HELLO && post) {
+ if (bare_version) {
+ ret = gnutls_ext_raw_parse(NULL, ext_callback, msg, GNUTLS_EXT_RAW_FLAG_CLIENT_HELLO);
+ } else {
+ unsigned pos;
+ gnutls_datum_t mmsg;
+ assert(msg->size >= HANDSHAKE_SESSION_ID_POS);
+ pos = HANDSHAKE_SESSION_ID_POS;
+ SKIP8(pos, msg->size);
+ SKIP16(pos, msg->size);
+ SKIP8(pos, msg->size);
+
+ mmsg.data = &msg->data[pos];
+ mmsg.size = msg->size - pos;
+ ret = gnutls_ext_raw_parse(NULL, ext_callback, &mmsg, 0);
+ }
+ assert(ret >= 0);
+ }
+ return 0;
+}
+
+static void client(int fd)
+{
+ int ret;
+ gnutls_certificate_credentials_t x509_cred;
+ gnutls_session_t session;
+
+ global_init();
+
+ if (debug) {
+ gnutls_global_set_log_function(client_log_func);
+ gnutls_global_set_log_level(7);
+ }
+
+ gnutls_certificate_allocate_credentials(&x509_cred);
+
+ /* Initialize TLS session
+ */
+ gnutls_init(&session, GNUTLS_CLIENT);
+ gnutls_handshake_set_timeout(session, 20 * 1000);
+
+ /* Use default priorities */
+ gnutls_priority_set_direct(session, "NORMAL", NULL);
+
+ gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
+
+ gnutls_transport_set_int(session, fd);
+ assert(gnutls_server_name_set(session, GNUTLS_NAME_DNS, HOSTNAME, strlen(HOSTNAME))>=0);
+
+ /* Perform the TLS handshake
+ */
+ do {
+ ret = gnutls_handshake(session);
+ }
+ while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
+
+ if (ret == GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM) {
+ /* success */
+ goto end;
+ }
+
+ if (ret < 0) {
+ fail("client: Handshake failed: %s\n", gnutls_strerror(ret));
+ } else {
+ if (debug)
+ success("client: Handshake was completed\n");
+ }
+
+ if (debug)
+ success("client: TLS version is: %s\n",
+ gnutls_protocol_get_name
+ (gnutls_protocol_get_version(session)));
+
+ gnutls_bye(session, GNUTLS_SHUT_WR);
+
+ end:
+
+ close(fd);
+
+ gnutls_deinit(session);
+
+ gnutls_certificate_free_credentials(x509_cred);
+
+ gnutls_global_deinit();
+}
+
+
+static void server(int fd)
+{
+ int ret;
+ gnutls_session_t session;
+ gnutls_certificate_credentials_t x509_cred;
+
+ /* this must be called once in the program
+ */
+ global_init();
+
+ if (debug) {
+ gnutls_global_set_log_function(server_log_func);
+ gnutls_global_set_log_level(4711);
+ }
+
+ gnutls_certificate_allocate_credentials(&x509_cred);
+ gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
+ &server_key,
+ GNUTLS_X509_FMT_PEM);
+
+ gnutls_init(&session, GNUTLS_SERVER);
+ gnutls_handshake_set_timeout(session, 20 * 1000);
+
+ gnutls_handshake_set_hook_function(session, GNUTLS_HANDSHAKE_CLIENT_HELLO,
+ GNUTLS_HOOK_POST,
+ handshake_callback);
+
+ /* avoid calling all the priority functions, since the defaults
+ * are adequate.
+ */
+ gnutls_priority_set_direct(session, "NORMAL", NULL);
+
+ gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
+
+ gnutls_transport_set_int(session, fd);
+
+ do {
+ ret = gnutls_handshake(session);
+ } while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
+ if (ret < 0) {
+ /* failure is expected here */
+ goto end;
+ }
+
+ if (debug)
+ success("server: Handshake was completed\n");
+
+ if (debug)
+ success("server: TLS version is: %s\n",
+ gnutls_protocol_get_name
+ (gnutls_protocol_get_version(session)));
+
+ assert(found_server_name != 0);
+ assert(found_status_req != 0);
+
+ gnutls_bye(session, GNUTLS_SHUT_WR);
+
+ end:
+ close(fd);
+ gnutls_deinit(session);
+
+ gnutls_certificate_free_credentials(x509_cred);
+
+ gnutls_global_deinit();
+
+ if (debug)
+ success("server: finished\n");
+}
+
+static void ch_handler(int sig)
+{
+ return;
+}
+
+static void start(unsigned val)
+{
+ int fd[2];
+ int ret, status = 0;
+ pid_t child;
+
+ signal(SIGCHLD, ch_handler);
+ signal(SIGPIPE, SIG_IGN);
+
+ bare_version = val;
+
+ ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
+ if (ret < 0) {
+ perror("socketpair");
+ exit(1);
+ }
+
+ child = fork();
+ if (child < 0) {
+ perror("fork");
+ fail("fork");
+ exit(1);
+ }
+
+ if (child) {
+ /* parent */
+ close(fd[1]);
+ server(fd[0]);
+ waitpid(child, &status, 0);
+ check_wait_status(status);
+ } else {
+ close(fd[0]);
+ client(fd[1]);
+ exit(0);
+ }
+
+ return;
+}
+
+void doit(void)
+{
+ start(0);
+ start(1);
+}
+
+#endif /* _WIN32 */