Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / tests / record-sizes.c
1 /*
2  * Copyright (C) 2008-2012 Free Software Foundation, Inc.
3  *
4  * Author: Simon Josefsson
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 #include <string.h>
30 #include <errno.h>
31 #include <gnutls/gnutls.h>
32 #include "eagain-common.h"
33
34 #include "utils.h"
35
36 const char *side = "";
37
38 static void tls_log_func(int level, const char *str)
39 {
40         fprintf(stderr, "%s|<%d>| %s", side, level, str);
41 }
42
43 /* This test attempts to transfer various sizes using ARCFOUR-128.
44  */
45
46 #ifndef ENABLE_FIPS140
47
48 #define MAX_BUF 16384
49 static char b1[MAX_BUF + 1];
50 static char buffer[MAX_BUF + 1];
51
52 void doit(void)
53 {
54         /* Server stuff. */
55         gnutls_anon_server_credentials_t s_anoncred;
56         const gnutls_datum_t p3 =
57             { (unsigned char *) pkcs3, strlen(pkcs3) };
58         static gnutls_dh_params_t dh_params;
59         gnutls_session_t server;
60         int sret = GNUTLS_E_AGAIN;
61         /* Client stuff. */
62         gnutls_anon_client_credentials_t c_anoncred;
63         gnutls_session_t client;
64         int cret = GNUTLS_E_AGAIN, i;
65         /* Need to enable anonymous KX specifically. */
66         ssize_t ns;
67         int ret, transferred = 0;
68
69         /* General init. */
70         global_init();
71         gnutls_global_set_log_function(tls_log_func);
72         if (debug)
73                 gnutls_global_set_log_level(4711);
74
75         /* Init server */
76         gnutls_anon_allocate_server_credentials(&s_anoncred);
77         gnutls_dh_params_init(&dh_params);
78         gnutls_dh_params_import_pkcs3(dh_params, &p3, GNUTLS_X509_FMT_PEM);
79         gnutls_anon_set_server_dh_params(s_anoncred, dh_params);
80         gnutls_init(&server, GNUTLS_SERVER);
81         gnutls_priority_set_direct(server,
82                                    "NONE:+VERS-TLS-ALL:+ARCFOUR-128:+MAC-ALL:+SIGN-ALL:+COMP-NULL:+ANON-DH",
83                                    NULL);
84         gnutls_credentials_set(server, GNUTLS_CRD_ANON, s_anoncred);
85         gnutls_transport_set_push_function(server, server_push);
86         gnutls_transport_set_pull_function(server, server_pull);
87         gnutls_transport_set_ptr(server, server);
88
89         /* Init client */
90         gnutls_anon_allocate_client_credentials(&c_anoncred);
91         gnutls_init(&client, GNUTLS_CLIENT);
92         gnutls_priority_set_direct(client,
93                                    "NONE:+VERS-TLS-ALL:+CIPHER-ALL:+ARCFOUR-128:+MAC-ALL:+SIGN-ALL:+COMP-NULL:+ANON-DH",
94                                    NULL);
95         gnutls_credentials_set(client, GNUTLS_CRD_ANON, c_anoncred);
96         gnutls_transport_set_push_function(client, client_push);
97         gnutls_transport_set_pull_function(client, client_pull);
98         gnutls_transport_set_ptr(client, client);
99
100         memset(b1, 0, sizeof(b1));
101         HANDSHAKE(client, server);
102
103         if (debug)
104                 success("Handshake established\n");
105
106         memset(b1, 1, MAX_BUF);
107
108         /* try the maximum allowed */
109         ret = gnutls_record_send(client, b1, MAX_BUF);
110         if (ret < 0) {
111                 fprintf(stderr, "Error sending %d bytes: %s\n",
112                         (int) MAX_BUF, gnutls_strerror(ret));
113                 exit(1);
114         }
115
116         if (ret != MAX_BUF) {
117                 fprintf(stderr, "Couldn't send %d bytes\n", (int) MAX_BUF);
118                 exit(1);
119         }
120
121         ret = gnutls_record_recv(server, buffer, MAX_BUF);
122         if (ret < 0) {
123                 fprintf(stderr, "Error receiving %d bytes: %s\n",
124                         (int) MAX_BUF, gnutls_strerror(ret));
125                 exit(1);
126         }
127
128         if (ret != MAX_BUF) {
129                 fprintf(stderr, "Couldn't receive %d bytes, received %d\n",
130                         (int) MAX_BUF, ret);
131                 exit(1);
132         }
133
134         if (memcmp(b1, buffer, MAX_BUF) != 0) {
135                 fprintf(stderr, "Buffers do not match!\n");
136                 exit(1);
137         }
138
139         /* Try sending various other sizes */
140         for (i = 1; i < 128; i++) {
141                 TRANSFER(client, server, b1, i, buffer, MAX_BUF);
142         }
143         if (debug)
144                 fputs("\n", stdout);
145
146
147
148         gnutls_bye(client, GNUTLS_SHUT_RDWR);
149         gnutls_bye(server, GNUTLS_SHUT_RDWR);
150
151         gnutls_deinit(client);
152         gnutls_deinit(server);
153
154         gnutls_anon_free_client_credentials(c_anoncred);
155         gnutls_anon_free_server_credentials(s_anoncred);
156
157         gnutls_dh_params_deinit(dh_params);
158
159         gnutls_global_deinit();
160 }
161
162 #else
163 void doit(void)
164 {
165         exit(77);
166 }
167 #endif