forked from eclipse-biscuit/biscuit-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignatureTest.java
More file actions
161 lines (137 loc) · 6.04 KB
/
SignatureTest.java
File metadata and controls
161 lines (137 loc) · 6.04 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
/*
* Copyright (c) 2019 Geoffroy Couprie <contact@geoffroycouprie.com> and Contributors to the Eclipse Foundation.
* SPDX-License-Identifier: Apache-2.0
*/
package org.eclipse.biscuit.crypto;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import biscuit.format.schema.Schema;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.SignatureException;
import org.eclipse.biscuit.error.Error;
import org.eclipse.biscuit.error.Result;
import org.eclipse.biscuit.token.Biscuit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* @serial exclude
*/
public class SignatureTest {
private SecureRandom rng;
@BeforeEach
public void setUp() throws NoSuchAlgorithmException {
byte[] seed = {0, 0, 0, 0};
rng = SecureRandom.getInstance("SHA1PRNG");
rng.setSeed(seed);
}
@Test
public void testSerialize() throws Error.FormatError, NoSuchAlgorithmException {
prTestSerialize(Schema.PublicKey.Algorithm.Ed25519, 32);
prTestSerialize(
// compressed - 0x02 or 0x03 prefix byte, 32 bytes for X coordinate
Schema.PublicKey.Algorithm.SECP256R1, 33);
}
@Test
public void testHex() throws Error.FormatError {
prGenSigKeys(Schema.PublicKey.Algorithm.SECP256R1);
prGenSigKeys(Schema.PublicKey.Algorithm.Ed25519);
}
@Test
public void testThreeMessages()
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
prTestThreeMessages(Schema.PublicKey.Algorithm.Ed25519);
prTestThreeMessages(Schema.PublicKey.Algorithm.SECP256R1);
}
@Test
public void testSerializeBiscuit() throws Error {
var root = KeyPair.generate(Schema.PublicKey.Algorithm.SECP256R1);
var biscuit =
Biscuit.builder(root)
.addAuthorityFact("user(\"1234\")")
.addAuthorityCheck("check if operation(\"read\")")
.build();
var serialized = biscuit.serialize();
var unverified = Biscuit.fromBytes(serialized);
assertDoesNotThrow(() -> unverified.verify(root.getPublicKey()));
}
@Test
void testInvalidSepc256r1Key() {
assertThrows(
Error.FormatError.InvalidKeySize.class,
() -> KeyPair.generate(Schema.PublicKey.Algorithm.SECP256R1, "badkey".getBytes()));
}
@Test
void testInvalidEd25519Key() {
assertThrows(
Error.FormatError.InvalidKeySize.class,
() -> KeyPair.generate(Schema.PublicKey.Algorithm.Ed25519, "badkey".getBytes()));
}
@Test
void testInvalidSepc256r1PublicKey() {
assertThrows(
Error.FormatError.InvalidKey.class,
() -> PublicKey.load(Schema.PublicKey.Algorithm.SECP256R1, "badkey".getBytes()));
}
@Test
void testInvalidEd25519PublicKey() {
assertThrows(
Error.FormatError.InvalidKey.class,
() -> PublicKey.load(Schema.PublicKey.Algorithm.Ed25519, "badkey".getBytes()));
}
private void prTestSerialize(Schema.PublicKey.Algorithm algorithm, int expectedPublicKeyLength)
throws Error.FormatError, NoSuchAlgorithmException {
KeyPair keypair = KeyPair.generate(algorithm, rng);
PublicKey pubkey = keypair.getPublicKey();
byte[] serializedSecretKey = keypair.toBytes();
byte[] serializedPublicKey = pubkey.toBytes();
final KeyPair deserializedSecretKey = KeyPair.generate(algorithm, serializedSecretKey);
final PublicKey deserializedPublicKey = PublicKey.load(algorithm, serializedPublicKey);
assertEquals(32, serializedSecretKey.length);
assertEquals(expectedPublicKeyLength, serializedPublicKey.length);
System.out.println(keypair.toHex());
System.out.println(deserializedSecretKey.toHex());
assertArrayEquals(keypair.toBytes(), deserializedSecretKey.toBytes());
System.out.println(pubkey.toHex());
System.out.println(deserializedPublicKey.toHex());
assertEquals(pubkey.toHex(), deserializedPublicKey.toHex());
}
private void prTestThreeMessages(Schema.PublicKey.Algorithm algorithm)
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
String message1 = "hello";
KeyPair root = KeyPair.generate(algorithm, rng);
KeyPair keypair2 = KeyPair.generate(algorithm, rng);
System.out.println("root key: " + root.toHex());
System.out.println("keypair2: " + keypair2.toHex());
System.out.println("root key public: " + root.getPublicKey().toHex());
System.out.println("keypair2 public: " + keypair2.getPublicKey().toHex());
Token token1 = new Token(root, message1.getBytes(), keypair2);
assertEquals(Result.ok(null), token1.verify(root.getPublicKey()));
String message2 = "world";
KeyPair keypair3 = KeyPair.generate(algorithm, rng);
Token token2 = token1.append(keypair3, message2.getBytes());
assertEquals(Result.ok(null), token2.verify(root.getPublicKey()));
String message3 = "!!";
KeyPair keypair4 = KeyPair.generate(algorithm, rng);
Token token3 = token2.append(keypair4, message3.getBytes());
assertEquals(Result.ok(null), token3.verify(root.getPublicKey()));
}
private static void prGenSigKeys(Schema.PublicKey.Algorithm algorithm) throws Error.FormatError {
var keypair = KeyPair.generate(algorithm);
var pubKey = keypair.getPublicKey();
var privHexString = keypair.toHex();
var pubKeyString = pubKey.toHex();
System.out.println(algorithm + " Keypair hex " + privHexString);
System.out.println(algorithm + " pubKey hex " + pubKeyString);
var pubKey2 = PublicKey.load(algorithm, pubKeyString);
var keyPair2 = KeyPair.generate(algorithm, privHexString);
System.out.println(algorithm + " Keypair2 hex " + keyPair2.toHex());
System.out.println(algorithm + " pubKey hex " + pubKey2.toHex());
assertEquals(keypair.toHex(), keyPair2.toHex(), "keypair hex");
assertEquals(pubKey.toHex(), pubKey2.toHex(), "public keys hex equals");
assertEquals(pubKey, pubKey2, "public keys equals");
}
}