Skip to content

Commit b3631cc

Browse files
authored
Merge pull request #124 from preuss-adam/apreuss/public-key-fixes
Updates to key accessors and fix proof algorithm type
2 parents d72a1a7 + edbb12e commit b3631cc

File tree

5 files changed

+62
-36
lines changed

5 files changed

+62
-36
lines changed

src/main/java/org/eclipse/biscuit/token/Biscuit.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,18 @@ private static Biscuit make(
128128
throw container.getLeft();
129129
} else {
130130
SerializedBiscuit s = container.get();
131-
List<byte[]> revocationIds = s.revocationIdentifiers();
132131

133132
Option<SerializedBiscuit> c = Option.some(s);
134-
return new Biscuit(authority, blocks, authority.getSymbolTable(), s, revocationIds);
133+
return new Biscuit(authority, blocks, authority.getSymbolTable(), s);
135134
}
136135
}
137136

138137
Biscuit(
139138
Block authority,
140139
List<Block> blocks,
141140
SymbolTable symbolTable,
142-
SerializedBiscuit serializedBiscuit,
143-
List<byte[]> revocationIds) {
144-
super(authority, blocks, symbolTable, serializedBiscuit, revocationIds);
141+
SerializedBiscuit serializedBiscuit) {
142+
super(authority, blocks, symbolTable, serializedBiscuit);
145143
}
146144

147145
/**
@@ -268,9 +266,7 @@ static Biscuit fromSerializedBiscuit(SerializedBiscuit ser, SymbolTable symbolTa
268266
Block authority = t._1;
269267
ArrayList<Block> blocks = t._2;
270268

271-
List<byte[]> revocationIds = ser.revocationIdentifiers();
272-
273-
return new Biscuit(authority, blocks, symbolTable, ser, revocationIds);
269+
return new Biscuit(authority, blocks, symbolTable, ser);
274270
}
275271

276272
/**
@@ -365,9 +361,8 @@ public Biscuit attenuate(final SecureRandom rng, final KeyPair keypair, Block bl
365361
blocks.add(block);
366362

367363
SerializedBiscuit container = containerRes.get();
368-
List<byte[]> revocationIds = container.revocationIdentifiers();
369364

370-
return new Biscuit(copiedBiscuit.authority, blocks, symbolTable, container, revocationIds);
365+
return new Biscuit(copiedBiscuit.authority, blocks, symbolTable, container);
371366
}
372367

373368
/** Generates a third party block request from a token */

src/main/java/org/eclipse/biscuit/token/UnverifiedBiscuit.java

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.Base64;
1818
import java.util.List;
1919
import java.util.stream.Collectors;
20+
import java.util.stream.Stream;
2021
import org.eclipse.biscuit.crypto.BlockSignatureBuffer;
2122
import org.eclipse.biscuit.crypto.KeyDelegate;
2223
import org.eclipse.biscuit.crypto.KeyPair;
@@ -36,19 +37,16 @@ public class UnverifiedBiscuit {
3637
protected final List<Block> blocks;
3738
protected final SymbolTable symbolTable;
3839
protected final SerializedBiscuit serializedBiscuit;
39-
protected final List<byte[]> revocationIds;
4040

4141
UnverifiedBiscuit(
4242
Block authority,
4343
List<Block> blocks,
4444
SymbolTable symbolTable,
45-
SerializedBiscuit serializedBiscuit,
46-
List<byte[]> revocationIds) {
45+
SerializedBiscuit serializedBiscuit) {
4746
this.authority = authority;
4847
this.blocks = blocks;
4948
this.symbolTable = symbolTable;
5049
this.serializedBiscuit = serializedBiscuit;
51-
this.revocationIds = revocationIds;
5250
}
5351

5452
/**
@@ -98,9 +96,7 @@ private static UnverifiedBiscuit fromSerializedBiscuit(
9896
Block authority = t._1;
9997
ArrayList<Block> blocks = t._2;
10098

101-
List<byte[]> revocationIds = ser.revocationIdentifiers();
102-
103-
return new UnverifiedBiscuit(authority, blocks, symbolTable, ser, revocationIds);
99+
return new UnverifiedBiscuit(authority, blocks, symbolTable, ser);
104100
}
105101

106102
/**
@@ -139,17 +135,15 @@ public org.eclipse.biscuit.token.builder.Block createBlock() {
139135
* @return
140136
*/
141137
public UnverifiedBiscuit attenuate(
142-
org.eclipse.biscuit.token.builder.Block block, Algorithm algorithm) throws Error {
138+
org.eclipse.biscuit.token.builder.Block block, Algorithm algorithm) throws Error {
143139
SecureRandom rng = new SecureRandom();
144140
KeyPair keypair = KeyPair.generate(algorithm, rng);
145141
SymbolTable builderSymbols = new SymbolTable(this.symbolTable);
146142
return attenuate(rng, keypair, block.build(builderSymbols));
147143
}
148144

149145
public UnverifiedBiscuit attenuate(
150-
final SecureRandom rng,
151-
final KeyPair keypair,
152-
org.eclipse.biscuit.token.builder.Block block)
146+
final SecureRandom rng, final KeyPair keypair, org.eclipse.biscuit.token.builder.Block block)
153147
throws Error {
154148
SymbolTable builderSymbols = new SymbolTable(this.symbolTable);
155149
return attenuate(rng, keypair, block.build(builderSymbols));
@@ -189,20 +183,25 @@ private UnverifiedBiscuit attenuate(final SecureRandom rng, final KeyPair keypai
189183
blocks.add(block);
190184
SerializedBiscuit container = containerRes.get();
191185

192-
List<byte[]> revocationIds = container.revocationIdentifiers();
193-
194-
return new UnverifiedBiscuit(
195-
copiedBiscuit.authority, blocks, symbols, container, revocationIds);
186+
return new UnverifiedBiscuit(copiedBiscuit.authority, blocks, symbols, container);
196187
}
197188

198189
// FIXME: attenuate 3rd Party
199190

200191
public List<RevocationIdentifier> revocationIdentifiers() {
201-
return this.revocationIds.stream()
192+
return this.serializedBiscuit.revocationIdentifiers().stream()
202193
.map(RevocationIdentifier::fromBytes)
203194
.collect(Collectors.toList());
204195
}
205196

197+
public List<Option<PublicKey>> externalPublicKeys() {
198+
return Stream.<Option<PublicKey>>concat(
199+
Stream.of(Option.none()),
200+
this.serializedBiscuit.getBlocks().stream()
201+
.map(b -> b.getExternalSignature().map(ExternalSignature::getKey)))
202+
.collect(Collectors.toList());
203+
}
204+
206205
public List<List<Check>> getChecks() {
207206
ArrayList<List<Check>> l = new ArrayList<>();
208207
l.add(new ArrayList<>(this.authority.getChecks()));
@@ -237,6 +236,26 @@ public Option<Integer> getRootKeyId() {
237236
return this.serializedBiscuit.getRootKeyId();
238237
}
239238

239+
public int blockCount() {
240+
return 1 + blocks.size();
241+
}
242+
243+
public Option<PublicKey> blockExternalKey(int index) {
244+
if (index == 0) {
245+
return authority.getExternalKey();
246+
} else {
247+
return blocks.get(index - 1).getExternalKey();
248+
}
249+
}
250+
251+
public List<PublicKey> blockPublicKeys(int index) {
252+
if (index == 0) {
253+
return authority.getPublicKeys();
254+
} else {
255+
return blocks.get(index - 1).getPublicKeys();
256+
}
257+
}
258+
240259
/** Generates a third party block request from a token */
241260
public ThirdPartyBlockRequest thirdPartyRequest() {
242261
PublicKey previousKey;
@@ -304,9 +323,7 @@ public UnverifiedBiscuit appendThirdPartyBlock(
304323
}
305324
blocks.add(block);
306325

307-
List<byte[]> revocationIds = container.revocationIdentifiers();
308-
return new UnverifiedBiscuit(
309-
copiedBiscuit.authority, blocks, symbols, container, revocationIds);
326+
return new UnverifiedBiscuit(copiedBiscuit.authority, blocks, symbols, container);
310327
}
311328

312329
/** Prints a token's content */

src/main/java/org/eclipse/biscuit/token/format/SerializedBiscuit.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,18 @@ private static SerializedBiscuit deserialize(Schema.Biscuit data)
172172
throw new Error.FormatError.DeserializationError("invalid proof");
173173
}
174174

175-
final Proof proof =
176-
data.getProof().hasFinalSignature()
177-
? new Proof.FinalSignature(data.getProof().getFinalSignature().toByteArray())
178-
: new Proof.NextSecret(
179-
KeyPair.generate(
180-
authority.getKey().getAlgorithm(),
181-
data.getProof().getNextSecret().toByteArray()));
175+
final Proof proof;
176+
if (data.getProof().hasFinalSignature()) {
177+
proof = new Proof.FinalSignature(data.getProof().getFinalSignature().toByteArray());
178+
} else {
179+
final Schema.PublicKey.Algorithm proofAlgorithm =
180+
blocks.isEmpty()
181+
? authority.getKey().getAlgorithm()
182+
: blocks.get(blocks.size() - 1).getKey().getAlgorithm();
183+
proof =
184+
new Proof.NextSecret(
185+
KeyPair.generate(proofAlgorithm, data.getProof().getNextSecret().toByteArray()));
186+
}
182187

183188
Option<Integer> rootKeyId =
184189
data.hasRootKeyId() ? Option.some(data.getRootKeyId()) : Option.none();

src/test/java/org/eclipse/biscuit/token/BiscuitTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public void testBasic()
7272

7373
System.out.println("deserializing the first token");
7474
Biscuit deser = Biscuit.fromBytes(data, root.getPublicKey());
75+
assertEquals(1, deser.blockCount());
7576

7677
System.out.println(deser.print());
7778

@@ -105,6 +106,7 @@ public void testBasic()
105106

106107
System.out.println("deserializing the second token");
107108
Biscuit deser2 = Biscuit.fromBytes(data2, root.getPublicKey());
109+
assertEquals(2, deser2.blockCount());
108110

109111
System.out.println(deser2.print());
110112

@@ -135,6 +137,7 @@ public void testBasic()
135137

136138
System.out.println("deserializing the third token");
137139
Biscuit finalToken = Biscuit.fromBytes(data3, root.getPublicKey());
140+
assertEquals(3, finalToken.blockCount());
138141

139142
System.out.println(finalToken.print());
140143

src/test/java/org/eclipse/biscuit/token/ThirdPartyTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
import static org.junit.jupiter.api.Assertions.assertEquals;
99

1010
import biscuit.format.schema.Schema;
11+
import io.vavr.control.Option;
1112
import java.io.IOException;
1213
import java.security.InvalidKeyException;
1314
import java.security.NoSuchAlgorithmException;
1415
import java.security.SecureRandom;
1516
import java.security.SignatureException;
1617
import java.time.Duration;
1718
import java.util.Arrays;
19+
import java.util.List;
1820
import org.eclipse.biscuit.crypto.KeyPair;
1921
import org.eclipse.biscuit.datalog.RunLimits;
2022
import org.eclipse.biscuit.error.Error;
@@ -66,6 +68,10 @@ public void testRoundTrip()
6668
byte[] data = b2.serialize();
6769
Biscuit deser = Biscuit.fromBytes(data, root.getPublicKey());
6870
assertEquals(b2.print(), deser.print());
71+
assertEquals(
72+
b2.externalPublicKeys(), List.of(Option.none(), Option.of(external.getPublicKey())));
73+
assertEquals(Option.none(), b2.blockExternalKey(0));
74+
assertEquals(Option.of(external.getPublicKey()), b2.blockExternalKey(1));
6975

7076
System.out.println("will check the token for resource=file1");
7177
Authorizer authorizer = deser.authorizer();

0 commit comments

Comments
 (0)