blob: d45dfc8efee645de1fc217656b1ffb4923ac56f0 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * NDEF(NFC Data Exchange Format) routines for Wi-Fi Protected Setup
3 * Reference is "NFCForum-TS-NDEF_1.0 2006-07-24".
Dmitry Shmidt04949592012-07-19 12:16:46 -07004 * Copyright (c) 2009-2012, Masashi Honma <masashi.honma@gmail.com>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidt04949592012-07-19 12:16:46 -07006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11#include "common.h"
12#include "wps/wps.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013
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)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080019#define FLAG_TNF_NFC_FORUM (0x01)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#define FLAG_TNF_RFC2046 (0x02)
21
22struct ndef_record {
Dmitry Shmidt04949592012-07-19 12:16:46 -070023 const u8 *type;
24 const u8 *id;
25 const u8 *payload;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026 u8 type_length;
27 u8 id_length;
28 u32 payload_length;
29 u32 total_length;
30};
31
32static char wifi_handover_type[] = "application/vnd.wfa.wsc";
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080033static char p2p_handover_type[] = "application/vnd.wfa.p2p";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034
Dmitry Shmidt04949592012-07-19 12:16:46 -070035static int ndef_parse_record(const u8 *data, u32 size,
36 struct ndef_record *record)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037{
Dmitry Shmidt04949592012-07-19 12:16:46 -070038 const u8 *pos = data + 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039
40 if (size < 2)
41 return -1;
42 record->type_length = *pos++;
43 if (data[0] & FLAG_SHORT_RECORD) {
44 if (size < 3)
45 return -1;
46 record->payload_length = *pos++;
47 } else {
48 if (size < 6)
49 return -1;
50 record->payload_length = ntohl(*(u32 *)pos);
51 pos += sizeof(u32);
52 }
53
54 if (data[0] & FLAG_ID_LENGTH_PRESENT) {
55 if ((int) size < pos - data + 1)
56 return -1;
57 record->id_length = *pos++;
58 } else
59 record->id_length = 0;
60
61 record->type = record->type_length == 0 ? NULL : pos;
62 pos += record->type_length;
63
64 record->id = record->id_length == 0 ? NULL : pos;
65 pos += record->id_length;
66
67 record->payload = record->payload_length == 0 ? NULL : pos;
68 pos += record->payload_length;
69
70 record->total_length = pos - data;
71 if (record->total_length > size)
72 return -1;
73 return 0;
74}
75
76
Dmitry Shmidt04949592012-07-19 12:16:46 -070077static struct wpabuf * ndef_parse_records(const struct wpabuf *buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078 int (*filter)(struct ndef_record *))
79{
80 struct ndef_record record;
81 int len = wpabuf_len(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -070082 const u8 *data = wpabuf_head(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083
84 while (len > 0) {
85 if (ndef_parse_record(data, len, &record) < 0) {
86 wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
87 return NULL;
88 }
89 if (filter == NULL || filter(&record))
90 return wpabuf_alloc_copy(record.payload,
91 record.payload_length);
92 data += record.total_length;
93 len -= record.total_length;
94 }
95 wpa_printf(MSG_ERROR, "NDEF : Record not found");
96 return NULL;
97}
98
99
100static struct wpabuf * ndef_build_record(u8 flags, void *type,
101 u8 type_length, void *id,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700102 u8 id_length,
103 const struct wpabuf *payload)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104{
105 struct wpabuf *record;
106 size_t total_len;
107 int short_record;
108 u8 local_flag;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700109 size_t payload_length = wpabuf_len(payload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110
111 short_record = payload_length < 256 ? 1 : 0;
112
113 total_len = 2; /* flag + type length */
114 /* payload length */
115 total_len += short_record ? sizeof(u8) : sizeof(u32);
116 if (id_length > 0)
117 total_len += 1;
118 total_len += type_length + id_length + payload_length;
119 record = wpabuf_alloc(total_len);
120 if (record == NULL) {
121 wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
122 "record for build");
123 return NULL;
124 }
125
126 local_flag = flags;
127 if (id_length > 0)
128 local_flag |= FLAG_ID_LENGTH_PRESENT;
129 if (short_record)
130 local_flag |= FLAG_SHORT_RECORD;
131 wpabuf_put_u8(record, local_flag);
132
133 wpabuf_put_u8(record, type_length);
134
135 if (short_record)
136 wpabuf_put_u8(record, payload_length);
137 else
138 wpabuf_put_be32(record, payload_length);
139
140 if (id_length > 0)
141 wpabuf_put_u8(record, id_length);
142 wpabuf_put_data(record, type, type_length);
143 wpabuf_put_data(record, id, id_length);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700144 wpabuf_put_buf(record, payload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145 return record;
146}
147
148
149static int wifi_filter(struct ndef_record *record)
150{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800151 if (record->type == NULL ||
152 record->type_length != os_strlen(wifi_handover_type))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 return 0;
154 if (os_memcmp(record->type, wifi_handover_type,
155 os_strlen(wifi_handover_type)) != 0)
156 return 0;
157 return 1;
158}
159
160
Dmitry Shmidt04949592012-07-19 12:16:46 -0700161struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162{
163 return ndef_parse_records(buf, wifi_filter);
164}
165
166
Dmitry Shmidt04949592012-07-19 12:16:46 -0700167struct wpabuf * ndef_build_wifi(const struct wpabuf *buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168{
169 return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
170 FLAG_TNF_RFC2046, wifi_handover_type,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700171 os_strlen(wifi_handover_type), NULL, 0, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800173
174
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800175static int p2p_filter(struct ndef_record *record)
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800176{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800177 if (record->type == NULL ||
178 record->type_length != os_strlen(p2p_handover_type))
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800179 return 0;
180 if (os_memcmp(record->type, p2p_handover_type,
181 os_strlen(p2p_handover_type)) != 0)
182 return 0;
183 return 1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800184}
185
186
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800187struct wpabuf * ndef_parse_p2p(const struct wpabuf *buf)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800188{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800189 return ndef_parse_records(buf, p2p_filter);
190}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800191
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800192
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800193struct wpabuf * ndef_build_p2p(const struct wpabuf *buf)
194{
195 return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
196 FLAG_TNF_RFC2046, p2p_handover_type,
197 os_strlen(p2p_handover_type), NULL, 0, buf);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800198}