Blame lib/tls13/psk_ext_parser.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit aea12f
 * Copyright (C) 2018 Red Hat, Inc.
Packit aea12f
 *
Packit aea12f
 * Author: Ander Juaristi, Nikos Mavrogiannopoulos
Packit aea12f
 *
Packit aea12f
 * This file is part of GnuTLS.
Packit aea12f
 *
Packit aea12f
 * The GnuTLS is free software; you can redistribute it and/or
Packit aea12f
 * modify it under the terms of the GNU Lesser General Public License
Packit aea12f
 * as published by the Free Software Foundation; either version 2.1 of
Packit aea12f
 * the License, or (at your option) any later version.
Packit aea12f
 *
Packit aea12f
 * This library is distributed in the hope that it will be useful, but
Packit aea12f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit aea12f
 * Lesser General Public License for more details.
Packit aea12f
 *
Packit aea12f
 * You should have received a copy of the GNU Lesser General Public License
Packit aea12f
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit aea12f
 *
Packit aea12f
 */
Packit aea12f
Packit aea12f
#include "gnutls_int.h"
Packit aea12f
#include "tls13/psk_ext_parser.h"
Packit aea12f
Packit aea12f
/* Returns GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE when no identities
Packit aea12f
 * are present, or 0, on success.
Packit aea12f
 */
Packit aea12f
int _gnutls13_psk_ext_parser_init(psk_ext_parser_st *p,
Packit 5407aa
				  const unsigned char *data, size_t len)
Packit aea12f
{
Packit aea12f
	if (!p || !data || !len)
Packit aea12f
		return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
Packit aea12f
Packit aea12f
	memset(p, 0, sizeof(*p));
Packit aea12f
Packit aea12f
	DECR_LEN(len, 2);
Packit aea12f
	p->identities_len = _gnutls_read_uint16(data);
Packit aea12f
	data += 2;
Packit aea12f
Packit aea12f
	if (p->identities_len == 0)
Packit aea12f
		return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
Packit aea12f
Packit aea12f
	p->identities_data = (unsigned char *) data;
Packit aea12f
Packit aea12f
	DECR_LEN(len, p->identities_len);
Packit aea12f
	data += p->identities_len;
Packit aea12f
Packit aea12f
	DECR_LEN(len, 2);
Packit aea12f
	p->binders_len = _gnutls_read_uint16(data);
Packit aea12f
	data += 2;
Packit aea12f
Packit aea12f
	p->binders_data = data;
Packit aea12f
	DECR_LEN(len, p->binders_len);
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
Packit aea12f
/* Extract PSK identity and move to the next iteration.
Packit aea12f
 *
Packit aea12f
 * Returns GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE when no more identities
Packit aea12f
 * are present, or 0, on success.
Packit aea12f
 */
Packit aea12f
int _gnutls13_psk_ext_iter_next_identity(psk_ext_iter_st *iter,
Packit aea12f
					 struct psk_st *psk)
Packit aea12f
{
Packit aea12f
	if (iter->identities_len == 0)
Packit aea12f
		return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
Packit aea12f
Packit aea12f
	DECR_LEN(iter->identities_len, 2);
Packit aea12f
	psk->identity.size = _gnutls_read_uint16(iter->identities_data);
Packit aea12f
	if (psk->identity.size == 0)
Packit aea12f
		return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
Packit aea12f
Packit aea12f
	iter->identities_data += 2;
Packit aea12f
	psk->identity.data = (void*)iter->identities_data;
Packit aea12f
Packit aea12f
	DECR_LEN(iter->identities_len, psk->identity.size);
Packit aea12f
	iter->identities_data += psk->identity.size;
Packit aea12f
Packit aea12f
	DECR_LEN(iter->identities_len, 4);
Packit aea12f
	psk->ob_ticket_age = _gnutls_read_uint32(iter->identities_data);
Packit aea12f
	iter->identities_data += 4;
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}
Packit aea12f
Packit aea12f
/* Extract PSK binder and move to the next iteration.
Packit aea12f
 *
Packit aea12f
 * Returns GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE when no more identities
Packit aea12f
 * are present, or 0, on success.
Packit aea12f
 */
Packit aea12f
int _gnutls13_psk_ext_iter_next_binder(psk_ext_iter_st *iter,
Packit aea12f
				       gnutls_datum_t *binder)
Packit aea12f
{
Packit aea12f
	if (iter->binders_len == 0)
Packit aea12f
		return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
Packit aea12f
Packit aea12f
	DECR_LEN(iter->binders_len, 1);
Packit aea12f
	binder->size = *iter->binders_data;
Packit aea12f
	if (binder->size == 0)
Packit aea12f
		return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
Packit aea12f
Packit aea12f
	iter->binders_data++;
Packit aea12f
	binder->data = (uint8_t *)iter->binders_data;
Packit aea12f
	DECR_LEN(iter->binders_len, binder->size);
Packit aea12f
	iter->binders_data += binder->size;
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}