Coverage Report

Created: 2026-08-14 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/lib/ext/ext_master_secret.c
Line
Count
Source
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 = GNUTLS_EXT_FLAG_TLS | GNUTLS_EXT_FLAG_DTLS |
45
        GNUTLS_EXT_FLAG_CLIENT_HELLO |
46
        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 _gnutls_ext_master_secret_recv_params(gnutls_session_t session,
72
             const uint8_t *data,
73
             size_t data_size)
74
0
{
75
0
  if ((session->internals.flags & GNUTLS_NO_DEFAULT_EXTENSIONS) ||
76
0
      session->internals.priorities->no_extensions ||
77
0
      session->internals.no_ext_master_secret != 0) {
78
0
    return 0;
79
0
  }
80
81
0
  if (data_size != 0) {
82
0
    return gnutls_assert_val(GNUTLS_E_UNEXPECTED_PACKET_LENGTH);
83
0
  }
84
#ifdef ENABLE_SSL3
85
  if (session->security_parameters.entity == GNUTLS_CLIENT) {
86
    const version_entry_st *ver = get_version(session);
87
88
    if (unlikely(ver == NULL))
89
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
90
91
    if (ver->id != GNUTLS_SSL3)
92
      session->security_parameters.ext_master_secret = 1;
93
    /* do not enable ext master secret if SSL 3.0 is the only protocol supported by server */
94
  } else if (!have_only_ssl3_enabled(session))
95
#endif
96
0
    session->security_parameters.ext_master_secret = 1;
97
98
0
  return 0;
99
0
}
100
101
/* returns data_size or a negative number on failure
102
 */
103
static int _gnutls_ext_master_secret_send_params(gnutls_session_t session,
104
             gnutls_buffer_st *extdata)
105
0
{
106
0
  if ((session->internals.flags & GNUTLS_NO_DEFAULT_EXTENSIONS) ||
107
0
      session->internals.priorities->no_extensions != 0 ||
108
0
      session->internals.no_ext_master_secret != 0) {
109
0
    session->security_parameters.ext_master_secret = 0;
110
0
    return 0;
111
0
  }
112
113
  /* this function sends the client extension data */
114
#ifdef ENABLE_SSL3
115
  if (session->security_parameters.entity == GNUTLS_CLIENT) {
116
    if (have_only_ssl3_enabled(session))
117
      return 0; /* this extension isn't available for SSL 3.0 */
118
119
    return GNUTLS_E_INT_RET_0;
120
  } else { /* server side */
121
    const version_entry_st *ver = get_version(session);
122
    if (unlikely(ver == NULL))
123
      return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
124
125
    if (ver->id != GNUTLS_SSL3 &&
126
        session->security_parameters.ext_master_secret != 0)
127
      return GNUTLS_E_INT_RET_0;
128
  }
129
130
  return 0;
131
#else
132
0
  if (session->security_parameters.entity == GNUTLS_CLIENT ||
133
0
      session->security_parameters.ext_master_secret != 0)
134
0
    return GNUTLS_E_INT_RET_0;
135
0
  return 0;
136
0
#endif
137
0
}
138
139
/**
140
 * gnutls_session_ext_master_secret_status:
141
 * @session: is a #gnutls_session_t type.
142
 *
143
 * Get the status of the extended master secret extension negotiation.
144
 * This is in accordance to RFC7627. That information is also
145
 * available to the more generic gnutls_session_get_flags().
146
 *
147
 * Returns: Non-zero if the negotiation was successful or zero otherwise.
148
 **/
149
unsigned gnutls_session_ext_master_secret_status(gnutls_session_t session)
150
0
{
151
0
  return session->security_parameters.ext_master_secret;
152
0
}