v2.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /* Copyright (c) 2016, Robert Escriva
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of this project nor the names of its contributors may
  13. * be used to endorse or promote products derived from this software
  14. * without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /* C */
  29. #include <assert.h>
  30. #include <ctype.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. /* macaroons */
  34. #include "v2.h"
  35. #include "base64.h"
  36. #include "constants.h"
  37. #include "varint.h"
  38. #define TYPE_LOCATION 1
  39. #define TYPE_IDENTIFIER 2
  40. #define TYPE_VID 4
  41. #define TYPE_SIGNATURE 6
  42. #define EOS 0
  43. #define ENC_STR 1
  44. #define ENC_B64 2
  45. struct field
  46. {
  47. uint8_t type;
  48. struct slice data;
  49. };
  50. size_t
  51. required_field_size(const struct slice* f)
  52. {
  53. return 1 + varint_length(f->size) + f->size;
  54. }
  55. size_t
  56. optional_field_size(const struct slice* f)
  57. {
  58. return f->size ? required_field_size(f) : 0;
  59. }
  60. size_t
  61. macaroon_serialize_size_hint_v2(const struct macaroon* M)
  62. {
  63. size_t i;
  64. size_t sz = 4 /* 1 for version, 3 for 3 EOS markers */
  65. + optional_field_size(&M->location)
  66. + required_field_size(&M->identifier)
  67. + required_field_size(&M->signature);
  68. for (i = 0; i < M->num_caveats; ++i)
  69. {
  70. sz += optional_field_size(&M->caveats[i].cl);
  71. sz += required_field_size(&M->caveats[i].cid);
  72. sz += optional_field_size(&M->caveats[i].vid);
  73. sz += 1 /* EOS */;
  74. }
  75. return sz;
  76. }
  77. int
  78. emit_required_field(uint8_t type, const struct slice* f,
  79. unsigned char** ptr,
  80. unsigned char* const end)
  81. {
  82. const size_t sz = 1 + varint_length(f->size) + f->size;
  83. if (*ptr + sz > end) return -1;
  84. **ptr = type;
  85. ++*ptr;
  86. *ptr = packvarint(f->size, *ptr);
  87. memmove(*ptr, f->data, f->size);
  88. *ptr += f->size;
  89. return 0;
  90. }
  91. int
  92. emit_optional_field(uint8_t type, const struct slice* f,
  93. unsigned char** ptr,
  94. unsigned char* const end)
  95. {
  96. return f->size ? emit_required_field(type, f, ptr, end) : 0;
  97. }
  98. int
  99. emit_eos(unsigned char** ptr, unsigned char* const end)
  100. {
  101. if (*ptr >= end) return -1;
  102. **ptr = EOS;
  103. ++*ptr;
  104. return 0;
  105. }
  106. size_t
  107. macaroon_serialize_v2(const struct macaroon* M,
  108. unsigned char* data, size_t data_sz,
  109. enum macaroon_returncode* err)
  110. {
  111. unsigned char* ptr = data;
  112. unsigned char* const end = ptr + data_sz;
  113. size_t i;
  114. if (ptr >= end) goto emit_buf_too_small;
  115. *ptr = 2;
  116. ++ptr;
  117. if (emit_optional_field(TYPE_LOCATION, &M->location, &ptr, end) < 0) goto emit_buf_too_small;
  118. if (emit_required_field(TYPE_IDENTIFIER, &M->identifier, &ptr, end) < 0) goto emit_buf_too_small;
  119. if (emit_eos(&ptr, end) < 0) goto emit_buf_too_small;
  120. for (i = 0; i < M->num_caveats; ++i)
  121. {
  122. const struct caveat* C = &M->caveats[i];
  123. if (emit_optional_field(TYPE_LOCATION, &C->cl, &ptr, end) < 0) goto emit_buf_too_small;
  124. if (emit_required_field(TYPE_IDENTIFIER, &C->cid, &ptr, end) < 0) goto emit_buf_too_small;
  125. if (emit_optional_field(TYPE_VID, &C->vid, &ptr, end) < 0) goto emit_buf_too_small;
  126. if (emit_eos(&ptr, end) < 0) goto emit_buf_too_small;
  127. }
  128. if (emit_eos(&ptr, end) < 0) goto emit_buf_too_small;
  129. if (emit_required_field(TYPE_SIGNATURE, &M->signature, &ptr, end) < 0) goto emit_buf_too_small;
  130. return ptr - data;
  131. emit_buf_too_small:
  132. *err = MACAROON_BUF_TOO_SMALL;
  133. return 0;
  134. }
  135. int
  136. parse_field(const unsigned char** _data,
  137. const unsigned char* const end,
  138. struct field* parsed)
  139. {
  140. const unsigned char* data = *_data;
  141. if (data >= end) return -1;
  142. uint64_t field = 0;
  143. uint64_t length = 0;
  144. data = unpackvarint(data, end, &field);
  145. if (!data) return -1;
  146. data = unpackvarint(data, end, &length);
  147. if (!data) return -1;
  148. if ((field & 0xffU) != field) return -1;
  149. if (data + length > end) return -1;
  150. parsed->type = field & 0xffU;
  151. parsed->data.data = data;
  152. parsed->data.size = length;
  153. data += length;
  154. assert(data <= end);
  155. *_data = data;
  156. return 0;
  157. }
  158. int
  159. parse_optional_field(const unsigned char** data,
  160. const unsigned char* const end,
  161. uint8_t type,
  162. struct field* parsed)
  163. {
  164. assert((type & 0x7fU) == type);
  165. if (*data >= end) return -1;
  166. if (*data == end || (*data < end && **data != type))
  167. {
  168. parsed->type = type;
  169. parsed->data.data = NULL;
  170. parsed->data.size = 0;
  171. return 0;
  172. }
  173. int ret = parse_field(data, end, parsed);
  174. assert(ret != 0 || parsed->type == type);
  175. return ret;
  176. }
  177. int
  178. parse_required_field(const unsigned char** data,
  179. const unsigned char* const end,
  180. uint8_t type,
  181. struct field* parsed)
  182. {
  183. assert((type & 0x7fU) == type);
  184. if (*data >= end) return -1;
  185. if (*data == end || (*data < end && **data != type))
  186. {
  187. return -1;
  188. }
  189. int ret = parse_field(data, end, parsed);
  190. assert(ret != 0 || parsed->type == type);
  191. return ret;
  192. }
  193. int
  194. parse_eos(const unsigned char** data, const unsigned char* const end)
  195. {
  196. int ret = (*data >= end || **data != EOS) ? -1 : 0;
  197. ++*data;
  198. return ret;
  199. }
  200. struct macaroon*
  201. macaroon_deserialize_v2(const unsigned char* data, size_t data_sz,
  202. enum macaroon_returncode* err)
  203. {
  204. const unsigned char* const end = data + data_sz;
  205. size_t i;
  206. if (data >= end || *data != 2)
  207. {
  208. *err = MACAROON_INVALID;
  209. return NULL;
  210. }
  211. ++data;
  212. struct caveat* caveats = malloc(sizeof(struct caveat) * 4);
  213. size_t caveats_cap = 4;
  214. size_t caveats_sz = 0;
  215. struct field location;
  216. struct field identifier;
  217. if (parse_optional_field(&data, end, TYPE_LOCATION, &location) < 0) goto parse_invalid;
  218. if (parse_required_field(&data, end, TYPE_IDENTIFIER, &identifier) < 0) goto parse_invalid;
  219. if (parse_eos(&data, end) < 0) goto parse_invalid;
  220. size_t body_sz = location.data.size + identifier.data.size;
  221. while (data < end && *data != EOS)
  222. {
  223. struct field cl;
  224. struct field cid;
  225. struct field vid;
  226. if (parse_optional_field(&data, end, TYPE_LOCATION, &cl) < 0) goto parse_invalid;
  227. if (parse_required_field(&data, end, TYPE_IDENTIFIER, &cid) < 0) goto parse_invalid;
  228. if (parse_optional_field(&data, end, TYPE_VID, &vid) < 0) goto parse_invalid;
  229. if (parse_eos(&data, end) < 0) goto parse_invalid;
  230. if (caveats_sz == caveats_cap)
  231. {
  232. caveats_cap *= 2;
  233. struct caveat* tmp = realloc(caveats, sizeof(struct caveat) * caveats_cap);
  234. if (!tmp) goto parse_invalid;
  235. caveats = tmp;
  236. }
  237. caveats[caveats_sz].cid = cid.data;
  238. caveats[caveats_sz].vid = vid.data;
  239. caveats[caveats_sz].cl = cl.data;
  240. ++caveats_sz;
  241. body_sz += cid.data.size + vid.data.size + cl.data.size;
  242. }
  243. if (parse_eos(&data, end) < 0) goto parse_invalid;
  244. struct field signature;
  245. if (parse_required_field(&data, end, TYPE_SIGNATURE, &signature) < 0) goto parse_invalid;
  246. body_sz += signature.data.size;
  247. unsigned char* ptr = NULL;
  248. struct macaroon* M = macaroon_malloc(caveats_sz, body_sz, &ptr);
  249. if (!M)
  250. {
  251. *err = MACAROON_OUT_OF_MEMORY;
  252. goto parse_error;
  253. }
  254. ptr = copy_slice(&location.data, &M->location, ptr);
  255. ptr = copy_slice(&identifier.data, &M->identifier, ptr);
  256. ptr = copy_slice(&signature.data, &M->signature, ptr);
  257. M->num_caveats = caveats_sz;
  258. for (i = 0; i < caveats_sz; ++i)
  259. {
  260. ptr = copy_slice(&caveats[i].cid, &M->caveats[i].cid, ptr);
  261. ptr = copy_slice(&caveats[i].vid, &M->caveats[i].vid, ptr);
  262. ptr = copy_slice(&caveats[i].cl, &M->caveats[i].cl, ptr);
  263. }
  264. free(caveats);
  265. return M;
  266. parse_invalid:
  267. *err = MACAROON_INVALID;
  268. parse_error:
  269. if (caveats)
  270. {
  271. free(caveats);
  272. }
  273. return NULL;
  274. }
  275. #define JSON_START "{\"v\":2"
  276. #define JSON_CAVEATS_START ",\"c\":["
  277. #define JSON_CAVEATS_FINISH "],"
  278. /* size prior to 64 suffix */
  279. #define JSON_MAX_FIELD_SIZE 1
  280. const char*
  281. json_field_type(uint8_t type)
  282. {
  283. /* If you elongate these strings, update JSON_MAX_FIELD_SIZE */
  284. switch (type)
  285. {
  286. case TYPE_LOCATION:
  287. return "l";
  288. case TYPE_IDENTIFIER:
  289. return "i";
  290. case TYPE_VID:
  291. return "v";
  292. case TYPE_SIGNATURE:
  293. return "s";
  294. default:
  295. return NULL;
  296. }
  297. }
  298. const char*
  299. json_field_type_b64(uint8_t type)
  300. {
  301. /* If you elongate these strings, update JSON_MAX_FIELD_SIZE */
  302. switch (type)
  303. {
  304. case TYPE_LOCATION:
  305. return "l64";
  306. case TYPE_IDENTIFIER:
  307. return "i64";
  308. case TYPE_VID:
  309. return "v64";
  310. case TYPE_SIGNATURE:
  311. return "s64";
  312. default:
  313. return NULL;
  314. }
  315. }
  316. const char*
  317. json_field_type_encoded(uint8_t type, int encoding)
  318. {
  319. switch (encoding)
  320. {
  321. case ENC_STR:
  322. return json_field_type(type);
  323. case ENC_B64:
  324. return json_field_type_b64(type);
  325. default:
  326. return NULL;
  327. }
  328. }
  329. size_t
  330. json_required_field_size(int encoding, const struct slice* f)
  331. {
  332. switch (encoding)
  333. {
  334. case ENC_STR:
  335. return 6 + JSON_MAX_FIELD_SIZE + f->size;
  336. case ENC_B64:
  337. return 6 + JSON_MAX_FIELD_SIZE + 2 + (8 * f->size + 7) / 6;
  338. default:
  339. abort();
  340. }
  341. }
  342. size_t
  343. json_optional_field_size(int encoding, const struct slice* f)
  344. {
  345. return f->size ? json_required_field_size(encoding, f) : 0;
  346. }
  347. size_t
  348. macaroon_serialize_size_hint_v2j(const struct macaroon* M)
  349. {
  350. size_t i;
  351. size_t sz = STRLENOF(JSON_START)
  352. + STRLENOF(JSON_CAVEATS_START)
  353. + STRLENOF(JSON_CAVEATS_FINISH)
  354. + 1 /* finishing */
  355. + json_optional_field_size(ENC_STR, &M->location)
  356. + json_required_field_size(ENC_STR, &M->identifier)
  357. + json_required_field_size(ENC_B64, &M->signature);
  358. for (i = 0; i < M->num_caveats; ++i)
  359. {
  360. sz += 3; /* ,{} */
  361. sz += json_optional_field_size(ENC_STR, &M->caveats[i].cl);
  362. sz += json_required_field_size(ENC_STR, &M->caveats[i].cid);
  363. sz += json_optional_field_size(ENC_STR, &M->caveats[i].vid);
  364. }
  365. return sz;
  366. }
  367. void
  368. json_emit_char(unsigned char c,
  369. unsigned char** ptr,
  370. unsigned char* const end)
  371. {
  372. assert(*ptr < end);
  373. **ptr = c;
  374. ++*ptr;
  375. }
  376. int
  377. json_emit_string(const char* str, size_t str_sz,
  378. unsigned char** ptr,
  379. unsigned char* const end)
  380. {
  381. if (*ptr + str_sz + 2 > end) return -1;
  382. json_emit_char('"', ptr, end);
  383. memmove(*ptr, str, str_sz);
  384. *ptr += str_sz;
  385. json_emit_char('"', ptr, end);
  386. return 0;
  387. }
  388. int
  389. json_emit_string_b64(const char* str, size_t str_sz,
  390. unsigned char** ptr,
  391. unsigned char* const end)
  392. {
  393. const size_t b64_sz = (str_sz * 8 + 7) / 6;
  394. if (*ptr + b64_sz + 2 > end) return -1;
  395. json_emit_char('"', ptr, end);
  396. #pragma GCC diagnostic push
  397. #pragma GCC diagnostic ignored "-Wpointer-sign"
  398. int ret = b64_ntop(str, str_sz, *ptr, end - *ptr);
  399. if (ret < 0) return -1;
  400. *ptr += ret;
  401. #pragma GCC diagnostic pop
  402. json_emit_char('"', ptr, end);
  403. return 0;
  404. }
  405. int
  406. json_emit_encoded_string(int encoding,
  407. const char* str, size_t str_sz,
  408. unsigned char** ptr,
  409. unsigned char* const end)
  410. {
  411. switch (encoding)
  412. {
  413. case ENC_STR:
  414. /* XXX check that it is UTF-8 and switch to other case if not */
  415. /* XXX if the above XXX is addressed, remember to adjust sz hint */
  416. return json_emit_string(str, str_sz, ptr, end);
  417. case ENC_B64:
  418. return json_emit_string_b64(str, str_sz, ptr, end);
  419. default:
  420. return -1;
  421. }
  422. }
  423. int
  424. json_emit_required_field(int comma, int encoding, uint8_t _type,
  425. const struct slice* f,
  426. unsigned char** ptr,
  427. unsigned char* const end)
  428. {
  429. const char* type = json_field_type_encoded(_type, encoding);
  430. assert(type);
  431. const size_t type_sz = strlen(type);
  432. const size_t sz = 6/*quote field + quote value + colon + comma */
  433. + type_sz + f->size;
  434. if (*ptr + sz > end) return -1;
  435. if (comma) json_emit_char(',', ptr, end);
  436. if (json_emit_string(type, type_sz, ptr, end) < 0) return -1;
  437. json_emit_char(':', ptr, end);
  438. if (json_emit_encoded_string(encoding, (const char*)f->data, f->size, ptr, end) < 0) return -1;
  439. assert(*ptr <= end);
  440. return 0;
  441. }
  442. int
  443. json_emit_optional_field(int comma, int encoding, uint8_t type,
  444. const struct slice* f,
  445. unsigned char** ptr,
  446. unsigned char* const end)
  447. {
  448. return f->size ? json_emit_required_field(comma, encoding, type, f, ptr, end) : 0;
  449. }
  450. int
  451. json_emit_start(unsigned char** ptr,
  452. unsigned char* const end)
  453. {
  454. const size_t sz = STRLENOF(JSON_START);
  455. if (*ptr + sz > end) return -1;
  456. memmove(*ptr, JSON_START, sz);
  457. *ptr += sz;
  458. return 0;
  459. }
  460. int
  461. json_emit_finish(unsigned char** ptr,
  462. unsigned char* const end)
  463. {
  464. if (*ptr >= end) return -1;
  465. json_emit_char('}', ptr, end);
  466. return 0;
  467. }
  468. int
  469. json_emit_caveats_start(unsigned char** ptr,
  470. unsigned char* const end)
  471. {
  472. const size_t sz = STRLENOF(JSON_CAVEATS_START);
  473. if (*ptr + sz > end) return -1;
  474. memmove(*ptr, JSON_CAVEATS_START, sz);
  475. *ptr += sz;
  476. return 0;
  477. }
  478. int
  479. json_emit_caveats_finish(unsigned char** ptr,
  480. unsigned char* const end)
  481. {
  482. const size_t sz = STRLENOF(JSON_CAVEATS_FINISH);
  483. if (*ptr + sz > end) return -1;
  484. memmove(*ptr, JSON_CAVEATS_FINISH, sz);
  485. *ptr += sz;
  486. return 0;
  487. }
  488. size_t
  489. macaroon_serialize_v2j(const struct macaroon* M,
  490. unsigned char* data, size_t data_sz,
  491. enum macaroon_returncode* err)
  492. {
  493. unsigned char* ptr = data;
  494. unsigned char* const end = ptr + data_sz;
  495. size_t i;
  496. if (ptr >= end) goto json_emit_buf_too_small;
  497. if (json_emit_start(&ptr, end) < 0) goto json_emit_buf_too_small;
  498. if (json_emit_optional_field(1, ENC_STR, TYPE_LOCATION, &M->location, &ptr, end) < 0) goto json_emit_buf_too_small;
  499. if (json_emit_required_field(1, ENC_STR, TYPE_IDENTIFIER, &M->identifier, &ptr, end) < 0) goto json_emit_buf_too_small;
  500. if (json_emit_caveats_start(&ptr, end) < 0) goto json_emit_buf_too_small;
  501. for (i = 0; i < M->num_caveats; ++i)
  502. {
  503. const struct caveat* C = &M->caveats[i];
  504. if (ptr + 3 >= end) goto json_emit_buf_too_small;
  505. if (i > 0) json_emit_char(',', &ptr, end);
  506. json_emit_char('{', &ptr, end);
  507. if (json_emit_required_field(0, ENC_STR, TYPE_IDENTIFIER, &C->cid, &ptr, end) < 0) goto json_emit_buf_too_small;
  508. if (json_emit_optional_field(1, ENC_STR, TYPE_LOCATION, &C->cl, &ptr, end) < 0) goto json_emit_buf_too_small;
  509. if (json_emit_optional_field(1, ENC_STR, TYPE_VID, &C->vid, &ptr, end) < 0) goto json_emit_buf_too_small;
  510. if (ptr >= end) goto json_emit_buf_too_small;
  511. json_emit_char('}', &ptr, end);
  512. }
  513. if (json_emit_caveats_finish(&ptr, end) < 0) goto json_emit_buf_too_small;
  514. if (json_emit_required_field(0, ENC_B64, TYPE_SIGNATURE, &M->signature, &ptr, end) < 0) goto json_emit_buf_too_small;
  515. if (json_emit_finish(&ptr, end) < 0) goto json_emit_buf_too_small;
  516. return ptr - data;
  517. json_emit_buf_too_small:
  518. *err = MACAROON_BUF_TOO_SMALL;
  519. return 0;
  520. }
  521. /* all but the top level parsing function only changes "err" if it's not
  522. * MACAROON_INVALID; it's assumed to already equal that.
  523. */
  524. void
  525. j2b_skip_whitespace(char** ptr, char* end)
  526. {
  527. while (*ptr < end)
  528. {
  529. if (!isspace(**ptr))
  530. {
  531. break;
  532. }
  533. ++*ptr;
  534. }
  535. }
  536. int
  537. j2b_string(char** ptr, char* end,
  538. enum macaroon_returncode* err, struct slice* s)
  539. {
  540. *err = MACAROON_INVALID;
  541. assert(*ptr < end);
  542. assert(**ptr == '"');
  543. ++*ptr;
  544. s->data = (const unsigned char*)*ptr;
  545. while (*ptr < end)
  546. {
  547. if (**ptr == '\\')
  548. {
  549. if (*ptr + 1 >= end)
  550. {
  551. return -1;
  552. }
  553. if ((*ptr)[1] == 'u')
  554. {
  555. if (*ptr + 6 >= end)
  556. {
  557. return -1;
  558. }
  559. *ptr += 6;
  560. }
  561. else
  562. {
  563. *ptr += 2;
  564. }
  565. }
  566. else if (**ptr == '"')
  567. {
  568. break;
  569. }
  570. else
  571. {
  572. ++*ptr;
  573. }
  574. }
  575. if (*ptr >= end)
  576. {
  577. return -1;
  578. }
  579. **ptr = '\0';
  580. s->size = (const unsigned char*)*ptr - s->data;
  581. ++*ptr;
  582. return 0;
  583. }
  584. int
  585. j2b_b64_decode(struct slice* s)
  586. {
  587. int ret;
  588. unsigned char* tmp = malloc(s->size);
  589. if (!tmp) return -1;
  590. ret = b64_pton((const char*)s->data, tmp, s->size);
  591. if (ret >= 0)
  592. {
  593. #pragma GCC diagnostic push
  594. #pragma GCC diagnostic ignored "-Wcast-qual"
  595. memmove((unsigned char*)s->data, tmp, ret);
  596. #pragma GCC diagnostic pop
  597. s->size = ret;
  598. ret = 0;
  599. }
  600. else
  601. {
  602. ret = -1;
  603. }
  604. free(tmp);
  605. return ret;
  606. }
  607. int
  608. j2b_caveat(char** ptr, char* end, enum macaroon_returncode* err, struct caveat* caveat)
  609. {
  610. struct slice s = EMPTY_SLICE;
  611. struct slice cl = EMPTY_SLICE;
  612. struct slice cid = EMPTY_SLICE;
  613. struct slice vid = EMPTY_SLICE;
  614. int seen_cl = 0;
  615. int seen_cid = 0;
  616. int seen_vid = 0;
  617. if (*ptr >= end) return -1;
  618. if (**ptr != '{') return -1;
  619. ++*ptr;
  620. int first = 1;
  621. while (*ptr < end)
  622. {
  623. j2b_skip_whitespace(ptr, end);
  624. if (*ptr < end && **ptr == '}')
  625. {
  626. break;
  627. }
  628. if (!first)
  629. {
  630. if (*ptr >= end || **ptr != ',') return -1;
  631. ++*ptr;
  632. }
  633. first = 0;
  634. j2b_skip_whitespace(ptr, end);
  635. if (*ptr >= end || **ptr != '"' ||
  636. j2b_string(ptr, end, err, &s) < 0)
  637. {
  638. return -1;
  639. }
  640. j2b_skip_whitespace(ptr, end);
  641. if (*ptr >= end || **ptr != ':') return -1;
  642. ++*ptr;
  643. j2b_skip_whitespace(ptr, end);
  644. if (s.size == 1 && memcmp("i", s.data, s.size) == 0)
  645. {
  646. if (seen_cid) return -1;
  647. if (j2b_string(ptr, end, err, &cid) < 0) return -1;
  648. seen_cid = 1;
  649. }
  650. else if (s.size == 1 && memcmp("l", s.data, s.size) == 0)
  651. {
  652. if (seen_cl) return -1;
  653. if (j2b_string(ptr, end, err, &cl) < 0) return -1;
  654. seen_cl = 1;
  655. }
  656. else if (s.size == 1 && memcmp("v", s.data, s.size) == 0)
  657. {
  658. if (seen_vid) return -1;
  659. if (j2b_string(ptr, end, err, &vid) < 0) return -1;
  660. seen_vid = 1;
  661. }
  662. else if (s.size == 3 && memcmp("i64", s.data, s.size) == 0)
  663. {
  664. if (seen_cid) return -1;
  665. if (j2b_string(ptr, end, err, &cid) < 0) return -1;
  666. seen_cid = 1;
  667. if (j2b_b64_decode(&cid) < 0)
  668. {
  669. *err = MACAROON_OUT_OF_MEMORY;
  670. return -1;
  671. }
  672. }
  673. else if (s.size == 3 && memcmp("l64", s.data, s.size) == 0)
  674. {
  675. if (seen_cl) return -1;
  676. if (j2b_string(ptr, end, err, &cl) < 0) return -1;
  677. seen_cl = 1;
  678. if (j2b_b64_decode(&cl) < 0)
  679. {
  680. *err = MACAROON_OUT_OF_MEMORY;
  681. return -1;
  682. }
  683. }
  684. else if (s.size == 3 && memcmp("v64", s.data, s.size) == 0)
  685. {
  686. if (seen_vid) return -1;
  687. if (j2b_string(ptr, end, err, &vid) < 0) return -1;
  688. seen_vid = 1;
  689. if (j2b_b64_decode(&vid) < 0)
  690. {
  691. *err = MACAROON_OUT_OF_MEMORY;
  692. return -1;
  693. }
  694. }
  695. else
  696. {
  697. return -1;
  698. }
  699. }
  700. if (*ptr >= end) return -1;
  701. ++*ptr;
  702. if (!seen_cid) return -1;
  703. caveat->cid = cid;
  704. caveat->vid = vid;
  705. caveat->cl = cl;
  706. return 0;
  707. }
  708. int
  709. j2b_caveats(char** ptr, char* end, enum macaroon_returncode* err,
  710. struct caveat** caveats, size_t* caveats_sz)
  711. {
  712. struct caveat* tmp = NULL;
  713. size_t caveats_cap = 4;
  714. *caveats_sz = 0;
  715. *caveats = malloc(sizeof(struct caveat) * caveats_cap);
  716. if (!*caveats)
  717. {
  718. *err = MACAROON_OUT_OF_MEMORY;
  719. return -1;
  720. }
  721. if (*ptr >= end || **ptr != '[') return -1;
  722. ++*ptr;
  723. j2b_skip_whitespace(ptr, end);
  724. while (*ptr < end)
  725. {
  726. if (**ptr == ']') break;
  727. if (*caveats_sz == caveats_cap)
  728. {
  729. caveats_cap = caveats_cap + (caveats_cap >> 1);
  730. tmp = realloc(*caveats, sizeof(struct caveat) * caveats_cap);
  731. if (!tmp)
  732. {
  733. *err = MACAROON_OUT_OF_MEMORY;
  734. return -1;
  735. }
  736. *caveats = tmp;
  737. }
  738. if (j2b_caveat(ptr, end, err, *caveats + *caveats_sz) < 0) return -1;
  739. ++*caveats_sz;
  740. j2b_skip_whitespace(ptr, end);
  741. if (*ptr >= end) return -1;
  742. if (**ptr == ',')
  743. {
  744. ++*ptr;
  745. j2b_skip_whitespace(ptr, end);
  746. }
  747. else if (**ptr != ']')
  748. {
  749. return -1;;
  750. }
  751. }
  752. if (*ptr >= end) return -1;
  753. ++*ptr;
  754. return 0;
  755. }
  756. struct macaroon*
  757. j2b_macaroon(char** ptr, char* end,
  758. enum macaroon_returncode* err)
  759. {
  760. struct macaroon* M = NULL;
  761. struct slice s;
  762. struct slice location;
  763. struct slice identifier;
  764. struct slice signature;
  765. int seen_location = 0;
  766. int seen_identifier = 0;
  767. int seen_signature = 0;
  768. int seen_caveats = 0;
  769. /* allocated by j2b_caveats */
  770. struct caveat* caveats = NULL;
  771. size_t caveats_sz = 0;
  772. size_t i = 0;
  773. *err = MACAROON_INVALID;
  774. j2b_skip_whitespace(ptr, end);
  775. if (*ptr >= end) goto invalid;
  776. if (**ptr != '{') goto invalid;
  777. ++*ptr;
  778. int first = 1;
  779. while (*ptr < end)
  780. {
  781. j2b_skip_whitespace(ptr, end);
  782. if (*ptr < end && **ptr == '}')
  783. {
  784. break;
  785. }
  786. if (!first)
  787. {
  788. if (*ptr >= end || **ptr != ',') goto invalid;
  789. ++*ptr;
  790. }
  791. first = 0;
  792. j2b_skip_whitespace(ptr, end);
  793. if (*ptr >= end || **ptr != '"' ||
  794. j2b_string(ptr, end, err, &s) < 0)
  795. {
  796. goto invalid;
  797. }
  798. j2b_skip_whitespace(ptr, end);
  799. if (*ptr >= end || **ptr != ':') goto invalid;
  800. ++*ptr;
  801. j2b_skip_whitespace(ptr, end);
  802. if (s.size == 1 && memcmp("v", s.data, s.size) == 0)
  803. {
  804. if (**ptr != '2') goto invalid;
  805. ++*ptr;
  806. j2b_skip_whitespace(ptr, end);
  807. }
  808. else if (s.size == 1 && memcmp("i", s.data, s.size) == 0)
  809. {
  810. if (seen_identifier) goto invalid;
  811. if (j2b_string(ptr, end, err, &identifier) < 0) goto invalid;
  812. seen_identifier = 1;
  813. }
  814. else if (s.size == 1 && memcmp("l", s.data, s.size) == 0)
  815. {
  816. if (seen_location) goto invalid;
  817. if (j2b_string(ptr, end, err, &location) < 0) goto invalid;
  818. seen_location = 1;
  819. }
  820. else if (s.size == 1 && memcmp("s", s.data, s.size) == 0)
  821. {
  822. if (seen_signature) goto invalid;
  823. if (j2b_string(ptr, end, err, &signature) < 0) goto invalid;
  824. seen_signature = 1;
  825. }
  826. else if (s.size == 1 && memcmp("c", s.data, s.size) == 0)
  827. {
  828. if (seen_caveats) goto invalid;
  829. seen_caveats = 1;
  830. if (j2b_caveats(ptr, end, err, &caveats, &caveats_sz) < 0) goto error;
  831. }
  832. else if (s.size == 3 && memcmp("i64", s.data, s.size) == 0)
  833. {
  834. if (seen_identifier) goto invalid;
  835. if (j2b_string(ptr, end, err, &identifier) < 0) goto invalid;
  836. seen_identifier = 1;
  837. if (j2b_b64_decode(&identifier) < 0)
  838. {
  839. *err = MACAROON_OUT_OF_MEMORY;
  840. goto error;
  841. }
  842. }
  843. else if (s.size == 3 && memcmp("l64", s.data, s.size) == 0)
  844. {
  845. if (seen_location) goto invalid;
  846. if (j2b_string(ptr, end, err, &location) < 0) goto invalid;
  847. seen_location = 1;
  848. if (j2b_b64_decode(&location) < 0)
  849. {
  850. *err = MACAROON_OUT_OF_MEMORY;
  851. goto error;
  852. }
  853. }
  854. else if (s.size == 3 && memcmp("s64", s.data, s.size) == 0)
  855. {
  856. if (seen_signature) goto invalid;
  857. if (j2b_string(ptr, end, err, &signature) < 0) goto invalid;
  858. seen_signature = 1;
  859. if (j2b_b64_decode(&signature) < 0)
  860. {
  861. *err = MACAROON_OUT_OF_MEMORY;
  862. goto error;
  863. }
  864. }
  865. else
  866. {
  867. goto invalid;
  868. }
  869. }
  870. /* on a good exit ptr will point to '}', so error out it doesn't, advance
  871. * it, skip the whitespace, and error out if there are trailing characters
  872. */
  873. if (*ptr >= end) goto invalid;
  874. ++*ptr;
  875. j2b_skip_whitespace(ptr, end);
  876. if (*ptr != end) goto invalid;
  877. /* sanity check */
  878. if (!seen_signature || !seen_identifier || !seen_caveats) goto invalid;
  879. unsigned char* write = NULL;
  880. M = macaroon_malloc(caveats_sz, 10000/*body_sz*/, &write);
  881. if (!M)
  882. {
  883. *err = MACAROON_OUT_OF_MEMORY;
  884. goto error;
  885. }
  886. write = copy_slice(&location, &M->location, write);
  887. write = copy_slice(&identifier, &M->identifier, write);
  888. write = copy_slice(&signature, &M->signature, write);
  889. M->num_caveats = caveats_sz;
  890. for (i = 0; i < caveats_sz; ++i)
  891. {
  892. write = copy_slice(&caveats[i].cid, &M->caveats[i].cid, write);
  893. write = copy_slice(&caveats[i].vid, &M->caveats[i].vid, write);
  894. write = copy_slice(&caveats[i].cl, &M->caveats[i].cl, write);
  895. }
  896. free(caveats);
  897. return M;
  898. invalid:
  899. *err = MACAROON_INVALID;
  900. error:
  901. if (caveats)
  902. {
  903. free(caveats);
  904. }
  905. return NULL;
  906. }
  907. struct macaroon*
  908. macaroon_deserialize_v2j(const unsigned char* data, size_t data_sz,
  909. enum macaroon_returncode* err)
  910. {
  911. struct macaroon* M = NULL;
  912. char* copy = malloc(data_sz);
  913. char* ptr = copy;
  914. char* const end = ptr + data_sz;
  915. if (!copy)
  916. {
  917. *err = MACAROON_OUT_OF_MEMORY;
  918. return NULL;
  919. }
  920. memmove(copy, data, data_sz);
  921. M = j2b_macaroon(&ptr, end, err);
  922. free(copy);
  923. return M;
  924. }