Coverage Report

Created: 2023-03-26 08:33

/src/gnutls/lib/ext/ext_master_secret.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2014-2017 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
/* This file contains the code for the RFC7627 (ext master secret) TLS extension.
24
 */
25
26
#include "gnutls_int.h"
27
#include "errors.h"
28
#include "num.h"
29
#include <hello_ext.h>
30
#include <ext/ext_master_secret.h>
31
32
static int _gnutls_ext_master_secret_recv_params(gnutls_session_t session,
33
             const uint8_t * data,
34
             size_t data_size);
35
static int _gnutls_ext_master_secret_send_params(gnutls_session_t session,
36
             gnutls_buffer_st * extdata);
37
38
const hello_ext_entry_st ext_mod_ext_master_secret = {
39
  .name = "Extended Master Secret",
40
  .tls_id = 23,
41
  .gid = GNUTLS_EXTENSION_EXT_MASTER_SECRET,
42
  .client_parse_point = GNUTLS_EXT_MANDATORY,
43
  .server_parse_point = GNUTLS_EXT_MANDATORY,
44
  .validity =
45
      GNUTLS_EXT_FLAG_TLS | GNUTLS_EXT_FLAG_DTLS |
46
      GNUTLS_EXT_FLAG_CLIENT_HELLO | GNUTLS_EXT_FLAG_TLS12_SERVER_HELLO,
47
  .recv_func = _gnutls_ext_master_secret_recv_params,
48
  .send_func = _gnutls_ext_master_secret_send_params,
49
  .pack_func = NULL,
50
  .unpack_func = NULL,
51
  .deinit_func = NULL,
52
  .cannot_be_overriden = 1
53
};
54
55
#ifdef ENABLE_SSL3
56
static inline unsigned have_only_ssl3_enabled(gnutls_session_t session)
57
{
58
  if (session->internals.priorities->protocol.num_priorities == 1 &&
59
      session->internals.priorities->protocol.priorities[0] ==
60
      GNUTLS_SSL3)
61
    return 1;
62
  return 0;
63
}
64
#endif
65
66
/*
67
 * In case of a server: if an EXT_MASTER_SECRET extension type is received then it
68
 * sets a flag into the session security parameters.
69
 *
70
 */
71
static int
72
_gnutls_ext_master_secret_recv_params(gnutls_session_t session,
73
              const uint8_t * data, size_t _data_size)
74
0
{
75
0
  ssize_t data_size = _data_size;
76
77
0
  if ((session->internals.flags & GNUTLS_NO_EXTENSIONS) ||
78
0
      session->internals.priorities->no_extensions ||
79
0
      session->internals.no_ext_master_secret != 0) {
80
0
    return 0;
81
0
  }
82
83
0
  if (data_size != 0) {
84
0
    return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);
85
0
  }
86
#ifdef ENABLE_SSL3
87
  if (session->security_parameters.entity == GNUTLS_CLIENT) {
88
    const version_entry_st *ver = get_version(session);
89
90
    if (unlikely(ver == NULL))
91
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
92
93
    if (ver->id != GNUTLS_SSL3)
94
      session->security_parameters.ext_master_secret = 1;
95
    /* do not enable ext master secret if SSL 3.0 is the only protocol supported by server */
96
  } else if (!have_only_ssl3_enabled(session))
97
#endif
98
0
    session->security_parameters.ext_master_secret = 1;
99
100
0
  return 0;
101
0
}
102
103
/* returns data_size or a negative number on failure
104
 */
105
static int
106
_gnutls_ext_master_secret_send_params(gnutls_session_t session,
107
              gnutls_buffer_st * extdata)
108
0
{
109
0
  if ((session->internals.flags & GNUTLS_NO_EXTENSIONS) ||
110
0
      session->internals.priorities->no_extensions != 0 ||
111
0
      session->internals.no_ext_master_secret != 0) {
112
0
    session->security_parameters.ext_master_secret = 0;
113
0
    return 0;
114
0
  }
115
116
  /* this function sends the client extension data */
117
#ifdef ENABLE_SSL3
118
  if (session->security_parameters.entity == GNUTLS_CLIENT) {
119
    if (have_only_ssl3_enabled(session))
120
      return 0; /* this extension isn't available for SSL 3.0 */
121
122
    return GNUTLS_E_INT_RET_0;
123
  } else {    /* server side */
124
    const version_entry_st *ver = get_version(session);
125
    if (unlikely(ver == NULL))
126
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
127
128
    if (ver->id != GNUTLS_SSL3
129
        && session->security_parameters.ext_master_secret != 0)
130
      return GNUTLS_E_INT_RET_0;
131
  }
132
133
  return 0;
134
#else
135
0
  if (session->security_parameters.entity == GNUTLS_CLIENT ||
136
0
      session->security_parameters.ext_master_secret != 0)
137
0
    return GNUTLS_E_INT_RET_0;
138
0
  return 0;
139
0
#endif
140
0
}
141
142
/**
143
 * gnutls_session_ext_master_secret_status:
144
 * @session: is a #gnutls_session_t type.
145
 *
146
 * Get the status of the extended master secret extension negotiation.
147
 * This is in accordance to RFC7627. That information is also
148
 * available to the more generic gnutls_session_get_flags().
149
 *
150
 * Returns: Non-zero if the negotiation was successful or zero otherwise.
151
 **/
152
unsigned gnutls_session_ext_master_secret_status(gnutls_session_t session)
153
0
{
154
0
  return session->security_parameters.ext_master_secret;
155
0
}