source: libabac/abac_verifier.c @ f91b32e

abac0-leakabac0-meicompt_changesgec13mei-idmei-rt0-nmei_rt0mei_rt2mei_rt2_fix_1meiyap-rt1meiyap1rt2tvf-new-xml
Last change on this file since f91b32e was f91b32e, checked in by Mike Ryan <mikeryan@…>, 14 years ago

remove comment: everything is cryptographically happy

  • Property mode set to 100644
File size: 8.1 KB
Line 
1#include <assert.h>
2
3#include <library.h>
4#include <credentials/certificates/certificate.h>
5#include <credentials/certificates/x509.h>
6#include <credentials/certificates/ac.h>
7
8#include "abac_verifier.h"
9
10#include "abac_util.h"
11
12typedef struct _abac_id_cert_t {
13    char *keyid;
14    certificate_t *cert;
15
16    UT_hash_handle hh;
17} abac_id_cert_t;
18
19struct _abac_credential_t {
20    abac_role_t *head;
21    abac_role_t *tail;
22    certificate_t *cert;
23    certificate_t *issuer;
24
25    int refcount;
26};
27
28abac_id_cert_t *id_certs = NULL;
29
30// convert a chunk to a lowercase binary string
31// malloc's the string
32static char *_chunk_to_string(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// verify that cert was issued by issuer
44// cert and issuer can be the same, in which case the self-sig is validated
45static int _verify_signature(certificate_t *issuer, certificate_t *cert) {
46    if (cert->issued_by(cert, issuer))
47        if (cert->get_validity(cert, NULL, NULL, NULL))
48            return 1;
49    return 0;
50}
51
52/**
53 * Init the verifier subsystem.
54 */
55void abac_verifier_init(void) {
56    atexit(library_deinit);
57
58    // silence all debugging
59    dbg_default_set_level(-1);
60
61    if (!library_init(NULL))
62        exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
63
64    if (!lib->plugins->load(lib->plugins, NULL,
65            lib->settings->get_str(lib->settings, "pki.load", PLUGINS)))
66        exit(SS_RC_INITIALIZATION_FAILED);
67}
68
69/**
70 * Uninitialize the system, free up any allocated memory.
71 */
72void abac_verifier_deinit(void) {
73    abac_id_cert_t *id;
74
75    while ((id = id_certs) != NULL) {
76        HASH_DEL(id_certs, id);
77
78        free(id->keyid);
79        id->cert->destroy(id->cert);
80        free(id);
81    }
82}
83
84/**
85 * Load an ID certificate.
86 */
87static int _load_id(certificate_t *cert) {
88    abac_id_cert_t *id_cert = NULL;
89    char *keyid = NULL;
90    chunk_t id;
91    int ret;
92    x509_t *x509 = (x509_t *)cert;
93
94    assert(cert != NULL);
95
96    // get the key ID
97    id = x509->get_subjectKeyIdentifier(x509);
98    keyid = _chunk_to_string(id);
99
100    // if we already have this cert 'error' with success
101    HASH_FIND_STR(id_certs, keyid, id_cert);
102    if (id_cert != NULL) {
103        ret = ABAC_CERT_SUCCESS;
104        goto error;
105    }
106
107    // validate sig
108    ret = _verify_signature(cert, cert);
109    if (!ret) {
110        ret = ABAC_CERT_BAD_SIG;
111        goto error;
112    }
113
114    // success, add the key to the map of certificates
115    id_cert = abac_xmalloc(sizeof(abac_id_cert_t));
116    id_cert->keyid = keyid;
117    id_cert->cert = cert;
118    HASH_ADD_KEYPTR(hh, id_certs, id_cert->keyid, strlen(id_cert->keyid), id_cert);
119
120    return ABAC_CERT_SUCCESS;
121
122error:
123    if (keyid != NULL) free(keyid);
124
125    return ret;
126}
127
128/**
129 * Load an ID cert from a file.
130 */
131int abac_verifier_load_id_file(char *filename) {
132    if (lib == NULL)
133        errx(1, "looks like you didn't call libabac_init() (lib is NULL)");
134
135    // load the cert
136    certificate_t *cert = lib->creds->create(
137        lib->creds, CRED_CERTIFICATE, CERT_X509,
138        BUILD_FROM_FILE, filename,
139        BUILD_X509_FLAG, X509_AA, // attribute authority, dumb
140        BUILD_END
141    );
142    if (cert == NULL)
143        return ABAC_CERT_INVALID;
144    return _load_id(cert);
145}
146
147/**
148 * Load an ID cert from a chunk.
149 */
150int abac_verifier_load_id_chunk(chunk_t chunk) {
151    // load the cert
152    certificate_t *cert = lib->creds->create(
153        lib->creds, CRED_CERTIFICATE, CERT_X509,
154        BUILD_BLOB_ASN1_DER, chunk,
155        BUILD_X509_FLAG, X509_AA, // attribute authority, dumb
156        BUILD_END
157    );
158    if (cert == NULL)
159        return ABAC_CERT_INVALID;
160    return _load_id(cert);
161}
162
163/**
164 * Load an attribute cert.
165 * Returns true only if the certificate is valid and is issued by the proper
166 * authority.
167 */
168static int _load_attribute_cert(certificate_t *cert, abac_credential_t **cred_ret) {
169    ietf_attributes_t *attr_cert = NULL;
170    abac_role_t *head_role = NULL;
171    abac_role_t *tail_role = NULL;
172    abac_id_cert_t *issuer;
173    int ret;
174
175    // get the attr
176    ac_t *ac = (ac_t *)cert;
177    attr_cert = ac->get_groups(ac);
178    if (attr_cert == NULL) {
179        ret = ABAC_CERT_INVALID;
180        goto error;
181    }
182
183    char *attr_string = attr_cert->get_string(attr_cert);
184    if (attr_string == NULL) {
185        ret = ABAC_CERT_INVALID;
186        goto error;
187    }
188
189    // split into head/tail parts
190    char head[256], tail[256];
191    ret = sscanf(attr_string, "%255s <- %255s", head, tail);
192    if (ret != 2) {
193        ret = ABAC_CERT_INVALID;
194        goto error;
195    }
196
197    // must be a role
198    head_role = abac_role_from_string(head);
199    if (head_role == NULL) goto error;
200    if (!abac_role_is_role(head_role)) {
201        ret = ABAC_CERT_INVALID;
202        goto error;
203    }
204
205    // make sure the tail role is valid
206    tail_role = abac_role_from_string(tail);
207    if (tail_role == NULL) {
208        ret = ABAC_CERT_INVALID;
209        goto error;
210    }
211
212    // get the issuer based on keyid
213    char *principal = abac_role_principal(head_role);
214    HASH_FIND_STR(id_certs, principal, issuer);
215    if (issuer == NULL) {
216        ret = ABAC_CERT_MISSING_ISSUER;
217        goto error;
218    }
219
220    // make sure the issuer's signed it
221    ret = _verify_signature(issuer->cert, cert);
222    if (!ret) {
223        ret = ABAC_CERT_BAD_SIG;
224        goto error;
225    }
226
227    // at this point we know we have a good attribute cert
228    abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t));
229    cred->head = head_role;
230    cred->tail = tail_role;
231    cred->cert = cert;
232    cred->issuer = issuer->cert->get_ref(issuer->cert);
233    cred->refcount = 1;
234    *cred_ret = cred;
235
236    // free up some crap
237    attr_cert->destroy(attr_cert);
238
239    return ABAC_CERT_SUCCESS;
240
241error:
242    if (cert != NULL) cert->destroy(cert);
243    if (attr_cert != NULL) attr_cert->destroy(attr_cert);
244    if (head_role != NULL) abac_role_free(head_role);
245    if (tail_role != NULL) abac_role_free(tail_role);
246
247    return ret;
248}
249
250/**
251 * Load an attribute cert from a file.
252 */
253int abac_verifier_load_attribute_cert_file(char *filename, abac_credential_t **cred) {
254    // load the cert
255    certificate_t *cert = lib->creds->create(
256        lib->creds, CRED_CERTIFICATE, CERT_X509_AC,
257        BUILD_FROM_FILE, filename,
258        BUILD_END
259    );
260    if (cert == NULL)
261        return ABAC_CERT_INVALID;
262    return _load_attribute_cert(cert, cred);
263}
264
265/**
266 * Load an attribute cert from a chunk.
267 */
268int abac_verifier_load_attribute_cert_chunk(chunk_t chunk, abac_credential_t **cred) {
269    // load the cert
270    certificate_t *cert = lib->creds->create(
271        lib->creds, CRED_CERTIFICATE, CERT_X509_AC,
272        BUILD_BLOB_ASN1_DER, chunk,
273        BUILD_END
274    );
275    if (cert == NULL)
276        return ABAC_CERT_INVALID;
277    return _load_attribute_cert(cert, cred);
278}
279
280/**
281 * Return the head role.
282 */
283abac_role_t *abac_credential_head(abac_credential_t *cred) {
284    return cred->head;
285}
286
287/**
288 * Return the tail role.
289 */
290abac_role_t *abac_credential_tail(abac_credential_t *cred) {
291    return cred->tail;
292}
293
294/**
295 * Return the encoding of the attribute cert.
296 */
297abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) {
298    chunk_t encoding = cred->cert->get_encoding(cred->cert);
299    abac_chunk_t ret = { encoding.ptr, encoding.len };
300    return ret;
301}
302
303/**
304 * Return the encoding of the issuer cert.
305 */
306abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) {
307    chunk_t encoding = cred->issuer->get_encoding(cred->issuer);
308    abac_chunk_t ret = { encoding.ptr, encoding.len };
309    return ret;
310}
311
312/**
313 * Increase the ref count of a credential.
314 */
315abac_credential_t *abac_credential_dup(abac_credential_t *cred) {
316    assert(cred != NULL);
317
318    ++cred->refcount;
319    return cred;
320}
321
322/**
323 * Decrease the reference count of a credential, freeing it when it reaches 0.
324 */
325void abac_credential_free(abac_credential_t *cred) {
326    if (cred == NULL)
327        return;
328
329    --cred->refcount;
330    if (cred->refcount > 0)
331        return;
332
333    abac_role_free(cred->head);
334    abac_role_free(cred->tail);
335    cred->cert->destroy(cred->cert);
336    cred->issuer->destroy(cred->issuer); // reference counted
337
338    free(cred);
339}
Note: See TracBrowser for help on using the repository browser.