33import biscuit .format .schema .Schema ;
44import biscuit .format .schema .Schema .PublicKey .Algorithm ;
55import com .google .protobuf .ByteString ;
6-
7- import java .io .IOException ;
6+ import java .security .InvalidKeyException ;
87import java .security .NoSuchAlgorithmException ;
9- import java .security .spec .InvalidKeySpecException ;
10- import java .util .Arrays ;
8+ import java .security .SignatureException ;
119import java .util .Optional ;
1210import java .util .Set ;
1311import org .biscuitsec .biscuit .error .Error ;
1412import org .biscuitsec .biscuit .token .builder .Utils ;
15- import org .bouncycastle .crypto .params .Ed25519PublicKeyParameters ;
16- import org .bouncycastle .jcajce .provider .asymmetric .ec .BCECPublicKey ;
17- import org .bouncycastle .jcajce .provider .asymmetric .edec .BCEdDSAPublicKey ;
18-
19- public final class PublicKey {
2013
21- private final byte [] key ;
22- private final Algorithm algorithm ;
14+ public abstract class PublicKey {
2315
2416 private static final Set <Algorithm > SUPPORTED_ALGORITHMS =
2517 Set .of (Algorithm .Ed25519 , Algorithm .SECP256R1 );
2618
27- public PublicKey (Algorithm algorithm , java . security . PublicKey publicKey ) {
28- this . key = publicKey . getEncoded ();
29- this . algorithm = algorithm ;
30- }
31-
32- public PublicKey ( Algorithm algorithm , byte [] data ) {
33- this . key = data ;
34- this . algorithm = algorithm ;
19+ public static PublicKey load (Algorithm algorithm , byte [] data ) {
20+ if ( algorithm == Algorithm . Ed25519 ) {
21+ return Ed25519PublicKey . loadEd25519 ( data ) ;
22+ } else if ( algorithm == Algorithm . SECP256R1 ) {
23+ return Secp256R1PublicKey . loadSecp256r1 ( data );
24+ } else {
25+ throw new IllegalArgumentException ( "Unsupported algorithm" ) ;
26+ }
3527 }
3628
37- public PublicKey (Algorithm algorithm , String hex ) {
38- this .key = Utils .hexStringToByteArray (hex );
39- this .algorithm = algorithm ;
29+ public static PublicKey load (Algorithm algorithm , String hex ) {
30+ return load (algorithm , Utils .hexStringToByteArray (hex ));
4031 }
4132
42- public byte [] toBytes () {
43- return this .key ;
44- }
33+ public abstract byte [] toBytes ();
4534
4635 public String toHex () {
4736 return Utils .byteArrayToHexString (this .toBytes ());
@@ -50,7 +39,7 @@ public String toHex() {
5039 public Schema .PublicKey serialize () {
5140 Schema .PublicKey .Builder publicKey = Schema .PublicKey .newBuilder ();
5241 publicKey .setKey (ByteString .copyFrom (this .toBytes ()));
53- publicKey .setAlgorithm (this .algorithm );
42+ publicKey .setAlgorithm (this .getAlgorithm () );
5443 return publicKey .build ();
5544 }
5645
@@ -59,7 +48,7 @@ public static PublicKey deserialize(Schema.PublicKey pk)
5948 if (!pk .hasAlgorithm () || !pk .hasKey () || !SUPPORTED_ALGORITHMS .contains (pk .getAlgorithm ())) {
6049 throw new Error .FormatError .DeserializationError ("Invalid public key" );
6150 }
62- return new PublicKey (pk .getAlgorithm (), pk .getKey ().toByteArray ());
51+ return PublicKey . load (pk .getAlgorithm (), pk .getKey ().toByteArray ());
6352 }
6453
6554 public static Optional <Error > validateSignatureLength (Algorithm algorithm , int length ) {
@@ -80,37 +69,8 @@ public static Optional<Error> validateSignatureLength(Algorithm algorithm, int l
8069 return error ;
8170 }
8271
83- @ Override
84- public boolean equals (Object o ) {
85- if (this == o ) {
86- return true ;
87- }
88- if (o == null || getClass () != o .getClass ()) {
89- return false ;
90- }
72+ public abstract Algorithm getAlgorithm ();
9173
92- PublicKey publicKey = (PublicKey ) o ;
93-
94- return this .algorithm .equals (publicKey .algorithm ) && Arrays .equals (this .key , publicKey .toBytes ());
95- }
96-
97- @ Override
98- public int hashCode () {
99- return Arrays .hashCode (this .key );
100- }
101-
102- @ Override
103- public String toString () {
104- if (this .algorithm == Algorithm .Ed25519 ) {
105- return "ed25519/" + toHex ().toLowerCase ();
106- } else if (this .algorithm == Algorithm .SECP256R1 ) {
107- return "secp256r1/" + toHex ().toLowerCase ();
108- } else {
109- return null ;
110- }
111- }
112-
113- public Algorithm getAlgorithm () {
114- return this .algorithm ;
115- }
74+ public abstract boolean verify (byte [] data , byte [] signature )
75+ throws InvalidKeyException , SignatureException , NoSuchAlgorithmException ;
11676}
0 commit comments