2 * Copyright (C) 2014 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
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.
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.
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
23 /* Parts copied from GnuTLS example programs. */
32 #include <sys/types.h>
34 #include <gnutls/gnutls.h>
35 #include <gnutls/x509.h>
39 /* Test for name constraints PKIX extension.
42 static void tls_log_func(int level, const char *str)
44 fprintf(stderr, "<%d>| %s", level, str);
47 static unsigned char cert_pem[] =
48 "-----BEGIN CERTIFICATE-----\n"
49 "MIIEMTCCAxmgAwIBAgIBADANBgkqhkiG9w0BAQUFADCBlTELMAkGA1UEBhMCR1Ix\n"
50 "RDBCBgNVBAoTO0hlbGxlbmljIEFjYWRlbWljIGFuZCBSZXNlYXJjaCBJbnN0aXR1\n"
51 "dGlvbnMgQ2VydC4gQXV0aG9yaXR5MUAwPgYDVQQDEzdIZWxsZW5pYyBBY2FkZW1p\n"
52 "YyBhbmQgUmVzZWFyY2ggSW5zdGl0dXRpb25zIFJvb3RDQSAyMDExMB4XDTExMTIw\n"
53 "NjEzNDk1MloXDTMxMTIwMTEzNDk1MlowgZUxCzAJBgNVBAYTAkdSMUQwQgYDVQQK\n"
54 "EztIZWxsZW5pYyBBY2FkZW1pYyBhbmQgUmVzZWFyY2ggSW5zdGl0dXRpb25zIENl\n"
55 "cnQuIEF1dGhvcml0eTFAMD4GA1UEAxM3SGVsbGVuaWMgQWNhZGVtaWMgYW5kIFJl\n"
56 "c2VhcmNoIEluc3Rp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\n"
72 "-----END CERTIFICATE-----\n";
74 const gnutls_datum_t cert = { cert_pem, sizeof(cert_pem) };
76 const gnutls_datum_t name1 = { (void*)"com", 3 };
77 const gnutls_datum_t name2 = { (void*)"example.com", sizeof("example.com")-1 };
78 const gnutls_datum_t name3 = { (void*)"another.example.com", sizeof("another.example.com")-1 };
80 const gnutls_datum_t mail1 = { (void*)"example.com", sizeof("example.com")-1 };
81 const gnutls_datum_t mail2 = { (void*)".example.net", sizeof(".example.net")-1 };
82 const gnutls_datum_t mail3 = { (void*)"nmav@redhat.com", sizeof("nmav@redhat.com")-1 };
83 const gnutls_datum_t mail4 = { (void*)"koko.example.net", sizeof("koko.example.net")-1 };
89 gnutls_x509_crt_t crt;
90 gnutls_x509_name_constraints_t nc;
94 /* this must be called once in the program
98 gnutls_global_set_log_function(tls_log_func);
100 gnutls_global_set_log_level(6);
102 /* 0: test the reading of name constraints */
104 ret = gnutls_x509_name_constraints_init(&nc);
106 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
108 ret = gnutls_x509_crt_init(&crt);
110 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
112 ret = gnutls_x509_crt_import(crt, &cert, GNUTLS_X509_FMT_PEM);
114 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
116 ret = gnutls_x509_crt_get_name_constraints(crt, nc, 0, &crit);
118 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
121 fail("error reading criticality\n");
126 ret = gnutls_x509_name_constraints_get_permitted(nc, i++, &type, &name);
128 if (ret >= 0 && i == 2) {
129 if (name.size != 3 || memcmp(name.data, ".eu", 3) != 0) {
130 fail("error reading 2nd constraint\n");
136 fail("Could not read all contraints; read %d, expected %d\n", i-1, 8);
139 gnutls_x509_name_constraints_deinit(nc);
140 gnutls_x509_crt_deinit(crt);
142 /* 1: test the generation of name constraints */
144 ret = gnutls_x509_name_constraints_init(&nc);
146 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
148 ret = gnutls_x509_crt_init(&crt);
150 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
152 ret = gnutls_x509_crt_import(crt, &cert, GNUTLS_X509_FMT_PEM);
154 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
156 ret = gnutls_x509_name_constraints_add_permitted(nc, GNUTLS_SAN_DNSNAME,
159 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
161 ret = gnutls_x509_name_constraints_add_excluded(nc, GNUTLS_SAN_DNSNAME,
164 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
166 ret = gnutls_x509_name_constraints_add_excluded(nc, GNUTLS_SAN_DNSNAME,
169 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
171 ret = gnutls_x509_name_constraints_add_excluded(nc, GNUTLS_SAN_URI,
174 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
176 ret = gnutls_x509_name_constraints_add_permitted(nc, GNUTLS_SAN_RFC822NAME,
179 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
181 ret = gnutls_x509_name_constraints_add_permitted(nc, GNUTLS_SAN_RFC822NAME,
184 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
186 ret = gnutls_x509_name_constraints_add_permitted(nc, GNUTLS_SAN_RFC822NAME,
189 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
191 ret = gnutls_x509_name_constraints_add_excluded(nc, GNUTLS_SAN_RFC822NAME,
194 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
196 ret = gnutls_x509_crt_set_name_constraints(crt, nc, 1);
198 fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
200 /* 2: test the reading of the generated constraints */
204 ret = gnutls_x509_name_constraints_get_permitted(nc, i++, &type, &name);
206 if (ret >= 0 && i == 1) {
207 if (name.size != name1.size || memcmp(name.data, name1.data, name1.size) != 0) {
208 fail("%d: error reading 1st constraint\n", __LINE__);
214 fail("Could not read all contraints; read %d, expected %d\n", i-1, 4);
219 ret = gnutls_x509_name_constraints_get_excluded(nc, i++, &type, &name);
221 if (ret >= 0 && i == 1) {
222 if (name.size != name2.size || memcmp(name.data, name2.data, name2.size) != 0) {
223 fail("%d: error reading 1st excluded constraint\n", __LINE__);
226 if (ret >= 0 && i == 2) {
227 if (name.size != name3.size || memcmp(name.data, name3.data, name3.size) != 0) {
228 fail("%d: error reading 1st excluded constraint\n", __LINE__);
234 fail("Could not read all excluded contraints; read %d, expected %d\n", i-1, 4);
237 /* 3: test the name constraints check function */
239 /* This name constraints structure doesn't have any excluded GNUTLS_SAN_DN so
240 * this test should succeed */
241 name.data = (unsigned char*)"ASFHAJHjhafjs";
242 name.size = strlen((char*)name.data);
243 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_DN, &name);
245 fail("Checking DN should have succeeded\n");
248 name.data = (unsigned char*)"nmav@redhat.com";
249 name.size = strlen((char*)name.data);
250 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
252 fail("Checking email should have succeeded\n");
254 name.data = (unsigned char*)"nmav@radhat.com";
255 name.size = strlen((char*)name.data);
256 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
258 fail("Checking email should have failed\n");
260 name.data = (unsigned char*)"nmav@example.com";
261 name.size = strlen((char*)name.data);
262 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
264 fail("Checking email should have succeeded\n");
266 name.data = (unsigned char*)"nmav@test.example.net";
267 name.size = strlen((char*)name.data);
268 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
270 fail("Checking email should have succeeded\n");
272 name.data = (unsigned char*)"nmav@example.net";
273 name.size = strlen((char*)name.data);
274 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
276 fail("Checking email should have failed\n");
278 name.data = (unsigned char*)"nmav@koko.example.net";
279 name.size = strlen((char*)name.data);
280 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
282 fail("Checking email should have failed\n");
284 /* This name constraints structure does have an excluded URI so
285 * this test should fail */
286 name.data = (unsigned char*)"http://www.com";
287 name.size = strlen((char*)name.data);
288 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_URI, &name);
290 fail("Checking URI should have failed\n");
292 name.data = (unsigned char*)"goodexample.com";
293 name.size = strlen((char*)name.data);
294 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_DNSNAME, &name);
296 fail("Checking %s should have succeeded\n", name.data);
298 name.data = (unsigned char*)"good.com";
299 name.size = strlen((char*)name.data);
300 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_DNSNAME, &name);
302 fail("Checking %s should have succeeded\n", name.data);
304 name.data = (unsigned char*)"www.example.com";
305 name.size = strlen((char*)name.data);
306 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_DNSNAME, &name);
308 fail("Checking %s should have failed\n", name.data);
310 name.data = (unsigned char*)"www.example.net";
311 name.size = strlen((char*)name.data);
312 ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_DNSNAME, &name);
314 fail("Checking %s should have failed\n", name.data);
316 gnutls_x509_name_constraints_deinit(nc);
317 gnutls_x509_crt_deinit(crt);
319 gnutls_global_deinit();