Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / tests / long-session-id.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 static int debug = 0;
51 static void terminate(int);
52
53 /* This program tests the robustness of record
54  * decoding.
55  */
56
57 static void client_log_func(int level, const char *str)
58 {
59         fprintf(stderr, "client|<%d>| %s", level, str);
60 }
61
62 static unsigned char server_cert_pem[] =
63     "-----BEGIN CERTIFICATE-----\n"
64     "MIICVjCCAcGgAwIBAgIERiYdMTALBgkqhkiG9w0BAQUwGTEXMBUGA1UEAxMOR251\n"
65     "VExTIHRlc3QgQ0EwHhcNMDcwNDE4MTMyOTIxWhcNMDgwNDE3MTMyOTIxWjA3MRsw\n"
66     "GQYDVQQKExJHbnVUTFMgdGVzdCBzZXJ2ZXIxGDAWBgNVBAMTD3Rlc3QuZ251dGxz\n"
67     "Lm9yZzCBnDALBgkqhkiG9w0BAQEDgYwAMIGIAoGA17pcr6MM8C6pJ1aqU46o63+B\n"
68     "dUxrmL5K6rce+EvDasTaDQC46kwTHzYWk95y78akXrJutsoKiFV1kJbtple8DDt2\n"
69     "DZcevensf9Op7PuFZKBroEjOd35znDET/z3IrqVgbtm2jFqab7a+n2q9p/CgMyf1\n"
70     "tx2S5Zacc1LWn9bIjrECAwEAAaOBkzCBkDAMBgNVHRMBAf8EAjAAMBoGA1UdEQQT\n"
71     "MBGCD3Rlc3QuZ251dGxzLm9yZzATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHQ8B\n"
72     "Af8EBQMDB6AAMB0GA1UdDgQWBBTrx0Vu5fglyoyNgw106YbU3VW0dTAfBgNVHSME\n"
73     "GDAWgBTpPBz7rZJu5gakViyi4cBTJ8jylTALBgkqhkiG9w0BAQUDgYEAaFEPTt+7\n"
74     "bzvBuOf7+QmeQcn29kT6Bsyh1RHJXf8KTk5QRfwp6ogbp94JQWcNQ/S7YDFHglD1\n"
75     "AwUNBRXwd3riUsMnsxgeSDxYBfJYbDLeohNBsqaPDJb7XailWbMQKfAbFQ8cnOxg\n"
76     "rOKLUQRWJ0K3HyXRMhbqjdLIaQiCvQLuizo=\n" "-----END CERTIFICATE-----\n";
77
78 const gnutls_datum_t server_cert = { server_cert_pem,
79         sizeof(server_cert_pem)
80 };
81
82 static unsigned char server_key_pem[] =
83     "-----BEGIN RSA PRIVATE KEY-----\n"
84     "MIICXAIBAAKBgQDXulyvowzwLqknVqpTjqjrf4F1TGuYvkrqtx74S8NqxNoNALjq\n"
85     "TBMfNhaT3nLvxqResm62ygqIVXWQlu2mV7wMO3YNlx696ex/06ns+4VkoGugSM53\n"
86     "fnOcMRP/PciupWBu2baMWppvtr6far2n8KAzJ/W3HZLllpxzUtaf1siOsQIDAQAB\n"
87     "AoGAYAFyKkAYC/PYF8e7+X+tsVCHXppp8AoP8TEZuUqOZz/AArVlle/ROrypg5kl\n"
88     "8YunrvUdzH9R/KZ7saNZlAPLjZyFG9beL/am6Ai7q7Ma5HMqjGU8kTEGwD7K+lbG\n"
89     "iomokKMOl+kkbY/2sI5Czmbm+/PqLXOjtVc5RAsdbgvtmvkCQQDdV5QuU8jap8Hs\n"
90     "Eodv/tLJ2z4+SKCV2k/7FXSKWe0vlrq0cl2qZfoTUYRnKRBcWxc9o92DxK44wgPi\n"
91     "oMQS+O7fAkEA+YG+K9e60sj1K4NYbMPAbYILbZxORDecvP8lcphvwkOVUqbmxOGh\n"
92     "XRmTZUuhBrJhJKKf6u7gf3KWlPl6ShKEbwJASC118cF6nurTjuLf7YKARDjNTEws\n"
93     "qZEeQbdWYINAmCMj0RH2P0mvybrsXSOD5UoDAyO7aWuqkHGcCLv6FGG+qwJAOVqq\n"
94     "tXdUucl6GjOKKw5geIvRRrQMhb/m5scb+5iw8A4LEEHPgGiBaF5NtJZLALgWfo5n\n"
95     "hmC8+G8F0F78znQtPwJBANexu+Tg5KfOnzSILJMo3oXiXhf5PqXIDmbN0BKyCKAQ\n"
96     "LfkcEcUbVfmDaHpvzwY9VEaoMOKVLitETXdNSxVpvWM=\n"
97     "-----END RSA PRIVATE KEY-----\n";
98
99 const gnutls_datum_t server_key = { server_key_pem,
100         sizeof(server_key_pem)
101 };
102
103
104 /* A very basic TLS client, with anonymous authentication.
105  */
106
107 static void client(int fd, const char *prio)
108 {
109         int ret;
110         gnutls_anon_client_credentials_t anoncred;
111         gnutls_certificate_credentials_t x509_cred;
112         gnutls_session_t session;
113         /* Need to enable anonymous KX specifically. */
114
115         gnutls_global_init();
116
117         if (debug) {
118                 gnutls_global_set_log_function(client_log_func);
119                 gnutls_global_set_log_level(7);
120         }
121
122         gnutls_anon_allocate_client_credentials(&anoncred);
123         gnutls_certificate_allocate_credentials(&x509_cred);
124
125         /* Initialize TLS session
126          */
127         gnutls_init(&session, GNUTLS_CLIENT);
128
129         /* Use default priorities */
130         gnutls_priority_set_direct(session, prio, NULL);
131
132         /* put the anonymous credentials to the current session
133          */
134         gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred);
135         gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
136
137         gnutls_transport_set_int(session, fd);
138
139         /* Perform the TLS handshake
140          */
141         do {
142                 ret = gnutls_handshake(session);
143         }
144         while (ret < 0 && gnutls_error_is_fatal(ret) == 0);
145
146         if (gnutls_ecc_curve_get(session) == 0xffffffff) {
147                 fprintf(stderr, "memory was overwritten\n");
148                 kill(getpid(), SIGSEGV);
149         }
150
151         if (ret < 0) {
152                 fprintf(stderr, "client: Handshake failed (expected)\n");
153                 gnutls_perror(ret);
154                 exit(0);
155         } else {
156                 if (debug)
157                         fprintf(stderr, "client: Handshake was completed\n");
158         }
159
160         close(fd);
161
162         gnutls_deinit(session);
163
164         gnutls_anon_free_client_credentials(anoncred);
165         gnutls_certificate_free_credentials(x509_cred);
166
167         gnutls_global_deinit();
168 }
169
170
171 /* These are global */
172 pid_t child;
173
174 static void terminate(int ret)
175 {
176         kill(child, SIGTERM);
177         exit(ret);
178 }
179
180 static void server(int fd, const char *prio)
181 {
182         int ret;
183         uint8_t id[255];
184         uint8_t buffer[] = "\x16\x03\x00\x01\x25"
185                 "\x02\x00\x01\x21"
186                 "\x03\x00"/*Server Version */
187                 /*Random*/"\x00\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00\x00\x00\x00\x01\x00\x00"
188                 /*SessionID*/"\xfe";
189
190         ret = read(fd, id, sizeof(id));
191         if (ret < 0) {
192                 abort();
193         }
194
195         ret = write(fd, buffer, sizeof(buffer));
196         if (ret < 0) {
197                 return;
198         }
199
200         memset(id, 0xff, sizeof(id));
201         ret = write(fd, id, sizeof(id));
202         if (ret < 0) {
203                 return;
204         }
205
206         memset(id, 0xff, sizeof(id));
207         ret = write(fd, id, sizeof(id));
208         if (ret < 0) {
209                 return;
210         }
211         sleep(3);
212
213         return;
214 }
215
216 static void start(const char *prio)
217 {
218         int fd[2];
219         int ret;
220
221         ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);
222         if (ret < 0) {
223                 perror("socketpair");
224                 exit(1);
225         }
226
227         child = fork();
228         if (child < 0) {
229                 perror("fork");
230                 exit(1);
231         }
232
233         if (child) {
234                 /* parent */
235                 close(fd[1]);
236                 server(fd[0], prio);
237                 kill(child, SIGTERM);
238         } else {
239                 close(fd[0]);
240                 client(fd[1], prio);
241                 exit(0);
242         }
243 }
244
245 static void ch_handler(int sig)
246 {
247         int status, ret = 0;
248         wait(&status);
249         if (WEXITSTATUS(status) != 0 ||
250             (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV)) {
251                 if (WIFSIGNALED(status)) {
252                         fprintf(stderr, "Child died with sigsegv\n");
253                         ret = 1;
254                 } else {
255                         fprintf(stderr, "Child died with status %d\n",
256                              WEXITSTATUS(status));
257                 }
258                 terminate(ret);
259         }
260         return;
261 }
262
263 int main(int argc, char **argv)
264 {
265         signal(SIGCHLD, ch_handler);
266         signal(SIGPIPE, SIG_IGN);
267
268         if (argc > 1)
269                 debug = 1;
270
271         start("NORMAL");
272         return 0;
273 }
274
275 #endif                          /* _WIN32 */