mediation connections should now properly rekey
[strongswan.git] / src / charon / credentials / sets / auth_info_wrapper.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 * $Id$
16 */
17
18 #include "auth_info_wrapper.h"
19
20 typedef struct private_auth_info_wrapper_t private_auth_info_wrapper_t;
21
22 /**
23 * private data of auth_info_wrapper
24 */
25 struct private_auth_info_wrapper_t {
26
27 /**
28 * public functions
29 */
30 auth_info_wrapper_t public;
31
32 /**
33 * wrapped auth info
34 */
35 auth_info_t *auth;
36 };
37
38 /**
39 * enumerator for auth_info_wrapper_t.create_cert_enumerator()
40 */
41 typedef struct {
42 /** implements enumerator_t */
43 enumerator_t public;
44 /** inner enumerator from auth_info */
45 enumerator_t *inner;
46 /** enumerated cert type */
47 certificate_type_t cert;
48 /** enumerated key type */
49 key_type_t key;
50 /** enumerated id */
51 identification_t *id;
52 } wrapper_enumerator_t;
53
54 /**
55 * enumerate function for wrapper_enumerator_t
56 */
57 static bool enumerate(wrapper_enumerator_t *this, certificate_t **cert)
58 {
59 auth_item_t type;
60 certificate_t *current;
61 public_key_t *public;
62
63 while (this->inner->enumerate(this->inner, &type, &current))
64 {
65 if (type != AUTHN_SUBJECT_CERT &&
66 type != AUTHN_IM_CERT)
67 {
68 continue;
69 }
70
71 if (this->cert != CERT_ANY && this->cert != current->get_type(current))
72 { /* CERT type requested, but does not match */
73 continue;
74 }
75 public = current->get_public_key(current);
76 if (this->key != KEY_ANY && !public)
77 { /* key type requested, but no public key */
78 DESTROY_IF(public);
79 continue;
80 }
81 if (this->key != KEY_ANY && public && this->key != public->get_type(public))
82 { /* key type requested, but public key has another type */
83 DESTROY_IF(public);
84 continue;
85 }
86 DESTROY_IF(public);
87 if (this->id && !current->has_subject(current, this->id))
88 { /* subject requested, but does not match */
89 continue;
90 }
91 *cert = current;
92 return TRUE;
93 }
94 return FALSE;
95 }
96
97 /**
98 * destroy function for wrapper_enumerator_t
99 */
100 static void wrapper_enumerator_destroy(wrapper_enumerator_t *this)
101 {
102 this->inner->destroy(this->inner);
103 free(this);
104 }
105
106 /**
107 * implementation of auth_info_wrapper_t.set.create_cert_enumerator
108 */
109 static enumerator_t *create_enumerator(private_auth_info_wrapper_t *this,
110 certificate_type_t cert, key_type_t key,
111 identification_t *id, bool trusted)
112 {
113 wrapper_enumerator_t *enumerator;
114
115 if (trusted)
116 {
117 return NULL;
118 }
119 enumerator = malloc_thing(wrapper_enumerator_t);
120 enumerator->cert = cert;
121 enumerator->key = key;
122 enumerator->id = id;
123 enumerator->inner = this->auth->create_item_enumerator(this->auth);
124 enumerator->public.enumerate = (void*)enumerate;
125 enumerator->public.destroy = (void*)wrapper_enumerator_destroy;
126 return &enumerator->public;
127 }
128
129 /**
130 * Implementation of auth_info_wrapper_t.destroy
131 */
132 static void destroy(private_auth_info_wrapper_t *this)
133 {
134 free(this);
135 }
136
137 /*
138 * see header file
139 */
140 auth_info_wrapper_t *auth_info_wrapper_create(auth_info_t *auth)
141 {
142 private_auth_info_wrapper_t *this = malloc_thing(private_auth_info_wrapper_t);
143
144 this->public.set.create_private_enumerator = (void*)return_null;
145 this->public.set.create_cert_enumerator = (void*)create_enumerator;
146 this->public.set.create_shared_enumerator = (void*)return_null;
147 this->public.set.create_cdp_enumerator = (void*)return_null;
148 this->public.destroy = (void(*)(auth_info_wrapper_t*))destroy;
149
150 this->auth = auth;
151
152 return &this->public;
153 }
154