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