1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* Copyright (C) 2014 Free Software Foundation, Inc.
*
* Author: Nikos Mavrogiannopoulos
*
* This file is part of GnuTLS.
*
* GnuTLS is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GnuTLS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GnuTLS; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* Parts copied from GnuTLS example programs. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include "utils.h"
/* Test for name constraints PKIX extension.
*/
static void tls_log_func(int level, const char *str)
{
fprintf(stderr, "<%d>| %s", level, str);
}
static unsigned char cert_pem[] =
"-----BEGIN CERTIFICATE-----\n"
"MIIEMTCCAxmgAwIBAgIBADANBgkqhkiG9w0BAQUFADCBlTELMAkGA1UEBhMCR1Ix\n"
"RDBCBgNVBAoTO0hlbGxlbmljIEFjYWRlbWljIGFuZCBSZXNlYXJjaCBJbnN0aXR1\n"
"dGlvbnMgQ2VydC4gQXV0aG9yaXR5MUAwPgYDVQQDEzdIZWxsZW5pYyBBY2FkZW1p\n"
"YyBhbmQgUmVzZWFyY2ggSW5zdGl0dXRpb25zIFJvb3RDQSAyMDExMB4XDTExMTIw\n"
"NjEzNDk1MloXDTMxMTIwMTEzNDk1MlowgZUxCzAJBgNVBAYTAkdSMUQwQgYDVQQK\n"
"EztIZWxsZW5pYyBBY2FkZW1pYyBhbmQgUmVzZWFyY2ggSW5zdGl0dXRpb25zIENl\n"
"cnQuIEF1dGhvcml0eTFAMD4GA1UEAxM3SGVsbGVuaWMgQWNhZGVtaWMgYW5kIFJl\n"
"c2VhcmNoIEluc3RpdHV0aW9ucyBSb290Q0EgMjAxMTCCASIwDQYJKoZIhvcNAQEB\n"
"BQADggEPADCCAQoCggEBAKlTAOMupvaO+mDYLZU++CwqVE7NuYRhlFhPjz2L5EPz\n"
"dYmNUeTDN9KKiE15HrcS3UN4SoqS5tdI1Q+kOilENbgH9mgdVc04UfCMJDGFr4PJ\n"
"fel3r+0ae50X+bOdOFAPplp5kYCvN66m0zH7tSYJnTxa71HFK9+WXesyHgLacEns\n"
"bgzImjeN9/E2YEsmLIKe0HjzDQ9jpFEw4fkrJxIH2Oq9GGKYsFk3fb7u8yBRQlqD\n"
"75O6aRXxYp2fmTmCobd0LovUxQt7L/DICto9eQqakxylKHJzkUOap9FNhYS5qXSP\n"
"FEDH3N6sQWRstBmbAmNtJGSPRLIl6s5ddAxjMlyNh+UCAwEAAaOBiTCBhjAPBgNV\n"
"HRMBAf8EBTADAQH/MAsGA1UdDwQEAwIBBjAdBgNVHQ4EFgQUppFC/RNhSiOeCKQp\n"
"5dgTBCPuQSUwRwYDVR0eBEAwPqA8MAWCAy5ncjAFggMuZXUwBoIELmVkdTAGggQu\n"
"b3JnMAWBAy5ncjAFgQMuZXUwBoEELmVkdTAGgQQub3JnMA0GCSqGSIb3DQEBBQUA\n"
"A4IBAQAf73lB4XtuP7KMhjdCSk4cNx6NZrokgclPEg8hwAOXhiVtXdMiKahsog2p\n"
"6z0GW5k6x8zDmjR/qw7IThzh+uTczQ2+vyT+bOdrwg3IBp5OjWEopmr95fZi6hg8\n"
"TqBTnbI6nOulnJEWtk2C4AwFSKls9cz4y51JtPACpf1wA+2KIaWuE4ZJwzNzvoc7\n"
"dIsXRSZMFpGD/md9zU1jZ/rzAxKWeAaNsWftjj++n08C9bMJL/NMh98qy5V8Acys\n"
"Nnq/onN694/BtZqhFLKPM58N7yLcZnuEvUUXBj08yrl3NI/K6s8/MT7jiOOASSXI\n"
"l7WdmplNsDz4SgCbZN2fOUvRJ9e4\n"
"-----END CERTIFICATE-----\n";
const gnutls_datum_t cert = { cert_pem, sizeof(cert_pem) };
const gnutls_datum_t name1 = { (void*)"com", 3 };
const gnutls_datum_t name2 = { (void*)"example.com", sizeof("example.com")-1 };
const gnutls_datum_t name3 = { (void*)"another.example.com", sizeof("another.example.com")-1 };
const gnutls_datum_t name4 = { (void*)".gr", 3 };
const gnutls_datum_t mail1 = { (void*)"example.com", sizeof("example.com")-1 };
const gnutls_datum_t mail2 = { (void*)".example.net", sizeof(".example.net")-1 };
const gnutls_datum_t mail3 = { (void*)"nmav@redhat.com", sizeof("nmav@redhat.com")-1 };
const gnutls_datum_t mail4 = { (void*)"koko.example.net", sizeof("koko.example.net")-1 };
void doit(void)
{
int ret;
unsigned int crit, i;
gnutls_x509_crt_t crt;
gnutls_x509_name_constraints_t nc;
unsigned type;
gnutls_datum_t name;
/* this must be called once in the program
*/
global_init();
gnutls_global_set_log_function(tls_log_func);
if (debug)
gnutls_global_set_log_level(6);
/* 0: test the reading of name constraints */
ret = gnutls_x509_name_constraints_init(&nc);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_crt_init(&crt);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_crt_import(crt, &cert, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_crt_get_name_constraints(crt, nc, 0, &crit);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
if (crit != 0) {
fail("error reading criticality\n");
}
i = 0;
do {
ret = gnutls_x509_name_constraints_get_permitted(nc, i++, &type, &name);
if (ret >= 0 && i == 2) {
if (name.size != 3 || memcmp(name.data, ".eu", 3) != 0) {
fail("error reading 2nd constraint\n");
}
}
} while(ret == 0);
if (i-1 != 8) {
fail("Could not read all contraints; read %d, expected %d\n", i-1, 8);
}
gnutls_x509_name_constraints_deinit(nc);
gnutls_x509_crt_deinit(crt);
/* 1: test the generation of name constraints */
ret = gnutls_x509_name_constraints_init(&nc);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_crt_init(&crt);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_crt_import(crt, &cert, GNUTLS_X509_FMT_PEM);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_name_constraints_add_permitted(nc, GNUTLS_SAN_DNSNAME,
&name1);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_name_constraints_add_excluded(nc, GNUTLS_SAN_DNSNAME,
&name2);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_name_constraints_add_excluded(nc, GNUTLS_SAN_DNSNAME,
&name3);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_name_constraints_add_permitted(nc, GNUTLS_SAN_DNSNAME,
&name4);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_name_constraints_add_excluded(nc, GNUTLS_SAN_URI,
&name3);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_name_constraints_add_permitted(nc, GNUTLS_SAN_RFC822NAME,
&mail1);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_name_constraints_add_permitted(nc, GNUTLS_SAN_RFC822NAME,
&mail2);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_name_constraints_add_permitted(nc, GNUTLS_SAN_RFC822NAME,
&mail3);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_name_constraints_add_excluded(nc, GNUTLS_SAN_RFC822NAME,
&mail4);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
ret = gnutls_x509_crt_set_name_constraints(crt, nc, 1);
if (ret < 0)
fail("error in %d: %s\n", __LINE__, gnutls_strerror(ret));
/* 2: test the reading of the generated constraints */
i = 0;
do {
ret = gnutls_x509_name_constraints_get_permitted(nc, i++, &type, &name);
if (ret >= 0 && i == 1) {
if (name.size != name1.size || memcmp(name.data, name1.data, name1.size) != 0) {
fail("%d: error reading 1st constraint\n", __LINE__);
}
}
} while(ret == 0);
if (i-1 != 5) {
fail("Could not read all contraints; read %d, expected %d\n", i-1, 5);
}
i = 0;
do {
ret = gnutls_x509_name_constraints_get_excluded(nc, i++, &type, &name);
if (ret >= 0 && i == 1) {
if (name.size != name2.size || memcmp(name.data, name2.data, name2.size) != 0) {
fail("%d: error reading 1st excluded constraint\n", __LINE__);
}
}
if (ret >= 0 && i == 2) {
if (name.size != name3.size || memcmp(name.data, name3.data, name3.size) != 0) {
fail("%d: error reading 1st excluded constraint\n", __LINE__);
}
}
} while(ret == 0);
if (i-1 != 4) {
fail("Could not read all excluded contraints; read %d, expected %d\n", i-1, 4);
}
/* 3: test the name constraints check function */
/* This name constraints structure doesn't have any excluded GNUTLS_SAN_DN so
* this test should succeed */
name.data = (unsigned char*)"ASFHAJHjhafjs";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_DN, &name);
if (ret == 0)
fail("Checking DN should have succeeded\n");
/* Test e-mails */
name.data = (unsigned char*)"nmav@redhat.com";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
if (ret == 0)
fail("Checking email should have succeeded\n");
name.data = (unsigned char*)"nmav@radhat.com";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
if (ret != 0)
fail("Checking email should have failed\n");
name.data = (unsigned char*)"nmav@example.com";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
if (ret == 0)
fail("Checking email should have succeeded\n");
name.data = (unsigned char*)"nmav@test.example.net";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
if (ret == 0)
fail("Checking email should have succeeded\n");
name.data = (unsigned char*)"nmav@example.net";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
if (ret != 0)
fail("Checking email should have failed\n");
name.data = (unsigned char*)"nmav@koko.example.net";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_RFC822NAME, &name);
if (ret != 0)
fail("Checking email should have failed\n");
/* This name constraints structure does have an excluded URI so
* this test should fail */
name.data = (unsigned char*)"http://www.com";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_URI, &name);
if (ret != 0)
fail("Checking URI should have failed\n");
name.data = (unsigned char*)"goodexample.com";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_DNSNAME, &name);
if (ret == 0)
fail("Checking %s should have succeeded\n", name.data);
name.data = (unsigned char*)"good.com";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_DNSNAME, &name);
if (ret == 0)
fail("Checking %s should have succeeded\n", name.data);
name.data = (unsigned char*)"www.example.com";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_DNSNAME, &name);
if (ret != 0)
fail("Checking %s should have failed\n", name.data);
name.data = (unsigned char*)"www.example.net";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_DNSNAME, &name);
if (ret != 0)
fail("Checking %s should have failed\n", name.data);
name.data = (unsigned char*)"www.example.gr";
name.size = strlen((char*)name.data);
ret = gnutls_x509_name_constraints_check(nc, GNUTLS_SAN_DNSNAME, &name);
if (ret == 0)
fail("Checking %s should have succeeded\n", name.data);
gnutls_x509_name_constraints_deinit(nc);
gnutls_x509_crt_deinit(crt);
gnutls_global_deinit();
if (debug)
success("success");
}
|