Blame lib/prf.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2002-2015 Free Software Foundation, Inc.
Packit aea12f
 * Copyright (C) 2014-2015 Nikos Mavrogiannopoulos
Packit aea12f
 * Copyright (C) 2016-2017 Red Hat, Inc.
Packit aea12f
 *
Packit aea12f
 * Author: 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
/* Functions for the TLS PRF handling.
Packit aea12f
 */
Packit aea12f
Packit aea12f
#include "gnutls_int.h"
Packit aea12f
#include "errors.h"
Packit aea12f
#include "handshake.h"
Packit aea12f
#include "secrets.h"
Packit aea12f
#include <num.h>
Packit aea12f
#include <state.h>
Packit aea12f
#include <algorithms.h>
Packit aea12f
Packit aea12f
/**
Packit aea12f
 * gnutls_prf_raw:
Packit aea12f
 * @session: is a #gnutls_session_t type.
Packit aea12f
 * @label_size: length of the @label variable.
Packit aea12f
 * @label: label used in PRF computation, typically a short string.
Packit aea12f
 * @seed_size: length of the @seed variable.
Packit aea12f
 * @seed: optional extra data to seed the PRF with.
Packit aea12f
 * @outsize: size of pre-allocated output buffer to hold the output.
Packit aea12f
 * @out: pre-allocated buffer to hold the generated data.
Packit aea12f
 *
Packit aea12f
 * Apply the TLS Pseudo-Random-Function (PRF) on the master secret
Packit aea12f
 * and the provided data.
Packit aea12f
 *
Packit aea12f
 * The @label variable usually contains a string denoting the purpose
Packit aea12f
 * for the generated data.  The @seed usually contains data such as the
Packit aea12f
 * client and server random, perhaps together with some additional
Packit aea12f
 * data that is added to guarantee uniqueness of the output for a
Packit aea12f
 * particular purpose.
Packit aea12f
 *
Packit aea12f
 * Because the output is not guaranteed to be unique for a particular
Packit aea12f
 * session unless @seed includes the client random and server random
Packit aea12f
 * fields (the PRF would output the same data on another connection
Packit aea12f
 * resumed from the first one), it is not recommended to use this
Packit aea12f
 * function directly.  The gnutls_prf() function seeds the PRF with the
Packit aea12f
 * client and server random fields directly, and is recommended if you
Packit aea12f
 * want to generate pseudo random data unique for each session.
Packit aea12f
 *
Packit aea12f
 * Note: This function will only operate under TLS versions prior to 1.3.
Packit aea12f
 * In TLS1.3 the use of PRF is replaced with HKDF and the generic
Packit aea12f
 * exporters like gnutls_prf_rfc5705() should be used instead. Under
Packit aea12f
 * TLS1.3 this function returns %GNUTLS_E_INVALID_REQUEST.
Packit aea12f
 *
Packit aea12f
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
Packit aea12f
 **/
Packit aea12f
int
Packit aea12f
gnutls_prf_raw(gnutls_session_t session,
Packit aea12f
	       size_t label_size,
Packit aea12f
	       const char *label,
Packit aea12f
	       size_t seed_size, const char *seed, size_t outsize,
Packit aea12f
	       char *out)
Packit aea12f
{
Packit aea12f
	int ret;
Packit aea12f
	const version_entry_st *vers = get_version(session);
Packit aea12f
Packit aea12f
	if (vers && vers->tls13_sem)
Packit aea12f
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit aea12f
Packit Service 991b93
	if (session->security_parameters.prf == NULL)
Packit Service 991b93
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit Service 991b93
Packit aea12f
	ret = _gnutls_prf_raw(session->security_parameters.prf->id,
Packit aea12f
			  GNUTLS_MASTER_SIZE, session->security_parameters.master_secret,
Packit aea12f
			  label_size, label,
Packit aea12f
			  seed_size, (uint8_t *) seed,
Packit aea12f
			  outsize, out);
Packit aea12f
Packit aea12f
	return ret;
Packit aea12f
}
Packit aea12f
Packit aea12f
static int
Packit aea12f
_tls13_derive_exporter(const mac_entry_st *prf,
Packit aea12f
		       gnutls_session_t session,
Packit aea12f
		       size_t label_size, const char *label,
Packit aea12f
		       size_t context_size, const char *context,
Packit aea12f
		       size_t outsize, char *out,
Packit aea12f
		       bool early)
Packit aea12f
{
Packit aea12f
	uint8_t secret[MAX_HASH_SIZE];
Packit aea12f
	uint8_t digest[MAX_HASH_SIZE];
Packit aea12f
	unsigned digest_size = prf->output_size;
Packit aea12f
	int ret;
Packit aea12f
Packit aea12f
	ret = _tls13_derive_secret2(prf, label, label_size, NULL, 0,
Packit aea12f
				    session->key.proto.tls13.ap_expkey,
Packit aea12f
				    secret);
Packit aea12f
	if (ret < 0)
Packit aea12f
		return gnutls_assert_val(ret);
Packit aea12f
Packit aea12f
	ret = gnutls_hash_fast((gnutls_digest_algorithm_t)prf->id,
Packit aea12f
			       context, context_size, digest);
Packit aea12f
	if (ret < 0)
Packit aea12f
		return gnutls_assert_val(ret);
Packit aea12f
Packit aea12f
	return _tls13_expand_secret2(prf,
Packit aea12f
				     EXPORTER_LABEL, sizeof(EXPORTER_LABEL)-1,
Packit aea12f
				     digest, digest_size,
Packit aea12f
				     secret, outsize, out);
Packit aea12f
}
Packit aea12f
Packit aea12f
/**
Packit aea12f
 * gnutls_prf_rfc5705:
Packit aea12f
 * @session: is a #gnutls_session_t type.
Packit aea12f
 * @label_size: length of the @label variable.
Packit aea12f
 * @label: label used in PRF computation, typically a short string.
Packit aea12f
 * @context_size: length of the @extra variable.
Packit aea12f
 * @context: optional extra data to seed the PRF with.
Packit aea12f
 * @outsize: size of pre-allocated output buffer to hold the output.
Packit aea12f
 * @out: pre-allocated buffer to hold the generated data.
Packit aea12f
 *
Packit aea12f
 * Exports keying material from TLS/DTLS session to an application, as
Packit aea12f
 * specified in RFC5705.
Packit aea12f
 *
Packit aea12f
 * In the TLS versions prior to 1.3, it applies the TLS
Packit aea12f
 * Pseudo-Random-Function (PRF) on the master secret and the provided
Packit aea12f
 * data, seeded with the client and server random fields.
Packit aea12f
 *
Packit aea12f
 * In TLS 1.3, it applies HKDF on the exporter master secret derived
Packit aea12f
 * from the master secret.
Packit aea12f
 *
Packit aea12f
 * The @label variable usually contains a string denoting the purpose
Packit aea12f
 * for the generated data.
Packit aea12f
 *
Packit aea12f
 * The @context variable can be used to add more data to the seed, after
Packit aea12f
 * the random variables.  It can be used to make sure the
Packit aea12f
 * generated output is strongly connected to some additional data
Packit aea12f
 * (e.g., a string used in user authentication). 
Packit aea12f
 *
Packit aea12f
 * The output is placed in @out, which must be pre-allocated.
Packit aea12f
 *
Packit aea12f
 * Note that, to provide the RFC5705 context, the @context variable
Packit aea12f
 * must be non-null.
Packit aea12f
 *
Packit aea12f
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
Packit aea12f
 *
Packit aea12f
 * Since: 3.4.4
Packit aea12f
 **/
Packit aea12f
int
Packit aea12f
gnutls_prf_rfc5705(gnutls_session_t session,
Packit aea12f
		   size_t label_size, const char *label,
Packit aea12f
		   size_t context_size, const char *context,
Packit aea12f
		   size_t outsize, char *out)
Packit aea12f
{
Packit aea12f
	const version_entry_st *vers = get_version(session);
Packit aea12f
	int ret;
Packit aea12f
Packit Service 991b93
	if (session->security_parameters.prf == NULL)
Packit Service 991b93
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit Service 991b93
Packit aea12f
	if (vers && vers->tls13_sem) {
Packit aea12f
		ret = _tls13_derive_exporter(session->security_parameters.prf,
Packit aea12f
					     session,
Packit aea12f
					     label_size, label,
Packit aea12f
					     context_size, context,
Packit aea12f
					     outsize, out,
Packit aea12f
					     0);
Packit aea12f
	} else {
Packit aea12f
		char *pctx = NULL;
Packit aea12f
Packit aea12f
		if (context != NULL && context_size > 65535)  {
Packit aea12f
			gnutls_assert();
Packit aea12f
			return GNUTLS_E_INVALID_REQUEST;
Packit aea12f
		}
Packit aea12f
Packit aea12f
		if (context != NULL) {
Packit aea12f
			pctx = gnutls_malloc(context_size+2);
Packit aea12f
			if (!pctx) {
Packit aea12f
				gnutls_assert();
Packit aea12f
				return GNUTLS_E_MEMORY_ERROR;
Packit aea12f
			}
Packit aea12f
Packit aea12f
			memcpy(pctx+2, context, context_size);
Packit aea12f
			_gnutls_write_uint16(context_size, (void*)pctx);
Packit aea12f
			context_size += 2;
Packit aea12f
		}
Packit aea12f
Packit aea12f
		ret = gnutls_prf(session, label_size, label, 0,
Packit aea12f
				 context_size, pctx, outsize, out);
Packit aea12f
Packit aea12f
		gnutls_free(pctx);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	return ret;
Packit aea12f
}
Packit aea12f
Packit aea12f
/**
Packit aea12f
 * gnutls_prf_early:
Packit aea12f
 * @session: is a #gnutls_session_t type.
Packit aea12f
 * @label_size: length of the @label variable.
Packit aea12f
 * @label: label used in PRF computation, typically a short string.
Packit aea12f
 * @context_size: length of the @extra variable.
Packit aea12f
 * @context: optional extra data to seed the PRF with.
Packit aea12f
 * @outsize: size of pre-allocated output buffer to hold the output.
Packit aea12f
 * @out: pre-allocated buffer to hold the generated data.
Packit aea12f
 *
Packit aea12f
 * This function is similar to gnutls_prf_rfc5705(), but only works in
Packit aea12f
 * TLS 1.3 or later to export early keying material.
Packit aea12f
 *
Packit aea12f
 * Note that the keying material is only available after the
Packit aea12f
 * ClientHello message is processed and before the application traffic
Packit aea12f
 * keys are established.  Therefore this function shall be called in a
Packit aea12f
 * handshake hook function for %GNUTLS_HANDSHAKE_CLIENT_HELLO.
Packit aea12f
 *
Packit aea12f
 * The @label variable usually contains a string denoting the purpose
Packit aea12f
 * for the generated data.
Packit aea12f
 *
Packit aea12f
 * The @context variable can be used to add more data to the seed, after
Packit aea12f
 * the random variables.  It can be used to make sure the
Packit aea12f
 * generated output is strongly connected to some additional data
Packit aea12f
 * (e.g., a string used in user authentication).
Packit aea12f
 *
Packit aea12f
 * The output is placed in @out, which must be pre-allocated.
Packit aea12f
 *
Packit aea12f
 * Note that, to provide the RFC5705 context, the @context variable
Packit aea12f
 * must be non-null.
Packit aea12f
 *
Packit aea12f
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
Packit aea12f
 *
Packit Service 991b93
 * Since: 3.6.8
Packit aea12f
 **/
Packit aea12f
int
Packit aea12f
gnutls_prf_early(gnutls_session_t session,
Packit aea12f
		 size_t label_size, const char *label,
Packit aea12f
		 size_t context_size, const char *context,
Packit aea12f
		 size_t outsize, char *out)
Packit aea12f
{
Packit aea12f
	if (session->internals.initial_negotiation_completed ||
Packit aea12f
	    session->key.binders[0].prf == NULL)
Packit aea12f
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit aea12f
Packit aea12f
	return _tls13_derive_exporter(session->key.binders[0].prf, session,
Packit aea12f
				      label_size, label,
Packit aea12f
				      context_size, context,
Packit aea12f
				      outsize, out,
Packit aea12f
				      1);
Packit aea12f
}
Packit aea12f
Packit aea12f
/**
Packit aea12f
 * gnutls_prf:
Packit aea12f
 * @session: is a #gnutls_session_t type.
Packit aea12f
 * @label_size: length of the @label variable.
Packit aea12f
 * @label: label used in PRF computation, typically a short string.
Packit aea12f
 * @server_random_first: non-zero if server random field should be first in seed
Packit aea12f
 * @extra_size: length of the @extra variable.
Packit aea12f
 * @extra: optional extra data to seed the PRF with.
Packit aea12f
 * @outsize: size of pre-allocated output buffer to hold the output.
Packit aea12f
 * @out: pre-allocated buffer to hold the generated data.
Packit aea12f
 *
Packit aea12f
 * Applies the TLS Pseudo-Random-Function (PRF) on the master secret
Packit aea12f
 * and the provided data, seeded with the client and server random fields.
Packit aea12f
 * For the key expansion specified in RFC5705 see gnutls_prf_rfc5705().
Packit aea12f
 *
Packit aea12f
 * The @label variable usually contains a string denoting the purpose
Packit aea12f
 * for the generated data.  The @server_random_first indicates whether
Packit aea12f
 * the client random field or the server random field should be first
Packit aea12f
 * in the seed.  Non-zero indicates that the server random field is first,
Packit aea12f
 * 0 that the client random field is first.
Packit aea12f
 *
Packit aea12f
 * The @extra variable can be used to add more data to the seed, after
Packit aea12f
 * the random variables.  It can be used to make sure the
Packit aea12f
 * generated output is strongly connected to some additional data
Packit aea12f
 * (e.g., a string used in user authentication).
Packit aea12f
 *
Packit aea12f
 * The output is placed in @out, which must be pre-allocated.
Packit aea12f
 *
Packit aea12f
 * Note: This function produces identical output with gnutls_prf_rfc5705()
Packit aea12f
 * when @server_random_first is set to 0 and @extra is %NULL. Under TLS1.3
Packit aea12f
 * this function will only operate when these conditions are true, or otherwise
Packit aea12f
 * return %GNUTLS_E_INVALID_REQUEST.
Packit aea12f
 *
Packit aea12f
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
Packit aea12f
 **/
Packit aea12f
int
Packit aea12f
gnutls_prf(gnutls_session_t session,
Packit aea12f
	   size_t label_size,
Packit aea12f
	   const char *label,
Packit aea12f
	   int server_random_first,
Packit aea12f
	   size_t extra_size, const char *extra,
Packit aea12f
	   size_t outsize, char *out)
Packit aea12f
{
Packit aea12f
	int ret;
Packit aea12f
	uint8_t *seed;
Packit aea12f
	const version_entry_st *vers = get_version(session);
Packit aea12f
	size_t seedsize = 2 * GNUTLS_RANDOM_SIZE + extra_size;
Packit aea12f
Packit aea12f
	if (vers && vers->tls13_sem) {
Packit aea12f
		if (extra == NULL && server_random_first == 0)
Packit aea12f
			return gnutls_prf_rfc5705(session, label_size, label,
Packit aea12f
						  extra_size, extra, outsize, out);
Packit aea12f
		else
Packit aea12f
			return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit aea12f
	}
Packit aea12f
Packit Service 991b93
	if (session->security_parameters.prf == NULL)
Packit Service 991b93
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit Service 991b93
Packit aea12f
	seed = gnutls_malloc(seedsize);
Packit aea12f
	if (!seed) {
Packit aea12f
		gnutls_assert();
Packit aea12f
		return GNUTLS_E_MEMORY_ERROR;
Packit aea12f
	}
Packit aea12f
Packit aea12f
	memcpy(seed, server_random_first ?
Packit aea12f
	       session->security_parameters.server_random :
Packit aea12f
	       session->security_parameters.client_random,
Packit aea12f
	       GNUTLS_RANDOM_SIZE);
Packit aea12f
	memcpy(seed + GNUTLS_RANDOM_SIZE,
Packit aea12f
	       server_random_first ? session->security_parameters.
Packit aea12f
	       client_random : session->security_parameters.server_random,
Packit aea12f
	       GNUTLS_RANDOM_SIZE);
Packit aea12f
Packit aea12f
	if (extra && extra_size) {
Packit aea12f
		memcpy(seed + 2 * GNUTLS_RANDOM_SIZE, extra, extra_size);
Packit aea12f
	}
Packit aea12f
Packit aea12f
	ret =
Packit aea12f
	    _gnutls_prf_raw(session->security_parameters.prf->id,
Packit aea12f
			GNUTLS_MASTER_SIZE, session->security_parameters.master_secret,
Packit aea12f
			label_size, label,
Packit aea12f
			seedsize, seed,
Packit aea12f
			outsize, out);
Packit aea12f
Packit aea12f
	gnutls_free(seed);
Packit aea12f
Packit aea12f
	return ret;
Packit aea12f
}
Packit aea12f