]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
constate: simplified allocation of epochs
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Fri, 15 Sep 2017 07:29:30 +0000 (09:29 +0200)
committerNikos Mavrogiannopoulos <nmav@redhat.com>
Mon, 25 Sep 2017 05:46:15 +0000 (07:46 +0200)
Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
lib/constate.c
lib/constate.h
lib/handshake.c
lib/state.c

index becdfd9dcbb8387957f5314a7ac933242ea53b42..45a4d2eea31a3f90186af320f43ba001fcee09a4 100644 (file)
@@ -260,23 +260,6 @@ _gnutls_set_cipher_suite2(gnutls_session_t session,
        return 0;
 }
 
-void
-_gnutls_epoch_set_null_algos(gnutls_session_t session,
-                            record_parameters_st * params)
-{
-       /* This is only called on startup. We are extra paranoid about this
-          because it may cause unencrypted application data to go out on
-          the wire. */
-       if (params->initialized || params->epoch != 0) {
-               gnutls_assert();
-               return;
-       }
-
-       params->cipher = cipher_to_entry(GNUTLS_CIPHER_NULL);
-       params->mac = mac_to_entry(GNUTLS_MAC_NULL);
-       params->initialized = 1;
-}
-
 int _gnutls_epoch_set_keys(gnutls_session_t session, uint16_t epoch)
 {
        int hash_size;
@@ -514,15 +497,14 @@ _gnutls_epoch_get(gnutls_session_t session, unsigned int epoch_rel,
 }
 
 int
-_gnutls_epoch_alloc(gnutls_session_t session, uint16_t epoch,
-                   record_parameters_st ** out)
+_gnutls_epoch_new(gnutls_session_t session, unsigned null_epoch, record_parameters_st **newp)
 {
        record_parameters_st **slot;
 
        _gnutls_record_log("REC[%p]: Allocating epoch #%u\n", session,
-                          epoch);
+                          session->security_parameters.epoch_next);
 
-       slot = epoch_get_slot(session, epoch);
+       slot = epoch_get_slot(session, session->security_parameters.epoch_next);
 
        /* If slot out of range or not empty. */
        if (slot == NULL)
@@ -535,17 +517,24 @@ _gnutls_epoch_alloc(gnutls_session_t session, uint16_t epoch,
        if (*slot == NULL)
                return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
 
-       (*slot)->epoch = epoch;
-       (*slot)->cipher = NULL;
-       (*slot)->mac = NULL;
+       (*slot)->epoch = session->security_parameters.epoch_next;
+
+       if (null_epoch) {
+               (*slot)->cipher = cipher_to_entry(GNUTLS_CIPHER_NULL);
+               (*slot)->mac = mac_to_entry(GNUTLS_MAC_NULL);
+               (*slot)->initialized = 1;
+       } else {
+               (*slot)->cipher = NULL;
+               (*slot)->mac = NULL;
+       }
 
        if (IS_DTLS(session))
-               _gnutls_write_uint16(epoch,
+               _gnutls_write_uint16(session->security_parameters.epoch_next,
                                     UINT64DATA((*slot)->write.
                                                sequence_number));
 
-       if (out != NULL)
-               *out = *slot;
+       if (newp != NULL)
+               *newp = *slot;
 
        return 0;
 }
index bb6b38762112c5ff1ac81efe78d2e09bb578a56d..6145d77f3d5f699780522c372422143de430655b 100644 (file)
@@ -32,10 +32,12 @@ int _gnutls_connection_state_init(gnutls_session_t session);
 int _gnutls_read_connection_state_init(gnutls_session_t session);
 int _gnutls_write_connection_state_init(gnutls_session_t session);
 
+#define _gnutls_epoch_bump(session) \
+       (session)->security_parameters.epoch_next++
+
 int _gnutls_epoch_get(gnutls_session_t session, unsigned int epoch_rel,
                      record_parameters_st ** params_out);
-int _gnutls_epoch_alloc(gnutls_session_t session, uint16_t epoch,
-                       record_parameters_st ** out);
+int _gnutls_epoch_new(gnutls_session_t session, unsigned null_epoch, record_parameters_st **newp);
 void _gnutls_epoch_gc(gnutls_session_t session);
 void _gnutls_epoch_free(gnutls_session_t session,
                        record_parameters_st * state);
index 8e20c76ab31b63dc94da9a80536f7a845844fc54..a9b1f80881565a33ce22c07735fac8ed8d4b40b8 100644 (file)
@@ -2180,6 +2180,11 @@ int gnutls_handshake(gnutls_session_t session)
                    session->internals.priorities->cs.size == 0)
                        return gnutls_assert_val(GNUTLS_E_NO_PRIORITIES_WERE_SET);
 
+               ret =
+                   _gnutls_epoch_new(session, 0, NULL);
+               if (ret < 0)
+                       return gnutls_assert_val(ret);
+
                session->internals.used_exts_size = 0;
                session->internals.crt_requested = 0;
                session->internals.handshake_in_progress = 1;
@@ -2196,20 +2201,6 @@ int gnutls_handshake(gnutls_session_t session)
                return gnutls_assert_val(GNUTLS_E_HANDSHAKE_DURING_FALSE_START);
        }
 
-       ret =
-           _gnutls_epoch_get(session,
-                             session->security_parameters.epoch_next,
-                             NULL);
-       if (ret < 0) {
-               /* We assume the epoch is not allocated if _gnutls_epoch_get fails. */
-               ret =
-                   _gnutls_epoch_alloc(session,
-                                       session->security_parameters.
-                                       epoch_next, NULL);
-               if (ret < 0)
-                       return gnutls_assert_val(ret);
-       }
-
        if (session->security_parameters.entity == GNUTLS_CLIENT) {
                do {
                        ret = handshake_client(session);
@@ -2242,7 +2233,7 @@ int gnutls_handshake(gnutls_session_t session)
 
                _gnutls_handshake_internal_state_clear(session);
 
-               session->security_parameters.epoch_next++;
+               _gnutls_epoch_bump(session);
        }
 
        return 0;
index 1551fa979400d5d04b285fc3ffb07d0095dd2b6c..3a4d80ffc346d0663520099fe5e2a2826205256a 100644 (file)
@@ -242,7 +242,6 @@ void _gnutls_handshake_internal_state_clear(gnutls_session_t session)
 int gnutls_init(gnutls_session_t * session, unsigned int flags)
 {
        int ret;
-       record_parameters_st *epoch;
        
        FAIL_IF_LIB_ERROR;
 
@@ -250,16 +249,12 @@ int gnutls_init(gnutls_session_t * session, unsigned int flags)
        if (*session == NULL)
                return GNUTLS_E_MEMORY_ERROR;
 
-       ret = _gnutls_epoch_alloc(*session, 0, &epoch);
+       ret = _gnutls_epoch_new(*session, 1, NULL);
        if (ret < 0) {
-               gnutls_assert();
-               return GNUTLS_E_MEMORY_ERROR;
+               gnutls_free(*session);
+               return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
        }
-
-       /* Set all NULL algos on epoch 0 */
-       _gnutls_epoch_set_null_algos(*session, epoch);
-
-       (*session)->security_parameters.epoch_next = 1;
+       _gnutls_epoch_bump(*session);
 
        (*session)->security_parameters.entity =
            (flags & GNUTLS_SERVER ? GNUTLS_SERVER : GNUTLS_CLIENT);