Implemented a callback based credential set, currently for shared keys only
[strongswan.git] / src / libstrongswan / credentials / sets / callback_cred.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
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 "callback_cred.h"
17
18 typedef struct private_callback_cred_t private_callback_cred_t;
19
20 /**
21 * Private data of an callback_cred_t object.
22 */
23 struct private_callback_cred_t {
24
25 /**
26 * Public callback_cred_t interface.
27 */
28 callback_cred_t public;
29
30 /**
31 * Callback of this set, for all types, and generic
32 */
33 union {
34 void *generic;
35 callback_cred_shared_cb_t shared;
36 } cb;
37
38 /**
39 * Data to pass to callback
40 */
41 void *data;
42 };
43
44 /**
45 * Shared key enumerator on callbacks
46 */
47 typedef struct {
48 /* implements enumerator_t */
49 enumerator_t public;
50 /* backref to this */
51 private_callback_cred_t *this;
52 /* own identity to match */
53 identification_t *me;
54 /* other identity to match */
55 identification_t *other;
56 /* current shared key */
57 shared_key_t *current;
58 } shared_enumerator_t;
59
60 METHOD(enumerator_t, shared_enumerate, bool,
61 shared_enumerator_t *this, shared_key_t **out,
62 id_match_t *match_me, id_match_t *match_other)
63 {
64 DESTROY_IF(this->current);
65 this->current = this->this->cb.shared(this->this->data,
66 this->me, this->other, match_me, match_other);
67 if (this->current)
68 {
69 *out = this->current;
70 return TRUE;
71 }
72 return FALSE;
73 }
74
75 METHOD(enumerator_t, shared_destroy, void,
76 shared_enumerator_t *this)
77 {
78 DESTROY_IF(this->current);
79 free(this);
80 }
81
82 METHOD(credential_set_t, create_shared_enumerator, enumerator_t*,
83 private_callback_cred_t *this, shared_key_type_t type,
84 identification_t *me, identification_t *other)
85 {
86 shared_enumerator_t *enumerator;
87
88 INIT(enumerator,
89 .public = {
90 .enumerate = (void*)_shared_enumerate,
91 .destroy = _shared_destroy,
92 },
93 .this = this,
94 .me = me,
95 .other = other,
96 );
97 return &enumerator->public;
98 }
99
100 METHOD(callback_cred_t, destroy, void,
101 private_callback_cred_t *this)
102 {
103 free(this);
104 }
105
106 /**
107 * Create a generic callback credential set
108 */
109 static private_callback_cred_t* create_generic(void *cb, void *data)
110 {
111 private_callback_cred_t *this;
112
113 INIT(this,
114 .public = {
115 .set = {
116 .create_shared_enumerator = (void*)return_null,
117 .create_private_enumerator = (void*)return_null,
118 .create_cert_enumerator = (void*)return_null,
119 .create_cdp_enumerator = (void*)return_null,
120 .cache_cert = (void*)nop,
121 },
122 .destroy = _destroy,
123 },
124 .cb.generic = cb,
125 .data = data,
126 );
127 return this;
128 }
129
130 /**
131 * See header
132 */
133 callback_cred_t *callback_cred_create_shared(callback_cred_shared_cb_t cb,
134 void *data)
135 {
136 private_callback_cred_t *this = create_generic(cb, data);
137
138 this->public.set.create_shared_enumerator = _create_shared_enumerator;
139
140 return &this->public;
141 }