Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / tests / multi-alerts.c
1 /*
2  * Copyright (C) 2016 Red Hat, 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 /* In this test we check whether the server will bail out after receiving
31  * a bunch of warning alerts. That is to avoid DoS due to the assymetry of
32  * cost of sending an alert vs the cost of receiving.
33  */
34
35 #if defined(_WIN32)
36
37 /* socketpair isn't supported on Win32. */
38 int main(int argc, char **argv)
39 {
40         exit(77);
41 }
42
43 #else
44
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #if !defined(_WIN32)
49 #include <sys/wait.h>
50 #endif
51 #include <unistd.h>
52 #include <gnutls/gnutls.h>
53
54 #include "utils.h"
55 #include "cert-common.h"
56
57 pid_t child;
58
59 static void tls_log_func(int level, const char *str)
60 {
61         fprintf(stderr, "%s |<%d>| %s", child ? "server" : "client", level,
62                 str);
63 }
64
65 static unsigned char tls_hello[] =
66         "\x16\x03\x01\x01\x38\x01\x00\x01"
67         "\x34\x03\x03\xfc\x77\xa8\xc7\x46"
68         "\xf7\xfd\x04\x5b\x3c\xc6\xfa\xa4"
69         "\xea\x3e\xfa\x76\x99\xfe\x1a\x2e"
70         "\xe0\x79\x17\xb2\x27\x06\xc4\x5c"
71         "\xd8\x78\x31\x00\x00\xb6\xc0\x30"
72         "\xc0\x2c\xc0\x28\xc0\x24\xc0\x14"
73         "\xc0\x0a\x00\xa5\x00\xa3\x00\xa1"
74         "\x00\x9f\x00\x6b\x00\x6a\x00\x69"
75         "\x00\x68\x00\x39\x00\x38\x00\x37"
76         "\x00\x36\x00\x88\x00\x87\x00\x86"
77         "\x00\x85\xc0\x32\xc0\x2e\xc0\x2a"
78         "\xc0\x26\xc0\x0f\xc0\x05\x00\x9d"
79         "\x00\x3d\x00\x35\x00\x84\xc0\x2f"
80         "\xc0\x2b\xc0\x27\xc0\x23\xc0\x13"
81         "\xc0\x09\x00\xa4\x00\xa2\x00\xa0"
82         "\x00\x9e\x00\x67\x00\x40\x00\x3f"
83         "\x00\x3e\x00\x33\x00\x32\x00\x31"
84         "\x00\x30\x00\x9a\x00\x99\x00\x98"
85         "\x00\x97\x00\x45\x00\x44\x00\x43"
86         "\x00\x42\xc0\x31\xc0\x2d\xc0\x29"
87         "\xc0\x25\xc0\x0e\xc0\x04\x00\x9c"
88         "\x00\x3c\x00\x2f\x00\x96\x00\x41"
89         "\x00\x07\xc0\x11\xc0\x07\xc0\x0c"
90         "\xc0\x02\x00\x05\x00\x04\xc0\x12"
91         "\xc0\x08\x00\x16\x00\x13\x00\x10"
92         "\x00\x0d\xc0\x0d\xc0\x03\x00\x0a"
93         "\x00\x15\x00\x12\x00\x0f\x00\x0c"
94         "\x00\x09\x00\xff\x01\x00\x00\x55"
95         "\x00\x0b\x00\x04\x03\x00\x01\x02"
96         "\x00\x0a\x00\x1c\x00\x1a\x00\x17"
97         "\x00\x19\x00\x1c\x00\x1b\x00\x18"
98         "\x00\x1a\x00\x16\x00\x0e\x00\x0d"
99         "\x00\x0b\x00\x0c\x00\x09\x00\x0a"
100         "\x00\x23\x00\x00\x00\x0d\x00\x20"
101         "\x00\x1e\x06\x01\x06\x02\x06\x03"
102         "\x05\x01\x05\x02\x05\x03\x04\x01"
103         "\x04\x02\x04\x03\x03\x01\x03\x02"
104         "\x03\x03\x02\x01\x02\x02\x02\x03"
105         "\x00\x0f\x00\x01\x01";
106
107 static unsigned char tls_alert[] = 
108         "\x15\x03\x03\x00\x02\x00\x0A";
109
110 static void client(int sd)
111 {
112         char buf[1024];
113         int ret;
114         unsigned i;
115
116         /* send a TLS hello, and then a list of warning alerts */
117
118         ret = send(sd, tls_hello, sizeof(tls_hello)-1, 0);
119         if (ret < 0)
120                 fail("error sending hello\n");
121
122         ret = recv(sd, buf, sizeof(buf), 0);
123         if (ret < 0)
124                 fail("error receiving hello\n");
125
126         for (i=0;i<128;i++) {
127                 ret = send(sd, tls_alert, sizeof(tls_alert)-1, 0);
128                 if (ret < 0)
129                         fail("error sending hello\n");
130         }
131
132         close(sd);
133 }
134
135 static void server(int sd)
136 {
137         gnutls_certificate_credentials_t x509_cred;
138         gnutls_session_t session;
139         int ret;
140         unsigned loops;
141
142         /* this must be called once in the program
143          */
144         global_init();
145
146         gnutls_global_set_log_function(tls_log_func);
147         if (debug)
148                 gnutls_global_set_log_level(6);
149
150         gnutls_certificate_allocate_credentials(&x509_cred);
151         gnutls_certificate_set_x509_trust_mem(x509_cred, &ca3_cert,
152                                               GNUTLS_X509_FMT_PEM);
153
154         gnutls_certificate_set_x509_key_mem(x509_cred, &server_ca3_localhost_cert,
155                                             &server_ca3_key,
156                                             GNUTLS_X509_FMT_PEM);
157
158         if (debug)
159                 success("Launched, generating DH parameters...\n");
160
161         gnutls_init(&session, GNUTLS_SERVER);
162
163         /* avoid calling all the priority functions, since the defaults
164          * are adequate.
165          */
166         gnutls_priority_set_direct(session, "NORMAL", NULL);
167
168         gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, x509_cred);
169
170         gnutls_transport_set_int(session, sd);
171         loops = 0;
172         do {
173                 ret = gnutls_handshake(session);
174                 loops++;
175                 if (loops > 64)
176                         fail("Too many loops in the handshake!\n");
177         } while (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_WARNING_ALERT_RECEIVED);
178
179         if (ret >= 0) {
180                 fail("server: Handshake succeeded unexpectedly\n");
181         }
182
183         close(sd);
184         gnutls_deinit(session);
185
186         gnutls_certificate_free_credentials(x509_cred);
187
188         gnutls_global_deinit();
189
190         if (debug)
191                 success("server: finished\n");
192 }
193
194
195 void doit(void)
196 {
197         int sockets[2];
198         int err;
199
200         err = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
201         if (err == -1) {
202                 perror("socketpair");
203                 fail("socketpair failed\n");
204                 return;
205         }
206
207         child = fork();
208         if (child < 0) {
209                 perror("fork");
210                 fail("fork");
211                 return;
212         }
213
214         if (child) {
215                 int status;
216
217                 server(sockets[0]);
218                 wait(&status);
219         } else
220                 client(sockets[1]);
221 }
222
223 #endif                          /* _WIN32 */