ndef.c 4.8 KB

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