Removed extra calls to HMAC_CTX_init()
[freeradius.git] / src / modules / rlm_wimax / rlm_wimax.c
1 /*
2  * rlm_wimax.c
3  *
4  * Version:     $Id$
5  *
6  * Copyright (C) 2008 Alan DeKok <aland@networkradius.com>
7  *
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
15  * SOFTWARE.
16  */
17
18 #include <freeradius-devel/ident.h>
19 RCSID("$Id$")
20
21 #include <freeradius-devel/radiusd.h>
22 #include <freeradius-devel/modules.h>
23
24 /*
25  *      FIXME: Add check for this header to configure.in
26  */
27 #include <openssl/hmac.h>
28
29 /*
30  *      FIXME: Fix the build system to create definitions from names.
31  */
32 typedef struct rlm_wimax_t {
33         int     delete_mppe_keys;
34 } rlm_wimax_t;
35
36 /*
37  *      A mapping of configuration file names to internal variables.
38  *
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
43  *      buffer over-flows.
44  */
45 static const CONF_PARSER module_config[] = {
46   { "delete_mppe_keys", PW_TYPE_BOOLEAN,
47     offsetof(rlm_wimax_t,delete_mppe_keys), NULL,   "no" },
48
49   { NULL, -1, 0, NULL, NULL }           /* end the list */
50 };
51
52
53 /*
54  *      Only free memory we allocated.  The strings allocated via
55  *      cf_section_parse() do not need to be freed.
56  */
57 static int wimax_detach(void *instance)
58 {
59         free(instance);
60         return 0;
61 }
62
63 /*
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.
68  *
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.
72  */
73 static int wimax_instantiate(CONF_SECTION *conf, void **instance)
74 {
75         rlm_wimax_t *inst;
76
77         /*
78          *      Set up a storage area for instance data
79          */
80         inst = rad_malloc(sizeof(*inst));
81         if (!inst) {
82                 return -1;
83         }
84         memset(inst, 0, sizeof(*inst));
85
86         /*
87          *      If the configuration parameters can't be parsed, then
88          *      fail.
89          */
90         if (cf_section_parse(conf, inst, module_config) < 0) {
91                 wimax_detach(inst);
92                 return -1;
93         }
94
95         *instance = inst;
96
97         return 0;
98 }
99
100 /*
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.
105  */
106 static int wimax_authorize(void *instance, REQUEST *request)
107 {
108         VALUE_PAIR *vp;
109
110         /* quiet the compiler */
111         instance = instance;
112         request = request;
113
114         /*
115          *      Fix Calling-Station-Id.  Damn you, WiMAX!
116          */
117         vp =  pairfind(request->packet->vps, PW_CALLING_STATION_ID, 0);
118         if (vp && (vp->length == 6)) {
119                 int i;
120                 uint8_t buffer[6];
121
122                 memcpy(buffer, vp->vp_octets, 6);
123
124                 /*
125                  *      RFC 3580 Section 3.20 says this is the preferred
126                  *      format.  Everyone *SANE* is using this format,
127                  *      so we fix it here.
128                  */
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] = '-';
132                 }
133
134                 vp->vp_strvalue[(5*3)+2] = '\0';
135                 vp->length = (5*3)+2;
136
137                 DEBUG2("rlm_wimax: Fixing WiMAX binary Calling-Station-Id to %s",
138                        vp->vp_strvalue);
139         }
140
141         return RLM_MODULE_OK;
142 }
143
144
145 /*
146  *      Massage the request before recording it or proxying it
147  */
148 static int wimax_preacct(void *instance, REQUEST *request)
149 {
150         return wimax_authorize(instance, request);
151 }
152
153 /*
154  *      Write accounting information to this modules database.
155  */
156 static int wimax_accounting(void *instance, REQUEST *request)
157 {
158         /* quiet the compiler */
159         instance = instance;
160         request = request;
161
162         return RLM_MODULE_OK;
163 }
164
165 /*
166  *      Generate the keys after the user has been authenticated.
167  */
168 static int wimax_postauth(void *instance, REQUEST *request)
169 {
170         rlm_wimax_t *inst = instance;
171         VALUE_PAIR *msk, *emsk, *vp;
172         VALUE_PAIR *mn_nai, *ip, *fa_rk;
173         HMAC_CTX hmac;
174         unsigned int rk1_len, rk2_len, rk_len;
175         int rk_lifetime = 3600; /* ? */
176         uint32_t mip_spi;
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];
180
181         msk = pairfind(request->reply->vps, 1129, 0);
182         emsk = pairfind(request->reply->vps, 1130, 0);
183         if (!msk || !emsk) {
184                 RDEBUG("No EAP-MSK or EAP-EMSK.  Cannot create WiMAX keys.");
185                 return RLM_MODULE_NOOP;
186         }
187
188         /*
189          *      If we delete the MS-MPPE-*-Key attributes, then add in
190          *      the WiMAX-MSK so that the client has a key available.
191          */
192         if (inst->delete_mppe_keys) {
193                 pairdelete(&request->reply->vps, 16, VENDORPEC_MICROSOFT);
194                 pairdelete(&request->reply->vps, 17, VENDORPEC_MICROSOFT);
195
196                 vp = radius_pairmake(request, &request->reply->vps, "WiMAX-MSK", "0x00", T_OP_EQ);
197                 if (vp) {
198                         memcpy(vp->vp_octets, msk->vp_octets, msk->length);
199                         vp->length = msk->length;
200                 }
201         }
202
203         /*
204          *      Initialize usage data.
205          */
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;
210
211         /*
212          *      MIP-RK-1 = HMAC-SSHA256(EMSK, usage-data | 0x01)
213          */
214         HMAC_CTX_init(&hmac);
215         HMAC_Init_ex(&hmac, emsk->vp_octets, emsk->length, EVP_sha256(), NULL);
216         
217         HMAC_Update(&hmac, &usage_data[0], sizeof(usage_data));
218         HMAC_Final(&hmac, &mip_rk_1[0], &rk1_len);
219
220         /*
221          *      MIP-RK-2 = HMAC-SSHA256(EMSK, MIP-RK-1 | usage-data | 0x01)
222          */
223         HMAC_Init_ex(&hmac, emsk->vp_octets, emsk->length, EVP_sha256(), NULL);
224         
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);
228
229         vp = pairfind(request->reply->vps, PW_SESSION_TIMEOUT, 0);
230         if (vp) rk_lifetime = vp->vp_integer;
231
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;
235
236         /*
237          *      MIP-SPI = HMAC-SSHA256(MIP-RK, "SPI CMIP PMIP");
238          */
239         HMAC_Init_ex(&hmac, mip_rk, rk_len, EVP_sha256(), NULL);
240         
241         HMAC_Update(&hmac, (const uint8_t *) "SPI CMIP PMIP", 12);
242         HMAC_Final(&hmac, &mip_rk_1[0], &rk1_len);
243
244         /*
245          *      Take the 4 most significant octets.
246          *      If less than 256, add 256.
247          */
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;
251
252         if (debug_flag) {
253                 int len = rk_len;
254                 char buffer[512];
255
256                 if (len > 128) len = 128; /* buffer size */
257
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",
261                                ntohl(mip_spi));
262         }
263
264         /*
265          *      FIXME: Perform SPI collision prevention
266          */
267
268         /*
269          *      Calculate mobility keys
270          */
271         mn_nai = pairfind(request->packet->vps, 1900, 0);
272         if (!mn_nai) mn_nai = pairfind(request->reply->vps, 1900, 0);
273         if (!mn_nai) {
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.");
276         }
277
278         /*
279          *      WiMAX-IP-Technology
280          */
281         vp = NULL;
282         if (mn_nai) vp = pairfind(request->reply->vps, 23, VENDORPEC_WIMAX);
283         if (!vp) {
284                 RDEBUG("WARNING: WiMAX-IP-Technology not found in reply.");
285                 RDEBUG("WARNING: Not calculating MN-HA keys");
286         }
287
288         if (vp) switch (vp->vp_integer) {
289         case 2:                 /* PMIP4 */
290                 /*
291                  *      Look for WiMAX-hHA-IP-MIP4
292                  */
293                 ip = pairfind(request->reply->vps, 6, VENDORPEC_WIMAX);
294                 if (!ip) {
295                         RDEBUG("WARNING: WiMAX-hHA-IP-MIP4 not found.  Cannot calculate MN-HA-PMIP4 key");
296                         break;
297                 }
298
299                 /*
300                  *      MN-HA-PMIP4 =
301                  *         H(MIP-RK, "PMIP4 MN HA" | HA-IPv4 | MN-NAI);
302                  */
303                 HMAC_Init_ex(&hmac, mip_rk, rk_len, EVP_sha1(), NULL);
304
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);
309
310                 /*
311                  *      Put MN-HA-PMIP4 into WiMAX-MN-hHA-MIP4-Key
312                  */
313                 vp = pairfind(request->reply->vps, 10, VENDORPEC_WIMAX);
314                 if (!vp) {
315                         vp = radius_paircreate(request, &request->reply->vps,
316                                                10, VENDORPEC_WIMAX, PW_TYPE_OCTETS);
317                 }
318                 if (!vp) {
319                         RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP4-Key");
320                         break;
321                 }
322                 memcpy(vp->vp_octets, &mip_rk_1[0], rk1_len);
323                 vp->length = rk1_len;
324
325                 /*
326                  *      Put MN-HA-PMIP4-SPI into WiMAX-MN-hHA-MIP4-SPI
327                  */
328                 vp = pairfind(request->reply->vps, 11, VENDORPEC_WIMAX);
329                 if (!vp) {
330                         vp = radius_paircreate(request, &request->reply->vps,
331                                                11, VENDORPEC_WIMAX, PW_TYPE_INTEGER);
332                 }
333                 if (!vp) {
334                         RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP4-SPI");
335                         break;
336                 }
337                 vp->vp_integer = mip_spi + 1;
338                 break;
339
340         case 3:                 /* CMIP4 */
341                 /*
342                  *      Look for WiMAX-hHA-IP-MIP4
343                  */
344                 ip = pairfind(request->reply->vps, 6, VENDORPEC_WIMAX);
345                 if (!ip) {
346                         RDEBUG("WARNING: WiMAX-hHA-IP-MIP4 not found.  Cannot calculate MN-HA-CMIP4 key");
347                         break;
348                 }
349
350                 /*
351                  *      MN-HA-CMIP4 =
352                  *         H(MIP-RK, "CMIP4 MN HA" | HA-IPv4 | MN-NAI);
353                  */
354                 HMAC_Init_ex(&hmac, mip_rk, rk_len, EVP_sha1(), NULL);
355
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);
360
361                 /*
362                  *      Put MN-HA-CMIP4 into WiMAX-MN-hHA-MIP4-Key
363                  */
364                 vp = pairfind(request->reply->vps, 10, VENDORPEC_WIMAX);
365                 if (!vp) {
366                         vp = radius_paircreate(request, &request->reply->vps,
367                                                10, VENDORPEC_WIMAX, PW_TYPE_OCTETS);
368                 }
369                 if (!vp) {
370                         RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP4-Key");
371                         break;
372                 }
373                 memcpy(vp->vp_octets, &mip_rk_1[0], rk1_len);
374                 vp->length = rk1_len;
375
376                 /*
377                  *      Put MN-HA-CMIP4-SPI into WiMAX-MN-hHA-MIP4-SPI
378                  */
379                 vp = pairfind(request->reply->vps, 11, VENDORPEC_WIMAX);
380                 if (!vp) {
381                         vp = radius_paircreate(request, &request->reply->vps,
382                                                11, VENDORPEC_WIMAX, PW_TYPE_INTEGER);
383                 }
384                 if (!vp) {
385                         RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP4-SPI");
386                         break;
387                 }
388                 vp->vp_integer = mip_spi;
389                 break;
390
391         case 4:                 /* CMIP6 */
392                 /*
393                  *      Look for WiMAX-hHA-IP-MIP6
394                  */
395                 ip = pairfind(request->reply->vps, 7, VENDORPEC_WIMAX);
396                 if (!ip) {
397                         RDEBUG("WARNING: WiMAX-hHA-IP-MIP6 not found.  Cannot calculate MN-HA-CMIP6 key");
398                         break;
399                 }
400
401                 /*
402                  *      MN-HA-CMIP6 =
403                  *         H(MIP-RK, "CMIP6 MN HA" | HA-IPv6 | MN-NAI);
404                  */
405                 HMAC_Init_ex(&hmac, mip_rk, rk_len, EVP_sha1(), NULL);
406
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);
411
412                 /*
413                  *      Put MN-HA-CMIP6 into WiMAX-MN-hHA-MIP6-Key
414                  */
415                 vp = pairfind(request->reply->vps, 12, VENDORPEC_WIMAX);
416                 if (!vp) {
417                         vp = radius_paircreate(request, &request->reply->vps,
418                                                12, VENDORPEC_WIMAX, PW_TYPE_OCTETS);
419                 }
420                 if (!vp) {
421                         RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP6-Key");
422                         break;
423                 }
424                 memcpy(vp->vp_octets, &mip_rk_1[0], rk1_len);
425                 vp->length = rk1_len;
426
427                 /*
428                  *      Put MN-HA-CMIP6-SPI into WiMAX-MN-hHA-MIP6-SPI
429                  */
430                 vp = pairfind(request->reply->vps, 13, VENDORPEC_WIMAX);
431                 if (!vp) {
432                         vp = radius_paircreate(request, &request->reply->vps,
433                                                13, VENDORPEC_WIMAX, PW_TYPE_INTEGER);
434                 }
435                 if (!vp) {
436                         RDEBUG("WARNING: Failed creating WiMAX-MN-hHA-MIP6-SPI");
437                         break;
438                 }
439                 vp->vp_integer = mip_spi + 2;
440                 break;
441
442         default:
443                 break;          /* do nothing */
444         }
445
446         /*
447          *      Generate FA-RK, if requested.
448          *
449          *      FA-RK= H(MIP-RK, "FA-RK")
450          */
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);
454                 
455                 HMAC_Update(&hmac, (const uint8_t *) "FA-RK", 5);
456
457                 HMAC_Final(&hmac, &mip_rk_1[0], &rk1_len);
458
459                 memcpy(fa_rk->vp_octets, &mip_rk_1[0], rk1_len);
460                 fa_rk->length = rk1_len;
461         }
462
463         /*
464          *      Create FA-RK-SPI, which is really SPI-CMIP4, which is
465          *      really MIP-SPI.  Clear?  Of course.  This is WiMAX.
466          */
467         if (fa_rk) {
468                 vp = pairfind(request->reply->vps, 61, VENDORPEC_WIMAX);
469                 if (!vp) {
470                         vp = radius_paircreate(request, &request->reply->vps,
471                                                61, VENDORPEC_WIMAX, PW_TYPE_INTEGER);
472                 }
473                 if (!vp) {
474                         RDEBUG("WARNING: Failed creating WiMAX-FA-RK-SPI");
475                 } else {
476                         vp->vp_integer = mip_spi;
477                 }
478         }
479
480         /*
481          *      Generate MN-FA = H(FA-RK, "MN FA" | FA-IP | MN-NAI)
482          */
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,
486                              EVP_sha1(), NULL);
487                 
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);
491
492                 HMAC_Final(&hmac, &mip_rk_1[0], &rk1_len);
493
494                 vp = radius_paircreate(request, &request->reply->vps,
495                                        1902, 0, PW_TYPE_OCTETS);
496                 if (!vp) {
497                         RDEBUG("WARNING: Failed creating WiMAX-MN-FA");
498                 } else {
499                         memcpy(vp->vp_octets, &mip_rk_1[0], rk1_len);
500                         vp->length = rk1_len;
501                 }
502         }
503
504         /*
505          *      Give additional information about requests && responses
506          *
507          *      WiMAX-RRQ-MN-HA-SPI
508          */
509         vp = pairfind(request->packet->vps, 20, VENDORPEC_WIMAX);
510         if (vp) {
511                 RDEBUG("Client requested MN-HA key: Should use SPI to look up key from storage.");
512                 if (!mn_nai) {
513                         RDEBUG("WARNING: MN-NAI was not found!");
514                 }
515
516                 /*
517                  *      WiMAX-RRQ-HA-IP
518                  */
519                 if (!pairfind(request->packet->vps, 18, VENDORPEC_WIMAX)) {
520                         RDEBUG("WARNING: HA-IP was not found!");
521                 }
522
523
524                 /*
525                  *      WiMAX-HA-RK-Key-Requested
526                  */
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.");
530                 }
531         }
532
533         /*
534          *      Wipe the context of all sensitive information.
535          */
536         HMAC_CTX_cleanup(&hmac);
537
538         return RLM_MODULE_UPDATED;
539 }
540
541
542 /*
543  *      The module name should be the only globally exported symbol.
544  *      That is, everything else should be 'static'.
545  *
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.
550  */
551 module_t rlm_wimax = {
552         RLM_MODULE_INIT,
553         "wimax",
554         RLM_TYPE_THREAD_SAFE,           /* type */
555         wimax_instantiate,              /* instantiation */
556         wimax_detach,                   /* detach */
557         {
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 */
566         },
567 };