Coverage Report

Created: 2023-03-26 07:33

/src/gnutls/lib/handshake-checks.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2015-2016 Red Hat, Inc.
3
 *
4
 * Author: Nikos Mavrogiannopoulos
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
/* Functions that relate to the TLS handshake procedure.
24
 */
25
26
#include "gnutls_int.h"
27
#include "errors.h"
28
#include "debug.h"
29
#include "handshake.h"
30
#include <auth/cert.h>
31
#include "constate.h"
32
#include <record.h>
33
#include <state.h>
34
#include <ext/safe_renegotiation.h>
35
#include <auth/anon.h>    /* for gnutls_anon_server_credentials_t */
36
#include <auth/psk.h>   /* for gnutls_psk_server_credentials_t */
37
#ifdef ENABLE_SRP
38
# include <auth/srp_kx.h>
39
#endif
40
41
int _gnutls_check_id_for_change(gnutls_session_t session)
42
0
{
43
0
  int cred_type;
44
45
  /* This checks in PSK and SRP ciphersuites that the username remained the
46
   * same on a rehandshake. */
47
0
  if (session->internals.flags & GNUTLS_ALLOW_ID_CHANGE)
48
0
    return 0;
49
50
0
  cred_type = gnutls_auth_get_type(session);
51
0
  if (cred_type == GNUTLS_CRD_PSK || cred_type == GNUTLS_CRD_SRP) {
52
0
    const char *username = NULL;
53
0
    int username_length;
54
55
0
    if (cred_type == GNUTLS_CRD_PSK) {
56
0
      psk_auth_info_t ai;
57
58
0
      ai = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK);
59
0
      if (ai == NULL)
60
0
        return
61
0
            gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
62
63
0
      username = ai->username;
64
0
      username_length = ai->username_len;
65
#ifdef ENABLE_SRP
66
    } else {
67
      srp_server_auth_info_t ai =
68
          _gnutls_get_auth_info(session, GNUTLS_CRD_SRP);
69
      if (ai == NULL)
70
        return
71
            gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
72
73
      username = ai->username;
74
      username_length = strlen(ai->username);
75
#endif
76
0
    }
77
78
0
    if (username == NULL)
79
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
80
81
0
    if (session->internals.saved_username &&
82
0
        session->internals.saved_username_size != -1) {
83
0
      if (session->internals.saved_username_size ==
84
0
          username_length
85
0
          && strncmp(session->internals.saved_username,
86
0
               username, username_length)) {
87
0
        _gnutls_debug_log
88
0
            ("Session's PSK username changed during rehandshake; aborting!\n");
89
0
        return
90
0
            gnutls_assert_val
91
0
            (GNUTLS_E_SESSION_USER_ID_CHANGED);
92
0
      }
93
0
    } else if (session->internals.saved_username == NULL &&
94
0
         session->internals.saved_username_size == -1) {
95
0
      if (username_length > MAX_USERNAME_SIZE)
96
0
        return
97
0
            gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
98
0
      char *tmp = gnutls_malloc(username_length + 1);
99
0
      if (tmp == NULL)
100
0
        return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
101
0
      memcpy(tmp, username, username_length);
102
0
      tmp[username_length] = '\0';
103
0
      session->internals.saved_username = tmp;
104
0
      session->internals.saved_username_size =
105
0
          username_length;
106
0
    } else
107
0
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
108
109
0
  }
110
111
0
  return 0;
112
0
}
113
114
int _gnutls_check_if_cert_hash_is_same(gnutls_session_t session,
115
               gnutls_certificate_credentials_t cred)
116
0
{
117
0
  cert_auth_info_t ai;
118
0
  char tmp[32];
119
0
  int ret;
120
121
0
  if (session->internals.flags & GNUTLS_ALLOW_ID_CHANGE)
122
0
    return 0;
123
124
0
  ai = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
125
0
  if (ai == NULL || ai->ncerts == 0)
126
0
    return 0;
127
128
0
  ret = gnutls_hash_fast(GNUTLS_DIG_SHA256,
129
0
             ai->raw_certificate_list[0].data,
130
0
             ai->raw_certificate_list[0].size, tmp);
131
0
  if (ret < 0)
132
0
    return gnutls_assert_val(ret);
133
134
0
  if (session->internals.cert_hash_set) {
135
0
    if (memcmp(tmp, session->internals.cert_hash, 32) != 0) {
136
0
      _gnutls_debug_log
137
0
          ("Session certificate changed during rehandshake; aborting!\n");
138
0
      return
139
0
          gnutls_assert_val(GNUTLS_E_SESSION_USER_ID_CHANGED);
140
0
    }
141
0
  } else {
142
0
    memcpy(session->internals.cert_hash, tmp, 32);
143
0
    session->internals.cert_hash_set = 1;
144
0
  }
145
146
0
  return 0;
147
0
}