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