Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / tests / utils.c
1 /*
2  * Copyright (C) 2004-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 <stdarg.h>
30 #include <time.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <assert.h>
34 #ifndef _WIN32
35 # include <netinet/in.h>
36 # include <sys/socket.h>
37 #else
38 #ifdef _WIN32
39 # include <windows.h>           /* for Sleep */
40 # include <winbase.h>  
41 #endif
42 #endif
43 #include <gnutls/gnutls.h>
44 #include <gnutls/crypto.h>
45
46 #include "utils.h"
47
48 int debug = 0;
49 int error_count = 0;
50 int break_on_error = 0;
51
52 const char *pkcs3 =
53     "-----BEGIN DH PARAMETERS-----\n"
54     "MIGGAoGAtkxw2jlsVCsrfLqxrN+IrF/3W8vVFvDzYbLmxi2GQv9s/PQGWP1d9i22\n"
55     "P2DprfcJknWt7KhCI1SaYseOQIIIAYP78CfyIpGScW/vS8khrw0rlQiyeCvQgF3O\n"
56     "GeGOEywcw+oQT4SmFOD7H0smJe2CNyjYpexBXQ/A0mbTF9QKm1cCAQU=\n"
57     "-----END DH PARAMETERS-----\n";
58
59 void fail(const char *format, ...)
60 {
61         char str[1024];
62         va_list arg_ptr;
63
64         va_start(arg_ptr, format);
65         vsnprintf(str, sizeof(str), format, arg_ptr);
66         va_end(arg_ptr);
67         fputs(str, stderr);
68         error_count++;
69         exit(1);
70 }
71
72 void sec_sleep(int sec)
73 {
74         int ret;
75 #ifdef HAVE_NANOSLEEP
76         struct timespec ts;
77
78         ts.tv_sec = sec;
79         ts.tv_nsec = 0;
80         do {
81                 ret = nanosleep(&ts, NULL);
82         } while (ret == -1 && errno == EINTR);
83         if (ret == -1)
84                 abort();
85 #else
86         do {
87                 ret = sleep(sec);
88         } while (ret == -1 && errno == EINTR);
89 #endif
90 }
91
92 void success(const char *format, ...)
93 {
94         char str[1024];
95         va_list arg_ptr;
96
97         va_start(arg_ptr, format);
98         vsnprintf(str, sizeof(str), format, arg_ptr);
99         va_end(arg_ptr);
100         fputs(str, stderr);
101 }
102
103 void escapeprint(const char *str, size_t len)
104 {
105         size_t i;
106
107         printf(" (length %d bytes):\n\t", (int) len);
108         for (i = 0; i < len; i++) {
109                 if (((str[i] & 0xFF) >= 'A' && (str[i] & 0xFF) <= 'Z') ||
110                     ((str[i] & 0xFF) >= 'a' && (str[i] & 0xFF) <= 'z') ||
111                     ((str[i] & 0xFF) >= '0' && (str[i] & 0xFF) <= '9')
112                     || (str[i] & 0xFF) == ' ' || (str[i] & 0xFF) == '.')
113                         printf("%c", (str[i] & 0xFF));
114                 else
115                         printf("\\x%02X", (str[i] & 0xFF));
116                 if ((i + 1) % 16 == 0 && (i + 1) < len)
117                         printf("'\n\t'");
118         }
119         printf("\n");
120 }
121
122 void hexprint(const void *_str, size_t len)
123 {
124         size_t i;
125         const char *str = _str;
126
127         printf("\t;; ");
128         for (i = 0; i < len; i++) {
129                 printf("%02x ", (str[i] & 0xFF));
130                 if ((i + 1) % 8 == 0)
131                         printf(" ");
132                 if ((i + 1) % 16 == 0 && i + 1 < len)
133                         printf("\n\t;; ");
134         }
135         printf("\n");
136 }
137
138 void binprint(const void *_str, size_t len)
139 {
140         size_t i;
141         const char *str = _str;
142
143         printf("\t;; ");
144         for (i = 0; i < len; i++) {
145                 printf("%d%d%d%d%d%d%d%d ",
146                        (str[i] & 0xFF) & 0x80 ? 1 : 0,
147                        (str[i] & 0xFF) & 0x40 ? 1 : 0,
148                        (str[i] & 0xFF) & 0x20 ? 1 : 0,
149                        (str[i] & 0xFF) & 0x10 ? 1 : 0,
150                        (str[i] & 0xFF) & 0x08 ? 1 : 0,
151                        (str[i] & 0xFF) & 0x04 ? 1 : 0,
152                        (str[i] & 0xFF) & 0x02 ? 1 : 0,
153                        (str[i] & 0xFF) & 0x01 ? 1 : 0);
154                 if ((i + 1) % 3 == 0)
155                         printf(" ");
156                 if ((i + 1) % 6 == 0 && i + 1 < len)
157                         printf("\n\t;; ");
158         }
159         printf("\n");
160 }
161
162 int main(int argc, char *argv[])
163 {
164         do
165                 if (strcmp(argv[argc - 1], "-v") == 0 ||
166                     strcmp(argv[argc - 1], "--verbose") == 0)
167                         debug = 1;
168                 else if (strcmp(argv[argc - 1], "-b") == 0 ||
169                          strcmp(argv[argc - 1], "--break-on-error") == 0)
170                         break_on_error = 1;
171                 else if (strcmp(argv[argc - 1], "-h") == 0 ||
172                          strcmp(argv[argc - 1], "-?") == 0 ||
173                          strcmp(argv[argc - 1], "--help") == 0) {
174                         printf
175                             ("Usage: %s [-vbh?] [--verbose] [--break-on-error] [--help]\n",
176                              argv[0]);
177                         return 1;
178                 }
179         while (argc-- > 1);
180
181         doit();
182
183         if (debug || error_count > 0)
184                 printf("Self test `%s' finished with %d errors\n", argv[0],
185                        error_count);
186
187         return error_count ? 1 : 0;
188 }
189
190 struct tmp_file_st {
191         char file[TMPNAME_SIZE];
192         struct tmp_file_st *next;
193 };
194
195 static struct tmp_file_st *temp_files = (void*)-1;
196
197 static void append(const char *file)
198 {
199         struct tmp_file_st *p;
200
201         if (temp_files == (void*)-1)
202                 return;
203
204         p = calloc(1, sizeof(*p));
205
206         assert(p != NULL);
207         strcpy(p->file, file);
208         p->next = temp_files;
209         temp_files = p;
210 }
211
212 char *get_tmpname(char s[TMPNAME_SIZE])
213 {
214         unsigned char rnd[6];
215         static char _s[TMPNAME_SIZE];
216         int ret;
217         char *p;
218         const char *path;
219
220         ret = gnutls_rnd(GNUTLS_RND_NONCE, rnd, sizeof(rnd));
221         if (ret < 0)
222                 return NULL;
223
224         path = getenv("builddir");
225         if (path == NULL)
226                 path = ".";
227
228         if (s == NULL)
229                 p = _s;
230         else
231                 p = s;
232
233         snprintf(p, TMPNAME_SIZE, "%s/tmpfile-%02x%02x%02x%02x%02x%02x.tmp", path, (unsigned)rnd[0], (unsigned)rnd[1],
234                 (unsigned)rnd[2], (unsigned)rnd[3], (unsigned)rnd[4], (unsigned)rnd[5]);
235
236         append(p);
237
238         return p;
239 }
240
241 void track_temp_files(void)
242 {
243         temp_files = NULL;
244 }
245
246 void delete_temp_files(void)
247 {
248         struct tmp_file_st *p = temp_files;
249         struct tmp_file_st *next;
250
251         if (p == (void*)-1)
252                 return;
253
254         while(p != NULL) {
255                 next = p->next;
256                 free(p);
257                 p = next;
258         }
259 }