Coverage Report

Created: 2024-06-20 06:28

/src/gnutls/lib/ext/ec_point_formats.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
3
 * Copyright (C) 2017 Red Hat, Inc.
4
 *
5
 * Author: Nikos Mavrogiannopoulos
6
 *
7
 * This file is part of GnuTLS.
8
 *
9
 * The GnuTLS is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public License
11
 * as published by the Free Software Foundation; either version 2.1 of
12
 * the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful, but
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
21
 *
22
 */
23
24
/* This file contains the code for the Elliptic Curve Point Formats extension.
25
 */
26
27
#include "ext/ec_point_formats.h"
28
#include "str.h"
29
#include "state.h"
30
#include <gnutls/gnutls.h>
31
32
static int _gnutls_supported_ec_point_formats_recv_params(
33
  gnutls_session_t session, const uint8_t *data, size_t data_size);
34
static int
35
_gnutls_supported_ec_point_formats_send_params(gnutls_session_t session,
36
                 gnutls_buffer_st *extdata);
37
38
const hello_ext_entry_st ext_mod_supported_ec_point_formats = {
39
  .name = "Supported EC Point Formats",
40
  .tls_id = 11,
41
  .gid = GNUTLS_EXTENSION_SUPPORTED_EC_POINT_FORMATS,
42
  .client_parse_point = GNUTLS_EXT_TLS,
43
  .server_parse_point = GNUTLS_EXT_TLS,
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_supported_ec_point_formats_recv_params,
48
  .send_func = _gnutls_supported_ec_point_formats_send_params,
49
  .pack_func = NULL,
50
  .unpack_func = NULL,
51
  .deinit_func = NULL
52
};
53
54
/* Receive point formats
55
 */
56
static int _gnutls_supported_ec_point_formats_recv_params(
57
  gnutls_session_t session, const uint8_t *data, size_t data_size)
58
0
{
59
0
  size_t len, i;
60
0
  int uncompressed = 0;
61
62
0
  if (session->security_parameters.entity == GNUTLS_CLIENT) {
63
0
    if (data_size < 1)
64
0
      return gnutls_assert_val(
65
0
        GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION);
66
67
0
    len = data[0];
68
0
    if (len < 1)
69
0
      return gnutls_assert_val(
70
0
        GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION);
71
72
0
    DECR_LEN(data_size, len + 1);
73
74
0
    for (i = 1; i <= len; i++)
75
0
      if (data[i] == 0) { /* uncompressed */
76
0
        uncompressed = 1;
77
0
        break;
78
0
      }
79
80
0
    if (uncompressed == 0)
81
0
      return gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM);
82
0
  } else {
83
    /* only sanity check here. We only support uncompressed points
84
     * and a client must support it thus nothing to check.
85
     */
86
0
    if (data_size < 1)
87
0
      return gnutls_assert_val(
88
0
        GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION);
89
0
  }
90
91
0
  return 0;
92
0
}
93
94
/* returns data_size or a negative number on failure
95
 */
96
static int
97
_gnutls_supported_ec_point_formats_send_params(gnutls_session_t session,
98
                 gnutls_buffer_st *extdata)
99
0
{
100
0
  const uint8_t p[2] = {
101
0
    0x01, 0x00
102
0
  }; /* only support uncompressed point format */
103
0
  int ret;
104
105
0
  if (session->security_parameters.entity == GNUTLS_SERVER &&
106
0
      !_gnutls_session_is_ecc(session))
107
0
    return 0;
108
109
0
  if (session->internals.priorities->groups.size > 0) {
110
0
    ret = _gnutls_buffer_append_data(extdata, p, 2);
111
0
    if (ret < 0)
112
0
      return gnutls_assert_val(ret);
113
114
0
    return 2;
115
0
  }
116
0
  return 0;
117
0
}