sim_card_t API gained support for pseudonym/fast reauthentication
[strongswan.git] / src / charon / plugins / eap_aka_3gpp2 / eap_aka_3gpp2_card.c
1 /*
2 * Copyright (C) 2008-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_aka_3gpp2_card.h"
17
18 #include <daemon.h>
19
20 typedef struct private_eap_aka_3gpp2_card_t private_eap_aka_3gpp2_card_t;
21
22 /**
23 * Private data of an eap_aka_3gpp2_card_t object.
24 */
25 struct private_eap_aka_3gpp2_card_t {
26
27 /**
28 * Public eap_aka_3gpp2_card_t interface.
29 */
30 eap_aka_3gpp2_card_t public;
31
32 /**
33 * AKA functions
34 */
35 eap_aka_3gpp2_functions_t *f;
36
37 /**
38 * do sequence number checking?
39 */
40 bool seq_check;
41
42 /**
43 * SQN stored in this pseudo-USIM
44 */
45 char sqn[AKA_SQN_LEN];
46 };
47
48 /**
49 * Functions from eap_aka_3gpp2_provider.c
50 */
51 bool eap_aka_3gpp2_get_k(identification_t *id, char k[AKA_K_LEN]);
52 void eap_aka_3gpp2_get_sqn(char sqn[AKA_SQN_LEN], int offset);
53
54 /**
55 * Implementation of sim_card_t.get_quintuplet
56 */
57 static status_t get_quintuplet(private_eap_aka_3gpp2_card_t *this,
58 identification_t *imsi, char rand[AKA_RAND_LEN],
59 char autn[AKA_AUTN_LEN], char ck[AKA_CK_LEN],
60 char ik[AKA_IK_LEN], char res[AKA_RES_LEN])
61 {
62 char *amf, *mac;
63 char k[AKA_K_LEN], ak[AKA_AK_LEN], sqn[AKA_SQN_LEN], xmac[AKA_MAC_LEN];
64
65 if (!eap_aka_3gpp2_get_k(imsi, k))
66 {
67 DBG1(DBG_IKE, "no EAP key found for %Y to authenticate with AKA", imsi);
68 return FAILED;
69 }
70
71 /* AUTN = SQN xor AK | AMF | MAC */
72 DBG3(DBG_IKE, "received autn %b", autn, AKA_AUTN_LEN);
73 DBG3(DBG_IKE, "using K %b", k, AKA_K_LEN);
74 DBG3(DBG_IKE, "using rand %b", rand, AKA_RAND_LEN);
75 memcpy(sqn, autn, AKA_SQN_LEN);
76 amf = autn + AKA_SQN_LEN;
77 mac = autn + AKA_SQN_LEN + AKA_AMF_LEN;
78
79 /* XOR anonymity key AK into SQN to decrypt it */
80 this->f->f5(this->f, k, rand, ak);
81 DBG3(DBG_IKE, "using ak %b", ak, AKA_AK_LEN);
82 memxor(sqn, ak, AKA_SQN_LEN);
83 DBG3(DBG_IKE, "using sqn %b", sqn, AKA_SQN_LEN);
84
85 /* calculate expected MAC and compare against received one */
86 this->f->f1(this->f, k, rand, sqn, amf, xmac);
87 if (!memeq(mac, xmac, AKA_MAC_LEN))
88 {
89 DBG1(DBG_IKE, "received MAC does not match XMAC");
90 DBG3(DBG_IKE, "MAC %b\nXMAC %b", mac, AKA_MAC_LEN, xmac, AKA_MAC_LEN);
91 return FAILED;
92 }
93
94 if (this->seq_check && memcmp(this->sqn, sqn, AKA_SQN_LEN) >= 0)
95 {
96 DBG3(DBG_IKE, "received SQN %b\ncurrent SQN %b",
97 sqn, AKA_SQN_LEN, this->sqn, AKA_SQN_LEN);
98 return INVALID_STATE;
99 }
100
101 /* update stored SQN to the received one */
102 memcpy(this->sqn, sqn, AKA_SQN_LEN);
103
104 /* CK/IK */
105 this->f->f3(this->f, k, rand, ck);
106 this->f->f4(this->f, k, rand, ik);
107 /* calculate RES */
108 this->f->f2(this->f, k, rand, res);
109
110 return SUCCESS;
111 }
112
113 /**
114 * Implementation of sim_card_t.resync
115 */
116 static bool resync(private_eap_aka_3gpp2_card_t *this, identification_t *imsi,
117 char rand[AKA_RAND_LEN], char auts[AKA_AUTS_LEN])
118 {
119 char amf[AKA_AMF_LEN], k[AKA_K_LEN], aks[AKA_AK_LEN], macs[AKA_MAC_LEN];
120
121 if (!eap_aka_3gpp2_get_k(imsi, k))
122 {
123 DBG1(DBG_IKE, "no EAP key found for %Y to resync AKA", imsi);
124 return FALSE;
125 }
126
127 /* AMF is set to zero in resync */
128 memset(amf, 0, AKA_AMF_LEN);
129 this->f->f5star(this->f, k, rand, aks);
130 this->f->f1star(this->f, k, rand, this->sqn, amf, macs);
131 /* AUTS = SQN xor AKS | MACS */
132 memcpy(auts, this->sqn, AKA_SQN_LEN);
133 memxor(auts, aks, AKA_AK_LEN);
134 memcpy(auts + AKA_AK_LEN, macs, AKA_MAC_LEN);
135
136 return TRUE;
137 }
138
139 /**
140 * Implementation of eap_aka_3gpp2_card_t.destroy.
141 */
142 static void destroy(private_eap_aka_3gpp2_card_t *this)
143 {
144 free(this);
145 }
146
147 /**
148 * See header
149 */
150 eap_aka_3gpp2_card_t *eap_aka_3gpp2_card_create(eap_aka_3gpp2_functions_t *f)
151 {
152 private_eap_aka_3gpp2_card_t *this = malloc_thing(private_eap_aka_3gpp2_card_t);
153
154 this->public.card.get_triplet = (bool(*)(sim_card_t*, identification_t *imsi, char rand[SIM_RAND_LEN], char sres[SIM_SRES_LEN], char kc[SIM_KC_LEN]))return_false;
155 this->public.card.get_quintuplet = (status_t(*)(sim_card_t*, identification_t *imsi, char rand[AKA_RAND_LEN], char autn[AKA_AUTN_LEN], char ck[AKA_CK_LEN], char ik[AKA_IK_LEN], char res[AKA_RES_LEN]))get_quintuplet;
156 this->public.card.resync = (bool(*)(sim_card_t*, identification_t *imsi, char rand[AKA_RAND_LEN], char auts[AKA_AUTS_LEN]))resync;
157 this->public.card.get_pseudonym = (identification_t*(*)(sim_card_t*, identification_t *perm))return_null;
158 this->public.card.set_pseudonym = (void(*)(sim_card_t*, identification_t *perm, identification_t *pseudonym))nop;
159 this->public.card.get_reauth = (identification_t*(*)(sim_card_t*, identification_t *perm, char mk[HASH_SIZE_SHA1], u_int16_t *counter))return_null;
160 this->public.card.set_reauth = (void(*)(sim_card_t*, identification_t *perm, identification_t* next, char mk[HASH_SIZE_SHA1], u_int16_t counter))nop;
161 this->public.destroy = (void(*)(eap_aka_3gpp2_card_t*))destroy;
162
163 this->f = f;
164 this->seq_check = lib->settings->get_bool(lib->settings,
165 "charon.plugins.eap-aka-3gpp2.seq_check",
166 #ifdef SEQ_CHECK /* handle legacy compile time configuration as default */
167 TRUE);
168 #else /* !SEQ_CHECK */
169 FALSE);
170 #endif /* SEQ_CHECK */
171
172 eap_aka_3gpp2_get_sqn(this->sqn, 0);
173
174 return &this->public;
175 }
176