Check rng return value when generating RAND in eap-aka-3gpp2 plugin
[strongswan.git] / src / libcharon / plugins / eap_aka_3gpp2 / eap_aka_3gpp2_provider.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_provider.h"
17
18 #include <daemon.h>
19 #include <credentials/keys/shared_key.h>
20
21 typedef struct private_eap_aka_3gpp2_provider_t private_eap_aka_3gpp2_provider_t;
22
23 /**
24 * Private data of an eap_aka_3gpp2_provider_t object.
25 */
26 struct private_eap_aka_3gpp2_provider_t {
27
28 /**
29 * Public eap_aka_3gpp2_provider_t interface.
30 */
31 eap_aka_3gpp2_provider_t public;
32
33 /**
34 * AKA functions
35 */
36 eap_aka_3gpp2_functions_t *f;
37
38 /**
39 * time based SQN, we use the same for all peers
40 */
41 char sqn[AKA_SQN_LEN];
42 };
43
44 /** Authentication management field */
45 static char amf[AKA_AMF_LEN] = {0x00, 0x01};
46
47 /**
48 * Get a shared key K from the credential database
49 */
50 bool eap_aka_3gpp2_get_k(identification_t *id, char k[AKA_K_LEN])
51 {
52 shared_key_t *shared;
53 chunk_t key;
54
55 shared = lib->credmgr->get_shared(lib->credmgr, SHARED_EAP, id, NULL);
56 if (shared == NULL)
57 {
58 return FALSE;
59 }
60 key = shared->get_key(shared);
61 memset(k, '\0', AKA_K_LEN);
62 memcpy(k, key.ptr, min(key.len, AKA_K_LEN));
63 shared->destroy(shared);
64 return TRUE;
65 }
66
67 /**
68 * get SQN using current time
69 */
70 void eap_aka_3gpp2_get_sqn(char sqn[AKA_SQN_LEN], int offset)
71 {
72 timeval_t time;
73
74 gettimeofday(&time, NULL);
75 /* set sqn to an integer containing 4 bytes seconds + 2 bytes usecs */
76 time.tv_sec = htonl(time.tv_sec + offset);
77 /* usec's are never larger than 0x000f423f, so we shift the 12 first bits */
78 time.tv_usec = htonl(time.tv_usec << 12);
79 memcpy(sqn, (char*)&time.tv_sec + sizeof(time_t) - 4, 4);
80 memcpy(sqn + 4, &time.tv_usec, 2);
81 }
82
83 METHOD(simaka_provider_t, get_quintuplet, bool,
84 private_eap_aka_3gpp2_provider_t *this, identification_t *id,
85 char rand[AKA_RAND_LEN], char xres[AKA_RES_MAX], int *xres_len,
86 char ck[AKA_CK_LEN], char ik[AKA_IK_LEN], char autn[AKA_AUTN_LEN])
87 {
88 rng_t *rng;
89 char mac[AKA_MAC_LEN], ak[AKA_AK_LEN], k[AKA_K_LEN];
90
91 /* generate RAND: we use a registered RNG, not f0() proposed in S.S0055 */
92 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
93 if (!rng || !rng->get_bytes(rng, AKA_RAND_LEN, rand))
94 {
95 DBG1(DBG_IKE, "generating RAND for AKA failed");
96 DESTROY_IF(rng);
97 return FALSE;
98 }
99 rng->destroy(rng);
100
101 if (!eap_aka_3gpp2_get_k(id, k))
102 {
103 DBG1(DBG_IKE, "no EAP key found for %Y to authenticate with AKA", id);
104 return FALSE;
105 }
106
107 DBG3(DBG_IKE, "generated rand %b", rand, AKA_RAND_LEN);
108 DBG3(DBG_IKE, "using K %b", k, AKA_K_LEN);
109
110 /* MAC */
111 this->f->f1(this->f, k, rand, this->sqn, amf, mac);
112 /* AK */
113 this->f->f5(this->f, k, rand, ak);
114 /* XRES as expected from client */
115 this->f->f2(this->f, k, rand, xres);
116 *xres_len = AKA_RES_MAX;
117 /* AUTN = (SQN xor AK) || AMF || MAC */
118 memcpy(autn, this->sqn, AKA_SQN_LEN);
119 memxor(autn, ak, AKA_AK_LEN);
120 memcpy(autn + AKA_SQN_LEN, amf, AKA_AMF_LEN);
121 memcpy(autn + AKA_SQN_LEN + AKA_AMF_LEN, mac, AKA_MAC_LEN);
122 DBG3(DBG_IKE, "AUTN %b", autn, AKA_AUTN_LEN);
123 /* CK/IK */
124 this->f->f3(this->f, k, rand, ck);
125 this->f->f4(this->f, k, rand, ik);
126
127 return TRUE;
128 }
129
130 METHOD(simaka_provider_t, resync, bool,
131 private_eap_aka_3gpp2_provider_t *this, identification_t *id,
132 char rand[AKA_RAND_LEN], char auts[AKA_AUTS_LEN])
133 {
134 char *sqn, *macs;
135 char aks[AKA_AK_LEN], k[AKA_K_LEN], amf[AKA_AMF_LEN], xmacs[AKA_MAC_LEN];
136
137 if (!eap_aka_3gpp2_get_k(id, k))
138 {
139 DBG1(DBG_IKE, "no EAP key found for %Y to authenticate with AKA", id);
140 return FALSE;
141 }
142
143 /* AUTHS = (AK xor SQN) | MAC */
144 sqn = auts;
145 macs = auts + AKA_SQN_LEN;
146 this->f->f5star(this->f, k, rand, aks);
147 memxor(sqn, aks, AKA_AK_LEN);
148
149 /* verify XMACS, AMF of zero is used in resynchronization */
150 memset(amf, 0, AKA_AMF_LEN);
151 this->f->f1star(this->f, k, rand, sqn, amf, xmacs);
152 if (!memeq(macs, xmacs, AKA_MAC_LEN))
153 {
154 DBG1(DBG_IKE, "received MACS does not match XMACS");
155 DBG3(DBG_IKE, "MACS %b XMACS %b",
156 macs, AKA_MAC_LEN, xmacs, AKA_MAC_LEN);
157 return FALSE;
158 }
159 /* update stored SQN to received SQN + 1 */
160 memcpy(this->sqn, sqn, AKA_SQN_LEN);
161 chunk_increment(chunk_create(this->sqn, AKA_SQN_LEN));
162 return TRUE;
163 }
164
165 METHOD(eap_aka_3gpp2_provider_t, destroy, void,
166 private_eap_aka_3gpp2_provider_t *this)
167 {
168 free(this);
169 }
170
171 /**
172 * See header
173 */
174 eap_aka_3gpp2_provider_t *eap_aka_3gpp2_provider_create(
175 eap_aka_3gpp2_functions_t *f)
176 {
177 private_eap_aka_3gpp2_provider_t *this;
178
179 INIT(this,
180 .public = {
181 .provider = {
182 .get_triplet = (void*)return_false,
183 .get_quintuplet = _get_quintuplet,
184 .resync = _resync,
185 .is_pseudonym = (void*)return_null,
186 .gen_pseudonym = (void*)return_null,
187 .is_reauth = (void*)return_null,
188 .gen_reauth = (void*)return_null,
189 },
190 .destroy = _destroy,
191 },
192 .f = f,
193 );
194 /* use an offset to accept clock skew between client/server without resync */
195 eap_aka_3gpp2_get_sqn(this->sqn, 180);
196
197 return &this->public;
198 }
199