json.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * JavaScript Object Notation (JSON) parser (RFC7159)
  3. * Copyright (c) 2017, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "base64.h"
  11. #include "json.h"
  12. #define JSON_MAX_DEPTH 10
  13. #define JSON_MAX_TOKENS 500
  14. void json_escape_string(char *txt, size_t maxlen, const char *data, size_t len)
  15. {
  16. char *end = txt + maxlen;
  17. size_t i;
  18. for (i = 0; i < len; i++) {
  19. if (txt + 4 >= end)
  20. break;
  21. switch (data[i]) {
  22. case '\"':
  23. *txt++ = '\\';
  24. *txt++ = '\"';
  25. break;
  26. case '\\':
  27. *txt++ = '\\';
  28. *txt++ = '\\';
  29. break;
  30. case '\n':
  31. *txt++ = '\\';
  32. *txt++ = 'n';
  33. break;
  34. case '\r':
  35. *txt++ = '\\';
  36. *txt++ = 'r';
  37. break;
  38. case '\t':
  39. *txt++ = '\\';
  40. *txt++ = 't';
  41. break;
  42. default:
  43. if (data[i] >= 32 && data[i] <= 126) {
  44. *txt++ = data[i];
  45. } else {
  46. txt += os_snprintf(txt, end - txt, "\\u%04x",
  47. data[i]);
  48. }
  49. break;
  50. }
  51. }
  52. *txt = '\0';
  53. }
  54. static char * json_parse_string(const char **json_pos, const char *end)
  55. {
  56. const char *pos = *json_pos;
  57. char *str, *spos, *s_end;
  58. size_t max_len, buf_len;
  59. u8 bin[2];
  60. pos++; /* skip starting quote */
  61. max_len = end - pos + 1;
  62. buf_len = max_len > 10 ? 10 : max_len;
  63. str = os_malloc(buf_len);
  64. if (!str)
  65. return NULL;
  66. spos = str;
  67. s_end = str + buf_len;
  68. for (; pos < end; pos++) {
  69. if (buf_len < max_len && s_end - spos < 3) {
  70. char *tmp;
  71. int idx;
  72. idx = spos - str;
  73. buf_len *= 2;
  74. if (buf_len > max_len)
  75. buf_len = max_len;
  76. tmp = os_realloc(str, buf_len);
  77. if (!tmp)
  78. goto fail;
  79. str = tmp;
  80. spos = str + idx;
  81. s_end = str + buf_len;
  82. }
  83. switch (*pos) {
  84. case '\"': /* end string */
  85. *spos = '\0';
  86. /* caller will move to the next position */
  87. *json_pos = pos;
  88. return str;
  89. case '\\':
  90. pos++;
  91. switch (*pos) {
  92. case '"':
  93. case '\\':
  94. case '/':
  95. *spos++ = *pos;
  96. break;
  97. case 'n':
  98. *spos++ = '\n';
  99. break;
  100. case 'r':
  101. *spos++ = '\r';
  102. break;
  103. case 't':
  104. *spos++ = '\t';
  105. break;
  106. case 'u':
  107. if (end - pos < 5 ||
  108. hexstr2bin(pos + 1, bin, 2) < 0 ||
  109. bin[1] == 0x00) {
  110. wpa_printf(MSG_DEBUG,
  111. "JSON: Invalid \\u escape");
  112. goto fail;
  113. }
  114. if (bin[0] == 0x00) {
  115. *spos++ = bin[1];
  116. } else {
  117. *spos++ = bin[0];
  118. *spos++ = bin[1];
  119. }
  120. pos += 4;
  121. break;
  122. default:
  123. wpa_printf(MSG_DEBUG,
  124. "JSON: Unknown escape '%c'", *pos);
  125. goto fail;
  126. }
  127. break;
  128. default:
  129. *spos++ = *pos;
  130. break;
  131. }
  132. }
  133. fail:
  134. os_free(str);
  135. return NULL;
  136. }
  137. static int json_parse_number(const char **json_pos, const char *end,
  138. int *ret_val)
  139. {
  140. const char *pos = *json_pos;
  141. size_t len;
  142. char *str;
  143. for (; pos < end; pos++) {
  144. if (*pos != '-' && (*pos < '0' || *pos > '9')) {
  145. pos--;
  146. break;
  147. }
  148. }
  149. if (pos < *json_pos)
  150. return -1;
  151. len = pos - *json_pos + 1;
  152. str = os_malloc(len + 1);
  153. if (!str)
  154. return -1;
  155. os_memcpy(str, *json_pos, len);
  156. str[len] = '\0';
  157. *ret_val = atoi(str);
  158. os_free(str);
  159. *json_pos = pos;
  160. return 0;
  161. }
  162. static int json_check_tree_state(struct json_token *token)
  163. {
  164. if (!token)
  165. return 0;
  166. if (json_check_tree_state(token->child) < 0 ||
  167. json_check_tree_state(token->sibling) < 0)
  168. return -1;
  169. if (token->state != JSON_COMPLETED) {
  170. wpa_printf(MSG_DEBUG,
  171. "JSON: Unexpected token state %d (name=%s type=%d)",
  172. token->state, token->name ? token->name : "N/A",
  173. token->type);
  174. return -1;
  175. }
  176. return 0;
  177. }
  178. static struct json_token * json_alloc_token(unsigned int *tokens)
  179. {
  180. (*tokens)++;
  181. if (*tokens > JSON_MAX_TOKENS) {
  182. wpa_printf(MSG_DEBUG, "JSON: Maximum token limit exceeded");
  183. return NULL;
  184. }
  185. return os_zalloc(sizeof(struct json_token));
  186. }
  187. struct json_token * json_parse(const char *data, size_t data_len)
  188. {
  189. struct json_token *root = NULL, *curr_token = NULL, *token = NULL;
  190. const char *pos, *end;
  191. char *str;
  192. int num;
  193. unsigned int depth = 0;
  194. unsigned int tokens = 0;
  195. pos = data;
  196. end = data + data_len;
  197. for (; pos < end; pos++) {
  198. switch (*pos) {
  199. case '[': /* start array */
  200. case '{': /* start object */
  201. if (!curr_token) {
  202. token = json_alloc_token(&tokens);
  203. if (!token)
  204. goto fail;
  205. } else if (curr_token->state == JSON_WAITING_VALUE) {
  206. token = curr_token;
  207. } else if (curr_token->parent &&
  208. curr_token->parent->type == JSON_ARRAY &&
  209. curr_token->parent->state == JSON_STARTED &&
  210. curr_token->state == JSON_EMPTY) {
  211. token = curr_token;
  212. } else {
  213. wpa_printf(MSG_DEBUG,
  214. "JSON: Invalid state for start array/object");
  215. goto fail;
  216. }
  217. depth++;
  218. if (depth > JSON_MAX_DEPTH) {
  219. wpa_printf(MSG_DEBUG,
  220. "JSON: Max depth exceeded");
  221. goto fail;
  222. }
  223. token->type = *pos == '[' ? JSON_ARRAY : JSON_OBJECT;
  224. token->state = JSON_STARTED;
  225. token->child = json_alloc_token(&tokens);
  226. if (!token->child)
  227. goto fail;
  228. curr_token = token->child;
  229. curr_token->parent = token;
  230. curr_token->state = JSON_EMPTY;
  231. break;
  232. case ']': /* end array */
  233. case '}': /* end object */
  234. if (!curr_token || !curr_token->parent ||
  235. curr_token->parent->state != JSON_STARTED) {
  236. wpa_printf(MSG_DEBUG,
  237. "JSON: Invalid state for end array/object");
  238. goto fail;
  239. }
  240. depth--;
  241. curr_token = curr_token->parent;
  242. if ((*pos == ']' &&
  243. curr_token->type != JSON_ARRAY) ||
  244. (*pos == '}' &&
  245. curr_token->type != JSON_OBJECT)) {
  246. wpa_printf(MSG_DEBUG,
  247. "JSON: Array/Object mismatch");
  248. goto fail;
  249. }
  250. if (curr_token->child->state == JSON_EMPTY &&
  251. !curr_token->child->child &&
  252. !curr_token->child->sibling) {
  253. /* Remove pending child token since the
  254. * array/object was empty. */
  255. json_free(curr_token->child);
  256. curr_token->child = NULL;
  257. }
  258. curr_token->state = JSON_COMPLETED;
  259. break;
  260. case '\"': /* string */
  261. str = json_parse_string(&pos, end);
  262. if (!str)
  263. goto fail;
  264. if (!curr_token) {
  265. token = json_alloc_token(&tokens);
  266. if (!token)
  267. goto fail;
  268. token->type = JSON_STRING;
  269. token->string = str;
  270. token->state = JSON_COMPLETED;
  271. } else if (curr_token->parent &&
  272. curr_token->parent->type == JSON_ARRAY &&
  273. curr_token->parent->state == JSON_STARTED &&
  274. curr_token->state == JSON_EMPTY) {
  275. curr_token->string = str;
  276. curr_token->state = JSON_COMPLETED;
  277. curr_token->type = JSON_STRING;
  278. wpa_printf(MSG_MSGDUMP,
  279. "JSON: String value: '%s'",
  280. curr_token->string);
  281. } else if (curr_token->state == JSON_EMPTY) {
  282. curr_token->type = JSON_VALUE;
  283. curr_token->name = str;
  284. curr_token->state = JSON_STARTED;
  285. } else if (curr_token->state == JSON_WAITING_VALUE) {
  286. curr_token->string = str;
  287. curr_token->state = JSON_COMPLETED;
  288. curr_token->type = JSON_STRING;
  289. wpa_printf(MSG_MSGDUMP,
  290. "JSON: String value: '%s' = '%s'",
  291. curr_token->name,
  292. curr_token->string);
  293. } else {
  294. wpa_printf(MSG_DEBUG,
  295. "JSON: Invalid state for a string");
  296. os_free(str);
  297. goto fail;
  298. }
  299. break;
  300. case ' ':
  301. case '\t':
  302. case '\r':
  303. case '\n':
  304. /* ignore whitespace */
  305. break;
  306. case ':': /* name/value separator */
  307. if (!curr_token || curr_token->state != JSON_STARTED)
  308. goto fail;
  309. curr_token->state = JSON_WAITING_VALUE;
  310. break;
  311. case ',': /* member separator */
  312. if (!curr_token)
  313. goto fail;
  314. curr_token->sibling = json_alloc_token(&tokens);
  315. if (!curr_token->sibling)
  316. goto fail;
  317. curr_token->sibling->parent = curr_token->parent;
  318. curr_token = curr_token->sibling;
  319. curr_token->state = JSON_EMPTY;
  320. break;
  321. case 't': /* true */
  322. case 'f': /* false */
  323. case 'n': /* null */
  324. if (!((end - pos >= 4 &&
  325. os_strncmp(pos, "true", 4) == 0) ||
  326. (end - pos >= 5 &&
  327. os_strncmp(pos, "false", 5) == 0) ||
  328. (end - pos >= 4 &&
  329. os_strncmp(pos, "null", 4) == 0))) {
  330. wpa_printf(MSG_DEBUG,
  331. "JSON: Invalid literal name");
  332. goto fail;
  333. }
  334. if (!curr_token) {
  335. token = json_alloc_token(&tokens);
  336. if (!token)
  337. goto fail;
  338. curr_token = token;
  339. } else if (curr_token->state == JSON_WAITING_VALUE) {
  340. wpa_printf(MSG_MSGDUMP,
  341. "JSON: Literal name: '%s' = %c",
  342. curr_token->name, *pos);
  343. } else if (curr_token->parent &&
  344. curr_token->parent->type == JSON_ARRAY &&
  345. curr_token->parent->state == JSON_STARTED &&
  346. curr_token->state == JSON_EMPTY) {
  347. wpa_printf(MSG_MSGDUMP,
  348. "JSON: Literal name: %c", *pos);
  349. } else {
  350. wpa_printf(MSG_DEBUG,
  351. "JSON: Invalid state for a literal name");
  352. goto fail;
  353. }
  354. switch (*pos) {
  355. case 't':
  356. curr_token->type = JSON_BOOLEAN;
  357. curr_token->number = 1;
  358. pos += 3;
  359. break;
  360. case 'f':
  361. curr_token->type = JSON_BOOLEAN;
  362. curr_token->number = 0;
  363. pos += 4;
  364. break;
  365. case 'n':
  366. curr_token->type = JSON_NULL;
  367. pos += 3;
  368. break;
  369. }
  370. curr_token->state = JSON_COMPLETED;
  371. break;
  372. case '-':
  373. case '0':
  374. case '1':
  375. case '2':
  376. case '3':
  377. case '4':
  378. case '5':
  379. case '6':
  380. case '7':
  381. case '8':
  382. case '9':
  383. /* number */
  384. if (json_parse_number(&pos, end, &num) < 0)
  385. goto fail;
  386. if (!curr_token) {
  387. token = json_alloc_token(&tokens);
  388. if (!token)
  389. goto fail;
  390. token->type = JSON_NUMBER;
  391. token->number = num;
  392. token->state = JSON_COMPLETED;
  393. } else if (curr_token->state == JSON_WAITING_VALUE) {
  394. curr_token->number = num;
  395. curr_token->state = JSON_COMPLETED;
  396. curr_token->type = JSON_NUMBER;
  397. wpa_printf(MSG_MSGDUMP,
  398. "JSON: Number value: '%s' = '%d'",
  399. curr_token->name,
  400. curr_token->number);
  401. } else if (curr_token->parent &&
  402. curr_token->parent->type == JSON_ARRAY &&
  403. curr_token->parent->state == JSON_STARTED &&
  404. curr_token->state == JSON_EMPTY) {
  405. curr_token->number = num;
  406. curr_token->state = JSON_COMPLETED;
  407. curr_token->type = JSON_NUMBER;
  408. wpa_printf(MSG_MSGDUMP,
  409. "JSON: Number value: %d",
  410. curr_token->number);
  411. } else {
  412. wpa_printf(MSG_DEBUG,
  413. "JSON: Invalid state for a number");
  414. goto fail;
  415. }
  416. break;
  417. default:
  418. wpa_printf(MSG_DEBUG,
  419. "JSON: Unexpected JSON character: %c", *pos);
  420. goto fail;
  421. }
  422. if (!root)
  423. root = token;
  424. if (!curr_token)
  425. curr_token = token;
  426. }
  427. if (json_check_tree_state(root) < 0) {
  428. wpa_printf(MSG_DEBUG, "JSON: Incomplete token in the tree");
  429. goto fail;
  430. }
  431. return root;
  432. fail:
  433. wpa_printf(MSG_DEBUG, "JSON: Parsing failed");
  434. json_free(root);
  435. return NULL;
  436. }
  437. void json_free(struct json_token *json)
  438. {
  439. if (!json)
  440. return;
  441. json_free(json->child);
  442. json_free(json->sibling);
  443. os_free(json->name);
  444. os_free(json->string);
  445. os_free(json);
  446. }
  447. struct json_token * json_get_member(struct json_token *json, const char *name)
  448. {
  449. struct json_token *token, *ret = NULL;
  450. if (!json || json->type != JSON_OBJECT)
  451. return NULL;
  452. /* Return last matching entry */
  453. for (token = json->child; token; token = token->sibling) {
  454. if (token->name && os_strcmp(token->name, name) == 0)
  455. ret = token;
  456. }
  457. return ret;
  458. }
  459. struct wpabuf * json_get_member_base64url(struct json_token *json,
  460. const char *name)
  461. {
  462. struct json_token *token;
  463. unsigned char *buf;
  464. size_t buflen;
  465. struct wpabuf *ret;
  466. token = json_get_member(json, name);
  467. if (!token || token->type != JSON_STRING)
  468. return NULL;
  469. buf = base64_url_decode((const unsigned char *) token->string,
  470. os_strlen(token->string), &buflen);
  471. if (!buf)
  472. return NULL;
  473. ret = wpabuf_alloc_ext_data(buf, buflen);
  474. if (!ret)
  475. os_free(buf);
  476. return ret;
  477. }
  478. static const char * json_type_str(enum json_type type)
  479. {
  480. switch (type) {
  481. case JSON_VALUE:
  482. return "VALUE";
  483. case JSON_OBJECT:
  484. return "OBJECT";
  485. case JSON_ARRAY:
  486. return "ARRAY";
  487. case JSON_STRING:
  488. return "STRING";
  489. case JSON_NUMBER:
  490. return "NUMBER";
  491. case JSON_BOOLEAN:
  492. return "BOOLEAN";
  493. case JSON_NULL:
  494. return "NULL";
  495. }
  496. return "??";
  497. }
  498. static void json_print_token(struct json_token *token, int depth,
  499. char *buf, size_t buflen)
  500. {
  501. size_t len;
  502. int ret;
  503. if (!token)
  504. return;
  505. len = os_strlen(buf);
  506. ret = os_snprintf(buf + len, buflen - len, "[%d:%s:%s]",
  507. depth, json_type_str(token->type),
  508. token->name ? token->name : "");
  509. if (os_snprintf_error(buflen - len, ret)) {
  510. buf[len] = '\0';
  511. return;
  512. }
  513. json_print_token(token->child, depth + 1, buf, buflen);
  514. json_print_token(token->sibling, depth, buf, buflen);
  515. }
  516. void json_print_tree(struct json_token *root, char *buf, size_t buflen)
  517. {
  518. buf[0] = '\0';
  519. json_print_token(root, 1, buf, buflen);
  520. }