LCOV - code coverage report
Current view: top level - src/wps - ndef.c (source / functions) Hit Total Coverage
Test: hostapd hwsim test run 1388338050 Lines: 58 132 43.9 %
Date: 2013-12-29 Functions: 6 8 75.0 %
Branches: 21 64 32.8 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * NDEF(NFC Data Exchange Format) routines for Wi-Fi Protected Setup
       3                 :            :  *   Reference is "NFCForum-TS-NDEF_1.0 2006-07-24".
       4                 :            :  * Copyright (c) 2009-2012, Masashi Honma <masashi.honma@gmail.com>
       5                 :            :  *
       6                 :            :  * This software may be distributed under the terms of the BSD license.
       7                 :            :  * See README for more details.
       8                 :            :  */
       9                 :            : 
      10                 :            : #include "includes.h"
      11                 :            : #include "common.h"
      12                 :            : #include "wps/wps.h"
      13                 :            : 
      14                 :            : #define FLAG_MESSAGE_BEGIN (1 << 7)
      15                 :            : #define FLAG_MESSAGE_END (1 << 6)
      16                 :            : #define FLAG_CHUNK (1 << 5)
      17                 :            : #define FLAG_SHORT_RECORD (1 << 4)
      18                 :            : #define FLAG_ID_LENGTH_PRESENT (1 << 3)
      19                 :            : #define FLAG_TNF_NFC_FORUM (0x01)
      20                 :            : #define FLAG_TNF_RFC2046 (0x02)
      21                 :            : 
      22                 :            : struct ndef_record {
      23                 :            :         const u8 *type;
      24                 :            :         const u8 *id;
      25                 :            :         const u8 *payload;
      26                 :            :         u8 type_length;
      27                 :            :         u8 id_length;
      28                 :            :         u32 payload_length;
      29                 :            :         u32 total_length;
      30                 :            : };
      31                 :            : 
      32                 :            : static char wifi_handover_type[] = "application/vnd.wfa.wsc";
      33                 :            : 
      34                 :          2 : static int ndef_parse_record(const u8 *data, u32 size,
      35                 :            :                              struct ndef_record *record)
      36                 :            : {
      37                 :          2 :         const u8 *pos = data + 1;
      38                 :            : 
      39         [ -  + ]:          2 :         if (size < 2)
      40                 :          0 :                 return -1;
      41                 :          2 :         record->type_length = *pos++;
      42         [ +  - ]:          2 :         if (data[0] & FLAG_SHORT_RECORD) {
      43         [ -  + ]:          2 :                 if (size < 3)
      44                 :          0 :                         return -1;
      45                 :          2 :                 record->payload_length = *pos++;
      46                 :            :         } else {
      47         [ #  # ]:          0 :                 if (size < 6)
      48                 :          0 :                         return -1;
      49                 :          0 :                 record->payload_length = ntohl(*(u32 *)pos);
      50                 :          0 :                 pos += sizeof(u32);
      51                 :            :         }
      52                 :            : 
      53         [ -  + ]:          2 :         if (data[0] & FLAG_ID_LENGTH_PRESENT) {
      54         [ #  # ]:          0 :                 if ((int) size < pos - data + 1)
      55                 :          0 :                         return -1;
      56                 :          0 :                 record->id_length = *pos++;
      57                 :            :         } else
      58                 :          2 :                 record->id_length = 0;
      59                 :            : 
      60         [ +  - ]:          2 :         record->type = record->type_length == 0 ? NULL : pos;
      61                 :          2 :         pos += record->type_length;
      62                 :            : 
      63         [ -  + ]:          2 :         record->id = record->id_length == 0 ? NULL : pos;
      64                 :          2 :         pos += record->id_length;
      65                 :            : 
      66         [ +  - ]:          2 :         record->payload = record->payload_length == 0 ? NULL : pos;
      67                 :          2 :         pos += record->payload_length;
      68                 :            : 
      69                 :          2 :         record->total_length = pos - data;
      70         [ -  + ]:          2 :         if (record->total_length > size)
      71                 :          0 :                 return -1;
      72                 :          2 :         return 0;
      73                 :            : }
      74                 :            : 
      75                 :            : 
      76                 :          2 : static struct wpabuf * ndef_parse_records(const struct wpabuf *buf,
      77                 :            :                                           int (*filter)(struct ndef_record *))
      78                 :            : {
      79                 :            :         struct ndef_record record;
      80                 :          2 :         int len = wpabuf_len(buf);
      81                 :          2 :         const u8 *data = wpabuf_head(buf);
      82                 :            : 
      83         [ +  - ]:          2 :         while (len > 0) {
      84         [ -  + ]:          2 :                 if (ndef_parse_record(data, len, &record) < 0) {
      85                 :          0 :                         wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
      86                 :          0 :                         return NULL;
      87                 :            :                 }
      88 [ +  - ][ +  - ]:          2 :                 if (filter == NULL || filter(&record))
      89                 :          2 :                         return wpabuf_alloc_copy(record.payload,
      90                 :          2 :                                                  record.payload_length);
      91                 :          0 :                 data += record.total_length;
      92                 :          0 :                 len -= record.total_length;
      93                 :            :         }
      94                 :          0 :         wpa_printf(MSG_ERROR, "NDEF : Record not found");
      95                 :          2 :         return NULL;
      96                 :            : }
      97                 :            : 
      98                 :            : 
      99                 :          4 : static struct wpabuf * ndef_build_record(u8 flags, void *type,
     100                 :            :                                          u8 type_length, void *id,
     101                 :            :                                          u8 id_length,
     102                 :            :                                          const struct wpabuf *payload)
     103                 :            : {
     104                 :            :         struct wpabuf *record;
     105                 :            :         size_t total_len;
     106                 :            :         int short_record;
     107                 :            :         u8 local_flag;
     108                 :          4 :         size_t payload_length = wpabuf_len(payload);
     109                 :            : 
     110                 :          4 :         short_record = payload_length < 256 ? 1 : 0;
     111                 :            : 
     112                 :          4 :         total_len = 2; /* flag + type length */
     113                 :            :         /* payload length */
     114         [ +  - ]:          4 :         total_len += short_record ? sizeof(u8) : sizeof(u32);
     115         [ -  + ]:          4 :         if (id_length > 0)
     116                 :          0 :                 total_len += 1;
     117                 :          4 :         total_len += type_length + id_length + payload_length;
     118                 :          4 :         record = wpabuf_alloc(total_len);
     119         [ -  + ]:          4 :         if (record == NULL) {
     120                 :          0 :                 wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
     121                 :            :                            "record for build");
     122                 :          0 :                 return NULL;
     123                 :            :         }
     124                 :            : 
     125                 :          4 :         local_flag = flags;
     126         [ -  + ]:          4 :         if (id_length > 0)
     127                 :          0 :                 local_flag |= FLAG_ID_LENGTH_PRESENT;
     128         [ +  - ]:          4 :         if (short_record)
     129                 :          4 :                 local_flag |= FLAG_SHORT_RECORD;
     130                 :          4 :         wpabuf_put_u8(record, local_flag);
     131                 :            : 
     132                 :          4 :         wpabuf_put_u8(record, type_length);
     133                 :            : 
     134         [ +  - ]:          4 :         if (short_record)
     135                 :          4 :                 wpabuf_put_u8(record, payload_length);
     136                 :            :         else
     137                 :          0 :                 wpabuf_put_be32(record, payload_length);
     138                 :            : 
     139         [ -  + ]:          4 :         if (id_length > 0)
     140                 :          0 :                 wpabuf_put_u8(record, id_length);
     141                 :          4 :         wpabuf_put_data(record, type, type_length);
     142                 :          4 :         wpabuf_put_data(record, id, id_length);
     143                 :          4 :         wpabuf_put_buf(record, payload);
     144                 :          4 :         return record;
     145                 :            : }
     146                 :            : 
     147                 :            : 
     148                 :          2 : static int wifi_filter(struct ndef_record *record)
     149                 :            : {
     150         [ -  + ]:          2 :         if (record->type_length != os_strlen(wifi_handover_type))
     151                 :          0 :                 return 0;
     152         [ -  + ]:          2 :         if (os_memcmp(record->type, wifi_handover_type,
     153                 :            :                       os_strlen(wifi_handover_type)) != 0)
     154                 :          0 :                 return 0;
     155                 :          2 :         return 1;
     156                 :            : }
     157                 :            : 
     158                 :            : 
     159                 :          2 : struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf)
     160                 :            : {
     161                 :          2 :         return ndef_parse_records(buf, wifi_filter);
     162                 :            : }
     163                 :            : 
     164                 :            : 
     165                 :          4 : struct wpabuf * ndef_build_wifi(const struct wpabuf *buf)
     166                 :            : {
     167                 :          4 :         return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
     168                 :            :                                  FLAG_TNF_RFC2046, wifi_handover_type,
     169                 :          4 :                                  os_strlen(wifi_handover_type), NULL, 0, buf);
     170                 :            : }
     171                 :            : 
     172                 :            : 
     173                 :          0 : struct wpabuf * ndef_build_wifi_hc(int begin)
     174                 :            : {
     175                 :            :         struct wpabuf *hc, *carrier;
     176                 :            : 
     177                 :          0 :         carrier = wpabuf_alloc(2 + os_strlen(wifi_handover_type));
     178         [ #  # ]:          0 :         if (carrier == NULL)
     179                 :          0 :                 return NULL;
     180                 :          0 :         wpabuf_put_u8(carrier, 0x02); /* Carrier Type Format */
     181                 :          0 :         wpabuf_put_u8(carrier, os_strlen(wifi_handover_type));
     182                 :          0 :         wpabuf_put_str(carrier, wifi_handover_type);
     183                 :            : 
     184         [ #  # ]:          0 :         hc = ndef_build_record((begin ? FLAG_MESSAGE_BEGIN : 0) |
     185                 :            :                                FLAG_MESSAGE_END | FLAG_TNF_NFC_FORUM, "Hc", 2,
     186                 :            :                                "0", 1, carrier);
     187                 :          0 :         wpabuf_free(carrier);
     188                 :            : 
     189                 :          0 :         return hc;
     190                 :            : }
     191                 :            : 
     192                 :            : 
     193                 :          0 : struct wpabuf * ndef_build_wifi_hr(void)
     194                 :            : {
     195                 :            :         struct wpabuf *rn, *cr, *ac_payload, *ac, *hr_payload, *hr;
     196                 :            :         struct wpabuf *hc;
     197                 :            : 
     198                 :          0 :         rn = wpabuf_alloc(2);
     199         [ #  # ]:          0 :         if (rn == NULL)
     200                 :          0 :                 return NULL;
     201                 :          0 :         wpabuf_put_be16(rn, os_random() & 0xffff);
     202                 :            : 
     203                 :          0 :         cr = ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_TNF_NFC_FORUM, "cr", 2,
     204                 :            :                                NULL, 0, rn);
     205                 :          0 :         wpabuf_free(rn);
     206                 :            : 
     207         [ #  # ]:          0 :         if (cr == NULL)
     208                 :          0 :                 return NULL;
     209                 :            : 
     210                 :          0 :         ac_payload = wpabuf_alloc(4);
     211         [ #  # ]:          0 :         if (ac_payload == NULL) {
     212                 :          0 :                 wpabuf_free(cr);
     213                 :          0 :                 return NULL;
     214                 :            :         }
     215                 :          0 :         wpabuf_put_u8(ac_payload, 0x01); /* Carrier Flags: CRS=1 "active" */
     216                 :          0 :         wpabuf_put_u8(ac_payload, 0x01); /* Carrier Data Reference Length */
     217                 :          0 :         wpabuf_put_u8(ac_payload, '0'); /* Carrier Data Reference: "0" */
     218                 :          0 :         wpabuf_put_u8(ac_payload, 0); /* Aux Data Reference Count */
     219                 :            : 
     220                 :          0 :         ac = ndef_build_record(FLAG_MESSAGE_END | FLAG_TNF_NFC_FORUM, "ac", 2,
     221                 :            :                                NULL, 0, ac_payload);
     222                 :          0 :         wpabuf_free(ac_payload);
     223         [ #  # ]:          0 :         if (ac == NULL) {
     224                 :          0 :                 wpabuf_free(cr);
     225                 :          0 :                 return NULL;
     226                 :            :         }
     227                 :            : 
     228                 :          0 :         hr_payload = wpabuf_alloc(1 + wpabuf_len(cr) + wpabuf_len(ac));
     229         [ #  # ]:          0 :         if (hr_payload == NULL) {
     230                 :          0 :                 wpabuf_free(cr);
     231                 :          0 :                 wpabuf_free(ac);
     232                 :          0 :                 return NULL;
     233                 :            :         }
     234                 :            : 
     235                 :          0 :         wpabuf_put_u8(hr_payload, 0x12); /* Connection Handover Version 1.2 */
     236                 :          0 :         wpabuf_put_buf(hr_payload, cr);
     237                 :          0 :         wpabuf_put_buf(hr_payload, ac);
     238                 :          0 :         wpabuf_free(cr);
     239                 :          0 :         wpabuf_free(ac);
     240                 :            : 
     241                 :          0 :         hr = ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_TNF_NFC_FORUM, "Hr", 2,
     242                 :            :                                NULL, 0, hr_payload);
     243                 :          0 :         wpabuf_free(hr_payload);
     244         [ #  # ]:          0 :         if (hr == NULL)
     245                 :          0 :                 return NULL;
     246                 :            : 
     247                 :          0 :         hc = ndef_build_wifi_hc(0);
     248         [ #  # ]:          0 :         if (hc == NULL) {
     249                 :          0 :                 wpabuf_free(hr);
     250                 :          0 :                 return NULL;
     251                 :            :         }
     252                 :            : 
     253                 :          0 :         return wpabuf_concat(hr, hc);
     254                 :            : }

Generated by: LCOV version 1.9