SIM card interface takes IMSI as parameter (same as in USIM)

This commit is contained in:
Martin Willi
2009-10-09 13:02:20 +02:00
parent 31f5280cee
commit 5d5e2853b6
7 changed files with 44 additions and 87 deletions
+6 -14
View File
@@ -576,30 +576,22 @@ static bool get_card_triplet(private_eap_sim_t *this,
char *rand, char *sres, char *kc) char *rand, char *sres, char *kc)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
sim_card_t *card = NULL, *current; sim_card_t *card;
id_match_t match, best = ID_MATCH_NONE;
bool success = FALSE; bool success = FALSE;
/* find the best matching SIM */
enumerator = charon->sim->create_card_enumerator(charon->sim); enumerator = charon->sim->create_card_enumerator(charon->sim);
while (enumerator->enumerate(enumerator, &current)) while (enumerator->enumerate(enumerator, &card))
{ {
match = this->peer->matches(this->peer, current->get_imsi(current)); if (card->get_triplet(card, this->peer, rand, sres, kc))
if (match > best)
{ {
card = current; success = TRUE;
best = match;
break; break;
} }
} }
if (card)
{
success = card->get_triplet(card, rand, sres, kc);
}
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
if (!card) if (!success)
{ {
DBG1(DBG_IKE, "no SIM card found matching '%Y'", this->peer); DBG1(DBG_IKE, "no SIM card found with triplets for '%Y'", this->peer);
} }
return success; return success;
} }
@@ -15,6 +15,8 @@
#include "eap_sim_file_card.h" #include "eap_sim_file_card.h"
#include <daemon.h>
typedef struct private_eap_sim_file_card_t private_eap_sim_file_card_t; typedef struct private_eap_sim_file_card_t private_eap_sim_file_card_t;
/** /**
@@ -27,62 +29,49 @@ struct private_eap_sim_file_card_t {
*/ */
eap_sim_file_card_t public; eap_sim_file_card_t public;
/**
* IMSI, is ID_ANY for file implementation
*/
identification_t *imsi;
/** /**
* source of triplets * source of triplets
*/ */
eap_sim_file_triplets_t *triplets; eap_sim_file_triplets_t *triplets;
}; };
#include <daemon.h>
/** /**
* Implementation of sim_card_t.get_triplet * Implementation of sim_card_t.get_triplet
*/ */
static bool get_triplet(private_eap_sim_file_card_t *this, static bool get_triplet(private_eap_sim_file_card_t *this,
char *rand, char *sres, char *kc) identification_t *imsi, char *rand, char *sres, char *kc)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
identification_t *id; identification_t *id;
char *c_rand, *c_sres, *c_kc; char *c_rand, *c_sres, *c_kc;
DBG2(DBG_CFG, "looking for rand: %b", rand, RAND_LEN); DBG2(DBG_CFG, "looking for rand: %b from %Y", rand, SIM_RAND_LEN, imsi);
enumerator = this->triplets->create_enumerator(this->triplets); enumerator = this->triplets->create_enumerator(this->triplets);
while (enumerator->enumerate(enumerator, &id, &c_rand, &c_sres, &c_kc)) while (enumerator->enumerate(enumerator, &id, &c_rand, &c_sres, &c_kc))
{ {
DBG2(DBG_CFG, "found triplet: rand %b\nsres %b\n kc %b", if (imsi->matches(imsi, id))
c_rand, RAND_LEN, c_sres, SRES_LEN, c_kc, KC_LEN);
if (memeq(c_rand, rand, RAND_LEN))
{ {
memcpy(sres, c_sres, SRES_LEN); DBG2(DBG_CFG, "found triplet: rand %b\nsres %b\n kc %b",
memcpy(kc, c_kc, KC_LEN); c_rand, SIM_RAND_LEN, c_sres, SIM_SRES_LEN, c_kc, SIM_KC_LEN);
enumerator->destroy(enumerator); if (memeq(c_rand, rand, SIM_RAND_LEN))
return TRUE; {
memcpy(sres, c_sres, SIM_SRES_LEN);
memcpy(kc, c_kc, SIM_KC_LEN);
enumerator->destroy(enumerator);
return TRUE;
}
} }
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
return FALSE; return FALSE;
} }
/**
* Implementation of sim_card_t.get_imsi
*/
static identification_t* get_imsi(private_eap_sim_file_card_t *this)
{
return this->imsi;
}
/** /**
* Implementation of eap_sim_file_card_t.destroy. * Implementation of eap_sim_file_card_t.destroy.
*/ */
static void destroy(private_eap_sim_file_card_t *this) static void destroy(private_eap_sim_file_card_t *this)
{ {
this->imsi->destroy(this->imsi);
free(this); free(this);
} }
@@ -93,12 +82,9 @@ eap_sim_file_card_t *eap_sim_file_card_create(eap_sim_file_triplets_t *triplets)
{ {
private_eap_sim_file_card_t *this = malloc_thing(private_eap_sim_file_card_t); private_eap_sim_file_card_t *this = malloc_thing(private_eap_sim_file_card_t);
this->public.card.get_triplet = (bool(*)(sim_card_t*, char *rand, char *sres, char *kc))get_triplet; this->public.card.get_triplet = (bool(*)(sim_card_t*, identification_t *imsi, char *rand, char *sres, char *kc))get_triplet;
this->public.card.get_imsi = (identification_t*(*)(sim_card_t*))get_imsi;
this->public.destroy = (void(*)(eap_sim_file_card_t*))destroy; this->public.destroy = (void(*)(eap_sim_file_card_t*))destroy;
/* this SIM card implementation does not have an ID, serve ID_ANY */
this->imsi = identification_create_from_encoding(ID_ANY, chunk_empty);
this->triplets = triplets; this->triplets = triplets;
return &this->public; return &this->public;
@@ -49,9 +49,9 @@ static bool get_triplet(private_eap_sim_file_provider_t *this,
{ {
if (imsi->matches(imsi, id)) if (imsi->matches(imsi, id))
{ {
memcpy(rand, c_rand, RAND_LEN); memcpy(rand, c_rand, SIM_RAND_LEN);
memcpy(sres, c_sres, SRES_LEN); memcpy(sres, c_sres, SIM_SRES_LEN);
memcpy(kc, c_kc, KC_LEN); memcpy(kc, c_kc, SIM_KC_LEN);
enumerator->destroy(enumerator); enumerator->destroy(enumerator);
return TRUE; return TRUE;
} }
@@ -23,8 +23,6 @@
#include "eap_sim_file_triplets.h" #include "eap_sim_file_triplets.h"
#include <sa/authenticators/eap/sim_manager.h>
typedef struct eap_sim_file_provider_t eap_sim_file_provider_t; typedef struct eap_sim_file_provider_t eap_sim_file_provider_t;
/** /**
@@ -50,9 +50,9 @@ struct private_eap_sim_file_triplets_t {
*/ */
typedef struct { typedef struct {
identification_t *imsi; identification_t *imsi;
char rand[RAND_LEN]; char rand[SIM_RAND_LEN];
char sres[SRES_LEN]; char sres[SIM_SRES_LEN];
char kc[KC_LEN]; char kc[SIM_KC_LEN];
} triplet_t; } triplet_t;
/** /**
@@ -197,13 +197,13 @@ static void read_triplets(private_eap_sim_file_triplets_t *this, char *path)
triplet->imsi = identification_create_from_string(token); triplet->imsi = identification_create_from_string(token);
continue; continue;
case 1: /* rand */ case 1: /* rand */
parse_token(triplet->rand, token, RAND_LEN); parse_token(triplet->rand, token, SIM_RAND_LEN);
continue; continue;
case 2: /* sres */ case 2: /* sres */
parse_token(triplet->sres, token, SRES_LEN); parse_token(triplet->sres, token, SIM_SRES_LEN);
continue; continue;
case 3: /* kc */ case 3: /* kc */
parse_token(triplet->kc, token, KC_LEN); parse_token(triplet->kc, token, SIM_KC_LEN);
continue; continue;
default: default:
break;; break;;
@@ -219,8 +219,8 @@ static void read_triplets(private_eap_sim_file_triplets_t *this, char *path)
} }
DBG2(DBG_CFG, "triplet: imsi %Y\nrand %b\nsres %b\nkc %b", DBG2(DBG_CFG, "triplet: imsi %Y\nrand %b\nsres %b\nkc %b",
triplet->imsi, triplet->rand, RAND_LEN, triplet->imsi, triplet->rand, SIM_RAND_LEN,
triplet->sres, SRES_LEN, triplet->kc, KC_LEN); triplet->sres, SIM_SRES_LEN, triplet->kc, SIM_KC_LEN);
this->triplets->insert_last(this->triplets, triplet); this->triplets->insert_last(this->triplets, triplet);
} }
@@ -21,23 +21,7 @@
#ifndef EAP_SIM_FILE_TRIPLETS_H_ #ifndef EAP_SIM_FILE_TRIPLETS_H_
#define EAP_SIM_FILE_TRIPLETS_H_ #define EAP_SIM_FILE_TRIPLETS_H_
#include <utils/enumerator.h> #include <sa/authenticators/eap/sim_manager.h>
#include <utils/identification.h>
/**
* size of RAND value
*/
#define RAND_LEN 16
/**
* size of SRES value
*/
#define SRES_LEN 4
/**
* size of KC value
*/
#define KC_LEN 8
typedef struct eap_sim_file_triplets_t eap_sim_file_triplets_t; typedef struct eap_sim_file_triplets_t eap_sim_file_triplets_t;
+11 -14
View File
@@ -28,31 +28,27 @@ typedef struct sim_manager_t sim_manager_t;
typedef struct sim_card_t sim_card_t; typedef struct sim_card_t sim_card_t;
typedef struct sim_provider_t sim_provider_t; typedef struct sim_provider_t sim_provider_t;
#define SIM_RAND_LEN 16
#define SIM_SRES_LEN 4
#define SIM_KC_LEN 8
/** /**
* Interface for a SIM card (used as EAP client). * Interface for a SIM card (used as EAP client).
*/ */
struct sim_card_t { struct sim_card_t {
/**
* Get the identity of a SIM card.
*
* The returned identity owned by the sim_card and not destroyed outside.
* The SIM card may return ID_ANY if it does not support/use an IMSI.
*
* @return identity
*/
identification_t* (*get_imsi)(sim_card_t *this);
/** /**
* Calculate SRES/KC from a RAND. * Calculate SRES/KC from a RAND.
* *
* @param imsi identity to get a triplet for
* @param rand RAND input buffer, fixed size 16 bytes * @param rand RAND input buffer, fixed size 16 bytes
* @param sres SRES output buffer, fixed size 4 byte * @param sres SRES output buffer, fixed size 4 byte
* @param kc KC output buffer, fixed size 8 bytes * @param kc KC output buffer, fixed size 8 bytes
* @return TRUE if SRES/KC calculated, FALSE on error * @return TRUE if SRES/KC calculated, FALSE on error/wrong identity
*/ */
bool (*get_triplet)(sim_card_t *this, bool (*get_triplet)(sim_card_t *this, identification_t *imsi,
char rand[16], char sres[4], char kc[8]); char rand[SIM_RAND_LEN], char sres[SIM_SRES_LEN],
char kc[SIM_KC_LEN]);
}; };
/** /**
@@ -70,7 +66,8 @@ struct sim_provider_t {
* @return TRUE if triplet received, FALSE otherwise * @return TRUE if triplet received, FALSE otherwise
*/ */
bool (*get_triplet)(sim_provider_t *this, identification_t *imsi, bool (*get_triplet)(sim_provider_t *this, identification_t *imsi,
char rand[16], char sres[4], char kc[8]); char rand[SIM_RAND_LEN], char sres[SIM_SRES_LEN],
char kc[SIM_KC_LEN]);
}; };
/** /**