source: libabac/abac_verifier.c @ 15200be

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

move libabac into its own directory

  • Property mode set to 100644
File size: 7.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
26abac_id_cert_t *id_certs = NULL;
27
28// convert a chunk to a lowercase binary string
29// malloc's the string
30static char *_chunk_to_string(chunk_t chunk) {
31    int i;
32
33    char *ret = abac_xmalloc(chunk.len * 2 + 1);
34
35    for (i = 0; i < chunk.len; ++i)
36        sprintf(ret + 2 * i, "%02x", chunk.ptr[i]);
37
38    return ret;
39}
40
41// verify that cert was issued by issuer
42// cert and issuer can be the same, in which case the self-sig is validated
43static int _verify_cert(certificate_t *issuer, certificate_t *cert) {
44    // XXX make sure this checks signature
45    if (cert->issued_by(cert, issuer))
46        if (cert->get_validity(cert, NULL, NULL, NULL))
47            return 1;
48    return 0;
49}
50
51/**
52 * Init the verifier subsystem.
53 */
54void abac_verifier_init(void) {
55    atexit(library_deinit);
56
57    if (!library_init(NULL))
58        exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
59
60    if (!lib->plugins->load(lib->plugins, NULL,
61            lib->settings->get_str(lib->settings, "pki.load", PLUGINS)))
62        exit(SS_RC_INITIALIZATION_FAILED);
63}
64
65/**
66 * Uninitialize the system, free up any allocated memory.
67 */
68void abac_verifier_deinit(void) {
69    abac_id_cert_t *id;
70
71    while ((id = id_certs) != NULL) {
72        HASH_DEL(id_certs, id);
73
74        free(id->keyid);
75        id->cert->destroy(id->cert);
76        free(id);
77    }
78}
79
80/**
81 * Load an ID certificate.
82 */
83static int _load_id(certificate_t *cert) {
84    public_key_t *public = NULL;
85    char *keyid = NULL;
86    chunk_t id;
87    int ret;
88
89    assert(cert != NULL);
90
91    // get the cert's pub key
92    public = cert->get_public_key(cert);
93    if (public == NULL) goto error;
94
95    // get the pubkey's fingerprint
96    ret = public->get_fingerprint(
97        public,
98        KEY_ID_PUBKEY_INFO_SHA1, &id
99    );
100    if (!ret) goto error;
101
102    // validate sig
103    ret = _verify_cert(cert, cert);
104    if (!ret) goto error;
105
106    keyid = _chunk_to_string(id);
107
108    // success, add the key to the map of certificates
109    abac_id_cert_t *id_cert = abac_xmalloc(sizeof(abac_id_cert_t));
110    id_cert->keyid = keyid;
111    id_cert->cert = cert;
112    HASH_ADD_KEYPTR(hh, id_certs, id_cert->keyid, strlen(id_cert->keyid), id_cert);
113
114    public->destroy(public);
115
116    return 1;
117
118error:
119    if (public != NULL) public->destroy(public);
120
121    return 0;
122}
123
124/**
125 * Load an ID cert from a file.
126 */
127int abac_verifier_load_id_file(char *filename) {
128    // load the cert
129    certificate_t *cert = lib->creds->create(
130        lib->creds, CRED_CERTIFICATE, CERT_X509,
131        BUILD_FROM_FILE, filename,
132        BUILD_X509_FLAG, X509_AA, // attribute authority, dumb
133        BUILD_END
134    );
135    if (cert == NULL)
136        return 0;
137    return _load_id(cert);
138}
139
140/**
141 * Load an ID cert from a chunk.
142 */
143int abac_verifier_load_id_chunk(chunk_t chunk) {
144    // load the cert
145    certificate_t *cert = lib->creds->create(
146        lib->creds, CRED_CERTIFICATE, CERT_X509,
147        BUILD_BLOB_ASN1_DER, chunk,
148        BUILD_X509_FLAG, X509_AA, // attribute authority, dumb
149        BUILD_END
150    );
151    if (cert == NULL)
152        return 0;
153    return _load_id(cert);
154}
155
156/**
157 * Load an attribute cert.
158 * Returns true only if the certificate is valid and is issued by the proper
159 * authority.
160 */
161static abac_credential_t *_load_attribute_cert(certificate_t *cert) {
162    ietf_attributes_t *attr_cert = NULL;
163    abac_role_t *head_role = NULL;
164    abac_role_t *tail_role = NULL;
165    abac_id_cert_t *issuer;
166    int ret;
167
168    // get the attr
169    ac_t *ac = (ac_t *)cert;
170    attr_cert = ac->get_groups(ac);
171    if (attr_cert == NULL) goto error;
172
173    char *attr_string = attr_cert->get_string(attr_cert);
174    if (attr_string == NULL) goto error;
175
176    // split into head/tail parts
177    char head[256], tail[256];
178    ret = sscanf(attr_string, "%255s <- %255s", head, tail);
179    if (ret != 2) goto error;
180
181    // must be a role
182    head_role = abac_role_from_string(head);
183    if (head_role == NULL) goto error;
184    if (!abac_role_is_role(head_role)) goto error;
185
186    // make sure the tail role is valid
187    tail_role = abac_role_from_string(tail);
188    if (tail_role == NULL) goto error;
189
190    // get the issuer based on keyid
191    char *principal = abac_role_principal(head_role);
192    HASH_FIND_STR(id_certs, principal, issuer);
193    if (issuer == NULL) goto error;
194
195    // make sure the issuer's signed it
196    ret = _verify_cert(issuer->cert, cert);
197    if (!ret) goto error;
198
199    // at this point we know we have a good attribute cert
200    abac_credential_t *cred = abac_xmalloc(sizeof(abac_credential_t));
201    cred->head = head_role;
202    cred->tail = tail_role;
203    cred->cert = cert;
204    cred->issuer = issuer->cert->get_ref(issuer->cert);
205
206    // free up some crap
207    attr_cert->destroy(attr_cert);
208
209    return cred;
210
211error:
212    if (cert != NULL) cert->destroy(cert);
213    if (attr_cert != NULL) attr_cert->destroy(attr_cert);
214    if (head_role != NULL) abac_role_free(head_role);
215    if (tail_role != NULL) abac_role_free(tail_role);
216
217    return NULL;
218}
219
220/**
221 * Load an attribute cert from a file.
222 */
223abac_credential_t *abac_verifier_load_attribute_cert_file(char *filename) {
224    // load the cert
225    certificate_t *cert = lib->creds->create(
226        lib->creds, CRED_CERTIFICATE, CERT_X509_AC,
227        BUILD_FROM_FILE, filename,
228        BUILD_END
229    );
230    if (cert == NULL)
231        return NULL;
232    return _load_attribute_cert(cert);
233}
234
235/**
236 * Load an attribute cert from a chunk.
237 */
238abac_credential_t *abac_verifier_load_attribute_cert_chunk(chunk_t chunk) {
239    // load the cert
240    certificate_t *cert = lib->creds->create(
241        lib->creds, CRED_CERTIFICATE, CERT_X509_AC,
242        BUILD_BLOB_ASN1_DER, chunk,
243        BUILD_END
244    );
245    if (cert == NULL)
246        return NULL;
247    return _load_attribute_cert(cert);
248}
249
250/**
251 * Return the head role.
252 */
253abac_role_t *abac_credential_head(abac_credential_t *cred) {
254    return cred->head;
255}
256
257/**
258 * Return the tail role.
259 */
260abac_role_t *abac_credential_tail(abac_credential_t *cred) {
261    return cred->tail;
262}
263
264/**
265 * Return the encoding of the attribute cert.
266 */
267abac_chunk_t abac_credential_attribute_cert(abac_credential_t *cred) {
268    chunk_t encoding = cred->cert->get_encoding(cred->cert);
269    abac_chunk_t ret = { encoding.ptr, encoding.len };
270    return ret;
271}
272
273/**
274 * Return the encoding of the issuer cert.
275 */
276abac_chunk_t abac_credential_issuer_cert(abac_credential_t *cred) {
277    chunk_t encoding = cred->issuer->get_encoding(cred->issuer);
278    abac_chunk_t ret = { encoding.ptr, encoding.len };
279    return ret;
280}
281
282/**
283 * Copy a credential.
284 */
285abac_credential_t *abac_credential_dup(abac_credential_t *cred) {
286    assert(cred != NULL);
287
288    abac_credential_t *clone = abac_xmalloc(sizeof(abac_credential_t));
289
290    clone->head = abac_role_dup(cred->head);
291    clone->tail = abac_role_dup(cred->tail);
292    clone->cert = cred->cert->get_ref(cred->cert);
293    clone->issuer = cred->issuer->get_ref(cred->issuer);
294
295    return clone;
296}
297
298/**
299 * Free a credential.
300 */
301void abac_credential_free(abac_credential_t *cred) {
302    if (cred == NULL)
303        return;
304
305    abac_role_free(cred->head);
306    abac_role_free(cred->tail);
307    cred->cert->destroy(cred->cert);
308    cred->issuer->destroy(cred->issuer); // reference counted
309
310    free(cred);
311}
Note: See TracBrowser for help on using the repository browser.