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