Skip to content

Commit 453023e

Browse files
committed
Migrate to new signature generation.
Old biscuits can still be verified using the older format, but new ones will be generated using the new format.
1 parent de036f6 commit 453023e

File tree

4 files changed

+171
-58
lines changed

4 files changed

+171
-58
lines changed

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.eclipse.biscuit.token;
77

88
import biscuit.format.schema.Schema;
9+
import com.google.protobuf.ByteString;
910
import com.google.protobuf.InvalidProtocolBufferException;
1011
import io.vavr.control.Either;
1112
import io.vavr.control.Option;
@@ -14,7 +15,7 @@
1415
import java.security.InvalidKeyException;
1516
import java.security.NoSuchAlgorithmException;
1617
import java.security.SignatureException;
17-
import java.util.Objects;
18+
import java.util.Arrays;
1819
import org.eclipse.biscuit.crypto.BlockSignatureBuffer;
1920
import org.eclipse.biscuit.crypto.PublicKey;
2021
import org.eclipse.biscuit.crypto.Signer;
@@ -23,10 +24,10 @@
2324
import org.eclipse.biscuit.token.builder.Block;
2425

2526
public final class ThirdPartyBlockRequest {
26-
private final PublicKey previousKey;
27+
private final byte[] previousSignature;
2728

28-
ThirdPartyBlockRequest(PublicKey previousKey) {
29-
this.previousKey = previousKey;
29+
ThirdPartyBlockRequest(byte[] previousSignature) {
30+
this.previousSignature = previousSignature;
3031
}
3132

3233
public Either<Error.FormatError, ThirdPartyBlockContents> createBlock(
@@ -43,8 +44,10 @@ public Either<Error.FormatError, ThirdPartyBlockContents> createBlock(
4344

4445
byte[] serializedBlock = res.get();
4546
byte[] payload =
46-
BlockSignatureBuffer.generateExternalBlockSignaturePayloadV0(
47-
serializedBlock, this.previousKey);
47+
BlockSignatureBuffer.generateExternalBlockSignaturePayloadV1(
48+
serializedBlock,
49+
this.previousSignature,
50+
BlockSignatureBuffer.THIRD_PARTY_SIGNATURE_VERSION);
4851
byte[] signature = externalSigner.sign(payload);
4952

5053
PublicKey publicKey = externalSigner.getPublicKey();
@@ -54,15 +57,29 @@ public Either<Error.FormatError, ThirdPartyBlockContents> createBlock(
5457

5558
public Schema.ThirdPartyBlockRequest serialize() throws Error.FormatError.SerializationError {
5659
Schema.ThirdPartyBlockRequest.Builder b = Schema.ThirdPartyBlockRequest.newBuilder();
57-
b.setLegacyPreviousKey(this.previousKey.serialize());
60+
b.setPreviousSignature(ByteString.copyFrom(this.previousSignature));
5861

5962
return b.build();
6063
}
6164

6265
public static ThirdPartyBlockRequest deserialize(Schema.ThirdPartyBlockRequest b)
6366
throws Error.FormatError.DeserializationError {
64-
PublicKey previousKey = PublicKey.deserialize(b.getLegacyPreviousKey());
65-
return new ThirdPartyBlockRequest(previousKey);
67+
68+
if (b.hasLegacyPreviousKey()) {
69+
throw new Error.FormatError.DeserializationError(
70+
"public keys were provided in third-party block request");
71+
}
72+
if (b.getLegacyPublicKeysCount() > 0) {
73+
throw new Error.FormatError.DeserializationError(
74+
"public keys were provided in third-party block request");
75+
}
76+
77+
if (!b.hasPreviousSignature()) {
78+
throw new Error.FormatError.DeserializationError(
79+
"missing previous signature in third-party block request");
80+
}
81+
82+
return new ThirdPartyBlockRequest(b.getPreviousSignature().toByteArray());
6683
}
6784

6885
public static ThirdPartyBlockRequest fromBytes(byte[] slice)
@@ -88,16 +105,16 @@ public boolean equals(Object o) {
88105

89106
ThirdPartyBlockRequest that = (ThirdPartyBlockRequest) o;
90107

91-
return Objects.equals(previousKey, that.previousKey);
108+
return Arrays.equals(previousSignature, that.previousSignature);
92109
}
93110

94111
@Override
95112
public int hashCode() {
96-
return previousKey != null ? previousKey.hashCode() : 0;
113+
return previousSignature != null ? Arrays.hashCode(previousSignature) : 0;
97114
}
98115

99116
@Override
100117
public String toString() {
101-
return "ThirdPartyBlockRequest{previousKey=" + previousKey + '}';
118+
return "ThirdPartyBlockRequest{previousKey=" + previousSignature + '}';
102119
}
103120
}

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.eclipse.biscuit.error.Error;
2828
import org.eclipse.biscuit.token.format.ExternalSignature;
2929
import org.eclipse.biscuit.token.format.SerializedBiscuit;
30+
import org.eclipse.biscuit.token.format.SignedBlock;
3031

3132
/**
3233
* UnverifiedBiscuit auth token. UnverifiedBiscuit means it's deserialized without checking
@@ -262,45 +263,43 @@ public List<PublicKey> blockPublicKeys(int index) {
262263

263264
/** Generates a third party block request from a token */
264265
public ThirdPartyBlockRequest thirdPartyRequest() {
265-
PublicKey previousKey;
266+
byte[] previousSignature;
266267
if (this.serializedBiscuit.getBlocks().isEmpty()) {
267-
previousKey = this.serializedBiscuit.getAuthority().getKey();
268+
previousSignature = this.serializedBiscuit.getAuthority().getSignature();
268269
} else {
269-
previousKey =
270+
previousSignature =
270271
this.serializedBiscuit
271272
.getBlocks()
272273
.get(this.serializedBiscuit.getBlocks().size() - 1)
273-
.getKey();
274+
.getSignature();
274275
}
275276

276-
return new ThirdPartyBlockRequest(previousKey);
277+
return new ThirdPartyBlockRequest(previousSignature);
277278
}
278279

279280
/** Generates a third party block request from a token */
280281
public UnverifiedBiscuit appendThirdPartyBlock(
281282
PublicKey externalKey, ThirdPartyBlockContents blockResponse)
282283
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException, Error {
283-
PublicKey previousKey;
284+
SignedBlock previousBlock;
284285
if (this.serializedBiscuit.getBlocks().isEmpty()) {
285-
previousKey = this.serializedBiscuit.getAuthority().getKey();
286+
previousBlock = this.serializedBiscuit.getAuthority();
286287
} else {
287-
previousKey =
288-
this.serializedBiscuit
289-
.getBlocks()
290-
.get(this.serializedBiscuit.getBlocks().size() - 1)
291-
.getKey();
288+
previousBlock =
289+
this.serializedBiscuit.getBlocks().get(this.serializedBiscuit.getBlocks().size() - 1);
292290
}
293-
KeyPair nextKeyPair = KeyPair.generate(previousKey.getAlgorithm());
291+
KeyPair nextKeyPair = KeyPair.generate(previousBlock.getKey().getAlgorithm());
294292
byte[] payload =
295-
BlockSignatureBuffer.generateExternalBlockSignaturePayloadV0(
296-
blockResponse.getPayload(), previousKey);
293+
BlockSignatureBuffer.generateExternalBlockSignaturePayloadV1(
294+
blockResponse.getPayload(),
295+
previousBlock.getSignature(),
296+
BlockSignatureBuffer.THIRD_PARTY_SIGNATURE_VERSION);
297297
if (!externalKey.verify(payload, blockResponse.getSignature())) {
298298
throw new Error.FormatError.Signature.InvalidSignature(
299299
"signature error: Verification equation was not satisfied");
300300
}
301301

302-
Either<Error.FormatError, Block> res =
303-
Block.fromBytes(blockResponse.getPayload(), Option.some(externalKey));
302+
var res = Block.fromBytes(blockResponse.getPayload(), Option.some(externalKey));
304303
if (res.isLeft()) {
305304
throw res.getLeft();
306305
}
@@ -312,7 +311,7 @@ public UnverifiedBiscuit appendThirdPartyBlock(
312311

313312
UnverifiedBiscuit copiedBiscuit = this.copy();
314313

315-
Either<Error.FormatError, SerializedBiscuit> containerRes =
314+
var containerRes =
316315
copiedBiscuit.serializedBiscuit.append(nextKeyPair, block, Option.some(externalSignature));
317316
if (containerRes.isLeft()) {
318317
throw containerRes.getLeft();

0 commit comments

Comments
 (0)