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
237
238
239
240
241
242
243
244
245
246
|
/*
* Copyright (C) 2018 Red Hat, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* GnuTLS is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GnuTLS 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
* 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/>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gnutls/gnutls.h>
#include <gnutls/gnutlsxx.h>
#include <iostream>
extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "cert-common.h"
#include <setjmp.h>
#include <cmocka.h>
#include <minmax.h>
}
/* This is a basic test for C++ API */
static void tls_log_func(int level, const char *str)
{
std::cerr << level << "| " << str << "\n";
}
static char to_server[64 * 1024];
static size_t to_server_len = 0;
static char to_client[64 * 1024];
static size_t to_client_len = 0;
static ssize_t
client_push(gnutls_transport_ptr_t tr, const void *data, size_t len)
{
size_t newlen;
len = MIN(len, sizeof(to_server) - to_server_len);
newlen = to_server_len + len;
memcpy(to_server + to_server_len, data, len);
to_server_len = newlen;
return len;
}
static ssize_t
client_pull(gnutls_transport_ptr_t tr, void *data, size_t len)
{
if (to_client_len == 0) {
gnutls_transport_set_errno ((gnutls_session_t)tr, EAGAIN);
return -1;
}
len = MIN(len, to_client_len);
memcpy(data, to_client, len);
memmove(to_client, to_client + len, to_client_len - len);
to_client_len -= len;
return len;
}
static ssize_t
server_pull(gnutls_transport_ptr_t tr, void *data, size_t len)
{
if (to_server_len == 0) {
gnutls_transport_set_errno ((gnutls_session_t)tr, EAGAIN);
return -1;
}
len = MIN(len, to_server_len);
memcpy(data, to_server, len);
memmove(to_server, to_server + len, to_server_len - len);
to_server_len -= len;
return len;
}
static ssize_t
server_push(gnutls_transport_ptr_t tr, const void *data, size_t len)
{
size_t newlen;
len = MIN(len, sizeof(to_client) - to_client_len);
newlen = to_client_len + len;
memcpy(to_client + to_client_len, data, len);
to_client_len = newlen;
return len;
}
inline static void reset_buffers(void)
{
to_server_len = 0;
to_client_len = 0;
}
#define MSG "test message"
static void test_handshake(void **glob_state, const char *prio,
gnutls::server_session& server, gnutls::client_session& client)
{
gnutls::certificate_credentials serverx509cred;
int sret, cret;
gnutls::certificate_credentials clientx509cred;
char buffer[64];
int ret;
/* General init. */
reset_buffers();
gnutls_global_set_log_function(tls_log_func);
try {
serverx509cred.set_x509_key(server_cert, server_key, GNUTLS_X509_FMT_PEM);
server.set_credentials(serverx509cred);
server.set_priority(prio, NULL);
server.set_transport_push_function(server_push);
server.set_transport_pull_function(server_pull);
server.set_transport_ptr(server.ptr());
client.set_priority(prio, NULL);
client.set_credentials(clientx509cred);
client.set_transport_push_function(client_push);
client.set_transport_pull_function(client_pull);
client.set_transport_ptr(client.ptr());
}
catch (std::exception &ex) {
std::cerr << "Exception caught: " << ex.what() << std::endl;
fail();
}
sret = cret = GNUTLS_E_AGAIN;
do {
if (cret == GNUTLS_E_AGAIN) {
try {
cret = client.handshake();
} catch(gnutls::exception &ex) {
cret = ex.get_code();
if (cret == GNUTLS_E_INTERRUPTED || cret == GNUTLS_E_AGAIN)
cret = GNUTLS_E_AGAIN;
}
}
if (sret == GNUTLS_E_AGAIN) {
try {
sret = server.handshake();
} catch(gnutls::exception &ex) {
sret = ex.get_code();
if (sret == GNUTLS_E_INTERRUPTED || sret == GNUTLS_E_AGAIN)
sret = GNUTLS_E_AGAIN;
}
}
}
while ((cret == GNUTLS_E_AGAIN || (cret == 0 && sret == GNUTLS_E_AGAIN)) &&
(sret == GNUTLS_E_AGAIN || (sret == 0 && cret == GNUTLS_E_AGAIN)));
if (sret < 0 || cret < 0) {
fail();
}
try {
client.send(MSG, sizeof(MSG)-1);
ret = server.recv(buffer, sizeof(buffer));
assert(ret == sizeof(MSG)-1);
assert(memcmp(buffer, MSG, sizeof(MSG)-1) == 0);
client.bye(GNUTLS_SHUT_WR);
server.bye(GNUTLS_SHUT_WR);
}
catch (std::exception &ex) {
std::cerr << "Exception caught: " << ex.what() << std::endl;
fail();
}
return;
}
static void tls_handshake(void **glob_state)
{
gnutls::server_session server;
gnutls::client_session client;
test_handshake(glob_state, "NORMAL", server, client);
}
static void tls_handshake_alt(void **glob_state)
{
gnutls::server_session server(0);
gnutls::client_session client(0);
test_handshake(glob_state, "NORMAL", server, client);
}
static void tls12_handshake(void **glob_state)
{
gnutls::server_session server;
gnutls::client_session client;
test_handshake(glob_state, "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.2", server, client);
}
static void tls13_handshake(void **glob_state)
{
gnutls::server_session server;
gnutls::client_session client;
test_handshake(glob_state, "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.3", server, client);
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(tls_handshake),
cmocka_unit_test(tls_handshake_alt),
cmocka_unit_test(tls13_handshake),
cmocka_unit_test(tls12_handshake)
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
|