Moving charon to libcharon.
[strongswan.git] / src / libcharon / plugins / uci / uci_creds.c
1 /*
2 * Copyright (C) 2008 Thomas Kallenberg
3 * Copyright (C) 2008 Martin Willi
4 * Copyright (C) 2008 Tobias Brunner
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "uci_creds.h"
19
20 #include <daemon.h>
21 #include <credentials/keys/shared_key.h>
22 #include <utils/identification.h>
23
24 typedef struct private_uci_creds_t private_uci_creds_t;
25
26 /**
27 * Private data of an uci_creds_t object
28 */
29 struct private_uci_creds_t {
30 /**
31 * Public part
32 */
33 uci_creds_t public;
34
35 /**
36 * UCI parser context
37 */
38 uci_parser_t *parser;
39 };
40
41 typedef struct {
42 /** implements enumerator */
43 enumerator_t public;
44 /** inneer UCI enumerator */
45 enumerator_t *inner;
46 /** currently enumerated shared shared */
47 shared_key_t *current;
48 /** local ID to match */
49 identification_t *me;
50 /** remote ID to match */
51 identification_t *other;
52 } shared_enumerator_t;
53
54 /**
55 * Implementation of shared_enumerator_t.public.enumerate
56 */
57 static bool shared_enumerator_enumerate(shared_enumerator_t *this,
58 shared_key_t **key, id_match_t *me, id_match_t *other)
59 {
60 char *local_id, *remote_id, *psk;
61 identification_t *local, *remote;
62
63 while (TRUE)
64 {
65 /* defaults */
66 local_id = "%any";
67 remote_id = "%any";
68 psk = NULL;
69
70 if (!this->inner->enumerate(this->inner, NULL,
71 &local_id, &remote_id, &psk))
72 {
73 return FALSE;
74 }
75 if (psk == NULL)
76 {
77 continue;
78 }
79 if (me)
80 {
81 local = identification_create_from_string(local_id);
82 *me = this->me ? this->me->matches(this->me, local)
83 : ID_MATCH_ANY;
84 local->destroy(local);
85 if (!*me)
86 {
87 continue;
88 }
89 }
90 if (other)
91 {
92 remote = identification_create_from_string(remote_id);
93 *other = this->other ? this->other->matches(this->other, remote)
94 : ID_MATCH_ANY;
95 remote->destroy(remote);
96 if (!*other)
97 {
98 continue;
99 }
100 }
101 break;
102 }
103 DESTROY_IF(this->current);
104 this->current = shared_key_create(SHARED_IKE,
105 chunk_clone(chunk_create(psk, strlen(psk))));
106 *key = this->current;
107 return TRUE;
108 }
109
110 /**
111 * Implementation of shared_enumerator_t.public.destroy
112 */
113 static void shared_enumerator_destroy(shared_enumerator_t *this)
114 {
115 this->inner->destroy(this->inner);
116 DESTROY_IF(this->current);
117 free(this);
118 }
119
120 /**
121 * Implementation of backend_t.create_shared_cfg_enumerator.
122 */
123 static enumerator_t* create_shared_enumerator(private_uci_creds_t *this,
124 shared_key_type_t type,
125 identification_t *me,
126 identification_t *other)
127 {
128 shared_enumerator_t *e;
129
130 if (type != SHARED_IKE)
131 {
132 return NULL;
133 }
134
135 e = malloc_thing(shared_enumerator_t);
136 e->current = NULL;
137 e->public.enumerate = (void*)shared_enumerator_enumerate;
138 e->public.destroy = (void*)shared_enumerator_destroy;
139 e->me = me;
140 e->other = other;
141 e->inner = this->parser->create_section_enumerator(this->parser,
142 "local_id", "remote_id", "psk", NULL);
143 if (!e->inner)
144 {
145 free(e);
146 return NULL;
147 }
148 return &e->public;
149 }
150
151 /**
152 * Implementation of uci_creds_t.destroy
153 */
154 static void destroy(private_uci_creds_t *this)
155 {
156 free(this);
157 }
158
159 uci_creds_t *uci_creds_create(uci_parser_t *parser)
160 {
161 private_uci_creds_t *this = malloc_thing(private_uci_creds_t);
162
163 this->public.credential_set.create_shared_enumerator = (enumerator_t*(*)(credential_set_t*, shared_key_type_t, identification_t*, identification_t*))create_shared_enumerator;
164 this->public.credential_set.create_private_enumerator = (enumerator_t*(*) (credential_set_t*, key_type_t, identification_t*))return_null;
165 this->public.credential_set.create_cert_enumerator = (enumerator_t*(*) (credential_set_t*, certificate_type_t, key_type_t,identification_t *, bool))return_null;
166 this->public.credential_set.create_cdp_enumerator = (enumerator_t*(*) (credential_set_t *,certificate_type_t, identification_t *))return_null;
167 this->public.credential_set.cache_cert = (void (*)(credential_set_t *, certificate_t *))nop;
168 this->public.destroy = (void(*) (uci_creds_t*))destroy;
169
170 this->parser = parser;
171
172 return &this->public;
173 }
174