2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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>.
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
19 #include <utils/mutex.h>
21 typedef struct private_nm_creds_t private_nm_creds_t
;
24 * private data of nm_creds
26 struct private_nm_creds_t
{
41 identification_t
*user
;
51 certificate_t
*usercert
;
65 * Enumerator for user certificate
67 static enumerator_t
*create_usercert_enumerator(private_nm_creds_t
*this,
68 certificate_type_t cert
, key_type_t key
)
72 if (cert
!= CERT_ANY
&& cert
!= this->usercert
->get_type(this->usercert
))
78 public = this->usercert
->get_public_key(this->usercert
);
83 if (public->get_type(public) != key
)
85 public->destroy(public);
88 public->destroy(public);
90 this->lock
->read_lock(this->lock
);
91 return enumerator_create_cleaner(
92 enumerator_create_single(this->usercert
, NULL
),
93 (void*)this->lock
->unlock
, this->lock
);
97 * Implements credential_set_t.create_cert_enumerator
99 static enumerator_t
* create_cert_enumerator(private_nm_creds_t
*this,
100 certificate_type_t cert
, key_type_t key
,
101 identification_t
*id
, bool trusted
)
103 if (id
&& this->usercert
&&
104 id
->equals(id
, this->usercert
->get_subject(this->usercert
)))
106 return create_usercert_enumerator(this, cert
, key
);
113 if (cert
!= CERT_ANY
&& cert
!= this->cert
->get_type(this->cert
))
117 if (id
&& !this->cert
->has_subject(this->cert
, id
))
123 public_key_t
*public;
125 public = this->cert
->get_public_key(this->cert
);
130 if (public->get_type(public) != key
)
132 public->destroy(public);
135 public->destroy(public);
137 this->lock
->read_lock(this->lock
);
138 return enumerator_create_cleaner(enumerator_create_single(this->cert
, NULL
),
139 (void*)this->lock
->unlock
, this->lock
);
143 * Implements credential_set_t.create_cert_enumerator
145 static enumerator_t
* create_private_enumerator(private_nm_creds_t
*this,
146 key_type_t type
, identification_t
*id
)
148 if (this->key
== NULL
)
152 if (type
!= KEY_ANY
&& type
!= this->key
->get_type(this->key
))
156 if (id
&& id
->get_type(id
) != ID_ANY
)
160 if (id
->get_type(id
) != ID_KEY_ID
||
161 !this->key
->get_fingerprint(this->key
, KEY_ID_PUBKEY_SHA1
, &keyid
) ||
162 !chunk_equals(keyid
, id
->get_encoding(id
)))
167 this->lock
->read_lock(this->lock
);
168 return enumerator_create_cleaner(enumerator_create_single(this->key
, NULL
),
169 (void*)this->lock
->unlock
, this->lock
);
173 * shared key enumerator implementation
177 private_nm_creds_t
*this;
180 } shared_enumerator_t
;
183 * enumerate function for shared enumerator
185 static bool shared_enumerate(shared_enumerator_t
*this, shared_key_t
**key
,
186 id_match_t
*me
, id_match_t
*other
)
193 *me
= ID_MATCH_PERFECT
;
194 *other
= ID_MATCH_ANY
;
200 * Destroy function for shared enumerator
202 static void shared_destroy(shared_enumerator_t
*this)
204 this->key
->destroy(this->key
);
205 this->this->lock
->unlock(this->this->lock
);
209 * Implements credential_set_t.create_cert_enumerator
211 static enumerator_t
* create_shared_enumerator(private_nm_creds_t
*this,
212 shared_key_type_t type
, identification_t
*me
,
213 identification_t
*other
)
215 shared_enumerator_t
*enumerator
;
217 if (!this->pass
|| !this->user
)
221 if (type
!= SHARED_EAP
&& type
!= SHARED_IKE
)
225 if (me
&& !me
->equals(me
, this->user
))
230 enumerator
= malloc_thing(shared_enumerator_t
);
231 enumerator
->public.enumerate
= (void*)shared_enumerate
;
232 enumerator
->public.destroy
= (void*)shared_destroy
;
233 enumerator
->this = this;
234 enumerator
->done
= FALSE
;
235 this->lock
->read_lock(this->lock
);
236 enumerator
->key
= shared_key_create(type
,
237 chunk_clone(chunk_create(this->pass
,
238 strlen(this->pass
))));
239 return &enumerator
->public;
243 * Implementation of nm_creds_t.set_certificate
245 static void set_certificate(private_nm_creds_t
*this, certificate_t
*cert
)
247 this->lock
->write_lock(this->lock
);
248 DESTROY_IF(this->cert
);
250 this->lock
->unlock(this->lock
);
254 * Implementation of nm_creds_t.set_password
256 static void set_username_password(private_nm_creds_t
*this, identification_t
*id
,
259 this->lock
->write_lock(this->lock
);
260 DESTROY_IF(this->user
);
261 this->user
= id
->clone(id
);
263 this->pass
= password ?
strdup(password
) : NULL
;
264 this->lock
->unlock(this->lock
);
268 * Implementation of nm_creds_t.set_cert_and_key
270 static void set_cert_and_key(private_nm_creds_t
*this, certificate_t
*cert
,
273 this->lock
->write_lock(this->lock
);
274 DESTROY_IF(this->key
);
275 DESTROY_IF(this->usercert
);
277 this->usercert
= cert
;
278 this->lock
->unlock(this->lock
);
282 * Implementation of nm_creds_t.clear
284 static void clear(private_nm_creds_t
*this)
286 DESTROY_IF(this->cert
);
287 DESTROY_IF(this->user
);
289 DESTROY_IF(this->usercert
);
290 DESTROY_IF(this->key
);
292 this->usercert
= NULL
;
299 * Implementation of nm_creds_t.destroy
301 static void destroy(private_nm_creds_t
*this)
304 this->lock
->destroy(this->lock
);
311 nm_creds_t
*nm_creds_create()
313 private_nm_creds_t
*this = malloc_thing(private_nm_creds_t
);
315 this->public.set
.create_private_enumerator
= (void*)create_private_enumerator
;
316 this->public.set
.create_cert_enumerator
= (void*)create_cert_enumerator
;
317 this->public.set
.create_shared_enumerator
= (void*)create_shared_enumerator
;
318 this->public.set
.create_cdp_enumerator
= (void*)return_null
;
319 this->public.set
.cache_cert
= (void*)nop
;
320 this->public.set_certificate
= (void(*)(nm_creds_t
*, certificate_t
*cert
))set_certificate
;
321 this->public.set_username_password
= (void(*)(nm_creds_t
*, identification_t
*id
, char *password
))set_username_password
;
322 this->public.set_cert_and_key
= (void(*)(nm_creds_t
*, certificate_t
*cert
, private_key_t
*key
))set_cert_and_key
;
323 this->public.clear
= (void(*)(nm_creds_t
*))clear
;
324 this->public.destroy
= (void(*)(nm_creds_t
*))destroy
;
326 this->lock
= rwlock_create(RWLOCK_TYPE_DEFAULT
);
331 this->usercert
= NULL
;
334 return &this->public;