/src/gnutls/lib/tls13/psk_ext_parser.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2017-2018 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2018 Red Hat, Inc. |
4 | | * |
5 | | * Author: Ander Juaristi, 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 | | #include "gnutls_int.h" |
25 | | #include "tls13/psk_ext_parser.h" |
26 | | |
27 | | /* Returns GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE when no identities |
28 | | * are present, or 0, on success. |
29 | | */ |
30 | | int _gnutls13_psk_ext_parser_init(psk_ext_parser_st * p, |
31 | | const unsigned char *data, size_t len) |
32 | 0 | { |
33 | 0 | if (!p || !data || !len) |
34 | 0 | return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); |
35 | | |
36 | 0 | memset(p, 0, sizeof(*p)); |
37 | |
|
38 | 0 | DECR_LEN(len, 2); |
39 | 0 | p->identities_len = _gnutls_read_uint16(data); |
40 | 0 | data += 2; |
41 | |
|
42 | 0 | if (p->identities_len == 0) |
43 | 0 | return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE); |
44 | | |
45 | 0 | p->identities_data = (unsigned char *)data; |
46 | |
|
47 | 0 | DECR_LEN(len, p->identities_len); |
48 | 0 | data += p->identities_len; |
49 | |
|
50 | 0 | DECR_LEN(len, 2); |
51 | 0 | p->binders_len = _gnutls_read_uint16(data); |
52 | 0 | data += 2; |
53 | |
|
54 | 0 | p->binders_data = data; |
55 | 0 | DECR_LEN(len, p->binders_len); |
56 | | |
57 | 0 | return 0; |
58 | 0 | } |
59 | | |
60 | | /* Extract PSK identity and move to the next iteration. |
61 | | * |
62 | | * Returns GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE when no more identities |
63 | | * are present, or 0, on success. |
64 | | */ |
65 | | int _gnutls13_psk_ext_iter_next_identity(psk_ext_iter_st * iter, |
66 | | struct psk_st *psk) |
67 | 0 | { |
68 | 0 | if (iter->identities_len == 0) |
69 | 0 | return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; |
70 | | |
71 | 0 | DECR_LEN(iter->identities_len, 2); |
72 | 0 | psk->identity.size = _gnutls_read_uint16(iter->identities_data); |
73 | 0 | if (psk->identity.size == 0) |
74 | 0 | return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER); |
75 | | |
76 | 0 | iter->identities_data += 2; |
77 | 0 | psk->identity.data = (void *)iter->identities_data; |
78 | |
|
79 | 0 | DECR_LEN(iter->identities_len, psk->identity.size); |
80 | 0 | iter->identities_data += psk->identity.size; |
81 | |
|
82 | 0 | DECR_LEN(iter->identities_len, 4); |
83 | 0 | psk->ob_ticket_age = _gnutls_read_uint32(iter->identities_data); |
84 | 0 | iter->identities_data += 4; |
85 | |
|
86 | 0 | return 0; |
87 | 0 | } |
88 | | |
89 | | /* Extract PSK binder and move to the next iteration. |
90 | | * |
91 | | * Returns GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE when no more identities |
92 | | * are present, or 0, on success. |
93 | | */ |
94 | | int _gnutls13_psk_ext_iter_next_binder(psk_ext_iter_st * iter, |
95 | | gnutls_datum_t * binder) |
96 | 0 | { |
97 | 0 | if (iter->binders_len == 0) |
98 | 0 | return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; |
99 | | |
100 | 0 | DECR_LEN(iter->binders_len, 1); |
101 | 0 | binder->size = *iter->binders_data; |
102 | 0 | if (binder->size == 0) |
103 | 0 | return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER); |
104 | | |
105 | 0 | iter->binders_data++; |
106 | 0 | binder->data = (uint8_t *) iter->binders_data; |
107 | 0 | DECR_LEN(iter->binders_len, binder->size); |
108 | 0 | iter->binders_data += binder->size; |
109 | |
|
110 | 0 | return 0; |
111 | 0 | } |