blob: a73a8687d78d4328d643846eee7060ea42d41240 [file] [log] [blame]
Steven Valdez538a1242019-12-16 12:12:31 -05001/* Copyright (c) 2020, Google Inc.
Steven Valdez0b710a32020-02-14 10:34:53 -05002 *
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
15#ifndef OPENSSL_HEADER_TRUST_TOKEN_H
16#define OPENSSL_HEADER_TRUST_TOKEN_H
17
18#include <openssl/base.h>
19#include <openssl/stack.h>
20
21#if defined(__cplusplus)
22extern "C" {
23#endif
24
25
26// Trust Token implementation.
27//
28// Trust Token is an implementation of an experimental mechanism similar to
29// Privacy Pass which allows issuance and redemption of anonymized tokens with
30// limited private metadata.
31//
32// References:
33// https://eprint.iacr.org/2020/072.pdf
34// https://github.com/alxdavids/privacy-pass-ietf/tree/master/drafts
35// https://github.com/WICG/trust-token-api/blob/master/README.md
36//
37// WARNING: This API is unstable and subject to change.
38
David Benjamin239634d2020-04-29 11:17:51 -040039// TRUST_TOKEN_experiment_v0 is an experimental Trust Tokens protocol using
40// PMBTokens and P-521.
41OPENSSL_EXPORT const TRUST_TOKEN_METHOD *TRUST_TOKEN_experiment_v0(void);
42
David Benjaminaa764c42020-04-29 12:39:10 -040043// TRUST_TOKEN_experiment_v1 is an experimental Trust Tokens protocol using
44// PMBTokens and P-384. This version is still under developement and should not
45// be used yet.
46OPENSSL_EXPORT const TRUST_TOKEN_METHOD *TRUST_TOKEN_experiment_v1(void);
47
Steven Valdez538a1242019-12-16 12:12:31 -050048// trust_token_st represents a single-use token for the Trust Token protocol.
49// For the client, this is the token and its corresponding signature. For the
50// issuer, this is the token itself.
51struct trust_token_st {
52 uint8_t *data;
53 size_t len;
54};
55
56DEFINE_STACK_OF(TRUST_TOKEN)
57
58// TRUST_TOKEN_new creates a newly-allocated |TRUST_TOKEN| with value |data| or
59// NULL on allocation failure.
60OPENSSL_EXPORT TRUST_TOKEN *TRUST_TOKEN_new(const uint8_t *data, size_t len);
61
62// TRUST_TOKEN_free releases memory associated with |token|.
63OPENSSL_EXPORT void TRUST_TOKEN_free(TRUST_TOKEN *token);
64
Steven Valdez0b710a32020-02-14 10:34:53 -050065#define TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE 512
66#define TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE 512
67
68// TRUST_TOKEN_generate_key creates a new Trust Token keypair labeled with |id|
69// and serializes the private and public keys, writing the private key to
70// |out_priv_key| and setting |*out_priv_key_len| to the number of bytes
71// written, and writing the public key to |out_pub_key| and setting
72// |*out_pub_key_len| to the number of bytes written.
73//
74// At most |max_priv_key_len| and |max_pub_key_len| bytes are written. In order
75// to ensure success, these should be at least
76// |TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE| and |TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE|.
77//
78// WARNING: This API is unstable and the serializations of these keys are
79// subject to change. Keys generated with this function may not be persisted.
80//
81// This function returns one on success or zero on error.
82OPENSSL_EXPORT int TRUST_TOKEN_generate_key(
David Benjamin239634d2020-04-29 11:17:51 -040083 const TRUST_TOKEN_METHOD *method, uint8_t *out_priv_key,
84 size_t *out_priv_key_len, size_t max_priv_key_len, uint8_t *out_pub_key,
85 size_t *out_pub_key_len, size_t max_pub_key_len, uint32_t id);
Steven Valdez0b710a32020-02-14 10:34:53 -050086
87
Steven Valdez538a1242019-12-16 12:12:31 -050088// Trust Token client implementation.
89//
90// These functions implements the client half of the Trust Token protocol. A
91// single |TRUST_TOKEN_CLIENT| can perform a single protocol operation.
92
93// TRUST_TOKEN_CLIENT_new returns a newly-allocated |TRUST_TOKEN_CLIENT|
94// configured to use a max batchsize of |max_batchsize| or NULL on error.
David Benjamin17078f22020-04-28 17:50:13 -040095// Issuance requests must be made in batches smaller than |max_batchsize|. This
96// function will return an error if |max_batchsize| is too large for Trust
97// Tokens.
David Benjamin239634d2020-04-29 11:17:51 -040098OPENSSL_EXPORT TRUST_TOKEN_CLIENT *TRUST_TOKEN_CLIENT_new(
99 const TRUST_TOKEN_METHOD *method, size_t max_batchsize);
Steven Valdez538a1242019-12-16 12:12:31 -0500100
101// TRUST_TOKEN_CLIENT_free releases memory associated with |ctx|.
102OPENSSL_EXPORT void TRUST_TOKEN_CLIENT_free(TRUST_TOKEN_CLIENT *ctx);
103
104// TRUST_TOKEN_CLIENT_add_key configures the |ctx| to support the public key
105// |key|. It sets |*out_key_index| to the index this key has been configured to.
106// It returns one on success or zero on error if the |key| can't be parsed or
107// too many keys have been configured.
108OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_add_key(TRUST_TOKEN_CLIENT *ctx,
109 size_t *out_key_index,
110 const uint8_t *key,
111 size_t key_len);
112
113// TRUST_TOKEN_CLIENT_set_srr_key sets the public key used to verify the SRR. It
114// returns one on success and zero on error.
115OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_set_srr_key(TRUST_TOKEN_CLIENT *ctx,
116 EVP_PKEY *key);
117
118// TRUST_TOKEN_CLIENT_begin_issuance produces a request for |count| trust tokens
119// and serializes the request into a newly-allocated buffer, setting |*out| to
120// that buffer and |*out_len| to its length. The caller takes ownership of the
121// buffer and must call |OPENSSL_free| when done. It returns one on success and
122// zero on error.
123OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_begin_issuance(TRUST_TOKEN_CLIENT *ctx,
124 uint8_t **out,
125 size_t *out_len,
126 size_t count);
127
128// TRUST_TOKEN_CLIENT_finish_issuance consumes |response| from the issuer and
129// extracts the tokens, returning a list of tokens and the index of the key used
130// to sign the tokens in |*out_key_index|. The caller can use this to determine
131// what key was used in an issuance and to drop tokens if a new key commitment
132// arrives without the specified key present. The caller takes ownership of the
133// list and must call |sk_TRUST_TOKEN_pop_free| when done. The list is empty if
134// issuance fails.
135OPENSSL_EXPORT STACK_OF(TRUST_TOKEN) *
136 TRUST_TOKEN_CLIENT_finish_issuance(TRUST_TOKEN_CLIENT *ctx,
137 size_t *out_key_index,
138 const uint8_t *response,
139 size_t response_len);
140
141
142// TRUST_TOKEN_CLIENT_begin_redemption produces a request to redeem a token
143// |token| and receive a signature over |data| and serializes the request into
144// a newly-allocated buffer, setting |*out| to that buffer and |*out_len| to
145// its length. |time| is the number of seconds since the UNIX epoch and used to
146// verify the validity of the issuer's response. The caller takes ownership of
147// the buffer and must call |OPENSSL_free| when done. It returns one on success
148// or zero on error.
149OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_begin_redemption(
150 TRUST_TOKEN_CLIENT *ctx, uint8_t **out, size_t *out_len,
151 const TRUST_TOKEN *token, const uint8_t *data, size_t data_len,
152 uint64_t time);
153
154// TRUST_TOKEN_CLIENT_finish_redemption consumes |response| from the issuer and
155// verifies the SRR. If valid, it returns one and sets |*out_srr| and
156// |*out_srr_len| (respectively, |*out_sig| and |*out_sig_len|) to a
157// newly-allocated buffer containing the SRR (respectively, the SRR signature).
158// Otherwise, it returns zero.
159OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_finish_redemption(
160 TRUST_TOKEN_CLIENT *ctx, uint8_t **out_srr, size_t *out_srr_len,
161 uint8_t **out_sig, size_t *out_sig_len, const uint8_t *response,
162 size_t response_len);
163
164
165// Trust Token issuer implementation.
166//
167// These functions implement the issuer half of the Trust Token protocol. A
168// |TRUST_TOKEN_ISSUER| can be reused across multiple protocol operations. It
169// may be used concurrently on multiple threads by non-mutating functions,
170// provided no other thread is concurrently calling a mutating function.
171// Functions which take a |const| pointer are non-mutating and functions which
172// take a non-|const| pointer are mutating.
173
174// TRUST_TOKEN_ISSUER_new returns a newly-allocated |TRUST_TOKEN_ISSUER|
175// configured to use a max batchsize of |max_batchsize| or NULL on error.
David Benjamin17078f22020-04-28 17:50:13 -0400176// Issuance requests must be made in batches smaller than |max_batchsize|. This
177// function will return an error if |max_batchsize| is too large for Trust
178// Tokens.
David Benjamin239634d2020-04-29 11:17:51 -0400179OPENSSL_EXPORT TRUST_TOKEN_ISSUER *TRUST_TOKEN_ISSUER_new(
180 const TRUST_TOKEN_METHOD *method, size_t max_batchsize);
Steven Valdez538a1242019-12-16 12:12:31 -0500181
182// TRUST_TOKEN_ISSUER_free releases memory associated with |ctx|.
183OPENSSL_EXPORT void TRUST_TOKEN_ISSUER_free(TRUST_TOKEN_ISSUER *ctx);
184
185// TRUST_TOKEN_ISSUER_add_key configures the |ctx| to support the private key
186// |key|. It must be a private key returned by |TRUST_TOKEN_generate_key|. It
187// returns one on success or zero on error. This function may fail if the |key|
188// can't be parsed or too many keys have been configured.
189OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_add_key(TRUST_TOKEN_ISSUER *ctx,
190 const uint8_t *key,
191 size_t key_len);
192
193// TRUST_TOKEN_ISSUER_set_srr_key sets the private key used to sign the SRR. It
194// returns one on success and zero on error.
195OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_set_srr_key(TRUST_TOKEN_ISSUER *ctx,
196 EVP_PKEY *key);
197
198// TRUST_TOKEN_ISSUER_set_metadata_key sets the key used to encrypt the private
199// metadata. The key is a randomly generated bytestring of at least 32 bytes
200// used to encode the private metadata bit in the SRR. It returns one on success
201// and zero on error.
202OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_set_metadata_key(TRUST_TOKEN_ISSUER *ctx,
203 const uint8_t *key,
204 size_t len);
205
206// TRUST_TOKEN_ISSUER_issue ingests |request| for token issuance
207// and generates up to |max_issuance| valid tokens, producing a list of blinded
208// tokens and storing the response into a newly-allocated buffer and setting
209// |*out| to that buffer, |*out_len| to its length, and |*out_tokens_issued| to
210// the number of tokens issued. The tokens are issued with public metadata of
211// |public_metadata| and a private metadata value of |private_metadata|.
212// |public_metadata| must be one of the previously configured key IDs.
213// |private_metadata| must be 0 or 1. The caller takes ownership of the buffer
214// and must call |OPENSSL_free| when done. It returns one on success or zero on
215// error.
216OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_issue(
217 const TRUST_TOKEN_ISSUER *ctx, uint8_t **out, size_t *out_len,
David Benjamin17078f22020-04-28 17:50:13 -0400218 size_t *out_tokens_issued, const uint8_t *request, size_t request_len,
Steven Valdez538a1242019-12-16 12:12:31 -0500219 uint32_t public_metadata, uint8_t private_metadata, size_t max_issuance);
220
221// TRUST_TOKEN_ISSUER_redeem ingests a |request| for token redemption and
222// verifies the token. If the token is valid, a SRR is produced with a lifetime
223// of |lifetime| (in seconds), signing over the requested data from the request
224// and the value of the token, storing the result into a newly-allocated buffer
225// and setting |*out| to that buffer and |*out_len| to its length. The extracted
226// |TRUST_TOKEN| is stored into a newly-allocated buffer and stored in
227// |*out_token|. The extracted client data is stored into a newly-allocated
228// buffer and stored in |*out_client_data|. The extracted redemption time is
229// stored in |*out_redemption_time|. The caller takes ownership of each output
230// buffer and must call |OPENSSL_free| when done. It returns one on success or
231// zero on error.
232//
Steven Valdez54304732020-05-04 11:56:12 -0400233// The caller must keep track of all values of |*out_token| seen globally before
234// returning the SRR to the client. If the value has been reused, the caller
235// must discard the SRR and report an error to the caller. Returning an SRR with
236// replayed values allows an attacker to double-spend tokens.
Steven Valdez538a1242019-12-16 12:12:31 -0500237//
Steven Valdez54304732020-05-04 11:56:12 -0400238// The private metadata construction in |TRUST_TOKEN_experiment_v0| does not
239// keep the value secret and should not be used when secrecy is required.
Steven Valdez538a1242019-12-16 12:12:31 -0500240OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_redeem(
241 const TRUST_TOKEN_ISSUER *ctx, uint8_t **out, size_t *out_len,
242 TRUST_TOKEN **out_token, uint8_t **out_client_data,
243 size_t *out_client_data_len, uint64_t *out_redemption_time,
244 const uint8_t *request, size_t request_len, uint64_t lifetime);
245
246// TRUST_TOKEN_decode_private_metadata decodes |encrypted_bit| using the
247// private metadata key specified by a |key| buffer of length |key_len| and the
Steven Valdez54304732020-05-04 11:56:12 -0400248// nonce by a |nonce| buffer of length |nonce_len|. The nonce in
249// |TRUST_TOKEN_experiment_v0| is the client-data field of the SRR. The nonce in
250// |TRUST_TOKEN_experiment_v1| is the token-hash field of the SRR. |*out_value|
251// is set to the decrypted value, either zero or one. It returns one on success
252// and zero on error.
Steven Valdez538a1242019-12-16 12:12:31 -0500253OPENSSL_EXPORT int TRUST_TOKEN_decode_private_metadata(
David Benjamin239634d2020-04-29 11:17:51 -0400254 const TRUST_TOKEN_METHOD *method, uint8_t *out_value, const uint8_t *key,
Steven Valdez54304732020-05-04 11:56:12 -0400255 size_t key_len, const uint8_t *nonce, size_t nonce_len,
David Benjamin239634d2020-04-29 11:17:51 -0400256 uint8_t encrypted_bit);
Steven Valdez538a1242019-12-16 12:12:31 -0500257
258
Steven Valdez0b710a32020-02-14 10:34:53 -0500259#if defined(__cplusplus)
260} // extern C
Steven Valdez538a1242019-12-16 12:12:31 -0500261
262extern "C++" {
263
264BSSL_NAMESPACE_BEGIN
265
266BORINGSSL_MAKE_DELETER(TRUST_TOKEN, TRUST_TOKEN_free)
267BORINGSSL_MAKE_DELETER(TRUST_TOKEN_CLIENT, TRUST_TOKEN_CLIENT_free)
268BORINGSSL_MAKE_DELETER(TRUST_TOKEN_ISSUER, TRUST_TOKEN_ISSUER_free)
269
Steven Valdez538a1242019-12-16 12:12:31 -0500270BSSL_NAMESPACE_END
271
272} // extern C++
Steven Valdez0b710a32020-02-14 10:34:53 -0500273#endif
274
275#define TRUST_TOKEN_R_KEYGEN_FAILURE 100
276#define TRUST_TOKEN_R_BUFFER_TOO_SMALL 101
Steven Valdez538a1242019-12-16 12:12:31 -0500277#define TRUST_TOKEN_R_OVER_BATCHSIZE 102
278#define TRUST_TOKEN_R_DECODE_ERROR 103
279#define TRUST_TOKEN_R_SRR_SIGNATURE_ERROR 104
280#define TRUST_TOKEN_R_DECODE_FAILURE 105
281#define TRUST_TOKEN_R_INVALID_METADATA 106
282#define TRUST_TOKEN_R_TOO_MANY_KEYS 107
283#define TRUST_TOKEN_R_NO_KEYS_CONFIGURED 108
284#define TRUST_TOKEN_R_INVALID_KEY_ID 109
285#define TRUST_TOKEN_R_INVALID_TOKEN 110
286#define TRUST_TOKEN_R_BAD_VALIDITY_CHECK 111
287#define TRUST_TOKEN_R_NO_SRR_KEY_CONFIGURED 112
288#define TRUST_TOKEN_R_INVALID_METADATA_KEY 113
Steven Valdez78987bb2020-04-09 14:57:31 -0400289#define TRUST_TOKEN_R_INVALID_PROOF 114
Steven Valdez0b710a32020-02-14 10:34:53 -0500290
291#endif // OPENSSL_HEADER_TRUST_TOKEN_H