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