LCOV - code coverage report
Current view: top level - src/eap_server - eap_server_fast.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 560 784 71.4 %
Date: 2014-03-02 Functions: 34 36 94.4 %
Branches: 224 373 60.1 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * EAP-FAST server (RFC 4851)
       3                 :            :  * Copyright (c) 2004-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/aes_wrap.h"
      13                 :            : #include "crypto/sha1.h"
      14                 :            : #include "crypto/tls.h"
      15                 :            : #include "crypto/random.h"
      16                 :            : #include "eap_common/eap_tlv_common.h"
      17                 :            : #include "eap_common/eap_fast_common.h"
      18                 :            : #include "eap_i.h"
      19                 :            : #include "eap_tls_common.h"
      20                 :            : 
      21                 :            : 
      22                 :            : static void eap_fast_reset(struct eap_sm *sm, void *priv);
      23                 :            : 
      24                 :            : 
      25                 :            : /* Private PAC-Opaque TLV types */
      26                 :            : #define PAC_OPAQUE_TYPE_PAD 0
      27                 :            : #define PAC_OPAQUE_TYPE_KEY 1
      28                 :            : #define PAC_OPAQUE_TYPE_LIFETIME 2
      29                 :            : #define PAC_OPAQUE_TYPE_IDENTITY 3
      30                 :            : 
      31                 :            : struct eap_fast_data {
      32                 :            :         struct eap_ssl_data ssl;
      33                 :            :         enum {
      34                 :            :                 START, PHASE1, PHASE2_START, PHASE2_ID, PHASE2_METHOD,
      35                 :            :                 CRYPTO_BINDING, REQUEST_PAC, SUCCESS, FAILURE
      36                 :            :         } state;
      37                 :            : 
      38                 :            :         int fast_version;
      39                 :            :         const struct eap_method *phase2_method;
      40                 :            :         void *phase2_priv;
      41                 :            :         int force_version;
      42                 :            :         int peer_version;
      43                 :            : 
      44                 :            :         u8 crypto_binding_nonce[32];
      45                 :            :         int final_result;
      46                 :            : 
      47                 :            :         struct eap_fast_key_block_provisioning *key_block_p;
      48                 :            : 
      49                 :            :         u8 simck[EAP_FAST_SIMCK_LEN];
      50                 :            :         u8 cmk[EAP_FAST_CMK_LEN];
      51                 :            :         int simck_idx;
      52                 :            : 
      53                 :            :         u8 pac_opaque_encr[16];
      54                 :            :         u8 *srv_id;
      55                 :            :         size_t srv_id_len;
      56                 :            :         char *srv_id_info;
      57                 :            : 
      58                 :            :         int anon_provisioning;
      59                 :            :         int send_new_pac; /* server triggered re-keying of Tunnel PAC */
      60                 :            :         struct wpabuf *pending_phase2_resp;
      61                 :            :         u8 *identity; /* from PAC-Opaque */
      62                 :            :         size_t identity_len;
      63                 :            :         int eap_seq;
      64                 :            :         int tnc_started;
      65                 :            : 
      66                 :            :         int pac_key_lifetime;
      67                 :            :         int pac_key_refresh_time;
      68                 :            : };
      69                 :            : 
      70                 :            : 
      71                 :            : static int eap_fast_process_phase2_start(struct eap_sm *sm,
      72                 :            :                                          struct eap_fast_data *data);
      73                 :            : 
      74                 :            : 
      75                 :         86 : static const char * eap_fast_state_txt(int state)
      76                 :            : {
      77   [ +  +  +  +  :         86 :         switch (state) {
          +  +  +  +  +  
                      - ]
      78                 :            :         case START:
      79                 :          7 :                 return "START";
      80                 :            :         case PHASE1:
      81                 :         14 :                 return "PHASE1";
      82                 :            :         case PHASE2_START:
      83                 :         14 :                 return "PHASE2_START";
      84                 :            :         case PHASE2_ID:
      85                 :          8 :                 return "PHASE2_ID";
      86                 :            :         case PHASE2_METHOD:
      87                 :         14 :                 return "PHASE2_METHOD";
      88                 :            :         case CRYPTO_BINDING:
      89                 :         14 :                 return "CRYPTO_BINDING";
      90                 :            :         case REQUEST_PAC:
      91                 :          8 :                 return "REQUEST_PAC";
      92                 :            :         case SUCCESS:
      93                 :          6 :                 return "SUCCESS";
      94                 :            :         case FAILURE:
      95                 :          1 :                 return "FAILURE";
      96                 :            :         default:
      97                 :         86 :                 return "Unknown?!";
      98                 :            :         }
      99                 :            : }
     100                 :            : 
     101                 :            : 
     102                 :         43 : static void eap_fast_state(struct eap_fast_data *data, int state)
     103                 :            : {
     104                 :         43 :         wpa_printf(MSG_DEBUG, "EAP-FAST: %s -> %s",
     105                 :         43 :                    eap_fast_state_txt(data->state),
     106                 :            :                    eap_fast_state_txt(state));
     107                 :         43 :         data->state = state;
     108                 :         43 : }
     109                 :            : 
     110                 :            : 
     111                 :          0 : static EapType eap_fast_req_failure(struct eap_sm *sm,
     112                 :            :                                     struct eap_fast_data *data)
     113                 :            : {
     114                 :            :         /* TODO: send Result TLV(FAILURE) */
     115                 :          0 :         eap_fast_state(data, FAILURE);
     116                 :          0 :         return EAP_TYPE_NONE;
     117                 :            : }
     118                 :            : 
     119                 :            : 
     120                 :          7 : static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
     121                 :            :                                       const u8 *client_random,
     122                 :            :                                       const u8 *server_random,
     123                 :            :                                       u8 *master_secret)
     124                 :            : {
     125                 :          7 :         struct eap_fast_data *data = ctx;
     126                 :            :         const u8 *pac_opaque;
     127                 :            :         size_t pac_opaque_len;
     128                 :          7 :         u8 *buf, *pos, *end, *pac_key = NULL;
     129                 :          7 :         os_time_t lifetime = 0;
     130                 :            :         struct os_time now;
     131                 :          7 :         u8 *identity = NULL;
     132                 :          7 :         size_t identity_len = 0;
     133                 :            : 
     134                 :          7 :         wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
     135                 :          7 :         wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket (PAC-Opaque)",
     136                 :            :                     ticket, len);
     137                 :            : 
     138 [ -  + ][ +  + ]:          7 :         if (len < 4 || WPA_GET_BE16(ticket) != PAC_TYPE_PAC_OPAQUE) {
     139                 :          4 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid "
     140                 :            :                            "SessionTicket");
     141                 :          4 :                 return 0;
     142                 :            :         }
     143                 :            : 
     144                 :          3 :         pac_opaque_len = WPA_GET_BE16(ticket + 2);
     145                 :          3 :         pac_opaque = ticket + 4;
     146 [ +  - ][ -  + ]:          3 :         if (pac_opaque_len < 8 || pac_opaque_len % 8 ||
                 [ +  - ]
     147                 :          3 :             pac_opaque_len > len - 4) {
     148                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid PAC-Opaque "
     149                 :            :                            "(len=%lu left=%lu)",
     150                 :            :                            (unsigned long) pac_opaque_len,
     151                 :            :                            (unsigned long) len);
     152                 :          0 :                 return 0;
     153                 :            :         }
     154                 :          3 :         wpa_hexdump(MSG_DEBUG, "EAP-FAST: Received PAC-Opaque",
     155                 :            :                     pac_opaque, pac_opaque_len);
     156                 :            : 
     157                 :          3 :         buf = os_malloc(pac_opaque_len - 8);
     158         [ -  + ]:          3 :         if (buf == NULL) {
     159                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to allocate memory "
     160                 :            :                            "for decrypting PAC-Opaque");
     161                 :          0 :                 return 0;
     162                 :            :         }
     163                 :            : 
     164         [ -  + ]:          3 :         if (aes_unwrap(data->pac_opaque_encr, (pac_opaque_len - 8) / 8,
     165                 :            :                        pac_opaque, buf) < 0) {
     166                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to decrypt "
     167                 :            :                            "PAC-Opaque");
     168                 :          0 :                 os_free(buf);
     169                 :            :                 /*
     170                 :            :                  * This may have been caused by server changing the PAC-Opaque
     171                 :            :                  * encryption key, so just ignore this PAC-Opaque instead of
     172                 :            :                  * failing the authentication completely. Provisioning can now
     173                 :            :                  * be used to provision a new PAC.
     174                 :            :                  */
     175                 :          0 :                 return 0;
     176                 :            :         }
     177                 :            : 
     178                 :          3 :         end = buf + pac_opaque_len - 8;
     179                 :          3 :         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Decrypted PAC-Opaque",
     180                 :          3 :                         buf, end - buf);
     181                 :            : 
     182                 :          3 :         pos = buf;
     183         [ +  + ]:         15 :         while (pos + 1 < end) {
     184         [ -  + ]:         12 :                 if (pos + 2 + pos[1] > end)
     185                 :          0 :                         break;
     186                 :            : 
     187   [ +  +  +  +  :         12 :                 switch (*pos) {
                      - ]
     188                 :            :                 case PAC_OPAQUE_TYPE_PAD:
     189                 :          3 :                         pos = end;
     190                 :          3 :                         break;
     191                 :            :                 case PAC_OPAQUE_TYPE_KEY:
     192         [ -  + ]:          3 :                         if (pos[1] != EAP_FAST_PAC_KEY_LEN) {
     193                 :          0 :                                 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid "
     194                 :          0 :                                            "PAC-Key length %d", pos[1]);
     195                 :          0 :                                 os_free(buf);
     196                 :          0 :                                 return -1;
     197                 :            :                         }
     198                 :          3 :                         pac_key = pos + 2;
     199                 :          3 :                         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key from "
     200                 :            :                                         "decrypted PAC-Opaque",
     201                 :            :                                         pac_key, EAP_FAST_PAC_KEY_LEN);
     202                 :          3 :                         break;
     203                 :            :                 case PAC_OPAQUE_TYPE_LIFETIME:
     204         [ -  + ]:          3 :                         if (pos[1] != 4) {
     205                 :          0 :                                 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid "
     206                 :            :                                            "PAC-Key lifetime length %d",
     207                 :          0 :                                            pos[1]);
     208                 :          0 :                                 os_free(buf);
     209                 :          0 :                                 return -1;
     210                 :            :                         }
     211                 :          3 :                         lifetime = WPA_GET_BE32(pos + 2);
     212                 :          3 :                         break;
     213                 :            :                 case PAC_OPAQUE_TYPE_IDENTITY:
     214                 :          3 :                         identity = pos + 2;
     215                 :          3 :                         identity_len = pos[1];
     216                 :          3 :                         break;
     217                 :            :                 }
     218                 :            : 
     219                 :         12 :                 pos += 2 + pos[1];
     220                 :            :         }
     221                 :            : 
     222         [ -  + ]:          3 :         if (pac_key == NULL) {
     223                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key included in "
     224                 :            :                            "PAC-Opaque");
     225                 :          0 :                 os_free(buf);
     226                 :          0 :                 return -1;
     227                 :            :         }
     228                 :            : 
     229         [ +  - ]:          3 :         if (identity) {
     230                 :          3 :                 wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Identity from "
     231                 :            :                                   "PAC-Opaque", identity, identity_len);
     232                 :          3 :                 os_free(data->identity);
     233                 :          3 :                 data->identity = os_malloc(identity_len);
     234         [ +  - ]:          3 :                 if (data->identity) {
     235                 :          3 :                         os_memcpy(data->identity, identity, identity_len);
     236                 :          3 :                         data->identity_len = identity_len;
     237                 :            :                 }
     238                 :            :         }
     239                 :            : 
     240 [ +  - ][ +  - ]:          3 :         if (os_get_time(&now) < 0 || lifetime <= 0 || now.sec > lifetime) {
                 [ -  + ]
     241                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Key not valid anymore "
     242                 :            :                            "(lifetime=%ld now=%ld)", lifetime, now.sec);
     243                 :          0 :                 data->send_new_pac = 2;
     244                 :            :                 /*
     245                 :            :                  * Allow PAC to be used to allow a PAC update with some level
     246                 :            :                  * of server authentication (i.e., do not fall back to full TLS
     247                 :            :                  * handshake since we cannot be sure that the peer would be
     248                 :            :                  * able to validate server certificate now). However, reject
     249                 :            :                  * the authentication since the PAC was not valid anymore. Peer
     250                 :            :                  * can connect again with the newly provisioned PAC after this.
     251                 :            :                  */
     252         [ -  + ]:          3 :         } else if (lifetime - now.sec < data->pac_key_refresh_time) {
     253                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Key soft timeout; send "
     254                 :            :                            "an update if authentication succeeds");
     255                 :          0 :                 data->send_new_pac = 1;
     256                 :            :         }
     257                 :            : 
     258                 :          3 :         eap_fast_derive_master_secret(pac_key, server_random, client_random,
     259                 :            :                                       master_secret);
     260                 :            : 
     261                 :          3 :         os_free(buf);
     262                 :            : 
     263                 :          7 :         return 1;
     264                 :            : }
     265                 :            : 
     266                 :            : 
     267                 :          6 : static void eap_fast_derive_key_auth(struct eap_sm *sm,
     268                 :            :                                      struct eap_fast_data *data)
     269                 :            : {
     270                 :            :         u8 *sks;
     271                 :            : 
     272                 :            :         /* RFC 4851, Section 5.1:
     273                 :            :          * Extra key material after TLS key_block: session_key_seed[40]
     274                 :            :          */
     275                 :            : 
     276                 :          6 :         sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
     277                 :            :                                   EAP_FAST_SKS_LEN);
     278         [ -  + ]:          6 :         if (sks == NULL) {
     279                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
     280                 :            :                            "session_key_seed");
     281                 :          6 :                 return;
     282                 :            :         }
     283                 :            : 
     284                 :            :         /*
     285                 :            :          * RFC 4851, Section 5.2:
     286                 :            :          * S-IMCK[0] = session_key_seed
     287                 :            :          */
     288                 :          6 :         wpa_hexdump_key(MSG_DEBUG,
     289                 :            :                         "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
     290                 :            :                         sks, EAP_FAST_SKS_LEN);
     291                 :          6 :         data->simck_idx = 0;
     292                 :          6 :         os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
     293                 :          6 :         os_free(sks);
     294                 :            : }
     295                 :            : 
     296                 :            : 
     297                 :          1 : static void eap_fast_derive_key_provisioning(struct eap_sm *sm,
     298                 :            :                                              struct eap_fast_data *data)
     299                 :            : {
     300                 :          1 :         os_free(data->key_block_p);
     301                 :          1 :         data->key_block_p = (struct eap_fast_key_block_provisioning *)
     302                 :          1 :                 eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
     303                 :            :                                     "key expansion",
     304                 :            :                                     sizeof(*data->key_block_p));
     305         [ -  + ]:          1 :         if (data->key_block_p == NULL) {
     306                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
     307                 :          1 :                 return;
     308                 :            :         }
     309                 :            :         /*
     310                 :            :          * RFC 4851, Section 5.2:
     311                 :            :          * S-IMCK[0] = session_key_seed
     312                 :            :          */
     313                 :          1 :         wpa_hexdump_key(MSG_DEBUG,
     314                 :            :                         "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
     315                 :          1 :                         data->key_block_p->session_key_seed,
     316                 :            :                         sizeof(data->key_block_p->session_key_seed));
     317                 :          1 :         data->simck_idx = 0;
     318                 :          1 :         os_memcpy(data->simck, data->key_block_p->session_key_seed,
     319                 :            :                   EAP_FAST_SIMCK_LEN);
     320                 :          1 :         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
     321                 :          1 :                         data->key_block_p->server_challenge,
     322                 :            :                         sizeof(data->key_block_p->server_challenge));
     323                 :          1 :         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
     324                 :          1 :                         data->key_block_p->client_challenge,
     325                 :            :                         sizeof(data->key_block_p->client_challenge));
     326                 :            : }
     327                 :            : 
     328                 :            : 
     329                 :          7 : static int eap_fast_get_phase2_key(struct eap_sm *sm,
     330                 :            :                                    struct eap_fast_data *data,
     331                 :            :                                    u8 *isk, size_t isk_len)
     332                 :            : {
     333                 :            :         u8 *key;
     334                 :            :         size_t key_len;
     335                 :            : 
     336                 :          7 :         os_memset(isk, 0, isk_len);
     337                 :            : 
     338 [ +  - ][ -  + ]:          7 :         if (data->phase2_method == NULL || data->phase2_priv == NULL) {
     339                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
     340                 :            :                            "available");
     341                 :          0 :                 return -1;
     342                 :            :         }
     343                 :            : 
     344         [ +  + ]:          7 :         if (data->phase2_method->getKey == NULL)
     345                 :          3 :                 return 0;
     346                 :            : 
     347         [ -  + ]:          4 :         if ((key = data->phase2_method->getKey(sm, data->phase2_priv,
     348                 :            :                                                &key_len)) == NULL) {
     349                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
     350                 :            :                            "from Phase 2");
     351                 :          0 :                 return -1;
     352                 :            :         }
     353                 :            : 
     354         [ -  + ]:          4 :         if (key_len > isk_len)
     355                 :          0 :                 key_len = isk_len;
     356 [ +  - ][ +  - ]:          4 :         if (key_len == 32 &&
     357         [ +  - ]:          4 :             data->phase2_method->vendor == EAP_VENDOR_IETF &&
     358                 :          4 :             data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
     359                 :            :                 /*
     360                 :            :                  * EAP-FAST uses reverse order for MS-MPPE keys when deriving
     361                 :            :                  * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
     362                 :            :                  * ISK for EAP-FAST cryptobinding.
     363                 :            :                  */
     364                 :          4 :                 os_memcpy(isk, key + 16, 16);
     365                 :          4 :                 os_memcpy(isk + 16, key, 16);
     366                 :            :         } else
     367                 :          0 :                 os_memcpy(isk, key, key_len);
     368                 :          4 :         os_free(key);
     369                 :            : 
     370                 :          7 :         return 0;
     371                 :            : }
     372                 :            : 
     373                 :            : 
     374                 :          7 : static int eap_fast_update_icmk(struct eap_sm *sm, struct eap_fast_data *data)
     375                 :            : {
     376                 :            :         u8 isk[32], imck[60];
     377                 :            : 
     378                 :          7 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Deriving ICMK[%d] (S-IMCK and CMK)",
     379                 :          7 :                    data->simck_idx + 1);
     380                 :            : 
     381                 :            :         /*
     382                 :            :          * RFC 4851, Section 5.2:
     383                 :            :          * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
     384                 :            :          *                 MSK[j], 60)
     385                 :            :          * S-IMCK[j] = first 40 octets of IMCK[j]
     386                 :            :          * CMK[j] = last 20 octets of IMCK[j]
     387                 :            :          */
     388                 :            : 
     389         [ -  + ]:          7 :         if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
     390                 :          0 :                 return -1;
     391                 :          7 :         wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
     392                 :          7 :         sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
     393                 :            :                    "Inner Methods Compound Keys",
     394                 :            :                    isk, sizeof(isk), imck, sizeof(imck));
     395                 :          7 :         data->simck_idx++;
     396                 :          7 :         os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
     397                 :          7 :         wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
     398                 :          7 :                         data->simck, EAP_FAST_SIMCK_LEN);
     399                 :          7 :         os_memcpy(data->cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
     400                 :          7 :         wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
     401                 :          7 :                         data->cmk, EAP_FAST_CMK_LEN);
     402                 :            : 
     403                 :          7 :         return 0;
     404                 :            : }
     405                 :            : 
     406                 :            : 
     407                 :          7 : static void * eap_fast_init(struct eap_sm *sm)
     408                 :            : {
     409                 :            :         struct eap_fast_data *data;
     410                 :          7 :         u8 ciphers[5] = {
     411                 :            :                 TLS_CIPHER_ANON_DH_AES128_SHA,
     412                 :            :                 TLS_CIPHER_AES128_SHA,
     413                 :            :                 TLS_CIPHER_RSA_DHE_AES128_SHA,
     414                 :            :                 TLS_CIPHER_RC4_SHA,
     415                 :            :                 TLS_CIPHER_NONE
     416                 :            :         };
     417                 :            : 
     418                 :          7 :         data = os_zalloc(sizeof(*data));
     419         [ -  + ]:          7 :         if (data == NULL)
     420                 :          0 :                 return NULL;
     421                 :          7 :         data->fast_version = EAP_FAST_VERSION;
     422                 :          7 :         data->force_version = -1;
     423 [ +  - ][ -  + ]:          7 :         if (sm->user && sm->user->force_version >= 0) {
     424                 :          0 :                 data->force_version = sm->user->force_version;
     425                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: forcing version %d",
     426                 :            :                            data->force_version);
     427                 :          0 :                 data->fast_version = data->force_version;
     428                 :            :         }
     429                 :          7 :         data->state = START;
     430                 :            : 
     431         [ -  + ]:          7 :         if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
     432                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
     433                 :          0 :                 eap_fast_reset(sm, data);
     434                 :          0 :                 return NULL;
     435                 :            :         }
     436                 :            : 
     437         [ -  + ]:          7 :         if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
     438                 :            :                                            ciphers) < 0) {
     439                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set TLS cipher "
     440                 :            :                            "suites");
     441                 :          0 :                 eap_fast_reset(sm, data);
     442                 :          0 :                 return NULL;
     443                 :            :         }
     444                 :            : 
     445         [ -  + ]:          7 :         if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
     446                 :            :                                                  eap_fast_session_ticket_cb,
     447                 :            :                                                  data) < 0) {
     448                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
     449                 :            :                            "callback");
     450                 :          0 :                 eap_fast_reset(sm, data);
     451                 :          0 :                 return NULL;
     452                 :            :         }
     453                 :            : 
     454         [ -  + ]:          7 :         if (sm->pac_opaque_encr_key == NULL) {
     455                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: No PAC-Opaque encryption key "
     456                 :            :                            "configured");
     457                 :          0 :                 eap_fast_reset(sm, data);
     458                 :          0 :                 return NULL;
     459                 :            :         }
     460                 :          7 :         os_memcpy(data->pac_opaque_encr, sm->pac_opaque_encr_key,
     461                 :            :                   sizeof(data->pac_opaque_encr));
     462                 :            : 
     463         [ -  + ]:          7 :         if (sm->eap_fast_a_id == NULL) {
     464                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: No A-ID configured");
     465                 :          0 :                 eap_fast_reset(sm, data);
     466                 :          0 :                 return NULL;
     467                 :            :         }
     468                 :          7 :         data->srv_id = os_malloc(sm->eap_fast_a_id_len);
     469         [ -  + ]:          7 :         if (data->srv_id == NULL) {
     470                 :          0 :                 eap_fast_reset(sm, data);
     471                 :          0 :                 return NULL;
     472                 :            :         }
     473                 :          7 :         os_memcpy(data->srv_id, sm->eap_fast_a_id, sm->eap_fast_a_id_len);
     474                 :          7 :         data->srv_id_len = sm->eap_fast_a_id_len;
     475                 :            : 
     476         [ -  + ]:          7 :         if (sm->eap_fast_a_id_info == NULL) {
     477                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: No A-ID-Info configured");
     478                 :          0 :                 eap_fast_reset(sm, data);
     479                 :          0 :                 return NULL;
     480                 :            :         }
     481                 :          7 :         data->srv_id_info = os_strdup(sm->eap_fast_a_id_info);
     482         [ -  + ]:          7 :         if (data->srv_id_info == NULL) {
     483                 :          0 :                 eap_fast_reset(sm, data);
     484                 :          0 :                 return NULL;
     485                 :            :         }
     486                 :            : 
     487                 :            :         /* PAC-Key lifetime in seconds (hard limit) */
     488                 :          7 :         data->pac_key_lifetime = sm->pac_key_lifetime;
     489                 :            : 
     490                 :            :         /*
     491                 :            :          * PAC-Key refresh time in seconds (soft limit on remaining hard
     492                 :            :          * limit). The server will generate a new PAC-Key when this number of
     493                 :            :          * seconds (or fewer) of the lifetime remains.
     494                 :            :          */
     495                 :          7 :         data->pac_key_refresh_time = sm->pac_key_refresh_time;
     496                 :            : 
     497                 :          7 :         return data;
     498                 :            : }
     499                 :            : 
     500                 :            : 
     501                 :          7 : static void eap_fast_reset(struct eap_sm *sm, void *priv)
     502                 :            : {
     503                 :          7 :         struct eap_fast_data *data = priv;
     504         [ -  + ]:          7 :         if (data == NULL)
     505                 :          7 :                 return;
     506 [ -  + ][ #  # ]:          7 :         if (data->phase2_priv && data->phase2_method)
     507                 :          0 :                 data->phase2_method->reset(sm, data->phase2_priv);
     508                 :          7 :         eap_server_tls_ssl_deinit(sm, &data->ssl);
     509                 :          7 :         os_free(data->srv_id);
     510                 :          7 :         os_free(data->srv_id_info);
     511                 :          7 :         os_free(data->key_block_p);
     512                 :          7 :         wpabuf_free(data->pending_phase2_resp);
     513                 :          7 :         os_free(data->identity);
     514                 :          7 :         os_free(data);
     515                 :            : }
     516                 :            : 
     517                 :            : 
     518                 :          7 : static struct wpabuf * eap_fast_build_start(struct eap_sm *sm,
     519                 :            :                                             struct eap_fast_data *data, u8 id)
     520                 :            : {
     521                 :            :         struct wpabuf *req;
     522                 :            : 
     523                 :          7 :         req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_FAST,
     524                 :          7 :                             1 + sizeof(struct pac_tlv_hdr) + data->srv_id_len,
     525                 :            :                             EAP_CODE_REQUEST, id);
     526         [ -  + ]:          7 :         if (req == NULL) {
     527                 :          0 :                 wpa_printf(MSG_ERROR, "EAP-FAST: Failed to allocate memory for"
     528                 :            :                            " request");
     529                 :          0 :                 eap_fast_state(data, FAILURE);
     530                 :          0 :                 return NULL;
     531                 :            :         }
     532                 :            : 
     533                 :          7 :         wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->fast_version);
     534                 :            : 
     535                 :            :         /* RFC 4851, 4.1.1. Authority ID Data */
     536                 :          7 :         eap_fast_put_tlv(req, PAC_TYPE_A_ID, data->srv_id, data->srv_id_len);
     537                 :            : 
     538                 :          7 :         eap_fast_state(data, PHASE1);
     539                 :            : 
     540                 :          7 :         return req;
     541                 :            : }
     542                 :            : 
     543                 :            : 
     544                 :          7 : static int eap_fast_phase1_done(struct eap_sm *sm, struct eap_fast_data *data)
     545                 :            : {
     546                 :            :         char cipher[64];
     547                 :            : 
     548                 :          7 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Phase1 done, starting Phase2");
     549                 :            : 
     550         [ -  + ]:          7 :         if (tls_get_cipher(sm->ssl_ctx, data->ssl.conn, cipher, sizeof(cipher))
     551                 :            :             < 0) {
     552                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to get cipher "
     553                 :            :                            "information");
     554                 :          0 :                 eap_fast_state(data, FAILURE);
     555                 :          0 :                 return -1;
     556                 :            :         }
     557                 :          7 :         data->anon_provisioning = os_strstr(cipher, "ADH") != NULL;
     558                 :            :                     
     559         [ +  + ]:          7 :         if (data->anon_provisioning) {
     560                 :          1 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Anonymous provisioning");
     561                 :          1 :                 eap_fast_derive_key_provisioning(sm, data);
     562                 :            :         } else
     563                 :          6 :                 eap_fast_derive_key_auth(sm, data);
     564                 :            : 
     565                 :          7 :         eap_fast_state(data, PHASE2_START);
     566                 :            : 
     567                 :          7 :         return 0;
     568                 :            : }
     569                 :            : 
     570                 :            : 
     571                 :         18 : static struct wpabuf * eap_fast_build_phase2_req(struct eap_sm *sm,
     572                 :            :                                                  struct eap_fast_data *data,
     573                 :            :                                                  u8 id)
     574                 :            : {
     575                 :            :         struct wpabuf *req;
     576                 :            : 
     577         [ -  + ]:         18 :         if (data->phase2_priv == NULL) {
     578                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
     579                 :            :                            "initialized");
     580                 :          0 :                 return NULL;
     581                 :            :         }
     582                 :         18 :         req = data->phase2_method->buildReq(sm, data->phase2_priv, id);
     583         [ -  + ]:         18 :         if (req == NULL)
     584                 :          0 :                 return NULL;
     585                 :            : 
     586                 :         18 :         wpa_hexdump_buf_key(MSG_MSGDUMP, "EAP-FAST: Phase 2 EAP-Request", req);
     587                 :         18 :         return eap_fast_tlv_eap_payload(req);
     588                 :            : }
     589                 :            : 
     590                 :            : 
     591                 :          7 : static struct wpabuf * eap_fast_build_crypto_binding(
     592                 :            :         struct eap_sm *sm, struct eap_fast_data *data)
     593                 :            : {
     594                 :            :         struct wpabuf *buf;
     595                 :            :         struct eap_tlv_result_tlv *result;
     596                 :            :         struct eap_tlv_crypto_binding_tlv *binding;
     597                 :            : 
     598                 :          7 :         buf = wpabuf_alloc(2 * sizeof(*result) + sizeof(*binding));
     599         [ -  + ]:          7 :         if (buf == NULL)
     600                 :          0 :                 return NULL;
     601                 :            : 
     602 [ +  - ][ +  + ]:          7 :         if (data->send_new_pac || data->anon_provisioning ||
                 [ -  + ]
     603                 :          6 :             data->phase2_method)
     604                 :          1 :                 data->final_result = 0;
     605                 :            :         else
     606                 :          6 :                 data->final_result = 1;
     607                 :            : 
     608 [ +  + ][ -  + ]:          7 :         if (!data->final_result || data->eap_seq > 1) {
     609                 :            :                 /* Intermediate-Result */
     610                 :          1 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Intermediate-Result TLV "
     611                 :            :                            "(status=SUCCESS)");
     612                 :          1 :                 result = wpabuf_put(buf, sizeof(*result));
     613                 :          1 :                 result->tlv_type = host_to_be16(
     614                 :            :                         EAP_TLV_TYPE_MANDATORY |
     615                 :            :                         EAP_TLV_INTERMEDIATE_RESULT_TLV);
     616                 :          1 :                 result->length = host_to_be16(2);
     617                 :          1 :                 result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS);
     618                 :            :         }
     619                 :            : 
     620         [ +  + ]:          7 :         if (data->final_result) {
     621                 :            :                 /* Result TLV */
     622                 :          6 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV "
     623                 :            :                            "(status=SUCCESS)");
     624                 :          6 :                 result = wpabuf_put(buf, sizeof(*result));
     625                 :          6 :                 result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
     626                 :            :                                                 EAP_TLV_RESULT_TLV);
     627                 :          6 :                 result->length = host_to_be16(2);
     628                 :          6 :                 result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS);
     629                 :            :         }
     630                 :            : 
     631                 :            :         /* Crypto-Binding TLV */
     632                 :          7 :         binding = wpabuf_put(buf, sizeof(*binding));
     633                 :          7 :         binding->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
     634                 :            :                                          EAP_TLV_CRYPTO_BINDING_TLV);
     635                 :          7 :         binding->length = host_to_be16(sizeof(*binding) -
     636                 :            :                                        sizeof(struct eap_tlv_hdr));
     637                 :          7 :         binding->version = EAP_FAST_VERSION;
     638                 :          7 :         binding->received_version = data->peer_version;
     639                 :          7 :         binding->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST;
     640         [ -  + ]:          7 :         if (random_get_bytes(binding->nonce, sizeof(binding->nonce)) < 0) {
     641                 :          0 :                 wpabuf_free(buf);
     642                 :          0 :                 return NULL;
     643                 :            :         }
     644                 :            : 
     645                 :            :         /*
     646                 :            :          * RFC 4851, Section 4.2.8:
     647                 :            :          * The nonce in a request MUST have its least significant bit set to 0.
     648                 :            :          */
     649                 :          7 :         binding->nonce[sizeof(binding->nonce) - 1] &= ~0x01;
     650                 :            : 
     651                 :          7 :         os_memcpy(data->crypto_binding_nonce, binding->nonce,
     652                 :            :                   sizeof(binding->nonce));
     653                 :            : 
     654                 :            :         /*
     655                 :            :          * RFC 4851, Section 5.3:
     656                 :            :          * CMK = CMK[j]
     657                 :            :          * Compound-MAC = HMAC-SHA1( CMK, Crypto-Binding TLV )
     658                 :            :          */
     659                 :            : 
     660                 :          7 :         hmac_sha1(data->cmk, EAP_FAST_CMK_LEN,
     661                 :            :                   (u8 *) binding, sizeof(*binding),
     662                 :          7 :                   binding->compound_mac);
     663                 :            : 
     664                 :          7 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Add Crypto-Binding TLV: Version %d "
     665                 :            :                    "Received Version %d SubType %d",
     666                 :         14 :                    binding->version, binding->received_version,
     667                 :          7 :                    binding->subtype);
     668                 :          7 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
     669                 :          7 :                     binding->nonce, sizeof(binding->nonce));
     670                 :          7 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
     671                 :          7 :                     binding->compound_mac, sizeof(binding->compound_mac));
     672                 :            : 
     673                 :          7 :         return buf;
     674                 :            : }
     675                 :            : 
     676                 :            : 
     677                 :          4 : static struct wpabuf * eap_fast_build_pac(struct eap_sm *sm,
     678                 :            :                                           struct eap_fast_data *data)
     679                 :            : {
     680                 :            :         u8 pac_key[EAP_FAST_PAC_KEY_LEN];
     681                 :            :         u8 *pac_buf, *pac_opaque;
     682                 :            :         struct wpabuf *buf;
     683                 :            :         u8 *pos;
     684                 :            :         size_t buf_len, srv_id_info_len, pac_len;
     685                 :            :         struct eap_tlv_hdr *pac_tlv;
     686                 :            :         struct pac_tlv_hdr *pac_info;
     687                 :            :         struct eap_tlv_result_tlv *result;
     688                 :            :         struct os_time now;
     689                 :            : 
     690   [ +  -  -  + ]:          8 :         if (random_get_bytes(pac_key, EAP_FAST_PAC_KEY_LEN) < 0 ||
     691                 :          4 :             os_get_time(&now) < 0)
     692                 :          0 :                 return NULL;
     693                 :          4 :         wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Generated PAC-Key",
     694                 :            :                         pac_key, EAP_FAST_PAC_KEY_LEN);
     695                 :            : 
     696                 :          4 :         pac_len = (2 + EAP_FAST_PAC_KEY_LEN) + (2 + 4) +
     697                 :          4 :                 (2 + sm->identity_len) + 8;
     698                 :          4 :         pac_buf = os_malloc(pac_len);
     699         [ -  + ]:          4 :         if (pac_buf == NULL)
     700                 :          0 :                 return NULL;
     701                 :            : 
     702                 :          4 :         srv_id_info_len = os_strlen(data->srv_id_info);
     703                 :            : 
     704                 :          4 :         pos = pac_buf;
     705                 :          4 :         *pos++ = PAC_OPAQUE_TYPE_KEY;
     706                 :          4 :         *pos++ = EAP_FAST_PAC_KEY_LEN;
     707                 :          4 :         os_memcpy(pos, pac_key, EAP_FAST_PAC_KEY_LEN);
     708                 :          4 :         pos += EAP_FAST_PAC_KEY_LEN;
     709                 :            : 
     710                 :          4 :         *pos++ = PAC_OPAQUE_TYPE_LIFETIME;
     711                 :          4 :         *pos++ = 4;
     712                 :          4 :         WPA_PUT_BE32(pos, now.sec + data->pac_key_lifetime);
     713                 :          4 :         pos += 4;
     714                 :            : 
     715         [ +  - ]:          4 :         if (sm->identity) {
     716                 :          4 :                 *pos++ = PAC_OPAQUE_TYPE_IDENTITY;
     717                 :          4 :                 *pos++ = sm->identity_len;
     718                 :          4 :                 os_memcpy(pos, sm->identity, sm->identity_len);
     719                 :          4 :                 pos += sm->identity_len;
     720                 :            :         }
     721                 :            : 
     722                 :          4 :         pac_len = pos - pac_buf;
     723         [ +  + ]:         12 :         while (pac_len % 8) {
     724                 :          8 :                 *pos++ = PAC_OPAQUE_TYPE_PAD;
     725                 :          8 :                 pac_len++;
     726                 :            :         }
     727                 :            : 
     728                 :          4 :         pac_opaque = os_malloc(pac_len + 8);
     729         [ -  + ]:          4 :         if (pac_opaque == NULL) {
     730                 :          0 :                 os_free(pac_buf);
     731                 :          0 :                 return NULL;
     732                 :            :         }
     733         [ -  + ]:          4 :         if (aes_wrap(data->pac_opaque_encr, pac_len / 8, pac_buf,
     734                 :            :                      pac_opaque) < 0) {
     735                 :          0 :                 os_free(pac_buf);
     736                 :          0 :                 os_free(pac_opaque);
     737                 :          0 :                 return NULL;
     738                 :            :         }
     739                 :          4 :         os_free(pac_buf);
     740                 :            : 
     741                 :          4 :         pac_len += 8;
     742                 :          4 :         wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque",
     743                 :            :                     pac_opaque, pac_len);
     744                 :            : 
     745                 :          4 :         buf_len = sizeof(*pac_tlv) +
     746                 :            :                 sizeof(struct pac_tlv_hdr) + EAP_FAST_PAC_KEY_LEN +
     747                 :          4 :                 sizeof(struct pac_tlv_hdr) + pac_len +
     748                 :          8 :                 data->srv_id_len + srv_id_info_len + 100 + sizeof(*result);
     749                 :          4 :         buf = wpabuf_alloc(buf_len);
     750         [ -  + ]:          4 :         if (buf == NULL) {
     751                 :          0 :                 os_free(pac_opaque);
     752                 :          0 :                 return NULL;
     753                 :            :         }
     754                 :            : 
     755                 :            :         /* Result TLV */
     756                 :          4 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV (status=SUCCESS)");
     757                 :          4 :         result = wpabuf_put(buf, sizeof(*result));
     758                 :          4 :         WPA_PUT_BE16((u8 *) &result->tlv_type,
     759                 :            :                      EAP_TLV_TYPE_MANDATORY | EAP_TLV_RESULT_TLV);
     760                 :          4 :         WPA_PUT_BE16((u8 *) &result->length, 2);
     761                 :          4 :         WPA_PUT_BE16((u8 *) &result->status, EAP_TLV_RESULT_SUCCESS);
     762                 :            : 
     763                 :            :         /* PAC TLV */
     764                 :          4 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV");
     765                 :          4 :         pac_tlv = wpabuf_put(buf, sizeof(*pac_tlv));
     766                 :          4 :         pac_tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
     767                 :            :                                          EAP_TLV_PAC_TLV);
     768                 :            : 
     769                 :            :         /* PAC-Key */
     770                 :          4 :         eap_fast_put_tlv(buf, PAC_TYPE_PAC_KEY, pac_key, EAP_FAST_PAC_KEY_LEN);
     771                 :            : 
     772                 :            :         /* PAC-Opaque */
     773                 :          4 :         eap_fast_put_tlv(buf, PAC_TYPE_PAC_OPAQUE, pac_opaque, pac_len);
     774                 :          4 :         os_free(pac_opaque);
     775                 :            : 
     776                 :            :         /* PAC-Info */
     777                 :          4 :         pac_info = wpabuf_put(buf, sizeof(*pac_info));
     778                 :          4 :         pac_info->type = host_to_be16(PAC_TYPE_PAC_INFO);
     779                 :            : 
     780                 :            :         /* PAC-Lifetime (inside PAC-Info) */
     781                 :          4 :         eap_fast_put_tlv_hdr(buf, PAC_TYPE_CRED_LIFETIME, 4);
     782                 :          4 :         wpabuf_put_be32(buf, now.sec + data->pac_key_lifetime);
     783                 :            : 
     784                 :            :         /* A-ID (inside PAC-Info) */
     785                 :          4 :         eap_fast_put_tlv(buf, PAC_TYPE_A_ID, data->srv_id, data->srv_id_len);
     786                 :            :         
     787                 :            :         /* Note: headers may be misaligned after A-ID */
     788                 :            : 
     789         [ +  - ]:          4 :         if (sm->identity) {
     790                 :          4 :                 eap_fast_put_tlv(buf, PAC_TYPE_I_ID, sm->identity,
     791                 :          4 :                                  sm->identity_len);
     792                 :            :         }
     793                 :            : 
     794                 :            :         /* A-ID-Info (inside PAC-Info) */
     795                 :          4 :         eap_fast_put_tlv(buf, PAC_TYPE_A_ID_INFO, data->srv_id_info,
     796                 :            :                          srv_id_info_len);
     797                 :            : 
     798                 :            :         /* PAC-Type (inside PAC-Info) */
     799                 :          4 :         eap_fast_put_tlv_hdr(buf, PAC_TYPE_PAC_TYPE, 2);
     800                 :          4 :         wpabuf_put_be16(buf, PAC_TYPE_TUNNEL_PAC);
     801                 :            : 
     802                 :            :         /* Update PAC-Info and PAC TLV Length fields */
     803                 :          4 :         pos = wpabuf_put(buf, 0);
     804                 :          4 :         pac_info->len = host_to_be16(pos - (u8 *) (pac_info + 1));
     805                 :          4 :         pac_tlv->length = host_to_be16(pos - (u8 *) (pac_tlv + 1));
     806                 :            : 
     807                 :          4 :         return buf;
     808                 :            : }
     809                 :            : 
     810                 :            : 
     811                 :         29 : static int eap_fast_encrypt_phase2(struct eap_sm *sm,
     812                 :            :                                    struct eap_fast_data *data,
     813                 :            :                                    struct wpabuf *plain, int piggyback)
     814                 :            : {
     815                 :            :         struct wpabuf *encr;
     816                 :            : 
     817                 :         29 :         wpa_hexdump_buf_key(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 TLVs",
     818                 :            :                             plain);
     819                 :         29 :         encr = eap_server_tls_encrypt(sm, &data->ssl, plain);
     820                 :         29 :         wpabuf_free(plain);
     821                 :            : 
     822 [ +  + ][ +  + ]:         29 :         if (data->ssl.tls_out && piggyback) {
     823                 :          4 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Piggyback Phase 2 data "
     824                 :            :                            "(len=%d) with last Phase 1 Message (len=%d "
     825                 :            :                            "used=%d)",
     826                 :          4 :                            (int) wpabuf_len(encr),
     827                 :          4 :                            (int) wpabuf_len(data->ssl.tls_out),
     828                 :          4 :                            (int) data->ssl.tls_out_pos);
     829         [ -  + ]:          4 :                 if (wpabuf_resize(&data->ssl.tls_out, wpabuf_len(encr)) < 0) {
     830                 :          0 :                         wpa_printf(MSG_WARNING, "EAP-FAST: Failed to resize "
     831                 :            :                                    "output buffer");
     832                 :          0 :                         wpabuf_free(encr);
     833                 :          0 :                         return -1;
     834                 :            :                 }
     835                 :          4 :                 wpabuf_put_buf(data->ssl.tls_out, encr);
     836                 :          4 :                 wpabuf_free(encr);
     837                 :            :         } else {
     838                 :         25 :                 wpabuf_free(data->ssl.tls_out);
     839                 :         25 :                 data->ssl.tls_out_pos = 0;
     840                 :         25 :                 data->ssl.tls_out = encr;
     841                 :            :         }
     842                 :            : 
     843                 :         29 :         return 0;
     844                 :            : }
     845                 :            : 
     846                 :            : 
     847                 :         46 : static struct wpabuf * eap_fast_buildReq(struct eap_sm *sm, void *priv, u8 id)
     848                 :            : {
     849                 :         46 :         struct eap_fast_data *data = priv;
     850                 :         46 :         struct wpabuf *req = NULL;
     851                 :         46 :         int piggyback = 0;
     852                 :            : 
     853         [ -  + ]:         46 :         if (data->ssl.state == FRAG_ACK) {
     854                 :          0 :                 return eap_server_tls_build_ack(id, EAP_TYPE_FAST,
     855                 :            :                                                 data->fast_version);
     856                 :            :         }
     857                 :            : 
     858         [ +  + ]:         46 :         if (data->ssl.state == WAIT_FRAG_ACK) {
     859                 :          3 :                 return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_FAST,
     860                 :            :                                                 data->fast_version, id);
     861                 :            :         }
     862                 :            : 
     863   [ +  +  +  +  :         43 :         switch (data->state) {
                   +  - ]
     864                 :            :         case START:
     865                 :          7 :                 return eap_fast_build_start(sm, data, id);
     866                 :            :         case PHASE1:
     867         [ +  + ]:         11 :                 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
     868         [ -  + ]:          4 :                         if (eap_fast_phase1_done(sm, data) < 0)
     869                 :          0 :                                 return NULL;
     870         [ +  - ]:          4 :                         if (data->state == PHASE2_START) {
     871                 :            :                                 /*
     872                 :            :                                  * Try to generate Phase 2 data to piggyback
     873                 :            :                                  * with the end of Phase 1 to avoid extra
     874                 :            :                                  * roundtrip.
     875                 :            :                                  */
     876                 :          4 :                                 wpa_printf(MSG_DEBUG, "EAP-FAST: Try to start "
     877                 :            :                                            "Phase 2");
     878         [ -  + ]:          4 :                                 if (eap_fast_process_phase2_start(sm, data))
     879                 :          0 :                                         break;
     880                 :          4 :                                 req = eap_fast_build_phase2_req(sm, data, id);
     881                 :          4 :                                 piggyback = 1;
     882                 :            :                         }
     883                 :            :                 }
     884                 :         11 :                 break;
     885                 :            :         case PHASE2_ID:
     886                 :            :         case PHASE2_METHOD:
     887                 :         14 :                 req = eap_fast_build_phase2_req(sm, data, id);
     888                 :         14 :                 break;
     889                 :            :         case CRYPTO_BINDING:
     890                 :          7 :                 req = eap_fast_build_crypto_binding(sm, data);
     891         [ -  + ]:          7 :                 if (data->phase2_method) {
     892                 :            :                         /*
     893                 :            :                          * Include the start of the next EAP method in the
     894                 :            :                          * sequence in the same message with Crypto-Binding to
     895                 :            :                          * save a round-trip.
     896                 :            :                          */
     897                 :            :                         struct wpabuf *eap;
     898                 :          0 :                         eap = eap_fast_build_phase2_req(sm, data, id);
     899                 :          0 :                         req = wpabuf_concat(req, eap);
     900                 :          0 :                         eap_fast_state(data, PHASE2_METHOD);
     901                 :            :                 }
     902                 :          7 :                 break;
     903                 :            :         case REQUEST_PAC:
     904                 :          4 :                 req = eap_fast_build_pac(sm, data);
     905                 :          4 :                 break;
     906                 :            :         default:
     907                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
     908                 :          0 :                            __func__, data->state);
     909                 :          0 :                 return NULL;
     910                 :            :         }
     911                 :            : 
     912   [ +  +  -  + ]:         65 :         if (req &&
     913                 :         29 :             eap_fast_encrypt_phase2(sm, data, req, piggyback) < 0)
     914                 :          0 :                 return NULL;
     915                 :            : 
     916                 :         46 :         return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_FAST,
     917                 :            :                                         data->fast_version, id);
     918                 :            : }
     919                 :            : 
     920                 :            : 
     921                 :         46 : static Boolean eap_fast_check(struct eap_sm *sm, void *priv,
     922                 :            :                               struct wpabuf *respData)
     923                 :            : {
     924                 :            :         const u8 *pos;
     925                 :            :         size_t len;
     926                 :            : 
     927                 :         46 :         pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_FAST, respData, &len);
     928 [ -  + ][ +  - ]:         46 :         if (pos == NULL || len < 1) {
     929                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Invalid frame");
     930                 :          0 :                 return TRUE;
     931                 :            :         }
     932                 :            : 
     933                 :         46 :         return FALSE;
     934                 :            : }
     935                 :            : 
     936                 :            : 
     937                 :         21 : static int eap_fast_phase2_init(struct eap_sm *sm, struct eap_fast_data *data,
     938                 :            :                                 EapType eap_type)
     939                 :            : {
     940 [ +  + ][ +  - ]:         21 :         if (data->phase2_priv && data->phase2_method) {
     941                 :         14 :                 data->phase2_method->reset(sm, data->phase2_priv);
     942                 :         14 :                 data->phase2_method = NULL;
     943                 :         14 :                 data->phase2_priv = NULL;
     944                 :            :         }
     945                 :         21 :         data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
     946                 :            :                                                         eap_type);
     947         [ +  + ]:         21 :         if (!data->phase2_method)
     948                 :          7 :                 return -1;
     949                 :            : 
     950         [ +  + ]:         14 :         if (data->key_block_p) {
     951                 :          2 :                 sm->auth_challenge = data->key_block_p->server_challenge;
     952                 :          2 :                 sm->peer_challenge = data->key_block_p->client_challenge;
     953                 :            :         }
     954                 :         14 :         sm->init_phase2 = 1;
     955                 :         14 :         data->phase2_priv = data->phase2_method->init(sm);
     956                 :         14 :         sm->init_phase2 = 0;
     957                 :         14 :         sm->auth_challenge = NULL;
     958                 :         14 :         sm->peer_challenge = NULL;
     959                 :            : 
     960         [ -  + ]:         21 :         return data->phase2_priv == NULL ? -1 : 0;
     961                 :            : }
     962                 :            : 
     963                 :            : 
     964                 :         18 : static void eap_fast_process_phase2_response(struct eap_sm *sm,
     965                 :            :                                              struct eap_fast_data *data,
     966                 :            :                                              u8 *in_data, size_t in_len)
     967                 :            : {
     968                 :         18 :         u8 next_type = EAP_TYPE_NONE;
     969                 :            :         struct eap_hdr *hdr;
     970                 :            :         u8 *pos;
     971                 :            :         size_t left;
     972                 :            :         struct wpabuf buf;
     973                 :         18 :         const struct eap_method *m = data->phase2_method;
     974                 :         18 :         void *priv = data->phase2_priv;
     975                 :            : 
     976         [ -  + ]:         18 :         if (priv == NULL) {
     977                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - Phase2 not "
     978                 :            :                            "initialized?!", __func__);
     979                 :          0 :                 return;
     980                 :            :         }
     981                 :            : 
     982                 :         18 :         hdr = (struct eap_hdr *) in_data;
     983                 :         18 :         pos = (u8 *) (hdr + 1);
     984                 :            : 
     985 [ +  - ][ +  + ]:         18 :         if (in_len > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
     986                 :          3 :                 left = in_len - sizeof(*hdr);
     987                 :          3 :                 wpa_hexdump(MSG_DEBUG, "EAP-FAST: Phase2 type Nak'ed; "
     988                 :            :                             "allowed types", pos + 1, left - 1);
     989                 :            : #ifdef EAP_SERVER_TNC
     990 [ +  - ][ -  + ]:          3 :                 if (m && m->vendor == EAP_VENDOR_IETF &&
                 [ +  - ]
     991                 :          3 :                     m->method == EAP_TYPE_TNC) {
     992                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Peer Nak'ed required "
     993                 :            :                                    "TNC negotiation");
     994                 :          0 :                         next_type = eap_fast_req_failure(sm, data);
     995                 :          0 :                         eap_fast_phase2_init(sm, data, next_type);
     996                 :          0 :                         return;
     997                 :            :                 }
     998                 :            : #endif /* EAP_SERVER_TNC */
     999                 :          3 :                 eap_sm_process_nak(sm, pos + 1, left - 1);
    1000 [ +  - ][ +  - ]:          3 :                 if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
                 [ +  - ]
    1001                 :          3 :                     sm->user->methods[sm->user_eap_method_index].method !=
    1002                 :            :                     EAP_TYPE_NONE) {
    1003                 :          6 :                         next_type = sm->user->methods[
    1004                 :          6 :                                 sm->user_eap_method_index++].method;
    1005                 :          3 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %d",
    1006                 :            :                                    next_type);
    1007                 :            :                 } else {
    1008                 :          0 :                         next_type = eap_fast_req_failure(sm, data);
    1009                 :            :                 }
    1010                 :          3 :                 eap_fast_phase2_init(sm, data, next_type);
    1011                 :          3 :                 return;
    1012                 :            :         }
    1013                 :            : 
    1014                 :         15 :         wpabuf_set(&buf, in_data, in_len);
    1015                 :            : 
    1016         [ -  + ]:         15 :         if (m->check(sm, priv, &buf)) {
    1017                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 check() asked to "
    1018                 :            :                            "ignore the packet");
    1019                 :          0 :                 next_type = eap_fast_req_failure(sm, data);
    1020                 :          0 :                 return;
    1021                 :            :         }
    1022                 :            : 
    1023                 :         15 :         m->process(sm, priv, &buf);
    1024                 :            : 
    1025         [ +  + ]:         15 :         if (!m->isDone(sm, priv))
    1026                 :          4 :                 return;
    1027                 :            : 
    1028         [ -  + ]:         11 :         if (!m->isSuccess(sm, priv)) {
    1029                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method failed");
    1030                 :          0 :                 next_type = eap_fast_req_failure(sm, data);
    1031                 :          0 :                 eap_fast_phase2_init(sm, data, next_type);
    1032                 :          0 :                 return;
    1033                 :            :         }
    1034                 :            : 
    1035   [ +  +  -  - ]:         11 :         switch (data->state) {
    1036                 :            :         case PHASE2_ID:
    1037         [ -  + ]:          4 :                 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
    1038                 :          0 :                         wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Phase2 "
    1039                 :            :                                           "Identity not found in the user "
    1040                 :            :                                           "database",
    1041                 :          0 :                                           sm->identity, sm->identity_len);
    1042                 :          0 :                         next_type = eap_fast_req_failure(sm, data);
    1043                 :          0 :                         break;
    1044                 :            :                 }
    1045                 :            : 
    1046                 :          4 :                 eap_fast_state(data, PHASE2_METHOD);
    1047         [ +  + ]:          4 :                 if (data->anon_provisioning) {
    1048                 :            :                         /*
    1049                 :            :                          * Only EAP-MSCHAPv2 is allowed for anonymous
    1050                 :            :                          * provisioning.
    1051                 :            :                          */
    1052                 :          1 :                         next_type = EAP_TYPE_MSCHAPV2;
    1053                 :          1 :                         sm->user_eap_method_index = 0;
    1054                 :            :                 } else {
    1055                 :          3 :                         next_type = sm->user->methods[0].method;
    1056                 :          3 :                         sm->user_eap_method_index = 1;
    1057                 :            :                 }
    1058                 :          4 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %d", next_type);
    1059                 :          4 :                 break;
    1060                 :            :         case PHASE2_METHOD:
    1061                 :            :         case CRYPTO_BINDING:
    1062                 :          7 :                 eap_fast_update_icmk(sm, data);
    1063                 :          7 :                 eap_fast_state(data, CRYPTO_BINDING);
    1064                 :          7 :                 data->eap_seq++;
    1065                 :          7 :                 next_type = EAP_TYPE_NONE;
    1066                 :            : #ifdef EAP_SERVER_TNC
    1067 [ #  # ][ -  + ]:          7 :                 if (sm->tnc && !data->tnc_started) {
    1068                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Initialize TNC");
    1069                 :          0 :                         next_type = EAP_TYPE_TNC;
    1070                 :          0 :                         data->tnc_started = 1;
    1071                 :            :                 }
    1072                 :            : #endif /* EAP_SERVER_TNC */
    1073                 :          7 :                 break;
    1074                 :            :         case FAILURE:
    1075                 :          0 :                 break;
    1076                 :            :         default:
    1077                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
    1078                 :          0 :                            __func__, data->state);
    1079                 :          0 :                 break;
    1080                 :            :         }
    1081                 :            : 
    1082                 :         18 :         eap_fast_phase2_init(sm, data, next_type);
    1083                 :            : }
    1084                 :            : 
    1085                 :            : 
    1086                 :         18 : static void eap_fast_process_phase2_eap(struct eap_sm *sm,
    1087                 :            :                                         struct eap_fast_data *data,
    1088                 :            :                                         u8 *in_data, size_t in_len)
    1089                 :            : {
    1090                 :            :         struct eap_hdr *hdr;
    1091                 :            :         size_t len;
    1092                 :            : 
    1093                 :         18 :         hdr = (struct eap_hdr *) in_data;
    1094         [ -  + ]:         18 :         if (in_len < (int) sizeof(*hdr)) {
    1095                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
    1096                 :            :                            "EAP frame (len=%lu)", (unsigned long) in_len);
    1097                 :          0 :                 eap_fast_req_failure(sm, data);
    1098                 :          0 :                 return;
    1099                 :            :         }
    1100                 :         18 :         len = be_to_host16(hdr->length);
    1101         [ -  + ]:         18 :         if (len > in_len) {
    1102                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Length mismatch in "
    1103                 :            :                            "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
    1104                 :            :                            (unsigned long) in_len, (unsigned long) len);
    1105                 :          0 :                 eap_fast_req_failure(sm, data);
    1106                 :          0 :                 return;
    1107                 :            :         }
    1108                 :         18 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: code=%d "
    1109                 :         36 :                    "identifier=%d length=%lu", hdr->code, hdr->identifier,
    1110                 :            :                    (unsigned long) len);
    1111         [ +  - ]:         18 :         switch (hdr->code) {
    1112                 :            :         case EAP_CODE_RESPONSE:
    1113                 :         18 :                 eap_fast_process_phase2_response(sm, data, (u8 *) hdr, len);
    1114                 :         18 :                 break;
    1115                 :            :         default:
    1116                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
    1117                 :          0 :                            "Phase 2 EAP header", hdr->code);
    1118                 :         18 :                 break;
    1119                 :            :         }
    1120                 :            : }
    1121                 :            : 
    1122                 :            : 
    1123                 :         29 : static int eap_fast_parse_tlvs(struct wpabuf *data,
    1124                 :            :                                struct eap_fast_tlv_parse *tlv)
    1125                 :            : {
    1126                 :            :         int mandatory, tlv_type, len, res;
    1127                 :            :         u8 *pos, *end;
    1128                 :            : 
    1129                 :         29 :         os_memset(tlv, 0, sizeof(*tlv));
    1130                 :            : 
    1131                 :         29 :         pos = wpabuf_mhead(data);
    1132                 :         29 :         end = pos + wpabuf_len(data);
    1133         [ +  + ]:         75 :         while (pos + 4 < end) {
    1134                 :         46 :                 mandatory = pos[0] & 0x80;
    1135                 :         46 :                 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
    1136                 :         46 :                 pos += 2;
    1137                 :         46 :                 len = WPA_GET_BE16(pos);
    1138                 :         46 :                 pos += 2;
    1139         [ -  + ]:         46 :                 if (pos + len > end) {
    1140                 :          0 :                         wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
    1141                 :          0 :                         return -1;
    1142                 :            :                 }
    1143         [ +  + ]:         46 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
    1144                 :            :                            "TLV type %d length %d%s",
    1145                 :            :                            tlv_type, len, mandatory ? " (mandatory)" : "");
    1146                 :            : 
    1147                 :         46 :                 res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
    1148         [ -  + ]:         46 :                 if (res == -2)
    1149                 :          0 :                         break;
    1150         [ -  + ]:         46 :                 if (res < 0) {
    1151         [ #  # ]:          0 :                         if (mandatory) {
    1152                 :          0 :                                 wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
    1153                 :            :                                            "mandatory TLV type %d", tlv_type);
    1154                 :            :                                 /* TODO: generate Nak TLV */
    1155                 :          0 :                                 break;
    1156                 :            :                         } else {
    1157                 :          0 :                                 wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored "
    1158                 :            :                                            "unknown optional TLV type %d",
    1159                 :            :                                            tlv_type);
    1160                 :            :                         }
    1161                 :            :                 }
    1162                 :            : 
    1163                 :         46 :                 pos += len;
    1164                 :            :         }
    1165                 :            : 
    1166                 :         29 :         return 0;
    1167                 :            : }
    1168                 :            : 
    1169                 :            : 
    1170                 :          7 : static int eap_fast_validate_crypto_binding(
    1171                 :            :         struct eap_fast_data *data, struct eap_tlv_crypto_binding_tlv *b,
    1172                 :            :         size_t bind_len)
    1173                 :            : {
    1174                 :            :         u8 cmac[SHA1_MAC_LEN];
    1175                 :            : 
    1176                 :          7 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: "
    1177                 :            :                    "Version %d Received Version %d SubType %d",
    1178                 :         21 :                    b->version, b->received_version, b->subtype);
    1179                 :          7 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
    1180                 :          7 :                     b->nonce, sizeof(b->nonce));
    1181                 :          7 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
    1182                 :          7 :                     b->compound_mac, sizeof(b->compound_mac));
    1183                 :            : 
    1184 [ -  + ][ +  - ]:          7 :         if (b->version != EAP_FAST_VERSION ||
    1185                 :          7 :             b->received_version != EAP_FAST_VERSION) {
    1186                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected version "
    1187                 :            :                            "in Crypto-Binding: version %d "
    1188                 :          0 :                            "received_version %d", b->version,
    1189                 :          0 :                            b->received_version);
    1190                 :          0 :                 return -1;
    1191                 :            :         }
    1192                 :            : 
    1193         [ -  + ]:          7 :         if (b->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE) {
    1194                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected subtype in "
    1195                 :          0 :                            "Crypto-Binding: %d", b->subtype);
    1196                 :          0 :                 return -1;
    1197                 :            :         }
    1198                 :            : 
    1199 [ +  - ][ -  + ]:          7 :         if (os_memcmp(data->crypto_binding_nonce, b->nonce, 31) != 0 ||
    1200                 :          7 :             (data->crypto_binding_nonce[31] | 1) != b->nonce[31]) {
    1201                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid nonce in "
    1202                 :            :                            "Crypto-Binding");
    1203                 :          0 :                 return -1;
    1204                 :            :         }
    1205                 :            : 
    1206                 :          7 :         os_memcpy(cmac, b->compound_mac, sizeof(cmac));
    1207                 :          7 :         os_memset(b->compound_mac, 0, sizeof(cmac));
    1208                 :          7 :         wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for "
    1209                 :            :                     "Compound MAC calculation",
    1210                 :            :                     (u8 *) b, bind_len);
    1211                 :          7 :         hmac_sha1(data->cmk, EAP_FAST_CMK_LEN, (u8 *) b, bind_len,
    1212                 :          7 :                   b->compound_mac);
    1213         [ -  + ]:          7 :         if (os_memcmp(cmac, b->compound_mac, sizeof(cmac)) != 0) {
    1214                 :          0 :                 wpa_hexdump(MSG_MSGDUMP,
    1215                 :            :                             "EAP-FAST: Calculated Compound MAC",
    1216                 :          0 :                             b->compound_mac, sizeof(cmac));
    1217                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not "
    1218                 :            :                            "match");
    1219                 :          0 :                 return -1;
    1220                 :            :         }
    1221                 :            : 
    1222                 :          7 :         return 0;
    1223                 :            : }
    1224                 :            : 
    1225                 :            : 
    1226                 :          3 : static int eap_fast_pac_type(u8 *pac, size_t len, u16 type)
    1227                 :            : {
    1228                 :            :         struct eap_tlv_pac_type_tlv *tlv;
    1229                 :            : 
    1230 [ +  - ][ -  + ]:          3 :         if (pac == NULL || len != sizeof(*tlv))
    1231                 :          0 :                 return 0;
    1232                 :            : 
    1233                 :          3 :         tlv = (struct eap_tlv_pac_type_tlv *) pac;
    1234                 :            : 
    1235 [ +  - ][ +  - ]:          6 :         return be_to_host16(tlv->tlv_type) == PAC_TYPE_PAC_TYPE &&
    1236         [ +  - ]:          3 :                 be_to_host16(tlv->length) == 2 &&
    1237                 :          3 :                 be_to_host16(tlv->pac_type) == type;
    1238                 :            : }
    1239                 :            : 
    1240                 :            : 
    1241                 :         29 : static void eap_fast_process_phase2_tlvs(struct eap_sm *sm,
    1242                 :            :                                          struct eap_fast_data *data,
    1243                 :            :                                          struct wpabuf *in_data)
    1244                 :            : {
    1245                 :            :         struct eap_fast_tlv_parse tlv;
    1246                 :         29 :         int check_crypto_binding = data->state == CRYPTO_BINDING;
    1247                 :            : 
    1248         [ -  + ]:         29 :         if (eap_fast_parse_tlvs(in_data, &tlv) < 0) {
    1249                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to parse received "
    1250                 :            :                            "Phase 2 TLVs");
    1251                 :          0 :                 return;
    1252                 :            :         }
    1253                 :            : 
    1254         [ -  + ]:         29 :         if (tlv.result == EAP_TLV_RESULT_FAILURE) {
    1255                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Result TLV indicated "
    1256                 :            :                            "failure");
    1257                 :          0 :                 eap_fast_state(data, FAILURE);
    1258                 :          0 :                 return;
    1259                 :            :         }
    1260                 :            : 
    1261         [ +  + ]:         29 :         if (data->state == REQUEST_PAC) {
    1262                 :            :                 u16 type, len, res;
    1263 [ +  - ][ -  + ]:          4 :                 if (tlv.pac == NULL || tlv.pac_len < 6) {
    1264                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC "
    1265                 :            :                                    "Acknowledgement received");
    1266                 :          0 :                         eap_fast_state(data, FAILURE);
    1267                 :          0 :                         return;
    1268                 :            :                 }
    1269                 :            : 
    1270                 :          4 :                 type = WPA_GET_BE16(tlv.pac);
    1271                 :          4 :                 len = WPA_GET_BE16(tlv.pac + 2);
    1272                 :          4 :                 res = WPA_GET_BE16(tlv.pac + 4);
    1273                 :            : 
    1274 [ +  - ][ -  + ]:          4 :                 if (type != PAC_TYPE_PAC_ACKNOWLEDGEMENT || len != 2 ||
                 [ +  - ]
    1275                 :            :                     res != EAP_TLV_RESULT_SUCCESS) {
    1276                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV did not "
    1277                 :            :                                    "contain acknowledgement");
    1278                 :          0 :                         eap_fast_state(data, FAILURE);
    1279                 :          0 :                         return;
    1280                 :            :                 }
    1281                 :            : 
    1282                 :          4 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Acknowledgement received "
    1283                 :            :                            "- PAC provisioning succeeded");
    1284 [ -  + ][ +  + ]:          4 :                 eap_fast_state(data, (data->anon_provisioning ||
    1285                 :          3 :                                       data->send_new_pac == 2) ?
    1286                 :            :                                FAILURE : SUCCESS);
    1287                 :          4 :                 return;
    1288                 :            :         }
    1289                 :            : 
    1290         [ +  + ]:         25 :         if (check_crypto_binding) {
    1291         [ -  + ]:          7 :                 if (tlv.crypto_binding == NULL) {
    1292                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: No Crypto-Binding "
    1293                 :            :                                    "TLV received");
    1294                 :          0 :                         eap_fast_state(data, FAILURE);
    1295                 :          0 :                         return;
    1296                 :            :                 }
    1297                 :            : 
    1298 [ +  + ][ -  + ]:          7 :                 if (data->final_result &&
    1299                 :          6 :                     tlv.result != EAP_TLV_RESULT_SUCCESS) {
    1300                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
    1301                 :            :                                    "without Success Result");
    1302                 :          0 :                         eap_fast_state(data, FAILURE);
    1303                 :          0 :                         return;
    1304                 :            :                 }
    1305                 :            : 
    1306 [ +  + ][ -  + ]:          7 :                 if (!data->final_result &&
    1307                 :          1 :                     tlv.iresult != EAP_TLV_RESULT_SUCCESS) {
    1308                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
    1309                 :            :                                    "without intermediate Success Result");
    1310                 :          0 :                         eap_fast_state(data, FAILURE);
    1311                 :          0 :                         return;
    1312                 :            :                 }
    1313                 :            : 
    1314         [ -  + ]:          7 :                 if (eap_fast_validate_crypto_binding(data, tlv.crypto_binding,
    1315                 :            :                                                      tlv.crypto_binding_len)) {
    1316                 :          0 :                         eap_fast_state(data, FAILURE);
    1317                 :          0 :                         return;
    1318                 :            :                 }
    1319                 :            : 
    1320                 :          7 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Valid Crypto-Binding TLV "
    1321                 :            :                            "received");
    1322         [ +  + ]:          7 :                 if (data->final_result) {
    1323                 :          6 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
    1324                 :            :                                    "completed successfully");
    1325                 :            :                 }
    1326                 :            : 
    1327 [ +  + ][ +  - ]:          7 :                 if (data->anon_provisioning &&
    1328         [ -  + ]:          1 :                     sm->eap_fast_prov != ANON_PROV &&
    1329                 :          1 :                     sm->eap_fast_prov != BOTH_PROV) {
    1330                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Client is trying to "
    1331                 :            :                                    "use unauthenticated provisioning which is "
    1332                 :            :                                    "disabled");
    1333                 :          0 :                         eap_fast_state(data, FAILURE);
    1334                 :          0 :                         return;
    1335                 :            :                 }
    1336                 :            : 
    1337 [ +  - ][ -  + ]:          7 :                 if (sm->eap_fast_prov != AUTH_PROV &&
    1338         [ #  # ]:          0 :                     sm->eap_fast_prov != BOTH_PROV &&
    1339         [ #  # ]:          0 :                     tlv.request_action == EAP_TLV_ACTION_PROCESS_TLV &&
    1340                 :          0 :                     eap_fast_pac_type(tlv.pac, tlv.pac_len,
    1341                 :            :                                       PAC_TYPE_TUNNEL_PAC)) {
    1342                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Client is trying to "
    1343                 :            :                                    "use authenticated provisioning which is "
    1344                 :            :                                    "disabled");
    1345                 :          0 :                         eap_fast_state(data, FAILURE);
    1346                 :          0 :                         return;
    1347                 :            :                 }
    1348                 :            : 
    1349 [ +  + ][ +  + ]:          7 :                 if (data->anon_provisioning ||
    1350         [ +  - ]:          3 :                     (tlv.request_action == EAP_TLV_ACTION_PROCESS_TLV &&
    1351                 :          3 :                      eap_fast_pac_type(tlv.pac, tlv.pac_len,
    1352                 :            :                                        PAC_TYPE_TUNNEL_PAC))) {
    1353                 :          4 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Requested a new "
    1354                 :            :                                    "Tunnel PAC");
    1355                 :          4 :                         eap_fast_state(data, REQUEST_PAC);
    1356         [ -  + ]:          3 :                 } else if (data->send_new_pac) {
    1357                 :          0 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Server triggered "
    1358                 :            :                                    "re-keying of Tunnel PAC");
    1359                 :          0 :                         eap_fast_state(data, REQUEST_PAC);
    1360         [ +  - ]:          3 :                 } else if (data->final_result)
    1361                 :          3 :                         eap_fast_state(data, SUCCESS);
    1362                 :            :         }
    1363                 :            : 
    1364         [ +  + ]:         25 :         if (tlv.eap_payload_tlv) {
    1365                 :         29 :                 eap_fast_process_phase2_eap(sm, data, tlv.eap_payload_tlv,
    1366                 :            :                                             tlv.eap_payload_tlv_len);
    1367                 :            :         }
    1368                 :            : }
    1369                 :            : 
    1370                 :            : 
    1371                 :         29 : static void eap_fast_process_phase2(struct eap_sm *sm,
    1372                 :            :                                     struct eap_fast_data *data,
    1373                 :            :                                     struct wpabuf *in_buf)
    1374                 :            : {
    1375                 :            :         struct wpabuf *in_decrypted;
    1376                 :            : 
    1377                 :         29 :         wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
    1378                 :            :                    " Phase 2", (unsigned long) wpabuf_len(in_buf));
    1379                 :            : 
    1380         [ -  + ]:         29 :         if (data->pending_phase2_resp) {
    1381                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 response - "
    1382                 :            :                            "skip decryption and use old data");
    1383                 :          0 :                 eap_fast_process_phase2_tlvs(sm, data,
    1384                 :            :                                              data->pending_phase2_resp);
    1385                 :          0 :                 wpabuf_free(data->pending_phase2_resp);
    1386                 :          0 :                 data->pending_phase2_resp = NULL;
    1387                 :          0 :                 return;
    1388                 :            :         }
    1389                 :            : 
    1390                 :         29 :         in_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
    1391                 :            :                                               in_buf);
    1392         [ -  + ]:         29 :         if (in_decrypted == NULL) {
    1393                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: Failed to decrypt Phase 2 "
    1394                 :            :                            "data");
    1395                 :          0 :                 eap_fast_state(data, FAILURE);
    1396                 :          0 :                 return;
    1397                 :            :         }
    1398                 :            : 
    1399                 :         29 :         wpa_hexdump_buf_key(MSG_DEBUG, "EAP-FAST: Decrypted Phase 2 TLVs",
    1400                 :            :                             in_decrypted);
    1401                 :            : 
    1402                 :         29 :         eap_fast_process_phase2_tlvs(sm, data, in_decrypted);
    1403                 :            : 
    1404         [ -  + ]:         29 :         if (sm->method_pending == METHOD_PENDING_WAIT) {
    1405                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method is in "
    1406                 :            :                            "pending wait state - save decrypted response");
    1407                 :          0 :                 wpabuf_free(data->pending_phase2_resp);
    1408                 :          0 :                 data->pending_phase2_resp = in_decrypted;
    1409                 :          0 :                 return;
    1410                 :            :         }
    1411                 :            : 
    1412                 :         29 :         wpabuf_free(in_decrypted);
    1413                 :            : }
    1414                 :            : 
    1415                 :            : 
    1416                 :         46 : static int eap_fast_process_version(struct eap_sm *sm, void *priv,
    1417                 :            :                                     int peer_version)
    1418                 :            : {
    1419                 :         46 :         struct eap_fast_data *data = priv;
    1420                 :            : 
    1421                 :         46 :         data->peer_version = peer_version;
    1422                 :            : 
    1423 [ -  + ][ #  # ]:         46 :         if (data->force_version >= 0 && peer_version != data->force_version) {
    1424                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: peer did not select the forced"
    1425                 :            :                            " version (forced=%d peer=%d) - reject",
    1426                 :            :                            data->force_version, peer_version);
    1427                 :          0 :                 return -1;
    1428                 :            :         }
    1429                 :            : 
    1430         [ -  + ]:         46 :         if (peer_version < data->fast_version) {
    1431                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: peer ver=%d, own ver=%d; "
    1432                 :            :                            "use version %d",
    1433                 :            :                            peer_version, data->fast_version, peer_version);
    1434                 :          0 :                 data->fast_version = peer_version;
    1435                 :            :         }
    1436                 :            : 
    1437                 :         46 :         return 0;
    1438                 :            : }
    1439                 :            : 
    1440                 :            : 
    1441                 :         14 : static int eap_fast_process_phase1(struct eap_sm *sm,
    1442                 :            :                                    struct eap_fast_data *data)
    1443                 :            : {
    1444         [ -  + ]:         14 :         if (eap_server_tls_phase1(sm, &data->ssl) < 0) {
    1445                 :          0 :                 wpa_printf(MSG_INFO, "EAP-FAST: TLS processing failed");
    1446                 :          0 :                 eap_fast_state(data, FAILURE);
    1447                 :          0 :                 return -1;
    1448                 :            :         }
    1449                 :            : 
    1450   [ +  +  +  + ]:         21 :         if (!tls_connection_established(sm->ssl_ctx, data->ssl.conn) ||
    1451                 :          7 :             wpabuf_len(data->ssl.tls_out) > 0)
    1452                 :         11 :                 return 1;
    1453                 :            : 
    1454                 :            :         /*
    1455                 :            :          * Phase 1 was completed with the received message (e.g., when using
    1456                 :            :          * abbreviated handshake), so Phase 2 can be started immediately
    1457                 :            :          * without having to send through an empty message to the peer.
    1458                 :            :          */
    1459                 :            : 
    1460                 :         14 :         return eap_fast_phase1_done(sm, data);
    1461                 :            : }
    1462                 :            : 
    1463                 :            : 
    1464                 :          7 : static int eap_fast_process_phase2_start(struct eap_sm *sm,
    1465                 :            :                                          struct eap_fast_data *data)
    1466                 :            : {
    1467                 :            :         u8 next_type;
    1468                 :            : 
    1469         [ +  + ]:          7 :         if (data->identity) {
    1470                 :          3 :                 os_free(sm->identity);
    1471                 :          3 :                 sm->identity = data->identity;
    1472                 :          3 :                 data->identity = NULL;
    1473                 :          3 :                 sm->identity_len = data->identity_len;
    1474                 :          3 :                 data->identity_len = 0;
    1475                 :          3 :                 sm->require_identity_match = 1;
    1476         [ -  + ]:          3 :                 if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
    1477                 :          0 :                         wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: "
    1478                 :            :                                           "Phase2 Identity not found "
    1479                 :            :                                           "in the user database",
    1480                 :          0 :                                           sm->identity, sm->identity_len);
    1481                 :          0 :                         next_type = eap_fast_req_failure(sm, data);
    1482                 :            :                 } else {
    1483                 :          3 :                         wpa_printf(MSG_DEBUG, "EAP-FAST: Identity already "
    1484                 :            :                                    "known - skip Phase 2 Identity Request");
    1485                 :          3 :                         next_type = sm->user->methods[0].method;
    1486                 :          3 :                         sm->user_eap_method_index = 1;
    1487                 :            :                 }
    1488                 :            : 
    1489                 :          3 :                 eap_fast_state(data, PHASE2_METHOD);
    1490                 :            :         } else {
    1491                 :          4 :                 eap_fast_state(data, PHASE2_ID);
    1492                 :          4 :                 next_type = EAP_TYPE_IDENTITY;
    1493                 :            :         }
    1494                 :            : 
    1495                 :          7 :         return eap_fast_phase2_init(sm, data, next_type);
    1496                 :            : }
    1497                 :            : 
    1498                 :            : 
    1499                 :         43 : static void eap_fast_process_msg(struct eap_sm *sm, void *priv,
    1500                 :            :                                  const struct wpabuf *respData)
    1501                 :            : {
    1502                 :         43 :         struct eap_fast_data *data = priv;
    1503                 :            : 
    1504   [ +  -  +  - ]:         43 :         switch (data->state) {
    1505                 :            :         case PHASE1:
    1506         [ +  + ]:         14 :                 if (eap_fast_process_phase1(sm, data))
    1507                 :         11 :                         break;
    1508                 :            : 
    1509                 :            :                 /* fall through to PHASE2_START */
    1510                 :            :         case PHASE2_START:
    1511                 :          3 :                 eap_fast_process_phase2_start(sm, data);
    1512                 :          3 :                 break;
    1513                 :            :         case PHASE2_ID:
    1514                 :            :         case PHASE2_METHOD:
    1515                 :            :         case CRYPTO_BINDING:
    1516                 :            :         case REQUEST_PAC:
    1517                 :         29 :                 eap_fast_process_phase2(sm, data, data->ssl.tls_in);
    1518                 :         29 :                 break;
    1519                 :            :         default:
    1520                 :          0 :                 wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected state %d in %s",
    1521                 :          0 :                            data->state, __func__);
    1522                 :          0 :                 break;
    1523                 :            :         }
    1524                 :         43 : }
    1525                 :            : 
    1526                 :            : 
    1527                 :         46 : static void eap_fast_process(struct eap_sm *sm, void *priv,
    1528                 :            :                              struct wpabuf *respData)
    1529                 :            : {
    1530                 :         46 :         struct eap_fast_data *data = priv;
    1531         [ -  + ]:         46 :         if (eap_server_tls_process(sm, &data->ssl, respData, data,
    1532                 :            :                                    EAP_TYPE_FAST, eap_fast_process_version,
    1533                 :            :                                    eap_fast_process_msg) < 0)
    1534                 :          0 :                 eap_fast_state(data, FAILURE);
    1535                 :         46 : }
    1536                 :            : 
    1537                 :            : 
    1538                 :         47 : static Boolean eap_fast_isDone(struct eap_sm *sm, void *priv)
    1539                 :            : {
    1540                 :         47 :         struct eap_fast_data *data = priv;
    1541 [ +  + ][ +  + ]:         47 :         return data->state == SUCCESS || data->state == FAILURE;
    1542                 :            : }
    1543                 :            : 
    1544                 :            : 
    1545                 :          7 : static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
    1546                 :            : {
    1547                 :          7 :         struct eap_fast_data *data = priv;
    1548                 :            :         u8 *eapKeyData;
    1549                 :            : 
    1550         [ +  + ]:          7 :         if (data->state != SUCCESS)
    1551                 :          1 :                 return NULL;
    1552                 :            : 
    1553                 :          6 :         eapKeyData = os_malloc(EAP_FAST_KEY_LEN);
    1554         [ -  + ]:          6 :         if (eapKeyData == NULL)
    1555                 :          0 :                 return NULL;
    1556                 :            : 
    1557                 :          6 :         eap_fast_derive_eap_msk(data->simck, eapKeyData);
    1558                 :          6 :         *len = EAP_FAST_KEY_LEN;
    1559                 :            : 
    1560                 :          7 :         return eapKeyData;
    1561                 :            : }
    1562                 :            : 
    1563                 :            : 
    1564                 :          0 : static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
    1565                 :            : {
    1566                 :          0 :         struct eap_fast_data *data = priv;
    1567                 :            :         u8 *eapKeyData;
    1568                 :            : 
    1569         [ #  # ]:          0 :         if (data->state != SUCCESS)
    1570                 :          0 :                 return NULL;
    1571                 :            : 
    1572                 :          0 :         eapKeyData = os_malloc(EAP_EMSK_LEN);
    1573         [ #  # ]:          0 :         if (eapKeyData == NULL)
    1574                 :          0 :                 return NULL;
    1575                 :            : 
    1576                 :          0 :         eap_fast_derive_eap_emsk(data->simck, eapKeyData);
    1577                 :          0 :         *len = EAP_EMSK_LEN;
    1578                 :            : 
    1579                 :          0 :         return eapKeyData;
    1580                 :            : }
    1581                 :            : 
    1582                 :            : 
    1583                 :          8 : static Boolean eap_fast_isSuccess(struct eap_sm *sm, void *priv)
    1584                 :            : {
    1585                 :          8 :         struct eap_fast_data *data = priv;
    1586                 :          8 :         return data->state == SUCCESS;
    1587                 :            : }
    1588                 :            : 
    1589                 :            : 
    1590                 :          2 : int eap_server_fast_register(void)
    1591                 :            : {
    1592                 :            :         struct eap_method *eap;
    1593                 :            :         int ret;
    1594                 :            : 
    1595                 :          2 :         eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
    1596                 :            :                                       EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
    1597         [ -  + ]:          2 :         if (eap == NULL)
    1598                 :          0 :                 return -1;
    1599                 :            : 
    1600                 :          2 :         eap->init = eap_fast_init;
    1601                 :          2 :         eap->reset = eap_fast_reset;
    1602                 :          2 :         eap->buildReq = eap_fast_buildReq;
    1603                 :          2 :         eap->check = eap_fast_check;
    1604                 :          2 :         eap->process = eap_fast_process;
    1605                 :          2 :         eap->isDone = eap_fast_isDone;
    1606                 :          2 :         eap->getKey = eap_fast_getKey;
    1607                 :          2 :         eap->get_emsk = eap_fast_get_emsk;
    1608                 :          2 :         eap->isSuccess = eap_fast_isSuccess;
    1609                 :            : 
    1610                 :          2 :         ret = eap_server_method_register(eap);
    1611         [ -  + ]:          2 :         if (ret)
    1612                 :          0 :                 eap_server_method_free(eap);
    1613                 :          2 :         return ret;
    1614                 :            : }

Generated by: LCOV version 1.9