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);
}
}
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);
}
}
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);
}
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);
}
}
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);
}
}
Aggregations