trust_token.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* Copyright (c) 2020, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #ifndef OPENSSL_HEADER_TRUST_TOKEN_H
  15. #define OPENSSL_HEADER_TRUST_TOKEN_H
  16. #include <openssl/base.h>
  17. #include <openssl/stack.h>
  18. #if defined(__cplusplus)
  19. extern "C" {
  20. #endif
  21. // Trust Token implementation.
  22. //
  23. // Trust Token is an implementation of an experimental mechanism similar to
  24. // Privacy Pass which allows issuance and redemption of anonymized tokens with
  25. // limited private metadata.
  26. //
  27. // References:
  28. // https://eprint.iacr.org/2020/072.pdf
  29. // https://github.com/alxdavids/privacy-pass-ietf/tree/master/drafts
  30. // https://github.com/WICG/trust-token-api/blob/master/README.md
  31. //
  32. // WARNING: This API is unstable and subject to change.
  33. // TRUST_TOKEN_experiment_v1 is an experimental Trust Tokens protocol using
  34. // PMBTokens and P-384.
  35. OPENSSL_EXPORT const TRUST_TOKEN_METHOD *TRUST_TOKEN_experiment_v1(void);
  36. // TRUST_TOKEN_experiment_v2_voprf is an experimental Trust Tokens protocol
  37. // using VOPRFs and P-384 with up to 6 keys, without RR verification.
  38. OPENSSL_EXPORT const TRUST_TOKEN_METHOD *TRUST_TOKEN_experiment_v2_voprf(void);
  39. // TRUST_TOKEN_experiment_v2_pmb is an experimental Trust Tokens protocol using
  40. // PMBTokens and P-384 with up to 3 keys, without RR verification.
  41. OPENSSL_EXPORT const TRUST_TOKEN_METHOD *TRUST_TOKEN_experiment_v2_pmb(void);
  42. // trust_token_st represents a single-use token for the Trust Token protocol.
  43. // For the client, this is the token and its corresponding signature. For the
  44. // issuer, this is the token itself.
  45. struct trust_token_st {
  46. uint8_t *data;
  47. size_t len;
  48. };
  49. DEFINE_STACK_OF(TRUST_TOKEN)
  50. // TRUST_TOKEN_new creates a newly-allocated |TRUST_TOKEN| with value |data| or
  51. // NULL on allocation failure.
  52. OPENSSL_EXPORT TRUST_TOKEN *TRUST_TOKEN_new(const uint8_t *data, size_t len);
  53. // TRUST_TOKEN_free releases memory associated with |token|.
  54. OPENSSL_EXPORT void TRUST_TOKEN_free(TRUST_TOKEN *token);
  55. #define TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE 512
  56. #define TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE 512
  57. // TRUST_TOKEN_generate_key creates a new Trust Token keypair labeled with |id|
  58. // and serializes the private and public keys, writing the private key to
  59. // |out_priv_key| and setting |*out_priv_key_len| to the number of bytes
  60. // written, and writing the public key to |out_pub_key| and setting
  61. // |*out_pub_key_len| to the number of bytes written.
  62. //
  63. // At most |max_priv_key_len| and |max_pub_key_len| bytes are written. In order
  64. // to ensure success, these should be at least
  65. // |TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE| and |TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE|.
  66. //
  67. // WARNING: This API is unstable and the serializations of these keys are
  68. // subject to change. Keys generated with this function may not be persisted.
  69. //
  70. // This function returns one on success or zero on error.
  71. OPENSSL_EXPORT int TRUST_TOKEN_generate_key(
  72. const TRUST_TOKEN_METHOD *method, uint8_t *out_priv_key,
  73. size_t *out_priv_key_len, size_t max_priv_key_len, uint8_t *out_pub_key,
  74. size_t *out_pub_key_len, size_t max_pub_key_len, uint32_t id);
  75. // Trust Token client implementation.
  76. //
  77. // These functions implements the client half of the Trust Token protocol. A
  78. // single |TRUST_TOKEN_CLIENT| can perform a single protocol operation.
  79. // TRUST_TOKEN_CLIENT_new returns a newly-allocated |TRUST_TOKEN_CLIENT|
  80. // configured to use a max batchsize of |max_batchsize| or NULL on error.
  81. // Issuance requests must be made in batches smaller than |max_batchsize|. This
  82. // function will return an error if |max_batchsize| is too large for Trust
  83. // Tokens.
  84. OPENSSL_EXPORT TRUST_TOKEN_CLIENT *TRUST_TOKEN_CLIENT_new(
  85. const TRUST_TOKEN_METHOD *method, size_t max_batchsize);
  86. // TRUST_TOKEN_CLIENT_free releases memory associated with |ctx|.
  87. OPENSSL_EXPORT void TRUST_TOKEN_CLIENT_free(TRUST_TOKEN_CLIENT *ctx);
  88. // TRUST_TOKEN_CLIENT_add_key configures the |ctx| to support the public key
  89. // |key|. It sets |*out_key_index| to the index this key has been configured to.
  90. // It returns one on success or zero on error if the |key| can't be parsed or
  91. // too many keys have been configured.
  92. OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_add_key(TRUST_TOKEN_CLIENT *ctx,
  93. size_t *out_key_index,
  94. const uint8_t *key,
  95. size_t key_len);
  96. // TRUST_TOKEN_CLIENT_set_srr_key sets the public key used to verify the SRR. It
  97. // returns one on success and zero on error.
  98. OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_set_srr_key(TRUST_TOKEN_CLIENT *ctx,
  99. EVP_PKEY *key);
  100. // TRUST_TOKEN_CLIENT_begin_issuance produces a request for |count| trust tokens
  101. // and serializes the request into a newly-allocated buffer, setting |*out| to
  102. // that buffer and |*out_len| to its length. The caller takes ownership of the
  103. // buffer and must call |OPENSSL_free| when done. It returns one on success and
  104. // zero on error.
  105. OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_begin_issuance(TRUST_TOKEN_CLIENT *ctx,
  106. uint8_t **out,
  107. size_t *out_len,
  108. size_t count);
  109. // TRUST_TOKEN_CLIENT_finish_issuance consumes |response| from the issuer and
  110. // extracts the tokens, returning a list of tokens and the index of the key used
  111. // to sign the tokens in |*out_key_index|. The caller can use this to determine
  112. // what key was used in an issuance and to drop tokens if a new key commitment
  113. // arrives without the specified key present. The caller takes ownership of the
  114. // list and must call |sk_TRUST_TOKEN_pop_free| when done. The list is empty if
  115. // issuance fails.
  116. OPENSSL_EXPORT STACK_OF(TRUST_TOKEN) *
  117. TRUST_TOKEN_CLIENT_finish_issuance(TRUST_TOKEN_CLIENT *ctx,
  118. size_t *out_key_index,
  119. const uint8_t *response,
  120. size_t response_len);
  121. // TRUST_TOKEN_CLIENT_begin_redemption produces a request to redeem a token
  122. // |token| and receive a signature over |data| and serializes the request into
  123. // a newly-allocated buffer, setting |*out| to that buffer and |*out_len| to
  124. // its length. |time| is the number of seconds since the UNIX epoch and used to
  125. // verify the validity of the issuer's response in TrustTokenV1 and ignored in
  126. // other versions. The caller takes ownership of the buffer and must call
  127. // |OPENSSL_free| when done. It returns one on success or zero on error.
  128. OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_begin_redemption(
  129. TRUST_TOKEN_CLIENT *ctx, uint8_t **out, size_t *out_len,
  130. const TRUST_TOKEN *token, const uint8_t *data, size_t data_len,
  131. uint64_t time);
  132. // TRUST_TOKEN_CLIENT_finish_redemption consumes |response| from the issuer. In
  133. // |TRUST_TOKEN_experiment_v1|, it then verifies the SRR and if valid sets
  134. // |*out_rr| and |*out_rr_len| (respectively, |*out_sig| and |*out_sig_len|)
  135. // to a newly-allocated buffer containing the SRR (respectively, the SRR
  136. // signature). In other versions, it sets |*out_rr| and |*out_rr_len|
  137. // to a newly-allocated buffer containing |response| and leaves all validation
  138. // to the caller. It returns one on success or zero on failure.
  139. OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_finish_redemption(
  140. TRUST_TOKEN_CLIENT *ctx, uint8_t **out_rr, size_t *out_rr_len,
  141. uint8_t **out_sig, size_t *out_sig_len, const uint8_t *response,
  142. size_t response_len);
  143. // Trust Token issuer implementation.
  144. //
  145. // These functions implement the issuer half of the Trust Token protocol. A
  146. // |TRUST_TOKEN_ISSUER| can be reused across multiple protocol operations. It
  147. // may be used concurrently on multiple threads by non-mutating functions,
  148. // provided no other thread is concurrently calling a mutating function.
  149. // Functions which take a |const| pointer are non-mutating and functions which
  150. // take a non-|const| pointer are mutating.
  151. // TRUST_TOKEN_ISSUER_new returns a newly-allocated |TRUST_TOKEN_ISSUER|
  152. // configured to use a max batchsize of |max_batchsize| or NULL on error.
  153. // Issuance requests must be made in batches smaller than |max_batchsize|. This
  154. // function will return an error if |max_batchsize| is too large for Trust
  155. // Tokens.
  156. OPENSSL_EXPORT TRUST_TOKEN_ISSUER *TRUST_TOKEN_ISSUER_new(
  157. const TRUST_TOKEN_METHOD *method, size_t max_batchsize);
  158. // TRUST_TOKEN_ISSUER_free releases memory associated with |ctx|.
  159. OPENSSL_EXPORT void TRUST_TOKEN_ISSUER_free(TRUST_TOKEN_ISSUER *ctx);
  160. // TRUST_TOKEN_ISSUER_add_key configures the |ctx| to support the private key
  161. // |key|. It must be a private key returned by |TRUST_TOKEN_generate_key|. It
  162. // returns one on success or zero on error. This function may fail if the |key|
  163. // can't be parsed or too many keys have been configured.
  164. OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_add_key(TRUST_TOKEN_ISSUER *ctx,
  165. const uint8_t *key,
  166. size_t key_len);
  167. // TRUST_TOKEN_ISSUER_set_srr_key sets the private key used to sign the SRR. It
  168. // returns one on success and zero on error.
  169. OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_set_srr_key(TRUST_TOKEN_ISSUER *ctx,
  170. EVP_PKEY *key);
  171. // TRUST_TOKEN_ISSUER_set_metadata_key sets the key used to encrypt the private
  172. // metadata. The key is a randomly generated bytestring of at least 32 bytes
  173. // used to encode the private metadata bit in the SRR. It returns one on success
  174. // and zero on error.
  175. OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_set_metadata_key(TRUST_TOKEN_ISSUER *ctx,
  176. const uint8_t *key,
  177. size_t len);
  178. // TRUST_TOKEN_ISSUER_issue ingests |request| for token issuance
  179. // and generates up to |max_issuance| valid tokens, producing a list of blinded
  180. // tokens and storing the response into a newly-allocated buffer and setting
  181. // |*out| to that buffer, |*out_len| to its length, and |*out_tokens_issued| to
  182. // the number of tokens issued. The tokens are issued with public metadata of
  183. // |public_metadata| and a private metadata value of |private_metadata|.
  184. // |public_metadata| must be one of the previously configured key IDs.
  185. // |private_metadata| must be 0 or 1. The caller takes ownership of the buffer
  186. // and must call |OPENSSL_free| when done. It returns one on success or zero on
  187. // error.
  188. OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_issue(
  189. const TRUST_TOKEN_ISSUER *ctx, uint8_t **out, size_t *out_len,
  190. size_t *out_tokens_issued, const uint8_t *request, size_t request_len,
  191. uint32_t public_metadata, uint8_t private_metadata, size_t max_issuance);
  192. // TRUST_TOKEN_ISSUER_redeem ingests a |request| for token redemption and
  193. // verifies the token. If the token is valid, a RR is produced with a lifetime
  194. // of |lifetime| (in seconds), signing over the requested data from the request
  195. // and the value of the token, storing the result into a newly-allocated buffer
  196. // and setting |*out| to that buffer and |*out_len| to its length. The extracted
  197. // |TRUST_TOKEN| is stored into a newly-allocated buffer and stored in
  198. // |*out_token|. The extracted client data is stored into a newly-allocated
  199. // buffer and stored in |*out_client_data|. In TrustTokenV1, the extracted
  200. // redemption time is stored in |*out_redemption_time|. The caller takes
  201. // ownership of each output buffer and must call |OPENSSL_free| when done. It
  202. // returns one on success or zero on error.
  203. //
  204. // The caller must keep track of all values of |*out_token| seen globally before
  205. // returning the SRR to the client. If the value has been reused, the caller
  206. // must discard the SRR and report an error to the caller. Returning an SRR with
  207. // replayed values allows an attacker to double-spend tokens.
  208. OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_redeem(
  209. const TRUST_TOKEN_ISSUER *ctx, uint8_t **out, size_t *out_len,
  210. TRUST_TOKEN **out_token, uint8_t **out_client_data,
  211. size_t *out_client_data_len, uint64_t *out_redemption_time,
  212. const uint8_t *request, size_t request_len, uint64_t lifetime);
  213. // TRUST_TOKEN_ISSUER_redeem_raw ingests a |request| for token redemption and
  214. // verifies the token. The public metadata is stored in |*out_public|. The
  215. // private metadata (if any) is stored in |*out_private|. The extracted
  216. // |TRUST_TOKEN| is stored into a newly-allocated buffer and stored in
  217. // |*out_token|. The extracted client data is stored into a newly-allocated
  218. // buffer and stored in |*out_client_data|. The caller takes ownership of each
  219. // output buffer and must call |OPENSSL_free| when done. It returns one on
  220. // success or zero on error.
  221. //
  222. // The caller must keep track of all values of |*out_token| seen globally before
  223. // returning a response to the client. If the value has been reused, the caller
  224. // must report an error to the client. Returning a response with replayed values
  225. // allows an attacker to double-spend tokens.
  226. OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_redeem_raw(
  227. const TRUST_TOKEN_ISSUER *ctx, uint32_t *out_public, uint8_t *out_private,
  228. TRUST_TOKEN **out_token, uint8_t **out_client_data,
  229. size_t *out_client_data_len, const uint8_t *request, size_t request_len);
  230. // TRUST_TOKEN_decode_private_metadata decodes |encrypted_bit| using the
  231. // private metadata key specified by a |key| buffer of length |key_len| and the
  232. // nonce by a |nonce| buffer of length |nonce_len|. The nonce in
  233. // |TRUST_TOKEN_experiment_v1| is the token-hash field of the SRR. |*out_value|
  234. // is set to the decrypted value, either zero or one. It returns one on success
  235. // and zero on error.
  236. OPENSSL_EXPORT int TRUST_TOKEN_decode_private_metadata(
  237. const TRUST_TOKEN_METHOD *method, uint8_t *out_value, const uint8_t *key,
  238. size_t key_len, const uint8_t *nonce, size_t nonce_len,
  239. uint8_t encrypted_bit);
  240. #if defined(__cplusplus)
  241. } // extern C
  242. extern "C++" {
  243. BSSL_NAMESPACE_BEGIN
  244. BORINGSSL_MAKE_DELETER(TRUST_TOKEN, TRUST_TOKEN_free)
  245. BORINGSSL_MAKE_DELETER(TRUST_TOKEN_CLIENT, TRUST_TOKEN_CLIENT_free)
  246. BORINGSSL_MAKE_DELETER(TRUST_TOKEN_ISSUER, TRUST_TOKEN_ISSUER_free)
  247. BSSL_NAMESPACE_END
  248. } // extern C++
  249. #endif
  250. #define TRUST_TOKEN_R_KEYGEN_FAILURE 100
  251. #define TRUST_TOKEN_R_BUFFER_TOO_SMALL 101
  252. #define TRUST_TOKEN_R_OVER_BATCHSIZE 102
  253. #define TRUST_TOKEN_R_DECODE_ERROR 103
  254. #define TRUST_TOKEN_R_SRR_SIGNATURE_ERROR 104
  255. #define TRUST_TOKEN_R_DECODE_FAILURE 105
  256. #define TRUST_TOKEN_R_INVALID_METADATA 106
  257. #define TRUST_TOKEN_R_TOO_MANY_KEYS 107
  258. #define TRUST_TOKEN_R_NO_KEYS_CONFIGURED 108
  259. #define TRUST_TOKEN_R_INVALID_KEY_ID 109
  260. #define TRUST_TOKEN_R_INVALID_TOKEN 110
  261. #define TRUST_TOKEN_R_BAD_VALIDITY_CHECK 111
  262. #define TRUST_TOKEN_R_NO_SRR_KEY_CONFIGURED 112
  263. #define TRUST_TOKEN_R_INVALID_METADATA_KEY 113
  264. #define TRUST_TOKEN_R_INVALID_PROOF 114
  265. #endif // OPENSSL_HEADER_TRUST_TOKEN_H