source: libabac/abac_verifier.c @ 61278ec

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

detect when libabac_init hasn't been called (instead of segfaulting)

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