Search in sources :

Example 11 with EncrypterException

use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.

the class SecurityConfig method getDefaultEncryptionProviders.

private List<EncryptionProvider> getDefaultEncryptionProviders() {
    final List<EncryptionProvider> encryptionProviderList = new ArrayList<>();
    try {
        final JreEncryptionProvider jreEncryptionProvider = new JreEncryptionProvider(this.jreEncryptionKeyResource.getFile());
        encryptionProviderList.add(jreEncryptionProvider);
        if (this.hsmKeystoreResource.isPresent()) {
            final HsmEncryptionProvider hsmEncryptionProvider = new HsmEncryptionProvider(this.hsmKeystoreResource.get().getFile());
            encryptionProviderList.add(hsmEncryptionProvider);
        }
        return encryptionProviderList;
    } catch (final IOException | EncrypterException e) {
        throw new IllegalStateException("Error creating default encryption providers", e);
    }
}
Also used : JreEncryptionProvider(org.opensmartgridplatform.shared.security.providers.JreEncryptionProvider) HsmEncryptionProvider(org.opensmartgridplatform.shared.security.providers.HsmEncryptionProvider) ArrayList(java.util.ArrayList) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) HsmEncryptionProvider(org.opensmartgridplatform.shared.security.providers.HsmEncryptionProvider) EncryptionProvider(org.opensmartgridplatform.shared.security.providers.EncryptionProvider) JreEncryptionProvider(org.opensmartgridplatform.shared.security.providers.JreEncryptionProvider) IOException(java.io.IOException)

Example 12 with EncrypterException

use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.

the class SecretManagementServiceTest method storeSecretsEncryptionError.

@Test
public void storeSecretsEncryptionError() throws EncrypterException {
    final TypedSecret typedSecret = new TypedSecret("$3cr3t".getBytes(), SecretType.E_METER_MASTER_KEY);
    final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
    keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
    keyReference.setReference("keyReferenceString");
    when(this.keyRepository.findByTypeAndValid(any(), any())).thenReturn(Arrays.asList(keyReference));
    when(this.encryptionDelegate.encrypt(any(), any(), anyString())).thenThrow(new EncrypterException("Encryption error"));
    assertThatIllegalStateException().isThrownBy(() -> this.service.storeSecrets(SOME_DEVICE, Arrays.asList(typedSecret)));
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) TypedSecret(org.opensmartgridplatform.secretmanagement.application.domain.TypedSecret) Test(org.junit.jupiter.api.Test)

Example 13 with EncrypterException

use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.

the class SecretManagementServiceTest method retrieveSecretsDecryptionError.

@Test
public void retrieveSecretsDecryptionError() throws EncrypterException {
    final DbEncryptionKeyReference keyReference = new DbEncryptionKeyReference();
    keyReference.setEncryptionProviderType(ENCRYPTION_PROVIDER_TYPE);
    keyReference.setReference("1");
    final DbEncryptedSecret secret = new DbEncryptedSecret();
    secret.setSecretType(SecretType.E_METER_MASTER_KEY);
    secret.setEncryptionKeyReference(keyReference);
    secret.setEncodedSecret("0123456789ABCDEF0123456789ABCDEF");
    final List<DbEncryptedSecret> secretPage = Arrays.asList(secret);
    when(this.secretRepository.findSecrets(SOME_DEVICE, SecretType.E_METER_MASTER_KEY, SecretStatus.ACTIVE)).thenReturn(secretPage);
    when(this.encryptionDelegate.decrypt(any(), any())).thenThrow(new EncrypterException("Decryption error"));
    assertThatIllegalStateException().isThrownBy(() -> this.service.retrieveSecrets(SOME_DEVICE, Arrays.asList(SecretType.E_METER_MASTER_KEY)));
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) DbEncryptedSecret(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptedSecret) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) Test(org.junit.jupiter.api.Test)

Example 14 with EncrypterException

use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.

the class SecretManagementService method generateAes128BitsSecret.

private EncryptedTypedSecret generateAes128BitsSecret(final SecretType secretType) {
    try {
        final DbEncryptionKeyReference currentKey = this.getCurrentKey();
        final byte[] aesEncrypted = this.encryptionDelegateForKeyStorage.generateAes128BitsSecret(this.encryptionProviderType, currentKey.getReference());
        return new EncryptedTypedSecret(aesEncrypted, secretType, currentKey.getReference(), currentKey.getEncryptionProviderType());
    } catch (final EncrypterException ee) {
        throw new IllegalStateException("Eror generating secret", ee);
    }
}
Also used : DbEncryptionKeyReference(org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)

Example 15 with EncrypterException

use of org.opensmartgridplatform.shared.exceptionhandling.EncrypterException in project open-smart-grid-platform by OSGP.

the class AbstractReplaceKeyCommandExecutor method decryptRsaKeys.

private SetKeysRequestDto decryptRsaKeys(final SetKeysRequestDto setKeysRequestDto) throws FunctionalException {
    try {
        final byte[] authenticationKey = this.decrypterForGxfSmartMetering.decrypt(setKeysRequestDto.getAuthenticationKey());
        final byte[] encryptionKey = this.decrypterForGxfSmartMetering.decrypt(setKeysRequestDto.getEncryptionKey());
        return new SetKeysRequestDto(authenticationKey, encryptionKey);
    } catch (final EncrypterException e) {
        throw new FunctionalException(FunctionalExceptionType.DECRYPTION_EXCEPTION, ComponentType.PROTOCOL_DLMS, e);
    }
}
Also used : SetKeysRequestDto(org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)

Aggregations

EncrypterException (org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)20 IOException (java.io.IOException)9 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 Cipher (javax.crypto.Cipher)4 ConnectionException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)4 FunctionalException (org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)4 DbEncryptionKeyReference (org.opensmartgridplatform.secretmanagement.application.domain.DbEncryptionKeyReference)3 UnknownHostException (java.net.UnknownHostException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 KeyFactory (java.security.KeyFactory)2 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)2 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 Test (org.junit.jupiter.api.Test)2 ProtocolAdapterException (org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException)2 SetKeysRequestDto (org.opensmartgridplatform.dto.valueobjects.smartmetering.SetKeysRequestDto)2 TechnicalException (org.opensmartgridplatform.shared.exceptionhandling.TechnicalException)2 ArrayList (java.util.ArrayList)1 KeyGenerator (javax.crypto.KeyGenerator)1