Moving charon to libcharon.
[strongswan.git] / src / libcharon / plugins / medsrv / medsrv_creds.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * 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 <utils/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 /**
55 * Implementation of cert_enumerator_t.public.enumerate
56 */
57 static bool cert_enumerator_enumerate(cert_enumerator_t *this,
58 certificate_t **cert)
59 {
60 certificate_t *trusted;
61 public_key_t *public;
62 chunk_t chunk;
63
64 DESTROY_IF(this->current);
65 while (this->inner->enumerate(this->inner, &chunk))
66 {
67 public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
68 BUILD_BLOB_ASN1_DER, chunk,
69 BUILD_END);
70 if (public)
71 {
72 if (this->type == KEY_ANY || this->type == public->get_type(public))
73 {
74 trusted = lib->creds->create(lib->creds,
75 CRED_CERTIFICATE, CERT_TRUSTED_PUBKEY,
76 BUILD_PUBLIC_KEY, public, BUILD_END);
77 public->destroy(public);
78 if (trusted)
79 {
80 *cert = this->current = trusted;
81 return TRUE;
82 }
83 }
84 else
85 {
86 public->destroy(public);
87 }
88 }
89 }
90 this->current = NULL;
91 return FALSE;
92 }
93
94 /**
95 * Implementation of cert_enumerator_t.public.destroy
96 */
97 static void cert_enumerator_destroy(cert_enumerator_t *this)
98 {
99 DESTROY_IF(this->current);
100 this->inner->destroy(this->inner);
101 free(this);
102 }
103
104 /**
105 * Implementation of credential_set_t.create_cert_enumerator.
106 */
107 static enumerator_t* create_cert_enumerator(private_medsrv_creds_t *this,
108 certificate_type_t cert, key_type_t key,
109 identification_t *id, bool trusted)
110 {
111 cert_enumerator_t *e;
112
113 if ((cert != CERT_TRUSTED_PUBKEY && cert != CERT_ANY) ||
114 id == NULL || id->get_type(id) != ID_KEY_ID)
115 {
116 return NULL;
117 }
118
119 e = malloc_thing(cert_enumerator_t);
120 e->current = NULL;
121 e->type = key;
122 e->public.enumerate = (void*)cert_enumerator_enumerate;
123 e->public.destroy = (void*)cert_enumerator_destroy;
124 e->inner = this->db->query(this->db,
125 "SELECT public_key FROM peer WHERE keyid = ?",
126 DB_BLOB, id->get_encoding(id),
127 DB_BLOB);
128 if (!e->inner)
129 {
130 free(e);
131 return NULL;
132 }
133 return &e->public;
134 }
135
136 /**
137 * Implementation of backend_t.destroy.
138 */
139 static void destroy(private_medsrv_creds_t *this)
140 {
141 free(this);
142 }
143
144 /**
145 * Described in header.
146 */
147 medsrv_creds_t *medsrv_creds_create(database_t *db)
148 {
149 private_medsrv_creds_t *this = malloc_thing(private_medsrv_creds_t);
150
151 this->public.set.create_private_enumerator = (void*)return_null;
152 this->public.set.create_cert_enumerator = (void*)create_cert_enumerator;
153 this->public.set.create_shared_enumerator = (void*)return_null;
154 this->public.set.create_cdp_enumerator = (void*)return_null;
155 this->public.set.cache_cert = (void*)nop;
156
157 this->public.destroy = (void (*)(medsrv_creds_t*))destroy;
158
159 this->db = db;
160
161 return &this->public;
162 }
163