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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/*
* Copyright (C) 2016-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 <stdio.h>
#include <stdlib.h>
#if defined(_WIN32)
/* socketpair isn't supported on Win32. */
int main(int argc, char **argv)
{
exit(77);
}
#else
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#if !defined(_WIN32)
#include <sys/wait.h>
#include <signal.h>
#endif
#include <unistd.h>
#include <gnutls/gnutls.h>
#include <assert.h>
#include "utils.h"
/* This test checks whether a large certificate packet can be sent by
* server and received by client. */
const char *side = "";
static void tls_log_func(int level, const char *str)
{
fprintf(stderr, "%s|<%d>| %s", side, level, str);
}
static void client(int sd, const char *prio)
{
int ret;
gnutls_session_t session;
char buf[1];
gnutls_certificate_credentials_t clientx509cred;
global_init();
gnutls_global_set_log_function(tls_log_func);
if (debug)
gnutls_global_set_log_level(4711);
side = "client";
assert(gnutls_certificate_allocate_credentials(&clientx509cred)>=0);
assert(gnutls_init(&session, GNUTLS_CLIENT)>=0);
assert(gnutls_priority_set_direct(session, prio, NULL) >= 0);
/* put the anonymous credentials to the current session
*/
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
clientx509cred);
gnutls_transport_set_int(session, sd);
gnutls_handshake_set_timeout(session, 60 * 1000);
do {
ret = gnutls_handshake(session);
} while(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
if (ret < 0) {
fail("client: Handshake failed\n");
} else {
if (debug)
success("client: Handshake was completed\n");
}
do {
ret = gnutls_record_recv(session, buf, 1);
} while(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
assert(ret == 0);
close(sd);
gnutls_deinit(session);
gnutls_certificate_free_credentials(clientx509cred);
gnutls_global_deinit();
}
static unsigned char server_key_pem[] =
"-----BEGIN RSA PRIVATE KEY-----\n"
"MIIEowIBAAKCAQEAwJo7662RNezA254WRsXlbuzkPV5eNX7pX5Pj66T8/RKfz7rL\n"
"GpKs4fNxhhIMSUDjy2KbEQXPUH9MWROgU8V//CfcnagLYCHtniqRE+eXv0fCLwWU\n"
"SrA8n0qCBj94/NMA4kSmHf6dE5YdjDSpukyza5IshAuxZ32MDevE3JqMjvnZ5vY7\n"
"drJSfal0V5gof3/7J41ZVxl+WJph50e2pY1E27/hY8q5yQ3DXnE5kTQjX664ozQ8\n"
"UPtqGfkr4YjYe4e6PUWAjU27mQng0O0K+/w8gg6xBxN/AH3U7dg5/cY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rHrY59yQaH1rwg7iO1PmhvAoRqb2DTSl+OHMn+WeR\n"
"eU5R2dRc3QysU60wxMy2QxVyG4vCfedUW0ABuutAVZARWOp0Y/khHluzscu57O/h\n"
"q3/ZIQKBgEXHmOjftWrkWV+/zfZT64k2Z1g7s3dpXW/SFK9jPrt6oqI1GNkYz6Ds\n"
"O1dUiPsNXDCLytUtvYrvrT3rJaPjJDRU2HrN/cYdxXgf6HSEr3Cdcpqyp/5rOOxD\n"
"ALEix6R4MZlsQV8FfgWjvTAET7NtY303JrCdFPqIigwl/PFGPLiB\n"
"-----END RSA PRIVATE KEY-----\n";
static void server(int sd, const char *prio)
{
gnutls_certificate_credentials_t serverx509cred;
const gnutls_datum_t key = { server_key_pem,
sizeof(server_key_pem)-1
};
int ret;
gnutls_session_t session;
gnutls_datum_t cert;
const char *src = getenv("srcdir");
char cert_path[256];
if (src == NULL)
src = ".";
global_init();
gnutls_global_set_log_function(tls_log_func);
if (debug)
gnutls_global_set_log_level(4711);
side = "server";
snprintf(cert_path, sizeof(cert_path), "%s/data/large-cert.pem", src);
assert(gnutls_load_file(cert_path, &cert)>=0);
assert(gnutls_certificate_allocate_credentials(&serverx509cred)>=0);
assert(gnutls_certificate_set_x509_key_mem(serverx509cred,
&cert, &key,
GNUTLS_X509_FMT_PEM)>=0);
gnutls_free(cert.data);
assert(gnutls_init(&session, GNUTLS_SERVER)>=0);
assert(gnutls_priority_set_direct(session, prio, NULL) >= 0);
gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE,
serverx509cred);
gnutls_transport_set_int(session, sd);
gnutls_handshake_set_timeout(session, 60 * 1000);
do {
ret = gnutls_handshake(session);
} while(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED);
if (ret < 0) {
close(sd);
gnutls_deinit(session);
fail("server: Handshake has failed (%s)\n\n",
gnutls_strerror(ret));
return;
}
if (debug)
success("server: Handshake was completed\n");
gnutls_bye(session, GNUTLS_SHUT_WR);
close(sd);
gnutls_deinit(session);
gnutls_certificate_free_credentials(serverx509cred);
gnutls_global_deinit();
if (debug)
success("server: finished\n");
}
static
void start(const char *name, const char *prio)
{
pid_t child;
int sockets[2];
int err;
success("testing %s\n", name);
signal(SIGPIPE, SIG_IGN);
signal(SIGCHLD, SIG_IGN);
err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
if (err == -1) {
perror("socketpair");
fail("socketpair failed\n");
return;
}
child = fork();
if (child < 0) {
perror("fork");
fail("fork");
return;
}
if (child) {
int status = 0;
/* parent */
close(sockets[0]);
client(sockets[1], prio);
wait(&status);
check_wait_status(status);
} else {
close(sockets[1]);
server(sockets[0], prio);
exit(0);
}
}
void doit(void)
{
start("tls1.2", "NORMAL:-VERS-ALL:+VERS-TLS1.2");
start("tls1.3", "NORMAL:-VERS-ALL:+VERS-TLS1.3");
start("default", "NORMAL");
}
#endif /* _WIN32 */
|