LCOV - code coverage report
Current view: top level - src/eap_common - eap_fast_common.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1422976643 Lines: 98 149 65.8 %
Date: 2015-02-03 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /*
       2             :  * EAP-FAST common helper functions (RFC 4851)
       3             :  * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "includes.h"
      10             : 
      11             : #include "common.h"
      12             : #include "crypto/sha1.h"
      13             : #include "crypto/tls.h"
      14             : #include "eap_defs.h"
      15             : #include "eap_tlv_common.h"
      16             : #include "eap_fast_common.h"
      17             : 
      18             : 
      19         210 : void eap_fast_put_tlv_hdr(struct wpabuf *buf, u16 type, u16 len)
      20             : {
      21             :         struct pac_tlv_hdr hdr;
      22         210 :         hdr.type = host_to_be16(type);
      23         210 :         hdr.len = host_to_be16(len);
      24         210 :         wpabuf_put_data(buf, &hdr, sizeof(hdr));
      25         210 : }
      26             : 
      27             : 
      28          78 : void eap_fast_put_tlv(struct wpabuf *buf, u16 type, const void *data,
      29             :                              u16 len)
      30             : {
      31          78 :         eap_fast_put_tlv_hdr(buf, type, len);
      32          78 :         wpabuf_put_data(buf, data, len);
      33          78 : }
      34             : 
      35             : 
      36         110 : void eap_fast_put_tlv_buf(struct wpabuf *buf, u16 type,
      37             :                                  const struct wpabuf *data)
      38             : {
      39         110 :         eap_fast_put_tlv_hdr(buf, type, wpabuf_len(data));
      40         110 :         wpabuf_put_buf(buf, data);
      41         110 : }
      42             : 
      43             : 
      44         110 : struct wpabuf * eap_fast_tlv_eap_payload(struct wpabuf *buf)
      45             : {
      46             :         struct wpabuf *e;
      47             : 
      48         110 :         if (buf == NULL)
      49           0 :                 return NULL;
      50             : 
      51             :         /* Encapsulate EAP packet in EAP-Payload TLV */
      52         110 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Add EAP-Payload TLV");
      53         110 :         e = wpabuf_alloc(sizeof(struct pac_tlv_hdr) + wpabuf_len(buf));
      54         110 :         if (e == NULL) {
      55           0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to allocate memory "
      56             :                            "for TLV encapsulation");
      57           0 :                 wpabuf_free(buf);
      58           0 :                 return NULL;
      59             :         }
      60         110 :         eap_fast_put_tlv_buf(e,
      61             :                              EAP_TLV_TYPE_MANDATORY | EAP_TLV_EAP_PAYLOAD_TLV,
      62             :                              buf);
      63         110 :         wpabuf_free(buf);
      64         110 :         return e;
      65             : }
      66             : 
      67             : 
      68          20 : void eap_fast_derive_master_secret(const u8 *pac_key, const u8 *server_random,
      69             :                                    const u8 *client_random, u8 *master_secret)
      70             : {
      71             : #define TLS_RANDOM_LEN 32
      72             : #define TLS_MASTER_SECRET_LEN 48
      73             :         u8 seed[2 * TLS_RANDOM_LEN];
      74             : 
      75          20 :         wpa_hexdump(MSG_DEBUG, "EAP-FAST: client_random",
      76             :                     client_random, TLS_RANDOM_LEN);
      77          20 :         wpa_hexdump(MSG_DEBUG, "EAP-FAST: server_random",
      78             :                     server_random, TLS_RANDOM_LEN);
      79             : 
      80             :         /*
      81             :          * RFC 4851, Section 5.1:
      82             :          * master_secret = T-PRF(PAC-Key, "PAC to master secret label hash", 
      83             :          *                       server_random + client_random, 48)
      84             :          */
      85          20 :         os_memcpy(seed, server_random, TLS_RANDOM_LEN);
      86          20 :         os_memcpy(seed + TLS_RANDOM_LEN, client_random, TLS_RANDOM_LEN);
      87          20 :         sha1_t_prf(pac_key, EAP_FAST_PAC_KEY_LEN,
      88             :                    "PAC to master secret label hash",
      89             :                    seed, sizeof(seed), master_secret, TLS_MASTER_SECRET_LEN);
      90             : 
      91          20 :         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: master_secret",
      92             :                         master_secret, TLS_MASTER_SECRET_LEN);
      93          20 : }
      94             : 
      95             : 
      96          42 : u8 * eap_fast_derive_key(void *ssl_ctx, struct tls_connection *conn,
      97             :                          const char *label, size_t len)
      98             : {
      99             :         struct tls_keys keys;
     100          42 :         u8 *rnd = NULL, *out;
     101             :         int block_size;
     102             : 
     103          42 :         block_size = tls_connection_get_keyblock_size(ssl_ctx, conn);
     104          42 :         if (block_size < 0)
     105           0 :                 return NULL;
     106             : 
     107          42 :         out = os_malloc(block_size + len);
     108          42 :         if (out == NULL)
     109           0 :                 return NULL;
     110             : 
     111          42 :         if (tls_connection_prf(ssl_ctx, conn, label, 1, out, block_size + len)
     112             :             == 0) {
     113           0 :                 os_memmove(out, out + block_size, len);
     114           0 :                 return out;
     115             :         }
     116             : 
     117          42 :         if (tls_connection_get_keys(ssl_ctx, conn, &keys))
     118           0 :                 goto fail;
     119             : 
     120          42 :         rnd = os_malloc(keys.client_random_len + keys.server_random_len);
     121          42 :         if (rnd == NULL)
     122           0 :                 goto fail;
     123             : 
     124          42 :         os_memcpy(rnd, keys.server_random, keys.server_random_len);
     125          42 :         os_memcpy(rnd + keys.server_random_len, keys.client_random,
     126             :                   keys.client_random_len);
     127             : 
     128          84 :         wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: master_secret for key "
     129          42 :                         "expansion", keys.master_key, keys.master_key_len);
     130          84 :         if (tls_prf_sha1_md5(keys.master_key, keys.master_key_len,
     131          42 :                              label, rnd, keys.client_random_len +
     132          42 :                              keys.server_random_len, out, block_size + len))
     133           0 :                 goto fail;
     134          42 :         os_free(rnd);
     135          42 :         os_memmove(out, out + block_size, len);
     136          42 :         return out;
     137             : 
     138             : fail:
     139           0 :         os_free(rnd);
     140           0 :         os_free(out);
     141           0 :         return NULL;
     142             : }
     143             : 
     144             : 
     145          33 : void eap_fast_derive_eap_msk(const u8 *simck, u8 *msk)
     146             : {
     147             :         /*
     148             :          * RFC 4851, Section 5.4: EAP Master Session Key Generation
     149             :          * MSK = T-PRF(S-IMCK[j], "Session Key Generating Function", 64)
     150             :          */
     151             : 
     152          33 :         sha1_t_prf(simck, EAP_FAST_SIMCK_LEN,
     153             :                    "Session Key Generating Function", (u8 *) "", 0,
     154             :                    msk, EAP_FAST_KEY_LEN);
     155          33 :         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Derived key (MSK)",
     156             :                         msk, EAP_FAST_KEY_LEN);
     157          33 : }
     158             : 
     159             : 
     160          18 : void eap_fast_derive_eap_emsk(const u8 *simck, u8 *emsk)
     161             : {
     162             :         /*
     163             :          * RFC 4851, Section 5.4: EAP Master Session Key Genreration
     164             :          * EMSK = T-PRF(S-IMCK[j],
     165             :          *        "Extended Session Key Generating Function", 64)
     166             :          */
     167             : 
     168          18 :         sha1_t_prf(simck, EAP_FAST_SIMCK_LEN,
     169             :                    "Extended Session Key Generating Function", (u8 *) "", 0,
     170             :                    emsk, EAP_EMSK_LEN);
     171          18 :         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Derived key (EMSK)",
     172             :                         emsk, EAP_EMSK_LEN);
     173          18 : }
     174             : 
     175             : 
     176         257 : int eap_fast_parse_tlv(struct eap_fast_tlv_parse *tlv,
     177             :                        int tlv_type, u8 *pos, size_t len)
     178             : {
     179         257 :         switch (tlv_type) {
     180             :         case EAP_TLV_EAP_PAYLOAD_TLV:
     181         111 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: EAP-Payload TLV",
     182             :                             pos, len);
     183         111 :                 if (tlv->eap_payload_tlv) {
     184           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
     185             :                                    "EAP-Payload TLV in the message");
     186           0 :                         tlv->iresult = EAP_TLV_RESULT_FAILURE;
     187           0 :                         return -2;
     188             :                 }
     189         111 :                 tlv->eap_payload_tlv = pos;
     190         111 :                 tlv->eap_payload_tlv_len = len;
     191         111 :                 break;
     192             :         case EAP_TLV_RESULT_TLV:
     193          54 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Result TLV", pos, len);
     194          54 :                 if (tlv->result) {
     195           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
     196             :                                    "Result TLV in the message");
     197           0 :                         tlv->result = EAP_TLV_RESULT_FAILURE;
     198           0 :                         return -2;
     199             :                 }
     200          54 :                 if (len < 2) {
     201           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
     202             :                                    "Result TLV");
     203           0 :                         tlv->result = EAP_TLV_RESULT_FAILURE;
     204           0 :                         break;
     205             :                 }
     206          54 :                 tlv->result = WPA_GET_BE16(pos);
     207          54 :                 if (tlv->result != EAP_TLV_RESULT_SUCCESS &&
     208           0 :                     tlv->result != EAP_TLV_RESULT_FAILURE) {
     209           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Unknown Result %d",
     210             :                                    tlv->result);
     211           0 :                         tlv->result = EAP_TLV_RESULT_FAILURE;
     212             :                 }
     213          54 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Result: %s",
     214          54 :                            tlv->result == EAP_TLV_RESULT_SUCCESS ?
     215             :                            "Success" : "Failure");
     216          54 :                 break;
     217             :         case EAP_TLV_INTERMEDIATE_RESULT_TLV:
     218          12 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Intermediate Result TLV",
     219             :                             pos, len);
     220          12 :                 if (len < 2) {
     221           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
     222             :                                    "Intermediate-Result TLV");
     223           0 :                         tlv->iresult = EAP_TLV_RESULT_FAILURE;
     224           0 :                         break;
     225             :                 }
     226          12 :                 if (tlv->iresult) {
     227           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
     228             :                                    "Intermediate-Result TLV in the message");
     229           0 :                         tlv->iresult = EAP_TLV_RESULT_FAILURE;
     230           0 :                         return -2;
     231             :                 }
     232          12 :                 tlv->iresult = WPA_GET_BE16(pos);
     233          12 :                 if (tlv->iresult != EAP_TLV_RESULT_SUCCESS &&
     234           0 :                     tlv->iresult != EAP_TLV_RESULT_FAILURE) {
     235           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Unknown Intermediate "
     236             :                                    "Result %d", tlv->iresult);
     237           0 :                         tlv->iresult = EAP_TLV_RESULT_FAILURE;
     238             :                 }
     239          12 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Intermediate Result: %s",
     240          12 :                            tlv->iresult == EAP_TLV_RESULT_SUCCESS ?
     241             :                            "Success" : "Failure");
     242          12 :                 break;
     243             :         case EAP_TLV_CRYPTO_BINDING_TLV:
     244          42 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV",
     245             :                             pos, len);
     246          42 :                 if (tlv->crypto_binding) {
     247           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
     248             :                                    "Crypto-Binding TLV in the message");
     249           0 :                         tlv->iresult = EAP_TLV_RESULT_FAILURE;
     250           0 :                         return -2;
     251             :                 }
     252          42 :                 tlv->crypto_binding_len = sizeof(struct eap_tlv_hdr) + len;
     253          42 :                 if (tlv->crypto_binding_len < sizeof(*tlv->crypto_binding)) {
     254           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
     255             :                                    "Crypto-Binding TLV");
     256           0 :                         tlv->iresult = EAP_TLV_RESULT_FAILURE;
     257           0 :                         return -2;
     258             :                 }
     259          42 :                 tlv->crypto_binding = (struct eap_tlv_crypto_binding_tlv *)
     260             :                         (pos - sizeof(struct eap_tlv_hdr));
     261          42 :                 break;
     262             :         case EAP_TLV_REQUEST_ACTION_TLV:
     263           8 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Request-Action TLV",
     264             :                             pos, len);
     265           8 :                 if (tlv->request_action) {
     266           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
     267             :                                    "Request-Action TLV in the message");
     268           0 :                         tlv->iresult = EAP_TLV_RESULT_FAILURE;
     269           0 :                         return -2;
     270             :                 }
     271           8 :                 if (len < 2) {
     272           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Too short "
     273             :                                    "Request-Action TLV");
     274           0 :                         tlv->iresult = EAP_TLV_RESULT_FAILURE;
     275           0 :                         break;
     276             :                 }
     277           8 :                 tlv->request_action = WPA_GET_BE16(pos);
     278           8 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Request-Action: %d",
     279             :                            tlv->request_action);
     280           8 :                 break;
     281             :         case EAP_TLV_PAC_TLV:
     282          30 :                 wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: PAC TLV", pos, len);
     283          30 :                 if (tlv->pac) {
     284           0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: More than one "
     285             :                                    "PAC TLV in the message");
     286           0 :                         tlv->iresult = EAP_TLV_RESULT_FAILURE;
     287           0 :                         return -2;
     288             :                 }
     289          30 :                 tlv->pac = pos;
     290          30 :                 tlv->pac_len = len;
     291          30 :                 break;
     292             :         default:
     293             :                 /* Unknown TLV */
     294           0 :                 return -1;
     295             :         }
     296             : 
     297         257 :         return 0;
     298             : }

Generated by: LCOV version 1.10