• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "wps/wps_i.h"
14 
15 #define FLAG_MESSAGE_BEGIN (1 << 7)
16 #define FLAG_MESSAGE_END (1 << 6)
17 #define FLAG_CHUNK (1 << 5)
18 #define FLAG_SHORT_RECORD (1 << 4)
19 #define FLAG_ID_LENGTH_PRESENT (1 << 3)
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 
ndef_parse_record(const u8 * data,u32 size,struct ndef_record * record)34 static int ndef_parse_record(const u8 *data, u32 size,
35 			     struct ndef_record *record)
36 {
37 	const u8 *pos = data + 1;
38 
39 	if (size < 2)
40 		return -1;
41 	record->type_length = *pos++;
42 	if (data[0] & FLAG_SHORT_RECORD) {
43 		if (size < 3)
44 			return -1;
45 		record->payload_length = *pos++;
46 	} else {
47 		if (size < 6)
48 			return -1;
49 		record->payload_length = ntohl(*(u32 *)pos);
50 		pos += sizeof(u32);
51 	}
52 
53 	if (data[0] & FLAG_ID_LENGTH_PRESENT) {
54 		if ((int) size < pos - data + 1)
55 			return -1;
56 		record->id_length = *pos++;
57 	} else
58 		record->id_length = 0;
59 
60 	record->type = record->type_length == 0 ? NULL : pos;
61 	pos += record->type_length;
62 
63 	record->id = record->id_length == 0 ? NULL : pos;
64 	pos += record->id_length;
65 
66 	record->payload = record->payload_length == 0 ? NULL : pos;
67 	pos += record->payload_length;
68 
69 	record->total_length = pos - data;
70 	if (record->total_length > size)
71 		return -1;
72 	return 0;
73 }
74 
75 
ndef_parse_records(const struct wpabuf * buf,int (* filter)(struct ndef_record *))76 static struct wpabuf * ndef_parse_records(const struct wpabuf *buf,
77 					  int (*filter)(struct ndef_record *))
78 {
79 	struct ndef_record record;
80 	int len = wpabuf_len(buf);
81 	const u8 *data = wpabuf_head(buf);
82 
83 	while (len > 0) {
84 		if (ndef_parse_record(data, len, &record) < 0) {
85 			wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
86 			return NULL;
87 		}
88 		if (filter == NULL || filter(&record))
89 			return wpabuf_alloc_copy(record.payload,
90 						 record.payload_length);
91 		data += record.total_length;
92 		len -= record.total_length;
93 	}
94 	wpa_printf(MSG_ERROR, "NDEF : Record not found");
95 	return NULL;
96 }
97 
98 
ndef_build_record(u8 flags,void * type,u8 type_length,void * id,u8 id_length,const struct wpabuf * payload)99 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 	size_t payload_length = wpabuf_len(payload);
109 
110 	short_record = payload_length < 256 ? 1 : 0;
111 
112 	total_len = 2; /* flag + type length */
113 	/* payload length */
114 	total_len += short_record ? sizeof(u8) : sizeof(u32);
115 	if (id_length > 0)
116 		total_len += 1;
117 	total_len += type_length + id_length + payload_length;
118 	record = wpabuf_alloc(total_len);
119 	if (record == NULL) {
120 		wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
121 			   "record for build");
122 		return NULL;
123 	}
124 
125 	local_flag = flags;
126 	if (id_length > 0)
127 		local_flag |= FLAG_ID_LENGTH_PRESENT;
128 	if (short_record)
129 		local_flag |= FLAG_SHORT_RECORD;
130 	wpabuf_put_u8(record, local_flag);
131 
132 	wpabuf_put_u8(record, type_length);
133 
134 	if (short_record)
135 		wpabuf_put_u8(record, payload_length);
136 	else
137 		wpabuf_put_be32(record, payload_length);
138 
139 	if (id_length > 0)
140 		wpabuf_put_u8(record, id_length);
141 	wpabuf_put_data(record, type, type_length);
142 	wpabuf_put_data(record, id, id_length);
143 	wpabuf_put_buf(record, payload);
144 	return record;
145 }
146 
147 
wifi_filter(struct ndef_record * record)148 static int wifi_filter(struct ndef_record *record)
149 {
150 	if (record->type_length != os_strlen(wifi_handover_type))
151 		return 0;
152 	if (os_memcmp(record->type, wifi_handover_type,
153 		      os_strlen(wifi_handover_type)) != 0)
154 		return 0;
155 	return 1;
156 }
157 
158 
ndef_parse_wifi(const struct wpabuf * buf)159 struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf)
160 {
161 	return ndef_parse_records(buf, wifi_filter);
162 }
163 
164 
ndef_build_wifi(const struct wpabuf * buf)165 struct wpabuf * ndef_build_wifi(const struct wpabuf *buf)
166 {
167 	return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
168 				 FLAG_TNF_RFC2046, wifi_handover_type,
169 				 os_strlen(wifi_handover_type), NULL, 0, buf);
170 }
171