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
|
/*
* Copyright (C) 2010 Martin Willi
* Copyright (C) 2010 revosec AG
*
* This program 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 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program 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.
*/
#include "constraints_validator.h"
#include <debug.h>
#include <credentials/certificates/x509.h>
typedef struct private_constraints_validator_t private_constraints_validator_t;
/**
* Private data of an constraints_validator_t object.
*/
struct private_constraints_validator_t {
/**
* Public constraints_validator_t interface.
*/
constraints_validator_t public;
};
/**
* Check pathlen constraint of issuer certificate
*/
static bool check_pathlen(x509_t *issuer, int pathlen)
{
int pathlen_constraint;
pathlen_constraint = issuer->get_pathLenConstraint(issuer);
if (pathlen_constraint != X509_NO_CONSTRAINT &&
pathlen > pathlen_constraint)
{
DBG1(DBG_CFG, "path length of %d violates constraint of %d",
pathlen, pathlen_constraint);
return FALSE;
}
return TRUE;
}
/**
* Check if a FQDN/RFC822 constraint matches (suffix match)
*/
static bool suffix_matches(identification_t *constraint, identification_t *id)
{
chunk_t c, i;
c = constraint->get_encoding(constraint);
i = id->get_encoding(id);
return i.len >= c.len && chunk_equals(c, chunk_skip(i, i.len - c.len));
}
/**
* Check if a DN constraint matches (RDN prefix match)
*/
static bool dn_matches(identification_t *constraint, identification_t *id)
{
enumerator_t *ec, *ei;
id_part_t pc, pi;
chunk_t cc, ci;
bool match = TRUE;
ec = constraint->create_part_enumerator(constraint);
ei = id->create_part_enumerator(id);
while (ec->enumerate(ec, &pc, &cc))
{
if (!ei->enumerate(ei, &pi, &ci) ||
pi != pc || !chunk_equals(cc, ci))
{
match = FALSE;
break;
}
}
ec->destroy(ec);
ei->destroy(ei);
return match;
}
/**
* Check if a certificate matches to a NameConstraint
*/
static bool name_constraint_matches(identification_t *constraint,
certificate_t *cert, bool permitted)
{
x509_t *x509 = (x509_t*)cert;
enumerator_t *enumerator;
identification_t *id;
id_type_t type;
bool matches = permitted;
type = constraint->get_type(constraint);
if (type == ID_DER_ASN1_DN)
{
matches = dn_matches(constraint, cert->get_subject(cert));
if (matches != permitted)
{
return matches;
}
}
enumerator = x509->create_subjectAltName_enumerator(x509);
while (enumerator->enumerate(enumerator, &id))
{
if (id->get_type(id) == type)
{
switch (type)
{
case ID_FQDN:
case ID_RFC822_ADDR:
matches = suffix_matches(constraint, id);
break;
case ID_DER_ASN1_DN:
matches = dn_matches(constraint, id);
break;
default:
DBG1(DBG_CFG, "%N NameConstraint matching not implemented",
id_type_names, type);
matches = FALSE;
break;
}
}
if (matches != permitted)
{
break;
}
}
enumerator->destroy(enumerator);
return matches;
}
/**
* Check if a permitted or excluded NameConstraint has been inherited to sub-CA
*/
static bool name_constraint_inherited(identification_t *constraint,
x509_t *x509, bool permitted)
{
enumerator_t *enumerator;
identification_t *id;
bool inherited = FALSE;
id_type_t type;
if (!(x509->get_flags(x509) & X509_CA))
{ /* not a sub-CA, not required */
return TRUE;
}
type = constraint->get_type(constraint);
enumerator = x509->create_name_constraint_enumerator(x509, permitted);
while (enumerator->enumerate(enumerator, &id))
{
if (id->get_type(id) == type)
{
switch (type)
{
case ID_FQDN:
case ID_RFC822_ADDR:
if (permitted)
{ /* permitted constraint can be narrowed */
inherited = suffix_matches(constraint, id);
}
else
{ /* excluded constraint can be widened */
inherited = suffix_matches(id, constraint);
}
break;
case ID_DER_ASN1_DN:
if (permitted)
{
inherited = dn_matches(constraint, id);
}
else
{
inherited = dn_matches(id, constraint);
}
break;
default:
DBG1(DBG_CFG, "%N NameConstraint matching not implemented",
id_type_names, type);
inherited = FALSE;
break;
}
}
if (inherited)
{
break;
}
}
enumerator->destroy(enumerator);
return inherited;
}
/**
* Check name constraints
*/
static bool check_name_constraints(certificate_t *subject, x509_t *issuer)
{
enumerator_t *enumerator;
identification_t *constraint;
enumerator = issuer->create_name_constraint_enumerator(issuer, TRUE);
while (enumerator->enumerate(enumerator, &constraint))
{
if (!name_constraint_matches(constraint, subject, TRUE))
{
DBG1(DBG_CFG, "certificate '%Y' does not match permitted name "
"constraint '%Y'", subject->get_subject(subject), constraint);
enumerator->destroy(enumerator);
return FALSE;
}
if (!name_constraint_inherited(constraint, (x509_t*)subject, TRUE))
{
DBG1(DBG_CFG, "intermediate CA '%Y' does not inherit permitted name "
"constraint '%Y'", subject->get_subject(subject), constraint);
enumerator->destroy(enumerator);
return FALSE;
}
}
enumerator->destroy(enumerator);
enumerator = issuer->create_name_constraint_enumerator(issuer, FALSE);
while (enumerator->enumerate(enumerator, &constraint))
{
if (name_constraint_matches(constraint, subject, FALSE))
{
DBG1(DBG_CFG, "certificate '%Y' matches excluded name "
"constraint '%Y'", subject->get_subject(subject), constraint);
enumerator->destroy(enumerator);
return FALSE;
}
if (!name_constraint_inherited(constraint, (x509_t*)subject, FALSE))
{
DBG1(DBG_CFG, "intermediate CA '%Y' does not inherit excluded name "
"constraint '%Y'", subject->get_subject(subject), constraint);
enumerator->destroy(enumerator);
return FALSE;
}
}
enumerator->destroy(enumerator);
return TRUE;
}
METHOD(cert_validator_t, validate, bool,
private_constraints_validator_t *this, certificate_t *subject,
certificate_t *issuer, bool online, int pathlen, auth_cfg_t *auth)
{
if (issuer->get_type(issuer) == CERT_X509 &&
subject->get_type(subject) == CERT_X509)
{
if (!check_pathlen((x509_t*)issuer, pathlen))
{
return FALSE;
}
if (!check_name_constraints(subject, (x509_t*)issuer))
{
return FALSE;
}
}
return TRUE;
}
METHOD(constraints_validator_t, destroy, void,
private_constraints_validator_t *this)
{
free(this);
}
/**
* See header
*/
constraints_validator_t *constraints_validator_create()
{
private_constraints_validator_t *this;
INIT(this,
.public = {
.validator.validate = _validate,
.destroy = _destroy,
},
);
return &this->public;
}
|