LCOV - code coverage report
Current view: top level - src/eap_server - eap_server_tls_common.c (source / functions) Hit Total Coverage
Test: hostapd/hlr_auc_gw (AS) hwsim test run 1388338050 Lines: 147 212 69.3 %
Date: 2013-12-29 Functions: 13 13 100.0 %
Branches: 62 110 56.4 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * EAP-TLS/PEAP/TTLS/FAST server common functions
       3                 :            :  * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
       4                 :            :  *
       5                 :            :  * This software may be distributed under the terms of the BSD license.
       6                 :            :  * See README for more details.
       7                 :            :  */
       8                 :            : 
       9                 :            : #include "includes.h"
      10                 :            : 
      11                 :            : #include "common.h"
      12                 :            : #include "crypto/sha1.h"
      13                 :            : #include "crypto/tls.h"
      14                 :            : #include "eap_i.h"
      15                 :            : #include "eap_tls_common.h"
      16                 :            : 
      17                 :            : 
      18                 :            : static void eap_server_tls_free_in_buf(struct eap_ssl_data *data);
      19                 :            : 
      20                 :            : 
      21                 :        215 : struct wpabuf * eap_tls_msg_alloc(EapType type, size_t payload_len,
      22                 :            :                                   u8 code, u8 identifier)
      23                 :            : {
      24         [ -  + ]:        215 :         if (type == EAP_UNAUTH_TLS_TYPE)
      25                 :          0 :                 return eap_msg_alloc(EAP_VENDOR_UNAUTH_TLS,
      26                 :            :                                      EAP_VENDOR_TYPE_UNAUTH_TLS, payload_len,
      27                 :            :                                      code, identifier);
      28                 :        215 :         return eap_msg_alloc(EAP_VENDOR_IETF, type, payload_len, code,
      29                 :            :                              identifier);
      30                 :            : }
      31                 :            : 
      32                 :            : 
      33                 :         61 : int eap_server_tls_ssl_init(struct eap_sm *sm, struct eap_ssl_data *data,
      34                 :            :                             int verify_peer)
      35                 :            : {
      36         [ -  + ]:         61 :         if (sm->ssl_ctx == NULL) {
      37                 :          0 :                 wpa_printf(MSG_ERROR, "TLS context not initialized - cannot use TLS-based EAP method");
      38                 :          0 :                 return -1;
      39                 :            :         }
      40                 :            : 
      41                 :         61 :         data->eap = sm;
      42                 :         61 :         data->phase2 = sm->init_phase2;
      43                 :            : 
      44                 :         61 :         data->conn = tls_connection_init(sm->ssl_ctx);
      45         [ -  + ]:         61 :         if (data->conn == NULL) {
      46                 :          0 :                 wpa_printf(MSG_INFO, "SSL: Failed to initialize new TLS "
      47                 :            :                            "connection");
      48                 :          0 :                 return -1;
      49                 :            :         }
      50                 :            : 
      51         [ -  + ]:         61 :         if (tls_connection_set_verify(sm->ssl_ctx, data->conn, verify_peer)) {
      52                 :          0 :                 wpa_printf(MSG_INFO, "SSL: Failed to configure verification "
      53                 :            :                            "of TLS peer certificate");
      54                 :          0 :                 tls_connection_deinit(sm->ssl_ctx, data->conn);
      55                 :          0 :                 data->conn = NULL;
      56                 :          0 :                 return -1;
      57                 :            :         }
      58                 :            : 
      59         [ -  + ]:         61 :         data->tls_out_limit = sm->fragment_size > 0 ? sm->fragment_size : 1398;
      60         [ -  + ]:         61 :         if (data->phase2) {
      61                 :            :                 /* Limit the fragment size in the inner TLS authentication
      62                 :            :                  * since the outer authentication with EAP-PEAP does not yet
      63                 :            :                  * support fragmentation */
      64         [ #  # ]:          0 :                 if (data->tls_out_limit > 100)
      65                 :          0 :                         data->tls_out_limit -= 100;
      66                 :            :         }
      67                 :         61 :         return 0;
      68                 :            : }
      69                 :            : 
      70                 :            : 
      71                 :         61 : void eap_server_tls_ssl_deinit(struct eap_sm *sm, struct eap_ssl_data *data)
      72                 :            : {
      73                 :         61 :         tls_connection_deinit(sm->ssl_ctx, data->conn);
      74                 :         61 :         eap_server_tls_free_in_buf(data);
      75                 :         61 :         wpabuf_free(data->tls_out);
      76                 :         61 :         data->tls_out = NULL;
      77                 :         61 : }
      78                 :            : 
      79                 :            : 
      80                 :         64 : u8 * eap_server_tls_derive_key(struct eap_sm *sm, struct eap_ssl_data *data,
      81                 :            :                                char *label, size_t len)
      82                 :            : {
      83                 :            :         struct tls_keys keys;
      84                 :         64 :         u8 *rnd = NULL, *out;
      85                 :            : 
      86                 :         64 :         out = os_malloc(len);
      87         [ -  + ]:         64 :         if (out == NULL)
      88                 :          0 :                 return NULL;
      89                 :            : 
      90         [ +  - ]:         64 :         if (tls_connection_prf(sm->ssl_ctx, data->conn, label, 0, out, len) ==
      91                 :            :             0)
      92                 :         64 :                 return out;
      93                 :            : 
      94         [ #  # ]:          0 :         if (tls_connection_get_keys(sm->ssl_ctx, data->conn, &keys))
      95                 :          0 :                 goto fail;
      96                 :            : 
      97 [ #  # ][ #  # ]:          0 :         if (keys.client_random == NULL || keys.server_random == NULL ||
                 [ #  # ]
      98                 :          0 :             keys.master_key == NULL)
      99                 :            :                 goto fail;
     100                 :            : 
     101                 :          0 :         rnd = os_malloc(keys.client_random_len + keys.server_random_len);
     102         [ #  # ]:          0 :         if (rnd == NULL)
     103                 :          0 :                 goto fail;
     104                 :          0 :         os_memcpy(rnd, keys.client_random, keys.client_random_len);
     105                 :          0 :         os_memcpy(rnd + keys.client_random_len, keys.server_random,
     106                 :            :                   keys.server_random_len);
     107                 :            : 
     108         [ #  # ]:          0 :         if (tls_prf_sha1_md5(keys.master_key, keys.master_key_len,
     109                 :          0 :                              label, rnd, keys.client_random_len +
     110                 :          0 :                              keys.server_random_len, out, len))
     111                 :          0 :                 goto fail;
     112                 :            : 
     113                 :          0 :         os_free(rnd);
     114                 :          0 :         return out;
     115                 :            : 
     116                 :            : fail:
     117                 :          0 :         os_free(out);
     118                 :          0 :         os_free(rnd);
     119                 :         64 :         return NULL;
     120                 :            : }
     121                 :            : 
     122                 :            : 
     123                 :        209 : struct wpabuf * eap_server_tls_build_msg(struct eap_ssl_data *data,
     124                 :            :                                          int eap_type, int version, u8 id)
     125                 :            : {
     126                 :            :         struct wpabuf *req;
     127                 :            :         u8 flags;
     128                 :            :         size_t send_len, plen;
     129                 :            : 
     130                 :        209 :         wpa_printf(MSG_DEBUG, "SSL: Generating Request");
     131         [ -  + ]:        209 :         if (data->tls_out == NULL) {
     132                 :          0 :                 wpa_printf(MSG_ERROR, "SSL: tls_out NULL in %s", __func__);
     133                 :          0 :                 return NULL;
     134                 :            :         }
     135                 :            : 
     136                 :        209 :         flags = version;
     137                 :        209 :         send_len = wpabuf_len(data->tls_out) - data->tls_out_pos;
     138         [ +  + ]:        209 :         if (1 + send_len > data->tls_out_limit) {
     139                 :         46 :                 send_len = data->tls_out_limit - 1;
     140                 :         46 :                 flags |= EAP_TLS_FLAGS_MORE_FRAGMENTS;
     141         [ +  - ]:         46 :                 if (data->tls_out_pos == 0) {
     142                 :         46 :                         flags |= EAP_TLS_FLAGS_LENGTH_INCLUDED;
     143                 :         46 :                         send_len -= 4;
     144                 :            :                 }
     145                 :            :         }
     146                 :            : 
     147                 :        209 :         plen = 1 + send_len;
     148         [ +  + ]:        209 :         if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED)
     149                 :         46 :                 plen += 4;
     150                 :            : 
     151                 :        209 :         req = eap_tls_msg_alloc(eap_type, plen, EAP_CODE_REQUEST, id);
     152         [ -  + ]:        209 :         if (req == NULL)
     153                 :          0 :                 return NULL;
     154                 :            : 
     155                 :        209 :         wpabuf_put_u8(req, flags); /* Flags */
     156         [ +  + ]:        209 :         if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED)
     157                 :         46 :                 wpabuf_put_be32(req, wpabuf_len(data->tls_out));
     158                 :            : 
     159                 :        209 :         wpabuf_put_data(req, wpabuf_head_u8(data->tls_out) + data->tls_out_pos,
     160                 :            :                         send_len);
     161                 :        209 :         data->tls_out_pos += send_len;
     162                 :            : 
     163         [ +  + ]:        209 :         if (data->tls_out_pos == wpabuf_len(data->tls_out)) {
     164                 :        163 :                 wpa_printf(MSG_DEBUG, "SSL: Sending out %lu bytes "
     165                 :            :                            "(message sent completely)",
     166                 :            :                            (unsigned long) send_len);
     167                 :        163 :                 wpabuf_free(data->tls_out);
     168                 :        163 :                 data->tls_out = NULL;
     169                 :        163 :                 data->tls_out_pos = 0;
     170                 :        163 :                 data->state = MSG;
     171                 :            :         } else {
     172                 :         46 :                 wpa_printf(MSG_DEBUG, "SSL: Sending out %lu bytes "
     173                 :            :                            "(%lu more to send)", (unsigned long) send_len,
     174                 :         46 :                            (unsigned long) wpabuf_len(data->tls_out) -
     175                 :         46 :                            data->tls_out_pos);
     176                 :         46 :                 data->state = WAIT_FRAG_ACK;
     177                 :            :         }
     178                 :            : 
     179                 :        209 :         return req;
     180                 :            : }
     181                 :            : 
     182                 :            : 
     183                 :          3 : struct wpabuf * eap_server_tls_build_ack(u8 id, int eap_type, int version)
     184                 :            : {
     185                 :            :         struct wpabuf *req;
     186                 :            : 
     187                 :          3 :         req = eap_tls_msg_alloc(eap_type, 1, EAP_CODE_REQUEST, id);
     188         [ -  + ]:          3 :         if (req == NULL)
     189                 :          0 :                 return NULL;
     190                 :          3 :         wpa_printf(MSG_DEBUG, "SSL: Building ACK");
     191                 :          3 :         wpabuf_put_u8(req, version); /* Flags */
     192                 :          3 :         return req;
     193                 :            : }
     194                 :            : 
     195                 :            : 
     196                 :          3 : static int eap_server_tls_process_cont(struct eap_ssl_data *data,
     197                 :            :                                        const u8 *buf, size_t len)
     198                 :            : {
     199                 :            :         /* Process continuation of a pending message */
     200         [ -  + ]:          3 :         if (len > wpabuf_tailroom(data->tls_in)) {
     201                 :          0 :                 wpa_printf(MSG_DEBUG, "SSL: Fragment overflow");
     202                 :          0 :                 return -1;
     203                 :            :         }
     204                 :            : 
     205                 :          3 :         wpabuf_put_data(data->tls_in, buf, len);
     206                 :          3 :         wpa_printf(MSG_DEBUG, "SSL: Received %lu bytes, waiting for %lu "
     207                 :            :                    "bytes more", (unsigned long) len,
     208                 :          3 :                    (unsigned long) wpabuf_tailroom(data->tls_in));
     209                 :            : 
     210                 :          3 :         return 0;
     211                 :            : }
     212                 :            : 
     213                 :            : 
     214                 :          3 : static int eap_server_tls_process_fragment(struct eap_ssl_data *data,
     215                 :            :                                            u8 flags, u32 message_length,
     216                 :            :                                            const u8 *buf, size_t len)
     217                 :            : {
     218                 :            :         /* Process a fragment that is not the last one of the message */
     219 [ +  - ][ -  + ]:          3 :         if (data->tls_in == NULL && !(flags & EAP_TLS_FLAGS_LENGTH_INCLUDED)) {
     220                 :          0 :                 wpa_printf(MSG_DEBUG, "SSL: No Message Length field in a "
     221                 :            :                            "fragmented packet");
     222                 :          0 :                 return -1;
     223                 :            :         }
     224                 :            : 
     225         [ +  - ]:          3 :         if (data->tls_in == NULL) {
     226                 :            :                 /* First fragment of the message */
     227                 :            : 
     228                 :            :                 /* Limit length to avoid rogue peers from causing large
     229                 :            :                  * memory allocations. */
     230         [ -  + ]:          3 :                 if (message_length > 65536) {
     231                 :          0 :                         wpa_printf(MSG_INFO, "SSL: Too long TLS fragment (size"
     232                 :            :                                    " over 64 kB)");
     233                 :          0 :                         return -1;
     234                 :            :                 }
     235                 :            : 
     236         [ -  + ]:          3 :                 if (len > message_length) {
     237                 :          0 :                         wpa_printf(MSG_INFO, "SSL: Too much data (%d bytes) in "
     238                 :            :                                    "first fragment of frame (TLS Message "
     239                 :            :                                    "Length %d bytes)",
     240                 :            :                                    (int) len, (int) message_length);
     241                 :          0 :                         return -1;
     242                 :            :                 }
     243                 :            : 
     244                 :          3 :                 data->tls_in = wpabuf_alloc(message_length);
     245         [ -  + ]:          3 :                 if (data->tls_in == NULL) {
     246                 :          0 :                         wpa_printf(MSG_DEBUG, "SSL: No memory for message");
     247                 :          0 :                         return -1;
     248                 :            :                 }
     249                 :          3 :                 wpabuf_put_data(data->tls_in, buf, len);
     250                 :          3 :                 wpa_printf(MSG_DEBUG, "SSL: Received %lu bytes in first "
     251                 :            :                            "fragment, waiting for %lu bytes more",
     252                 :            :                            (unsigned long) len,
     253                 :          3 :                            (unsigned long) wpabuf_tailroom(data->tls_in));
     254                 :            :         }
     255                 :            : 
     256                 :          3 :         return 0;
     257                 :            : }
     258                 :            : 
     259                 :            : 
     260                 :         94 : int eap_server_tls_phase1(struct eap_sm *sm, struct eap_ssl_data *data)
     261                 :            : {
     262         [ -  + ]:         94 :         if (data->tls_out) {
     263                 :            :                 /* This should not happen.. */
     264                 :          0 :                 wpa_printf(MSG_INFO, "SSL: pending tls_out data when "
     265                 :            :                            "processing new message");
     266                 :          0 :                 wpabuf_free(data->tls_out);
     267                 :            :                 WPA_ASSERT(data->tls_out == NULL);
     268                 :            :         }
     269                 :            : 
     270                 :         94 :         data->tls_out = tls_connection_server_handshake(sm->ssl_ctx,
     271                 :            :                                                         data->conn,
     272                 :         94 :                                                         data->tls_in, NULL);
     273         [ -  + ]:         94 :         if (data->tls_out == NULL) {
     274                 :          0 :                 wpa_printf(MSG_INFO, "SSL: TLS processing failed");
     275                 :          0 :                 return -1;
     276                 :            :         }
     277         [ +  + ]:         94 :         if (tls_connection_get_failed(sm->ssl_ctx, data->conn)) {
     278                 :            :                 /* TLS processing has failed - return error */
     279                 :          2 :                 wpa_printf(MSG_DEBUG, "SSL: Failed - tls_out available to "
     280                 :            :                            "report error");
     281                 :          2 :                 return -1;
     282                 :            :         }
     283                 :            : 
     284                 :         94 :         return 0;
     285                 :            : }
     286                 :            : 
     287                 :            : 
     288                 :        259 : static int eap_server_tls_reassemble(struct eap_ssl_data *data, u8 flags,
     289                 :            :                                      const u8 **pos, size_t *left)
     290                 :            : {
     291                 :        259 :         unsigned int tls_msg_len = 0;
     292                 :        259 :         const u8 *end = *pos + *left;
     293                 :            : 
     294         [ +  + ]:        259 :         if (flags & EAP_TLS_FLAGS_LENGTH_INCLUDED) {
     295         [ -  + ]:          7 :                 if (*left < 4) {
     296                 :          0 :                         wpa_printf(MSG_INFO, "SSL: Short frame with TLS "
     297                 :            :                                    "length");
     298                 :          0 :                         return -1;
     299                 :            :                 }
     300                 :          7 :                 tls_msg_len = WPA_GET_BE32(*pos);
     301                 :          7 :                 wpa_printf(MSG_DEBUG, "SSL: TLS Message Length: %d",
     302                 :            :                            tls_msg_len);
     303                 :          7 :                 *pos += 4;
     304                 :          7 :                 *left -= 4;
     305                 :            : 
     306         [ -  + ]:          7 :                 if (*left > tls_msg_len) {
     307                 :          0 :                         wpa_printf(MSG_INFO, "SSL: TLS Message Length (%d "
     308                 :            :                                    "bytes) smaller than this fragment (%d "
     309                 :          0 :                                    "bytes)", (int) tls_msg_len, (int) *left);
     310                 :          0 :                         return -1;
     311                 :            :                 }
     312                 :            :         }
     313                 :            : 
     314                 :        259 :         wpa_printf(MSG_DEBUG, "SSL: Received packet: Flags 0x%x "
     315                 :            :                    "Message Length %u", flags, tls_msg_len);
     316                 :            : 
     317         [ +  + ]:        259 :         if (data->state == WAIT_FRAG_ACK) {
     318         [ -  + ]:         46 :                 if (*left != 0) {
     319                 :          0 :                         wpa_printf(MSG_DEBUG, "SSL: Unexpected payload in "
     320                 :            :                                    "WAIT_FRAG_ACK state");
     321                 :          0 :                         return -1;
     322                 :            :                 }
     323                 :         46 :                 wpa_printf(MSG_DEBUG, "SSL: Fragment acknowledged");
     324                 :         46 :                 return 1;
     325                 :            :         }
     326                 :            : 
     327   [ +  +  -  + ]:        216 :         if (data->tls_in &&
     328                 :          3 :             eap_server_tls_process_cont(data, *pos, end - *pos) < 0)
     329                 :          0 :                 return -1;
     330                 :            :                 
     331         [ +  + ]:        213 :         if (flags & EAP_TLS_FLAGS_MORE_FRAGMENTS) {
     332         [ -  + ]:          3 :                 if (eap_server_tls_process_fragment(data, flags, tls_msg_len,
     333                 :          3 :                                                     *pos, end - *pos) < 0)
     334                 :          0 :                         return -1;
     335                 :            : 
     336                 :          3 :                 data->state = FRAG_ACK;
     337                 :          3 :                 return 1;
     338                 :            :         }
     339                 :            : 
     340         [ +  + ]:        210 :         if (data->state == FRAG_ACK) {
     341                 :          3 :                 wpa_printf(MSG_DEBUG, "SSL: All fragments received");
     342                 :          3 :                 data->state = MSG;
     343                 :            :         }
     344                 :            : 
     345         [ +  + ]:        210 :         if (data->tls_in == NULL) {
     346                 :            :                 /* Wrap unfragmented messages as wpabuf without extra copy */
     347                 :        207 :                 wpabuf_set(&data->tmpbuf, *pos, end - *pos);
     348                 :        207 :                 data->tls_in = &data->tmpbuf;
     349                 :            :         }
     350                 :            : 
     351                 :        259 :         return 0;
     352                 :            : }
     353                 :            : 
     354                 :            : 
     355                 :        271 : static void eap_server_tls_free_in_buf(struct eap_ssl_data *data)
     356                 :            : {
     357         [ +  + ]:        271 :         if (data->tls_in != &data->tmpbuf)
     358                 :         64 :                 wpabuf_free(data->tls_in);
     359                 :        271 :         data->tls_in = NULL;
     360                 :        271 : }
     361                 :            : 
     362                 :            : 
     363                 :         73 : struct wpabuf * eap_server_tls_encrypt(struct eap_sm *sm,
     364                 :            :                                        struct eap_ssl_data *data,
     365                 :            :                                        const struct wpabuf *plain)
     366                 :            : {
     367                 :            :         struct wpabuf *buf;
     368                 :            : 
     369                 :         73 :         buf = tls_connection_encrypt(sm->ssl_ctx, data->conn,
     370                 :            :                                      plain);
     371         [ -  + ]:         73 :         if (buf == NULL) {
     372                 :          0 :                 wpa_printf(MSG_INFO, "SSL: Failed to encrypt Phase 2 data");
     373                 :          0 :                 return NULL;
     374                 :            :         }
     375                 :            : 
     376                 :         73 :         return buf;
     377                 :            : }
     378                 :            : 
     379                 :            : 
     380                 :        259 : int eap_server_tls_process(struct eap_sm *sm, struct eap_ssl_data *data,
     381                 :            :                            struct wpabuf *respData, void *priv, int eap_type,
     382                 :            :                            int (*proc_version)(struct eap_sm *sm, void *priv,
     383                 :            :                                                int peer_version),
     384                 :            :                            void (*proc_msg)(struct eap_sm *sm, void *priv,
     385                 :            :                                             const struct wpabuf *respData))
     386                 :            : {
     387                 :            :         const u8 *pos;
     388                 :            :         u8 flags;
     389                 :            :         size_t left;
     390                 :        259 :         int ret, res = 0;
     391                 :            : 
     392         [ -  + ]:        259 :         if (eap_type == EAP_UNAUTH_TLS_TYPE)
     393                 :          0 :                 pos = eap_hdr_validate(EAP_VENDOR_UNAUTH_TLS,
     394                 :            :                                        EAP_VENDOR_TYPE_UNAUTH_TLS, respData,
     395                 :            :                                        &left);
     396                 :            :         else
     397                 :        259 :                 pos = eap_hdr_validate(EAP_VENDOR_IETF, eap_type, respData,
     398                 :            :                                        &left);
     399 [ +  - ][ -  + ]:        259 :         if (pos == NULL || left < 1)
     400                 :          0 :                 return 0; /* Should not happen - frame already validated */
     401                 :        259 :         flags = *pos++;
     402                 :        259 :         left--;
     403                 :        259 :         wpa_printf(MSG_DEBUG, "SSL: Received packet(len=%lu) - Flags 0x%02x",
     404                 :            :                    (unsigned long) wpabuf_len(respData), flags);
     405                 :            : 
     406   [ +  +  -  + ]:        503 :         if (proc_version &&
     407                 :        244 :             proc_version(sm, priv, flags & EAP_TLS_VERSION_MASK) < 0)
     408                 :          0 :                 return -1;
     409                 :            : 
     410                 :        259 :         ret = eap_server_tls_reassemble(data, flags, &pos, &left);
     411         [ -  + ]:        259 :         if (ret < 0) {
     412                 :          0 :                 res = -1;
     413                 :          0 :                 goto done;
     414         [ +  + ]:        259 :         } else if (ret == 1)
     415                 :         49 :                 return 0;
     416                 :            : 
     417         [ +  - ]:        210 :         if (proc_msg)
     418                 :        210 :                 proc_msg(sm, priv, respData);
     419                 :            : 
     420         [ -  + ]:        210 :         if (tls_connection_get_write_alerts(sm->ssl_ctx, data->conn) > 1) {
     421                 :          0 :                 wpa_printf(MSG_INFO, "SSL: Locally detected fatal error in "
     422                 :            :                            "TLS processing");
     423                 :          0 :                 res = -1;
     424                 :            :         }
     425                 :            : 
     426                 :            : done:
     427                 :        210 :         eap_server_tls_free_in_buf(data);
     428                 :            : 
     429                 :        259 :         return res;
     430                 :            : }

Generated by: LCOV version 1.9