source: libabac/abac_verifier.c @ 327e808

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

do not add a duplicate id cert more than once (prevents memory leak)

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