4
4
import COSE .CoseException ;
5
5
import COSE .OneKey ;
6
6
import com .fasterxml .jackson .databind .ObjectMapper ;
7
+ import com .fasterxml .jackson .databind .SerializationFeature ;
8
+ import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
7
9
import ehn .techiop .hcert .model .CertificatePayload ;
8
10
import ehn .techiop .hcert .model .HealthCertificate ;
9
11
import org .apache .commons .compress .compressors .CompressorException ;
10
12
import org .bouncycastle .jce .provider .BouncyCastleProvider ;
13
+ import org .bouncycastle .x509 .X509V1CertificateGenerator ;
11
14
import org .junit .jupiter .api .BeforeAll ;
12
15
import org .junit .jupiter .api .Test ;
16
+ import se .digg .dgc .payload .v1 .DGCSchemaException ;
17
+ import se .digg .dgc .service .impl .DefaultDGCEncoder ;
18
+ import se .digg .dgc .signatures .impl .DefaultDGCSigner ;
13
19
20
+ import javax .security .auth .x500 .X500Principal ;
14
21
import java .io .IOException ;
15
- import java .security .NoSuchAlgorithmException ;
16
- import java .security .Security ;
22
+ import java .math .BigInteger ;
23
+ import java .nio .charset .StandardCharsets ;
24
+ import java .security .*;
25
+ import java .security .cert .Certificate ;
26
+ import java .security .cert .CertificateException ;
27
+ import java .security .cert .X509Certificate ;
17
28
import java .time .LocalDateTime ;
18
29
import java .time .ZoneOffset ;
19
- import java .util .ArrayList ;
20
- import java .util .Date ;
21
- import java .util .List ;
22
- import java .util .UUID ;
30
+ import java .util .*;
23
31
24
32
import static org .junit .jupiter .api .Assertions .assertEquals ;
25
33
import static org .junit .jupiter .api .Assertions .assertThrows ;
@@ -79,7 +87,7 @@ static void beforeAll() throws Exception {
79
87
@ Test
80
88
void roundtrip () throws CompressorException , CoseException , IOException {
81
89
82
- String encoded = new GreenCertificateEncoder (cborPrivateKey , UUID .randomUUID ().toString ()).encode (json );
90
+ String encoded = new GreenCertificateEncoder (cborPrivateKey , UUID .randomUUID ().toString (). getBytes ( StandardCharsets . UTF_8 ) ).encode (json );
83
91
String result = new GreenCertificateDecoder ((kid , issuer ) -> cborPublicKey .AsPublicKey ()).decode (encoded );
84
92
85
93
ObjectMapper mapper = new ObjectMapper ();
@@ -92,7 +100,7 @@ void wrongPublicKey() throws CompressorException, CoseException, IOException {
92
100
93
101
OneKey anotherPublicKey = OneKey .generateKey (AlgorithmID .ECDSA_256 ).PublicKey ();
94
102
95
- String encoded = new GreenCertificateEncoder (cborPrivateKey , UUID .randomUUID ().toString ()).encode (json );
103
+ String encoded = new GreenCertificateEncoder (cborPrivateKey , UUID .randomUUID ().toString (). getBytes ( StandardCharsets . UTF_8 ) ).encode (json );
96
104
97
105
Exception exception = assertThrows (RuntimeException .class ,
98
106
() -> new GreenCertificateDecoder ((kid , issuer ) -> anotherPublicKey .AsPublicKey ()).decode (encoded ));
@@ -107,7 +115,7 @@ void rsaKeys() throws CompressorException, CoseException, IOException, NoSuchAlg
107
115
108
116
OneKey keys = OneKey .generateKey (AlgorithmID .RSA_PSS_256 );
109
117
110
- String encoded = new GreenCertificateEncoder (keys , UUID .randomUUID ().toString ()).encode (json );
118
+ String encoded = new GreenCertificateEncoder (keys , UUID .randomUUID ().toString (). getBytes ( StandardCharsets . UTF_8 ) ).encode (json );
111
119
String result = new GreenCertificateDecoder ((kid , issuer ) -> keys .AsPublicKey ()).decode (encoded );
112
120
ObjectMapper mapper = new ObjectMapper ();
113
121
assertEquals (mapper .readTree (json ), mapper .readTree (result ));
@@ -170,7 +178,7 @@ void rsaKeys() throws CompressorException, CoseException, IOException, NoSuchAlg
170
178
.build ();
171
179
172
180
CertificatePayload test_0405870109 = new CertificateDSL ()
173
- .withSubject ("Judy " , "Jensen " )
181
+ .withSubject ("Øjvind " , "Ørn " )
174
182
.withExpiredVaccine ()
175
183
.withExpiredTestResult ()
176
184
.withRecovery ()
@@ -181,11 +189,58 @@ void testSetDKExample() throws IOException, CompressorException, CoseException {
181
189
182
190
String input = new ObjectMapper ().writeValueAsString (test_0405870101 );
183
191
184
- String encoded = new GreenCertificateEncoder (cborPrivateKey , UUID .randomUUID ().toString ()).encode (input );
192
+ String encoded = new GreenCertificateEncoder (cborPrivateKey , UUID .randomUUID ().toString (). getBytes ( StandardCharsets . UTF_8 ) ).encode (input );
185
193
String result = new GreenCertificateDecoder ((kid , issuer ) -> cborPublicKey .AsPublicKey ()).decode (encoded );
186
194
187
195
ObjectMapper mapper = new ObjectMapper ();
188
196
assertEquals (mapper .readTree (input ), mapper .readTree (result ));
189
197
}
190
198
199
+ @ Test
200
+ void testAgainstDGCJava () throws CertificateException , IOException , NoSuchAlgorithmException , DGCSchemaException , SignatureException , CompressorException , CoseException , NoSuchProviderException , InvalidKeyException {
201
+
202
+ Security .insertProviderAt (new BouncyCastleProvider (), 1 );
203
+ KeyPairGenerator keyPairGenerator = KeyPairGenerator .getInstance ("RSA" , "BC" );
204
+ keyPairGenerator .initialize (2048 , new SecureRandom ());
205
+
206
+ KeyPair keyPair = keyPairGenerator .generateKeyPair ();
207
+ Certificate certificate = generateCertificate (keyPair );
208
+
209
+
210
+ se .digg .dgc .payload .v1 .DigitalGreenCertificate testData = DefaultDGCExample .getTestDGC ();
211
+ DefaultDGCEncoder encoder = new DefaultDGCEncoder (new DefaultDGCSigner (keyPair .getPrivate (), (X509Certificate ) certificate ));
212
+ String bytes = encoder .encode (testData , LocalDateTime .now ().toInstant (ZoneOffset .UTC ));
213
+ String result = new GreenCertificateDecoder ((kid , issuer ) -> certificate .getPublicKey ()).decode (bytes );
214
+
215
+
216
+ ObjectMapper mapper = new ObjectMapper ();
217
+ mapper .registerModule (new JavaTimeModule ());
218
+
219
+ mapper .configure (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS , false );
220
+
221
+
222
+ DigitalGreenCertificate dgcResult = mapper .readValue (result , CertificatePayload .class ).getHcert ().getDigitalGreenCertificate ();
223
+ assertEquals ( mapper .valueToTree (testData ), mapper .valueToTree (dgcResult ));
224
+ }
225
+
226
+
227
+
228
+ public static Certificate generateCertificate (KeyPair keyPair ) throws CertificateException , NoSuchAlgorithmException , SignatureException , NoSuchProviderException , InvalidKeyException {
229
+ // yesterday
230
+ Date validityBeginDate = new Date (System .currentTimeMillis () - 24 * 60 * 60 * 1000 );
231
+ // in 2 years
232
+ Date validityEndDate = new Date (System .currentTimeMillis () + 2 * 365 * 24 * 60 * 60 * 1000 );
233
+
234
+ X509V1CertificateGenerator certGen = new X509V1CertificateGenerator ();
235
+ X500Principal dnName = new X500Principal ("CN=John Doe, C=DK" );
236
+ certGen .setSerialNumber (BigInteger .valueOf (System .currentTimeMillis ()));
237
+ certGen .setSubjectDN (dnName );
238
+ certGen .setIssuerDN (dnName ); // use the same
239
+ certGen .setNotBefore (validityBeginDate );
240
+ certGen .setNotAfter (validityEndDate );
241
+ certGen .setPublicKey (keyPair .getPublic ());
242
+ certGen .setSignatureAlgorithm ("SHA256WithRSAEncryption" );
243
+ return certGen .generate (keyPair .getPrivate (), "BC" );
244
+
245
+ }
191
246
}
0 commit comments