forked from eclipse-biscuit/biscuit-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiscuit.java
More file actions
408 lines (366 loc) · 13 KB
/
Biscuit.java
File metadata and controls
408 lines (366 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
* Copyright (c) 2019 Geoffroy Couprie <contact@geoffroycouprie.com> and Contributors to the Eclipse Foundation.
* SPDX-License-Identifier: Apache-2.0
*/
package org.eclipse.biscuit.token;
import biscuit.format.schema.Schema.PublicKey.Algorithm;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Optional;
import org.eclipse.biscuit.crypto.KeyDelegate;
import org.eclipse.biscuit.crypto.KeyPair;
import org.eclipse.biscuit.crypto.PublicKey;
import org.eclipse.biscuit.crypto.Signer;
import org.eclipse.biscuit.datalog.Pair;
import org.eclipse.biscuit.datalog.SymbolTable;
import org.eclipse.biscuit.error.Error;
import org.eclipse.biscuit.token.format.SerializedBiscuit;
/** Biscuit auth token */
public final class Biscuit extends UnverifiedBiscuit {
/**
* Creates a token builder
*
* <p>this function uses the default symbol table
*
* @param root root private key
* @return
*/
public static org.eclipse.biscuit.token.builder.Biscuit builder(final Signer root) {
return new org.eclipse.biscuit.token.builder.Biscuit(new SecureRandom(), root);
}
/**
* Creates a token builder
*
* <p>this function uses the default symbol table
*
* @param rng random number generator
* @param root root private key
* @return
*/
public static org.eclipse.biscuit.token.builder.Biscuit builder(
final SecureRandom rng, final KeyPair root) {
return new org.eclipse.biscuit.token.builder.Biscuit(rng, root);
}
/**
* Creates a token builder
*
* @param rng random number generator
* @param root root private key
* @return
*/
public static org.eclipse.biscuit.token.builder.Biscuit builder(
final SecureRandom rng,
final org.eclipse.biscuit.crypto.Signer root,
final Optional<Integer> rootKeyId) {
return new org.eclipse.biscuit.token.builder.Biscuit(rng, root, rootKeyId);
}
/**
* Creates a token
*
* @param rng random number generator
* @param root root private key
* @param authority authority block
* @return Biscuit
*/
public static Biscuit make(final SecureRandom rng, final Signer root, final Block authority)
throws Error.FormatError {
return Biscuit.make(rng, root, Optional.empty(), authority);
}
/**
* Creates a token
*
* @param rng random number generator
* @param root root private key
* @param authority authority block
* @return Biscuit
*/
public static Biscuit make(
final SecureRandom rng, final Signer root, final Integer rootKeyId, final Block authority)
throws Error.FormatError {
return Biscuit.make(rng, root, Optional.of(rootKeyId), authority);
}
/**
* Creates a token
*
* @param rng random number generator
* @param root root private key
* @param authority authority block
* @return Biscuit
*/
private static Biscuit make(
final SecureRandom rng,
final org.eclipse.biscuit.crypto.Signer root,
final Optional<Integer> rootKeyId,
final Block authority)
throws Error.FormatError {
ArrayList<Block> blocks = new ArrayList<>();
KeyPair next = KeyPair.generate(root.getPublicKey().getAlgorithm(), rng);
for (PublicKey pk : authority.getPublicKeys()) {
authority.getSymbolTable().insert(pk);
}
var container = SerializedBiscuit.make(root, rootKeyId, authority, next);
if (container.isErr()) {
throw container.getErr();
} else {
SerializedBiscuit s = container.getOk();
Optional<SerializedBiscuit> c = Optional.of(s);
return new Biscuit(authority, blocks, authority.getSymbolTable(), s);
}
}
Biscuit(
Block authority,
List<Block> blocks,
SymbolTable symbolTable,
SerializedBiscuit serializedBiscuit) {
super(authority, blocks, symbolTable, serializedBiscuit);
}
/**
* Deserializes a Biscuit token from a base64 url (RFC4648_URLSAFE) string
*
* <p>This checks the signature, but does not verify that the first key is the root key, to allow
* appending blocks without knowing about the root key.
*
* <p>The root key check is performed in the verify method
*
* <p>This method uses the default symbol table
*
* <p>Note: This method accepts both padded and unpadded base64 strings
*
* @param data
* @return Biscuit
*/
public static Biscuit fromBase64Url(String data, PublicKey root)
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, Error {
return Biscuit.fromBytes(Base64.getUrlDecoder().decode(data), root);
}
/**
* Deserializes a Biscuit token from a base64 url (RFC4648_URLSAFE) string
*
* <p>This checks the signature, but does not verify that the first key is the root key, to allow
* appending blocks without knowing about the root key.
*
* <p>The root key check is performed in the verify method
*
* <p>This method uses the default symbol table
*
* <p>Note: This method accepts both padded and unpadded base64 strings
*
* @param data
* @return Biscuit
*/
public static Biscuit fromBase64Url(String data, KeyDelegate delegate)
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, Error {
return Biscuit.fromBytes(Base64.getUrlDecoder().decode(data), delegate);
}
/**
* Deserializes a Biscuit token from a byte array
*
* <p>This checks the signature, but does not verify that the first key is the root key, to allow
* appending blocks without knowing about the root key.
*
* <p>The root key check is performed in the verify method
*
* <p>This method uses the default symbol table
*
* @param data
* @return
*/
public static Biscuit fromBytes(byte[] data, PublicKey root)
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, Error {
return fromBytesWithSymbols(data, root, defaultSymbolTable());
}
/**
* Deserializes a Biscuit token from a byte array
*
* <p>This checks the signature, but does not verify that the first key is the root key, to allow
* appending blocks without knowing about the root key.
*
* <p>The root key check is performed in the verify method
*
* <p>This method uses the default symbol table
*
* @param data
* @return
*/
public static Biscuit fromBytes(byte[] data, KeyDelegate delegate)
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, Error {
return fromBytesWithSymbols(data, delegate, defaultSymbolTable());
}
/**
* Deserializes a Biscuit token from a byte array
*
* <p>This checks the signature, but does not verify that the first key is the root key, to allow
* appending blocks without knowing about the root key.
*
* <p>The root key check is performed in the verify method
*
* @param data
* @return
*/
public static Biscuit fromBytesWithSymbols(byte[] data, PublicKey root, SymbolTable symbolTable)
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, Error {
// System.out.println("will deserialize and verify token");
SerializedBiscuit ser = SerializedBiscuit.fromBytes(data, root);
// System.out.println("deserialized token, will populate Biscuit structure");
return Biscuit.fromSerializedBiscuit(ser, symbolTable);
}
/**
* Deserializes a Biscuit token from a byte array
*
* <p>This checks the signature, but does not verify that the first key is the root key, to allow
* appending blocks without knowing about the root key.
*
* <p>The root key check is performed in the verify method
*
* @param data
* @return
*/
public static Biscuit fromBytesWithSymbols(
byte[] data, KeyDelegate delegate, SymbolTable symbolTable)
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, Error {
// System.out.println("will deserialize and verify token");
SerializedBiscuit ser = SerializedBiscuit.fromBytes(data, delegate);
// System.out.println("deserialized token, will populate Biscuit structure");
return Biscuit.fromSerializedBiscuit(ser, symbolTable);
}
/**
* Fills a Biscuit structure from a deserialized token
*
* @return
*/
static Biscuit fromSerializedBiscuit(SerializedBiscuit ser, SymbolTable symbolTable)
throws Error {
Pair<Block, ArrayList<Block>> t = ser.extractBlocks(symbolTable);
Block authority = t._1;
ArrayList<Block> blocks = t._2;
return new Biscuit(authority, blocks, symbolTable, ser);
}
/**
* Creates a authorizer for this token
*
* <p>This function checks that the root key is the one we expect
*
* @return
*/
public Authorizer authorizer() throws Error.FailedLogic {
return Authorizer.make(this);
}
/**
* Serializes a token to a byte array
*
* @return
*/
public byte[] serialize() throws Error.FormatError.SerializationError {
return this.serializedBiscuit.serialize();
}
/**
* Serializes a token to base 64 url String using RFC4648_URLSAFE
*
* @return String
* @throws Error.FormatError.SerializationError
*/
public String serializeBase64Url() throws Error.FormatError.SerializationError {
return Base64.getUrlEncoder().encodeToString(serialize());
}
/**
* Serializes a token to base 64 url String using RFC4648_URLSAFE without padding
*
* <p>This is useful for embedding tokens in URLs where padding characters (=) may cause issues
*
* @return String
* @throws Error.FormatError.SerializationError
*/
public String serializeBase64UrlNoPadding() throws Error.FormatError.SerializationError {
return Base64.getUrlEncoder().withoutPadding().encodeToString(serialize());
}
/**
* Generates a new token from an existing one and a new block
*
* @param block new block (should be generated from a Block builder)
* @param algorithm algorithm to use for the ephemeral key pair
* @return
*/
public Biscuit attenuate(org.eclipse.biscuit.token.builder.Block block, Algorithm algorithm)
throws Error {
SecureRandom rng = new SecureRandom();
KeyPair keypair = KeyPair.generate(algorithm, rng);
SymbolTable builderSymbols = new SymbolTable(this.symbolTable);
return attenuate(rng, keypair, block.build(builderSymbols));
}
public Biscuit attenuate(
final SecureRandom rng, final KeyPair keypair, org.eclipse.biscuit.token.builder.Block block)
throws Error {
SymbolTable builderSymbols = new SymbolTable(this.symbolTable);
return attenuate(rng, keypair, block.build(builderSymbols));
}
/**
* Generates a new token from an existing one and a new block
*
* @param rng random number generator
* @param keypair ephemeral key pair
* @param block new block (should be generated from a Block builder)
* @return
*/
public Biscuit attenuate(final SecureRandom rng, final KeyPair keypair, Block block)
throws Error {
Biscuit copiedBiscuit = this.copy();
if (!copiedBiscuit.symbolTable.disjoint(block.getSymbolTable())) {
throw new Error.SymbolTableOverlap();
}
var containerRes = copiedBiscuit.serializedBiscuit.append(keypair, block, Optional.empty());
if (containerRes.isErr()) {
throw containerRes.getErr();
}
SymbolTable symbolTable = new SymbolTable(copiedBiscuit.symbolTable);
for (String s : block.getSymbolTable().symbols()) {
symbolTable.add(s);
}
for (PublicKey pk : block.getPublicKeys()) {
symbolTable.insert(pk);
}
ArrayList<Block> blocks = new ArrayList<>();
for (Block b : copiedBiscuit.blocks) {
blocks.add(b);
}
blocks.add(block);
SerializedBiscuit container = containerRes.getOk();
return new Biscuit(copiedBiscuit.authority, blocks, symbolTable, container);
}
/** Generates a third party block request from a token */
public Biscuit appendThirdPartyBlock(PublicKey externalKey, ThirdPartyBlockContents blockResponse)
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, Error {
UnverifiedBiscuit b = super.appendThirdPartyBlock(externalKey, blockResponse);
// no need to verify again, we are already working from a verified token
return Biscuit.fromSerializedBiscuit(b.serializedBiscuit, b.symbolTable);
}
/** Prints a token's content */
public String print() {
StringBuilder s = new StringBuilder();
s.append("Biscuit {\n\tsymbols: ");
s.append(this.symbolTable.getAllSymbols());
s.append("\n\tpublic keys: ");
s.append(this.symbolTable.getPublicKeys());
s.append("\n\tauthority: ");
s.append(this.authority.print(this.symbolTable));
s.append("\n\tblocks: [\n");
for (Block b : this.blocks) {
s.append("\t\t");
if (b.getExternalKey().isPresent()) {
s.append(b.print(b.getSymbolTable()));
} else {
s.append(b.print(this.symbolTable));
}
s.append("\n");
}
s.append("\t]\n}");
return s.toString();
}
public Biscuit copy() throws Error {
return Biscuit.fromSerializedBiscuit(this.serializedBiscuit, this.symbolTable);
}
}