]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
gnutls_ext_raw_parse: introduced function
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Fri, 23 Feb 2018 08:55:50 +0000 (09:55 +0100)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Mon, 26 Feb 2018 07:45:08 +0000 (08:45 +0100)
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>
doc/cha-gtls-app.texi
lib/extv.c
lib/extv.h
lib/hello_ext.c
lib/includes/gnutls/gnutls.h.in
lib/libgnutls.map
lib/tls13/certificate.c
lib/tls13/certificate_request.c
lib/tls13/session_ticket.c
tests/Makefile.am
tests/gnutls_ext_raw_parse.c [new file with mode: 0644]

index e401713814871445d32e566a52209d0fb46b1701..6575120756125a84c919707ac443030f7567fda4 100644 (file)
@@ -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);
 @}
 
index 32dbc942fea6c442518f2d93d15a215d284968a9..0abfd370fb539a6dd73bc6482a0722f9cc050912 100644 (file)
@@ -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:
index b5089efed5b0fe8f0647fa41ce3fb1c26f11a6cd..9f13f7a2ce65dcff8855935aab6d94648ca2b27c 100644 (file)
@@ -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
index 4a4b2cc675e3e71e450891d2afc742a21b74d71c..844b570061908afd35b9bdf28e494aadfa39ebfa 100644 (file)
@@ -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;
index dd123aaf4e9ed5268db3ceab20fe6e9ce35fa095..b9142519aad74be124bc57c144de6388132cbe53 100644 (file)
@@ -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:
index 5d7d7a5acee93cc9f32990bb934256bb5b38af78..07aaf714ccb21c669055a1cc2e0b126e015b1aee 100644 (file)
@@ -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 {
index 1d688de0b2462f96e1c570b3f77032da81df49bc..ad05f372c5601516aa8a0eaa87544f3e382f9c5b 100644 (file)
@@ -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;
index 959603f477cf6bd20496765cebe67bec240a30fa..4e7c104afb65c4a90bba5257873b3f52b41fecf8 100644 (file)
@@ -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;
index 3dbec9260f0ab26f3196a95ae7b6823daa178672..d5d62f433f6daaa10aa329e740ddbc6ed02b40b1 100644 (file)
@@ -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;
index c3c74f545150d6af1b0588202420027a8931b371..27d971232b0791356e02924939c18567de1e777c 100644 (file)
@@ -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 (file)
index 0000000..1402cdf
--- /dev/null
@@ -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 */