xref: /aosp_15_r20/external/wpa_supplicant_8/src/wps/ndef.c (revision 03f9172ca588f91df233974f4258bab95191f931)
1*03f9172cSAndroid Build Coastguard Worker /*
2*03f9172cSAndroid Build Coastguard Worker  * NDEF(NFC Data Exchange Format) routines for Wi-Fi Protected Setup
3*03f9172cSAndroid Build Coastguard Worker  *   Reference is "NFCForum-TS-NDEF_1.0 2006-07-24".
4*03f9172cSAndroid Build Coastguard Worker  * Copyright (c) 2009-2012, Masashi Honma <[email protected]>
5*03f9172cSAndroid Build Coastguard Worker  *
6*03f9172cSAndroid Build Coastguard Worker  * This software may be distributed under the terms of the BSD license.
7*03f9172cSAndroid Build Coastguard Worker  * See README for more details.
8*03f9172cSAndroid Build Coastguard Worker  */
9*03f9172cSAndroid Build Coastguard Worker 
10*03f9172cSAndroid Build Coastguard Worker #include "includes.h"
11*03f9172cSAndroid Build Coastguard Worker #include "common.h"
12*03f9172cSAndroid Build Coastguard Worker #include "wps/wps.h"
13*03f9172cSAndroid Build Coastguard Worker 
14*03f9172cSAndroid Build Coastguard Worker #define FLAG_MESSAGE_BEGIN (1 << 7)
15*03f9172cSAndroid Build Coastguard Worker #define FLAG_MESSAGE_END (1 << 6)
16*03f9172cSAndroid Build Coastguard Worker #define FLAG_CHUNK (1 << 5)
17*03f9172cSAndroid Build Coastguard Worker #define FLAG_SHORT_RECORD (1 << 4)
18*03f9172cSAndroid Build Coastguard Worker #define FLAG_ID_LENGTH_PRESENT (1 << 3)
19*03f9172cSAndroid Build Coastguard Worker #define FLAG_TNF_NFC_FORUM (0x01)
20*03f9172cSAndroid Build Coastguard Worker #define FLAG_TNF_RFC2046 (0x02)
21*03f9172cSAndroid Build Coastguard Worker 
22*03f9172cSAndroid Build Coastguard Worker struct ndef_record {
23*03f9172cSAndroid Build Coastguard Worker 	const u8 *type;
24*03f9172cSAndroid Build Coastguard Worker 	const u8 *id;
25*03f9172cSAndroid Build Coastguard Worker 	const u8 *payload;
26*03f9172cSAndroid Build Coastguard Worker 	u8 type_length;
27*03f9172cSAndroid Build Coastguard Worker 	u8 id_length;
28*03f9172cSAndroid Build Coastguard Worker 	u32 payload_length;
29*03f9172cSAndroid Build Coastguard Worker 	u32 total_length;
30*03f9172cSAndroid Build Coastguard Worker };
31*03f9172cSAndroid Build Coastguard Worker 
32*03f9172cSAndroid Build Coastguard Worker static const char wifi_handover_type[] = "application/vnd.wfa.wsc";
33*03f9172cSAndroid Build Coastguard Worker static const char p2p_handover_type[] = "application/vnd.wfa.p2p";
34*03f9172cSAndroid Build Coastguard Worker 
ndef_parse_record(const u8 * data,u32 size,struct ndef_record * record)35*03f9172cSAndroid Build Coastguard Worker static int ndef_parse_record(const u8 *data, u32 size,
36*03f9172cSAndroid Build Coastguard Worker 			     struct ndef_record *record)
37*03f9172cSAndroid Build Coastguard Worker {
38*03f9172cSAndroid Build Coastguard Worker 	const u8 *pos = data + 1;
39*03f9172cSAndroid Build Coastguard Worker 
40*03f9172cSAndroid Build Coastguard Worker 	if (size < 2)
41*03f9172cSAndroid Build Coastguard Worker 		return -1;
42*03f9172cSAndroid Build Coastguard Worker 	record->type_length = *pos++;
43*03f9172cSAndroid Build Coastguard Worker 	if (data[0] & FLAG_SHORT_RECORD) {
44*03f9172cSAndroid Build Coastguard Worker 		if (size < 3)
45*03f9172cSAndroid Build Coastguard Worker 			return -1;
46*03f9172cSAndroid Build Coastguard Worker 		record->payload_length = *pos++;
47*03f9172cSAndroid Build Coastguard Worker 	} else {
48*03f9172cSAndroid Build Coastguard Worker 		u32 len;
49*03f9172cSAndroid Build Coastguard Worker 
50*03f9172cSAndroid Build Coastguard Worker 		if (size < 6)
51*03f9172cSAndroid Build Coastguard Worker 			return -1;
52*03f9172cSAndroid Build Coastguard Worker 		len = WPA_GET_BE32(pos);
53*03f9172cSAndroid Build Coastguard Worker 		if (len > size - 6 || len > 20000)
54*03f9172cSAndroid Build Coastguard Worker 			return -1;
55*03f9172cSAndroid Build Coastguard Worker 		record->payload_length = len;
56*03f9172cSAndroid Build Coastguard Worker 		pos += sizeof(u32);
57*03f9172cSAndroid Build Coastguard Worker 	}
58*03f9172cSAndroid Build Coastguard Worker 
59*03f9172cSAndroid Build Coastguard Worker 	if (data[0] & FLAG_ID_LENGTH_PRESENT) {
60*03f9172cSAndroid Build Coastguard Worker 		if ((int) size < pos - data + 1)
61*03f9172cSAndroid Build Coastguard Worker 			return -1;
62*03f9172cSAndroid Build Coastguard Worker 		record->id_length = *pos++;
63*03f9172cSAndroid Build Coastguard Worker 	} else
64*03f9172cSAndroid Build Coastguard Worker 		record->id_length = 0;
65*03f9172cSAndroid Build Coastguard Worker 
66*03f9172cSAndroid Build Coastguard Worker 	if (record->type_length > data + size - pos)
67*03f9172cSAndroid Build Coastguard Worker 		return -1;
68*03f9172cSAndroid Build Coastguard Worker 	record->type = record->type_length == 0 ? NULL : pos;
69*03f9172cSAndroid Build Coastguard Worker 	pos += record->type_length;
70*03f9172cSAndroid Build Coastguard Worker 
71*03f9172cSAndroid Build Coastguard Worker 	if (record->id_length > data + size - pos)
72*03f9172cSAndroid Build Coastguard Worker 		return -1;
73*03f9172cSAndroid Build Coastguard Worker 	record->id = record->id_length == 0 ? NULL : pos;
74*03f9172cSAndroid Build Coastguard Worker 	pos += record->id_length;
75*03f9172cSAndroid Build Coastguard Worker 
76*03f9172cSAndroid Build Coastguard Worker 	if (record->payload_length > (size_t) (data + size - pos))
77*03f9172cSAndroid Build Coastguard Worker 		return -1;
78*03f9172cSAndroid Build Coastguard Worker 	record->payload = record->payload_length == 0 ? NULL : pos;
79*03f9172cSAndroid Build Coastguard Worker 	pos += record->payload_length;
80*03f9172cSAndroid Build Coastguard Worker 
81*03f9172cSAndroid Build Coastguard Worker 	record->total_length = pos - data;
82*03f9172cSAndroid Build Coastguard Worker 	if (record->total_length > size ||
83*03f9172cSAndroid Build Coastguard Worker 	    record->total_length < record->payload_length)
84*03f9172cSAndroid Build Coastguard Worker 		return -1;
85*03f9172cSAndroid Build Coastguard Worker 	return 0;
86*03f9172cSAndroid Build Coastguard Worker }
87*03f9172cSAndroid Build Coastguard Worker 
88*03f9172cSAndroid Build Coastguard Worker 
ndef_parse_records(const struct wpabuf * buf,int (* filter)(struct ndef_record *))89*03f9172cSAndroid Build Coastguard Worker static struct wpabuf * ndef_parse_records(const struct wpabuf *buf,
90*03f9172cSAndroid Build Coastguard Worker 					  int (*filter)(struct ndef_record *))
91*03f9172cSAndroid Build Coastguard Worker {
92*03f9172cSAndroid Build Coastguard Worker 	struct ndef_record record;
93*03f9172cSAndroid Build Coastguard Worker 	int len = wpabuf_len(buf);
94*03f9172cSAndroid Build Coastguard Worker 	const u8 *data = wpabuf_head(buf);
95*03f9172cSAndroid Build Coastguard Worker 
96*03f9172cSAndroid Build Coastguard Worker 	while (len > 0) {
97*03f9172cSAndroid Build Coastguard Worker 		if (ndef_parse_record(data, len, &record) < 0) {
98*03f9172cSAndroid Build Coastguard Worker 			wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
99*03f9172cSAndroid Build Coastguard Worker 			return NULL;
100*03f9172cSAndroid Build Coastguard Worker 		}
101*03f9172cSAndroid Build Coastguard Worker 		if (filter == NULL || filter(&record))
102*03f9172cSAndroid Build Coastguard Worker 			return wpabuf_alloc_copy(record.payload,
103*03f9172cSAndroid Build Coastguard Worker 						 record.payload_length);
104*03f9172cSAndroid Build Coastguard Worker 		data += record.total_length;
105*03f9172cSAndroid Build Coastguard Worker 		len -= record.total_length;
106*03f9172cSAndroid Build Coastguard Worker 	}
107*03f9172cSAndroid Build Coastguard Worker 	wpa_printf(MSG_ERROR, "NDEF : Record not found");
108*03f9172cSAndroid Build Coastguard Worker 	return NULL;
109*03f9172cSAndroid Build Coastguard Worker }
110*03f9172cSAndroid Build Coastguard Worker 
111*03f9172cSAndroid Build Coastguard Worker 
ndef_build_record(u8 flags,const void * type,u8 type_length,void * id,u8 id_length,const struct wpabuf * payload)112*03f9172cSAndroid Build Coastguard Worker static struct wpabuf * ndef_build_record(u8 flags, const void *type,
113*03f9172cSAndroid Build Coastguard Worker 					 u8 type_length, void *id,
114*03f9172cSAndroid Build Coastguard Worker 					 u8 id_length,
115*03f9172cSAndroid Build Coastguard Worker 					 const struct wpabuf *payload)
116*03f9172cSAndroid Build Coastguard Worker {
117*03f9172cSAndroid Build Coastguard Worker 	struct wpabuf *record;
118*03f9172cSAndroid Build Coastguard Worker 	size_t total_len;
119*03f9172cSAndroid Build Coastguard Worker 	int short_record;
120*03f9172cSAndroid Build Coastguard Worker 	u8 local_flag;
121*03f9172cSAndroid Build Coastguard Worker 	size_t payload_length = wpabuf_len(payload);
122*03f9172cSAndroid Build Coastguard Worker 
123*03f9172cSAndroid Build Coastguard Worker 	short_record = payload_length < 256 ? 1 : 0;
124*03f9172cSAndroid Build Coastguard Worker 
125*03f9172cSAndroid Build Coastguard Worker 	total_len = 2; /* flag + type length */
126*03f9172cSAndroid Build Coastguard Worker 	/* payload length */
127*03f9172cSAndroid Build Coastguard Worker 	total_len += short_record ? sizeof(u8) : sizeof(u32);
128*03f9172cSAndroid Build Coastguard Worker 	if (id_length > 0)
129*03f9172cSAndroid Build Coastguard Worker 		total_len += 1;
130*03f9172cSAndroid Build Coastguard Worker 	total_len += type_length + id_length + payload_length;
131*03f9172cSAndroid Build Coastguard Worker 	record = wpabuf_alloc(total_len);
132*03f9172cSAndroid Build Coastguard Worker 	if (record == NULL) {
133*03f9172cSAndroid Build Coastguard Worker 		wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
134*03f9172cSAndroid Build Coastguard Worker 			   "record for build");
135*03f9172cSAndroid Build Coastguard Worker 		return NULL;
136*03f9172cSAndroid Build Coastguard Worker 	}
137*03f9172cSAndroid Build Coastguard Worker 
138*03f9172cSAndroid Build Coastguard Worker 	local_flag = flags;
139*03f9172cSAndroid Build Coastguard Worker 	if (id_length > 0)
140*03f9172cSAndroid Build Coastguard Worker 		local_flag |= FLAG_ID_LENGTH_PRESENT;
141*03f9172cSAndroid Build Coastguard Worker 	if (short_record)
142*03f9172cSAndroid Build Coastguard Worker 		local_flag |= FLAG_SHORT_RECORD;
143*03f9172cSAndroid Build Coastguard Worker 	wpabuf_put_u8(record, local_flag);
144*03f9172cSAndroid Build Coastguard Worker 
145*03f9172cSAndroid Build Coastguard Worker 	wpabuf_put_u8(record, type_length);
146*03f9172cSAndroid Build Coastguard Worker 
147*03f9172cSAndroid Build Coastguard Worker 	if (short_record)
148*03f9172cSAndroid Build Coastguard Worker 		wpabuf_put_u8(record, payload_length);
149*03f9172cSAndroid Build Coastguard Worker 	else
150*03f9172cSAndroid Build Coastguard Worker 		wpabuf_put_be32(record, payload_length);
151*03f9172cSAndroid Build Coastguard Worker 
152*03f9172cSAndroid Build Coastguard Worker 	if (id_length > 0)
153*03f9172cSAndroid Build Coastguard Worker 		wpabuf_put_u8(record, id_length);
154*03f9172cSAndroid Build Coastguard Worker 	wpabuf_put_data(record, type, type_length);
155*03f9172cSAndroid Build Coastguard Worker 	wpabuf_put_data(record, id, id_length);
156*03f9172cSAndroid Build Coastguard Worker 	wpabuf_put_buf(record, payload);
157*03f9172cSAndroid Build Coastguard Worker 	return record;
158*03f9172cSAndroid Build Coastguard Worker }
159*03f9172cSAndroid Build Coastguard Worker 
160*03f9172cSAndroid Build Coastguard Worker 
wifi_filter(struct ndef_record * record)161*03f9172cSAndroid Build Coastguard Worker static int wifi_filter(struct ndef_record *record)
162*03f9172cSAndroid Build Coastguard Worker {
163*03f9172cSAndroid Build Coastguard Worker 	if (record->type == NULL ||
164*03f9172cSAndroid Build Coastguard Worker 	    record->type_length != os_strlen(wifi_handover_type))
165*03f9172cSAndroid Build Coastguard Worker 		return 0;
166*03f9172cSAndroid Build Coastguard Worker 	if (os_memcmp(record->type, wifi_handover_type,
167*03f9172cSAndroid Build Coastguard Worker 		      os_strlen(wifi_handover_type)) != 0)
168*03f9172cSAndroid Build Coastguard Worker 		return 0;
169*03f9172cSAndroid Build Coastguard Worker 	return 1;
170*03f9172cSAndroid Build Coastguard Worker }
171*03f9172cSAndroid Build Coastguard Worker 
172*03f9172cSAndroid Build Coastguard Worker 
ndef_parse_wifi(const struct wpabuf * buf)173*03f9172cSAndroid Build Coastguard Worker struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf)
174*03f9172cSAndroid Build Coastguard Worker {
175*03f9172cSAndroid Build Coastguard Worker 	return ndef_parse_records(buf, wifi_filter);
176*03f9172cSAndroid Build Coastguard Worker }
177*03f9172cSAndroid Build Coastguard Worker 
178*03f9172cSAndroid Build Coastguard Worker 
ndef_build_wifi(const struct wpabuf * buf)179*03f9172cSAndroid Build Coastguard Worker struct wpabuf * ndef_build_wifi(const struct wpabuf *buf)
180*03f9172cSAndroid Build Coastguard Worker {
181*03f9172cSAndroid Build Coastguard Worker 	return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
182*03f9172cSAndroid Build Coastguard Worker 				 FLAG_TNF_RFC2046, wifi_handover_type,
183*03f9172cSAndroid Build Coastguard Worker 				 os_strlen(wifi_handover_type), NULL, 0, buf);
184*03f9172cSAndroid Build Coastguard Worker }
185*03f9172cSAndroid Build Coastguard Worker 
186*03f9172cSAndroid Build Coastguard Worker 
p2p_filter(struct ndef_record * record)187*03f9172cSAndroid Build Coastguard Worker static int p2p_filter(struct ndef_record *record)
188*03f9172cSAndroid Build Coastguard Worker {
189*03f9172cSAndroid Build Coastguard Worker 	if (record->type == NULL ||
190*03f9172cSAndroid Build Coastguard Worker 	    record->type_length != os_strlen(p2p_handover_type))
191*03f9172cSAndroid Build Coastguard Worker 		return 0;
192*03f9172cSAndroid Build Coastguard Worker 	if (os_memcmp(record->type, p2p_handover_type,
193*03f9172cSAndroid Build Coastguard Worker 		      os_strlen(p2p_handover_type)) != 0)
194*03f9172cSAndroid Build Coastguard Worker 		return 0;
195*03f9172cSAndroid Build Coastguard Worker 	return 1;
196*03f9172cSAndroid Build Coastguard Worker }
197*03f9172cSAndroid Build Coastguard Worker 
198*03f9172cSAndroid Build Coastguard Worker 
ndef_parse_p2p(const struct wpabuf * buf)199*03f9172cSAndroid Build Coastguard Worker struct wpabuf * ndef_parse_p2p(const struct wpabuf *buf)
200*03f9172cSAndroid Build Coastguard Worker {
201*03f9172cSAndroid Build Coastguard Worker 	return ndef_parse_records(buf, p2p_filter);
202*03f9172cSAndroid Build Coastguard Worker }
203*03f9172cSAndroid Build Coastguard Worker 
204*03f9172cSAndroid Build Coastguard Worker 
ndef_build_p2p(const struct wpabuf * buf)205*03f9172cSAndroid Build Coastguard Worker struct wpabuf * ndef_build_p2p(const struct wpabuf *buf)
206*03f9172cSAndroid Build Coastguard Worker {
207*03f9172cSAndroid Build Coastguard Worker 	return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
208*03f9172cSAndroid Build Coastguard Worker 				 FLAG_TNF_RFC2046, p2p_handover_type,
209*03f9172cSAndroid Build Coastguard Worker 				 os_strlen(p2p_handover_type), NULL, 0, buf);
210*03f9172cSAndroid Build Coastguard Worker }
211