Added CDP support to mem_cred
[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 /* type if requested key */
53 shared_key_type_t type;
54 /* own identity to match */
55 identification_t *me;
56 /* other identity to match */
57 identification_t *other;
58 /* current shared key */
59 shared_key_t *current;
60 } shared_enumerator_t;
61
62 METHOD(enumerator_t, shared_enumerate, bool,
63 shared_enumerator_t *this, shared_key_t **out,
64 id_match_t *match_me, id_match_t *match_other)
65 {
66 DESTROY_IF(this->current);
67 this->current = this->this->cb.shared(this->this->data, this->type,
68 this->me, this->other, match_me, match_other);
69 if (this->current)
70 {
71 *out = this->current;
72 return TRUE;
73 }
74 return FALSE;
75 }
76
77 METHOD(enumerator_t, shared_destroy, void,
78 shared_enumerator_t *this)
79 {
80 DESTROY_IF(this->current);
81 free(this);
82 }
83
84 METHOD(credential_set_t, create_shared_enumerator, enumerator_t*,
85 private_callback_cred_t *this, shared_key_type_t type,
86 identification_t *me, identification_t *other)
87 {
88 shared_enumerator_t *enumerator;
89
90 INIT(enumerator,
91 .public = {
92 .enumerate = (void*)_shared_enumerate,
93 .destroy = _shared_destroy,
94 },
95 .this = this,
96 .type = type,
97 .me = me,
98 .other = other,
99 );
100 return &enumerator->public;
101 }
102
103 METHOD(callback_cred_t, destroy, void,
104 private_callback_cred_t *this)
105 {
106 free(this);
107 }
108
109 /**
110 * Create a generic callback credential set
111 */
112 static private_callback_cred_t* create_generic(void *cb, void *data)
113 {
114 private_callback_cred_t *this;
115
116 INIT(this,
117 .public = {
118 .set = {
119 .create_shared_enumerator = (void*)return_null,
120 .create_private_enumerator = (void*)return_null,
121 .create_cert_enumerator = (void*)return_null,
122 .create_cdp_enumerator = (void*)return_null,
123 .cache_cert = (void*)nop,
124 },
125 .destroy = _destroy,
126 },
127 .cb.generic = cb,
128 .data = data,
129 );
130 return this;
131 }
132
133 /**
134 * See header
135 */
136 callback_cred_t *callback_cred_create_shared(callback_cred_shared_cb_t cb,
137 void *data)
138 {
139 private_callback_cred_t *this = create_generic(cb, data);
140
141 this->public.set.create_shared_enumerator = _create_shared_enumerator;
142
143 return &this->public;
144 }