66package org .eclipse .biscuit .token ;
77
88import biscuit .format .schema .Schema ;
9+ import com .google .protobuf .ByteString ;
910import com .google .protobuf .InvalidProtocolBufferException ;
1011import io .vavr .control .Either ;
1112import io .vavr .control .Option ;
1415import java .security .InvalidKeyException ;
1516import java .security .NoSuchAlgorithmException ;
1617import java .security .SignatureException ;
17- import java .util .Objects ;
18+ import java .util .Arrays ;
1819import org .eclipse .biscuit .crypto .BlockSignatureBuffer ;
1920import org .eclipse .biscuit .crypto .PublicKey ;
2021import org .eclipse .biscuit .crypto .Signer ;
2324import org .eclipse .biscuit .token .builder .Block ;
2425
2526public 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}
0 commit comments