Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / tests / mini-eagain-dtls.c
1 /*
2  * Copyright (C) 2008-2012 Free Software Foundation, Inc.
3  *
4  * Author: Simon Josefsson, 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 #include <string.h>
30 #include <errno.h>
31 #include <gnutls/gnutls.h>
32 #include <gnutls/crypto.h>
33 #include "utils.h"
34 #define RANDOMIZE
35 #include "eagain-common.h"
36
37 const char *side = "";
38
39 static void tls_log_func(int level, const char *str)
40 {
41         fprintf(stderr, "%s|<%d>| %s", side, level, str);
42 }
43
44 static int handshake = 0;
45
46 #define MAX_BUF 1024
47 #define MSG "Hello TLS, and hi and how are you and more data here... and more... and even more and even more more data..."
48
49 void doit(void)
50 {
51         /* Server stuff. */
52         gnutls_anon_server_credentials_t s_anoncred;
53         const gnutls_datum_t p3 = { (void *) pkcs3, strlen(pkcs3) };
54         static gnutls_dh_params_t dh_params;
55         gnutls_session_t server;
56         int sret, cret;
57         /* Client stuff. */
58         gnutls_anon_client_credentials_t c_anoncred;
59         gnutls_session_t client;
60         /* Need to enable anonymous KX specifically. */
61         char buffer[MAX_BUF + 1];
62         ssize_t ns;
63         int ret, transferred = 0, msglen;
64
65         /* General init. */
66         global_init();
67         gnutls_global_set_log_function(tls_log_func);
68         if (debug)
69                 gnutls_global_set_log_level(99);
70
71         /* Init server */
72         gnutls_anon_allocate_server_credentials(&s_anoncred);
73         gnutls_dh_params_init(&dh_params);
74         gnutls_dh_params_import_pkcs3(dh_params, &p3, GNUTLS_X509_FMT_PEM);
75         gnutls_anon_set_server_dh_params(s_anoncred, dh_params);
76         gnutls_init(&server,
77                     GNUTLS_SERVER | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK);
78         ret =
79             gnutls_priority_set_direct(server,
80                                        "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH",
81                                        NULL);
82         if (ret < 0)
83                 exit(1);
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_pull_timeout_function(server,
88                                                    server_pull_timeout_func);
89         gnutls_transport_set_ptr(server, server);
90
91         /* Init client */
92         gnutls_anon_allocate_client_credentials(&c_anoncred);
93         gnutls_init(&client,
94                     GNUTLS_CLIENT | GNUTLS_DATAGRAM | GNUTLS_NONBLOCK);
95         cret =
96             gnutls_priority_set_direct(client,
97                                        "NONE:+VERS-DTLS1.0:+CIPHER-ALL:+MAC-ALL:+SIGN-ALL:+COMP-ALL:+ANON-DH",
98                                        NULL);
99         if (cret < 0)
100                 exit(1);
101         gnutls_credentials_set(client, GNUTLS_CRD_ANON, c_anoncred);
102         gnutls_transport_set_push_function(client, client_push);
103         gnutls_transport_set_pull_function(client, client_pull);
104         gnutls_transport_set_pull_timeout_function(client,
105                                                    client_pull_timeout_func);
106         gnutls_transport_set_ptr(client, client);
107
108         handshake = 1;
109         HANDSHAKE(client, server);
110
111         handshake = 0;
112         if (debug)
113                 success("Handshake established\n");
114
115         do {
116                 ret = gnutls_record_send(client, MSG, strlen(MSG));
117         }
118         while (ret == GNUTLS_E_AGAIN);
119         //success ("client: sent %d\n", ns);
120
121         msglen = strlen(MSG);
122         TRANSFER(client, server, MSG, msglen, buffer, MAX_BUF);
123
124         if (debug)
125                 fputs("\n", stdout);
126
127         gnutls_bye(client, GNUTLS_SHUT_WR);
128         gnutls_bye(server, GNUTLS_SHUT_WR);
129
130         gnutls_deinit(client);
131         gnutls_deinit(server);
132
133         gnutls_anon_free_client_credentials(c_anoncred);
134         gnutls_anon_free_server_credentials(s_anoncred);
135
136         gnutls_dh_params_deinit(dh_params);
137
138         gnutls_global_deinit();
139 }