| 1 |
// Copyright (c) 2020, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_TRUST_TOKEN_H |
| 5 |
#define OPENSSL_HEADER_TRUST_TOKEN_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
#include <openssl/stack.h> |
| 9 |
|
| 10 |
#if defined(__cplusplus) |
| 11 |
extern "C" { |
| 12 |
#endif |
| 13 |
|
| 14 |
|
| 15 |
// Trust Token implementation. |
| 16 |
// |
| 17 |
// Trust Token is an implementation of an experimental mechanism similar to |
| 18 |
// Privacy Pass which allows issuance and redemption of anonymized tokens with |
| 19 |
// limited private metadata. |
| 20 |
// |
| 21 |
// References: |
| 22 |
// https://eprint.iacr.org/2020/072.pdf |
| 23 |
// https://github.com/alxdavids/privacy-pass-ietf/tree/master/drafts |
| 24 |
// https://github.com/WICG/trust-token-api/blob/master/README.md |
| 25 |
// |
| 26 |
// WARNING: This API is unstable and subject to change. |
| 27 |
|
| 28 |
// TRUST_TOKEN_experiment_v1 is an experimental Trust Tokens protocol using |
| 29 |
// PMBTokens and P-384. |
| 30 |
OPENSSL_EXPORT const TRUST_TOKEN_METHOD *TRUST_TOKEN_experiment_v1(void); |
| 31 |
|
| 32 |
// TRUST_TOKEN_experiment_v2_voprf is an experimental Trust Tokens protocol |
| 33 |
// using VOPRFs and P-384 with up to 6 keys, without RR verification. |
| 34 |
OPENSSL_EXPORT const TRUST_TOKEN_METHOD *TRUST_TOKEN_experiment_v2_voprf(void); |
| 35 |
|
| 36 |
// TRUST_TOKEN_experiment_v2_pmb is an experimental Trust Tokens protocol using |
| 37 |
// PMBTokens and P-384 with up to 3 keys, without RR verification. |
| 38 |
OPENSSL_EXPORT const TRUST_TOKEN_METHOD *TRUST_TOKEN_experiment_v2_pmb(void); |
| 39 |
|
| 40 |
// TRUST_TOKEN_pst_v1_voprf is an experimental Trust Tokens protocol |
| 41 |
// using VOPRFs and P-384 with up to 6 keys, without RR verification. |
| 42 |
OPENSSL_EXPORT const TRUST_TOKEN_METHOD *TRUST_TOKEN_pst_v1_voprf(void); |
| 43 |
|
| 44 |
// TRUST_TOKEN_pst_v1_pmb is an experimental Trust Tokens protocol using |
| 45 |
// PMBTokens and P-384 with up to 3 keys, without RR verification. |
| 46 |
OPENSSL_EXPORT const TRUST_TOKEN_METHOD *TRUST_TOKEN_pst_v1_pmb(void); |
| 47 |
|
| 48 |
// 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. |
| 51 |
struct trust_token_st { |
| 52 |
uint8_t *data; |
| 53 |
size_t len; |
| 54 |
}; |
| 55 |
|
| 56 |
DEFINE_STACK_OF(TRUST_TOKEN) |
| 57 |
|
| 58 |
// TRUST_TOKEN_new creates a newly-allocated |TRUST_TOKEN| with value |data| or |
| 59 |
// NULL on allocation failure. |
| 60 |
OPENSSL_EXPORT TRUST_TOKEN *TRUST_TOKEN_new(const uint8_t *data, size_t len); |
| 61 |
|
| 62 |
// TRUST_TOKEN_free releases memory associated with |token|. |
| 63 |
OPENSSL_EXPORT void TRUST_TOKEN_free(TRUST_TOKEN *token); |
| 64 |
|
| 65 |
#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 |
// This function returns one on success or zero on error. |
| 79 |
OPENSSL_EXPORT int TRUST_TOKEN_generate_key( |
| 80 |
const TRUST_TOKEN_METHOD *method, uint8_t *out_priv_key, |
| 81 |
size_t *out_priv_key_len, size_t max_priv_key_len, uint8_t *out_pub_key, |
| 82 |
size_t *out_pub_key_len, size_t max_pub_key_len, uint32_t id); |
| 83 |
|
| 84 |
// TRUST_TOKEN_derive_key_from_secret deterministically derives a new Trust |
| 85 |
// Token keypair labeled with |id| from an input |secret| and serializes the |
| 86 |
// private and public keys, writing the private key to |out_priv_key| and |
| 87 |
// setting |*out_priv_key_len| to the number of bytes written, and writing the |
| 88 |
// public key to |out_pub_key| and setting |*out_pub_key_len| to the number of |
| 89 |
// bytes written. |
| 90 |
// |
| 91 |
// At most |max_priv_key_len| and |max_pub_key_len| bytes are written. In order |
| 92 |
// to ensure success, these should be at least |
| 93 |
// |TRUST_TOKEN_MAX_PRIVATE_KEY_SIZE| and |TRUST_TOKEN_MAX_PUBLIC_KEY_SIZE|. |
| 94 |
// |
| 95 |
// This function returns one on success or zero on error. |
| 96 |
OPENSSL_EXPORT int TRUST_TOKEN_derive_key_from_secret( |
| 97 |
const TRUST_TOKEN_METHOD *method, uint8_t *out_priv_key, |
| 98 |
size_t *out_priv_key_len, size_t max_priv_key_len, uint8_t *out_pub_key, |
| 99 |
size_t *out_pub_key_len, size_t max_pub_key_len, uint32_t id, |
| 100 |
const uint8_t *secret, size_t secret_len); |
| 101 |
|
| 102 |
|
| 103 |
// Trust Token client implementation. |
| 104 |
// |
| 105 |
// These functions implements the client half of the Trust Token protocol. A |
| 106 |
// single |TRUST_TOKEN_CLIENT| can perform a single protocol operation. |
| 107 |
|
| 108 |
// TRUST_TOKEN_CLIENT_new returns a newly-allocated |TRUST_TOKEN_CLIENT| |
| 109 |
// configured to use a max batchsize of |max_batchsize| or NULL on error. |
| 110 |
// Issuance requests must be made in batches smaller than |max_batchsize|. This |
| 111 |
// function will return an error if |max_batchsize| is too large for Trust |
| 112 |
// Tokens. |
| 113 |
OPENSSL_EXPORT TRUST_TOKEN_CLIENT *TRUST_TOKEN_CLIENT_new( |
| 114 |
const TRUST_TOKEN_METHOD *method, size_t max_batchsize); |
| 115 |
|
| 116 |
// TRUST_TOKEN_CLIENT_free releases memory associated with |ctx|. |
| 117 |
OPENSSL_EXPORT void TRUST_TOKEN_CLIENT_free(TRUST_TOKEN_CLIENT *ctx); |
| 118 |
|
| 119 |
// TRUST_TOKEN_CLIENT_add_key configures the |ctx| to support the public key |
| 120 |
// |key|. It sets |*out_key_index| to the index this key has been configured to. |
| 121 |
// It returns one on success or zero on error if the |key| can't be parsed or |
| 122 |
// too many keys have been configured. |
| 123 |
OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_add_key(TRUST_TOKEN_CLIENT *ctx, |
| 124 |
size_t *out_key_index, |
| 125 |
const uint8_t *key, |
| 126 |
size_t key_len); |
| 127 |
|
| 128 |
// TRUST_TOKEN_CLIENT_set_srr_key sets the public key used to verify the SRR. It |
| 129 |
// returns one on success and zero on error. |
| 130 |
OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_set_srr_key(TRUST_TOKEN_CLIENT *ctx, |
| 131 |
EVP_PKEY *key); |
| 132 |
|
| 133 |
// TRUST_TOKEN_CLIENT_begin_issuance produces a request for |count| trust tokens |
| 134 |
// and serializes the request into a newly-allocated buffer, setting |*out| to |
| 135 |
// that buffer and |*out_len| to its length. The caller takes ownership of the |
| 136 |
// buffer and must call |OPENSSL_free| when done. It returns one on success and |
| 137 |
// zero on error. |
| 138 |
OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_begin_issuance(TRUST_TOKEN_CLIENT *ctx, |
| 139 |
uint8_t **out, |
| 140 |
size_t *out_len, |
| 141 |
size_t count); |
| 142 |
|
| 143 |
// TRUST_TOKEN_CLIENT_begin_issuance_over_message produces a request for a trust |
| 144 |
// token derived from |msg| and serializes the request into a newly-allocated |
| 145 |
// buffer, setting |*out| to that buffer and |*out_len| to its length. The |
| 146 |
// caller takes ownership of the buffer and must call |OPENSSL_free| when done. |
| 147 |
// It returns one on success and zero on error. |
| 148 |
OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_begin_issuance_over_message( |
| 149 |
TRUST_TOKEN_CLIENT *ctx, uint8_t **out, size_t *out_len, size_t count, |
| 150 |
const uint8_t *msg, size_t msg_len); |
| 151 |
|
| 152 |
// TRUST_TOKEN_CLIENT_finish_issuance consumes |response| from the issuer and |
| 153 |
// extracts the tokens, returning a list of tokens and the index of the key used |
| 154 |
// to sign the tokens in |*out_key_index|. The caller can use this to determine |
| 155 |
// what key was used in an issuance and to drop tokens if a new key commitment |
| 156 |
// arrives without the specified key present. The caller takes ownership of the |
| 157 |
// list and must call |sk_TRUST_TOKEN_pop_free| when done. The list is empty if |
| 158 |
// issuance fails. |
| 159 |
OPENSSL_EXPORT STACK_OF(TRUST_TOKEN) * |
| 160 |
TRUST_TOKEN_CLIENT_finish_issuance(TRUST_TOKEN_CLIENT *ctx, |
| 161 |
size_t *out_key_index, |
| 162 |
const uint8_t *response, |
| 163 |
size_t response_len); |
| 164 |
|
| 165 |
|
| 166 |
// TRUST_TOKEN_CLIENT_begin_redemption produces a request to redeem a token |
| 167 |
// |token| and receive a signature over |data| and serializes the request into |
| 168 |
// a newly-allocated buffer, setting |*out| to that buffer and |*out_len| to |
| 169 |
// its length. |time| is the number of seconds since the UNIX epoch and used to |
| 170 |
// verify the validity of the issuer's response in TrustTokenV1 and ignored in |
| 171 |
// other versions. The caller takes ownership of the buffer and must call |
| 172 |
// |OPENSSL_free| when done. It returns one on success or zero on error. |
| 173 |
OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_begin_redemption( |
| 174 |
TRUST_TOKEN_CLIENT *ctx, uint8_t **out, size_t *out_len, |
| 175 |
const TRUST_TOKEN *token, const uint8_t *data, size_t data_len, |
| 176 |
uint64_t time); |
| 177 |
|
| 178 |
// TRUST_TOKEN_CLIENT_finish_redemption consumes |response| from the issuer. In |
| 179 |
// |TRUST_TOKEN_experiment_v1|, it then verifies the SRR and if valid sets |
| 180 |
// |*out_rr| and |*out_rr_len| (respectively, |*out_sig| and |*out_sig_len|) |
| 181 |
// to a newly-allocated buffer containing the SRR (respectively, the SRR |
| 182 |
// signature). In other versions, it sets |*out_rr| and |*out_rr_len| |
| 183 |
// to a newly-allocated buffer containing |response| and leaves all validation |
| 184 |
// to the caller. It returns one on success or zero on failure. |
| 185 |
OPENSSL_EXPORT int TRUST_TOKEN_CLIENT_finish_redemption( |
| 186 |
TRUST_TOKEN_CLIENT *ctx, uint8_t **out_rr, size_t *out_rr_len, |
| 187 |
uint8_t **out_sig, size_t *out_sig_len, const uint8_t *response, |
| 188 |
size_t response_len); |
| 189 |
|
| 190 |
|
| 191 |
// Trust Token issuer implementation. |
| 192 |
// |
| 193 |
// These functions implement the issuer half of the Trust Token protocol. A |
| 194 |
// |TRUST_TOKEN_ISSUER| can be reused across multiple protocol operations. It |
| 195 |
// may be used concurrently on multiple threads by non-mutating functions, |
| 196 |
// provided no other thread is concurrently calling a mutating function. |
| 197 |
// Functions which take a |const| pointer are non-mutating and functions which |
| 198 |
// take a non-|const| pointer are mutating. |
| 199 |
|
| 200 |
// TRUST_TOKEN_ISSUER_new returns a newly-allocated |TRUST_TOKEN_ISSUER| |
| 201 |
// configured to use a max batchsize of |max_batchsize| or NULL on error. |
| 202 |
// Issuance requests must be made in batches smaller than |max_batchsize|. This |
| 203 |
// function will return an error if |max_batchsize| is too large for Trust |
| 204 |
// Tokens. |
| 205 |
OPENSSL_EXPORT TRUST_TOKEN_ISSUER *TRUST_TOKEN_ISSUER_new( |
| 206 |
const TRUST_TOKEN_METHOD *method, size_t max_batchsize); |
| 207 |
|
| 208 |
// TRUST_TOKEN_ISSUER_free releases memory associated with |ctx|. |
| 209 |
OPENSSL_EXPORT void TRUST_TOKEN_ISSUER_free(TRUST_TOKEN_ISSUER *ctx); |
| 210 |
|
| 211 |
// TRUST_TOKEN_ISSUER_add_key configures the |ctx| to support the private key |
| 212 |
// |key|. It must be a private key returned by |TRUST_TOKEN_generate_key|. It |
| 213 |
// returns one on success or zero on error. This function may fail if the |key| |
| 214 |
// can't be parsed or too many keys have been configured. |
| 215 |
OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_add_key(TRUST_TOKEN_ISSUER *ctx, |
| 216 |
const uint8_t *key, |
| 217 |
size_t key_len); |
| 218 |
|
| 219 |
// TRUST_TOKEN_ISSUER_set_srr_key sets the private key used to sign the SRR. It |
| 220 |
// returns one on success and zero on error. |
| 221 |
OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_set_srr_key(TRUST_TOKEN_ISSUER *ctx, |
| 222 |
EVP_PKEY *key); |
| 223 |
|
| 224 |
// TRUST_TOKEN_ISSUER_set_metadata_key sets the key used to encrypt the private |
| 225 |
// metadata. The key is a randomly generated bytestring of at least 32 bytes |
| 226 |
// used to encode the private metadata bit in the SRR. It returns one on success |
| 227 |
// and zero on error. |
| 228 |
OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_set_metadata_key(TRUST_TOKEN_ISSUER *ctx, |
| 229 |
const uint8_t *key, |
| 230 |
size_t len); |
| 231 |
|
| 232 |
// TRUST_TOKEN_ISSUER_issue ingests |request| for token issuance |
| 233 |
// and generates up to |max_issuance| valid tokens, producing a list of blinded |
| 234 |
// tokens and storing the response into a newly-allocated buffer and setting |
| 235 |
// |*out| to that buffer, |*out_len| to its length, and |*out_tokens_issued| to |
| 236 |
// the number of tokens issued. The tokens are issued with public metadata of |
| 237 |
// |public_metadata| and a private metadata value of |private_metadata|. |
| 238 |
// |public_metadata| must be one of the previously configured key IDs. |
| 239 |
// |private_metadata| must be 0 or 1. The caller takes ownership of the buffer |
| 240 |
// and must call |OPENSSL_free| when done. It returns one on success or zero on |
| 241 |
// error. |
| 242 |
OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_issue( |
| 243 |
const TRUST_TOKEN_ISSUER *ctx, uint8_t **out, size_t *out_len, |
| 244 |
size_t *out_tokens_issued, const uint8_t *request, size_t request_len, |
| 245 |
uint32_t public_metadata, uint8_t private_metadata, size_t max_issuance); |
| 246 |
|
| 247 |
// TRUST_TOKEN_ISSUER_redeem ingests a |request| for token redemption and |
| 248 |
// verifies the token. The public metadata is stored in |*out_public|. The |
| 249 |
// private metadata (if any) is stored in |*out_private|. The extracted |
| 250 |
// |TRUST_TOKEN| is stored into a newly-allocated buffer and stored in |
| 251 |
// |*out_token|. The extracted client data is stored into a newly-allocated |
| 252 |
// buffer and stored in |*out_client_data|. The caller takes ownership of each |
| 253 |
// output buffer and must call |OPENSSL_free| when done. It returns one on |
| 254 |
// success or zero on error. |
| 255 |
// |
| 256 |
// The caller must keep track of all values of |*out_token| seen globally before |
| 257 |
// returning a response to the client. If the value has been reused, the caller |
| 258 |
// must report an error to the client. Returning a response with replayed values |
| 259 |
// allows an attacker to double-spend tokens. |
| 260 |
OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_redeem( |
| 261 |
const TRUST_TOKEN_ISSUER *ctx, uint32_t *out_public, uint8_t *out_private, |
| 262 |
TRUST_TOKEN **out_token, uint8_t **out_client_data, |
| 263 |
size_t *out_client_data_len, const uint8_t *request, size_t request_len); |
| 264 |
|
| 265 |
// TRUST_TOKEN_ISSUER_redeem_raw is a legacy alias for |
| 266 |
// |TRUST_TOKEN_ISSUER_redeem|. |
| 267 |
#define TRUST_TOKEN_ISSUER_redeem_raw TRUST_TOKEN_ISSUER_redeem |
| 268 |
|
| 269 |
// TRUST_TOKEN_ISSUER_redeem_over_message ingests a |request| for token |
| 270 |
// redemption and a message and verifies the token and that it is derived from |
| 271 |
// the provided |msg|. The public metadata is stored in |
| 272 |
// |*out_public|. The private metadata (if any) is stored in |*out_private|. The |
| 273 |
// extracted |TRUST_TOKEN| is stored into a newly-allocated buffer and stored in |
| 274 |
// |*out_token|. The extracted client data is stored into a newly-allocated |
| 275 |
// buffer and stored in |*out_client_data|. The caller takes ownership of each |
| 276 |
// output buffer and must call |OPENSSL_free| when done. It returns one on |
| 277 |
// success or zero on error. |
| 278 |
// |
| 279 |
// The caller must keep track of all values of |*out_token| seen globally before |
| 280 |
// returning a response to the client. If the value has been reused, the caller |
| 281 |
// must report an error to the client. Returning a response with replayed values |
| 282 |
// allows an attacker to double-spend tokens. |
| 283 |
OPENSSL_EXPORT int TRUST_TOKEN_ISSUER_redeem_over_message( |
| 284 |
const TRUST_TOKEN_ISSUER *ctx, uint32_t *out_public, uint8_t *out_private, |
| 285 |
TRUST_TOKEN **out_token, uint8_t **out_client_data, |
| 286 |
size_t *out_client_data_len, const uint8_t *request, size_t request_len, |
| 287 |
const uint8_t *msg, size_t msg_len); |
| 288 |
|
| 289 |
// TRUST_TOKEN_decode_private_metadata decodes |encrypted_bit| using the |
| 290 |
// private metadata key specified by a |key| buffer of length |key_len| and the |
| 291 |
// nonce by a |nonce| buffer of length |nonce_len|. The nonce in |
| 292 |
// |TRUST_TOKEN_experiment_v1| is the token-hash field of the SRR. |*out_value| |
| 293 |
// is set to the decrypted value, either zero or one. It returns one on success |
| 294 |
// and zero on error. |
| 295 |
OPENSSL_EXPORT int TRUST_TOKEN_decode_private_metadata( |
| 296 |
const TRUST_TOKEN_METHOD *method, uint8_t *out_value, const uint8_t *key, |
| 297 |
size_t key_len, const uint8_t *nonce, size_t nonce_len, |
| 298 |
uint8_t encrypted_bit); |
| 299 |
|
| 300 |
|
| 301 |
#if defined(__cplusplus) |
| 302 |
} // extern C |
| 303 |
|
| 304 |
extern "C++" { |
| 305 |
|
| 306 |
BSSL_NAMESPACE_BEGIN |
| 307 |
|
| 308 |
BORINGSSL_MAKE_DELETER(TRUST_TOKEN, TRUST_TOKEN_free) |
| 309 |
BORINGSSL_MAKE_DELETER(TRUST_TOKEN_CLIENT, TRUST_TOKEN_CLIENT_free) |
| 310 |
BORINGSSL_MAKE_DELETER(TRUST_TOKEN_ISSUER, TRUST_TOKEN_ISSUER_free) |
| 311 |
|
| 312 |
BSSL_NAMESPACE_END |
| 313 |
|
| 314 |
} // extern C++ |
| 315 |
#endif |
| 316 |
|
| 317 |
#define TRUST_TOKEN_R_KEYGEN_FAILURE 100 |
| 318 |
#define TRUST_TOKEN_R_BUFFER_TOO_SMALL 101 |
| 319 |
#define TRUST_TOKEN_R_OVER_BATCHSIZE 102 |
| 320 |
#define TRUST_TOKEN_R_DECODE_ERROR 103 |
| 321 |
#define TRUST_TOKEN_R_SRR_SIGNATURE_ERROR 104 |
| 322 |
#define TRUST_TOKEN_R_DECODE_FAILURE 105 |
| 323 |
#define TRUST_TOKEN_R_INVALID_METADATA 106 |
| 324 |
#define TRUST_TOKEN_R_TOO_MANY_KEYS 107 |
| 325 |
#define TRUST_TOKEN_R_NO_KEYS_CONFIGURED 108 |
| 326 |
#define TRUST_TOKEN_R_INVALID_KEY_ID 109 |
| 327 |
#define TRUST_TOKEN_R_INVALID_TOKEN 110 |
| 328 |
#define TRUST_TOKEN_R_BAD_VALIDITY_CHECK 111 |
| 329 |
#define TRUST_TOKEN_R_NO_SRR_KEY_CONFIGURED 112 |
| 330 |
#define TRUST_TOKEN_R_INVALID_METADATA_KEY 113 |
| 331 |
#define TRUST_TOKEN_R_INVALID_PROOF 114 |
| 332 |
|
| 333 |
#endif // OPENSSL_HEADER_TRUST_TOKEN_H |