blob: 63f0d527d5874e3e5636d7c4d7108d4dba158b10 [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
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070032static const char wifi_handover_type[] = "application/vnd.wfa.wsc";
33static const 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 {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080048 u32 len;
49
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050 if (size < 6)
51 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080052 len = WPA_GET_BE32(pos);
53 if (len > size - 6 || len > 20000)
54 return -1;
55 record->payload_length = len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056 pos += sizeof(u32);
57 }
58
59 if (data[0] & FLAG_ID_LENGTH_PRESENT) {
60 if ((int) size < pos - data + 1)
61 return -1;
62 record->id_length = *pos++;
63 } else
64 record->id_length = 0;
65
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000066 if (record->type_length > data + size - pos)
67 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068 record->type = record->type_length == 0 ? NULL : pos;
69 pos += record->type_length;
70
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000071 if (record->id_length > data + size - pos)
72 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073 record->id = record->id_length == 0 ? NULL : pos;
74 pos += record->id_length;
75
Sunil Ravi38ad1ed2023-01-17 23:58:31 +000076 if (record->payload_length > (size_t) (data + size - pos))
77 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070078 record->payload = record->payload_length == 0 ? NULL : pos;
79 pos += record->payload_length;
80
81 record->total_length = pos - data;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080082 if (record->total_length > size ||
83 record->total_length < record->payload_length)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084 return -1;
85 return 0;
86}
87
88
Dmitry Shmidt04949592012-07-19 12:16:46 -070089static struct wpabuf * ndef_parse_records(const struct wpabuf *buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090 int (*filter)(struct ndef_record *))
91{
92 struct ndef_record record;
93 int len = wpabuf_len(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -070094 const u8 *data = wpabuf_head(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095
96 while (len > 0) {
97 if (ndef_parse_record(data, len, &record) < 0) {
98 wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
99 return NULL;
100 }
101 if (filter == NULL || filter(&record))
102 return wpabuf_alloc_copy(record.payload,
103 record.payload_length);
104 data += record.total_length;
105 len -= record.total_length;
106 }
107 wpa_printf(MSG_ERROR, "NDEF : Record not found");
108 return NULL;
109}
110
111
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700112static struct wpabuf * ndef_build_record(u8 flags, const void *type,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 u8 type_length, void *id,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700114 u8 id_length,
115 const struct wpabuf *payload)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116{
117 struct wpabuf *record;
118 size_t total_len;
119 int short_record;
120 u8 local_flag;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700121 size_t payload_length = wpabuf_len(payload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122
123 short_record = payload_length < 256 ? 1 : 0;
124
125 total_len = 2; /* flag + type length */
126 /* payload length */
127 total_len += short_record ? sizeof(u8) : sizeof(u32);
128 if (id_length > 0)
129 total_len += 1;
130 total_len += type_length + id_length + payload_length;
131 record = wpabuf_alloc(total_len);
132 if (record == NULL) {
133 wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
134 "record for build");
135 return NULL;
136 }
137
138 local_flag = flags;
139 if (id_length > 0)
140 local_flag |= FLAG_ID_LENGTH_PRESENT;
141 if (short_record)
142 local_flag |= FLAG_SHORT_RECORD;
143 wpabuf_put_u8(record, local_flag);
144
145 wpabuf_put_u8(record, type_length);
146
147 if (short_record)
148 wpabuf_put_u8(record, payload_length);
149 else
150 wpabuf_put_be32(record, payload_length);
151
152 if (id_length > 0)
153 wpabuf_put_u8(record, id_length);
154 wpabuf_put_data(record, type, type_length);
155 wpabuf_put_data(record, id, id_length);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700156 wpabuf_put_buf(record, payload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700157 return record;
158}
159
160
161static int wifi_filter(struct ndef_record *record)
162{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800163 if (record->type == NULL ||
164 record->type_length != os_strlen(wifi_handover_type))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165 return 0;
166 if (os_memcmp(record->type, wifi_handover_type,
167 os_strlen(wifi_handover_type)) != 0)
168 return 0;
169 return 1;
170}
171
172
Dmitry Shmidt04949592012-07-19 12:16:46 -0700173struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700174{
175 return ndef_parse_records(buf, wifi_filter);
176}
177
178
Dmitry Shmidt04949592012-07-19 12:16:46 -0700179struct wpabuf * ndef_build_wifi(const struct wpabuf *buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700180{
181 return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
182 FLAG_TNF_RFC2046, wifi_handover_type,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700183 os_strlen(wifi_handover_type), NULL, 0, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800185
186
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800187static int p2p_filter(struct ndef_record *record)
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800188{
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800189 if (record->type == NULL ||
190 record->type_length != os_strlen(p2p_handover_type))
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800191 return 0;
192 if (os_memcmp(record->type, p2p_handover_type,
193 os_strlen(p2p_handover_type)) != 0)
194 return 0;
195 return 1;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800196}
197
198
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800199struct wpabuf * ndef_parse_p2p(const struct wpabuf *buf)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800200{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800201 return ndef_parse_records(buf, p2p_filter);
202}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800203
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800204
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800205struct wpabuf * ndef_build_p2p(const struct wpabuf *buf)
206{
207 return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
208 FLAG_TNF_RFC2046, p2p_handover_type,
209 os_strlen(p2p_handover_type), NULL, 0, buf);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800210}