-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix cert hash * claim signature * checkstyle * tan expiration
- Loading branch information
Showing
7 changed files
with
232 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/test/java/eu/europa/ec/dgc/issuance/GenerateWalletRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/*- | ||
* ---license-start | ||
* EU Digital Green Certificate Issuance Service / dgca-issuance-service | ||
* --- | ||
* Copyright (C) 2021 T-Systems International GmbH and all other contributors | ||
* --- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ---license-end | ||
*/ | ||
|
||
package eu.europa.ec.dgc.issuance; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import eu.europa.ec.dgc.issuance.restapi.dto.ClaimRequest; | ||
import eu.europa.ec.dgc.issuance.restapi.dto.PublicKey; | ||
import eu.europa.ec.dgc.issuance.service.TanService; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.InvalidKeyException; | ||
import java.security.KeyPair; | ||
import java.security.KeyPairGenerator; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.Signature; | ||
import java.security.SignatureException; | ||
import java.util.Base64; | ||
import org.junit.Test; | ||
|
||
public class GenerateWalletRequestTest { | ||
// This can be used to generate valid json structure for claim | ||
@Test | ||
public void testGenerateWalletRequest() throws Exception { | ||
TanService tanService = new TanService(); | ||
|
||
// Please adapt this to your certificate (the values can be get from browser network log | ||
// see POST /dgci | ||
// and PUT /dgci/{id} | ||
String dgci = "dgci:V1:DE:2e974b3b-d932-4bc9-bbae-d387f93f8bf3:edbcb873196f24be"; | ||
String certHash = "mfg0MI7wPFexNkOa4n9OKojrzhe9a9lcim4JzJO3WtY="; | ||
String tan = "U7ULCYZY"; | ||
String tanHash = tanService.hashTan(tan); | ||
|
||
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); | ||
keyPairGen.initialize(2048); | ||
KeyPair keyPair = keyPairGen.generateKeyPair(); | ||
byte[] publicKeyBytes = keyPair.getPublic().getEncoded(); | ||
String sigAlg = "SHA256WithRSA"; | ||
|
||
ClaimRequest claimRequest = new ClaimRequest(); | ||
claimRequest.setDgci(dgci); | ||
claimRequest.setTanHash(tanHash); | ||
PublicKey publicKeyDTO = new PublicKey(); | ||
publicKeyDTO.setType(keyPair.getPublic().getAlgorithm()); | ||
publicKeyDTO.setValue(Base64.getEncoder().encodeToString(publicKeyBytes)); | ||
claimRequest.setPublicKey(publicKeyDTO); | ||
|
||
claimRequest.setSigAlg(sigAlg); | ||
claimRequest.setCertHash(certHash); | ||
createClaimSignature(claimRequest, keyPair.getPrivate(), sigAlg); | ||
|
||
ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); | ||
System.out.println(objectMapper.writeValueAsString(claimRequest)); | ||
} | ||
|
||
private void createClaimSignature(ClaimRequest claimRequest, PrivateKey privateKey, String sigAlg) throws InvalidKeyException, NoSuchAlgorithmException, SignatureException { | ||
StringBuilder sigValue = new StringBuilder(); | ||
sigValue.append(claimRequest.getTanHash()) | ||
.append(claimRequest.getCertHash()) | ||
.append(claimRequest.getPublicKey().getValue()); | ||
Signature signature = Signature.getInstance(sigAlg); | ||
signature.initSign(privateKey); | ||
signature.update(sigValue.toString().getBytes(StandardCharsets.UTF_8)); | ||
byte[] sigData = signature.sign(); | ||
claimRequest.setSignature(Base64.getEncoder().encodeToString(sigData)); | ||
} | ||
|
||
@Test | ||
public void testGenerateWalletRequestEC() throws Exception { | ||
TanService tanService = new TanService(); | ||
|
||
// Please adapt this to your certificate (the values can be get from browser network log | ||
// see POST /dgci | ||
// and PUT /dgci/{id} | ||
String dgci = "dgci:V1:DE:2e974b3b-d932-4bc9-bbae-d387f93f8bf3:edbcb873196f24be"; | ||
String certHash = "mfg0MI7wPFexNkOa4n9OKojrzhe9a9lcim4JzJO3WtY="; | ||
String tan = "U7ULCYZY"; | ||
String tanHash = tanService.hashTan(tan); | ||
|
||
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("EC"); | ||
keyPairGen.initialize(256); | ||
KeyPair keyPair = keyPairGen.generateKeyPair(); | ||
byte[] publicKeyBytes = keyPair.getPublic().getEncoded(); | ||
String sigAlg = "SHA256withECDSA"; | ||
|
||
ClaimRequest claimRequest = new ClaimRequest(); | ||
claimRequest.setDgci(dgci); | ||
claimRequest.setTanHash(tanHash); | ||
PublicKey publicKeyDTO = new PublicKey(); | ||
publicKeyDTO.setType(keyPair.getPublic().getAlgorithm()); | ||
publicKeyDTO.setValue(Base64.getEncoder().encodeToString(publicKeyBytes)); | ||
claimRequest.setPublicKey(publicKeyDTO); | ||
|
||
claimRequest.setSigAlg(sigAlg); | ||
claimRequest.setCertHash(certHash); | ||
createClaimSignature(claimRequest,keyPair.getPrivate(),sigAlg); | ||
|
||
ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); | ||
System.out.println(objectMapper.writeValueAsString(claimRequest)); | ||
} | ||
|
||
} |
72 changes: 0 additions & 72 deletions
72
src/test/java/eu/europa/ec/dgc/issuance/GerateWalletRequestTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.