use of com.alliander.osgp.oslp.OslpEnvelope in project Protocol-Adapter-OSLP by OSGP.
the class OslpEnvelopeRsaTest method buildOslpMessageSignatureFailure.
/**
* Valid must fail when message is changed and hash does not match
*
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
* @throws NoSuchProviderException
*/
@Test
public void buildOslpMessageSignatureFailure() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
final OslpEnvelope request = this.buildMessage();
final byte[] fakeDeviceId = new byte[] { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9 };
// Validate security key is set in request
final byte[] securityKey = request.getSecurityKey();
assertTrue(securityKey.length == OslpEnvelope.SECURITY_KEY_LENGTH);
assertFalse(ArrayUtils.isEmpty(securityKey));
// Verify the message using public certificate
final OslpEnvelope response = new OslpEnvelope.Builder().withSignature(SIGNATURE).withProvider(PROVIDER).withSecurityKey(request.getSecurityKey()).withDeviceId(fakeDeviceId).withSequenceNumber(request.getSequenceNumber()).withPayloadMessage(request.getPayloadMessage()).build();
assertFalse(response.validate(CertificateHelper.createPublicKeyFromBase64(PUBLIC_KEY_BASE_64, KEY_TYPE, PROVIDER)));
}
use of com.alliander.osgp.oslp.OslpEnvelope in project Protocol-Adapter-OSLP by OSGP.
the class OslpEnvelopeRsaTest method buildOslpMessageDecryptFailure.
private void buildOslpMessageDecryptFailure(final String provider) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, Exception {
final OslpEnvelope request = this.buildMessage();
// Verify the message using wrong public certificate
final OslpEnvelope response = new OslpEnvelope.Builder().withSignature(SIGNATURE).withProvider(PROVIDER).withSecurityKey(request.getSecurityKey()).withDeviceId(request.getDeviceId()).withSequenceNumber(request.getSequenceNumber()).withPayloadMessage(request.getPayloadMessage()).build();
assertFalse(response.validate(CertificateHelper.createPublicKeyFromBase64(DEVIATING_PUBLIC_KEY_BASE_64, KEY_TYPE, PROVIDER)));
}
use of com.alliander.osgp.oslp.OslpEnvelope in project Protocol-Adapter-OSLP by OSGP.
the class SigningService method doSignMessage.
private void doSignMessage(final UnsignedOslpEnvelopeDto unsignedOslpEnvelopeDto, final String correlationUid, final String deviceIdentification, final Destination replyToQueue) {
final byte[] deviceId = unsignedOslpEnvelopeDto.getDeviceId();
final byte[] sequenceNumber = unsignedOslpEnvelopeDto.getSequenceNumber();
final Message payloadMessage = unsignedOslpEnvelopeDto.getPayloadMessage();
final String organisationIdentification = unsignedOslpEnvelopeDto.getOrganisationIdentification();
final OslpEnvelope oslpEnvelope = new OslpEnvelope.Builder().withDeviceId(deviceId).withSequenceNumber(sequenceNumber).withPrimaryKey(this.privateKey).withSignature(this.signature).withProvider(this.signatureProvider).withPayloadMessage(payloadMessage).build();
ResponseMessage responseMessage = null;
if (oslpEnvelope == null) {
LOGGER.error("Message for device: {} with correlationId: {} NOT SIGNED, sending error to protocol-adpater", deviceIdentification, correlationUid);
responseMessage = ResponseMessage.newResponseMessageBuilder().withCorrelationUid(correlationUid).withOrganisationIdentification(organisationIdentification).withDeviceIdentification(deviceIdentification).withResult(ResponseMessageResultType.NOT_OK).withOsgpException(new OsgpException(ComponentType.UNKNOWN, "Failed to build signed OslpEnvelope", null)).withDataObject(unsignedOslpEnvelopeDto).build();
} else {
LOGGER.info("Message for device: {} with correlationId: {} signed, sending response to protocol-adapter", deviceIdentification, correlationUid);
final SignedOslpEnvelopeDto signedOslpEnvelopeDto = new SignedOslpEnvelopeDto(oslpEnvelope, unsignedOslpEnvelopeDto);
responseMessage = ResponseMessage.newResponseMessageBuilder().withCorrelationUid(correlationUid).withOrganisationIdentification(organisationIdentification).withDeviceIdentification(deviceIdentification).withResult(ResponseMessageResultType.OK).withDataObject(signedOslpEnvelopeDto).build();
}
this.signingServerResponseMessageSender.send(responseMessage, "SIGNING_RESPONSE", replyToQueue);
}
use of com.alliander.osgp.oslp.OslpEnvelope in project Protocol-Adapter-OSLP by OSGP.
the class OslpSecurityHandler method messageReceived.
@Override
public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent evt) throws Exception {
final OslpEnvelope message = (OslpEnvelope) evt.getMessage();
message.validate(this.publicKey);
ctx.sendUpstream(evt);
}
use of com.alliander.osgp.oslp.OslpEnvelope in project Protocol-Adapter-OSLP by OSGP.
the class RegisterDevice method sendRegisterDeviceCommand.
public DeviceMessageStatus sendRegisterDeviceCommand(final long deviceId, final Boolean hasSchedule) {
// Find device.
Device device = this.deviceManagementService.findDevice(deviceId);
if (device == null) {
// Set the DeviceMessageStatus NOT_FOUND as the Device is not found.
return DeviceMessageStatus.NOT_FOUND;
}
this.errorMessage = "";
try {
// Create new deviceUID. This is a temporary fix for devices that
// have been created in the past (with a 10 byte deviceUID).
// Alternative would be to 1) change the deviceUID in the database
// or 2) delete all devices and create new devices (with a 12 byte
// deviceUID).
// There seems no problem with creating a new deviceUID for every
// registration attempt of the device.
// However, NOTE: THIS BEHAVIOUR IS NOT EQUAL TO THE REAL SSLD/PSLD.
device.setDeviceUid(this.createRandomDeviceUid());
device = this.deviceManagementService.updateDevice(device);
// Generate random sequence number and random device number.
final Integer sequenceNumber = device.doGenerateRandomNumber();
final Integer randomDevice = device.doGenerateRandomNumber();
// Create registration message.
final OslpEnvelope oslpRequest = this.createEnvelopeBuilder(device.getDeviceUid(), sequenceNumber).withPayloadMessage(Message.newBuilder().setRegisterDeviceRequest(Oslp.RegisterDeviceRequest.newBuilder().setDeviceIdentification(device.getDeviceIdentification()).setIpAddress(ByteString.copyFrom(InetAddress.getByName(device.getIpAddress()).getAddress())).setDeviceType(device.getDeviceType().isEmpty() ? DeviceType.PSLD : DeviceType.valueOf(device.getDeviceType())).setHasSchedule(hasSchedule).setRandomDevice(randomDevice)).build()).build();
// Write outgoing request to log.
this.writeOslpLogItem(oslpRequest, device, false);
final OslpEnvelope response = this.sendRequest(device, oslpRequest);
// Write incoming response to log.
this.writeOslpLogItem(response, device, true);
this.currentTime = response.getPayloadMessage().getRegisterDeviceResponse().getCurrentTime();
// Get the sequence number from the response envelope and check it.
this.checkSequenceNumber(response.getSequenceNumber(), sequenceNumber);
// Get the two random numbers and check them both.
this.checkRandomDeviceAndRandomPlatform(randomDevice, response.getPayloadMessage().getRegisterDeviceResponse().getRandomDevice(), response.getPayloadMessage().getRegisterDeviceResponse().getRandomPlatform());
// Set the sequence number and persist it.
device.setSequenceNumber(sequenceNumber);
// Get the two random numbers and persist them both.
device.setRandomDevice(response.getPayloadMessage().getRegisterDeviceResponse().getRandomDevice());
device.setRandomPlatform(response.getPayloadMessage().getRegisterDeviceResponse().getRandomPlatform());
// Save the entity.
device = this.deviceManagementService.updateDevice(device);
// Set the DeviceMessageStatus OK as the registration is successful.
return DeviceMessageStatus.OK;
} catch (final UnknownHostException ex) {
LOGGER.error("incorrect IP address format", ex);
} catch (final Exception e) {
LOGGER.error("register device exception", e);
this.errorMessage = e.getMessage();
// successful.
return DeviceMessageStatus.FAILURE;
}
return DeviceMessageStatus.NOT_FOUND;
}
Aggregations