WPS: Add preliminary NFC connection handover support for Enrollee
[mech_eap.git] / src / wps / ndef.c
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_NFC_FORUM (0x01)
21 #define FLAG_TNF_RFC2046 (0x02)
22
23 struct ndef_record {
24         const u8 *type;
25         const u8 *id;
26         const u8 *payload;
27         u8 type_length;
28         u8 id_length;
29         u32 payload_length;
30         u32 total_length;
31 };
32
33 static char wifi_handover_type[] = "application/vnd.wfa.wsc";
34
35 static int ndef_parse_record(const u8 *data, u32 size,
36                              struct ndef_record *record)
37 {
38         const u8 *pos = data + 1;
39
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
77 static struct wpabuf * ndef_parse_records(const struct wpabuf *buf,
78                                           int (*filter)(struct ndef_record *))
79 {
80         struct ndef_record record;
81         int len = wpabuf_len(buf);
82         const u8 *data = wpabuf_head(buf);
83
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
100 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         size_t payload_length = wpabuf_len(payload);
110
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);
144         wpabuf_put_buf(record, payload);
145         return record;
146 }
147
148
149 static int wifi_filter(struct ndef_record *record)
150 {
151         if (record->type_length != os_strlen(wifi_handover_type))
152                 return 0;
153         if (os_memcmp(record->type, wifi_handover_type,
154                       os_strlen(wifi_handover_type)) != 0)
155                 return 0;
156         return 1;
157 }
158
159
160 struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf)
161 {
162         return ndef_parse_records(buf, wifi_filter);
163 }
164
165
166 struct wpabuf * ndef_build_wifi(const struct wpabuf *buf)
167 {
168         return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
169                                  FLAG_TNF_RFC2046, wifi_handover_type,
170                                  os_strlen(wifi_handover_type), NULL, 0, buf);
171 }
172
173
174 struct wpabuf * ndef_build_wifi_hr(void)
175 {
176         struct wpabuf *rn, *cr, *ac_payload, *ac, *hr_payload, *hr;
177         struct wpabuf *carrier, *hc;
178
179         rn = wpabuf_alloc(2);
180         if (rn == NULL)
181                 return NULL;
182         wpabuf_put_be16(rn, os_random() & 0xffff);
183
184         cr = ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_TNF_NFC_FORUM, "cr", 2,
185                                NULL, 0, rn);
186         wpabuf_free(rn);
187
188         if (cr == NULL)
189                 return NULL;
190
191         ac_payload = wpabuf_alloc(4);
192         if (ac_payload == NULL) {
193                 wpabuf_free(cr);
194                 return NULL;
195         }
196         wpabuf_put_u8(ac_payload, 0x01); /* Carrier Flags: CRS=1 "active" */
197         wpabuf_put_u8(ac_payload, 0x01); /* Carrier Data Reference Length */
198         wpabuf_put_u8(ac_payload, '0'); /* Carrier Data Reference: "0" */
199         wpabuf_put_u8(ac_payload, 0); /* Aux Data Reference Count */
200
201         ac = ndef_build_record(FLAG_MESSAGE_END | FLAG_TNF_NFC_FORUM, "ac", 2,
202                                NULL, 0, ac_payload);
203         wpabuf_free(ac_payload);
204         if (ac == NULL) {
205                 wpabuf_free(cr);
206                 return NULL;
207         }
208
209         hr_payload = wpabuf_alloc(1 + wpabuf_len(cr) + wpabuf_len(ac));
210         if (hr_payload == NULL) {
211                 wpabuf_free(cr);
212                 wpabuf_free(ac);
213                 return NULL;
214         }
215
216         wpabuf_put_u8(hr_payload, 0x12); /* Connection Handover Version 1.2 */
217         wpabuf_put_buf(hr_payload, cr);
218         wpabuf_put_buf(hr_payload, ac);
219         wpabuf_free(cr);
220         wpabuf_free(ac);
221
222         hr = ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_TNF_NFC_FORUM, "Hr", 2,
223                                NULL, 0, hr_payload);
224         wpabuf_free(hr_payload);
225         if (hr == NULL)
226                 return NULL;
227
228         carrier = wpabuf_alloc(2 + os_strlen(wifi_handover_type));
229         if (carrier == NULL) {
230                 wpabuf_free(hr);
231                 return NULL;
232         }
233         wpabuf_put_u8(carrier, 0x02); /* Carrier Type Format */
234         wpabuf_put_u8(carrier, os_strlen(wifi_handover_type));
235         wpabuf_put_str(carrier, wifi_handover_type);
236
237         hc = ndef_build_record(FLAG_MESSAGE_END | FLAG_TNF_NFC_FORUM, "Hc", 2,
238                                "0", 1, carrier);
239         wpabuf_free(carrier);
240         if (hc == NULL) {
241                 wpabuf_free(hr);
242                 return NULL;
243         }
244
245         return wpabuf_concat(hr, hc);
246 }