Fix CVE-2017-6891 in minitasn1 code
[platform/upstream/gnutls.git] / tests / priorities.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 #include <string.h>
30 #include <gnutls/gnutls.h>
31
32 #include "utils.h"
33
34 #if defined(ENABLE_FIPS140) || defined(ENABLE_ARCFOUR128)
35 void doit(void)
36 {
37         exit(77);
38 }
39
40 #else
41
42 static void
43 try_prio(const char *prio, unsigned expected_cs, unsigned expected_ciphers)
44 {
45         int ret;
46         gnutls_priority_t p;
47         const char *err;
48         const unsigned int *t;
49         unsigned i, si, count = 0;
50
51         /* this must be called once in the program
52          */
53         global_init();
54
55         ret = gnutls_priority_init(&p, prio, &err);
56         if (ret < 0) {
57                 fprintf(stderr, "error: %s: %s\n", gnutls_strerror(ret),
58                         err);
59                 exit(1);
60         }
61
62         for (i = 0;; i++) {
63                 ret = gnutls_priority_get_cipher_suite_index(p, i, &si);
64                 if (ret == GNUTLS_E_UNKNOWN_CIPHER_SUITE)
65                         continue;
66                 else if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
67                         break;
68                 else if (ret == 0) {
69                         count++;
70                         /* fprintf(stderr, "%s\n", gnutls_cipher_suite_info(si, NULL, NULL, NULL, NULL, NULL)); */
71                 }
72
73         }
74
75         ret = gnutls_priority_cipher_list(p, &t);
76         if ((unsigned) ret != expected_ciphers) {
77 #if 0
78                 for (i = 0; i < ret; i++)
79                         fprintf(stderr, "%s\n",
80                                 gnutls_cipher_get_name(t[i]));
81 #endif
82                 fail("expected %d ciphers, found %d\n", expected_ciphers,
83                      ret);
84                 exit(1);
85         }
86
87         gnutls_priority_deinit(p);
88
89         /* fprintf(stderr, "count: %d\n", count); */
90
91         if (debug)
92                 success("finished: %s\n", prio);
93
94         if (count != expected_cs) {
95                 fail("expected %d ciphersuites, found %d\n", expected_cs,
96                      count);
97                 exit(1);
98         }
99 }
100
101 void doit(void)
102 {
103         const int normal = 61;
104         const int null = 5;
105         const int sec128 = 56;
106
107         try_prio("NORMAL", normal, 9);
108         try_prio("NORMAL:-MAC-ALL:+MD5:+MAC-ALL", normal, 9);
109         try_prio("NORMAL:+CIPHER-ALL", normal, 9);      /* all (except null) */
110         try_prio("NORMAL:-CIPHER-ALL:+NULL", null, 1);  /* null */
111         try_prio("NORMAL:-CIPHER-ALL:+NULL:+CIPHER-ALL", normal + null, 10);    /* should be null + all */
112         try_prio("NORMAL:-CIPHER-ALL:+NULL:+CIPHER-ALL:-CIPHER-ALL:+AES-128-CBC", 10, 1);       /* should be null + all */
113         try_prio("PERFORMANCE", normal, 9);
114         try_prio("SECURE256", 20, 4);
115         try_prio("SECURE128", sec128, 8);
116         try_prio("SECURE128:+SECURE256", sec128, 8);    /* should be the same as SECURE128 */
117         try_prio("SECURE128:+SECURE256:+NORMAL", normal, 9);    /* should be the same as NORMAL */
118         try_prio("SUITEB192", 1, 1);
119 }
120
121 #endif