v1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* Copyright (c) 2014, 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 <string.h>
  31. /* macaroons */
  32. #include "constants.h"
  33. #include "packet.h"
  34. #include "v1.h"
  35. #pragma GCC diagnostic ignored "-Wcast-qual"
  36. enum encoding
  37. {
  38. ENCODING_RAW,
  39. ENCODING_BASE64,
  40. ENCODING_HEX
  41. };
  42. static size_t
  43. encoded_size(enum encoding encoding, size_t data_sz)
  44. {
  45. switch (encoding)
  46. {
  47. case ENCODING_HEX:
  48. return data_sz * 2;
  49. case ENCODING_BASE64:
  50. return (data_sz + 2) / 3 * 4;
  51. case ENCODING_RAW:
  52. return data_sz;
  53. default:
  54. assert(0);
  55. }
  56. }
  57. static size_t
  58. macaroon_inner_size_hint(const struct macaroon* M)
  59. {
  60. size_t i;
  61. size_t sz = PACKET_SIZE(LOCATION, M->location.size)
  62. + PACKET_SIZE(IDENTIFIER, M->identifier.size)
  63. + PACKET_SIZE(SIGNATURE, M->signature.size);
  64. assert(M);
  65. VALIDATE(M);
  66. for (i = 0; i < M->num_caveats; ++i)
  67. {
  68. sz += PACKET_SIZE(CID, M->caveats[i].cid.size);
  69. sz += PACKET_SIZE(VID, M->caveats[i].vid.size);
  70. sz += PACKET_SIZE(CL, M->caveats[i].cl.size);
  71. }
  72. return sz;
  73. }
  74. static size_t
  75. macaroon_inner_size_hint_ascii(const struct macaroon* M)
  76. {
  77. size_t i;
  78. size_t sz = PACKET_SIZE(LOCATION, M->location.size)
  79. + PACKET_SIZE(IDENTIFIER, M->identifier.size)
  80. + PACKET_SIZE(SIGNATURE, encoded_size(ENCODING_HEX, M->signature.size));
  81. assert(M);
  82. VALIDATE(M);
  83. for (i = 0; i < M->num_caveats; ++i)
  84. {
  85. sz += PACKET_SIZE(CID, M->caveats[i].cid.size);
  86. sz += PACKET_SIZE(VID, encoded_size(ENCODING_BASE64, M->caveats[i].vid.size));
  87. sz += PACKET_SIZE(CL, M->caveats[i].cl.size);
  88. }
  89. return sz;
  90. }
  91. size_t
  92. macaroon_serialize_size_hint_v1(const struct macaroon* M)
  93. {
  94. return encoded_size(ENCODING_BASE64, macaroon_inner_size_hint(M)) + 1;
  95. }
  96. int
  97. macaroon_serialize_v1(const struct macaroon* M,
  98. char* data, size_t data_sz,
  99. enum macaroon_returncode* err)
  100. {
  101. const size_t sz = macaroon_serialize_size_hint_v1(M);
  102. size_t i;
  103. unsigned char* tmp = NULL;
  104. unsigned char* ptr = NULL;
  105. int rc = 0;
  106. if (data_sz < sz)
  107. {
  108. *err = MACAROON_BUF_TOO_SMALL;
  109. return -1;
  110. }
  111. tmp = malloc(sizeof(unsigned char) * sz);
  112. if (!tmp)
  113. {
  114. *err = MACAROON_OUT_OF_MEMORY;
  115. return -1;
  116. }
  117. ptr = tmp;
  118. ptr = serialize_slice_as_packet(LOCATION, LOCATION_SZ, &M->location, ptr);
  119. ptr = serialize_slice_as_packet(IDENTIFIER, IDENTIFIER_SZ, &M->identifier, ptr);
  120. for (i = 0; i < M->num_caveats; ++i)
  121. {
  122. if (M->caveats[i].cid.size)
  123. {
  124. ptr = serialize_slice_as_packet(CID, CID_SZ, &M->caveats[i].cid, ptr);
  125. }
  126. if (M->caveats[i].vid.size)
  127. {
  128. ptr = serialize_slice_as_packet(VID, VID_SZ, &M->caveats[i].vid, ptr);
  129. }
  130. if (M->caveats[i].cl.size)
  131. {
  132. ptr = serialize_slice_as_packet(CL, CL_SZ, &M->caveats[i].cl, ptr);
  133. }
  134. }
  135. ptr = serialize_slice_as_packet(SIGNATURE, SIGNATURE_SZ, &M->signature, ptr);
  136. rc = b64_ntop(tmp, ptr - tmp, data, data_sz);
  137. free(tmp);
  138. if (rc < 0)
  139. {
  140. *err = MACAROON_BUF_TOO_SMALL;
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. struct macaroon*
  146. macaroon_deserialize_v1(const char* _data, const size_t _data_sz, enum macaroon_returncode* err)
  147. {
  148. size_t num_pkts = 0;
  149. struct packet pkt = EMPTY_PACKET;
  150. unsigned char* data = NULL;
  151. const unsigned char* end = NULL;
  152. const unsigned char* rptr = NULL;
  153. unsigned char* wptr = NULL;
  154. const unsigned char* tmp = NULL;
  155. const unsigned char* sig;
  156. const unsigned char* key;
  157. const unsigned char* val;
  158. size_t data_sz;
  159. size_t key_sz;
  160. size_t val_sz;
  161. int b64_sz;
  162. struct macaroon* M;
  163. data = malloc(sizeof(unsigned char) * _data_sz);
  164. if (!data)
  165. {
  166. *err = MACAROON_OUT_OF_MEMORY;
  167. return NULL;
  168. }
  169. b64_sz = b64_pton(_data, data, _data_sz);
  170. if (b64_sz <= 0)
  171. {
  172. *err = MACAROON_INVALID;
  173. free(data);
  174. return NULL;
  175. }
  176. if (data[0] == '{')
  177. {
  178. *err = MACAROON_NO_JSON_SUPPORT;
  179. free(data);
  180. return NULL;
  181. }
  182. data_sz = b64_sz;
  183. rptr = data;
  184. end = rptr + data_sz;
  185. while (rptr && rptr < end)
  186. {
  187. rptr = parse_packet(rptr, end, &pkt);
  188. ++num_pkts;
  189. }
  190. if (!rptr || num_pkts < 3)
  191. {
  192. *err = MACAROON_INVALID;
  193. free(data);
  194. return NULL;
  195. }
  196. assert(num_pkts < data_sz);
  197. M = macaroon_malloc((num_pkts - 3/*loc,id,sig*/), data_sz, &wptr);
  198. if (!M)
  199. {
  200. *err = MACAROON_OUT_OF_MEMORY;
  201. free(data);
  202. return NULL;
  203. }
  204. rptr = data;
  205. *err = MACAROON_INVALID;
  206. /* location */
  207. if (copy_if_parses(&rptr, end, parse_location_packet, &M->location, &wptr) < 0)
  208. {
  209. free(M);
  210. free(data);
  211. return NULL;
  212. }
  213. /* identifier */
  214. if (copy_if_parses(&rptr, end, parse_identifier_packet, &M->identifier, &wptr) < 0)
  215. {
  216. free(M);
  217. free(data);
  218. return NULL;
  219. }
  220. M->num_caveats = 0;
  221. while (1)
  222. {
  223. tmp = parse_packet(rptr, end, &pkt);
  224. if (parse_kv_packet(&pkt, &key, &key_sz, &val, &val_sz) < 0)
  225. {
  226. break;
  227. }
  228. if (key_sz == CID_SZ && memcmp(key, CID, CID_SZ) == 0)
  229. {
  230. if (M->caveats[M->num_caveats].cid.size)
  231. {
  232. ++M->num_caveats;
  233. }
  234. wptr = copy_to_slice(val, val_sz, &M->caveats[M->num_caveats].cid, wptr);
  235. }
  236. else if (key_sz == VID_SZ && memcmp(key, VID, VID_SZ) == 0)
  237. {
  238. if (M->caveats[M->num_caveats].vid.size)
  239. {
  240. free(M);
  241. free(data);
  242. return NULL;
  243. }
  244. wptr = copy_to_slice(val, val_sz, &M->caveats[M->num_caveats].vid, wptr);
  245. }
  246. else if (key_sz == CL_SZ && memcmp(key, CL, CL_SZ) == 0)
  247. {
  248. if (M->caveats[M->num_caveats].cl.size)
  249. {
  250. free(M);
  251. free(data);
  252. return NULL;
  253. }
  254. wptr = copy_to_slice(val, val_sz, &M->caveats[M->num_caveats].cl, wptr);
  255. }
  256. else
  257. {
  258. break;
  259. }
  260. /* advance to the next packet */
  261. rptr = tmp;
  262. }
  263. /* catch the tail packet */
  264. if (M->caveats[M->num_caveats].cid.size)
  265. {
  266. ++M->num_caveats;
  267. }
  268. /* signature */
  269. rptr = parse_packet(rptr, end, &pkt);
  270. assert(rptr);
  271. if (parse_signature_packet(&pkt, &sig) < 0)
  272. {
  273. free(M);
  274. free(data);
  275. return NULL;
  276. }
  277. wptr = copy_to_slice(sig, MACAROON_HASH_BYTES, &M->signature, wptr);
  278. if (macaroon_validate(M) < 0)
  279. {
  280. free(M);
  281. free(data);
  282. return NULL;
  283. }
  284. free(data);
  285. *err = MACAROON_SUCCESS;
  286. return M;
  287. }
  288. size_t
  289. macaroon_inspect_size_hint_v1(const struct macaroon* M)
  290. {
  291. /* TODO why the extra MACAROON_HASH_BYTES here? */
  292. return macaroon_inner_size_hint_ascii(M) + MACAROON_HASH_BYTES;
  293. }
  294. /*
  295. * encode encodes the given data, putting
  296. * the resulting data and size into result and result_sz.
  297. * On return, if *result != data, the caller is
  298. * responsible for freeing it.
  299. */
  300. static int
  301. encode(enum encoding encoding,
  302. const unsigned char* val, size_t val_sz,
  303. const unsigned char** result, size_t* result_sz,
  304. enum macaroon_returncode* err)
  305. {
  306. char* enc;
  307. int enc_sz;
  308. if (encoding == ENCODING_RAW) {
  309. *result = val;
  310. *result_sz = val_sz;
  311. return 0;
  312. }
  313. enc_sz = encoded_size(encoding, val_sz);
  314. enc = malloc(enc_sz + 1);
  315. if (enc == NULL)
  316. {
  317. *err = MACAROON_OUT_OF_MEMORY;
  318. return -1;
  319. }
  320. switch (encoding)
  321. {
  322. case ENCODING_BASE64:
  323. enc_sz = b64_ntop(val, val_sz, enc, enc_sz + 1);
  324. if (enc_sz < 0)
  325. {
  326. *err = MACAROON_BUF_TOO_SMALL;
  327. return -1;
  328. }
  329. break;
  330. case ENCODING_HEX:
  331. macaroon_bin2hex(val, val_sz, enc);
  332. break;
  333. case ENCODING_RAW: /* should never get here */
  334. default:
  335. assert(0);
  336. }
  337. *result = (const unsigned char*)enc;
  338. *result_sz = enc_sz;
  339. return 0;
  340. }
  341. static char*
  342. inspect_packet(const char* key,
  343. const struct slice* from,
  344. enum encoding encoding,
  345. char* ptr, char* ptr_end,
  346. enum macaroon_returncode *err)
  347. {
  348. const unsigned char* enc_val = NULL;
  349. size_t key_sz = strlen(key);
  350. size_t enc_sz = 0;
  351. size_t total_sz = 0;
  352. if (encode(encoding, from->data, from->size, &enc_val, &enc_sz, err) < 0)
  353. {
  354. return NULL;
  355. }
  356. total_sz = key_sz + 1 + enc_sz + 1;
  357. assert(ptr_end >= ptr);
  358. assert(total_sz <= (size_t)(ptr_end - ptr));
  359. memmove(ptr, key, key_sz);
  360. ptr[key_sz] = ' ';
  361. memmove(ptr + key_sz + 1, enc_val, enc_sz);
  362. ptr[key_sz + 1 + enc_sz] = '\n';
  363. if (enc_val != from->data)
  364. {
  365. free((void *)enc_val);
  366. }
  367. return ptr + total_sz;
  368. }
  369. int
  370. macaroon_inspect_v1(const struct macaroon* M,
  371. char* data, size_t data_sz,
  372. enum macaroon_returncode* err)
  373. {
  374. const size_t sz = macaroon_inspect_size_hint(M);
  375. size_t i = 0;
  376. char* ptr = data;
  377. char* ptr_end = data + data_sz;
  378. if (data_sz < sz)
  379. {
  380. *err = MACAROON_BUF_TOO_SMALL;
  381. return -1;
  382. }
  383. ptr = inspect_packet(LOCATION, &M->location, ENCODING_RAW, ptr, ptr_end, err);
  384. if (ptr == NULL)
  385. {
  386. return -1;
  387. }
  388. ptr = inspect_packet(IDENTIFIER, &M->identifier, ENCODING_RAW, ptr, ptr_end, err);
  389. if (ptr == NULL)
  390. {
  391. return -1;
  392. }
  393. for (i = 0; i < M->num_caveats; ++i)
  394. {
  395. if (M->caveats[i].cid.size)
  396. {
  397. ptr = inspect_packet(CID, &M->caveats[i].cid, ENCODING_RAW, ptr, ptr_end, err);
  398. if (ptr == NULL)
  399. {
  400. return -1;
  401. }
  402. }
  403. if (M->caveats[i].vid.size)
  404. {
  405. ptr = inspect_packet(VID, &M->caveats[i].vid, ENCODING_BASE64, ptr, ptr_end, err);
  406. if (ptr == NULL)
  407. {
  408. return -1;
  409. }
  410. }
  411. if (M->caveats[i].cl.size)
  412. {
  413. ptr = inspect_packet(CL, &M->caveats[i].cl, ENCODING_RAW, ptr, ptr_end, err);
  414. if (ptr == NULL)
  415. {
  416. return -1;
  417. }
  418. }
  419. }
  420. ptr = inspect_packet(SIGNATURE, &M->signature, ENCODING_HEX, ptr, ptr_end, err);
  421. if (ptr == NULL)
  422. {
  423. return -1;
  424. }
  425. /* Replace final newline with terminator. */
  426. ptr[-1] = '\0';
  427. return 0;
  428. }