cec06fbd7e8c07cd49b76247880348271d3e1113
[strongswan.git] / src / libcharon / 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 METHOD(simaka_card_t, get_quintuplet, status_t,
55 private_eap_aka_3gpp2_card_t *this, identification_t *id,
56 char rand[AKA_RAND_LEN], char autn[AKA_AUTN_LEN], char ck[AKA_CK_LEN],
57 char ik[AKA_IK_LEN], char res[AKA_RES_MAX], int *res_len)
58 {
59 char *amf, *mac;
60 char k[AKA_K_LEN], ak[AKA_AK_LEN], sqn[AKA_SQN_LEN], xmac[AKA_MAC_LEN];
61
62 if (!eap_aka_3gpp2_get_k(id, k))
63 {
64 DBG1(DBG_IKE, "no EAP key found for %Y to authenticate with AKA", id);
65 return FAILED;
66 }
67
68 /* AUTN = SQN xor AK | AMF | MAC */
69 DBG3(DBG_IKE, "received autn %b", autn, AKA_AUTN_LEN);
70 DBG3(DBG_IKE, "using K %b", k, AKA_K_LEN);
71 DBG3(DBG_IKE, "using rand %b", rand, AKA_RAND_LEN);
72 memcpy(sqn, autn, AKA_SQN_LEN);
73 amf = autn + AKA_SQN_LEN;
74 mac = autn + AKA_SQN_LEN + AKA_AMF_LEN;
75
76 /* XOR anonymity key AK into SQN to decrypt it */
77 this->f->f5(this->f, k, rand, ak);
78 DBG3(DBG_IKE, "using ak %b", ak, AKA_AK_LEN);
79 memxor(sqn, ak, AKA_SQN_LEN);
80 DBG3(DBG_IKE, "using sqn %b", sqn, AKA_SQN_LEN);
81
82 /* calculate expected MAC and compare against received one */
83 this->f->f1(this->f, k, rand, sqn, amf, xmac);
84 if (!memeq(mac, xmac, AKA_MAC_LEN))
85 {
86 DBG1(DBG_IKE, "received MAC does not match XMAC");
87 DBG3(DBG_IKE, "MAC %b\nXMAC %b", mac, AKA_MAC_LEN, xmac, AKA_MAC_LEN);
88 return FAILED;
89 }
90
91 if (this->seq_check && memcmp(this->sqn, sqn, AKA_SQN_LEN) >= 0)
92 {
93 DBG3(DBG_IKE, "received SQN %b\ncurrent SQN %b",
94 sqn, AKA_SQN_LEN, this->sqn, AKA_SQN_LEN);
95 return INVALID_STATE;
96 }
97
98 /* update stored SQN to the received one */
99 memcpy(this->sqn, sqn, AKA_SQN_LEN);
100
101 /* CK/IK */
102 this->f->f3(this->f, k, rand, ck);
103 this->f->f4(this->f, k, rand, ik);
104 /* calculate RES */
105 this->f->f2(this->f, k, rand, res);
106 *res_len = AKA_RES_MAX;
107
108 return SUCCESS;
109 }
110
111 METHOD(simaka_card_t, resync, bool,
112 private_eap_aka_3gpp2_card_t *this, identification_t *id,
113 char rand[AKA_RAND_LEN], char auts[AKA_AUTS_LEN])
114 {
115 char amf[AKA_AMF_LEN], k[AKA_K_LEN], aks[AKA_AK_LEN], macs[AKA_MAC_LEN];
116
117 if (!eap_aka_3gpp2_get_k(id, k))
118 {
119 DBG1(DBG_IKE, "no EAP key found for %Y to resync AKA", id);
120 return FALSE;
121 }
122
123 /* AMF is set to zero in resync */
124 memset(amf, 0, AKA_AMF_LEN);
125 this->f->f5star(this->f, k, rand, aks);
126 this->f->f1star(this->f, k, rand, this->sqn, amf, macs);
127 /* AUTS = SQN xor AKS | MACS */
128 memcpy(auts, this->sqn, AKA_SQN_LEN);
129 memxor(auts, aks, AKA_AK_LEN);
130 memcpy(auts + AKA_AK_LEN, macs, AKA_MAC_LEN);
131
132 return TRUE;
133 }
134
135 METHOD(eap_aka_3gpp2_card_t, destroy, void,
136 private_eap_aka_3gpp2_card_t *this)
137 {
138 free(this);
139 }
140
141 /**
142 * See header
143 */
144 eap_aka_3gpp2_card_t *eap_aka_3gpp2_card_create(eap_aka_3gpp2_functions_t *f)
145 {
146 private_eap_aka_3gpp2_card_t *this;
147
148 INIT(this,
149 .public = {
150 .card = {
151 .get_triplet = (void*)return_false,
152 .get_quintuplet = _get_quintuplet,
153 .resync = _resync,
154 .get_pseudonym = (void*)return_null,
155 .set_pseudonym = (void*)nop,
156 .get_reauth = (void*)return_null,
157 .set_reauth = (void*)nop,
158 },
159 .destroy = _destroy,
160 },
161 .f = f,
162 .seq_check = lib->settings->get_bool(lib->settings,
163 "charon.plugins.eap-aka-3gpp2.seq_check",
164 #ifdef SEQ_CHECK /* handle legacy compile time configuration as default */
165 TRUE),
166 #else /* !SEQ_CHECK */
167 FALSE),
168 #endif /* SEQ_CHECK */
169 );
170
171 eap_aka_3gpp2_get_sqn(this->sqn, 0);
172
173 return &this->public;
174 }
175