Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2002-2016 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2014-2016 Nikos Mavrogiannopoulos |
4 | | * Copyright (C) 2015-2018 Red Hat, Inc. |
5 | | * |
6 | | * Author: Nikos Mavrogiannopoulos |
7 | | * |
8 | | * This file is part of GnuTLS. |
9 | | * |
10 | | * The GnuTLS is free software; you can redistribute it and/or |
11 | | * modify it under the terms of the GNU Lesser General Public License |
12 | | * as published by the Free Software Foundation; either version 2.1 of |
13 | | * the License, or (at your option) any later version. |
14 | | * |
15 | | * This library is distributed in the hope that it will be useful, but |
16 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | | * Lesser General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU Lesser General Public License |
21 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
22 | | * |
23 | | */ |
24 | | |
25 | | /* Functions to manipulate the session (gnutls_int.h), and some other stuff |
26 | | * are included here. The file's name is traditionally gnutls_state even if the |
27 | | * state has been renamed to session. |
28 | | */ |
29 | | |
30 | | #include "gnutls_int.h" |
31 | | #include "errors.h" |
32 | | #include <auth.h> |
33 | | #include <num.h> |
34 | | #include <datum.h> |
35 | | #include <db.h> |
36 | | #include <record.h> |
37 | | #include <handshake.h> |
38 | | #include <dh.h> |
39 | | #include <buffers.h> |
40 | | #include <mbuffers.h> |
41 | | #include <state.h> |
42 | | #include <constate.h> |
43 | | #include <auth/cert.h> |
44 | | #include <auth/anon.h> |
45 | | #include <auth/psk.h> |
46 | | #include <algorithms.h> |
47 | | #include <hello_ext.h> |
48 | | #include <system.h> |
49 | | #include <random.h> |
50 | | #include <fips.h> |
51 | | #include <intprops.h> |
52 | | #include <gnutls/dtls.h> |
53 | | #include "dtls.h" |
54 | | #include "tls13/session_ticket.h" |
55 | | #include "ext/cert_types.h" |
56 | | #include "locks.h" |
57 | | #include "kx.h" |
58 | | |
59 | | /* to be used by supplemental data support to disable TLS1.3 |
60 | | * when supplemental data have been globally registered */ |
61 | | unsigned _gnutls_disable_tls13 = 0; |
62 | | |
63 | | /* These should really be static, but src/tests.c calls them. Make |
64 | | them public functions? */ |
65 | | void |
66 | | _gnutls_rsa_pms_set_version(gnutls_session_t session, |
67 | | unsigned char major, unsigned char minor); |
68 | | |
69 | | /** |
70 | | * gnutls_cipher_get: |
71 | | * @session: is a #gnutls_session_t type. |
72 | | * |
73 | | * Get the currently used cipher. |
74 | | * |
75 | | * Returns: the currently used cipher, a #gnutls_cipher_algorithm_t |
76 | | * type. |
77 | | **/ |
78 | | gnutls_cipher_algorithm_t gnutls_cipher_get(gnutls_session_t session) |
79 | 0 | { |
80 | 0 | record_parameters_st *record_params; |
81 | 0 | int ret; |
82 | |
|
83 | 0 | ret = _gnutls_epoch_get(session, EPOCH_READ_CURRENT, &record_params); |
84 | 0 | if (ret < 0) |
85 | 0 | return gnutls_assert_val(GNUTLS_CIPHER_NULL); |
86 | | |
87 | 0 | return record_params->cipher->id; |
88 | 0 | } |
89 | | |
90 | | /** |
91 | | * gnutls_early_cipher_get: |
92 | | * @session: is a #gnutls_session_t type. |
93 | | * |
94 | | * Get the cipher algorithm used for encrypting early data. |
95 | | * |
96 | | * Returns: the cipher used for early data, a |
97 | | * #gnutls_cipher_algorithm_t type. |
98 | | * |
99 | | * Since: 3.7.2 |
100 | | **/ |
101 | | gnutls_cipher_algorithm_t gnutls_early_cipher_get(gnutls_session_t session) |
102 | 0 | { |
103 | 0 | const cipher_entry_st *ce; |
104 | |
|
105 | 0 | if (!(session->internals.hsk_flags & HSK_EARLY_DATA_IN_FLIGHT)) { |
106 | 0 | return gnutls_assert_val(GNUTLS_CIPHER_UNKNOWN); |
107 | 0 | } |
108 | | |
109 | 0 | if (unlikely(session->internals.resumed_security_parameters.cs == NULL)) { |
110 | 0 | return gnutls_assert_val(GNUTLS_CIPHER_UNKNOWN); |
111 | 0 | } |
112 | | |
113 | 0 | ce = cipher_to_entry(session->internals.resumed_security_parameters. |
114 | 0 | cs->block_algorithm); |
115 | 0 | if (unlikely(ce == NULL)) { |
116 | 0 | return gnutls_assert_val(GNUTLS_CIPHER_UNKNOWN); |
117 | 0 | } |
118 | | |
119 | 0 | return ce->id; |
120 | 0 | } |
121 | | |
122 | | /** |
123 | | * gnutls_certificate_type_get: |
124 | | * @session: is a #gnutls_session_t type. |
125 | | * |
126 | | * This function returns the type of the certificate that is negotiated |
127 | | * for this side to send to the peer. The certificate type is by default |
128 | | * X.509, unless an alternative certificate type is enabled by |
129 | | * gnutls_init() and negotiated during the session. |
130 | | * |
131 | | * Resumed sessions will return the certificate type that was negotiated |
132 | | * and used in the original session. |
133 | | * |
134 | | * As of version 3.6.4 it is recommended to use |
135 | | * gnutls_certificate_type_get2() which is more fine-grained. |
136 | | * |
137 | | * Returns: the currently used #gnutls_certificate_type_t certificate |
138 | | * type as negotiated for 'our' side of the connection. |
139 | | **/ |
140 | | gnutls_certificate_type_t gnutls_certificate_type_get(gnutls_session_t session) |
141 | 0 | { |
142 | 0 | return gnutls_certificate_type_get2(session, GNUTLS_CTYPE_OURS); |
143 | 0 | } |
144 | | |
145 | | /** |
146 | | * gnutls_certificate_type_get2: |
147 | | * @session: is a #gnutls_session_t type. |
148 | | * @target: is a #gnutls_ctype_target_t type. |
149 | | * |
150 | | * This function returns the type of the certificate that a side |
151 | | * is negotiated to use. The certificate type is by default X.509, |
152 | | * unless an alternative certificate type is enabled by gnutls_init() and |
153 | | * negotiated during the session. |
154 | | * |
155 | | * The @target parameter specifies whether to request the negotiated |
156 | | * certificate type for the client (%GNUTLS_CTYPE_CLIENT), |
157 | | * or for the server (%GNUTLS_CTYPE_SERVER). Additionally, in P2P mode |
158 | | * connection set up where you don't know in advance who will be client |
159 | | * and who will be server you can use the flag (%GNUTLS_CTYPE_OURS) and |
160 | | * (%GNUTLS_CTYPE_PEERS) to retrieve the corresponding certificate types. |
161 | | * |
162 | | * Resumed sessions will return the certificate type that was negotiated |
163 | | * and used in the original session. That is, this function can be used |
164 | | * to reliably determine the type of the certificate returned by |
165 | | * gnutls_certificate_get_peers(). |
166 | | * |
167 | | * Returns: the currently used #gnutls_certificate_type_t certificate |
168 | | * type for the client or the server. |
169 | | * |
170 | | * Since: 3.6.4 |
171 | | **/ |
172 | | gnutls_certificate_type_t |
173 | | gnutls_certificate_type_get2(gnutls_session_t session, |
174 | | gnutls_ctype_target_t target) |
175 | 0 | { |
176 | | /* We want to inline this function so therefore |
177 | | * we've defined it in gnutls_int.h */ |
178 | 0 | return get_certificate_type(session, target); |
179 | 0 | } |
180 | | |
181 | | /** |
182 | | * gnutls_kx_get: |
183 | | * @session: is a #gnutls_session_t type. |
184 | | * |
185 | | * Get the currently used key exchange algorithm. |
186 | | * |
187 | | * This function will return %GNUTLS_KX_ECDHE_RSA, or %GNUTLS_KX_DHE_RSA |
188 | | * under TLS 1.3, to indicate an elliptic curve DH key exchange or |
189 | | * a finite field one. The precise group used is available |
190 | | * by calling gnutls_group_get() instead. |
191 | | * |
192 | | * Returns: the key exchange algorithm used in the last handshake, a |
193 | | * #gnutls_kx_algorithm_t value. |
194 | | **/ |
195 | | gnutls_kx_algorithm_t gnutls_kx_get(gnutls_session_t session) |
196 | 0 | { |
197 | 0 | if (session->security_parameters.cs == 0) |
198 | 0 | return 0; |
199 | | |
200 | 0 | if (session->security_parameters.cs->kx_algorithm == 0) { /* TLS 1.3 */ |
201 | 0 | const version_entry_st *ver = get_version(session); |
202 | 0 | const gnutls_group_entry_st *group = get_group(session); |
203 | |
|
204 | 0 | if (ver->tls13_sem) { |
205 | 0 | if (session->internals.hsk_flags & HSK_PSK_SELECTED) { |
206 | 0 | if (group) { |
207 | 0 | if (group->pk == GNUTLS_PK_DH) |
208 | 0 | return GNUTLS_KX_DHE_PSK; |
209 | 0 | else |
210 | 0 | return GNUTLS_KX_ECDHE_PSK; |
211 | 0 | } else { |
212 | 0 | return GNUTLS_KX_PSK; |
213 | 0 | } |
214 | 0 | } else if (group) { |
215 | 0 | if (group->pk == GNUTLS_PK_DH) |
216 | 0 | return GNUTLS_KX_DHE_RSA; |
217 | 0 | else |
218 | 0 | return GNUTLS_KX_ECDHE_RSA; |
219 | 0 | } |
220 | 0 | } |
221 | 0 | } |
222 | | |
223 | 0 | return session->security_parameters.cs->kx_algorithm; |
224 | 0 | } |
225 | | |
226 | | /** |
227 | | * gnutls_mac_get: |
228 | | * @session: is a #gnutls_session_t type. |
229 | | * |
230 | | * Get the currently used MAC algorithm. |
231 | | * |
232 | | * Returns: the currently used mac algorithm, a |
233 | | * #gnutls_mac_algorithm_t value. |
234 | | **/ |
235 | | gnutls_mac_algorithm_t gnutls_mac_get(gnutls_session_t session) |
236 | 0 | { |
237 | 0 | record_parameters_st *record_params; |
238 | 0 | int ret; |
239 | |
|
240 | 0 | ret = _gnutls_epoch_get(session, EPOCH_READ_CURRENT, &record_params); |
241 | 0 | if (ret < 0) |
242 | 0 | return gnutls_assert_val(GNUTLS_MAC_NULL); |
243 | | |
244 | 0 | return record_params->mac->id; |
245 | 0 | } |
246 | | |
247 | | /** |
248 | | * gnutls_compression_get: |
249 | | * @session: is a #gnutls_session_t type. |
250 | | * |
251 | | * Get the currently used compression algorithm. |
252 | | * |
253 | | * Returns: the currently used compression method, a |
254 | | * #gnutls_compression_method_t value. |
255 | | **/ |
256 | | gnutls_compression_method_t gnutls_compression_get(gnutls_session_t session) |
257 | 0 | { |
258 | 0 | return GNUTLS_COMP_NULL; |
259 | 0 | } |
260 | | |
261 | | /** |
262 | | * gnutls_prf_hash_get: |
263 | | * @session: is a #gnutls_session_t type. |
264 | | * |
265 | | * Get the currently used hash algorithm. In TLS 1.3, the hash |
266 | | * algorithm is used for both the key derivation function and |
267 | | * handshake message authentication code. In TLS 1.2, it matches the |
268 | | * hash algorithm used for PRF. |
269 | | * |
270 | | * Returns: the currently used hash algorithm, a |
271 | | * #gnutls_digest_algorithm_t value. |
272 | | * |
273 | | * Since: 3.6.13 |
274 | | **/ |
275 | | gnutls_digest_algorithm_t gnutls_prf_hash_get(const gnutls_session_t session) |
276 | 0 | { |
277 | 0 | if (session->security_parameters.prf == NULL) |
278 | 0 | return gnutls_assert_val(GNUTLS_DIG_UNKNOWN); |
279 | | |
280 | 0 | if (session->security_parameters.prf->id >= GNUTLS_MAC_AEAD) |
281 | 0 | return gnutls_assert_val(GNUTLS_DIG_UNKNOWN); |
282 | | |
283 | 0 | return (gnutls_digest_algorithm_t) session->security_parameters.prf->id; |
284 | 0 | } |
285 | | |
286 | | /** |
287 | | * gnutls_early_prf_hash_get: |
288 | | * @session: is a #gnutls_session_t type. |
289 | | * |
290 | | * Get the hash algorithm used as a PRF to derive keys for encrypting |
291 | | * early data in TLS 1.3. |
292 | | * |
293 | | * Returns: the hash algorithm used for early data, a |
294 | | * #gnutls_digest_algorithm_t value. |
295 | | * |
296 | | * Since: 3.7.2 |
297 | | **/ |
298 | | gnutls_digest_algorithm_t |
299 | | gnutls_early_prf_hash_get(const gnutls_session_t session) |
300 | 0 | { |
301 | 0 | if (!(session->internals.hsk_flags & HSK_EARLY_DATA_IN_FLIGHT)) { |
302 | 0 | return gnutls_assert_val(GNUTLS_DIG_UNKNOWN); |
303 | 0 | } |
304 | | |
305 | 0 | if (unlikely |
306 | 0 | (session->internals.resumed_security_parameters.prf == NULL)) { |
307 | 0 | return gnutls_assert_val(GNUTLS_DIG_UNKNOWN); |
308 | 0 | } |
309 | | |
310 | 0 | if (unlikely |
311 | 0 | (session->internals.resumed_security_parameters.prf->id >= |
312 | 0 | GNUTLS_MAC_AEAD)) { |
313 | 0 | return gnutls_assert_val(GNUTLS_DIG_UNKNOWN); |
314 | 0 | } |
315 | | |
316 | 0 | return (gnutls_digest_algorithm_t) session->internals. |
317 | 0 | resumed_security_parameters.prf->id; |
318 | 0 | } |
319 | | |
320 | | /** |
321 | | * gnutls_ciphersuite_get: |
322 | | * @session: is a #gnutls_session_t type. |
323 | | * |
324 | | * Get the canonical name of negotiated TLS ciphersuite. The names |
325 | | * returned by this function match the IANA registry, with one |
326 | | * exception: |
327 | | * |
328 | | * TLS_DHE_DSS_RC4_128_SHA { 0x00, 0x66 } |
329 | | * |
330 | | * which is reserved for compatibility. |
331 | | * |
332 | | * To get a detailed description of the current ciphersuite, it is |
333 | | * recommended to use gnutls_session_get_desc(). |
334 | | * |
335 | | * Returns: a string that contains the canonical name of a TLS ciphersuite, |
336 | | * or %NULL if the handshake is not completed. |
337 | | * |
338 | | * Since: 3.7.4 |
339 | | **/ |
340 | | const char *gnutls_ciphersuite_get(gnutls_session_t session) |
341 | 0 | { |
342 | 0 | if (unlikely(session->internals.handshake_in_progress)) { |
343 | 0 | return NULL; |
344 | 0 | } |
345 | 0 | return session->security_parameters.cs->canonical_name; |
346 | 0 | } |
347 | | |
348 | | void reset_binders(gnutls_session_t session) |
349 | 0 | { |
350 | 0 | _gnutls_free_temp_key_datum(&session->key.binders[0].psk); |
351 | 0 | _gnutls_free_temp_key_datum(&session->key.binders[1].psk); |
352 | 0 | memset(session->key.binders, 0, sizeof(session->key.binders)); |
353 | 0 | } |
354 | | |
355 | | /* Check whether certificate credentials of type @cert_type are set |
356 | | * for the current session. |
357 | | */ |
358 | | static bool _gnutls_has_cert_credentials(gnutls_session_t session, |
359 | | gnutls_certificate_type_t cert_type) |
360 | 0 | { |
361 | 0 | unsigned i; |
362 | 0 | unsigned cert_found = 0; |
363 | 0 | gnutls_certificate_credentials_t cred; |
364 | | |
365 | | /* First, check for certificate credentials. If we have no certificate |
366 | | * credentials set then we don't support certificates at all. |
367 | | */ |
368 | 0 | cred = (gnutls_certificate_credentials_t) |
369 | 0 | _gnutls_get_cred(session, GNUTLS_CRD_CERTIFICATE); |
370 | |
|
371 | 0 | if (cred == NULL) |
372 | 0 | return false; |
373 | | |
374 | | /* There are credentials initialized. Now check whether we can find |
375 | | * pre-set certificates of the required type, but only if we don't |
376 | | * use the callback functions. |
377 | | */ |
378 | 0 | if (cred->get_cert_callback3 == NULL) { |
379 | 0 | for (i = 0; i < cred->ncerts; i++) { |
380 | 0 | if (cred->certs[i].cert_list[0].type == cert_type) { |
381 | 0 | cert_found = 1; |
382 | 0 | break; |
383 | 0 | } |
384 | 0 | } |
385 | |
|
386 | 0 | if (cert_found == 0) { |
387 | | /* No matching certificate found. */ |
388 | 0 | return false; |
389 | 0 | } |
390 | 0 | } |
391 | | |
392 | 0 | return true; // OK |
393 | 0 | } |
394 | | |
395 | | /* Check if the given certificate type is supported. |
396 | | * This means that it is enabled by the priority functions, |
397 | | * and in some cases a matching certificate exists. A check for |
398 | | * the latter can be toggled via the parameter @check_credentials. |
399 | | */ |
400 | | bool |
401 | | _gnutls_session_is_cert_type_supported(gnutls_session_t session, |
402 | | gnutls_certificate_type_t cert_type, |
403 | | bool check_credentials, |
404 | | gnutls_ctype_target_t target) |
405 | 0 | { |
406 | 0 | unsigned i; |
407 | 0 | priority_st *ctype_priorities; |
408 | | |
409 | | // Check whether this cert type is enabled by the application |
410 | 0 | if (!is_cert_type_enabled(session, cert_type)) |
411 | 0 | return gnutls_assert_val(GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE); |
412 | | |
413 | | // Perform a credentials check if requested |
414 | 0 | if (check_credentials) { |
415 | 0 | if (!_gnutls_has_cert_credentials(session, cert_type)) |
416 | 0 | return |
417 | 0 | gnutls_assert_val |
418 | 0 | (GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE); |
419 | 0 | } |
420 | | |
421 | | /* So far so good. We have the required credentials (if needed). |
422 | | * Now check whether we are allowed to use them according to our |
423 | | * priorities. |
424 | | */ |
425 | | // Which certificate type should we query? |
426 | 0 | switch (target) { |
427 | 0 | case GNUTLS_CTYPE_CLIENT: |
428 | 0 | ctype_priorities = |
429 | 0 | &(session->internals.priorities->client_ctype); |
430 | 0 | break; |
431 | 0 | case GNUTLS_CTYPE_SERVER: |
432 | 0 | ctype_priorities = |
433 | 0 | &(session->internals.priorities->server_ctype); |
434 | 0 | break; |
435 | 0 | default: |
436 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
437 | 0 | } |
438 | | |
439 | | // No explicit priorities set, and default ctype is asked |
440 | 0 | if (ctype_priorities->num_priorities == 0 |
441 | 0 | && cert_type == DEFAULT_CERT_TYPE) |
442 | 0 | return 0; |
443 | | |
444 | | /* Now lets find out whether our cert type is in our priority |
445 | | * list, i.e. set of allowed cert types. |
446 | | */ |
447 | 0 | for (i = 0; i < ctype_priorities->num_priorities; i++) { |
448 | 0 | if (ctype_priorities->priorities[i] == cert_type) |
449 | 0 | return 0; |
450 | 0 | } |
451 | | |
452 | 0 | return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE; |
453 | 0 | } |
454 | | |
455 | | static void deinit_keys(gnutls_session_t session) |
456 | 0 | { |
457 | 0 | const version_entry_st *vers = get_version(session); |
458 | |
|
459 | 0 | if (vers == NULL) |
460 | 0 | return; |
461 | | |
462 | 0 | gnutls_pk_params_release(&session->key.kshare.ecdhx_params); |
463 | 0 | gnutls_pk_params_release(&session->key.kshare.ecdh_params); |
464 | 0 | gnutls_pk_params_release(&session->key.kshare.dh_params); |
465 | |
|
466 | 0 | if (!vers->tls13_sem && session->key.binders[0].prf == NULL) { |
467 | 0 | gnutls_pk_params_release(&session->key.proto.tls12.ecdh.params); |
468 | 0 | gnutls_pk_params_release(&session->key.proto.tls12.dh.params); |
469 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.ecdh.x); |
470 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.ecdh.y); |
471 | 0 | _gnutls_free_temp_key_datum(&session->key.proto.tls12.ecdh.raw); |
472 | |
|
473 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.dh.client_Y); |
474 | | |
475 | | /* SRP */ |
476 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.srp.srp_p); |
477 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.srp.srp_g); |
478 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.srp.srp_key); |
479 | |
|
480 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.srp.u); |
481 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.srp.a); |
482 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.srp.x); |
483 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.srp.A); |
484 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.srp.B); |
485 | 0 | zrelease_temp_mpi_key(&session->key.proto.tls12.srp.b); |
486 | 0 | } else { |
487 | 0 | gnutls_memset(session->key.proto.tls13.temp_secret, 0, |
488 | 0 | sizeof(session->key.proto.tls13.temp_secret)); |
489 | 0 | } |
490 | |
|
491 | 0 | reset_binders(session); |
492 | 0 | _gnutls_free_temp_key_datum(&session->key.key); |
493 | 0 | } |
494 | | |
495 | | /* An internal version of _gnutls_handshake_internal_state_clear(), |
496 | | * it will not attempt to deallocate, only initialize */ |
497 | | static void handshake_internal_state_clear1(gnutls_session_t session) |
498 | 0 | { |
499 | | /* by default no selected certificate */ |
500 | 0 | session->internals.adv_version_major = 0; |
501 | 0 | session->internals.adv_version_minor = 0; |
502 | 0 | session->internals.direction = 0; |
503 | | |
504 | | /* use out of band data for the last |
505 | | * handshake messages received. |
506 | | */ |
507 | 0 | session->internals.last_handshake_in = -1; |
508 | 0 | session->internals.last_handshake_out = -1; |
509 | |
|
510 | 0 | session->internals.resumable = true; |
511 | |
|
512 | 0 | session->internals.handshake_suspicious_loops = 0; |
513 | 0 | session->internals.dtls.hsk_read_seq = 0; |
514 | 0 | session->internals.dtls.hsk_write_seq = 0; |
515 | |
|
516 | 0 | session->internals.cand_ec_group = 0; |
517 | 0 | session->internals.cand_dh_group = 0; |
518 | |
|
519 | 0 | session->internals.hrr_cs[0] = CS_INVALID_MAJOR; |
520 | 0 | session->internals.hrr_cs[1] = CS_INVALID_MINOR; |
521 | 0 | } |
522 | | |
523 | | /* This function will clear all the variables in internals |
524 | | * structure within the session, which depend on the current handshake. |
525 | | * This is used to allow further handshakes. |
526 | | */ |
527 | | void _gnutls_handshake_internal_state_clear(gnutls_session_t session) |
528 | 0 | { |
529 | 0 | handshake_internal_state_clear1(session); |
530 | |
|
531 | 0 | _gnutls_handshake_hash_buffers_clear(session); |
532 | 0 | deinit_keys(session); |
533 | |
|
534 | 0 | _gnutls_epoch_gc(session); |
535 | |
|
536 | 0 | session->internals.handshake_abs_timeout.tv_sec = 0; |
537 | 0 | session->internals.handshake_abs_timeout.tv_nsec = 0; |
538 | 0 | session->internals.handshake_in_progress = 0; |
539 | |
|
540 | 0 | session->internals.tfo.connect_addrlen = 0; |
541 | 0 | session->internals.tfo.connect_only = 0; |
542 | 0 | session->internals.early_data_received = 0; |
543 | 0 | session->internals.session_ticket_renew = 0; |
544 | 0 | } |
545 | | |
546 | | /** |
547 | | * gnutls_init: |
548 | | * @session: is a pointer to a #gnutls_session_t type. |
549 | | * @flags: indicate if this session is to be used for server or client. |
550 | | * |
551 | | * This function initializes the provided session. Every session must |
552 | | * be initialized before use, and after successful initialization and |
553 | | * use must be deinitialized by calling gnutls_deinit(). |
554 | | * |
555 | | * @flags can be any combination of flags from %gnutls_init_flags_t. |
556 | | * |
557 | | * Note that since version 3.1.2 this function enables some common |
558 | | * TLS extensions such as session tickets and OCSP certificate status |
559 | | * request in client side by default. To prevent that use the %GNUTLS_NO_EXTENSIONS |
560 | | * flag. |
561 | | * |
562 | | * Note that it is never mandatory to use gnutls_deinit() after this |
563 | | * function fails. Since gnutls 3.8.0, it is safe to unconditionally |
564 | | * use gnutls_deinit() even after failure regardless of whether the |
565 | | * memory was initialized prior to gnutls_init(); however, clients |
566 | | * wanting to be portable to older versions of the library should |
567 | | * either skip deinitialization on failure, or pre-initialize the |
568 | | * memory passed in to gnutls_init() to all zeroes via memset() or |
569 | | * similar. |
570 | | * |
571 | | * Returns: %GNUTLS_E_SUCCESS on success, or an error code. |
572 | | **/ |
573 | | int gnutls_init(gnutls_session_t * session, unsigned int flags) |
574 | 0 | { |
575 | 0 | int ret; |
576 | |
|
577 | 0 | *session = NULL; |
578 | 0 | FAIL_IF_LIB_ERROR; |
579 | | |
580 | 0 | *session = gnutls_calloc(1, sizeof(struct gnutls_session_int)); |
581 | 0 | if (*session == NULL) |
582 | 0 | return GNUTLS_E_MEMORY_ERROR; |
583 | | |
584 | 0 | ret = gnutls_mutex_init(&(*session)->internals.post_negotiation_lock); |
585 | 0 | if (ret < 0) { |
586 | 0 | gnutls_assert(); |
587 | 0 | gnutls_free(*session); |
588 | 0 | return ret; |
589 | 0 | } |
590 | | |
591 | 0 | ret = gnutls_mutex_init(&(*session)->internals.epoch_lock); |
592 | 0 | if (ret < 0) { |
593 | 0 | gnutls_assert(); |
594 | 0 | gnutls_mutex_deinit(&(*session)-> |
595 | 0 | internals.post_negotiation_lock); |
596 | 0 | gnutls_free(*session); |
597 | 0 | return ret; |
598 | 0 | } |
599 | | |
600 | 0 | ret = _gnutls_epoch_setup_next(*session, 1, NULL); |
601 | 0 | if (ret < 0) { |
602 | 0 | gnutls_mutex_deinit(&(*session)-> |
603 | 0 | internals.post_negotiation_lock); |
604 | 0 | gnutls_mutex_deinit(&(*session)->internals.epoch_lock); |
605 | 0 | gnutls_free(*session); |
606 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
607 | 0 | } |
608 | 0 | _gnutls_epoch_bump(*session); |
609 | |
|
610 | 0 | (*session)->security_parameters.entity = |
611 | 0 | (flags & GNUTLS_SERVER ? GNUTLS_SERVER : GNUTLS_CLIENT); |
612 | | |
613 | | /* the default certificate type for TLS */ |
614 | 0 | (*session)->security_parameters.client_ctype = DEFAULT_CERT_TYPE; |
615 | 0 | (*session)->security_parameters.server_ctype = DEFAULT_CERT_TYPE; |
616 | | |
617 | | /* Initialize buffers */ |
618 | 0 | _gnutls_buffer_init(&(*session)->internals.handshake_hash_buffer); |
619 | 0 | _gnutls_buffer_init(&(*session)->internals.post_handshake_hash_buffer); |
620 | 0 | _gnutls_buffer_init(&(*session)->internals.hb_remote_data); |
621 | 0 | _gnutls_buffer_init(&(*session)->internals.hb_local_data); |
622 | 0 | _gnutls_buffer_init(&(*session)->internals.record_presend_buffer); |
623 | 0 | _gnutls_buffer_init(&(*session)->internals.record_key_update_buffer); |
624 | 0 | _gnutls_buffer_init(&(*session)->internals.reauth_buffer); |
625 | |
|
626 | 0 | _mbuffer_head_init(&(*session)->internals.record_buffer); |
627 | 0 | _mbuffer_head_init(&(*session)->internals.record_send_buffer); |
628 | 0 | _mbuffer_head_init(&(*session)->internals.record_recv_buffer); |
629 | 0 | _mbuffer_head_init(&(*session)->internals.early_data_recv_buffer); |
630 | 0 | _gnutls_buffer_init(&(*session)->internals.early_data_presend_buffer); |
631 | |
|
632 | 0 | _mbuffer_head_init(&(*session)->internals.handshake_send_buffer); |
633 | 0 | _gnutls_handshake_recv_buffer_init(*session); |
634 | |
|
635 | 0 | (*session)->internals.expire_time = DEFAULT_EXPIRE_TIME; |
636 | | |
637 | | /* Ticket key rotation - set the default X to 3 times the ticket expire time */ |
638 | 0 | (*session)->key.totp.last_result = 0; |
639 | |
|
640 | 0 | gnutls_handshake_set_max_packet_length((*session), |
641 | 0 | MAX_HANDSHAKE_PACKET_SIZE); |
642 | | |
643 | | /* set the socket pointers to -1; |
644 | | */ |
645 | 0 | (*session)->internals.transport_recv_ptr = (gnutls_transport_ptr_t) - 1; |
646 | 0 | (*session)->internals.transport_send_ptr = (gnutls_transport_ptr_t) - 1; |
647 | | |
648 | | /* set the default maximum record size for TLS |
649 | | */ |
650 | 0 | (*session)->security_parameters.max_record_recv_size = |
651 | 0 | DEFAULT_MAX_RECORD_SIZE; |
652 | 0 | (*session)->security_parameters.max_record_send_size = |
653 | 0 | DEFAULT_MAX_RECORD_SIZE; |
654 | 0 | (*session)->security_parameters.max_user_record_recv_size = |
655 | 0 | DEFAULT_MAX_RECORD_SIZE; |
656 | 0 | (*session)->security_parameters.max_user_record_send_size = |
657 | 0 | DEFAULT_MAX_RECORD_SIZE; |
658 | | |
659 | | /* set the default early data size for TLS |
660 | | */ |
661 | 0 | if ((*session)->security_parameters.entity == GNUTLS_SERVER) { |
662 | 0 | (*session)->security_parameters.max_early_data_size = |
663 | 0 | DEFAULT_MAX_EARLY_DATA_SIZE; |
664 | 0 | } else { |
665 | 0 | (*session)->security_parameters.max_early_data_size = |
666 | 0 | UINT32_MAX; |
667 | 0 | } |
668 | | |
669 | | /* Everything else not initialized here is initialized as NULL |
670 | | * or 0. This is why calloc is used. However, we want to |
671 | | * ensure that certain portions of data are initialized at |
672 | | * runtime before being used. Mark such regions with a |
673 | | * valgrind client request as undefined. |
674 | | */ |
675 | 0 | _gnutls_memory_mark_undefined((*session)-> |
676 | 0 | security_parameters.master_secret, |
677 | 0 | GNUTLS_MASTER_SIZE); |
678 | 0 | _gnutls_memory_mark_undefined((*session)-> |
679 | 0 | security_parameters.client_random, |
680 | 0 | GNUTLS_RANDOM_SIZE); |
681 | 0 | _gnutls_memory_mark_undefined((*session)-> |
682 | 0 | security_parameters.server_random, |
683 | 0 | GNUTLS_RANDOM_SIZE); |
684 | 0 | _gnutls_memory_mark_undefined((*session)->key.session_ticket_key, |
685 | 0 | TICKET_MASTER_KEY_SIZE); |
686 | 0 | _gnutls_memory_mark_undefined((*session)->key.previous_ticket_key, |
687 | 0 | TICKET_MASTER_KEY_SIZE); |
688 | 0 | _gnutls_memory_mark_undefined((*session)->key.initial_stek, |
689 | 0 | TICKET_MASTER_KEY_SIZE); |
690 | |
|
691 | 0 | handshake_internal_state_clear1(*session); |
692 | |
|
693 | 0 | #ifdef MSG_NOSIGNAL |
694 | 0 | if (flags & GNUTLS_NO_SIGNAL) |
695 | 0 | gnutls_transport_set_vec_push_function(*session, |
696 | 0 | system_writev_nosignal); |
697 | 0 | else |
698 | 0 | #endif |
699 | 0 | gnutls_transport_set_vec_push_function(*session, system_writev); |
700 | 0 | (*session)->internals.pull_timeout_func = gnutls_system_recv_timeout; |
701 | 0 | (*session)->internals.pull_func = system_read; |
702 | 0 | (*session)->internals.errno_func = system_errno; |
703 | |
|
704 | 0 | (*session)->internals.saved_username = NULL; |
705 | 0 | (*session)->internals.saved_username_size = -1; |
706 | | |
707 | | /* heartbeat timeouts */ |
708 | 0 | (*session)->internals.hb_retrans_timeout_ms = 1000; |
709 | 0 | (*session)->internals.hb_total_timeout_ms = 60000; |
710 | |
|
711 | 0 | if (flags & GNUTLS_DATAGRAM) { |
712 | 0 | (*session)->internals.dtls.mtu = DTLS_DEFAULT_MTU; |
713 | 0 | (*session)->internals.transport = GNUTLS_DGRAM; |
714 | |
|
715 | 0 | gnutls_dtls_set_timeouts(*session, DTLS_RETRANS_TIMEOUT, 60000); |
716 | 0 | } else { |
717 | 0 | (*session)->internals.transport = GNUTLS_STREAM; |
718 | 0 | } |
719 | | |
720 | | /* Enable useful extensions */ |
721 | 0 | if ((flags & GNUTLS_CLIENT) && !(flags & GNUTLS_NO_EXTENSIONS)) { |
722 | 0 | #ifdef ENABLE_OCSP |
723 | 0 | if (!(flags & GNUTLS_NO_STATUS_REQUEST)) |
724 | 0 | gnutls_ocsp_status_request_enable_client(*session, NULL, |
725 | 0 | 0, NULL); |
726 | 0 | #endif |
727 | 0 | } |
728 | | |
729 | | /* session tickets in server side are enabled by setting a key */ |
730 | 0 | if (flags & GNUTLS_SERVER) |
731 | 0 | flags |= GNUTLS_NO_TICKETS; |
732 | |
|
733 | 0 | (*session)->internals.flags = flags; |
734 | |
|
735 | 0 | if (_gnutls_disable_tls13 != 0) |
736 | 0 | (*session)->internals.flags |= INT_FLAG_NO_TLS13; |
737 | | |
738 | | /* Install the default keylog function */ |
739 | 0 | gnutls_session_set_keylog_function(*session, _gnutls_nss_keylog_func); |
740 | |
|
741 | 0 | return 0; |
742 | 0 | } |
743 | | |
744 | | /** |
745 | | * gnutls_deinit: |
746 | | * @session: is a #gnutls_session_t type. |
747 | | * |
748 | | * This function clears all buffers associated with the @session. |
749 | | * This function will also remove session data from the session |
750 | | * database if the session was terminated abnormally. |
751 | | **/ |
752 | | void gnutls_deinit(gnutls_session_t session) |
753 | 0 | { |
754 | 0 | unsigned int i; |
755 | |
|
756 | 0 | if (session == NULL) |
757 | 0 | return; |
758 | | |
759 | | /* remove auth info firstly */ |
760 | 0 | _gnutls_free_auth_info(session); |
761 | |
|
762 | 0 | _gnutls_handshake_internal_state_clear(session); |
763 | 0 | _gnutls_handshake_io_buffer_clear(session); |
764 | 0 | _gnutls_hello_ext_priv_deinit(session); |
765 | |
|
766 | 0 | for (i = 0; i < MAX_EPOCH_INDEX; i++) |
767 | 0 | if (session->record_parameters[i] != NULL) { |
768 | 0 | _gnutls_epoch_free(session, |
769 | 0 | session->record_parameters[i]); |
770 | 0 | session->record_parameters[i] = NULL; |
771 | 0 | } |
772 | |
|
773 | 0 | _gnutls_buffer_clear(&session->internals.handshake_hash_buffer); |
774 | 0 | _gnutls_buffer_clear(&session->internals.post_handshake_hash_buffer); |
775 | 0 | _gnutls_buffer_clear(&session->internals.hb_remote_data); |
776 | 0 | _gnutls_buffer_clear(&session->internals.hb_local_data); |
777 | 0 | _gnutls_buffer_clear(&session->internals.record_presend_buffer); |
778 | 0 | _gnutls_buffer_clear(&session->internals.record_key_update_buffer); |
779 | 0 | _gnutls_buffer_clear(&session->internals.reauth_buffer); |
780 | |
|
781 | 0 | _mbuffer_head_clear(&session->internals.record_buffer); |
782 | 0 | _mbuffer_head_clear(&session->internals.record_recv_buffer); |
783 | 0 | _mbuffer_head_clear(&session->internals.record_send_buffer); |
784 | |
|
785 | 0 | _mbuffer_head_clear(&session->internals.early_data_recv_buffer); |
786 | 0 | _gnutls_buffer_clear(&session->internals.early_data_presend_buffer); |
787 | |
|
788 | 0 | _gnutls_free_datum(&session->internals.resumption_data); |
789 | 0 | _gnutls_free_datum(&session->internals.dtls.dcookie); |
790 | |
|
791 | 0 | for (i = 0; i < session->internals.rexts_size; i++) |
792 | 0 | gnutls_free(session->internals.rexts[i].name); |
793 | 0 | gnutls_free(session->internals.rexts); |
794 | 0 | gnutls_free(session->internals.post_handshake_cr_context.data); |
795 | |
|
796 | 0 | gnutls_free(session->internals.saved_username); |
797 | 0 | gnutls_free(session->internals.rsup); |
798 | |
|
799 | 0 | gnutls_credentials_clear(session); |
800 | 0 | _gnutls_selected_certs_deinit(session); |
801 | | |
802 | | /* destroy any session ticket we may have received */ |
803 | 0 | tls13_ticket_deinit(&session->internals.tls13_ticket); |
804 | | |
805 | | /* we rely on priorities' internal reference counting */ |
806 | 0 | gnutls_priority_deinit(session->internals.priorities); |
807 | | |
808 | | /* overwrite any temp TLS1.3 keys */ |
809 | 0 | gnutls_memset(&session->key.proto, 0, sizeof(session->key.proto)); |
810 | | |
811 | | /* clear session ticket keys */ |
812 | 0 | _gnutls_memory_mark_defined(session->key.session_ticket_key, |
813 | 0 | TICKET_MASTER_KEY_SIZE); |
814 | 0 | gnutls_memset(&session->key.session_ticket_key, 0, |
815 | 0 | TICKET_MASTER_KEY_SIZE); |
816 | 0 | _gnutls_memory_mark_undefined(session->key.session_ticket_key, |
817 | 0 | TICKET_MASTER_KEY_SIZE); |
818 | |
|
819 | 0 | _gnutls_memory_mark_defined(session->key.previous_ticket_key, |
820 | 0 | TICKET_MASTER_KEY_SIZE); |
821 | 0 | gnutls_memset(&session->key.previous_ticket_key, 0, |
822 | 0 | TICKET_MASTER_KEY_SIZE); |
823 | 0 | _gnutls_memory_mark_undefined(session->key.previous_ticket_key, |
824 | 0 | TICKET_MASTER_KEY_SIZE); |
825 | |
|
826 | 0 | _gnutls_memory_mark_defined(session->key.initial_stek, |
827 | 0 | TICKET_MASTER_KEY_SIZE); |
828 | 0 | gnutls_memset(&session->key.initial_stek, 0, TICKET_MASTER_KEY_SIZE); |
829 | 0 | _gnutls_memory_mark_undefined(session->key.initial_stek, |
830 | 0 | TICKET_MASTER_KEY_SIZE); |
831 | |
|
832 | 0 | gnutls_mutex_deinit(&session->internals.post_negotiation_lock); |
833 | 0 | gnutls_mutex_deinit(&session->internals.epoch_lock); |
834 | |
|
835 | 0 | gnutls_free(session); |
836 | 0 | } |
837 | | |
838 | | int _gnutls_dh_set_peer_public(gnutls_session_t session, bigint_t public) |
839 | 0 | { |
840 | 0 | dh_info_st *dh; |
841 | 0 | int ret; |
842 | |
|
843 | 0 | switch (gnutls_auth_get_type(session)) { |
844 | 0 | case GNUTLS_CRD_ANON: |
845 | 0 | { |
846 | 0 | anon_auth_info_t info; |
847 | 0 | info = _gnutls_get_auth_info(session, GNUTLS_CRD_ANON); |
848 | 0 | if (info == NULL) |
849 | 0 | return |
850 | 0 | gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
851 | | |
852 | 0 | dh = &info->dh; |
853 | 0 | break; |
854 | 0 | } |
855 | 0 | case GNUTLS_CRD_PSK: |
856 | 0 | { |
857 | 0 | psk_auth_info_t info; |
858 | 0 | info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK); |
859 | 0 | if (info == NULL) |
860 | 0 | return |
861 | 0 | gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
862 | | |
863 | 0 | dh = &info->dh; |
864 | 0 | break; |
865 | 0 | } |
866 | 0 | case GNUTLS_CRD_CERTIFICATE: |
867 | 0 | { |
868 | 0 | cert_auth_info_t info; |
869 | |
|
870 | 0 | info = |
871 | 0 | _gnutls_get_auth_info(session, |
872 | 0 | GNUTLS_CRD_CERTIFICATE); |
873 | 0 | if (info == NULL) |
874 | 0 | return |
875 | 0 | gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
876 | | |
877 | 0 | dh = &info->dh; |
878 | 0 | break; |
879 | 0 | } |
880 | 0 | default: |
881 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
882 | 0 | } |
883 | | |
884 | 0 | if (dh->public_key.data) |
885 | 0 | _gnutls_free_datum(&dh->public_key); |
886 | |
|
887 | 0 | ret = _gnutls_mpi_dprint_lz(public, &dh->public_key); |
888 | 0 | if (ret < 0) { |
889 | 0 | gnutls_assert(); |
890 | 0 | return ret; |
891 | 0 | } |
892 | | |
893 | 0 | return 0; |
894 | 0 | } |
895 | | |
896 | | int _gnutls_dh_set_secret_bits(gnutls_session_t session, unsigned bits) |
897 | 0 | { |
898 | 0 | switch (gnutls_auth_get_type(session)) { |
899 | 0 | case GNUTLS_CRD_ANON: |
900 | 0 | { |
901 | 0 | anon_auth_info_t info; |
902 | 0 | info = _gnutls_get_auth_info(session, GNUTLS_CRD_ANON); |
903 | 0 | if (info == NULL) |
904 | 0 | return |
905 | 0 | gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
906 | 0 | info->dh.secret_bits = bits; |
907 | 0 | break; |
908 | 0 | } |
909 | 0 | case GNUTLS_CRD_PSK: |
910 | 0 | { |
911 | 0 | psk_auth_info_t info; |
912 | 0 | info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK); |
913 | 0 | if (info == NULL) |
914 | 0 | return |
915 | 0 | gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
916 | 0 | info->dh.secret_bits = bits; |
917 | 0 | break; |
918 | 0 | } |
919 | 0 | case GNUTLS_CRD_CERTIFICATE: |
920 | 0 | { |
921 | 0 | cert_auth_info_t info; |
922 | |
|
923 | 0 | info = |
924 | 0 | _gnutls_get_auth_info(session, |
925 | 0 | GNUTLS_CRD_CERTIFICATE); |
926 | 0 | if (info == NULL) |
927 | 0 | return |
928 | 0 | gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
929 | | |
930 | 0 | info->dh.secret_bits = bits; |
931 | 0 | break; |
932 | 0 | default: |
933 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
934 | 0 | } |
935 | 0 | } |
936 | | |
937 | 0 | return 0; |
938 | 0 | } |
939 | | |
940 | | /* Sets the prime and the generator in the auth info structure. |
941 | | */ |
942 | | int |
943 | | _gnutls_dh_save_group(gnutls_session_t session, bigint_t gen, bigint_t prime) |
944 | 0 | { |
945 | 0 | dh_info_st *dh; |
946 | 0 | int ret; |
947 | |
|
948 | 0 | switch (gnutls_auth_get_type(session)) { |
949 | 0 | case GNUTLS_CRD_ANON: |
950 | 0 | { |
951 | 0 | anon_auth_info_t info; |
952 | 0 | info = _gnutls_get_auth_info(session, GNUTLS_CRD_ANON); |
953 | 0 | if (info == NULL) |
954 | 0 | return |
955 | 0 | gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
956 | | |
957 | 0 | dh = &info->dh; |
958 | 0 | break; |
959 | 0 | } |
960 | 0 | case GNUTLS_CRD_PSK: |
961 | 0 | { |
962 | 0 | psk_auth_info_t info; |
963 | 0 | info = _gnutls_get_auth_info(session, GNUTLS_CRD_PSK); |
964 | 0 | if (info == NULL) |
965 | 0 | return |
966 | 0 | gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
967 | | |
968 | 0 | dh = &info->dh; |
969 | 0 | break; |
970 | 0 | } |
971 | 0 | case GNUTLS_CRD_CERTIFICATE: |
972 | 0 | { |
973 | 0 | cert_auth_info_t info; |
974 | |
|
975 | 0 | info = |
976 | 0 | _gnutls_get_auth_info(session, |
977 | 0 | GNUTLS_CRD_CERTIFICATE); |
978 | 0 | if (info == NULL) |
979 | 0 | return |
980 | 0 | gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
981 | | |
982 | 0 | dh = &info->dh; |
983 | 0 | break; |
984 | 0 | } |
985 | 0 | default: |
986 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
987 | 0 | } |
988 | | |
989 | 0 | if (dh->prime.data) |
990 | 0 | _gnutls_free_datum(&dh->prime); |
991 | |
|
992 | 0 | if (dh->generator.data) |
993 | 0 | _gnutls_free_datum(&dh->generator); |
994 | | |
995 | | /* prime |
996 | | */ |
997 | 0 | ret = _gnutls_mpi_dprint_lz(prime, &dh->prime); |
998 | 0 | if (ret < 0) { |
999 | 0 | gnutls_assert(); |
1000 | 0 | return ret; |
1001 | 0 | } |
1002 | | |
1003 | | /* generator |
1004 | | */ |
1005 | 0 | ret = _gnutls_mpi_dprint_lz(gen, &dh->generator); |
1006 | 0 | if (ret < 0) { |
1007 | 0 | gnutls_assert(); |
1008 | 0 | _gnutls_free_datum(&dh->prime); |
1009 | 0 | return ret; |
1010 | 0 | } |
1011 | | |
1012 | 0 | return 0; |
1013 | 0 | } |
1014 | | |
1015 | | /** |
1016 | | * gnutls_certificate_send_x509_rdn_sequence: |
1017 | | * @session: a #gnutls_session_t type. |
1018 | | * @status: is 0 or 1 |
1019 | | * |
1020 | | * If status is non zero, this function will order gnutls not to send |
1021 | | * the rdnSequence in the certificate request message. That is the |
1022 | | * server will not advertise its trusted CAs to the peer. If status |
1023 | | * is zero then the default behaviour will take effect, which is to |
1024 | | * advertise the server's trusted CAs. |
1025 | | * |
1026 | | * This function has no effect in clients, and in authentication |
1027 | | * methods other than certificate with X.509 certificates. |
1028 | | **/ |
1029 | | void |
1030 | | gnutls_certificate_send_x509_rdn_sequence(gnutls_session_t session, int status) |
1031 | 0 | { |
1032 | 0 | session->internals.ignore_rdn_sequence = status; |
1033 | 0 | } |
1034 | | |
1035 | | /*- |
1036 | | * _gnutls_record_set_default_version - Used to set the default version for the first record packet |
1037 | | * @session: is a #gnutls_session_t type. |
1038 | | * @major: is a tls major version |
1039 | | * @minor: is a tls minor version |
1040 | | * |
1041 | | * This function sets the default version that we will use in the first |
1042 | | * record packet (client hello). This function is only useful to people |
1043 | | * that know TLS internals and want to debug other implementations. |
1044 | | -*/ |
1045 | | void |
1046 | | _gnutls_record_set_default_version(gnutls_session_t session, |
1047 | | unsigned char major, unsigned char minor) |
1048 | 0 | { |
1049 | 0 | session->internals.default_record_version[0] = major; |
1050 | 0 | session->internals.default_record_version[1] = minor; |
1051 | 0 | } |
1052 | | |
1053 | | /*- |
1054 | | * _gnutls_hello_set_default_version - Used to set the default version for the first record packet |
1055 | | * @session: is a #gnutls_session_t type. |
1056 | | * @major: is a tls major version |
1057 | | * @minor: is a tls minor version |
1058 | | * |
1059 | | * This function sets the default version that we will use in the first |
1060 | | * record packet (client hello). This function is only useful to people |
1061 | | * that know TLS internals and want to debug other implementations. |
1062 | | -*/ |
1063 | | void |
1064 | | _gnutls_hello_set_default_version(gnutls_session_t session, |
1065 | | unsigned char major, unsigned char minor) |
1066 | 0 | { |
1067 | 0 | session->internals.default_hello_version[0] = major; |
1068 | 0 | session->internals.default_hello_version[1] = minor; |
1069 | 0 | } |
1070 | | |
1071 | | /** |
1072 | | * gnutls_handshake_set_private_extensions: |
1073 | | * @session: is a #gnutls_session_t type. |
1074 | | * @allow: is an integer (0 or 1) |
1075 | | * |
1076 | | * This function will enable or disable the use of private cipher |
1077 | | * suites (the ones that start with 0xFF). By default or if @allow |
1078 | | * is 0 then these cipher suites will not be advertised nor used. |
1079 | | * |
1080 | | * Currently GnuTLS does not include such cipher-suites or |
1081 | | * compression algorithms. |
1082 | | * |
1083 | | * Enabling the private ciphersuites when talking to other than |
1084 | | * gnutls servers and clients may cause interoperability problems. |
1085 | | **/ |
1086 | | void |
1087 | | gnutls_handshake_set_private_extensions(gnutls_session_t session, int allow) |
1088 | 0 | { |
1089 | | /* we have no private extensions */ |
1090 | 0 | return; |
1091 | 0 | } |
1092 | | |
1093 | | /** |
1094 | | * gnutls_session_is_resumed: |
1095 | | * @session: is a #gnutls_session_t type. |
1096 | | * |
1097 | | * Checks whether session is resumed or not. This is functional |
1098 | | * for both server and client side. |
1099 | | * |
1100 | | * Returns: non zero if this session is resumed, or a zero if this is |
1101 | | * a new session. |
1102 | | **/ |
1103 | | int gnutls_session_is_resumed(gnutls_session_t session) |
1104 | 0 | { |
1105 | 0 | if (session->security_parameters.entity == GNUTLS_CLIENT) { |
1106 | 0 | const version_entry_st *ver = get_version(session); |
1107 | 0 | if (ver && ver->tls13_sem) { |
1108 | 0 | return session->internals.resumed; |
1109 | 0 | } |
1110 | | |
1111 | 0 | if (session->security_parameters.session_id_size > 0 && |
1112 | 0 | session->security_parameters.session_id_size == |
1113 | 0 | session->internals.resumed_security_parameters. |
1114 | 0 | session_id_size |
1115 | 0 | && memcmp(session->security_parameters.session_id, |
1116 | 0 | session->internals.resumed_security_parameters. |
1117 | 0 | session_id, |
1118 | 0 | session->security_parameters.session_id_size) == |
1119 | 0 | 0) |
1120 | 0 | return 1; |
1121 | 0 | } else { |
1122 | 0 | if (session->internals.resumed) |
1123 | 0 | return 1; |
1124 | 0 | } |
1125 | | |
1126 | 0 | return 0; |
1127 | 0 | } |
1128 | | |
1129 | | /** |
1130 | | * gnutls_session_resumption_requested: |
1131 | | * @session: is a #gnutls_session_t type. |
1132 | | * |
1133 | | * Check whether the client has asked for session resumption. |
1134 | | * This function is valid only on server side. |
1135 | | * |
1136 | | * Returns: non zero if session resumption was asked, or a zero if not. |
1137 | | **/ |
1138 | | int gnutls_session_resumption_requested(gnutls_session_t session) |
1139 | 0 | { |
1140 | 0 | if (session->security_parameters.entity == GNUTLS_CLIENT) { |
1141 | 0 | return 0; |
1142 | 0 | } else { |
1143 | 0 | return session->internals.resumption_requested; |
1144 | 0 | } |
1145 | 0 | } |
1146 | | |
1147 | | /*- |
1148 | | * _gnutls_session_is_psk - Used to check whether this session uses PSK kx |
1149 | | * @session: is a #gnutls_session_t type. |
1150 | | * |
1151 | | * This function will return non zero if this session uses a PSK key |
1152 | | * exchange algorithm. |
1153 | | -*/ |
1154 | | int _gnutls_session_is_psk(gnutls_session_t session) |
1155 | 0 | { |
1156 | 0 | gnutls_kx_algorithm_t kx; |
1157 | |
|
1158 | 0 | kx = session->security_parameters.cs->kx_algorithm; |
1159 | 0 | if (kx == GNUTLS_KX_PSK || kx == GNUTLS_KX_DHE_PSK |
1160 | 0 | || kx == GNUTLS_KX_RSA_PSK) |
1161 | 0 | return 1; |
1162 | | |
1163 | 0 | return 0; |
1164 | 0 | } |
1165 | | |
1166 | | /*- |
1167 | | * _gnutls_session_is_ecc - Used to check whether this session uses ECC kx |
1168 | | * @session: is a #gnutls_session_t type. |
1169 | | * |
1170 | | * This function will return non zero if this session uses an elliptic |
1171 | | * curves key exchange exchange algorithm. |
1172 | | -*/ |
1173 | | int _gnutls_session_is_ecc(gnutls_session_t session) |
1174 | 0 | { |
1175 | 0 | gnutls_kx_algorithm_t kx; |
1176 | | |
1177 | | /* We get the key exchange algorithm through the ciphersuite because |
1178 | | * the negotiated key exchange might not have been set yet. |
1179 | | */ |
1180 | 0 | kx = session->security_parameters.cs->kx_algorithm; |
1181 | |
|
1182 | 0 | return _gnutls_kx_is_ecc(kx); |
1183 | 0 | } |
1184 | | |
1185 | | /** |
1186 | | * gnutls_session_get_ptr: |
1187 | | * @session: is a #gnutls_session_t type. |
1188 | | * |
1189 | | * Get user pointer for session. Useful in callbacks. This is the |
1190 | | * pointer set with gnutls_session_set_ptr(). |
1191 | | * |
1192 | | * Returns: the user given pointer from the session structure, or |
1193 | | * %NULL if it was never set. |
1194 | | **/ |
1195 | | void *gnutls_session_get_ptr(gnutls_session_t session) |
1196 | 0 | { |
1197 | 0 | return session->internals.user_ptr; |
1198 | 0 | } |
1199 | | |
1200 | | /** |
1201 | | * gnutls_session_set_ptr: |
1202 | | * @session: is a #gnutls_session_t type. |
1203 | | * @ptr: is the user pointer |
1204 | | * |
1205 | | * This function will set (associate) the user given pointer @ptr to |
1206 | | * the session structure. This pointer can be accessed with |
1207 | | * gnutls_session_get_ptr(). |
1208 | | **/ |
1209 | | void gnutls_session_set_ptr(gnutls_session_t session, void *ptr) |
1210 | 0 | { |
1211 | 0 | session->internals.user_ptr = ptr; |
1212 | 0 | } |
1213 | | |
1214 | | /** |
1215 | | * gnutls_session_set_verify_function: |
1216 | | * @session: is a #gnutls_session_t type. |
1217 | | * @func: is the callback function |
1218 | | * |
1219 | | * This function sets a callback to be called when peer's certificate |
1220 | | * has been received in order to verify it on receipt rather than |
1221 | | * doing after the handshake is completed. This overrides any callback |
1222 | | * set using gnutls_certificate_set_verify_function(). |
1223 | | * |
1224 | | * The callback's function prototype is: |
1225 | | * int (*callback)(gnutls_session_t); |
1226 | | * |
1227 | | * If the callback function is provided then gnutls will call it, in the |
1228 | | * handshake, just after the certificate message has been received. |
1229 | | * To verify or obtain the certificate the gnutls_certificate_verify_peers2(), |
1230 | | * gnutls_certificate_type_get(), gnutls_certificate_get_peers() functions |
1231 | | * can be used. |
1232 | | * |
1233 | | * The callback function should return 0 for the handshake to continue |
1234 | | * or non-zero to terminate. |
1235 | | * |
1236 | | * Since: 3.4.6 |
1237 | | **/ |
1238 | | void gnutls_session_set_verify_function |
1239 | 0 | (gnutls_session_t session, gnutls_certificate_verify_function * func) { |
1240 | 0 | session->internals.verify_callback = func; |
1241 | 0 | } |
1242 | | |
1243 | | /** |
1244 | | * gnutls_record_get_direction: |
1245 | | * @session: is a #gnutls_session_t type. |
1246 | | * |
1247 | | * This function is useful to determine whether a GnuTLS function was interrupted |
1248 | | * while sending or receiving, so that select() or poll() may be called appropriately. |
1249 | | * |
1250 | | * It provides information about the internals of the record |
1251 | | * protocol and is only useful if a prior gnutls function call, |
1252 | | * e.g. gnutls_handshake(), was interrupted and returned |
1253 | | * %GNUTLS_E_INTERRUPTED or %GNUTLS_E_AGAIN. After such an interrupt |
1254 | | * applications may call select() or poll() before restoring the |
1255 | | * interrupted GnuTLS function. |
1256 | | * |
1257 | | * This function's output is unreliable if you are using the same |
1258 | | * @session in different threads for sending and receiving. |
1259 | | * |
1260 | | * Returns: 0 if interrupted while trying to read data, or 1 while trying to write data. |
1261 | | **/ |
1262 | | int gnutls_record_get_direction(gnutls_session_t session) |
1263 | 0 | { |
1264 | 0 | return session->internals.direction; |
1265 | 0 | } |
1266 | | |
1267 | | /*- |
1268 | | * _gnutls_rsa_pms_set_version - Sets a version to be used at the RSA PMS |
1269 | | * @session: is a #gnutls_session_t type. |
1270 | | * @major: is the major version to use |
1271 | | * @minor: is the minor version to use |
1272 | | * |
1273 | | * This function will set the given version number to be used at the |
1274 | | * RSA PMS secret. This is only useful to clients, which want to |
1275 | | * test server's capabilities. |
1276 | | -*/ |
1277 | | void |
1278 | | _gnutls_rsa_pms_set_version(gnutls_session_t session, |
1279 | | unsigned char major, unsigned char minor) |
1280 | 0 | { |
1281 | 0 | session->internals.rsa_pms_version[0] = major; |
1282 | 0 | session->internals.rsa_pms_version[1] = minor; |
1283 | 0 | } |
1284 | | |
1285 | | void _gnutls_session_client_cert_type_set(gnutls_session_t session, |
1286 | | gnutls_certificate_type_t ct) |
1287 | 0 | { |
1288 | 0 | _gnutls_handshake_log |
1289 | 0 | ("HSK[%p]: Selected client certificate type %s (%d)\n", session, |
1290 | 0 | gnutls_certificate_type_get_name(ct), ct); |
1291 | 0 | session->security_parameters.client_ctype = ct; |
1292 | 0 | } |
1293 | | |
1294 | | void _gnutls_session_server_cert_type_set(gnutls_session_t session, |
1295 | | gnutls_certificate_type_t ct) |
1296 | 0 | { |
1297 | 0 | _gnutls_handshake_log |
1298 | 0 | ("HSK[%p]: Selected server certificate type %s (%d)\n", session, |
1299 | 0 | gnutls_certificate_type_get_name(ct), ct); |
1300 | 0 | session->security_parameters.server_ctype = ct; |
1301 | 0 | } |
1302 | | |
1303 | | /** |
1304 | | * gnutls_handshake_set_post_client_hello_function: |
1305 | | * @session: is a #gnutls_session_t type. |
1306 | | * @func: is the function to be called |
1307 | | * |
1308 | | * This function will set a callback to be called after the client |
1309 | | * hello has been received (callback valid in server side only). This |
1310 | | * allows the server to adjust settings based on received extensions. |
1311 | | * |
1312 | | * Those settings could be ciphersuites, requesting certificate, or |
1313 | | * anything else except for version negotiation (this is done before |
1314 | | * the hello message is parsed). |
1315 | | * |
1316 | | * This callback must return 0 on success or a gnutls error code to |
1317 | | * terminate the handshake. |
1318 | | * |
1319 | | * Since GnuTLS 3.3.5 the callback is |
1320 | | * allowed to return %GNUTLS_E_AGAIN or %GNUTLS_E_INTERRUPTED to |
1321 | | * put the handshake on hold. In that case gnutls_handshake() |
1322 | | * will return %GNUTLS_E_INTERRUPTED and can be resumed when needed. |
1323 | | * |
1324 | | * Warning: You should not use this function to terminate the |
1325 | | * handshake based on client input unless you know what you are |
1326 | | * doing. Before the handshake is finished there is no way to know if |
1327 | | * there is a man-in-the-middle attack being performed. |
1328 | | **/ |
1329 | | void |
1330 | | gnutls_handshake_set_post_client_hello_function(gnutls_session_t session, |
1331 | | gnutls_handshake_simple_hook_func |
1332 | | func) |
1333 | 0 | { |
1334 | 0 | session->internals.user_hello_func = func; |
1335 | 0 | } |
1336 | | |
1337 | | /** |
1338 | | * gnutls_session_enable_compatibility_mode: |
1339 | | * @session: is a #gnutls_session_t type. |
1340 | | * |
1341 | | * This function can be used to disable certain (security) features in |
1342 | | * TLS in order to maintain maximum compatibility with buggy |
1343 | | * clients. Because several trade-offs with security are enabled, |
1344 | | * if required they will be reported through the audit subsystem. |
1345 | | * |
1346 | | * Normally only servers that require maximum compatibility with |
1347 | | * everything out there, need to call this function. |
1348 | | * |
1349 | | * Note that this function must be called after any call to gnutls_priority |
1350 | | * functions. |
1351 | | * |
1352 | | * Since: 2.1.4 |
1353 | | **/ |
1354 | | void gnutls_session_enable_compatibility_mode(gnutls_session_t session) |
1355 | 0 | { |
1356 | 0 | ENABLE_COMPAT(&session->internals); |
1357 | 0 | } |
1358 | | |
1359 | | /** |
1360 | | * gnutls_session_channel_binding: |
1361 | | * @session: is a #gnutls_session_t type. |
1362 | | * @cbtype: an #gnutls_channel_binding_t enumeration type |
1363 | | * @cb: output buffer array with data |
1364 | | * |
1365 | | * Extract given channel binding data of the @cbtype (e.g., |
1366 | | * %GNUTLS_CB_TLS_UNIQUE) type. |
1367 | | * |
1368 | | * Returns: %GNUTLS_E_SUCCESS on success, |
1369 | | * %GNUTLS_E_UNIMPLEMENTED_FEATURE if the @cbtype is unsupported, |
1370 | | * %GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE if the data is not |
1371 | | * currently available, or an error code. |
1372 | | * |
1373 | | * Since: 2.12.0 |
1374 | | **/ |
1375 | | int |
1376 | | gnutls_session_channel_binding(gnutls_session_t session, |
1377 | | gnutls_channel_binding_t cbtype, |
1378 | | gnutls_datum_t * cb) |
1379 | 0 | { |
1380 | 0 | if (!session->internals.initial_negotiation_completed) |
1381 | 0 | return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE; |
1382 | | |
1383 | 0 | if (cbtype == GNUTLS_CB_TLS_UNIQUE) { |
1384 | 0 | const version_entry_st *ver = get_version(session); |
1385 | 0 | if (unlikely(ver == NULL || ver->tls13_sem)) |
1386 | 0 | return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE; |
1387 | | |
1388 | 0 | cb->size = session->internals.cb_tls_unique_len; |
1389 | 0 | cb->data = gnutls_malloc(cb->size); |
1390 | 0 | if (cb->data == NULL) |
1391 | 0 | return GNUTLS_E_MEMORY_ERROR; |
1392 | | |
1393 | 0 | memcpy(cb->data, session->internals.cb_tls_unique, cb->size); |
1394 | |
|
1395 | 0 | return 0; |
1396 | 0 | } |
1397 | | |
1398 | 0 | if (cbtype == GNUTLS_CB_TLS_SERVER_END_POINT) { |
1399 | 0 | const gnutls_datum_t *ders; |
1400 | 0 | unsigned int num_certs = 1; |
1401 | 0 | int ret; |
1402 | 0 | size_t rlen; |
1403 | 0 | gnutls_x509_crt_t cert; |
1404 | 0 | gnutls_digest_algorithm_t algo; |
1405 | | |
1406 | | /* Only X509 certificates are supported for this binding type */ |
1407 | 0 | ret = gnutls_certificate_type_get(session); |
1408 | 0 | if (ret != GNUTLS_CRT_X509) |
1409 | 0 | return GNUTLS_E_UNIMPLEMENTED_FEATURE; |
1410 | | |
1411 | 0 | if (session->security_parameters.entity == GNUTLS_CLIENT) |
1412 | 0 | ders = |
1413 | 0 | gnutls_certificate_get_peers(session, &num_certs); |
1414 | 0 | else |
1415 | 0 | ders = gnutls_certificate_get_ours(session); |
1416 | | |
1417 | | /* Previous check indicated we have x509 but you never know */ |
1418 | 0 | if (!ders || num_certs == 0) |
1419 | 0 | return GNUTLS_E_UNIMPLEMENTED_FEATURE; |
1420 | | |
1421 | 0 | ret = gnutls_x509_crt_list_import(&cert, &num_certs, ders, |
1422 | 0 | GNUTLS_X509_FMT_DER, 0); |
1423 | | /* Again, this is not supposed to happen (normally) */ |
1424 | 0 | if (ret < 0 || num_certs == 0) |
1425 | 0 | return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE; |
1426 | | |
1427 | | /* Obtain signature algorithm used by certificate */ |
1428 | 0 | ret = gnutls_x509_crt_get_signature_algorithm(cert); |
1429 | 0 | if (ret < 0 || ret == GNUTLS_SIGN_UNKNOWN) |
1430 | 0 | return GNUTLS_E_UNIMPLEMENTED_FEATURE; |
1431 | | |
1432 | | /* obtain hash function from signature and normalize it */ |
1433 | 0 | algo = gnutls_sign_get_hash_algorithm(ret); |
1434 | 0 | switch (algo) { |
1435 | 0 | case GNUTLS_DIG_MD5: |
1436 | 0 | case GNUTLS_DIG_SHA1: |
1437 | 0 | algo = GNUTLS_DIG_SHA256; |
1438 | 0 | break; |
1439 | 0 | case GNUTLS_DIG_UNKNOWN: |
1440 | 0 | case GNUTLS_DIG_NULL: |
1441 | 0 | case GNUTLS_DIG_MD5_SHA1: |
1442 | | /* double hashing not supported either */ |
1443 | 0 | gnutls_x509_crt_deinit(cert); |
1444 | 0 | return GNUTLS_E_UNIMPLEMENTED_FEATURE; |
1445 | 0 | default: |
1446 | 0 | break; |
1447 | 0 | } |
1448 | | |
1449 | | /* preallocate 512 bits buffer as maximum supported digest */ |
1450 | 0 | rlen = MAX_HASH_SIZE; |
1451 | 0 | cb->data = gnutls_malloc(rlen); |
1452 | 0 | if (cb->data == NULL) { |
1453 | 0 | gnutls_x509_crt_deinit(cert); |
1454 | 0 | return GNUTLS_E_MEMORY_ERROR; |
1455 | 0 | } |
1456 | | |
1457 | 0 | ret = gnutls_x509_crt_get_fingerprint(cert, algo, cb->data, |
1458 | 0 | &rlen); |
1459 | 0 | if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) { |
1460 | 0 | cb->data = gnutls_realloc_fast(cb->data, cb->size); |
1461 | 0 | if (cb->data == NULL) { |
1462 | 0 | gnutls_x509_crt_deinit(cert); |
1463 | 0 | return GNUTLS_E_MEMORY_ERROR; |
1464 | 0 | } |
1465 | 0 | ret = gnutls_x509_crt_get_fingerprint(cert, algo, |
1466 | 0 | cb->data, &rlen); |
1467 | 0 | } |
1468 | 0 | cb->size = rlen; |
1469 | 0 | gnutls_x509_crt_deinit(cert); |
1470 | 0 | return ret; |
1471 | 0 | } |
1472 | | |
1473 | 0 | if (cbtype == GNUTLS_CB_TLS_EXPORTER) { |
1474 | 0 | #define RFC5705_LABEL_DATA "EXPORTER-Channel-Binding" |
1475 | 0 | #define RFC5705_LABEL_LEN 24 |
1476 | 0 | #define EXPORTER_CTX_DATA "" |
1477 | 0 | #define EXPORTER_CTX_LEN 0 |
1478 | |
|
1479 | 0 | const version_entry_st *ver = get_version(session); |
1480 | 0 | if (unlikely(ver == NULL)) { |
1481 | 0 | return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE; |
1482 | 0 | } |
1483 | | |
1484 | | /* "tls-exporter" channel binding is defined only when |
1485 | | * the TLS handshake results in unique master secrets, |
1486 | | * i.e., either TLS 1.3, or TLS 1.2 with extended |
1487 | | * master secret negotiated. |
1488 | | */ |
1489 | 0 | if (!ver->tls13_sem && |
1490 | 0 | gnutls_session_ext_master_secret_status(session) == 0) { |
1491 | 0 | return GNUTLS_E_CHANNEL_BINDING_NOT_AVAILABLE; |
1492 | 0 | } |
1493 | | |
1494 | 0 | cb->size = 32; |
1495 | 0 | cb->data = gnutls_malloc(cb->size); |
1496 | 0 | if (cb->data == NULL) |
1497 | 0 | return GNUTLS_E_MEMORY_ERROR; |
1498 | | |
1499 | 0 | return gnutls_prf_rfc5705(session, |
1500 | 0 | RFC5705_LABEL_LEN, RFC5705_LABEL_DATA, |
1501 | 0 | EXPORTER_CTX_LEN, EXPORTER_CTX_DATA, |
1502 | 0 | cb->size, (char *)cb->data); |
1503 | 0 | } |
1504 | | |
1505 | 0 | return GNUTLS_E_UNIMPLEMENTED_FEATURE; |
1506 | 0 | } |
1507 | | |
1508 | | /** |
1509 | | * gnutls_ecc_curve_get: |
1510 | | * @session: is a #gnutls_session_t type. |
1511 | | * |
1512 | | * Returns the currently used elliptic curve for key exchange. Only valid |
1513 | | * when using an elliptic curve ciphersuite. |
1514 | | * |
1515 | | * Returns: the currently used curve, a #gnutls_ecc_curve_t |
1516 | | * type. |
1517 | | * |
1518 | | * Since: 3.0 |
1519 | | **/ |
1520 | | gnutls_ecc_curve_t gnutls_ecc_curve_get(gnutls_session_t session) |
1521 | 0 | { |
1522 | 0 | const gnutls_group_entry_st *e; |
1523 | |
|
1524 | 0 | e = get_group(session); |
1525 | 0 | if (e == NULL || e->curve == 0) |
1526 | 0 | return 0; |
1527 | 0 | return e->curve; |
1528 | 0 | } |
1529 | | |
1530 | | /** |
1531 | | * gnutls_group_get: |
1532 | | * @session: is a #gnutls_session_t type. |
1533 | | * |
1534 | | * Returns the currently used group for key exchange. Only valid |
1535 | | * when using an elliptic curve or DH ciphersuite. |
1536 | | * |
1537 | | * Returns: the currently used group, a #gnutls_group_t |
1538 | | * type. |
1539 | | * |
1540 | | * Since: 3.6.0 |
1541 | | **/ |
1542 | | gnutls_group_t gnutls_group_get(gnutls_session_t session) |
1543 | 0 | { |
1544 | 0 | const gnutls_group_entry_st *e; |
1545 | |
|
1546 | 0 | e = get_group(session); |
1547 | 0 | if (e == NULL) |
1548 | 0 | return 0; |
1549 | 0 | return e->id; |
1550 | 0 | } |
1551 | | |
1552 | | /** |
1553 | | * gnutls_protocol_get_version: |
1554 | | * @session: is a #gnutls_session_t type. |
1555 | | * |
1556 | | * Get TLS version, a #gnutls_protocol_t value. |
1557 | | * |
1558 | | * Returns: The version of the currently used protocol. |
1559 | | **/ |
1560 | | gnutls_protocol_t gnutls_protocol_get_version(gnutls_session_t session) |
1561 | 0 | { |
1562 | 0 | return get_num_version(session); |
1563 | 0 | } |
1564 | | |
1565 | | /** |
1566 | | * gnutls_session_get_random: |
1567 | | * @session: is a #gnutls_session_t type. |
1568 | | * @client: the client part of the random |
1569 | | * @server: the server part of the random |
1570 | | * |
1571 | | * This function returns pointers to the client and server |
1572 | | * random fields used in the TLS handshake. The pointers are |
1573 | | * not to be modified or deallocated. |
1574 | | * |
1575 | | * If a client random value has not yet been established, the output |
1576 | | * will be garbage. |
1577 | | * |
1578 | | * Since: 3.0 |
1579 | | **/ |
1580 | | void |
1581 | | gnutls_session_get_random(gnutls_session_t session, |
1582 | | gnutls_datum_t * client, gnutls_datum_t * server) |
1583 | 0 | { |
1584 | 0 | if (client) { |
1585 | 0 | client->data = session->security_parameters.client_random; |
1586 | 0 | client->size = |
1587 | 0 | sizeof(session->security_parameters.client_random); |
1588 | 0 | } |
1589 | |
|
1590 | 0 | if (server) { |
1591 | 0 | server->data = session->security_parameters.server_random; |
1592 | 0 | server->size = |
1593 | 0 | sizeof(session->security_parameters.server_random); |
1594 | 0 | } |
1595 | 0 | } |
1596 | | |
1597 | | /** |
1598 | | * gnutls_session_get_master_secret: |
1599 | | * @session: is a #gnutls_session_t type. |
1600 | | * @secret: the session's master secret |
1601 | | * |
1602 | | * This function returns pointers to the master secret |
1603 | | * used in the TLS session. The pointers are not to be modified or deallocated. |
1604 | | * |
1605 | | * This function is only applicable under TLS 1.2 or earlier versions. |
1606 | | * |
1607 | | * Since: 3.5.0 |
1608 | | **/ |
1609 | | void |
1610 | | gnutls_session_get_master_secret(gnutls_session_t session, |
1611 | | gnutls_datum_t * secret) |
1612 | 0 | { |
1613 | 0 | secret->data = session->security_parameters.master_secret; |
1614 | 0 | secret->size = sizeof(session->security_parameters.master_secret); |
1615 | 0 | } |
1616 | | |
1617 | | unsigned int timespec_sub_ms(struct timespec *a, struct timespec *b) |
1618 | 0 | { |
1619 | 0 | time_t dsecs; |
1620 | |
|
1621 | 0 | dsecs = a->tv_sec - b->tv_sec; |
1622 | 0 | if (!INT_MULTIPLY_OVERFLOW(dsecs, 1000)) { |
1623 | 0 | return (dsecs * 1000 + |
1624 | 0 | (a->tv_nsec - b->tv_nsec) / (1000 * 1000)); |
1625 | 0 | } else { |
1626 | 0 | return UINT_MAX; |
1627 | 0 | } |
1628 | 0 | } |
1629 | | |
1630 | | /** |
1631 | | * gnutls_handshake_set_random: |
1632 | | * @session: is a #gnutls_session_t type. |
1633 | | * @random: a random value of 32-bytes |
1634 | | * |
1635 | | * This function will explicitly set the server or client hello |
1636 | | * random value in the subsequent TLS handshake. The random value |
1637 | | * should be a 32-byte value. |
1638 | | * |
1639 | | * Note that this function should not normally be used as gnutls |
1640 | | * will select automatically a random value for the handshake. |
1641 | | * |
1642 | | * This function should not be used when resuming a session. |
1643 | | * |
1644 | | * Returns: %GNUTLS_E_SUCCESS on success, or an error code. |
1645 | | * |
1646 | | * Since 3.1.9 |
1647 | | **/ |
1648 | | int |
1649 | | gnutls_handshake_set_random(gnutls_session_t session, |
1650 | | const gnutls_datum_t * random) |
1651 | 0 | { |
1652 | 0 | if (random->size != GNUTLS_RANDOM_SIZE) |
1653 | 0 | return GNUTLS_E_INVALID_REQUEST; |
1654 | | |
1655 | 0 | session->internals.sc_random_set = 1; |
1656 | 0 | if (session->security_parameters.entity == GNUTLS_CLIENT) |
1657 | 0 | memcpy(session->internals.resumed_security_parameters. |
1658 | 0 | client_random, random->data, random->size); |
1659 | 0 | else |
1660 | 0 | memcpy(session->internals.resumed_security_parameters. |
1661 | 0 | server_random, random->data, random->size); |
1662 | |
|
1663 | 0 | return 0; |
1664 | 0 | } |
1665 | | |
1666 | | /** |
1667 | | * gnutls_handshake_set_hook_function: |
1668 | | * @session: is a #gnutls_session_t type |
1669 | | * @htype: the %gnutls_handshake_description_t of the message to hook at |
1670 | | * @when: %GNUTLS_HOOK_* depending on when the hook function should be called |
1671 | | * @func: is the function to be called |
1672 | | * |
1673 | | * This function will set a callback to be called after or before the specified |
1674 | | * handshake message has been received or generated. This is a |
1675 | | * generalization of gnutls_handshake_set_post_client_hello_function(). |
1676 | | * |
1677 | | * To call the hook function prior to the message being generated or processed |
1678 | | * use %GNUTLS_HOOK_PRE as @when parameter, %GNUTLS_HOOK_POST to call |
1679 | | * after, and %GNUTLS_HOOK_BOTH for both cases. |
1680 | | * |
1681 | | * This callback must return 0 on success or a gnutls error code to |
1682 | | * terminate the handshake. |
1683 | | * |
1684 | | * To hook at all handshake messages use an @htype of %GNUTLS_HANDSHAKE_ANY. |
1685 | | * |
1686 | | * Warning: You should not use this function to terminate the |
1687 | | * handshake based on client input unless you know what you are |
1688 | | * doing. Before the handshake is finished there is no way to know if |
1689 | | * there is a man-in-the-middle attack being performed. |
1690 | | **/ |
1691 | | void |
1692 | | gnutls_handshake_set_hook_function(gnutls_session_t session, |
1693 | | unsigned int htype, |
1694 | | int when, gnutls_handshake_hook_func func) |
1695 | 0 | { |
1696 | 0 | session->internals.h_hook = func; |
1697 | 0 | session->internals.h_type = htype; |
1698 | 0 | session->internals.h_post = when; |
1699 | 0 | } |
1700 | | |
1701 | | /** |
1702 | | * gnutls_handshake_set_read_function: |
1703 | | * @session: is #gnutls_session_t type |
1704 | | * @func: is the function to be called |
1705 | | * |
1706 | | * This function will set a callback to be called when a handshake |
1707 | | * message is being sent. |
1708 | | * |
1709 | | * Since: 3.7.0 |
1710 | | */ |
1711 | | void |
1712 | | gnutls_handshake_set_read_function(gnutls_session_t session, |
1713 | | gnutls_handshake_read_func func) |
1714 | 0 | { |
1715 | 0 | session->internals.h_read_func = func; |
1716 | 0 | } |
1717 | | |
1718 | | /** |
1719 | | * gnutls_alert_set_read_function: |
1720 | | * @session: is #gnutls_session_t type |
1721 | | * @func: is the function to be called |
1722 | | * |
1723 | | * This function will set a callback to be called when an alert |
1724 | | * message is being sent. |
1725 | | * |
1726 | | * Since: 3.7.0 |
1727 | | */ |
1728 | | void |
1729 | | gnutls_alert_set_read_function(gnutls_session_t session, |
1730 | | gnutls_alert_read_func func) |
1731 | 0 | { |
1732 | 0 | session->internals.alert_read_func = func; |
1733 | 0 | } |
1734 | | |
1735 | | /** |
1736 | | * gnutls_record_get_state: |
1737 | | * @session: is a #gnutls_session_t type |
1738 | | * @read: if non-zero the read parameters are returned, otherwise the write |
1739 | | * @mac_key: the key used for MAC (if a MAC is used) |
1740 | | * @IV: the initialization vector or nonce used |
1741 | | * @cipher_key: the cipher key |
1742 | | * @seq_number: A 64-bit sequence number |
1743 | | * |
1744 | | * This function will return the parameters of the current record state. |
1745 | | * These are only useful to be provided to an external off-loading device |
1746 | | * or subsystem. The returned values should be considered constant |
1747 | | * and valid for the lifetime of the session. |
1748 | | * |
1749 | | * In that case, to sync the state back you must call gnutls_record_set_state(). |
1750 | | * |
1751 | | * Returns: %GNUTLS_E_SUCCESS on success, or an error code. |
1752 | | * |
1753 | | * Since 3.4.0 |
1754 | | **/ |
1755 | | int |
1756 | | gnutls_record_get_state(gnutls_session_t session, |
1757 | | unsigned read, |
1758 | | gnutls_datum_t * mac_key, |
1759 | | gnutls_datum_t * IV, |
1760 | | gnutls_datum_t * cipher_key, |
1761 | | unsigned char seq_number[8]) |
1762 | 0 | { |
1763 | 0 | record_parameters_st *record_params; |
1764 | 0 | record_state_st *record_state; |
1765 | 0 | unsigned int epoch; |
1766 | 0 | int ret; |
1767 | |
|
1768 | 0 | if (read) |
1769 | 0 | epoch = EPOCH_READ_CURRENT; |
1770 | 0 | else |
1771 | 0 | epoch = EPOCH_WRITE_CURRENT; |
1772 | |
|
1773 | 0 | ret = _gnutls_epoch_get(session, epoch, &record_params); |
1774 | 0 | if (ret < 0) |
1775 | 0 | return gnutls_assert_val(ret); |
1776 | | |
1777 | 0 | if (!record_params->initialized) |
1778 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1779 | | |
1780 | 0 | if (read) |
1781 | 0 | record_state = &record_params->read; |
1782 | 0 | else |
1783 | 0 | record_state = &record_params->write; |
1784 | |
|
1785 | 0 | if (mac_key) { |
1786 | 0 | mac_key->data = record_state->mac_key; |
1787 | 0 | mac_key->size = record_state->mac_key_size; |
1788 | 0 | } |
1789 | |
|
1790 | 0 | if (IV) { |
1791 | 0 | IV->data = record_state->iv; |
1792 | 0 | IV->size = record_state->iv_size; |
1793 | 0 | } |
1794 | |
|
1795 | 0 | if (cipher_key) { |
1796 | 0 | cipher_key->data = record_state->key; |
1797 | 0 | cipher_key->size = record_state->key_size; |
1798 | 0 | } |
1799 | |
|
1800 | 0 | if (seq_number) |
1801 | 0 | _gnutls_write_uint64(record_state->sequence_number, seq_number); |
1802 | 0 | return 0; |
1803 | 0 | } |
1804 | | |
1805 | | /** |
1806 | | * gnutls_record_set_state: |
1807 | | * @session: is a #gnutls_session_t type |
1808 | | * @read: if non-zero the read parameters are returned, otherwise the write |
1809 | | * @seq_number: A 64-bit sequence number |
1810 | | * |
1811 | | * This function will set the sequence number in the current record state. |
1812 | | * This function is useful if sending and receiving are offloaded from |
1813 | | * gnutls. That is, if gnutls_record_get_state() was used. |
1814 | | * |
1815 | | * Returns: %GNUTLS_E_SUCCESS on success, or an error code. |
1816 | | * |
1817 | | * Since 3.4.0 |
1818 | | **/ |
1819 | | int |
1820 | | gnutls_record_set_state(gnutls_session_t session, |
1821 | | unsigned read, const unsigned char seq_number[8]) |
1822 | 0 | { |
1823 | 0 | record_parameters_st *record_params; |
1824 | 0 | record_state_st *record_state; |
1825 | 0 | int epoch, ret; |
1826 | |
|
1827 | 0 | if (read) |
1828 | 0 | epoch = EPOCH_READ_CURRENT; |
1829 | 0 | else |
1830 | 0 | epoch = EPOCH_WRITE_CURRENT; |
1831 | |
|
1832 | 0 | ret = _gnutls_epoch_get(session, epoch, &record_params); |
1833 | 0 | if (ret < 0) |
1834 | 0 | return gnutls_assert_val(ret); |
1835 | | |
1836 | 0 | if (!record_params->initialized) |
1837 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1838 | | |
1839 | 0 | if (read) |
1840 | 0 | record_state = &record_params->read; |
1841 | 0 | else |
1842 | 0 | record_state = &record_params->write; |
1843 | |
|
1844 | 0 | record_state->sequence_number = _gnutls_read_uint64(seq_number); |
1845 | |
|
1846 | 0 | if (IS_DTLS(session)) { |
1847 | 0 | _dtls_reset_window(record_params); |
1848 | 0 | } |
1849 | |
|
1850 | 0 | return 0; |
1851 | 0 | } |
1852 | | |
1853 | | /** |
1854 | | * gnutls_session_get_flags: |
1855 | | * @session: is a #gnutls_session_t type. |
1856 | | * |
1857 | | * This function will return a series (ORed) of flags, applicable |
1858 | | * for the current session. |
1859 | | * |
1860 | | * This replaces individual informational functions such as |
1861 | | * gnutls_safe_renegotiation_status(), gnutls_session_ext_master_secret_status(), |
1862 | | * etc. |
1863 | | * |
1864 | | * Returns: An ORed sequence of flags (see %gnutls_session_flags_t) |
1865 | | * |
1866 | | * Since: 3.5.0 |
1867 | | **/ |
1868 | | unsigned gnutls_session_get_flags(gnutls_session_t session) |
1869 | 0 | { |
1870 | 0 | unsigned flags = 0; |
1871 | |
|
1872 | 0 | if (gnutls_safe_renegotiation_status(session)) |
1873 | 0 | flags |= GNUTLS_SFLAGS_SAFE_RENEGOTIATION; |
1874 | 0 | if (gnutls_session_ext_master_secret_status(session)) |
1875 | 0 | flags |= GNUTLS_SFLAGS_EXT_MASTER_SECRET; |
1876 | 0 | if (gnutls_session_etm_status(session)) |
1877 | 0 | flags |= GNUTLS_SFLAGS_ETM; |
1878 | 0 | if (gnutls_heartbeat_allowed(session, GNUTLS_HB_LOCAL_ALLOWED_TO_SEND)) |
1879 | 0 | flags |= GNUTLS_SFLAGS_HB_LOCAL_SEND; |
1880 | 0 | if (gnutls_heartbeat_allowed(session, GNUTLS_HB_PEER_ALLOWED_TO_SEND)) |
1881 | 0 | flags |= GNUTLS_SFLAGS_HB_PEER_SEND; |
1882 | 0 | if (session->internals.hsk_flags & HSK_FALSE_START_USED) |
1883 | 0 | flags |= GNUTLS_SFLAGS_FALSE_START; |
1884 | 0 | if ((session->internals.hsk_flags & HSK_EARLY_START_USED) && |
1885 | 0 | (session->internals.flags & GNUTLS_ENABLE_EARLY_START)) |
1886 | 0 | flags |= GNUTLS_SFLAGS_EARLY_START; |
1887 | 0 | if (session->internals.hsk_flags & HSK_USED_FFDHE) |
1888 | 0 | flags |= GNUTLS_SFLAGS_RFC7919; |
1889 | 0 | if (session->internals.hsk_flags & HSK_TICKET_RECEIVED) |
1890 | 0 | flags |= GNUTLS_SFLAGS_SESSION_TICKET; |
1891 | 0 | if (session->security_parameters.post_handshake_auth) |
1892 | 0 | flags |= GNUTLS_SFLAGS_POST_HANDSHAKE_AUTH; |
1893 | 0 | if (session->internals.hsk_flags & HSK_EARLY_DATA_ACCEPTED) |
1894 | 0 | flags |= GNUTLS_SFLAGS_EARLY_DATA; |
1895 | 0 | if (session->internals.hsk_flags & HSK_OCSP_REQUESTED) |
1896 | 0 | flags |= GNUTLS_SFLAGS_CLI_REQUESTED_OCSP; |
1897 | 0 | if (session->internals.hsk_flags & HSK_CLIENT_OCSP_REQUESTED) |
1898 | 0 | flags |= GNUTLS_SFLAGS_SERV_REQUESTED_OCSP; |
1899 | |
|
1900 | 0 | return flags; |
1901 | 0 | } |