Search in sources :

Example 36 with OslpEnvelope

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)));
}
Also used : OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope) Test(org.junit.Test)

Example 37 with OslpEnvelope

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)));
}
Also used : OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Example 38 with OslpEnvelope

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);
}
Also used : OsgpException(com.alliander.osgp.shared.exceptionhandling.OsgpException) Message(com.alliander.osgp.oslp.Oslp.Message) ResponseMessage(com.alliander.osgp.shared.infra.jms.ResponseMessage) SignedOslpEnvelopeDto(com.alliander.osgp.oslp.SignedOslpEnvelopeDto) ResponseMessage(com.alliander.osgp.shared.infra.jms.ResponseMessage) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Example 39 with OslpEnvelope

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);
}
Also used : OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Example 40 with OslpEnvelope

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) Device(com.alliander.osgp.webdevicesimulator.domain.entities.Device) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DeviceSimulatorException(com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Aggregations

OslpEnvelope (com.alliander.osgp.oslp.OslpEnvelope)40 IOException (java.io.IOException)25 UnsignedOslpEnvelopeDto (com.alliander.osgp.oslp.UnsignedOslpEnvelopeDto)23 DeviceResponseHandler (com.alliander.osgp.adapter.protocol.oslp.elster.device.DeviceResponseHandler)21 DeviceResponse (com.alliander.osgp.adapter.protocol.oslp.elster.device.DeviceResponse)20 DeviceRequest (com.alliander.osgp.adapter.protocol.oslp.elster.device.DeviceRequest)15 GetStatusDeviceRequest (com.alliander.osgp.adapter.protocol.oslp.elster.device.requests.GetStatusDeviceRequest)4 DeviceSimulatorException (com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException)4 GetStatusDeviceResponse (com.alliander.osgp.adapter.protocol.oslp.elster.device.responses.GetStatusDeviceResponse)3 Device (com.alliander.osgp.webdevicesimulator.domain.entities.Device)3 UnknownHostException (java.net.UnknownHostException)3 SetScheduleDeviceRequest (com.alliander.osgp.adapter.protocol.oslp.elster.device.requests.SetScheduleDeviceRequest)2 ScheduleMessageDataContainerDto (com.alliander.osgp.dto.valueobjects.ScheduleMessageDataContainerDto)2 Message (com.alliander.osgp.oslp.Oslp.Message)2 GetPowerUsageHistoryDeviceRequest (com.alliander.osgp.adapter.protocol.oslp.elster.device.requests.GetPowerUsageHistoryDeviceRequest)1 ResumeScheduleDeviceRequest (com.alliander.osgp.adapter.protocol.oslp.elster.device.requests.ResumeScheduleDeviceRequest)1 SetConfigurationDeviceRequest (com.alliander.osgp.adapter.protocol.oslp.elster.device.requests.SetConfigurationDeviceRequest)1 SetDeviceVerificationKeyDeviceRequest (com.alliander.osgp.adapter.protocol.oslp.elster.device.requests.SetDeviceVerificationKeyDeviceRequest)1 SetEventNotificationsDeviceRequest (com.alliander.osgp.adapter.protocol.oslp.elster.device.requests.SetEventNotificationsDeviceRequest)1 SetLightDeviceRequest (com.alliander.osgp.adapter.protocol.oslp.elster.device.requests.SetLightDeviceRequest)1