2 * Copyright (C) 2008 Martin Willi
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 #include "eap_sim_file_triplets.h"
23 #include <collections/linked_list.h>
24 #include <threading/mutex.h>
25 #include <simaka_manager.h>
27 typedef struct private_eap_sim_file_triplets_t private_eap_sim_file_triplets_t
;
30 * Private data of an eap_sim_file_triplets_t object.
32 struct private_eap_sim_file_triplets_t
{
35 * Public eap_sim_file_triplets_t interface.
37 eap_sim_file_triplets_t
public;
40 * List of triplets, as triplet_t
42 linked_list_t
*triplets
;
45 * mutex to lock triplets list
54 identification_t
*imsi
;
55 char rand
[SIM_RAND_LEN
];
56 char sres
[SIM_SRES_LEN
];
63 static void triplet_destroy(triplet_t
*this)
65 DESTROY_IF(this->imsi
);
73 /** implements enumerator */
75 /** inner enumerator */
77 /** current enumerating triplet */
80 private_eap_sim_file_triplets_t
*this;
81 } triplet_enumerator_t
;
83 METHOD(enumerator_t
, enumerator_destroy
, void,
84 triplet_enumerator_t
*e
)
88 /* We assume that the current element is used on invocation if destroy.
89 * We move that triplet to the end to avoid handout of the same triplet
91 e
->this->triplets
->remove_at(e
->this->triplets
, e
->inner
);
92 e
->this->triplets
->insert_last(e
->this->triplets
, e
->current
);
94 e
->inner
->destroy(e
->inner
);
95 e
->this->mutex
->unlock(e
->this->mutex
);
99 METHOD(enumerator_t
, enumerator_enumerate
, bool,
100 triplet_enumerator_t
*e
, va_list args
)
102 identification_t
**imsi
;
104 char **rand
, **sres
, **kc
;
106 VA_ARGS_VGET(args
, imsi
, rand
, sres
, kc
);
108 if (e
->inner
->enumerate(e
->inner
, &triplet
))
110 e
->current
= triplet
;
111 *imsi
= triplet
->imsi
;
112 *rand
= triplet
->rand
;
113 *sres
= triplet
->sres
;
121 METHOD(eap_sim_file_triplets_t
, create_enumerator
, enumerator_t
*,
122 private_eap_sim_file_triplets_t
*this)
124 triplet_enumerator_t
*enumerator
;
126 this->mutex
->lock(this->mutex
);
129 .enumerate
= enumerator_enumerate_default
,
130 .venumerate
= _enumerator_enumerate
,
131 .destroy
= _enumerator_destroy
,
133 .inner
= this->triplets
->create_enumerator(this->triplets
),
136 return &enumerator
->public;
140 * convert to token into the array
142 static void parse_token(char *to
, char *from
, size_t len
)
146 chunk
= chunk_create(from
, min(strlen(from
), len
* 2));
147 chunk
= chunk_from_hex(chunk
, NULL
);
149 memcpy(to
+ len
- chunk
.len
, chunk
.ptr
, chunk
.len
);
154 * Read the triplets from the file
156 static bool read_triplets(private_eap_sim_file_triplets_t
*this, char *path
)
162 file
= fopen(path
, "r");
165 DBG1(DBG_CFG
, "opening triplet file %s failed: %s",
166 path
, strerror(errno
));
170 /* read line by line */
171 while (fgets(line
, sizeof(line
), file
))
174 enumerator_t
*enumerator
;
178 /* skip comments, empty lines */
189 triplet
= malloc_thing(triplet_t
);
190 memset(triplet
, 0, sizeof(triplet_t
));
193 enumerator
= enumerator_create_token(line
, ",", " \n\r#");
194 while (enumerator
->enumerate(enumerator
, &token
))
199 triplet
->imsi
= identification_create_from_string(token
);
202 parse_token(triplet
->rand
, token
, SIM_RAND_LEN
);
205 parse_token(triplet
->sres
, token
, SIM_SRES_LEN
);
208 parse_token(triplet
->kc
, token
, SIM_KC_LEN
);
215 enumerator
->destroy(enumerator
);
218 DBG1(DBG_CFG
, "error in triplet file, line %d", nr
);
219 triplet_destroy(triplet
);
223 DBG2(DBG_CFG
, "triplet: imsi %Y\nrand %b\nsres %b\nkc %b",
224 triplet
->imsi
, triplet
->rand
, SIM_RAND_LEN
,
225 triplet
->sres
, SIM_SRES_LEN
, triplet
->kc
, SIM_KC_LEN
);
227 this->triplets
->insert_last(this->triplets
, triplet
);
231 DBG1(DBG_CFG
, "read %d triplets from %s",
232 this->triplets
->get_count(this->triplets
), path
);
236 METHOD(eap_sim_file_triplets_t
, destroy
, void,
237 private_eap_sim_file_triplets_t
*this)
239 this->triplets
->destroy_function(this->triplets
, (void*)triplet_destroy
);
240 this->mutex
->destroy(this->mutex
);
247 eap_sim_file_triplets_t
*eap_sim_file_triplets_create(char *file
)
249 private_eap_sim_file_triplets_t
*this;
253 .create_enumerator
= _create_enumerator
,
256 .triplets
= linked_list_create(),
257 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
260 if (!read_triplets(this, file
))
265 return &this->public;