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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*
* Copyright (C) 2017-2019 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 <https://www.gnu.org/licenses/>
*
*/
#include "gnutls_int.h"
#include "errors.h"
#include <auth/cert.h>
#include <algorithms.h>
#include <ext/signature.h>
#include <abstract_int.h>
#include "tls13-sig.h"
#include "tls-sig.h"
#include "hash_int.h"
#undef PREFIX_SIZE
#define PREFIX_SIZE 64
#if PREFIX_SIZE < MAX_HASH_SIZE
/* we assume later that prefix is sufficient to store hash output */
# error Need to modify code
#endif
int
_gnutls13_handshake_verify_data(gnutls_session_t session,
unsigned verify_flags,
gnutls_pcert_st *cert,
const gnutls_datum_t *context,
const gnutls_datum_t *signature,
const gnutls_sign_entry_st *se)
{
int ret;
const version_entry_st *ver = get_version(session);
gnutls_buffer_st buf;
uint8_t prefix[PREFIX_SIZE];
unsigned key_usage = 0;
gnutls_datum_t p;
_gnutls_handshake_log
("HSK[%p]: verifying TLS 1.3 handshake data using %s\n", session,
se->name);
ret =
_gnutls_pubkey_compatible_with_sig(session,
cert->pubkey, ver,
se->id);
if (ret < 0)
return gnutls_assert_val(ret);
if (unlikely(sign_supports_cert_pk_algorithm(se, cert->pubkey->params.algo) == 0)) {
_gnutls_handshake_log("HSK[%p]: certificate of %s cannot be combined with %s sig\n",
session, gnutls_pk_get_name(cert->pubkey->params.algo), se->name);
return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
}
ret =
_gnutls_session_sign_algo_enabled(session, se->id);
if (ret < 0)
return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
if ((se->flags & GNUTLS_SIGN_FLAG_TLS13_OK) == 0) /* explicitly prohibited */
return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
gnutls_pubkey_get_key_usage(cert->pubkey, &key_usage);
ret = _gnutls_check_key_usage_for_sig(session, key_usage, 0);
if (ret < 0)
return gnutls_assert_val(ret);
_gnutls_buffer_init(&buf);
memset(prefix, 0x20, sizeof(prefix));
ret = _gnutls_buffer_append_data(&buf, prefix, sizeof(prefix));
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = _gnutls_buffer_append_data(&buf, context->data, context->size);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = _gnutls_buffer_append_data(&buf, "\x00", 1);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = gnutls_hash_fast(session->security_parameters.prf->id,
session->internals.handshake_hash_buffer.data,
session->internals.handshake_hash_buffer_prev_len,
prefix);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = _gnutls_buffer_append_data(&buf, prefix, session->security_parameters.prf->output_size);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
p.data = buf.data;
p.size = buf.length;
/* Here we intentionally enable flag GNUTLS_VERIFY_ALLOW_BROKEN
* because we have checked whether the currently used signature
* algorithm is allowed in the session. */
ret = gnutls_pubkey_verify_data2(cert->pubkey, se->id,
verify_flags|GNUTLS_VERIFY_ALLOW_BROKEN,
&p, signature);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = 0;
cleanup:
_gnutls_buffer_clear(&buf);
return ret;
}
int
_gnutls13_handshake_sign_data(gnutls_session_t session,
gnutls_pcert_st * cert, gnutls_privkey_t pkey,
const gnutls_datum_t *context,
gnutls_datum_t * signature,
const gnutls_sign_entry_st *se)
{
gnutls_datum_t p;
int ret;
gnutls_buffer_st buf;
uint8_t tmp[MAX_HASH_SIZE];
if (unlikely(se == NULL || (se->flags & GNUTLS_SIGN_FLAG_TLS13_OK) == 0))
return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
if (unlikely(sign_supports_priv_pk_algorithm(se, pkey->pk_algorithm) == 0))
return gnutls_assert_val(GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER);
/* when we reach here we know we have a signing certificate */
_gnutls_handshake_log
("HSK[%p]: signing TLS 1.3 handshake data: using %s and PRF: %s\n", session, se->name,
session->security_parameters.prf->name);
_gnutls_buffer_init(&buf);
ret = _gnutls_buffer_resize(&buf, PREFIX_SIZE);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
memset(buf.data, 0x20, PREFIX_SIZE);
buf.length += PREFIX_SIZE;
ret = _gnutls_buffer_append_data(&buf, context->data, context->size);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = _gnutls_buffer_append_data(&buf, "\x00", 1);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = gnutls_hash_fast(session->security_parameters.prf->id,
session->internals.handshake_hash_buffer.data,
session->internals.handshake_hash_buffer.length,
tmp);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = _gnutls_buffer_append_data(&buf, tmp, session->security_parameters.prf->output_size);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
p.data = buf.data;
p.size = buf.length;
ret = gnutls_privkey_sign_data2(pkey, se->id, 0, &p, signature);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = 0;
cleanup:
_gnutls_buffer_clear(&buf);
return ret;
}
|