LCOV - code coverage report
Current view: top level - builds/gnutls/coverage/gnutls-git/lib/tls13 - anti_replay.c (source / functions) Hit Total Coverage
Test: GnuTLS-3.6.14 Code Coverage Lines: 72 77 93.5 %
Date: 2020-10-30 04:50:48 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2018 Red Hat, Inc.
       3             :  *
       4             :  * Author: Daiki Ueno
       5             :  *
       6             :  * This file is part of GnuTLS.
       7             :  *
       8             :  * The GnuTLS is free software; you can redistribute it and/or
       9             :  * modify it under the terms of the GNU Lesser General Public License
      10             :  * as published by the Free Software Foundation; either version 2.1 of
      11             :  * the License, or (at your option) any later version.
      12             :  *
      13             :  * This library is distributed in the hope that it will be useful, but
      14             :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      16             :  * Lesser General Public License for more details.
      17             :  *
      18             :  * You should have received a copy of the GNU Lesser General Public License
      19             :  * along with this program.  If not, see <https://www.gnu.org/licenses/>
      20             :  *
      21             :  */
      22             : 
      23             : #include "gnutls_int.h"
      24             : #include "db.h"
      25             : #include "system.h"
      26             : #include "tls13/anti_replay.h"
      27             : 
      28             : /* The default time window in milliseconds; RFC8446 suggests the order
      29             :  * of ten seconds is sufficient for the clients on the Internet. */
      30             : #define DEFAULT_WINDOW_MS 10000
      31             : 
      32             : struct gnutls_anti_replay_st {
      33             :         uint32_t window;
      34             :         struct timespec start_time;
      35             :         gnutls_db_add_func db_add_func;
      36             :         void *db_ptr;
      37             : };
      38             : 
      39             : /**
      40             :  * gnutls_anti_replay_init:
      41             :  * @anti_replay: is a pointer to #gnutls_anti_replay_t type
      42             :  *
      43             :  * This function will allocate and initialize the @anti_replay context
      44             :  * to be usable for detect replay attacks. The context can then be
      45             :  * attached to a @gnutls_session_t with
      46             :  * gnutls_anti_replay_enable().
      47             :  *
      48             :  * Returns: Zero or a negative error code on error.
      49             :  *
      50             :  * Since: 3.6.5
      51             :  **/
      52             : int
      53           9 : gnutls_anti_replay_init(gnutls_anti_replay_t *anti_replay)
      54             : {
      55           9 :         *anti_replay = gnutls_calloc(1, sizeof(struct gnutls_anti_replay_st));
      56           9 :         if (!*anti_replay)
      57           0 :                 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
      58             : 
      59           9 :         (*anti_replay)->window = DEFAULT_WINDOW_MS;
      60             : 
      61           9 :         gnutls_gettime(&(*anti_replay)->start_time);
      62             : 
      63           9 :         return 0;
      64             : }
      65             : 
      66             : /**
      67             :  * gnutls_anti_replay_set_window:
      68             :  * @anti_replay: is a #gnutls_anti_replay_t type.
      69             :  * @window: is the time window recording ClientHello, in milliseconds
      70             :  *
      71             :  * Sets the time window used for ClientHello recording.  In order to
      72             :  * protect against replay attacks, the server records ClientHello
      73             :  * messages within this time period from the last update, and
      74             :  * considers it a replay when a ClientHello outside of the period; if
      75             :  * a ClientHello arrives within this period, the server checks the
      76             :  * database and detects duplicates.
      77             :  *
      78             :  * For the details of the algorithm, see RFC 8446, section 8.2.
      79             :  *
      80             :  * Since: 3.6.5
      81             :  */
      82             : void
      83           3 : gnutls_anti_replay_set_window(gnutls_anti_replay_t anti_replay,
      84             :                               unsigned int window)
      85             : {
      86           3 :         anti_replay->window = window;
      87           3 : }
      88             : 
      89             : /**
      90             :  * gnutls_anti_replay_deinit:
      91             :  * @anti_replay: is a #gnutls_anti_replay type
      92             :  *
      93             :  * This function will deinitialize all resources occupied by the given
      94             :  * anti-replay context.
      95             :  *
      96             :  * Since: 3.6.5
      97             :  **/
      98             : void
      99           7 : gnutls_anti_replay_deinit(gnutls_anti_replay_t anti_replay)
     100             : {
     101           7 :         gnutls_free(anti_replay);
     102           7 : }
     103             : 
     104             : /**
     105             :  * gnutls_anti_replay_enable:
     106             :  * @session: is a #gnutls_session_t type.
     107             :  * @anti_replay: is a #gnutls_anti_replay_t type.
     108             :  *
     109             :  * Request that the server should use anti-replay mechanism.
     110             :  *
     111             :  * Since: 3.6.5
     112             :  **/
     113             : void
     114          16 : gnutls_anti_replay_enable(gnutls_session_t session,
     115             :                           gnutls_anti_replay_t anti_replay)
     116             : {
     117          16 :         if (unlikely(session->security_parameters.entity != GNUTLS_SERVER)) {
     118           0 :                 gnutls_assert();
     119           0 :                 return;
     120             :         }
     121             : 
     122          16 :         session->internals.anti_replay = anti_replay;
     123             : }
     124             : 
     125             : int
     126          14 : _gnutls_anti_replay_check(gnutls_anti_replay_t anti_replay,
     127             :                           uint32_t client_ticket_age,
     128             :                           struct timespec *ticket_creation_time,
     129             :                           gnutls_datum_t *id)
     130             : {
     131          14 :         struct timespec now;
     132          14 :         time_t window;
     133          14 :         uint32_t server_ticket_age, diff;
     134          14 :         gnutls_datum_t key = { NULL, 0 };
     135          14 :         gnutls_datum_t entry = { NULL, 0 };
     136          14 :         unsigned char key_buffer[MAX_HASH_SIZE + 12];
     137          14 :         unsigned char entry_buffer[12]; /* magic + timestamp + expire_time */
     138          14 :         unsigned char *p;
     139          14 :         int ret;
     140             : 
     141          14 :         if (unlikely(id->size > MAX_HASH_SIZE))
     142           0 :                 return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
     143             : 
     144          14 :         gnutls_gettime(&now);
     145          14 :         server_ticket_age = timespec_sub_ms(&now, ticket_creation_time);
     146             : 
     147             :         /* It shouldn't be possible that the server's view of ticket
     148             :          * age is smaller than the client's view.
     149             :          */
     150          14 :         if (unlikely(server_ticket_age < client_ticket_age))
     151           1 :                 return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
     152             : 
     153             :         /* If ticket is created before recording has started, discard
     154             :          * reject early data.
     155             :          */
     156          13 :         if (_gnutls_timespec_cmp(ticket_creation_time,
     157             :                                  &anti_replay->start_time) < 0) {
     158           1 :                 _gnutls_handshake_log("anti_replay: ticket is created before recording has started\n");
     159           1 :                 return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
     160             :         }
     161             : 
     162             :         /* If certain amount of time (window) has elapsed, rollover
     163             :          * the recording.
     164             :          */
     165          12 :         diff = timespec_sub_ms(&now, &anti_replay->start_time);
     166          12 :         if (diff > anti_replay->window)
     167           2 :                 gnutls_gettime(&anti_replay->start_time);
     168             : 
     169             :         /* If expected_arrival_time is out of window, reject early
     170             :          * data.
     171             :          */
     172          12 :         if (server_ticket_age - client_ticket_age > anti_replay->window) {
     173           1 :                 _gnutls_handshake_log("anti_replay: server ticket age: %u, client ticket age: %u\n",
     174             :                                       server_ticket_age,
     175             :                                       client_ticket_age);
     176           1 :                 return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
     177             :         }
     178             : 
     179             :         /* Check if the ClientHello is stored in the database.
     180             :          */
     181          11 :         if (!anti_replay->db_add_func)
     182           0 :                 return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
     183             : 
     184             :         /* Create a key for database lookup, prefixing window start
     185             :          * time to ID.  Note that this shouldn't clash with session ID
     186             :          * used in TLS 1.2, because such IDs are 32 octets, while here
     187             :          * the key becomes 44+ octets.
     188             :          */
     189          11 :         p = key_buffer;
     190          11 :         _gnutls_write_uint32((uint64_t) anti_replay->start_time.tv_sec >> 32, p);
     191          11 :         p += 4;
     192          11 :         _gnutls_write_uint32(anti_replay->start_time.tv_sec & 0xFFFFFFFF, p);
     193          11 :         p += 4;
     194          11 :         _gnutls_write_uint32(anti_replay->start_time.tv_nsec, p);
     195          11 :         p += 4;
     196          11 :         memcpy(p, id->data, id->size);
     197          11 :         p += id->size;
     198          11 :         key.data = key_buffer;
     199          11 :         key.size = p - key_buffer;
     200             : 
     201             :         /* Create an entry to be stored on database if the lookup
     202             :          * failed.  This is formatted so that
     203             :          * gnutls_db_check_entry_expire_time() work.
     204             :          */
     205          11 :         p = entry_buffer;
     206          11 :         _gnutls_write_uint32(PACKED_SESSION_MAGIC, p);
     207          11 :         p += 4;
     208          11 :         _gnutls_write_uint32(now.tv_sec, p);
     209          11 :         p += 4;
     210          11 :         window = anti_replay->window / 1000;
     211          11 :         _gnutls_write_uint32(window, p);
     212          11 :         p += 4;
     213          11 :         entry.data = entry_buffer;
     214          11 :         entry.size = p - entry_buffer;
     215             : 
     216          22 :         ret = anti_replay->db_add_func(anti_replay->db_ptr,
     217          11 :                                        (uint64_t)now.tv_sec+(uint64_t)window, &key, &entry);
     218          11 :         if (ret < 0) {
     219           4 :                 _gnutls_handshake_log("anti_replay: duplicate ClientHello found\n");
     220           4 :                 return gnutls_assert_val(GNUTLS_E_EARLY_DATA_REJECTED);
     221             :         }
     222             : 
     223             :         return 0;
     224             : }
     225             : 
     226             : /**
     227             :  * gnutls_anti_replay_set_ptr:
     228             :  * @anti_replay: is a #gnutls_anti_replay_t type.
     229             :  * @ptr: is the pointer
     230             :  *
     231             :  * Sets the pointer that will be provided to db add function
     232             :  * as the first argument.
     233             :  **/
     234           9 : void gnutls_anti_replay_set_ptr(gnutls_anti_replay_t anti_replay, void *ptr)
     235             : {
     236           9 :         anti_replay->db_ptr = ptr;
     237           9 : }
     238             : 
     239             : /**
     240             :  * gnutls_anti_replay_set_add_function:
     241             :  * @anti_replay: is a #gnutls_anti_replay_t type.
     242             :  * @add_func: is the function.
     243             :  *
     244             :  * Sets the function that will be used to store an entry if it is not
     245             :  * already present in the resumed sessions database.  This function returns 0
     246             :  * if the entry is successfully stored, and a negative error code
     247             :  * otherwise.  In particular, if the entry is found in the database,
     248             :  * it returns %GNUTLS_E_DB_ENTRY_EXISTS.
     249             :  *
     250             :  * The arguments to the @add_func are:
     251             :  *  - %ptr: the pointer set with gnutls_anti_replay_set_ptr()
     252             :  *  - %exp_time: the expiration time of the entry
     253             :  *  - %key: a pointer to the key
     254             :  *  - %data: a pointer to data to store
     255             :  *
     256             :  * The data set by this function can be examined using
     257             :  * gnutls_db_check_entry_expire_time() and gnutls_db_check_entry_time().
     258             :  *
     259             :  * Since: 3.6.5
     260             :  **/
     261             : void
     262           9 : gnutls_anti_replay_set_add_function(gnutls_anti_replay_t anti_replay,
     263             :                                     gnutls_db_add_func add_func)
     264             : {
     265           9 :         anti_replay->db_add_func = add_func;
     266           9 : }

Generated by: LCOV version 1.14