Annotation of embedaddon/strongswan/src/libcharon/plugins/medsrv/medsrv_creds.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2008 Martin Willi
        !             3:  * HSR Hochschule fuer Technik Rapperswil
        !             4:  *
        !             5:  * This program is free software; you can redistribute it and/or modify it
        !             6:  * under the terms of the GNU General Public License as published by the
        !             7:  * Free Software Foundation; either version 2 of the License, or (at your
        !             8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
        !             9:  *
        !            10:  * This program is distributed in the hope that it will be useful, but
        !            11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
        !            13:  * for more details.
        !            14:  */
        !            15: 
        !            16: #include "medsrv_creds.h"
        !            17: 
        !            18: #include <daemon.h>
        !            19: #include <library.h>
        !            20: #include <collections/enumerator.h>
        !            21: 
        !            22: typedef struct private_medsrv_creds_t private_medsrv_creds_t;
        !            23: 
        !            24: /**
        !            25:  * Private data of an medsrv_creds_t object
        !            26:  */
        !            27: struct private_medsrv_creds_t {
        !            28: 
        !            29:        /**
        !            30:         * Public part
        !            31:         */
        !            32:        medsrv_creds_t public;
        !            33: 
        !            34:        /**
        !            35:         * underlying database handle
        !            36:         */
        !            37:        database_t *db;
        !            38: };
        !            39: 
        !            40: /**
        !            41:  * enumerator over certificates
        !            42:  */
        !            43: typedef struct {
        !            44:        /** implements enumerator */
        !            45:        enumerator_t public;
        !            46:        /** inner SQL enumerator */
        !            47:        enumerator_t *inner;
        !            48:        /** currently enumerated cert */
        !            49:        certificate_t *current;
        !            50:        /** type of requested key */
        !            51:        key_type_t type;
        !            52: } cert_enumerator_t;
        !            53: 
        !            54: METHOD(enumerator_t, cert_enumerator_enumerate, bool,
        !            55:        cert_enumerator_t *this, va_list args)
        !            56: {
        !            57:        certificate_t *trusted, **cert;
        !            58:        public_key_t *public;
        !            59:        chunk_t chunk;
        !            60: 
        !            61:        VA_ARGS_VGET(args, cert);
        !            62: 
        !            63:        DESTROY_IF(this->current);
        !            64:        while (this->inner->enumerate(this->inner, &chunk))
        !            65:        {
        !            66:                public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
        !            67:                                                                        BUILD_BLOB_ASN1_DER, chunk,
        !            68:                                                                        BUILD_END);
        !            69:                if (public)
        !            70:                {
        !            71:                        if (this->type == KEY_ANY || this->type == public->get_type(public))
        !            72:                        {
        !            73:                                trusted = lib->creds->create(lib->creds,
        !            74:                                                                                CRED_CERTIFICATE, CERT_TRUSTED_PUBKEY,
        !            75:                                                                                BUILD_PUBLIC_KEY, public, BUILD_END);
        !            76:                                public->destroy(public);
        !            77:                                if (trusted)
        !            78:                                {
        !            79:                                        *cert = this->current = trusted;
        !            80:                                        return TRUE;
        !            81:                                }
        !            82:                        }
        !            83:                        else
        !            84:                        {
        !            85:                                public->destroy(public);
        !            86:                        }
        !            87:                }
        !            88:        }
        !            89:        this->current = NULL;
        !            90:        return FALSE;
        !            91: }
        !            92: 
        !            93: METHOD(enumerator_t, cert_enumerator_destroy, void,
        !            94:        cert_enumerator_t *this)
        !            95: {
        !            96:        DESTROY_IF(this->current);
        !            97:        this->inner->destroy(this->inner);
        !            98:        free(this);
        !            99: }
        !           100: 
        !           101: METHOD(credential_set_t, create_cert_enumerator, enumerator_t*,
        !           102:        private_medsrv_creds_t *this, certificate_type_t cert, key_type_t key,
        !           103:        identification_t *id, bool trusted)
        !           104: {
        !           105:        cert_enumerator_t *e;
        !           106: 
        !           107:        if ((cert != CERT_TRUSTED_PUBKEY && cert != CERT_ANY) ||
        !           108:                id == NULL || id->get_type(id) != ID_KEY_ID)
        !           109:        {
        !           110:                return NULL;
        !           111:        }
        !           112: 
        !           113:        INIT(e,
        !           114:                .public = {
        !           115:                        .enumerate = enumerator_enumerate_default,
        !           116:                        .venumerate = _cert_enumerator_enumerate,
        !           117:                        .destroy = _cert_enumerator_destroy,
        !           118:                },
        !           119:                .type = key,
        !           120:                .inner = this->db->query(this->db,
        !           121:                                                                 "SELECT public_key FROM peer WHERE keyid = ?",
        !           122:                                                                 DB_BLOB, id->get_encoding(id),
        !           123:                                                                 DB_BLOB),
        !           124:        );
        !           125:        if (!e->inner)
        !           126:        {
        !           127:                free(e);
        !           128:                return NULL;
        !           129:        }
        !           130:        return &e->public;
        !           131: }
        !           132: 
        !           133: METHOD(medsrv_creds_t, destroy, void,
        !           134:        private_medsrv_creds_t *this)
        !           135: {
        !           136:        free(this);
        !           137: }
        !           138: 
        !           139: /**
        !           140:  * Described in header.
        !           141:  */
        !           142: medsrv_creds_t *medsrv_creds_create(database_t *db)
        !           143: {
        !           144:        private_medsrv_creds_t *this;
        !           145: 
        !           146:        INIT(this,
        !           147:                .public = {
        !           148:                        .set = {
        !           149:                                .create_private_enumerator = (void*)return_null,
        !           150:                                .create_cert_enumerator = _create_cert_enumerator,
        !           151:                                .create_shared_enumerator = (void*)return_null,
        !           152:                                .create_cdp_enumerator = (void*)return_null,
        !           153:                                .cache_cert = (void*)nop,
        !           154:                        },
        !           155:                        .destroy = _destroy,
        !           156:                },
        !           157:                .db = db,
        !           158:        );
        !           159: 
        !           160:        return &this->public;
        !           161: }
        !           162: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>