/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(gnutls_session_t |
33 | | session, |
34 | | const uint8_t * data, |
35 | | size_t data_size); |
36 | | static int _gnutls_supported_ec_point_formats_send_params(gnutls_session_t |
37 | | session, |
38 | | gnutls_buffer_st * |
39 | | extdata); |
40 | | |
41 | | const hello_ext_entry_st ext_mod_supported_ec_point_formats = { |
42 | | .name = "Supported EC Point Formats", |
43 | | .tls_id = 11, |
44 | | .gid = GNUTLS_EXTENSION_SUPPORTED_EC_POINT_FORMATS, |
45 | | .client_parse_point = GNUTLS_EXT_TLS, |
46 | | .server_parse_point = GNUTLS_EXT_TLS, |
47 | | .validity = GNUTLS_EXT_FLAG_TLS | GNUTLS_EXT_FLAG_DTLS | |
48 | | GNUTLS_EXT_FLAG_CLIENT_HELLO | GNUTLS_EXT_FLAG_TLS12_SERVER_HELLO, |
49 | | .recv_func = _gnutls_supported_ec_point_formats_recv_params, |
50 | | .send_func = _gnutls_supported_ec_point_formats_send_params, |
51 | | .pack_func = NULL, |
52 | | .unpack_func = NULL, |
53 | | .deinit_func = NULL |
54 | | }; |
55 | | |
56 | | /* Receive point formats |
57 | | */ |
58 | | static int |
59 | | _gnutls_supported_ec_point_formats_recv_params(gnutls_session_t session, |
60 | | const uint8_t * data, |
61 | | size_t data_size) |
62 | 0 | { |
63 | 0 | size_t len, i; |
64 | 0 | int uncompressed = 0; |
65 | |
|
66 | 0 | if (session->security_parameters.entity == GNUTLS_CLIENT) { |
67 | 0 | if (data_size < 1) |
68 | 0 | return |
69 | 0 | gnutls_assert_val |
70 | 0 | (GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION); |
71 | | |
72 | 0 | len = data[0]; |
73 | 0 | if (len < 1) |
74 | 0 | return |
75 | 0 | gnutls_assert_val |
76 | 0 | (GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION); |
77 | | |
78 | 0 | DECR_LEN(data_size, len + 1); |
79 | | |
80 | 0 | for (i = 1; i <= len; i++) |
81 | 0 | if (data[i] == 0) { /* uncompressed */ |
82 | 0 | uncompressed = 1; |
83 | 0 | break; |
84 | 0 | } |
85 | |
|
86 | 0 | if (uncompressed == 0) |
87 | 0 | return gnutls_assert_val(GNUTLS_E_UNKNOWN_PK_ALGORITHM); |
88 | 0 | } else { |
89 | | /* only sanity check here. We only support uncompressed points |
90 | | * and a client must support it thus nothing to check. |
91 | | */ |
92 | 0 | if (data_size < 1) |
93 | 0 | return |
94 | 0 | gnutls_assert_val |
95 | 0 | (GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION); |
96 | 0 | } |
97 | | |
98 | 0 | return 0; |
99 | 0 | } |
100 | | |
101 | | /* returns data_size or a negative number on failure |
102 | | */ |
103 | | static int |
104 | | _gnutls_supported_ec_point_formats_send_params(gnutls_session_t session, |
105 | | gnutls_buffer_st * extdata) |
106 | 0 | { |
107 | 0 | const uint8_t p[2] = { 0x01, 0x00 }; /* only support uncompressed point format */ |
108 | 0 | int ret; |
109 | |
|
110 | 0 | if (session->security_parameters.entity == GNUTLS_SERVER |
111 | 0 | && !_gnutls_session_is_ecc(session)) |
112 | 0 | return 0; |
113 | | |
114 | 0 | if (session->internals.priorities->groups.size > 0) { |
115 | 0 | ret = _gnutls_buffer_append_data(extdata, p, 2); |
116 | 0 | if (ret < 0) |
117 | 0 | return gnutls_assert_val(ret); |
118 | | |
119 | 0 | return 2; |
120 | 0 | } |
121 | 0 | return 0; |
122 | 0 | } |