Line data Source code
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 2172 : int _gnutls13_psk_ext_parser_init(psk_ext_parser_st *p, 31 : const unsigned char *data, size_t len) 32 : { 33 2172 : if (!p || !data || !len) 34 0 : return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR); 35 : 36 2172 : memset(p, 0, sizeof(*p)); 37 : 38 2172 : DECR_LEN(len, 2); 39 2172 : p->identities_len = _gnutls_read_uint16(data); 40 2172 : data += 2; 41 : 42 2172 : if (p->identities_len == 0) 43 0 : return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE); 44 : 45 2172 : p->identities_data = (unsigned char *) data; 46 : 47 2172 : DECR_LEN(len, p->identities_len); 48 2172 : data += p->identities_len; 49 : 50 2172 : DECR_LEN(len, 2); 51 2172 : p->binders_len = _gnutls_read_uint16(data); 52 2172 : data += 2; 53 : 54 2172 : p->binders_data = data; 55 2172 : DECR_LEN(len, p->binders_len); 56 : 57 : return 0; 58 : } 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 2201 : int _gnutls13_psk_ext_iter_next_identity(psk_ext_iter_st *iter, 66 : struct psk_st *psk) 67 : { 68 2201 : if (iter->identities_len == 0) 69 : return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; 70 : 71 2183 : DECR_LEN(iter->identities_len, 2); 72 2183 : psk->identity.size = _gnutls_read_uint16(iter->identities_data); 73 2183 : if (psk->identity.size == 0) 74 0 : return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER); 75 : 76 2183 : iter->identities_data += 2; 77 2183 : psk->identity.data = (void*)iter->identities_data; 78 : 79 2183 : DECR_LEN(iter->identities_len, psk->identity.size); 80 2183 : iter->identities_data += psk->identity.size; 81 : 82 2183 : DECR_LEN(iter->identities_len, 4); 83 2183 : psk->ob_ticket_age = _gnutls_read_uint32(iter->identities_data); 84 2183 : iter->identities_data += 4; 85 : 86 2183 : return 0; 87 : } 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 2100 : int _gnutls13_psk_ext_iter_next_binder(psk_ext_iter_st *iter, 95 : gnutls_datum_t *binder) 96 : { 97 2100 : if (iter->binders_len == 0) 98 : return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; 99 : 100 2099 : DECR_LEN(iter->binders_len, 1); 101 2099 : binder->size = *iter->binders_data; 102 2099 : if (binder->size == 0) 103 0 : return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER); 104 : 105 2099 : iter->binders_data++; 106 2099 : binder->data = (uint8_t *)iter->binders_data; 107 2099 : DECR_LEN(iter->binders_len, binder->size); 108 2099 : iter->binders_data += binder->size; 109 : 110 2099 : return 0; 111 : }