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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*
* 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 <https://www.gnu.org/licenses/>
*
*/
#include "gnutls_int.h"
#include "errors.h"
#include "handshake.h"
#include "auth/cert.h"
#include "ext/signature.h"
#include "algorithms.h"
#include "tls13-sig.h"
#include "mbuffers.h"
#include "tls13/certificate_verify.h"
#define SRV_CTX "TLS 1.3, server CertificateVerify"
static const gnutls_datum_t srv_ctx = {
(void*)SRV_CTX, sizeof(SRV_CTX)-1
};
#define CLI_CTX "TLS 1.3, client CertificateVerify"
static const gnutls_datum_t cli_ctx = {
(void*)CLI_CTX, sizeof(CLI_CTX)-1
};
int _gnutls13_recv_certificate_verify(gnutls_session_t session)
{
int ret;
gnutls_buffer_st buf;
const gnutls_sign_entry_st *se;
gnutls_datum_t sig_data;
gnutls_certificate_credentials_t cred;
unsigned vflags;
gnutls_pcert_st peer_cert;
cert_auth_info_t info = _gnutls_get_auth_info(session, GNUTLS_CRD_CERTIFICATE);
bool server = 0;
gnutls_certificate_type_t cert_type;
memset(&peer_cert, 0, sizeof(peer_cert));
/* this message is only expected if we have received
* a certificate message */
if (!(session->internals.hsk_flags & HSK_CRT_VRFY_EXPECTED))
return 0;
if (session->security_parameters.entity == GNUTLS_SERVER)
server = 1;
cred = (gnutls_certificate_credentials_t)
_gnutls_get_cred(session, GNUTLS_CRD_CERTIFICATE);
if (unlikely(cred == NULL))
return gnutls_assert_val(GNUTLS_E_INSUFFICIENT_CREDENTIALS);
if (unlikely(info == NULL))
return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
ret = _gnutls_recv_handshake(session, GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY, 0, &buf);
if (ret < 0)
return gnutls_assert_val(ret);
_gnutls_handshake_log("HSK[%p]: Parsing certificate verify\n", session);
if (buf.length < 2) {
gnutls_assert();
ret = GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
goto cleanup;
}
se = _gnutls_tls_aid_to_sign_entry(buf.data[0], buf.data[1], get_version(session));
if (se == NULL) {
_gnutls_handshake_log("Found unsupported signature (%d.%d)\n", (int)buf.data[0], (int)buf.data[1]);
ret = gnutls_assert_val(GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);
goto cleanup;
}
if (server)
gnutls_sign_algorithm_set_client(session, se->id);
else
gnutls_sign_algorithm_set_server(session, se->id);
buf.data+=2;
buf.length-=2;
/* we check during verification whether the algorithm is enabled */
ret = _gnutls_buffer_pop_datum_prefix16(&buf, &sig_data);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
if (sig_data.size == 0) {
gnutls_assert();
ret = GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
goto cleanup;
}
/* We verify the certificate of the peer. Therefore we need to
* retrieve the negotiated certificate type for the peer. */
cert_type = get_certificate_type(session, GNUTLS_CTYPE_PEERS);
/* Verify the signature */
ret = _gnutls_get_auth_info_pcert(&peer_cert, cert_type, info);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
vflags = cred->verify_flags | session->internals.additional_verify_flags;
ret = _gnutls13_handshake_verify_data(session, vflags, &peer_cert,
server?(&cli_ctx):(&srv_ctx),
&sig_data, se);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
if (buf.length > 0) {
gnutls_assert();
ret = GNUTLS_E_UNEXPECTED_PACKET_LENGTH;
goto cleanup;
}
ret = 0;
cleanup:
gnutls_pcert_deinit(&peer_cert);
_gnutls_buffer_clear(&buf);
return ret;
}
int _gnutls13_send_certificate_verify(gnutls_session_t session, unsigned again)
{
int ret;
gnutls_pcert_st *apr_cert_list;
gnutls_privkey_t apr_pkey;
int apr_cert_list_length;
mbuffer_st *bufel = NULL;
gnutls_buffer_st buf;
gnutls_datum_t sig = {NULL, 0};
gnutls_sign_algorithm_t algo;
const gnutls_sign_entry_st *se;
bool server = 0;
if (again == 0) {
if (!session->internals.initial_negotiation_completed &&
session->internals.hsk_flags & HSK_PSK_SELECTED)
return 0;
if (session->security_parameters.entity == GNUTLS_SERVER &&
session->internals.resumed)
return 0;
if (session->security_parameters.entity == GNUTLS_SERVER)
server = 1;
ret = _gnutls_get_selected_cert(session, &apr_cert_list,
&apr_cert_list_length, &apr_pkey);
if (ret < 0)
return gnutls_assert_val(ret);
if (apr_cert_list_length == 0) {
if (server) {
return gnutls_assert_val(GNUTLS_E_INSUFFICIENT_CREDENTIALS);
} else {
/* for client, this means either we
* didn't get a cert request or we are
* declining authentication; in either
* case we don't send a cert verify */
return 0;
}
}
algo = _gnutls_session_get_sign_algo(session, &apr_cert_list[0], apr_pkey, 0);
if (algo == GNUTLS_SIGN_UNKNOWN)
return gnutls_assert_val(GNUTLS_E_INCOMPATIBLE_SIG_WITH_KEY);
if (server)
gnutls_sign_algorithm_set_server(session, algo);
else
gnutls_sign_algorithm_set_client(session, algo);
se = _gnutls_sign_to_entry(algo);
ret = _gnutls13_handshake_sign_data(session, &apr_cert_list[0], apr_pkey,
server?(&srv_ctx):(&cli_ctx),
&sig, se);
if (ret < 0)
return gnutls_assert_val(ret);
ret = _gnutls_buffer_init_handshake_mbuffer(&buf);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = _gnutls_buffer_append_data(&buf, se->aid.id, 2);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
ret = _gnutls_buffer_append_data_prefix(&buf, 16, sig.data, sig.size);
if (ret < 0) {
gnutls_assert();
goto cleanup;
}
bufel = _gnutls_buffer_to_mbuffer(&buf);
gnutls_free(sig.data);
}
return _gnutls_send_handshake(session, bufel, GNUTLS_HANDSHAKE_CERTIFICATE_VERIFY);
cleanup:
gnutls_free(sig.data);
_gnutls_buffer_clear(&buf);
return ret;
}
|