Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / tests / mini-overhead.c
1 /*
2  * Copyright (C) 2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
8  * GnuTLS is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuTLS is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with GnuTLS; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #if defined(_WIN32)
31
32 int main()
33 {
34         exit(77);
35 }
36
37 #else
38
39 #include <string.h>
40 #include <sys/types.h>
41 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #include <sys/wait.h>
44 #include <arpa/inet.h>
45 #include <unistd.h>
46 #include <gnutls/gnutls.h>
47 #include <gnutls/dtls.h>
48 #include <signal.h>
49
50 #include "utils.h"
51
52 static void terminate(void);
53
54 /* This program tests the robustness of record
55  * decoding.
56  */
57
58 static void server_log_func(int level, const char *str)
59 {
60 //  fprintf (stderr, "server|<%d>| %s", level, str);
61 }
62
63 static void client_log_func(int level, const char *str)
64 {
65         fprintf(stderr, "client|<%d>| %s", level, str);
66 }
67
68 static unsigned char server_cert_pem[] =
69     "-----BEGIN CERTIFICATE-----\n"
70     "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
71     "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
72     "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
73     "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
74     "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
75     "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
76     "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
77     "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
78     "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
79     "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
80     "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
81     "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
82     "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
83
84 const gnutls_datum_t server_cert = { server_cert_pem,
85         sizeof(server_cert_pem)
86 };
87
88 static unsigned char server_key_pem[] =
89     "-----BEGIN RSA PRIVATE KEY-----\n"
90     "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
91     "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
92     "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
93     "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
94     "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
95     "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
96     "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
97     "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
98     "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
99     "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
100     "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
101     "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
102     "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
103     "-----END RSA PRIVATE KEY-----\n";
104
105 const gnutls_datum_t server_key = { server_key_pem,
106         sizeof(server_key_pem)
107 };
108
109
110 /* A very basic TLS client, with anonymous authentication.
111  */
112
113 #define MAX_BUF 1024
114 #define MTU 1500
115
116 static void client(int fd, const char *prio, unsigned overhead)
117 {
118         int ret;
119         gnutls_anon_client_credentials_t anoncred;
120         gnutls_certificate_credentials_t x509_cred;
121         gnutls_session_t session;
122         /* Need to enable anonymous KX specifically. */
123
124         global_init();
125
126         if (debug) {
127                 gnutls_global_set_log_function(client_log_func);
128                 gnutls_global_set_log_level(7);
129         }
130
131         gnutls_anon_allocate_client_credentials(&anoncred);
132         gnutls_certificate_allocate_credentials(&x509_cred);
133
134         /* Initialize TLS session
135          */
136         gnutls_init(&session, GNUTLS_CLIENT | GNUTLS_DATAGRAM);
137
138         /* Use default priorities */
139         ret = gnutls_priority_set_direct(session, prio, NULL);
140         if (ret < 0) {
141                 fail("error in setting priority: %s\n", prio);
142                 exit(1);
143         }
144
145         /* put the anonymous credentials to the current session
146          */
147         gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
148         gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
149
150         gnutls_transport_set_int(session, fd);
151
152         /* Perform the TLS handshake
153          */
154         do {
155                 ret = gnutls_handshake(session);
156         }
157         while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
158
159         if (ret < 0) {
160                 fail("client: Handshake failed for %s\n", prio);
161                 gnutls_perror(ret);
162                 exit(1);
163         } else {
164                 if (debug)
165                         success("client: Handshake was completed\n");
166         }
167
168         if (debug)
169                 success("client: TLS version is: %s\n",
170                         gnutls_protocol_get_name
171                         (gnutls_protocol_get_version(session)));
172
173         gnutls_dtls_set_mtu(session, MTU);
174         ret = gnutls_dtls_get_data_mtu(session);
175
176         if (MTU - ret != (int) overhead) {
177                 fail("overhead for %s is %d, expected %u\n", prio,
178                      MTU - ret, overhead);
179                 exit(1);
180         }
181
182         close(fd);
183
184         gnutls_deinit(session);
185
186         gnutls_anon_free_client_credentials(anoncred);
187         gnutls_certificate_free_credentials(x509_cred);
188
189         gnutls_global_deinit();
190 }
191
192
193 /* These are global */
194 gnutls_anon_server_credentials_t anoncred;
195 gnutls_certificate_credentials_t x509_cred;
196 pid_t child;
197
198 static gnutls_session_t initialize_tls_session(const char *prio)
199 {
200         gnutls_session_t session;
201
202         gnutls_init(&session, GNUTLS_SERVER | GNUTLS_DATAGRAM);
203
204         /* avoid calling all the priority functions, since the defaults
205          * are adequate.
206          */
207         gnutls_priority_set_direct(session, prio, NULL);
208
209         gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
210         gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
211
212         return session;
213 }
214
215 static void terminate(void)
216 {
217         kill(child, SIGTERM);
218         exit(1);
219 }
220
221 static void server(int fd, const char *prio)
222 {
223         int ret;
224         char buffer[MAX_BUF + 1];
225         gnutls_session_t session;
226
227         /* this must be called once in the program
228          */
229         global_init();
230         memset(buffer, 0, sizeof(buffer));
231
232         if (debug) {
233                 gnutls_global_set_log_function(server_log_func);
234                 gnutls_global_set_log_level(4711);
235         }
236
237         gnutls_certificate_allocate_credentials(&x509_cred);
238         gnutls_certificate_set_x509_key_mem(x509_cred, &server_cert,
239                                             &server_key,
240                                             GNUTLS_X509_FMT_PEM);
241
242         gnutls_anon_allocate_server_credentials(&anoncred);
243
244         session = initialize_tls_session(prio);
245
246         gnutls_transport_set_int(session, fd);
247
248         do {
249                 ret = gnutls_handshake(session);
250         }
251         while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
252         if (ret < 0) {
253                 close(fd);
254                 gnutls_deinit(session);
255                 fail("server: Handshake has failed (%s) for %s\n\n",
256                      gnutls_strerror(ret), prio);
257                 terminate();
258         }
259         if (debug)
260                 success("server: Handshake was completed\n");
261
262         if (debug)
263                 success("server: TLS version is: %s\n",
264                         gnutls_protocol_get_name
265                         (gnutls_protocol_get_version(session)));
266         close(fd);
267         gnutls_deinit(session);
268
269         gnutls_anon_free_server_credentials(anoncred);
270         gnutls_certificate_free_credentials(x509_cred);
271
272         gnutls_global_deinit();
273
274         if (debug)
275                 success("server: finished\n");
276 }
277
278 static void start(const char *prio, unsigned overhead)
279 {
280         int fd[2];
281         int ret;
282
283         ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
284         if (ret < 0) {
285                 perror("socketpair");
286                 exit(1);
287         }
288
289         child = fork();
290         if (child < 0) {
291                 perror("fork");
292                 fail("fork");
293                 exit(1);
294         }
295
296         if (child) {
297                 /* parent */
298                 close(fd[1]);
299                 server(fd[0], prio);
300                 waitpid(-1, NULL, 0);
301                 //kill(child, SIGTERM);
302         } else {
303                 close(fd[0]);
304                 client(fd[1], prio, overhead);
305                 exit(0);
306         }
307 }
308
309 static void ch_handler(int sig)
310 {
311         int status = 0;
312
313         waitpid(-1, &status, 0);
314         if (WEXITSTATUS(status) != 0 ||
315             (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV)) {
316                 if (WIFSIGNALED(status))
317                         fail("Child died with sigsegv\n");
318                 else
319                         fail("Child died with status %d\n",
320                              WEXITSTATUS(status));
321                 terminate();
322         }
323         return;
324 }
325
326 void doit(void)
327 {
328         signal(SIGCHLD, ch_handler);
329
330         /* 13 + 20(sha1) + 16(iv) + 16(max pad) */
331         start
332             ("NONE:+VERS-DTLS1.0:+AES-128-CBC:+SHA1:+SIGN-ALL:+COMP-NULL:+RSA",
333              65);
334         /* 13 + 16(tag) + 4(iv) */
335         start
336             ("NONE:+VERS-DTLS1.2:+AES-128-GCM:+AEAD:+SIGN-ALL:+COMP-NULL:+RSA",
337              37);
338 #ifndef ENABLE_FIPS140
339         /* 13 + 20(tag) */
340         start
341             ("NONE:+VERS-DTLS1.0:+SALSA20-256:+SHA1:+SIGN-ALL:+COMP-NULL:+RSA",
342              33);
343 #endif
344 }
345
346 #endif                          /* _WIN32 */