1 /*
2  * Copyright (C) 2012 Free Software Foundation, Inc.
3  * Copyright (C) 2012-2015 Nikos Mavrogiannopoulos
4  *
5  * Author: Nikos Mavrogiannopoulos
6  *
7  * This file is part of GnuTLS.
8  *
9  * GnuTLS is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * GnuTLS is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GnuTLS; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <gnutls/gnutls.h>
32 
33 #include "utils.h"
34 
35 static void
try_prio(const char * prio,unsigned expected_cs,unsigned expected_ciphers,unsigned line)36 try_prio(const char *prio, unsigned expected_cs, unsigned expected_ciphers, unsigned line)
37 {
38 	int ret;
39 	gnutls_priority_t p;
40 	const char *err;
41 	const unsigned int *t;
42 	unsigned i, si, count = 0;
43 
44 	/* this must be called once in the program
45 	 */
46 	global_init();
47 
48 	ret = gnutls_priority_init(&p, prio, &err);
49 	if (ret < 0) {
50 		fprintf(stderr, "error: %s: %s\n", gnutls_strerror(ret),
51 			err);
52 		exit(1);
53 	}
54 
55 	for (i = 0;; i++) {
56 		ret = gnutls_priority_get_cipher_suite_index(p, i, &si);
57 		if (ret == GNUTLS_E_UNKNOWN_CIPHER_SUITE)
58 			continue;
59 		else if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
60 			break;
61 		else if (ret == 0) {
62 			count++;
63 			/* fprintf(stderr, "%s\n", gnutls_cipher_suite_info(si, NULL, NULL, NULL, NULL, NULL)); */
64 		}
65 
66 	}
67 
68 	ret = gnutls_priority_cipher_list(p, &t);
69 	if ((unsigned) ret != expected_ciphers) {
70 #if 0
71 		for (i = 0; i < ret; i++)
72 			fprintf(stderr, "%s\n",
73 				gnutls_cipher_get_name(t[i]));
74 #endif
75 		fail("%s:%d: expected %d ciphers, found %d\n", prio, line, expected_ciphers,
76 		     ret);
77 		exit(1);
78 	}
79 
80 	gnutls_priority_deinit(p);
81 
82 	/* fprintf(stderr, "count: %d\n", count); */
83 
84 	if (debug)
85 		success("finished: %s\n", prio);
86 
87 	if (count != expected_cs) {
88 		fail("%s:%d: expected %d ciphersuites, found %d\n", prio, line, expected_cs,
89 		     count);
90 		exit(1);
91 	}
92 }
93 
94 static void
try_prio_err(const char * prio,int err)95 try_prio_err(const char *prio, int err)
96 {
97 	int ret;
98 	gnutls_priority_t p;
99 
100 	ret = gnutls_priority_init(&p, prio, NULL);
101 	if (ret < 0 && ret != err) {
102 		fprintf(stderr, "error: %s\n", gnutls_strerror(ret));
103 		exit(1);
104 	}
105 
106 	if (ret >= 0)
107 		gnutls_priority_deinit(p);
108 
109 	if (debug)
110 		success("finished: %s\n", prio);
111 }
112 
113 
doit(void)114 void doit(void)
115 {
116 	const int null = 3;
117 	int sec128_cs = 29;
118 	int sec256_cs = 12;
119 	int normal_cs = 29;
120 	int pfs_cs = 23;
121 	int null_normal_cs = 28; /* disables TLS1.3 CS */
122 	int normal_ciphers = 7;
123 
124 	if (gnutls_fips140_mode_enabled()) {
125 		normal_cs = 25;
126 		normal_ciphers = 6;
127 		pfs_cs = 25;
128 		sec256_cs = 8;
129 		sec128_cs = 25;
130 	}
131 
132 	try_prio("NORMAL", normal_cs, normal_ciphers, __LINE__);
133 	try_prio("NORMAL:-MAC-ALL:+MD5:+MAC-ALL", normal_cs, normal_ciphers, __LINE__);
134 
135 	if (!gnutls_fips140_mode_enabled()) {
136 		try_prio("PFS", pfs_cs, normal_ciphers, __LINE__);
137 		try_prio("NORMAL:+CIPHER-ALL", normal_cs, 7, __LINE__);	/* all (except null) */
138 		try_prio("NORMAL:-CIPHER-ALL:+NULL", null, 1, __LINE__);	/* null */
139 		try_prio("NORMAL:-CIPHER-ALL:+NULL:+CIPHER-ALL", null_normal_cs, 8, __LINE__);	/* should be null + all */
140 		try_prio("NORMAL:-CIPHER-ALL:+NULL:+CIPHER-ALL:-CIPHER-ALL:+AES-128-CBC", 4, 1, __LINE__);	/* should be null + all */
141 #ifdef ENABLE_GOST
142 		try_prio("NONE:+VERS-TLS1.2:+GOST", 1, 1, __LINE__);
143 #endif
144 	}
145 
146 	try_prio("PERFORMANCE", normal_cs, normal_ciphers, __LINE__);
147 	try_prio("SECURE256", sec256_cs, 4, __LINE__);
148 	try_prio("SECURE128", sec128_cs, 7, __LINE__);
149 	try_prio("SECURE128:+SECURE256", sec128_cs, 7, __LINE__);	/* should be the same as SECURE128 */
150 	try_prio("SECURE128:+SECURE256:+NORMAL", normal_cs, 7, __LINE__);	/* should be the same as NORMAL */
151 	try_prio("SUITEB192", 1, 1, __LINE__);
152 	try_prio("SUITEB128", 2, 2, __LINE__);
153 	/* check legacy strings */
154 	try_prio("NORMAL:+RSA-EXPORT:+ARCFOUR-40", normal_cs, normal_ciphers, __LINE__);
155 
156 	try_prio_err("NORMAL:-VERS-ALL:+VERS-TLS1.2:-SIGN-ALL:+SIGN-ECDSA-SECP256R1-SHA256", GNUTLS_E_NO_PRIORITIES_WERE_SET);
157 	try_prio_err("NORMAL:-VERS-ALL:+VERS-TLS1.2:-SIGN-ALL", GNUTLS_E_NO_PRIORITIES_WERE_SET);
158 	try_prio_err("NORMAL:-VERS-ALL:+VERS-DTLS1.2:-SIGN-ALL", GNUTLS_E_NO_PRIORITIES_WERE_SET);
159 }
160