LCOV - code coverage report
Current view: top level - src/wps - ndef.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1401264779 Lines: 77 94 81.9 %
Date: 2014-05-28 Functions: 9 9 100.0 %

          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             : static char p2p_handover_type[] = "application/vnd.wfa.p2p";
      34             : 
      35          65 : static int ndef_parse_record(const u8 *data, u32 size,
      36             :                              struct ndef_record *record)
      37             : {
      38          65 :         const u8 *pos = data + 1;
      39             : 
      40          65 :         if (size < 2)
      41           0 :                 return -1;
      42          65 :         record->type_length = *pos++;
      43          65 :         if (data[0] & FLAG_SHORT_RECORD) {
      44          62 :                 if (size < 3)
      45           0 :                         return -1;
      46          62 :                 record->payload_length = *pos++;
      47             :         } else {
      48           3 :                 if (size < 6)
      49           3 :                         return -1;
      50           0 :                 record->payload_length = ntohl(*(u32 *)pos);
      51           0 :                 pos += sizeof(u32);
      52             :         }
      53             : 
      54          62 :         if (data[0] & FLAG_ID_LENGTH_PRESENT) {
      55           0 :                 if ((int) size < pos - data + 1)
      56           0 :                         return -1;
      57           0 :                 record->id_length = *pos++;
      58             :         } else
      59          62 :                 record->id_length = 0;
      60             : 
      61          62 :         record->type = record->type_length == 0 ? NULL : pos;
      62          62 :         pos += record->type_length;
      63             : 
      64          62 :         record->id = record->id_length == 0 ? NULL : pos;
      65          62 :         pos += record->id_length;
      66             : 
      67          62 :         record->payload = record->payload_length == 0 ? NULL : pos;
      68          62 :         pos += record->payload_length;
      69             : 
      70          62 :         record->total_length = pos - data;
      71          62 :         if (record->total_length > size)
      72           0 :                 return -1;
      73          62 :         return 0;
      74             : }
      75             : 
      76             : 
      77          65 : static struct wpabuf * ndef_parse_records(const struct wpabuf *buf,
      78             :                                           int (*filter)(struct ndef_record *))
      79             : {
      80             :         struct ndef_record record;
      81          65 :         int len = wpabuf_len(buf);
      82          65 :         const u8 *data = wpabuf_head(buf);
      83             : 
      84         138 :         while (len > 0) {
      85          65 :                 if (ndef_parse_record(data, len, &record) < 0) {
      86           3 :                         wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
      87           3 :                         return NULL;
      88             :                 }
      89          62 :                 if (filter == NULL || filter(&record))
      90          54 :                         return wpabuf_alloc_copy(record.payload,
      91          54 :                                                  record.payload_length);
      92           8 :                 data += record.total_length;
      93           8 :                 len -= record.total_length;
      94             :         }
      95           8 :         wpa_printf(MSG_ERROR, "NDEF : Record not found");
      96           8 :         return NULL;
      97             : }
      98             : 
      99             : 
     100          67 : static struct wpabuf * ndef_build_record(u8 flags, void *type,
     101             :                                          u8 type_length, void *id,
     102             :                                          u8 id_length,
     103             :                                          const struct wpabuf *payload)
     104             : {
     105             :         struct wpabuf *record;
     106             :         size_t total_len;
     107             :         int short_record;
     108             :         u8 local_flag;
     109          67 :         size_t payload_length = wpabuf_len(payload);
     110             : 
     111          67 :         short_record = payload_length < 256 ? 1 : 0;
     112             : 
     113          67 :         total_len = 2; /* flag + type length */
     114             :         /* payload length */
     115          67 :         total_len += short_record ? sizeof(u8) : sizeof(u32);
     116          67 :         if (id_length > 0)
     117           0 :                 total_len += 1;
     118          67 :         total_len += type_length + id_length + payload_length;
     119          67 :         record = wpabuf_alloc(total_len);
     120          67 :         if (record == NULL) {
     121           0 :                 wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
     122             :                            "record for build");
     123           0 :                 return NULL;
     124             :         }
     125             : 
     126          67 :         local_flag = flags;
     127          67 :         if (id_length > 0)
     128           0 :                 local_flag |= FLAG_ID_LENGTH_PRESENT;
     129          67 :         if (short_record)
     130          67 :                 local_flag |= FLAG_SHORT_RECORD;
     131          67 :         wpabuf_put_u8(record, local_flag);
     132             : 
     133          67 :         wpabuf_put_u8(record, type_length);
     134             : 
     135          67 :         if (short_record)
     136          67 :                 wpabuf_put_u8(record, payload_length);
     137             :         else
     138           0 :                 wpabuf_put_be32(record, payload_length);
     139             : 
     140          67 :         if (id_length > 0)
     141           0 :                 wpabuf_put_u8(record, id_length);
     142          67 :         wpabuf_put_data(record, type, type_length);
     143          67 :         wpabuf_put_data(record, id, id_length);
     144          67 :         wpabuf_put_buf(record, payload);
     145          67 :         return record;
     146             : }
     147             : 
     148             : 
     149          38 : static int wifi_filter(struct ndef_record *record)
     150             : {
     151          76 :         if (record->type == NULL ||
     152          38 :             record->type_length != os_strlen(wifi_handover_type))
     153           0 :                 return 0;
     154          38 :         if (os_memcmp(record->type, wifi_handover_type,
     155             :                       os_strlen(wifi_handover_type)) != 0)
     156           8 :                 return 0;
     157          30 :         return 1;
     158             : }
     159             : 
     160             : 
     161          40 : struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf)
     162             : {
     163          40 :         return ndef_parse_records(buf, wifi_filter);
     164             : }
     165             : 
     166             : 
     167          41 : struct wpabuf * ndef_build_wifi(const struct wpabuf *buf)
     168             : {
     169          41 :         return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
     170             :                                  FLAG_TNF_RFC2046, wifi_handover_type,
     171          41 :                                  os_strlen(wifi_handover_type), NULL, 0, buf);
     172             : }
     173             : 
     174             : 
     175          24 : static int p2p_filter(struct ndef_record *record)
     176             : {
     177          48 :         if (record->type == NULL ||
     178          24 :             record->type_length != os_strlen(p2p_handover_type))
     179           0 :                 return 0;
     180          24 :         if (os_memcmp(record->type, p2p_handover_type,
     181             :                       os_strlen(p2p_handover_type)) != 0)
     182           0 :                 return 0;
     183          24 :         return 1;
     184             : }
     185             : 
     186             : 
     187          25 : struct wpabuf * ndef_parse_p2p(const struct wpabuf *buf)
     188             : {
     189          25 :         return ndef_parse_records(buf, p2p_filter);
     190             : }
     191             : 
     192             : 
     193          26 : struct wpabuf * ndef_build_p2p(const struct wpabuf *buf)
     194             : {
     195          26 :         return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
     196             :                                  FLAG_TNF_RFC2046, p2p_handover_type,
     197          26 :                                  os_strlen(p2p_handover_type), NULL, 0, buf);
     198             : }

Generated by: LCOV version 1.10