source: libabac/abac_verifier.c @ fbb591e

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

reference count roles and credentials

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