Skip to content

Commit

Permalink
cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
bhuism committed Dec 4, 2024
1 parent e91833d commit 58861f2
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 34 deletions.
5 changes: 4 additions & 1 deletion src/main/java/nl/ictu/PseudoniemenServiceApplication.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nl.ictu;


import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.NoArgsConstructor;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.boot.SpringApplication;
Expand All @@ -9,7 +10,9 @@
import java.security.NoSuchAlgorithmException;
import java.security.Security;

@SuppressWarnings("HideUtilityClassConstructor")
@SuppressWarnings({"HideUtilityClassConstructor"})
@SuppressFBWarnings(value = "EI_EXPOSE_STATIC_REP2",
justification = "nl.ictu.PseudoniemenServiceApplication$$SpringCGLIB$$0")
@SpringBootApplication
@NoArgsConstructor
public class PseudoniemenServiceApplication {
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/nl/ictu/controller/v1/ExchangeIdentifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import lombok.SneakyThrows;
import nl.ictu.Identifier;
import nl.ictu.pseudoniemenservice.generated.server.api.ExchangeIdentifierApi;
import nl.ictu.pseudoniemenservice.generated.server.model.WsExchangeIdentifierForIdentifierRequest;
import nl.ictu.pseudoniemenservice.generated.server.model.WsExchangeIdentifierRequest;
import nl.ictu.pseudoniemenservice.generated.server.model.WsExchangeIdentifierResponse;
import nl.ictu.pseudoniemenservice.generated.server.model.WsIdentifier;
import nl.ictu.pseudoniemenservice.generated.server.model.WsIdentifierTypes;
import nl.ictu.service.AesGcmSivCryptographer;
import nl.ictu.service.IdentifierConverter;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -24,13 +23,11 @@
@RestController
public final class ExchangeIdentifier implements ExchangeIdentifierApi, VersionOneController {

private final IdentifierConverter identifierConverter;

private final AesGcmSivCryptographer aesGcmSivCryptographer;

@Override
@SneakyThrows
public ResponseEntity<WsExchangeIdentifierResponse> exchangeIdentifierForIdentifier(final String callerOIN, final WsExchangeIdentifierForIdentifierRequest wsExchangeIdentifierForIdentifierRequest) {
public ResponseEntity<WsExchangeIdentifierResponse> exchangeIdentifier(final String callerOIN, final WsExchangeIdentifierRequest wsExchangeIdentifierForIdentifierRequest) {

final WsIdentifier wsIdentifierRequest = wsExchangeIdentifierForIdentifierRequest.getIdentifier();

Expand Down
7 changes: 2 additions & 5 deletions src/main/java/nl/ictu/controller/v1/ExchangeToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import nl.ictu.Identifier;
import nl.ictu.Token;
import nl.ictu.pseudoniemenservice.generated.server.api.ExchangeTokenApi;
import nl.ictu.pseudoniemenservice.generated.server.model.WsExchangeTokenForIdentifierRequest;
import nl.ictu.pseudoniemenservice.generated.server.model.WsExchangeTokenRequest;
import nl.ictu.pseudoniemenservice.generated.server.model.WsExchangeTokenResponse;
import nl.ictu.pseudoniemenservice.generated.server.model.WsIdentifier;
import nl.ictu.service.AesGcmCryptographer;
import nl.ictu.service.AesGcmSivCryptographer;
import nl.ictu.service.IdentifierConverter;
import nl.ictu.service.TokenConverter;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -31,11 +30,9 @@ public final class ExchangeToken implements ExchangeTokenApi, VersionOneControll

private final TokenConverter tokenConverter;

private final IdentifierConverter identifierConverter;

@Override
@SneakyThrows
public ResponseEntity<WsExchangeTokenResponse> exchangeTokenForIdentifier(final String callerOIN, final WsExchangeTokenForIdentifierRequest wsExchangeTokenForIdentifierRequest) {
public ResponseEntity<WsExchangeTokenResponse> exchangeToken(final String callerOIN, final WsExchangeTokenRequest wsExchangeTokenForIdentifierRequest) {

final String encodedToken = aesGcmCryptographer.decrypt(wsExchangeTokenForIdentifierRequest.getToken(), callerOIN);

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/nl/ictu/controller/v1/GetToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import nl.ictu.pseudoniemenservice.generated.server.model.WsGetTokenResponse;
import nl.ictu.service.AesGcmCryptographer;
import nl.ictu.service.AesGcmSivCryptographer;
import nl.ictu.service.IdentifierConverter;
import nl.ictu.service.TokenConverter;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -26,8 +25,6 @@ public final class GetToken implements GetTokenApi, VersionOneController {

private final TokenConverter tokenConverter;

private final IdentifierConverter identifierConverter;

@Override
@SneakyThrows
public ResponseEntity<WsGetTokenResponse> getToken(final String callerOIN, final WsGetTokenRequest wsGetTokenRequest) {
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/nl/ictu/service/AESHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ public static GCMParameterSpec generateIV() {
byte[] iv = new byte[IV_LENGTH]; // AES block size is 16 bytes
SECURE_RANDOM.nextBytes(iv);

final GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH, iv);
return new GCMParameterSpec(TAG_LENGTH, iv);

return gcmParameterSpec;
}

public static GCMParameterSpec createIVfromValues(final byte[] iv) {
final GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(TAG_LENGTH, iv);
return gcmParameterSpec;
return new GCMParameterSpec(TAG_LENGTH, iv);
}

public static Cipher createCipher() throws NoSuchPaddingException, NoSuchAlgorithmException {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/nl/ictu/service/AesGcmCryptographerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ private SecretKey createSecretKey(final String salt) {

byte[] key = sha256Digest.digest(salterSecretBytes);

final SecretKey secretKey = new SecretKeySpec(key, "AES");
return new SecretKeySpec(key, "AES");

return secretKey;
}

@Override
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/nl/ictu/service/AesGcmSivCryptographerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import nl.ictu.Identifier;
import nl.ictu.configuration.PseudoniemenServiceProperties;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.MultiBlockCipher;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.GCMSIVBlockCipher;
import org.bouncycastle.crypto.params.AEADParameters;
Expand All @@ -29,6 +30,8 @@
@Service
public class AesGcmSivCryptographerImpl implements AesGcmSivCryptographer {

public static final int MAC_SIZE = 128;

private final PseudoniemenServiceProperties pseudoniemenServiceProperties;

private static final int NONCE_LENTH = 12;
Expand All @@ -37,7 +40,7 @@ public class AesGcmSivCryptographerImpl implements AesGcmSivCryptographer {

private final Base64.Decoder base64Decoder = Base64.getDecoder();

private final AESEngine aesEngine;
private final MultiBlockCipher aesEngine;

private final MessageDigest sha256Digest;

Expand All @@ -50,7 +53,7 @@ public AesGcmSivCryptographerImpl(final PseudoniemenServiceProperties pseudoniem
pseudoniemenServiceProperties = pseudoniemenServicePropertiesArg;
identifierConverter = identifierConverterArg;

aesEngine = new AESEngine();
aesEngine = AESEngine.newInstance();
sha256Digest = MessageDigest.getInstance("SHA-256");

if (!StringUtils.hasText(pseudoniemenServiceProperties.getIdentifierPrivateKey())) {
Expand All @@ -69,9 +72,7 @@ private AEADParameters createSecretKey(final String salt) {

final KeyParameter keyParameter = new KeyParameter(base64Decoder.decode(identifierPrivateKey));

final AEADParameters cipherParameter = new AEADParameters(keyParameter, 128, nonce12);

return cipherParameter;
return new AEADParameters(keyParameter, MAC_SIZE, nonce12);

}

Expand Down Expand Up @@ -117,9 +118,7 @@ public Identifier decrypt(final String ciphertextString, final String salt) thro

final String encodedIdentifier = new String(plaintext, StandardCharsets.UTF_8);

final Identifier identifier = identifierConverter.decode(encodedIdentifier);

return identifier;
return identifierConverter.decode(encodedIdentifier);

}

Expand Down
11 changes: 9 additions & 2 deletions src/main/resources/public/v1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ servers:
paths:
/getToken:
post:
tags:
- Token
summary: get a token
operationId: getToken
parameters:
Expand All @@ -19,8 +21,11 @@ paths:
$ref: '#/components/responses/getTokenResponse'
/exchangeToken:
post:
tags:
- Token
- Identifier
summary: excgange token for an identifier
operationId: exchangeTokenForIdentifier
operationId: exchangeToken
parameters:
- $ref: "#/components/parameters/callerOIN"
requestBody:
Expand All @@ -30,8 +35,10 @@ paths:
$ref: '#/components/responses/exchangeTokenResponse'
/exchangeIdentifier:
post:
tags:
- Identifier
summary: exchange an identifier for another identifier
operationId: exchangeIdentifierForIdentifier
operationId: exchangeIdentifier
parameters:
- $ref: "#/components/parameters/callerOIN"
requestBody:
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/nl/ictu/service/TestAesGcmCryptographer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
@ActiveProfiles("test")
public class TestAesGcmCryptographer {

private AesGcmCryptographer aesGcmCryptographer = new AesGcmCryptographerImpl(new PseudoniemenServiceProperties().setTokenPrivateKey("bFUyS1FRTVpON0pCSFFRRGdtSllSeUQ1MlRna2txVmI="));
private final AesGcmCryptographer aesGcmCryptographer = new AesGcmCryptographerImpl(new PseudoniemenServiceProperties().setTokenPrivateKey("bFUyS1FRTVpON0pCSFFRRGdtSllSeUQ1MlRna2txVmI="));

private Set<String> testStrings = new HashSet<>(Arrays.asList("a", "bb", "dsv", "ghad", "dhaht", "uDg5Av", "d93fdvv", "dj83hzHo", "38iKawKv9", "dk(gkzm)Mh", "gjk)s3$g9cQ"));
private final Set<String> testStrings = new HashSet<>(Arrays.asList("a", "bb", "dsv", "ghad", "dhaht", "uDg5Av", "d93fdvv", "dj83hzHo", "38iKawKv9", "dk(gkzm)Mh", "gjk)s3$g9cQ"));

@Test
public void testEncyptDecryptForDifferentStringLengths() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/nl/ictu/service/TestAesGcmSivCryptographer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
@ActiveProfiles("test")
public class TestAesGcmSivCryptographer {

private AesGcmSivCryptographer aesGcmSivCryptographer = new AesGcmSivCryptographerImpl(
private final AesGcmSivCryptographer aesGcmSivCryptographer = new AesGcmSivCryptographerImpl(
new PseudoniemenServiceProperties().setIdentifierPrivateKey("QTBtVEhLN3EwMHJ3QXN1ZUFqNzVrT3hDQTBIWWNIZTU="),
new IdentifierConverterImpl(new ObjectMapper())
);

private Set<String> testStrings = new HashSet<>(Arrays.asList("a", "bb", "dsv", "ghad", "dhaht", "uDg5Av", "d93fdvv", "dj83hzHo", "38iKawKv9", "dk(gkzm)Mh", "gjk)s3$g9cQ"));
private final Set<String> testStrings = new HashSet<>(Arrays.asList("a", "bb", "dsv", "ghad", "dhaht", "uDg5Av", "d93fdvv", "dj83hzHo", "38iKawKv9", "dk(gkzm)Mh", "gjk)s3$g9cQ"));

@Test
public void testEncyptDecryptForDifferentStringLengths() {
Expand Down

0 comments on commit 58861f2

Please sign in to comment.