Blame lib/tls13/psk_ext_parser.c

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