ebfc10177aa023f565b6148d1c7130c8f8cb8a41
[strongswan.git] / src / charon / plugins / nm / nm_creds.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 #define _GNU_SOURCE
19 #include <pthread.h>
20
21 #include "nm_creds.h"
22
23 #include <daemon.h>
24
25 typedef struct private_nm_creds_t private_nm_creds_t;
26
27 /**
28 * private data of nm_creds
29 */
30 struct private_nm_creds_t {
31
32 /**
33 * public functions
34 */
35 nm_creds_t public;
36
37 /**
38 * gateway certificate
39 */
40 certificate_t *cert;
41
42 /**
43 * User password
44 */
45 char *pass;
46
47 /**
48 * read/write lock
49 */
50 pthread_rwlock_t lock;
51 };
52
53 /**
54 * Implements credential_set_t.create_cert_enumerator
55 */
56 static enumerator_t* create_cert_enumerator(private_nm_creds_t *this,
57 certificate_type_t cert, key_type_t key,
58 identification_t *id, bool trusted)
59 {
60 if (!this->cert ||
61 (cert != CERT_ANY && cert != this->cert->get_type(this->cert)))
62 {
63 return NULL;
64 }
65 return enumerator_create_cleaner(enumerator_create_single(this->cert, NULL),
66 (void*)pthread_rwlock_unlock, &this->lock);
67 }
68
69 /**
70 * Implements credential_set_t.create_cert_enumerator
71 */
72 static enumerator_t* create_shared_enumerator(private_nm_creds_t *this,
73 shared_key_type_t type, identification_t *me,
74 identification_t *other)
75 {
76 shared_key_t *key;
77
78 if (!this->pass || (type != SHARED_EAP && type != SHARED_IKE))
79 {
80 return NULL;
81 }
82 key = shared_key_create(type, chunk_clone(
83 chunk_create(this->pass, strlen(this->pass))));
84 return enumerator_create_cleaner(
85 enumerator_create_single(key, (void*)key->destroy),
86 (void*)pthread_rwlock_unlock, &this->lock);
87 }
88
89 /**
90 * Implementation of nm_creds_t.set_certificate
91 */
92 static void set_certificate(private_nm_creds_t *this, certificate_t *cert)
93 {
94 pthread_rwlock_wrlock(&this->lock);
95 DESTROY_IF(this->cert);
96 this->cert = cert;
97 pthread_rwlock_unlock(&this->lock);
98 }
99
100 /**
101 * Implementation of nm_creds_t.set_password
102 */
103 static void set_password(private_nm_creds_t *this, char *password)
104 {
105 pthread_rwlock_wrlock(&this->lock);
106 free(this->pass);
107 this->pass = strdup(password);
108 pthread_rwlock_unlock(&this->lock);
109 }
110
111 /**
112 * Implementation of nm_creds_t.destroy
113 */
114 static void destroy(private_nm_creds_t *this)
115 {
116 DESTROY_IF(this->cert);
117 free(this->pass);
118 pthread_rwlock_destroy(&this->lock);
119 free(this);
120 }
121
122 /*
123 * see header file
124 */
125 nm_creds_t *nm_creds_create()
126 {
127 private_nm_creds_t *this = malloc_thing(private_nm_creds_t);
128
129 this->public.set.create_private_enumerator = (void*)return_null;
130 this->public.set.create_cert_enumerator = (void*)create_cert_enumerator;
131 this->public.set.create_shared_enumerator = (void*)create_shared_enumerator;
132 this->public.set.create_cdp_enumerator = (void*)return_null;
133 this->public.set.cache_cert = (void*)nop;
134 this->public.set_certificate = (void(*)(nm_creds_t*, certificate_t *cert))set_certificate;
135 this->public.set_password = (void(*)(nm_creds_t*, char *password))set_password;
136 this->public.destroy = (void(*)(nm_creds_t*))destroy;
137
138 pthread_rwlock_init(&this->lock, NULL);
139
140 this->cert = NULL;
141 this->pass = NULL;
142
143 return &this->public;
144 }
145