Search in sources :

Example 6 with EncrypterException

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

the class Hls5Connector 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) {
        // Unknown IP, unrecoverable.
        LOGGER.error("The IP address is not found: {}", device.getIpAddress(), e);
        throw new TechnicalException(ComponentType.PROTOCOL_DLMS, "The IP address is not found: " + device.getIpAddress());
    } catch (final IOException e) {
        // Queue key recovery process
        if (this.secretManagementService.hasNewSecret(messageMetadata, device.getDeviceIdentification())) {
            this.recoverKeyProcessInitiator.initiate(messageMetadata, device.getDeviceIdentification());
        }
        final String msg = String.format("Error creating connection for device %s with Ip address:%s Port:%d UseHdlc:%b UseSn:%b " + "Message:%s", device.getDeviceIdentification(), device.getIpAddress(), device.getPort(), device.isUseHdlc(), device.isUseSn(), e.getMessage());
        LOGGER.error(msg);
        throw new ConnectionException(msg, e);
    } catch (final EncrypterException e) {
        throw new FunctionalException(FunctionalExceptionType.INVALID_DLMS_KEY_FORMAT, ComponentType.PROTOCOL_DLMS, e);
    }
}
Also used : TechnicalException(org.opensmartgridplatform.shared.exceptionhandling.TechnicalException) UnknownHostException(java.net.UnknownHostException) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) IOException(java.io.IOException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)

Example 7 with EncrypterException

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

the class SetEncryptionKeyExchangeOnGMeterCommandExecutor method execute.

@Override
public MethodResultCode execute(final DlmsConnectionManager conn, final DlmsDevice device, final GMeterInfoDto gMeterInfo, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
    try {
        LOGGER.debug("SetEncryptionKeyExchangeOnGMeterCommandExecutor.execute called");
        final String mbusDeviceIdentification = gMeterInfo.getDeviceIdentification();
        final int channel = gMeterInfo.getChannel();
        final ObisCode obisCode = OBIS_HASHMAP.get(channel);
        final byte[] gMeterEncryptionKey = this.secretManagementService.generate128BitsKeyAndStoreAsNewKey(messageMetadata, mbusDeviceIdentification, G_METER_ENCRYPTION);
        MethodResult methodResultCode = this.transferKey(conn, mbusDeviceIdentification, channel, gMeterEncryptionKey, messageMetadata);
        this.checkMethodResultCode(methodResultCode, "M-Bus Setup transfer_key", obisCode);
        methodResultCode = this.setEncryptionKey(conn, channel, gMeterEncryptionKey);
        this.checkMethodResultCode(methodResultCode, "M-Bus Setup set_encryption_key", obisCode);
        this.secretManagementService.activateNewKey(messageMetadata, mbusDeviceIdentification, G_METER_ENCRYPTION);
        return MethodResultCode.SUCCESS;
    } catch (final IOException e) {
        throw new ConnectionException(e);
    } catch (final EncrypterException e) {
        throw new ProtocolAdapterException("Unexpected exception during decryption of security keys, reason = " + e.getMessage(), e);
    }
}
Also used : EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) ObisCode(org.openmuc.jdlms.ObisCode) IOException(java.io.IOException) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException) MethodResult(org.openmuc.jdlms.MethodResult)

Example 8 with EncrypterException

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

the class Lls1Connector method setSecurity.

@Override
protected void setSecurity(final MessageMetadata messageMetadata, final DlmsDevice device, final SecurityKeyProvider keyProvider, final TcpConnectionBuilder tcpConnectionBuilder) throws OsgpException {
    final byte[] password;
    try {
        password = keyProvider.getKeys(messageMetadata, device.getDeviceIdentification(), Collections.singletonList(SecurityKeyType.PASSWORD)).get(SecurityKeyType.PASSWORD);
    } catch (final EncrypterException e) {
        LOGGER.error("Error determining DLMS password setting up LLS1 connection", e);
        throw new FunctionalException(FunctionalExceptionType.INVALID_DLMS_KEY_ENCRYPTION, ComponentType.PROTOCOL_DLMS);
    }
    if (password == null) {
        LOGGER.error("There is no password available for device {}", device.getDeviceIdentification());
        throw new FunctionalException(FunctionalExceptionType.INVALID_DLMS_KEY_ENCRYPTION, ComponentType.PROTOCOL_DLMS);
    }
    final SecuritySuite securitySuite = SecuritySuite.builder().setAuthenticationMechanism(AuthenticationMechanism.LOW).setPassword(password).build();
    tcpConnectionBuilder.setSecuritySuite(securitySuite).setClientId(this.clientId);
}
Also used : SecuritySuite(org.openmuc.jdlms.SecuritySuite) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) FunctionalException(org.opensmartgridplatform.shared.exceptionhandling.FunctionalException)

Example 9 with EncrypterException

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

the class AbstractReplaceKeyCommandExecutor method sendToDevice.

/**
 * Send the key to the device.
 *
 * @param conn jDLMS connection.
 * @param deviceIdentification Device identification
 * @param keyWrapper Key data
 * @param messageMetadata
 */
private void sendToDevice(final DlmsConnectionManager conn, final String deviceIdentification, final ReplaceKeyInput keyWrapper, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
    try {
        final byte[] decryptedKey = keyWrapper.getBytes();
        final byte[] decryptedMasterKey = this.secretManagementService.getKey(messageMetadata, deviceIdentification, SecurityKeyType.E_METER_MASTER);
        final MethodParameter methodParameterAuth = SecurityUtils.keyChangeMethodParamFor(decryptedMasterKey, decryptedKey, keyWrapper.getKeyId());
        conn.getDlmsMessageListener().setDescription("ReplaceKey for " + keyWrapper.getSecurityKeyType() + " " + keyWrapper.getKeyId() + ", call method: " + JdlmsObjectToStringUtil.describeMethod(methodParameterAuth));
        final MethodResultCode methodResultCode = conn.getConnection().action(methodParameterAuth).getResultCode();
        if (!MethodResultCode.SUCCESS.equals(methodResultCode)) {
            throw new ProtocolAdapterException("AccessResultCode for replace keys was not SUCCESS: " + methodResultCode);
        }
        if (keyWrapper.getSecurityKeyType() == SecurityKeyType.E_METER_AUTHENTICATION) {
            conn.getConnection().changeClientGlobalAuthenticationKey(decryptedKey);
        } else if (keyWrapper.getSecurityKeyType() == SecurityKeyType.E_METER_ENCRYPTION) {
            conn.getConnection().changeClientGlobalEncryptionKey(decryptedKey);
        }
    } catch (final IOException e) {
        throw new ConnectionException(e);
    } catch (final EncrypterException e) {
        throw new ProtocolAdapterException("Unexpected exception during decryption of security keys, reason = " + e.getMessage(), e);
    }
}
Also used : MethodResultCode(org.openmuc.jdlms.MethodResultCode) EncrypterException(org.opensmartgridplatform.shared.exceptionhandling.EncrypterException) ProtocolAdapterException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException) IOException(java.io.IOException) MethodParameter(org.openmuc.jdlms.MethodParameter) ConnectionException(org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ConnectionException)

Example 10 with EncrypterException

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

the class GenerateAndReplaceKeyCommandExecutor method generateSetKeysRequest.

private SetKeysRequestDto generateSetKeysRequest(final MessageMetadata messageMetadata, final String deviceIdentification) throws FunctionalException {
    try {
        final List<SecurityKeyType> keyTypes = Arrays.asList(E_METER_AUTHENTICATION, E_METER_ENCRYPTION);
        final Map<SecurityKeyType, byte[]> generatedKeys = this.secretManagementService.generate128BitsKeysAndStoreAsNewKeys(messageMetadata, deviceIdentification, keyTypes);
        final SetKeysRequestDto setKeysRequest = new SetKeysRequestDto(generatedKeys.get(E_METER_AUTHENTICATION), generatedKeys.get(E_METER_ENCRYPTION));
        setKeysRequest.setGeneratedKeys(true);
        return setKeysRequest;
    } catch (final EncrypterException e) {
        throw new FunctionalException(FunctionalExceptionType.ENCRYPTION_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) SecurityKeyType(org.opensmartgridplatform.adapter.protocol.dlms.domain.entities.SecurityKeyType)

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