summaryrefslogtreecommitdiff
path: root/lib/tls13/finished.c
blob: bb535fff87d2f6c355a564e4286e766b8c306a60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (C) 2017 Red Hat, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

#include "gnutls_int.h"
#include "errors.h"
#include "handshake.h"
#include "tls13/finished.h"
#include "mem.h"
#include "mbuffers.h"
#include "secrets.h"

int _gnutls13_compute_finished(const mac_entry_st *prf,
		const uint8_t *base_key,
		unsigned hash_size,
		gnutls_buffer_st *handshake_hash_buffer,
		void *out)
{
	int ret;
	uint8_t fkey[MAX_HASH_SIZE];
	uint8_t ts_hash[MAX_HASH_SIZE];

	ret = _tls13_expand_secret2(prf,
			"finished", 8,
			NULL, 0,
			base_key,
			hash_size, fkey);
	if (ret < 0)
		return gnutls_assert_val(ret);

	ret = gnutls_hash_fast(prf->id,
			       handshake_hash_buffer->data,
			       handshake_hash_buffer->length,
			       ts_hash);
	if (ret < 0)
		return gnutls_assert_val(ret);

	ret = gnutls_hmac_fast(prf->id,
			       fkey, hash_size,
			       ts_hash, hash_size,
			       out);
	if (ret < 0)
		return gnutls_assert_val(ret);

	return 0;
}

int _gnutls13_recv_finished(gnutls_session_t session)
{
	int ret;
	gnutls_buffer_st buf;
	uint8_t verifier[MAX_HASH_SIZE];
	const uint8_t *base_key;
	unsigned hash_size;

	if (unlikely(session->security_parameters.prf == NULL))
		return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);

	hash_size = session->security_parameters.prf->output_size;

	if (session->security_parameters.entity == GNUTLS_CLIENT)
		base_key = session->key.proto.tls13.hs_skey;
	else
		base_key = session->key.proto.tls13.hs_ckey;

	ret = _gnutls13_compute_finished(session->security_parameters.prf,
			base_key, hash_size,
			&session->internals.handshake_hash_buffer,
			verifier);
	if (ret < 0) {
		gnutls_assert();
		goto cleanup;
	}

	ret = _gnutls_recv_handshake(session, GNUTLS_HANDSHAKE_FINISHED, 0, &buf);
	if (ret < 0)
		return gnutls_assert_val(ret);

	_gnutls_handshake_log("HSK[%p]: parsing finished\n", session);

	if (buf.length != hash_size) {
		gnutls_assert();
		ret = GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
		goto cleanup;
	}


#if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
# warning This is unsafe for production builds
#else
	if (safe_memcmp(verifier, buf.data, buf.length) != 0) {
		gnutls_assert();
		ret = GNUTLS_E_ERROR_IN_FINISHED_PACKET;
		goto cleanup;
	}
#endif

	ret = 0;
cleanup:
	
	_gnutls_buffer_clear(&buf);
	return ret;
}

int _gnutls13_send_finished(gnutls_session_t session, unsigned again)
{
	int ret;
	uint8_t verifier[MAX_HASH_SIZE];
	mbuffer_st *bufel = NULL;
	const uint8_t *base_key;
	unsigned hash_size;

	if (again == 0) {
		if (unlikely(session->security_parameters.prf == NULL))
			return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);

		hash_size = session->security_parameters.prf->output_size;

		if (session->security_parameters.entity == GNUTLS_CLIENT)
			base_key = session->key.proto.tls13.hs_ckey;
		else
			base_key = session->key.proto.tls13.hs_skey;

		ret = _gnutls13_compute_finished(session->security_parameters.prf,
				base_key, hash_size,
				&session->internals.handshake_hash_buffer,
				verifier);
		if (ret < 0) {
			gnutls_assert();
			goto cleanup;
		}

		_gnutls_handshake_log("HSK[%p]: sending finished\n", session);

		bufel = _gnutls_handshake_alloc(session, hash_size);
		if (bufel == NULL)
			return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);

		_mbuffer_set_udata_size(bufel, 0);
		ret = _mbuffer_append_data(bufel, verifier, hash_size);
		if (ret < 0) {
			gnutls_assert();
			goto cleanup;
		}
	}

	return _gnutls_send_handshake(session, bufel, GNUTLS_HANDSHAKE_FINISHED);

cleanup:
	_mbuffer_xfree(&bufel);
	return ret;
}