source: libabac/abac_verifier.c @ 55c272b

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

remove libabac_init and libabac_deinit

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