wpa_supplicant AP: Add EAPOL frame TX and RX
[wpasupplicant] / 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, Masashi Honma <honma@ictec.co.jp>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #include "includes.h"
17 #include "common.h"
18 #include "wps/wps.h"
19
20 #define FLAG_MESSAGE_BEGIN (1 << 7)
21 #define FLAG_MESSAGE_END (1 << 6)
22 #define FLAG_CHUNK (1 << 5)
23 #define FLAG_SHORT_RECORD (1 << 4)
24 #define FLAG_ID_LENGTH_PRESENT (1 << 3)
25 #define FLAG_TNF_RFC2046 (0x02)
26
27 struct ndef_record {
28         u8 *type;
29         u8 *id;
30         u8 *payload;
31         u8 type_length;
32         u8 id_length;
33         u32 payload_length;
34         u32 total_length;
35 };
36
37 static char wifi_handover_type[] = "application/vnd.wfa.wsc";
38
39 static int ndef_parse_record(u8 *data, u32 size, struct ndef_record *record)
40 {
41         u8 *pos = data + 1;
42
43         if (size < 2)
44                 return -1;
45         record->type_length = *pos++;
46         if (data[0] & FLAG_SHORT_RECORD) {
47                 if (size < 3)
48                         return -1;
49                 record->payload_length = *pos++;
50         } else {
51                 if (size < 6)
52                         return -1;
53                 record->payload_length = ntohl(*(u32 *)pos);
54                 pos += sizeof(u32);
55         }
56
57         if (data[0] & FLAG_ID_LENGTH_PRESENT) {
58                 if ((int) size < pos - data + 1)
59                         return -1;
60                 record->id_length = *pos++;
61         } else
62                 record->id_length = 0;
63
64         record->type = record->type_length == 0 ? NULL : pos;
65         pos += record->type_length;
66
67         record->id = record->id_length == 0 ? NULL : pos;
68         pos += record->id_length;
69
70         record->payload = record->payload_length == 0 ? NULL : pos;
71         pos += record->payload_length;
72
73         record->total_length = pos - data;
74         if (record->total_length > size)
75                 return -1;
76         return 0;
77 }
78
79
80 static struct wpabuf * ndef_parse_records(struct wpabuf *buf,
81                                           int (*filter)(struct ndef_record *))
82 {
83         struct ndef_record record;
84         int len = wpabuf_len(buf);
85         u8 *data = wpabuf_mhead(buf);
86
87         while (len > 0) {
88                 if (ndef_parse_record(data, len, &record) < 0) {
89                         wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
90                         return NULL;
91                 }
92                 if (filter == NULL || filter(&record))
93                         return wpabuf_alloc_copy(record.payload,
94                                                  record.payload_length);
95                 data += record.total_length;
96                 len -= record.total_length;
97         }
98         wpa_printf(MSG_ERROR, "NDEF : Record not found");
99         return NULL;
100 }
101
102
103 static struct wpabuf * ndef_build_record(u8 flags, void *type,
104                                          u8 type_length, void *id,
105                                          u8 id_length, void *payload,
106                                          u32 payload_length)
107 {
108         struct wpabuf *record;
109         size_t total_len;
110         int short_record;
111         u8 local_flag;
112
113         short_record = payload_length < 256 ? 1 : 0;
114
115         total_len = 2; /* flag + type length */
116         /* payload length */
117         total_len += short_record ? sizeof(u8) : sizeof(u32);
118         if (id_length > 0)
119                 total_len += 1;
120         total_len += type_length + id_length + payload_length;
121         record = wpabuf_alloc(total_len);
122         if (record == NULL) {
123                 wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
124                            "record for build");
125                 return NULL;
126         }
127
128         local_flag = flags;
129         if (id_length > 0)
130                 local_flag |= FLAG_ID_LENGTH_PRESENT;
131         if (short_record)
132                 local_flag |= FLAG_SHORT_RECORD;
133         wpabuf_put_u8(record, local_flag);
134
135         wpabuf_put_u8(record, type_length);
136
137         if (short_record)
138                 wpabuf_put_u8(record, payload_length);
139         else
140                 wpabuf_put_be32(record, payload_length);
141
142         if (id_length > 0)
143                 wpabuf_put_u8(record, id_length);
144         wpabuf_put_data(record, type, type_length);
145         wpabuf_put_data(record, id, id_length);
146         wpabuf_put_data(record, payload, payload_length);
147         return record;
148 }
149
150
151 static int wifi_filter(struct ndef_record *record)
152 {
153         if (record->type_length != os_strlen(wifi_handover_type))
154                 return 0;
155         if (os_memcmp(record->type, wifi_handover_type,
156                       os_strlen(wifi_handover_type)) != 0)
157                 return 0;
158         return 1;
159 }
160
161
162 struct wpabuf * ndef_parse_wifi(struct wpabuf *buf)
163 {
164         return ndef_parse_records(buf, wifi_filter);
165 }
166
167
168 struct wpabuf * ndef_build_wifi(struct wpabuf *buf)
169 {
170         return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
171                                  FLAG_TNF_RFC2046, wifi_handover_type,
172                                  os_strlen(wifi_handover_type), NULL, 0,
173                                  wpabuf_mhead(buf), wpabuf_len(buf));
174 }