/src/gnutls/lib/ext/supported_groups.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 Supported Groups extension (rfc7919). |
25 | | * This extension was previously named Supported Elliptic Curves under TLS 1.2. |
26 | | */ |
27 | | |
28 | | #include "ext/supported_groups.h" |
29 | | #include "str.h" |
30 | | #include "num.h" |
31 | | #include "auth/psk.h" |
32 | | #include "auth/cert.h" |
33 | | #include "auth/anon.h" |
34 | | #include "algorithms.h" |
35 | | #include <gnutls/gnutls.h> |
36 | | |
37 | | static int _gnutls_supported_groups_recv_params(gnutls_session_t session, |
38 | | const uint8_t *data, |
39 | | size_t data_size); |
40 | | static int _gnutls_supported_groups_send_params(gnutls_session_t session, |
41 | | gnutls_buffer_st *extdata); |
42 | | |
43 | | const hello_ext_entry_st ext_mod_supported_groups = { |
44 | | .name = "Supported Groups", |
45 | | .tls_id = 10, |
46 | | .gid = GNUTLS_EXTENSION_SUPPORTED_GROUPS, |
47 | | .client_parse_point = GNUTLS_EXT_TLS, |
48 | | .server_parse_point = GNUTLS_EXT_TLS, |
49 | | .validity = GNUTLS_EXT_FLAG_TLS | GNUTLS_EXT_FLAG_DTLS | |
50 | | GNUTLS_EXT_FLAG_CLIENT_HELLO | GNUTLS_EXT_FLAG_EE | |
51 | | GNUTLS_EXT_FLAG_TLS12_SERVER_HELLO, |
52 | | .recv_func = _gnutls_supported_groups_recv_params, |
53 | | .send_func = _gnutls_supported_groups_send_params, |
54 | | .pack_func = NULL, |
55 | | .unpack_func = NULL, |
56 | | .deinit_func = NULL, |
57 | | .cannot_be_overriden = 1 |
58 | | }; |
59 | | |
60 | | static unsigned get_min_dh(gnutls_session_t session) |
61 | 0 | { |
62 | 0 | gnutls_certificate_credentials_t cert_cred; |
63 | 0 | gnutls_psk_server_credentials_t psk_cred; |
64 | 0 | gnutls_anon_server_credentials_t anon_cred; |
65 | 0 | unsigned level = 0; |
66 | |
|
67 | 0 | cert_cred = (gnutls_certificate_credentials_t)_gnutls_get_cred( |
68 | 0 | session, GNUTLS_CRD_CERTIFICATE); |
69 | 0 | psk_cred = (gnutls_psk_server_credentials_t)_gnutls_get_cred( |
70 | 0 | session, GNUTLS_CRD_PSK); |
71 | 0 | anon_cred = (gnutls_anon_server_credentials_t)_gnutls_get_cred( |
72 | 0 | session, GNUTLS_CRD_ANON); |
73 | |
|
74 | 0 | if (cert_cred) { |
75 | 0 | level = cert_cred->dh_sec_param; |
76 | 0 | } else if (psk_cred) { |
77 | 0 | level = psk_cred->dh_sec_param; |
78 | 0 | } else if (anon_cred) { |
79 | 0 | level = anon_cred->dh_sec_param; |
80 | 0 | } |
81 | |
|
82 | 0 | if (level) |
83 | 0 | return gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, level); |
84 | | |
85 | 0 | return 0; |
86 | 0 | } |
87 | | |
88 | | /* |
89 | | * In case of a server: if a SUPPORTED_GROUPS extension type is received then it stores |
90 | | * into the session security parameters the new value. The server may use gnutls_session_certificate_type_get(), |
91 | | * to access it. |
92 | | * |
93 | | * In case of a client: If supported_eccs have been specified then we send the extension. |
94 | | * |
95 | | */ |
96 | | static int _gnutls_supported_groups_recv_params(gnutls_session_t session, |
97 | | const uint8_t *data, |
98 | | size_t data_size) |
99 | 0 | { |
100 | 0 | int i; |
101 | 0 | uint16_t len; |
102 | 0 | const uint8_t *p = data; |
103 | 0 | const gnutls_group_entry_st *group = NULL; |
104 | 0 | unsigned have_ffdhe = 0; |
105 | 0 | unsigned tls_id; |
106 | 0 | unsigned min_dh; |
107 | 0 | unsigned j; |
108 | 0 | int serv_ec_idx, serv_dh_idx; /* index in server's priority listing */ |
109 | 0 | int cli_ec_pos, cli_dh_pos; /* position in listing sent by client */ |
110 | |
|
111 | 0 | if (session->security_parameters.entity == GNUTLS_CLIENT) { |
112 | | /* A client shouldn't receive this extension in TLS1.2. It is |
113 | | * possible to read that message under TLS1.3 as an encrypted |
114 | | * extension. */ |
115 | 0 | return 0; |
116 | 0 | } else { /* SERVER SIDE - we must check if the sent supported ecc type is the right one |
117 | | */ |
118 | 0 | if (data_size < 2) |
119 | 0 | return gnutls_assert_val( |
120 | 0 | GNUTLS_E_RECEIVED_ILLEGAL_EXTENSION); |
121 | | |
122 | 0 | DECR_LEN(data_size, 2); |
123 | 0 | len = _gnutls_read_uint16(p); |
124 | 0 | p += 2; |
125 | |
|
126 | 0 | if (len % 2 != 0) |
127 | 0 | return gnutls_assert_val( |
128 | 0 | GNUTLS_E_UNEXPECTED_PACKET_LENGTH); |
129 | | |
130 | 0 | DECR_LEN(data_size, len); |
131 | | |
132 | | /* we figure what is the minimum DH allowed for this session, if any */ |
133 | 0 | min_dh = get_min_dh(session); |
134 | |
|
135 | 0 | serv_ec_idx = serv_dh_idx = -1; |
136 | 0 | cli_ec_pos = cli_dh_pos = -1; |
137 | | |
138 | | /* This extension is being processed prior to a ciphersuite being selected, |
139 | | * so we cannot rely on ciphersuite information. */ |
140 | 0 | for (i = 0; i < len; i += 2) { |
141 | 0 | if (have_ffdhe == 0 && p[i] == 0x01) |
142 | 0 | have_ffdhe = 1; |
143 | |
|
144 | 0 | tls_id = _gnutls_read_uint16(&p[i]); |
145 | 0 | group = _gnutls_tls_id_to_group(tls_id); |
146 | |
|
147 | 0 | _gnutls_handshake_log( |
148 | 0 | "EXT[%p]: Received group %s (0x%x)\n", session, |
149 | 0 | group ? group->name : "unknown", tls_id); |
150 | 0 | if (group == NULL) |
151 | 0 | continue; |
152 | | |
153 | 0 | if (min_dh > 0 && group->prime && |
154 | 0 | group->prime->size * 8 < min_dh) |
155 | 0 | continue; |
156 | | |
157 | | /* we simulate _gnutls_session_supports_group, but we prioritize if |
158 | | * %SERVER_PRECEDENCE is given */ |
159 | 0 | for (j = 0; |
160 | 0 | j < session->internals.priorities->groups.size; |
161 | 0 | j++) { |
162 | 0 | if (session->internals.priorities->groups |
163 | 0 | .entry[j] |
164 | 0 | ->id == group->id) { |
165 | 0 | if (session->internals.priorities |
166 | 0 | ->server_precedence) { |
167 | 0 | if (group->pk == GNUTLS_PK_DH) { |
168 | 0 | if (serv_dh_idx != -1 && |
169 | 0 | (int)j > |
170 | 0 | serv_dh_idx) |
171 | 0 | break; |
172 | 0 | serv_dh_idx = j; |
173 | 0 | cli_dh_pos = i; |
174 | 0 | } else if (IS_EC(group->pk)) { |
175 | 0 | if (serv_ec_idx != -1 && |
176 | 0 | (int)j > |
177 | 0 | serv_ec_idx) |
178 | 0 | break; |
179 | 0 | serv_ec_idx = j; |
180 | 0 | cli_ec_pos = i; |
181 | 0 | } |
182 | 0 | } else { |
183 | 0 | if (group->pk == GNUTLS_PK_DH) { |
184 | 0 | if (cli_dh_pos != -1) |
185 | 0 | break; |
186 | 0 | cli_dh_pos = i; |
187 | 0 | serv_dh_idx = j; |
188 | 0 | } else if (IS_EC(group->pk)) { |
189 | 0 | if (cli_ec_pos != -1) |
190 | 0 | break; |
191 | 0 | cli_ec_pos = i; |
192 | 0 | serv_ec_idx = j; |
193 | 0 | } |
194 | 0 | } |
195 | 0 | break; |
196 | 0 | } |
197 | 0 | } |
198 | 0 | } |
199 | | |
200 | | /* serv_dh/ec_pos contain the index of the groups we want to use. |
201 | | */ |
202 | 0 | if (serv_dh_idx != -1) { |
203 | 0 | session->internals.cand_dh_group = |
204 | 0 | session->internals.priorities->groups |
205 | 0 | .entry[serv_dh_idx]; |
206 | 0 | session->internals.cand_group = |
207 | 0 | session->internals.cand_dh_group; |
208 | 0 | } |
209 | |
|
210 | 0 | if (serv_ec_idx != -1) { |
211 | 0 | session->internals.cand_ec_group = |
212 | 0 | session->internals.priorities->groups |
213 | 0 | .entry[serv_ec_idx]; |
214 | 0 | if (session->internals.cand_group == NULL || |
215 | 0 | (session->internals.priorities->server_precedence && |
216 | 0 | serv_ec_idx < serv_dh_idx) || |
217 | 0 | (!session->internals.priorities->server_precedence && |
218 | 0 | cli_ec_pos < cli_dh_pos)) { |
219 | 0 | session->internals.cand_group = |
220 | 0 | session->internals.cand_ec_group; |
221 | 0 | } |
222 | 0 | } |
223 | |
|
224 | 0 | if (session->internals.cand_group) |
225 | 0 | _gnutls_handshake_log( |
226 | 0 | "EXT[%p]: Selected group %s\n", session, |
227 | 0 | session->internals.cand_group->name); |
228 | |
|
229 | 0 | if (have_ffdhe) |
230 | 0 | session->internals.hsk_flags |= HSK_HAVE_FFDHE; |
231 | 0 | } |
232 | | |
233 | 0 | return 0; |
234 | 0 | } |
235 | | |
236 | | /* returns data_size or a negative number on failure |
237 | | */ |
238 | | static int _gnutls_supported_groups_send_params(gnutls_session_t session, |
239 | | gnutls_buffer_st *extdata) |
240 | 0 | { |
241 | 0 | unsigned len, i; |
242 | 0 | int ret; |
243 | 0 | uint16_t p; |
244 | | |
245 | | /* this extension is only being sent on client side */ |
246 | 0 | if (session->security_parameters.entity == GNUTLS_CLIENT) { |
247 | 0 | len = session->internals.priorities->groups.size; |
248 | 0 | if (len > 0) { |
249 | 0 | ret = _gnutls_buffer_append_prefix(extdata, 16, |
250 | 0 | len * 2); |
251 | 0 | if (ret < 0) |
252 | 0 | return gnutls_assert_val(ret); |
253 | | |
254 | 0 | for (i = 0; i < len; i++) { |
255 | 0 | p = session->internals.priorities->groups |
256 | 0 | .entry[i] |
257 | 0 | ->tls_id; |
258 | |
|
259 | 0 | _gnutls_handshake_log( |
260 | 0 | "EXT[%p]: Sent group %s (0x%x)\n", |
261 | 0 | session, |
262 | 0 | session->internals.priorities->groups |
263 | 0 | .entry[i] |
264 | 0 | ->name, |
265 | 0 | (unsigned)p); |
266 | |
|
267 | 0 | ret = _gnutls_buffer_append_prefix(extdata, 16, |
268 | 0 | p); |
269 | 0 | if (ret < 0) |
270 | 0 | return gnutls_assert_val(ret); |
271 | 0 | } |
272 | 0 | return (len + 1) * 2; |
273 | 0 | } |
274 | 0 | } |
275 | | |
276 | 0 | return 0; |
277 | 0 | } |
278 | | |
279 | | /* Returns 0 if the given ECC curve is allowed in the current |
280 | | * session. A negative error value is returned otherwise. |
281 | | */ |
282 | | int _gnutls_session_supports_group(gnutls_session_t session, unsigned int group) |
283 | 0 | { |
284 | 0 | unsigned i; |
285 | |
|
286 | 0 | for (i = 0; i < session->internals.priorities->groups.size; i++) { |
287 | 0 | if (session->internals.priorities->groups.entry[i]->id == group) |
288 | 0 | return 0; |
289 | 0 | } |
290 | | |
291 | 0 | return GNUTLS_E_ECC_UNSUPPORTED_CURVE; |
292 | 0 | } |