reintegrated two-sim branch providing SIM card plugin API
[strongswan.git] / src / charon / plugins / eap_sim_file / eap_sim_file_provider.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 "eap_sim_file_provider.h"
19
20 typedef struct private_eap_sim_file_provider_t private_eap_sim_file_provider_t;
21
22 /**
23 * Private data of an eap_sim_file_provider_t object.
24 */
25 struct private_eap_sim_file_provider_t {
26
27 /**
28 * Public eap_sim_file_provider_t interface.
29 */
30 eap_sim_file_provider_t public;
31
32 /**
33 * source of triplets
34 */
35 eap_sim_file_triplets_t *triplets;
36 };
37
38 /**
39 * Implementation of sim_provider_t.get_triplet
40 */
41 static bool get_triplet(private_eap_sim_file_provider_t *this,
42 identification_t *imsi,
43 char *rand, char *sres, char *kc)
44 {
45 enumerator_t *enumerator;
46 identification_t *id;
47 char *c_rand, *c_sres, *c_kc;
48
49 enumerator = this->triplets->create_enumerator(this->triplets);
50 while (enumerator->enumerate(enumerator, &id, &c_rand, &c_sres, &c_kc))
51 {
52 if (imsi->matches(imsi, id))
53 {
54 memcpy(rand, c_rand, RAND_LEN);
55 memcpy(sres, c_sres, SRES_LEN);
56 memcpy(kc, c_kc, KC_LEN);
57 enumerator->destroy(enumerator);
58 return TRUE;
59 }
60 }
61 enumerator->destroy(enumerator);
62 return FALSE;
63 }
64
65 /**
66 * Implementation of eap_sim_file_provider_t.destroy.
67 */
68 static void destroy(private_eap_sim_file_provider_t *this)
69 {
70 free(this);
71 }
72
73 /**
74 * See header
75 */
76 eap_sim_file_provider_t *eap_sim_file_provider_create(
77 eap_sim_file_triplets_t *triplets)
78 {
79 private_eap_sim_file_provider_t *this = malloc_thing(private_eap_sim_file_provider_t);
80
81 this->public.provider.get_triplet = (bool(*)(sim_provider_t*, identification_t *imsi, char rand[16], char sres[4], char kc[8]))get_triplet;
82 this->public.destroy = (void(*)(eap_sim_file_provider_t*))destroy;
83
84 this->triplets = triplets;
85
86 return &this->public;
87 }
88