macaroons.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #ifndef macaroons_h_
  29. #define macaroons_h_
  30. /* C */
  31. #include <stdlib.h>
  32. #ifdef __cplusplus
  33. extern "C"
  34. {
  35. #endif /* __cplusplus */
  36. /* All byte strings must be less than this length.
  37. * Enforced via "assert" internally.
  38. * */
  39. #define MACAROON_MAX_STRLEN 32768
  40. /* Place a sane limit on the number of caveats */
  41. #define MACAROON_MAX_CAVEATS 65536
  42. /* Recommended secret length */
  43. #define MACAROON_SUGGESTED_SECRET_LENGTH 32
  44. /* Opaque type whose internals are private to libmacaroons */
  45. struct macaroon;
  46. struct macaroon_verifier;
  47. enum macaroon_returncode
  48. {
  49. MACAROON_SUCCESS = 2048,
  50. MACAROON_OUT_OF_MEMORY = 2049,
  51. MACAROON_HASH_FAILED = 2050,
  52. MACAROON_INVALID = 2051,
  53. MACAROON_TOO_MANY_CAVEATS = 2052,
  54. MACAROON_CYCLE = 2053,
  55. MACAROON_BUF_TOO_SMALL = 2054,
  56. MACAROON_NOT_AUTHORIZED = 2055,
  57. MACAROON_NO_JSON_SUPPORT = 2056,
  58. MACAROON_UNSUPPORTED_FORMAT=2057
  59. };
  60. const char*
  61. macaroon_error(enum macaroon_returncode err);
  62. /* Create a new macaroon.
  63. * - location/location_sz is a hint to the target's location
  64. * - key/key_sz is the key used as a secret for macaroon construction
  65. * - id/id_sz is the public identifier the macaroon issuer can use to identify
  66. * the key
  67. */
  68. struct macaroon*
  69. macaroon_create(const unsigned char* location, size_t location_sz,
  70. const unsigned char* key, size_t key_sz,
  71. const unsigned char* id, size_t id_sz,
  72. enum macaroon_returncode* err);
  73. /* Destroy a macaroon, freeing resources */
  74. void
  75. macaroon_destroy(struct macaroon* M);
  76. /* Check a macaroon's integrity
  77. *
  78. * This routine is used internally, and is exposed as part of the public API for
  79. * use in assert() statements.
  80. *
  81. * 0 -> all good
  82. * !0 -> no good
  83. */
  84. int
  85. macaroon_validate(const struct macaroon* M);
  86. /* Add a new first party caveat, and return a new macaroon.
  87. * - predicate/predicate_sz is the caveat to be added to the macaroon
  88. *
  89. * Returns a new macaroon, leaving the original untouched.
  90. */
  91. struct macaroon*
  92. macaroon_add_first_party_caveat(const struct macaroon* M,
  93. const unsigned char* predicate, size_t predicate_sz,
  94. enum macaroon_returncode* err);
  95. /* Add a new third party caveat, and return a new macaroon.
  96. * - location/location_sz is a hint to the third party's location
  97. * - key/keys_sz is a secret shared shared between this macaroon and the third
  98. * party. Guard it as carefully as you do the key for macaroon_create.
  99. * - id/id_sz is the identifier for this macaroon. If presented to the third
  100. * party, the third party must be able to recall the secret and predicate to
  101. * check.
  102. * A good way to generate this ID is to generate N random bytes as the key,
  103. * and encrypt these bytes and the caveat. Pass the bytes and N as the key,
  104. * and pass the ciphertext as the ID.
  105. *
  106. * Returns a new macaroon, leaving the original untouched.
  107. */
  108. struct macaroon*
  109. macaroon_add_third_party_caveat(const struct macaroon* M,
  110. const unsigned char* location, size_t location_sz,
  111. const unsigned char* key, size_t key_sz,
  112. const unsigned char* id, size_t id_sz,
  113. enum macaroon_returncode* err);
  114. /* Where are the third-parties that give discharge macaroons? */
  115. unsigned
  116. macaroon_num_third_party_caveats(const struct macaroon* M);
  117. int
  118. macaroon_third_party_caveat(const struct macaroon* M, unsigned which,
  119. const unsigned char** location, size_t* location_sz,
  120. const unsigned char** identifier, size_t* identifier_sz);
  121. /* Prepare the macaroon for a request */
  122. struct macaroon*
  123. macaroon_prepare_for_request(const struct macaroon* M,
  124. const struct macaroon* D,
  125. enum macaroon_returncode* err);
  126. /* Verification tool for verifying macaroons */
  127. struct macaroon_verifier*
  128. macaroon_verifier_create();
  129. void
  130. macaroon_verifier_destroy(struct macaroon_verifier* V);
  131. int
  132. macaroon_verifier_satisfy_exact(struct macaroon_verifier* V,
  133. const unsigned char* predicate, size_t predicate_sz,
  134. enum macaroon_returncode* err);
  135. int
  136. macaroon_verifier_satisfy_general(struct macaroon_verifier* V,
  137. int (*general_check)(void* f, const unsigned char* pred, size_t pred_sz),
  138. void* f, enum macaroon_returncode* err);
  139. int
  140. macaroon_verify(const struct macaroon_verifier* V,
  141. const struct macaroon* M,
  142. const unsigned char* key, size_t key_sz,
  143. struct macaroon** MS, size_t MS_sz,
  144. enum macaroon_returncode* err);
  145. /* Access routines for the macaroon */
  146. void
  147. macaroon_location(const struct macaroon* M,
  148. const unsigned char** location, size_t* location_sz);
  149. void
  150. macaroon_identifier(const struct macaroon* M,
  151. const unsigned char** identifier, size_t* identifier_sz);
  152. void
  153. macaroon_signature(const struct macaroon* M,
  154. const unsigned char** signature, size_t* signature_sz);
  155. /* Serialize and deserialize macaroons */
  156. enum macaroon_format
  157. {
  158. MACAROON_V1,
  159. MACAROON_V2,
  160. MACAROON_V2J
  161. };
  162. #define MACAROON_LATEST MACAROON_V2
  163. #define MACAROON_LATEST_JSON MACAROON_V2J
  164. /* a return value of 0 indicates an unsupported format
  165. * a return value >0 indicates the number of bytes necessary to serialize M
  166. * using format f
  167. */
  168. size_t
  169. macaroon_serialize_size_hint(const struct macaroon* M,
  170. enum macaroon_format f);
  171. /* a return value of 0 indicates an error;
  172. * a return value >0 indicates the number of bytes written to the buffer
  173. */
  174. size_t
  175. macaroon_serialize(const struct macaroon* M,
  176. enum macaroon_format f,
  177. unsigned char* buf, size_t buf_sz,
  178. enum macaroon_returncode* err);
  179. struct macaroon*
  180. macaroon_deserialize(const unsigned char* data, size_t data_sz,
  181. enum macaroon_returncode* err);
  182. /* Human-readable representation *FOR DEBUGGING ONLY* */
  183. size_t
  184. macaroon_inspect_size_hint(const struct macaroon* M);
  185. int
  186. macaroon_inspect(const struct macaroon* M,
  187. char* data, size_t data_sz,
  188. enum macaroon_returncode* err);
  189. /* Utilities for manipulating and comparing macaroons */
  190. /* allocate a new copy of the macaroon */
  191. struct macaroon*
  192. macaroon_copy(const struct macaroon* M,
  193. enum macaroon_returncode* err);
  194. /* 0 if equal; !0 if non-equal; no other comparison implied */
  195. int
  196. macaroon_cmp(const struct macaroon* M, const struct macaroon* N);
  197. #ifdef __cplusplus
  198. } /* extern "C" */
  199. #endif /* __cplusplus */
  200. #endif /* macaroons_h_ */