ndef.c 4.7 KB

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