ndef.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #include "includes.h"
  10. #include "common.h"
  11. #include "wps/wps.h"
  12. #define FLAG_MESSAGE_BEGIN (1 << 7)
  13. #define FLAG_MESSAGE_END (1 << 6)
  14. #define FLAG_CHUNK (1 << 5)
  15. #define FLAG_SHORT_RECORD (1 << 4)
  16. #define FLAG_ID_LENGTH_PRESENT (1 << 3)
  17. #define FLAG_TNF_NFC_FORUM (0x01)
  18. #define FLAG_TNF_RFC2046 (0x02)
  19. struct ndef_record {
  20. const u8 *type;
  21. const u8 *id;
  22. const u8 *payload;
  23. u8 type_length;
  24. u8 id_length;
  25. u32 payload_length;
  26. u32 total_length;
  27. };
  28. static char wifi_handover_type[] = "application/vnd.wfa.wsc";
  29. static int ndef_parse_record(const u8 *data, u32 size,
  30. struct ndef_record *record)
  31. {
  32. const u8 *pos = data + 1;
  33. if (size < 2)
  34. return -1;
  35. record->type_length = *pos++;
  36. if (data[0] & FLAG_SHORT_RECORD) {
  37. if (size < 3)
  38. return -1;
  39. record->payload_length = *pos++;
  40. } else {
  41. if (size < 6)
  42. return -1;
  43. record->payload_length = ntohl(*(u32 *)pos);
  44. pos += sizeof(u32);
  45. }
  46. if (data[0] & FLAG_ID_LENGTH_PRESENT) {
  47. if ((int) size < pos - data + 1)
  48. return -1;
  49. record->id_length = *pos++;
  50. } else
  51. record->id_length = 0;
  52. record->type = record->type_length == 0 ? NULL : pos;
  53. pos += record->type_length;
  54. record->id = record->id_length == 0 ? NULL : pos;
  55. pos += record->id_length;
  56. record->payload = record->payload_length == 0 ? NULL : pos;
  57. pos += record->payload_length;
  58. record->total_length = pos - data;
  59. if (record->total_length > size)
  60. return -1;
  61. return 0;
  62. }
  63. static struct wpabuf * ndef_parse_records(const struct wpabuf *buf,
  64. int (*filter)(struct ndef_record *))
  65. {
  66. struct ndef_record record;
  67. int len = wpabuf_len(buf);
  68. const u8 *data = wpabuf_head(buf);
  69. while (len > 0) {
  70. if (ndef_parse_record(data, len, &record) < 0) {
  71. wpa_printf(MSG_ERROR, "NDEF : Failed to parse");
  72. return NULL;
  73. }
  74. if (filter == NULL || filter(&record))
  75. return wpabuf_alloc_copy(record.payload,
  76. record.payload_length);
  77. data += record.total_length;
  78. len -= record.total_length;
  79. }
  80. wpa_printf(MSG_ERROR, "NDEF : Record not found");
  81. return NULL;
  82. }
  83. static struct wpabuf * ndef_build_record(u8 flags, void *type,
  84. u8 type_length, void *id,
  85. u8 id_length,
  86. const struct wpabuf *payload)
  87. {
  88. struct wpabuf *record;
  89. size_t total_len;
  90. int short_record;
  91. u8 local_flag;
  92. size_t payload_length = wpabuf_len(payload);
  93. short_record = payload_length < 256 ? 1 : 0;
  94. total_len = 2; /* flag + type length */
  95. /* payload length */
  96. total_len += short_record ? sizeof(u8) : sizeof(u32);
  97. if (id_length > 0)
  98. total_len += 1;
  99. total_len += type_length + id_length + payload_length;
  100. record = wpabuf_alloc(total_len);
  101. if (record == NULL) {
  102. wpa_printf(MSG_ERROR, "NDEF : Failed to allocate "
  103. "record for build");
  104. return NULL;
  105. }
  106. local_flag = flags;
  107. if (id_length > 0)
  108. local_flag |= FLAG_ID_LENGTH_PRESENT;
  109. if (short_record)
  110. local_flag |= FLAG_SHORT_RECORD;
  111. wpabuf_put_u8(record, local_flag);
  112. wpabuf_put_u8(record, type_length);
  113. if (short_record)
  114. wpabuf_put_u8(record, payload_length);
  115. else
  116. wpabuf_put_be32(record, payload_length);
  117. if (id_length > 0)
  118. wpabuf_put_u8(record, id_length);
  119. wpabuf_put_data(record, type, type_length);
  120. wpabuf_put_data(record, id, id_length);
  121. wpabuf_put_buf(record, payload);
  122. return record;
  123. }
  124. static int wifi_filter(struct ndef_record *record)
  125. {
  126. if (record->type_length != os_strlen(wifi_handover_type))
  127. return 0;
  128. if (os_memcmp(record->type, wifi_handover_type,
  129. os_strlen(wifi_handover_type)) != 0)
  130. return 0;
  131. return 1;
  132. }
  133. struct wpabuf * ndef_parse_wifi(const struct wpabuf *buf)
  134. {
  135. return ndef_parse_records(buf, wifi_filter);
  136. }
  137. struct wpabuf * ndef_build_wifi(const struct wpabuf *buf)
  138. {
  139. return ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_MESSAGE_END |
  140. FLAG_TNF_RFC2046, wifi_handover_type,
  141. os_strlen(wifi_handover_type), NULL, 0, buf);
  142. }
  143. struct wpabuf * ndef_build_wifi_hc(int begin)
  144. {
  145. struct wpabuf *hc, *carrier;
  146. carrier = wpabuf_alloc(2 + os_strlen(wifi_handover_type));
  147. if (carrier == NULL)
  148. return NULL;
  149. wpabuf_put_u8(carrier, 0x02); /* Carrier Type Format */
  150. wpabuf_put_u8(carrier, os_strlen(wifi_handover_type));
  151. wpabuf_put_str(carrier, wifi_handover_type);
  152. hc = ndef_build_record((begin ? FLAG_MESSAGE_BEGIN : 0) |
  153. FLAG_MESSAGE_END | FLAG_TNF_NFC_FORUM, "Hc", 2,
  154. "0", 1, carrier);
  155. wpabuf_free(carrier);
  156. return hc;
  157. }
  158. struct wpabuf * ndef_build_wifi_hr(void)
  159. {
  160. struct wpabuf *rn, *cr, *ac_payload, *ac, *hr_payload, *hr;
  161. struct wpabuf *hc;
  162. rn = wpabuf_alloc(2);
  163. if (rn == NULL)
  164. return NULL;
  165. wpabuf_put_be16(rn, os_random() & 0xffff);
  166. cr = ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_TNF_NFC_FORUM, "cr", 2,
  167. NULL, 0, rn);
  168. wpabuf_free(rn);
  169. if (cr == NULL)
  170. return NULL;
  171. ac_payload = wpabuf_alloc(4);
  172. if (ac_payload == NULL) {
  173. wpabuf_free(cr);
  174. return NULL;
  175. }
  176. wpabuf_put_u8(ac_payload, 0x01); /* Carrier Flags: CRS=1 "active" */
  177. wpabuf_put_u8(ac_payload, 0x01); /* Carrier Data Reference Length */
  178. wpabuf_put_u8(ac_payload, '0'); /* Carrier Data Reference: "0" */
  179. wpabuf_put_u8(ac_payload, 0); /* Aux Data Reference Count */
  180. ac = ndef_build_record(FLAG_MESSAGE_END | FLAG_TNF_NFC_FORUM, "ac", 2,
  181. NULL, 0, ac_payload);
  182. wpabuf_free(ac_payload);
  183. if (ac == NULL) {
  184. wpabuf_free(cr);
  185. return NULL;
  186. }
  187. hr_payload = wpabuf_alloc(1 + wpabuf_len(cr) + wpabuf_len(ac));
  188. if (hr_payload == NULL) {
  189. wpabuf_free(cr);
  190. wpabuf_free(ac);
  191. return NULL;
  192. }
  193. wpabuf_put_u8(hr_payload, 0x12); /* Connection Handover Version 1.2 */
  194. wpabuf_put_buf(hr_payload, cr);
  195. wpabuf_put_buf(hr_payload, ac);
  196. wpabuf_free(cr);
  197. wpabuf_free(ac);
  198. hr = ndef_build_record(FLAG_MESSAGE_BEGIN | FLAG_TNF_NFC_FORUM, "Hr", 2,
  199. NULL, 0, hr_payload);
  200. wpabuf_free(hr_payload);
  201. if (hr == NULL)
  202. return NULL;
  203. hc = ndef_build_wifi_hc(0);
  204. if (hc == NULL) {
  205. wpabuf_free(hr);
  206. return NULL;
  207. }
  208. return wpabuf_concat(hr, hc);
  209. }