Debian Codegraph @

src/proxy/evaluate.c

raw
1 /*********************************************************************
2 *
3 * Authors: Vincenzo Ciaschini - [email protected]
4 *
5 * Copyright (c) Members of the EGEE Collaboration. 2004-2010.
6 * See http://www.eu-egee.org/partners/ for details on the copyright holders.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * Parts of this code may be based upon or even include verbatim pieces,
21 * originally written by other people, in which case the original header
22 * follows.
23 *
24 *********************************************************************/
25 #include "config.h"
26 #include <openssl/x509.h>
27 #include "parsertypes.h"
28 #include "doio.h"
29 #include "listfunc.h"
30 #include "normalize.h"
31
32 #include <regex.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 static char *gethash(X509 *cert, char *hash);
37 static int find_policy(struct policy **policies, X509 *cert, int current);
38 static int evaluate_match_namespace(char *pattern, char *subject, int type);
39 static int evaluate_match_signing(char *pattern, char *subject, int type);
40 static int restriction_evaluate_policy(X509 *cert, struct policy *policy);
41 static int evaluate_cert(X509 *cert, struct policy **namespaces);
42 static int restriction_evaluate_namespace(STACK_OF(X509) *chain, struct policy **namespaces);
43 static int restriction_evaluate_signing(STACK_OF(X509) *chain, struct policy **signings);
44 static FILE *open_from_dir(char *path, char *file);
45
46 extern int signinglex_init (void** scanner);
47 extern void signingset_in (FILE * in_str ,void *yyscanner );
48 extern int signinglex_destroy (void* yyscanner );
49 extern int signingparse(struct policy ***policies, void *scanner);
50
51 extern int namespaceslex_init (void** scanner);
52 extern void namespacesset_in (FILE * in_str ,void *yyscanner );
53 extern int namespaceslex_destroy (void* yyscanner );
54 extern int namespacesparse(struct policy ***policies, void *scanner);
55
56 static int find_policy(struct policy **policies, X509 *cert, int current)
57 {
58 int i = (current == -1 ? 0 : current + 1);
59
60 char hash[EVP_MAX_MD_SIZE+1];
61
62 if (!policies || !(policies[0]) || !cert)
63 return -1;
64
65 while (policies[i]) {
66 if (policies[i]->self) {
67 if (!strcmp(gethash(cert, hash), policies[i]->caname))
68 return i;
69 }
70 else {
71 char *issuer = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
72 int ret = strcmp(issuer, policies[i]->caname);
73 OPENSSL_free(issuer);
74
75 if (!ret)
76 return i;
77 }
78 i++;
79 }
80
81 /* If code reaches here, no match was found. */
82 return -1;
83 }
84
85 static char *gethash(X509 *cert, char *hash)
86 {
87 unsigned long hashvalue = X509_subject_name_hash(cert);
88 sprintf(hash, "%08lx", hashvalue);
89 return hash;
90 }
91
92 static int evaluate_match_namespace(char *pattern, char *subject, int type)
93 {
94 regex_t compiled;
95 regmatch_t match[1];
96 int success = SUCCESS_UNDECIDED;
97 char *patterntmp = normalize(pattern);
98 char *subjecttmp = normalize(subject);
99
100 if (!regcomp(&compiled, patterntmp, REG_NOSUB)) {
101 if (!regexec(&compiled, subjecttmp, 0, match, 0)) {
102 /* matched */
103 if (type)
104 success = SUCCESS_PERMIT;
105 else
106 success = SUCCESS_DENY;
107 }
108 }
109
110 regfree(&compiled);
111 free(patterntmp);
112 free(subjecttmp);
113
114 return success;
115 }
116
117 static int evaluate_match_signing(char *pattern, char *subject, int type)
118 {
119 int success = SUCCESS_UNDECIDED;
120 int len = 0;
121 int compare;
122 char *patterntmp = normalize(pattern);
123 char *subjecttmp = normalize(subject);
124
125 if (!pattern || !subject)
126 return success;
127
128 len = strlen(pattern);
129
130 if (pattern[len-1] == '*')
131 compare = strncmp(patterntmp, subjecttmp, len-1);
132 else
133 compare = strcmp(patterntmp, subjecttmp);
134
135 free(patterntmp);
136 free(subjecttmp);
137
138 if (!compare) {
139 if (type)
140 return SUCCESS_PERMIT;
141 else
142 return SUCCESS_DENY;
143 }
144
145 return success;
146 }
147
148 static int restriction_evaluate_policy(X509 *cert, struct policy *policy)
149 {
150 int success = SUCCESS_UNDECIDED;
151 char *subject = NULL;
152
153 struct condition **cond = NULL;
154 int condindex = 0;
155 int subjindex = 0;
156
157 if (!policy || !cert || !policy->conds)
158 return success;
159
160 subject = X509_NAME_oneline(X509_get_subject_name(cert), 0 ,0);
161 if (!subject)
162 return success;
163
164 cond = policy->conds;
165
166 while (cond[condindex]) {
167 if (cond[condindex]->subjects) {
168 char **subjects = cond[condindex]->subjects;
169 int tempsuccess;
170
171 while (subjects[subjindex]) {
172 if (policy->type == TYPE_NAMESPACE)
173 tempsuccess = evaluate_match_namespace(subjects[subjindex],
174 subject,
175 cond[condindex]->positive);
176 else
177 tempsuccess = evaluate_match_signing(subjects[subjindex],
178 subject,
179 cond[condindex]->positive);
180
181
182 if (tempsuccess != SUCCESS_UNDECIDED)
183 success = tempsuccess;
184
185 if (success == SUCCESS_DENY)
186 goto end;
187
188 subjindex++;
189 }
190 }
191 condindex++;
192 }
193
194 end:
195 OPENSSL_free(subject);
196 return success;
197 }
198
199 static int isselfsigned(X509*cert)
200 {
201 return !X509_NAME_cmp(X509_get_subject_name(cert),
202 X509_get_issuer_name(cert));
203
204 }
205
206 static int evaluate_cert(X509 *cert, struct policy **namespaces)
207 {
208 int result = SUCCESS_UNDECIDED;
209 int policyindex = -1,
210 currentindex = -1;
211
212 /* self-signed certificates always pass */
213 if (isselfsigned(cert)) {
214 char *subject = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
215 OPENSSL_free(subject);
216 return SUCCESS_PERMIT;
217 }
218 while ((policyindex = find_policy(namespaces, cert, currentindex)) != -1) {
219 struct policy *policy = namespaces[policyindex];
220
221 result = restriction_evaluate_policy(cert, policy);
222
223 if (result != SUCCESS_UNDECIDED)
224 break;
225 currentindex = policyindex;
226 }
227
228 return result;
229 }
230
231 static int restriction_evaluate_namespace(STACK_OF(X509) *chain, struct policy **namespaces)
232 {
233 int size = sk_X509_num(chain);
234 int i = 0;
235 int result = 0;
236 int start = 0, end = 0;
237 int step = 0;
238
239 if (size > 1 && isselfsigned(sk_X509_value(chain,0))) {
240 /* reverse certificate ordering. Reverse direction of visit */
241
242 start = size - 1;
243 end = -1;
244 step = -1;
245 }
246 else {
247 /* right order */
248 start = 0;
249 end = size;
250 step = 1;
251 }
252
253 for (i = start; i != end; i += step) {
254 int j;
255 X509 *cert = sk_X509_value(chain, i);
256
257 for (j = i; j >= 0; j--) {
258 result = evaluate_cert(cert, namespaces);
259
260 if (result != SUCCESS_UNDECIDED)
261 break;
262 }
263 }
264
265 if (result == SUCCESS_UNDECIDED) {
266 result = SUCCESS_PERMIT;
267 }
268
269 return result;
270 }
271
272 static int restriction_evaluate_signing(STACK_OF(X509) *chain, struct policy **signings)
273 {
274 int size = sk_X509_num(chain);
275 int i = 0;
276 int result = 0;
277
278 for (i = 0; i < size; i++) {
279 X509 *cert = sk_X509_value(chain, i);
280
281 result = evaluate_cert(cert, signings);
282
283 if (result != SUCCESS_UNDECIDED)
284 break;
285 }
286
287 if (result == SUCCESS_UNDECIDED)
288 result = SUCCESS_DENY;
289
290 return result;
291 }
292
293 int PRIVATE restriction_evaluate(STACK_OF(X509) *chain, struct policy **namespaces,
294 struct policy **signings)
295 {
296 int result = 0;
297
298 result = restriction_evaluate_namespace(chain, namespaces);
299
300 if (result == SUCCESS_UNDECIDED) {
301 result = restriction_evaluate_signing(chain, signings);
302 }
303 return result;
304 }
305
306 static void free_condition(struct condition *cond)
307 {
308 free(cond->original);
309 free(cond->subjects);
310 free(cond);
311 }
312
313 static void free_policy(struct policy *pol)
314 {
315 free(pol->caname);
316
317 listfree((char**)(pol->conds), (freefn)free_condition);
318 free(pol);
319 }
320
321 void PRIVATE voms_free_policies(struct policy **policies)
322 {
323 listfree((char**)policies, (freefn)free_policy);
324 }
325
326 static FILE *open_from_dir(char *path, char *filename)
327 {
328 char *realpath=snprintf_wrap("%s%s", path, filename);
329 FILE *file = NULL;
330
331 file = fopen(realpath, "rb");
332
333 free(realpath);
334
335 return file;
336 }
337
338
339 void PRIVATE read_pathrestriction(STACK_OF(X509) *chain, char *path,
340 struct policy ***names,
341 struct policy ***signs)
342 {
343 int size = sk_X509_num(chain);
344 char hashed[9];
345 char *hash;
346 char signing[25] = "/XXXXXXXX.signing_policy";
347 char namespace[21] = "/XXXXXXXX.namespaces";
348 int i = 0, j = 0;
349 FILE *file = NULL;
350
351 for (i = 0; i < size; i++) {
352 X509 *cert = sk_X509_value(chain, i);
353 hash = gethash(cert, hashed);
354
355 /* Determine file names */
356 strncpy(signing + 1, hash, 8);
357 strncpy(namespace + 1, hash, 8);
358
359 file = open_from_dir(path, signing);
360 if (file) {
361 void *scanner = NULL;
362
363 signinglex_init(&scanner);
364 signingset_in(file, scanner);
365 (void)signingparse(signs, scanner);
366 signinglex_destroy(scanner);
367 fclose(file);
368 }
369
370 j = 0;
371 if (*signs) {
372 while ((*signs)[j]) {
373 if ((*signs)[j]->self)
374 (*signs)[j]->caname = strdup(hash);
375 j++;
376 }
377 }
378
379 file = open_from_dir(path, namespace);
380 if (file) {
381 void *scanner = NULL;
382
383 namespaceslex_init(&scanner);
384 namespacesset_in(file, scanner);
385 (void)namespacesparse(names, scanner);
386 namespaceslex_destroy(scanner);
387 fclose(file);
388 }
389
390 if (*names) {
391 int j = 0;
392
393 while ((*names)[j]) {
394 if ((*names)[j]->self)
395 (*names)[j]->caname = strdup(hash);
396 j++;
397 }
398 }
399 }
400 }