6 * Copyright (C) 2008 Alan DeKok <aland@networkradius.com>
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
11 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
12 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
13 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
14 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 #include <freeradius-devel/ident.h>
21 #include <freeradius-devel/radiusd.h>
22 #include <freeradius-devel/modules.h>
25 * FIXME: Add check for this header to configure.in
27 #include <openssl/hmac.h>
30 * FIXME: Fix the build system to create definitions from names.
32 typedef struct rlm_wimax_t {
37 * A mapping of configuration file names to internal variables.
39 * Note that the string is dynamically allocated, so it MUST
40 * be freed. When the configuration file parse re-reads the string,
41 * it free's the old one, and strdup's the new one, placing the pointer
42 * to the strdup'd string into 'config.string'. This gets around
45 static const CONF_PARSER module_config[] = {
46 { "delete_mppe_keys", PW_TYPE_BOOLEAN,
47 offsetof(rlm_wimax_t,delete_mppe_keys), NULL, "no" },
49 { NULL, -1, 0, NULL, NULL } /* end the list */
54 * Only free memory we allocated. The strings allocated via
55 * cf_section_parse() do not need to be freed.
57 static int wimax_detach(void *instance)
64 * Do any per-module initialization that is separate to each
65 * configured instance of the module. e.g. set up connections
66 * to external databases, read configuration files, set up
67 * dictionary entries, etc.
69 * If configuration information is given in the config section
70 * that must be referenced in later calls, store a handle to it
71 * in *instance otherwise put a null pointer there.
73 static int wimax_instantiate(CONF_SECTION *conf, void **instance)
78 * Set up a storage area for instance data
80 inst = rad_malloc(sizeof(*inst));
84 memset(inst, 0, sizeof(*inst));
87 * If the configuration parameters can't be parsed, then
90 if (cf_section_parse(conf, inst, module_config) < 0) {
101 * Find the named user in this modules database. Create the set
102 * of attribute-value pairs to check and reply with for this user
103 * from the database. The authentication code only needs to check
104 * the password, the rest is done here.
106 static int wimax_authorize(void *instance, REQUEST *request)
110 /* quiet the compiler */
115 * Fix Calling-Station-Id. Damn you, WiMAX!
117 vp = pairfind(request->packet->vps, PW_CALLING_STATION_ID, 0);
118 if (vp && (vp->length == 6)) {
122 memcpy(buffer, vp->vp_octets, 6);
125 * RFC 3580 Section 3.20 says this is the preferred
126 * format. Everyone *SANE* is using this format,
129 for (i = 0; i < 6; i++) {
130 fr_bin2hex(&buffer[i], &vp->vp_strvalue[i * 3], 1);
131 vp->vp_strvalue[(i * 3) + 2] = '-';
134 vp->vp_strvalue[(5*3)+2] = '\0';
135 vp->length = (5*3)+2;
137 DEBUG2("rlm_wimax: Fixing WiMAX binary Calling-Station-Id to %s",
141 return RLM_MODULE_OK;
146 * Massage the request before recording it or proxying it
148 static int wimax_preacct(void *instance, REQUEST *request)
150 return wimax_authorize(instance, request);
154 * Write accounting information to this modules database.
156 static int wimax_accounting(void *instance, REQUEST *request)
158 /* quiet the compiler */
162 return RLM_MODULE_OK;
166 * Generate the keys after the user has been authenticated.
168 static int wimax_postauth(void *instance, REQUEST *request)
170 rlm_wimax_t *inst = instance;
171 VALUE_PAIR *msk, *emsk, *vp;
172 VALUE_PAIR *mn_nai, *ip, *fa_rk;
174 unsigned int rk1_len, rk2_len, rk_len;
175 int rk_lifetime = 3600; /* ? */
177 uint8_t usage_data[24];
178 uint8_t mip_rk_1[EVP_MAX_MD_SIZE], mip_rk_2[EVP_MAX_MD_SIZE];
179 uint8_t mip_rk[2 * EVP_MAX_MD_SIZE];
181 msk = pairfind(request->reply->vps, 1129, 0);
182 emsk = pairfind(request->reply->vps, 1130, 0);
184 RDEBUG("No EAP-MSK or EAP-EMSK. Cannot create WiMAX keys.");
185 return RLM_MODULE_NOOP;
189 * If we delete the MS-MPPE-*-Key attributes, then add in
190 * the WiMAX-MSK so that the client has a key available.
192 if (inst->delete_mppe_keys) {
193 pairdelete(&request->reply->vps, 16, VENDORPEC_MICROSOFT);
194 pairdelete(&request->reply->vps, 17, VENDORPEC_MICROSOFT);
196 vp = radius_pairmake(request, &request->reply->vps, "WiMAX-MSK", "0x00", T_OP_EQ);
198 memcpy(vp->vp_octets, msk->vp_octets, msk->length);
199 vp->length = msk->length;
204 * Initialize usage data.
206 memcpy(usage_data, "miprk@wimaxforum.org", 21); /* with trailing \0 */
207 usage_data[21] = 0x02;
208 usage_data[22] = 0x00;
209 usage_data[23] = 0x01;
212 * MIP-RK-1 = HMAC-SSHA256(EMSK, usage-data | 0x01)
214 HMAC_CTX_init(&hmac);
215 HMAC_Init_ex(&hmac, emsk->vp_octets, emsk->length, EVP_sha256(), NULL);
217 HMAC_Update(&hmac, &usage_data[0], sizeof(usage_data));
218 HMAC_Final(&hmac, &mip_rk_1[0], &rk1_len);
221 * MIP-RK-2 = HMAC-SSHA256(EMSK, MIP-RK-1 | usage-data | 0x01)
223 HMAC_Init_ex(&hmac, emsk->vp_octets, emsk->length, EVP_sha256(), NULL);
225 HMAC_Update(&hmac, (const uint8_t *) &mip_rk_1, rk1_len);
226 HMAC_Update(&hmac, &usage_data[0], sizeof(usage_data));
227 HMAC_Final(&hmac, &mip_rk_2[0], &rk2_len);
229 vp = pairfind(request->reply->vps, PW_SESSION_TIMEOUT, 0);
230 if (vp) rk_lifetime = vp->vp_integer;
232 memcpy(mip_rk, mip_rk_1, rk1_len);
233 memcpy(mip_rk + rk1_len, mip_rk_2, rk2_len);
234 rk_len = rk1_len + rk2_len;
237 * MIP-SPI = HMAC-SSHA256(MIP-RK, "SPI CMIP PMIP");
239 HMAC_Init_ex(&hmac, mip_rk, rk_len, EVP_sha256(), NULL);
241 HMAC_Update(&hmac, (const uint8_t *) "SPI CMIP PMIP", 12);
242 HMAC_Final(&hmac, &mip_rk_1[0], &rk1_len);
245 * Take the 4 most significant octets.
246 * If less than 256, add 256.
248 mip_spi = ((mip_rk_1[0] << 24) | (mip_rk_1[1] << 16) |
249 (mip_rk_1[2] << 8) | mip_rk_1[3]);
250 if (mip_spi < 256) mip_spi += 256;
256 if (len > 128) len = 128; /* buffer size */
258 fr_bin2hex(mip_rk, buffer, len);
259 radlog_request(L_DBG, 0, request, "MIP-RK = 0x%s", buffer);
260 radlog_request(L_DBG, 0, request, "MIP-SPI = %08x",
265 * FIXME: Perform SPI collision prevention
269 * Calculate mobility keys
271 mn_nai = pairfind(request->packet->vps, 1900, 0);
272 if (!mn_nai) mn_nai = pairfind(request->reply->vps, 1900, 0);
274 RDEBUG("WARNING: WiMAX-MN-NAI was not found in the request or in the reply.");
275 RDEBUG("WARNING: We cannot calculate MN-HA keys.");
279 * WiMAX-IP-Technology
282 if (mn_nai) vp = pairfind(request->reply->vps, 23, VENDORPEC_WIMAX);
284 RDEBUG("WARNING: WiMAX-IP-Technology not found in reply.");
285 RDEBUG("WARNING: Not calculating MN-HA keys");
288 if (vp) switch (vp->vp_integer) {
291 * Look for WiMAX-hHA-IP-MIP4
293 ip = pairfind(request->reply->vps, 6, VENDORPEC_WIMAX);
295 RDEBUG("WARNING: WiMAX-hHA-IP-MIP4 not found. Cannot calculate MN-HA-PMIP4 key");
301 * H(MIP-RK, "PMIP4 MN HA" | HA-IPv4 | MN-NAI);
303 HMAC_Init_ex(&hmac, mip_rk, rk_len, EVP_sha1(), NULL);
305 HMAC_Update(&hmac, (const uint8_t *) "PMIP4 MN HA", 11);
306 HMAC_Update(&hmac, (const uint8_t *) &ip->vp_ipaddr, 4);
307 HMAC_Update(&hmac, (const uint8_t *) &mn_nai->vp_strvalue, mn_nai->length);
308 HMAC_Final(&hmac, &mip_rk_1[0], &rk1_len);
311 * Put MN-HA-PMIP4 into WiMAX-MN-hHA-MIP4-Key
313 vp = pairfind(request->reply->vps, 10, VENDORPEC_WIMAX);
315 vp = radius_paircreate(request, &request->reply->vps,
316 10, VENDORPEC_WIMAX, PW_TYPE_OCTETS);
319 RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP4-Key");
322 memcpy(vp->vp_octets, &mip_rk_1[0], rk1_len);
323 vp->length = rk1_len;
326 * Put MN-HA-PMIP4-SPI into WiMAX-MN-hHA-MIP4-SPI
328 vp = pairfind(request->reply->vps, 11, VENDORPEC_WIMAX);
330 vp = radius_paircreate(request, &request->reply->vps,
331 11, VENDORPEC_WIMAX, PW_TYPE_INTEGER);
334 RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP4-SPI");
337 vp->vp_integer = mip_spi + 1;
342 * Look for WiMAX-hHA-IP-MIP4
344 ip = pairfind(request->reply->vps, 6, VENDORPEC_WIMAX);
346 RDEBUG("WARNING: WiMAX-hHA-IP-MIP4 not found. Cannot calculate MN-HA-CMIP4 key");
352 * H(MIP-RK, "CMIP4 MN HA" | HA-IPv4 | MN-NAI);
354 HMAC_Init_ex(&hmac, mip_rk, rk_len, EVP_sha1(), NULL);
356 HMAC_Update(&hmac, (const uint8_t *) "CMIP4 MN HA", 11);
357 HMAC_Update(&hmac, (const uint8_t *) &ip->vp_ipaddr, 4);
358 HMAC_Update(&hmac, (const uint8_t *) &mn_nai->vp_strvalue, mn_nai->length);
359 HMAC_Final(&hmac, &mip_rk_1[0], &rk1_len);
362 * Put MN-HA-CMIP4 into WiMAX-MN-hHA-MIP4-Key
364 vp = pairfind(request->reply->vps, 10, VENDORPEC_WIMAX);
366 vp = radius_paircreate(request, &request->reply->vps,
367 10, VENDORPEC_WIMAX, PW_TYPE_OCTETS);
370 RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP4-Key");
373 memcpy(vp->vp_octets, &mip_rk_1[0], rk1_len);
374 vp->length = rk1_len;
377 * Put MN-HA-CMIP4-SPI into WiMAX-MN-hHA-MIP4-SPI
379 vp = pairfind(request->reply->vps, 11, VENDORPEC_WIMAX);
381 vp = radius_paircreate(request, &request->reply->vps,
382 11, VENDORPEC_WIMAX, PW_TYPE_INTEGER);
385 RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP4-SPI");
388 vp->vp_integer = mip_spi;
393 * Look for WiMAX-hHA-IP-MIP6
395 ip = pairfind(request->reply->vps, 7, VENDORPEC_WIMAX);
397 RDEBUG("WARNING: WiMAX-hHA-IP-MIP6 not found. Cannot calculate MN-HA-CMIP6 key");
403 * H(MIP-RK, "CMIP6 MN HA" | HA-IPv6 | MN-NAI);
405 HMAC_Init_ex(&hmac, mip_rk, rk_len, EVP_sha1(), NULL);
407 HMAC_Update(&hmac, (const uint8_t *) "CMIP6 MN HA", 11);
408 HMAC_Update(&hmac, (const uint8_t *) &ip->vp_ipv6addr, 16);
409 HMAC_Update(&hmac, (const uint8_t *) &mn_nai->vp_strvalue, mn_nai->length);
410 HMAC_Final(&hmac, &mip_rk_1[0], &rk1_len);
413 * Put MN-HA-CMIP6 into WiMAX-MN-hHA-MIP6-Key
415 vp = pairfind(request->reply->vps, 12, VENDORPEC_WIMAX);
417 vp = radius_paircreate(request, &request->reply->vps,
418 12, VENDORPEC_WIMAX, PW_TYPE_OCTETS);
421 RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP6-Key");
424 memcpy(vp->vp_octets, &mip_rk_1[0], rk1_len);
425 vp->length = rk1_len;
428 * Put MN-HA-CMIP6-SPI into WiMAX-MN-hHA-MIP6-SPI
430 vp = pairfind(request->reply->vps, 13, VENDORPEC_WIMAX);
432 vp = radius_paircreate(request, &request->reply->vps,
433 13, VENDORPEC_WIMAX, PW_TYPE_INTEGER);
436 RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP6-SPI");
439 vp->vp_integer = mip_spi + 2;
443 break; /* do nothing */
447 * Generate FA-RK, if requested.
449 * FA-RK= H(MIP-RK, "FA-RK")
451 fa_rk = pairfind(request->reply->vps, 14, VENDORPEC_WIMAX);
452 if (fa_rk && (fa_rk->length <= 1)) {
453 HMAC_Init_ex(&hmac, mip_rk, rk_len, EVP_sha1(), NULL);
455 HMAC_Update(&hmac, (const uint8_t *) "FA-RK", 5);
457 HMAC_Final(&hmac, &mip_rk_1[0], &rk1_len);
459 memcpy(fa_rk->vp_octets, &mip_rk_1[0], rk1_len);
460 fa_rk->length = rk1_len;
464 * Create FA-RK-SPI, which is really SPI-CMIP4, which is
465 * really MIP-SPI. Clear? Of course. This is WiMAX.
468 vp = pairfind(request->reply->vps, 61, VENDORPEC_WIMAX);
470 vp = radius_paircreate(request, &request->reply->vps,
471 61, VENDORPEC_WIMAX, PW_TYPE_INTEGER);
474 RDEBUG("WARNING: Failed creating WiMAX-FA-RK-SPI");
476 vp->vp_integer = mip_spi;
481 * Generate MN-FA = H(FA-RK, "MN FA" | FA-IP | MN-NAI)
483 ip = pairfind(request->reply->vps, 1901, 0);
484 if (fa_rk && ip && mn_nai) {
485 HMAC_Init_ex(&hmac, fa_rk->vp_octets, fa_rk->length,
488 HMAC_Update(&hmac, (const uint8_t *) "MN FA", 5);
489 HMAC_Update(&hmac, (const uint8_t *) &ip->vp_ipaddr, 4);
490 HMAC_Update(&hmac, (const uint8_t *) &mn_nai->vp_strvalue, mn_nai->length);
492 HMAC_Final(&hmac, &mip_rk_1[0], &rk1_len);
494 vp = radius_paircreate(request, &request->reply->vps,
495 1902, 0, PW_TYPE_OCTETS);
497 RDEBUG("WARNING: Failed creating WiMAX-MN-FA");
499 memcpy(vp->vp_octets, &mip_rk_1[0], rk1_len);
500 vp->length = rk1_len;
505 * Give additional information about requests && responses
507 * WiMAX-RRQ-MN-HA-SPI
509 vp = pairfind(request->packet->vps, 20, VENDORPEC_WIMAX);
511 RDEBUG("Client requested MN-HA key: Should use SPI to look up key from storage.");
513 RDEBUG("WARNING: MN-NAI was not found!");
519 if (!pairfind(request->packet->vps, 18, VENDORPEC_WIMAX)) {
520 RDEBUG("WARNING: HA-IP was not found!");
525 * WiMAX-HA-RK-Key-Requested
527 vp = pairfind(request->packet->vps, 58, VENDORPEC_WIMAX);
528 if (vp && (vp->vp_integer == 1)) {
529 RDEBUG("Client requested HA-RK: Should use IP to look it up from storage.");
534 * Wipe the context of all sensitive information.
536 HMAC_CTX_cleanup(&hmac);
538 return RLM_MODULE_UPDATED;
543 * The module name should be the only globally exported symbol.
544 * That is, everything else should be 'static'.
546 * If the module needs to temporarily modify it's instantiation
547 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
548 * The server will then take care of ensuring that the module
549 * is single-threaded.
551 module_t rlm_wimax = {
554 RLM_TYPE_THREAD_SAFE, /* type */
555 wimax_instantiate, /* instantiation */
556 wimax_detach, /* detach */
558 NULL, /* authentication */
559 wimax_authorize, /* authorization */
560 wimax_preacct, /* preaccounting */
561 wimax_accounting, /* accounting */
562 NULL, /* checksimul */
563 NULL, /* pre-proxy */
564 NULL, /* post-proxy */
565 wimax_postauth /* post-auth */