Search in sources :

Example 16 with EncrypterException

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

the class CertificateHelper method createPublicKey.

private static PublicKey createPublicKey(final byte[] key, final String algorithm, final String provider) {
    try {
        final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(key);
        final KeyFactory publicKeyFactory = KeyFactory.getInstance(algorithm, provider);
        return publicKeyFactory.generatePublic(publicKeySpec);
    } catch (final GeneralSecurityException e) {
        throw new EncrypterException(String.format("Security exception creating public key for algorithm \"%s\" by provider \"%s\"", algorithm, provider), e);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 17 with EncrypterException

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

the class RsaEncrypter method decrypt.

public byte[] decrypt(final byte[] rsaEncrypted) {
    if (rsaEncrypted == null) {
        throw new IllegalArgumentException("Can not decrypt NULL value");
    }
    try {
        final Cipher cipher = this.getCipher();
        cipher.init(Cipher.DECRYPT_MODE, this.getSecretEncryptionKey(Cipher.DECRYPT_MODE));
        final byte[] decryptedData = cipher.doFinal(rsaEncrypted);
        if (this.checkNullBytesPrepended(decryptedData)) {
            return Arrays.copyOfRange(decryptedData, BLOCK_SIZE, decryptedData.length);
        } else {
            return decryptedData;
        }
    } catch (final Exception e) {
        throw new EncrypterException("Could not decrypt secret", e);
    }
}
Also used : EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) Cipher(javax.crypto.Cipher) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)

Example 18 with EncrypterException

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

the class RsaEncrypter method setPublicKeyStore.

public void setPublicKeyStore(final File publicKeyStoreFile) {
    try {
        final byte[] keyData = Files.readAllBytes(publicKeyStoreFile.toPath());
        final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyData);
        this.publicKey = KeyFactory.getInstance(ALG).generatePublic(publicKeySpec);
    } catch (final NoSuchAlgorithmException | IOException | InvalidKeySpecException e) {
        throw new EncrypterException("Could not set public keystore", e);
    }
}
Also used : EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 19 with EncrypterException

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

the class AbstractEncryptionProvider method decrypt.

@Override
public byte[] decrypt(final EncryptedSecret secret, final String keyReference) {
    if (secret.getType() != this.getType()) {
        throw new EncrypterException("EncryptionProvider for type " + this.getType().name() + " cannot decrypt secrets of type " + secret.getType().name());
    }
    try {
        final Cipher cipher = this.getCipher();
        cipher.init(Cipher.DECRYPT_MODE, this.getSecretEncryptionKey(keyReference, Cipher.DECRYPT_MODE), this.getAlgorithmParameterSpec());
        final byte[] decryptedData = cipher.doFinal(secret.getSecret());
        if (this.checkNullBytesPrepended(decryptedData)) {
            return Arrays.copyOfRange(decryptedData, BLOCK_SIZE, decryptedData.length);
        } else {
            return decryptedData;
        }
    } catch (final Exception e) {
        throw new EncrypterException("Could not decrypt secret with keyReference " + keyReference, e);
    }
}
Also used : EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) Cipher(javax.crypto.Cipher) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException)

Example 20 with EncrypterException

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

the class Lls1Connector method connect.

@Override
public DlmsConnection connect(final MessageMetadata messageMetadata, final DlmsDevice device, final DlmsMessageListener dlmsMessageListener) throws OsgpException {
    // Make sure neither device or device.getIpAddress() is null.
    this.checkDevice(device);
    this.checkIpAddress(device);
    try {
        return this.createConnection(messageMetadata, device, dlmsMessageListener, this.secretManagementService::getKeys);
    } catch (final UnknownHostException e) {
        LOGGER.warn("The IP address is not found: {}", device.getIpAddress(), e);
        // Unknown IP, unrecoverable.
        throw new TechnicalException(ComponentType.PROTOCOL_DLMS, "The IP address is not found: " + device.getIpAddress());
    } catch (final IOException e) {
        throw new ConnectionException(e);
    } catch (final EncrypterException e) {
        LOGGER.error("decryption of security keys failed for device: {}", device.getDeviceIdentification(), e);
        throw new TechnicalException(ComponentType.PROTOCOL_DLMS, "decryption of security keys failed for device: " + device.getDeviceIdentification());
    }
}
Also used : TechnicalException(org.opensmartgridplatform.shared.exceptionhandling.TechnicalException) UnknownHostException(java.net.UnknownHostException) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) IOException(java.io.IOException) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)

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