b5bbdd60fe6c1ab0ee481d002a8b125977e28923
[strongswan.git] / src / libcharon / plugins / eap_simaka_pseudonym / eap_simaka_pseudonym_card.c
1 /*
2 * Copyright (C) 2009 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
16 #include "eap_simaka_pseudonym_card.h"
17
18 #include <daemon.h>
19 #include <collections/hashtable.h>
20
21 typedef struct private_eap_simaka_pseudonym_card_t private_eap_simaka_pseudonym_card_t;
22
23 /**
24 * Private data of an eap_simaka_pseudonym_card_t object.
25 */
26 struct private_eap_simaka_pseudonym_card_t {
27
28 /**
29 * Public eap_simaka_pseudonym_card_t interface.
30 */
31 eap_simaka_pseudonym_card_t public;
32
33 /**
34 * Permanent -> pseudonym mappings
35 */
36 hashtable_t *pseudonym;
37
38 /**
39 * Reverse pseudonym -> permanent mappings
40 */
41 hashtable_t *permanent;
42 };
43
44 /**
45 * hashtable hash function
46 */
47 static u_int hash(identification_t *key)
48 {
49 return chunk_hash(key->get_encoding(key));
50 }
51
52 /**
53 * hashtable equals function
54 */
55 static bool equals(identification_t *key1, identification_t *key2)
56 {
57 return key1->equals(key1, key2);
58 }
59
60 METHOD(simaka_card_t, get_pseudonym, identification_t*,
61 private_eap_simaka_pseudonym_card_t *this, identification_t *id)
62 {
63 identification_t *pseudonym;
64
65 pseudonym = this->pseudonym->get(this->pseudonym, id);
66 if (pseudonym)
67 {
68 return pseudonym->clone(pseudonym);
69 }
70 return NULL;
71 }
72
73 METHOD(simaka_card_t, set_pseudonym, void,
74 private_eap_simaka_pseudonym_card_t *this, identification_t *id,
75 identification_t *pseudonym)
76 {
77 identification_t *permanent;
78
79 /* create new entries */
80 id = id->clone(id);
81 pseudonym = pseudonym->clone(pseudonym);
82 permanent = this->permanent->put(this->permanent, pseudonym, id);
83 pseudonym = this->pseudonym->put(this->pseudonym, id, pseudonym);
84
85 /* delete old entries */
86 DESTROY_IF(permanent);
87 DESTROY_IF(pseudonym);
88 }
89
90 METHOD(simaka_card_t, get_quintuplet, status_t,
91 private_eap_simaka_pseudonym_card_t *this, identification_t *id,
92 char rand[AKA_RAND_LEN], char autn[AKA_AUTN_LEN], char ck[AKA_CK_LEN],
93 char ik[AKA_IK_LEN], char res[AKA_RES_MAX], int *res_len)
94 {
95 return NOT_SUPPORTED;
96 }
97
98 METHOD(eap_simaka_pseudonym_card_t, destroy, void,
99 private_eap_simaka_pseudonym_card_t *this)
100 {
101 enumerator_t *enumerator;
102 identification_t *id;
103 void *key;
104
105 enumerator = this->pseudonym->create_enumerator(this->pseudonym);
106 while (enumerator->enumerate(enumerator, &key, &id))
107 {
108 id->destroy(id);
109 }
110 enumerator->destroy(enumerator);
111
112 enumerator = this->permanent->create_enumerator(this->permanent);
113 while (enumerator->enumerate(enumerator, &key, &id))
114 {
115 id->destroy(id);
116 }
117 enumerator->destroy(enumerator);
118
119 this->pseudonym->destroy(this->pseudonym);
120 this->permanent->destroy(this->permanent);
121 free(this);
122 }
123
124 /**
125 * See header
126 */
127 eap_simaka_pseudonym_card_t *eap_simaka_pseudonym_card_create()
128 {
129 private_eap_simaka_pseudonym_card_t *this;
130
131 INIT(this,
132 .public = {
133 .card = {
134 .get_triplet = (void*)return_false,
135 .get_quintuplet = _get_quintuplet,
136 .resync = (void*)return_false,
137 .get_pseudonym = _get_pseudonym,
138 .set_pseudonym = _set_pseudonym,
139 .get_reauth = (void*)return_null,
140 .set_reauth = (void*)nop,
141 },
142 .destroy = _destroy,
143 },
144 .pseudonym = hashtable_create((void*)hash, (void*)equals, 0),
145 .permanent = hashtable_create((void*)hash, (void*)equals, 0),
146 );
147
148 return &this->public;
149 }
150