1 | |
---|
2 | /* abac_verifier.c */ |
---|
3 | #include <assert.h> |
---|
4 | #include <string.h> |
---|
5 | |
---|
6 | #include "libabac_common.h" |
---|
7 | #include "abac.h" |
---|
8 | #include "abac_list.h" |
---|
9 | #include "abac_util.h" |
---|
10 | |
---|
11 | struct _abac_id_cert_t { |
---|
12 | char *keyid; // using cert's keyid |
---|
13 | abac_id_t *cert; |
---|
14 | }; |
---|
15 | |
---|
16 | struct _abac_credential_t { |
---|
17 | abac_role_t *head; |
---|
18 | abac_role_t *tail; |
---|
19 | abac_id_t *issuer; /* Acme of Acme.customer <- Coyote */ |
---|
20 | abac_attribute_t *cert; |
---|
21 | |
---|
22 | int refcount; |
---|
23 | }; |
---|
24 | |
---|
25 | extern int abac_id_pass_privkey_from_id(abac_id_t *to_id, abac_id_t *from_id); |
---|
26 | extern char *make_pem_from_naked_pem(char *naked_pem); |
---|
27 | |
---|
28 | /***********************************************************************/ |
---|
29 | // convert a chunk to a lowercase binary string |
---|
30 | // malloc's the string |
---|
31 | /*** not used, |
---|
32 | static char *_chunk_to_string(abac_chunk_t chunk) { |
---|
33 | int i; |
---|
34 | |
---|
35 | char *ret = abac_xmalloc(chunk.len * 2 + 1); |
---|
36 | |
---|
37 | for (i = 0; i < chunk.len; ++i) |
---|
38 | sprintf(ret + 2 * i, "%02x", chunk.ptr[i]); |
---|
39 | |
---|
40 | return ret; |
---|
41 | } |
---|
42 | ***/ |
---|
43 | |
---|
44 | // verify that cert was issued by issuer |
---|
45 | // cert and issuer can be the same, in which case the self-sig is validated |
---|
46 | /*** not used |
---|
47 | static int _verify_cert(abac_id_t *cert) { |
---|
48 | |
---|
49 | assert(cert); |
---|
50 | if(!abac_id_still_valid(cert)) |
---|
51 | return 0; |
---|
52 | return 1; |
---|
53 | } |
---|
54 | ***/ |
---|
55 | |
---|
56 | /** |
---|
57 | * Init the verifier subsystem. |
---|
58 | */ |
---|
59 | void abac_verifier_init(void) { |
---|
60 | init_openssl(); |
---|
61 | init_xmlsec(); |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Uninitialize the system, free up any allocated memory. |
---|
66 | */ |
---|
67 | void abac_verifier_deinit(void) { |
---|
68 | deinit_xmlsec(); |
---|
69 | deinit_openssl(); |
---|
70 | } |
---|
71 | |
---|
72 | static abac_id_cert_t *_lookup_cert(abac_list_t *id_certs, char *keyid) |
---|
73 | { |
---|
74 | abac_id_cert_t *id_cert=NULL; |
---|
75 | if(id_certs != NULL) { |
---|
76 | abac_list_foreach(id_certs, id_cert, |
---|
77 | if(strcmp(keyid,id_cert->keyid)==0) |
---|
78 | return id_cert; |
---|
79 | ); |
---|
80 | } |
---|
81 | return NULL; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Load an ID certificate. |
---|
86 | */ |
---|
87 | static int _load_id(abac_list_t *id_certs, abac_id_t **cert, |
---|
88 | abac_keyid_map_t *km) { |
---|
89 | abac_id_cert_t *id_cert = NULL; |
---|
90 | char *keyid = NULL; |
---|
91 | char *nick = NULL; |
---|
92 | int ret; |
---|
93 | |
---|
94 | assert(*cert); |
---|
95 | |
---|
96 | // get the key ID |
---|
97 | keyid = abac_id_keyid(*cert); |
---|
98 | |
---|
99 | // if we already have this cert 'error' with success |
---|
100 | id_cert=_lookup_cert(id_certs, keyid); |
---|
101 | |
---|
102 | if (id_cert != NULL) { |
---|
103 | /* special case, if |
---|
104 | existing id does not have private key and |
---|
105 | incoming does, then need to bring that bit of |
---|
106 | information in */ |
---|
107 | if(abac_id_has_privkey(*cert) && |
---|
108 | !abac_id_has_privkey(id_cert->cert)) { |
---|
109 | abac_id_pass_privkey_from_id(id_cert->cert, *cert); |
---|
110 | } |
---|
111 | /* free the new one and set the ptr to dup of old */ |
---|
112 | abac_id_free(*cert); |
---|
113 | *cert=abac_id_dup(id_cert->cert); |
---|
114 | ret = ABAC_CERT_SUCCESS; |
---|
115 | goto error; |
---|
116 | } |
---|
117 | |
---|
118 | ret = abac_id_still_valid(*cert); |
---|
119 | if (!ret) { |
---|
120 | ret = ABAC_CERT_INVALID; |
---|
121 | goto error; |
---|
122 | } |
---|
123 | |
---|
124 | // success, add the key to the map of certificates |
---|
125 | id_cert = abac_xmalloc(sizeof(abac_id_cert_t)); |
---|
126 | id_cert->keyid = abac_xstrdup(keyid); |
---|
127 | id_cert->cert = *cert; |
---|
128 | abac_list_add(id_certs, id_cert); |
---|
129 | /* Add the new id and issuer to the keyid <-> name map */ |
---|
130 | if ( km && keyid && *cert ) { |
---|
131 | if ( (nick= abac_id_issuer(*cert)) ) { |
---|
132 | /* If the issuer starts with /CN=, as many do, |
---|
133 | * trim the /CN= off */ |
---|
134 | if (!strncmp(nick, "/CN=", 4) && strlen(nick) > 4) |
---|
135 | abac_keyid_map_add_nickname(km, keyid, nick+4); |
---|
136 | else |
---|
137 | abac_keyid_map_add_nickname(km, keyid, nick); |
---|
138 | free(nick); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | return ABAC_CERT_SUCCESS; |
---|
144 | |
---|
145 | error: |
---|
146 | // No one owns cert, so delete it. |
---|
147 | if (*cert != NULL) abac_id_free(*cert); |
---|
148 | |
---|
149 | return ret; |
---|
150 | } |
---|
151 | |
---|
152 | abac_id_t *abac_verifier_lookup_id(abac_list_t *id_certs, char *keyid) |
---|
153 | { |
---|
154 | abac_id_cert_t *id_cert=_lookup_cert(id_certs,keyid); |
---|
155 | if(id_cert == NULL) return NULL; |
---|
156 | return id_cert->cert; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * Load an ID cert from a file. |
---|
161 | */ |
---|
162 | int abac_verifier_load_id_file(abac_list_t *id_certs, char *filename, |
---|
163 | abac_keyid_map_t *km) { |
---|
164 | // load the cert |
---|
165 | abac_id_t *cert = abac_id_from_file(filename); |
---|
166 | |
---|
167 | if (cert == NULL) |
---|
168 | return ABAC_CERT_INVALID; |
---|
169 | |
---|
170 | return _load_id(id_certs,&cert, km); |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * Load an ID cert from a chunk. |
---|
175 | */ |
---|
176 | int abac_verifier_load_id_chunk(abac_list_t *id_certs,abac_chunk_t chunk, |
---|
177 | abac_keyid_map_t *km) { |
---|
178 | |
---|
179 | // load the cert |
---|
180 | abac_id_t *cert= abac_id_from_chunk(chunk); |
---|
181 | |
---|
182 | if (cert == NULL) |
---|
183 | return ABAC_CERT_INVALID; |
---|
184 | |
---|
185 | return _load_id(id_certs,&cert, km); |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * Load an ID cert with an id |
---|
190 | */ |
---|
191 | int abac_verifier_load_id_id(abac_list_t *id_certs,abac_id_t *id, |
---|
192 | abac_keyid_map_t *km) { |
---|
193 | |
---|
194 | if (id == NULL) |
---|
195 | return ABAC_CERT_INVALID; |
---|
196 | |
---|
197 | return _load_id(id_certs,&id, km); |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Load an ID cert from a char ptr of a X509 pem data |
---|
202 | * this is called from parse_privilege/parse_abac |
---|
203 | */ |
---|
204 | int abac_verifier_load_id_chars(abac_list_t *id_certs,char *naked_pem, |
---|
205 | abac_keyid_map_t *km) { |
---|
206 | /* if id_certs is NULL, don't even try to load it */ |
---|
207 | if(id_certs == NULL) return ABAC_CERT_SUCCESS; |
---|
208 | /* make a well formed pem from this */ |
---|
209 | char *new_naked_pem=abac_xstrdup(naked_pem); |
---|
210 | char *pem=make_pem_from_naked_pem(new_naked_pem); |
---|
211 | int len=strlen(pem); |
---|
212 | free(new_naked_pem); |
---|
213 | |
---|
214 | abac_chunk_t chunk = { (unsigned char *) pem, len }; |
---|
215 | int rc=abac_verifier_load_id_chunk(id_certs,chunk, km); |
---|
216 | abac_chunk_free(&chunk); |
---|
217 | return rc; |
---|
218 | |
---|
219 | } |
---|
220 | /** |
---|
221 | * Load an attribute cert. |
---|
222 | * Returns true only if the certificate is valid and is issued by the proper |
---|
223 | * authority. |
---|
224 | */ |
---|
225 | static int _load_attribute_cert(abac_list_t *id_certs,abac_attribute_t *cert, |
---|
226 | abac_credential_t **cred_ret, abac_keyid_map_t *km) { |
---|
227 | abac_role_t *head_role = NULL; |
---|
228 | abac_role_t *tail_role = NULL; |
---|
229 | abac_id_cert_t *issuer=NULL; |
---|
230 | abac_keyid_map_t *local_names = NULL; |
---|
231 | int ret; |
---|
232 | |
---|
233 | char *attr_string=abac_attribute_role_string(cert); |
---|
234 | |
---|
235 | if(attr_string == NULL) { |
---|
236 | ret = ABAC_CERT_INVALID; |
---|
237 | goto error; |
---|
238 | } |
---|
239 | |
---|
240 | // split into head/tail parts |
---|
241 | char *head_tail[2]; |
---|
242 | ret = 2; |
---|
243 | abac_split(attr_string, "<-", head_tail, &ret); |
---|
244 | if (ret != 2) { |
---|
245 | ret = ABAC_CERT_INVALID; |
---|
246 | goto error; |
---|
247 | } |
---|
248 | |
---|
249 | // must be a role |
---|
250 | head_role = abac_role_from_string(head_tail[0]); |
---|
251 | if (head_role == NULL) goto error; |
---|
252 | if (!abac_role_is_role(head_role)) { |
---|
253 | ret = ABAC_CERT_INVALID; |
---|
254 | goto error; |
---|
255 | } |
---|
256 | |
---|
257 | // make sure the tail's valid too |
---|
258 | tail_role = abac_role_from_string(head_tail[1]); |
---|
259 | if (tail_role == NULL) { |
---|
260 | ret = ABAC_CERT_INVALID; |
---|
261 | goto error; |
---|
262 | } |
---|
263 | |
---|
264 | char *principal = abac_role_principal(head_role); |
---|
265 | issuer=_lookup_cert(id_certs,principal); |
---|
266 | if (issuer == NULL) { |
---|
267 | ret = ABAC_CERT_MISSING_ISSUER; |
---|
268 | goto error; |
---|
269 | } |
---|
270 | |
---|
271 | // check if issuer is still valid |
---|
272 | if (!abac_id_still_valid(issuer->cert)) { |
---|
273 | ret = ABAC_CERT_INVALID_ISSUER; |
---|
274 | goto error; |
---|
275 | } |
---|
276 | |
---|
277 | // make sure principal match up with keyid |
---|
278 | if(!abac_id_has_keyid(issuer->cert,principal)) { |
---|
279 | ret = ABAC_CERT_BAD_PRINCIPAL; |
---|
280 | goto error; |
---|
281 | } |
---|
282 | |
---|
283 | if ( (local_names = abac_attribute_get_keyid_map(cert)) && km ) |
---|
284 | abac_keyid_map_merge(km, local_names, 1); |
---|
285 | |
---|
286 | // at this point we know we have a good attribute cert |
---|
287 | abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t)); |
---|
288 | cred->head = head_role; |
---|
289 | cred->tail = tail_role; |
---|
290 | cred->cert = cert; |
---|
291 | |
---|
292 | /* acme's cert */ |
---|
293 | cred->issuer = abac_id_dup(issuer->cert); |
---|
294 | cred->refcount = 1; |
---|
295 | *cred_ret = cred; |
---|
296 | |
---|
297 | free(attr_string); |
---|
298 | |
---|
299 | return ABAC_CERT_SUCCESS; |
---|
300 | |
---|
301 | error: |
---|
302 | free(attr_string); |
---|
303 | if (head_role != NULL) abac_role_free(head_role); |
---|
304 | if (tail_role != NULL) abac_role_free(tail_role); |
---|
305 | |
---|
306 | return ret; |
---|
307 | } |
---|
308 | |
---|
309 | static int _load_attribute_certs(abac_list_t *id_certs,abac_list_t *attr_list, |
---|
310 | abac_list_t *cred_list, abac_keyid_map_t *km) { |
---|
311 | |
---|
312 | int sz=abac_list_size(attr_list); |
---|
313 | abac_credential_t *cred_ret=NULL; |
---|
314 | abac_attribute_t *attr; |
---|
315 | if(sz) { |
---|
316 | abac_list_foreach(attr_list, attr, |
---|
317 | /* attr is being used to build cred_ret, so, don't remove it */ |
---|
318 | int ret=_load_attribute_cert(id_certs, attr, &cred_ret, km); |
---|
319 | if(ret==ABAC_CERT_SUCCESS) { |
---|
320 | abac_list_add(cred_list, cred_ret); |
---|
321 | } |
---|
322 | ); |
---|
323 | } |
---|
324 | |
---|
325 | /* just free the list ptr */ |
---|
326 | abac_list_free(attr_list); |
---|
327 | return sz; |
---|
328 | } |
---|
329 | |
---|
330 | /** |
---|
331 | * Load an attribute cert from a file. |
---|
332 | */ |
---|
333 | int abac_verifier_load_attribute_cert_file(abac_list_t *id_certs,char *filename, abac_list_t *cred_list, abac_keyid_map_t *km) { |
---|
334 | |
---|
335 | // load the cert |
---|
336 | abac_list_t *attr_list = abac_attribute_certs_from_file(id_certs,filename); |
---|
337 | |
---|
338 | if (abac_list_size(attr_list) == 0) { |
---|
339 | abac_list_free(attr_list); |
---|
340 | return ABAC_CERT_INVALID; |
---|
341 | } |
---|
342 | |
---|
343 | int ret=_load_attribute_certs(id_certs,attr_list, cred_list, km); |
---|
344 | |
---|
345 | if(ret) return ABAC_CERT_SUCCESS; |
---|
346 | return ABAC_CERT_INVALID; |
---|
347 | } |
---|
348 | |
---|
349 | /** |
---|
350 | * Load an attribute cert from a chunk. |
---|
351 | */ |
---|
352 | int abac_verifier_load_attribute_cert_chunk(abac_list_t *id_certs,abac_chunk_t chunk, abac_list_t *cred_list, abac_keyid_map_t *km) { |
---|
353 | |
---|
354 | // load the cert |
---|
355 | abac_list_t *attr_list = abac_attribute_certs_from_chunk(id_certs,chunk); |
---|
356 | |
---|
357 | if (abac_list_size(attr_list) == 0) |
---|
358 | return ABAC_CERT_INVALID; |
---|
359 | |
---|
360 | int ret=_load_attribute_certs(id_certs,attr_list,cred_list, km); |
---|
361 | |
---|
362 | if(ret) return ABAC_CERT_SUCCESS; |
---|
363 | return ABAC_CERT_INVALID; |
---|
364 | } |
---|
365 | |
---|
366 | /** |
---|
367 | * Return the head role. |
---|
368 | */ |
---|
369 | abac_role_t *abac_credential_head(abac_credential_t *cred) { |
---|
370 | return cred->head; |
---|
371 | } |
---|
372 | |
---|
373 | /** |
---|
374 | * Return the tail role. |
---|
375 | */ |
---|
376 | abac_role_t *abac_credential_tail(abac_credential_t *cred) { |
---|
377 | return cred->tail; |
---|
378 | } |
---|
379 | |
---|
380 | /** |
---|
381 | * Return the xml chunk of the attribute cert. |
---|
382 | */ |
---|
383 | abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) { |
---|
384 | assert(cred); assert(cred->cert); |
---|
385 | return abac_attribute_cert_chunk(cred->cert); |
---|
386 | } |
---|
387 | |
---|
388 | /** |
---|
389 | * Return the chunk of the issuer cert. |
---|
390 | */ |
---|
391 | abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) { |
---|
392 | assert(cred); assert(cred->issuer); |
---|
393 | abac_chunk_t ret= abac_id_in_PEM(cred->issuer); |
---|
394 | return ret; |
---|
395 | } |
---|
396 | |
---|
397 | /** |
---|
398 | * Increase the ref count of a credential. |
---|
399 | */ |
---|
400 | abac_credential_t *abac_credential_dup(abac_credential_t *cred) { |
---|
401 | assert(cred != NULL); |
---|
402 | |
---|
403 | ++cred->refcount; |
---|
404 | return cred; |
---|
405 | } |
---|
406 | |
---|
407 | abac_id_cert_t *abac_id_cert_dup(abac_id_cert_t *id_cert) { |
---|
408 | assert(id_cert != NULL && id_cert->keyid!=NULL); |
---|
409 | |
---|
410 | abac_id_cert_t *new_cert = abac_xmalloc(sizeof(abac_id_cert_t)); |
---|
411 | new_cert->keyid=abac_xstrdup(id_cert->keyid); |
---|
412 | new_cert->cert=abac_id_dup(id_cert->cert); |
---|
413 | |
---|
414 | return new_cert; |
---|
415 | } |
---|
416 | |
---|
417 | |
---|
418 | /** |
---|
419 | * Decrease the reference count of a credential, freeing it when it reaches 0. |
---|
420 | */ |
---|
421 | void abac_credential_free(abac_credential_t *cred) { |
---|
422 | if (cred == NULL) |
---|
423 | return; |
---|
424 | |
---|
425 | --cred->refcount; |
---|
426 | if (cred->refcount > 0) |
---|
427 | return; |
---|
428 | |
---|
429 | abac_role_free(cred->head); |
---|
430 | abac_role_free(cred->tail); |
---|
431 | abac_attribute_free(cred->cert); |
---|
432 | abac_id_free(cred->issuer); |
---|
433 | |
---|
434 | free(cred); |
---|
435 | } |
---|
436 | |
---|
437 | /** |
---|
438 | * remove an id cert, (not reference counted, so abac_id_certs are |
---|
439 | * deep-copied) |
---|
440 | */ |
---|
441 | void abac_id_cert_free(abac_id_cert_t *id_cert) { |
---|
442 | if (id_cert==NULL) |
---|
443 | return; |
---|
444 | |
---|
445 | abac_id_free(id_cert->cert); |
---|
446 | if(id_cert->keyid) free(id_cert->keyid); |
---|
447 | free(id_cert); |
---|
448 | } |
---|
449 | |
---|
450 | char *abac_id_cert_keyid(abac_id_cert_t *id_cert) |
---|
451 | { |
---|
452 | assert(id_cert); |
---|
453 | return id_cert->keyid; |
---|
454 | } |
---|
455 | |
---|
456 | char *abac_id_cert_cn(abac_id_cert_t *id_cert) |
---|
457 | { |
---|
458 | assert(id_cert); |
---|
459 | return abac_id_cn(id_cert->cert); |
---|
460 | } |
---|
461 | |
---|
462 | |
---|
463 | |
---|