Search in sources :

Example 1 with Message

use of com.alliander.osgp.oslp.Oslp.Message in project Protocol-Adapter-OSLP by OSGP.

the class OslpEnvelopeRsaTest method buildOslpMessageIncorrectSignature.

/**
 * Valid must fail when decryption fails using incorrect keys
 *
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws NoSuchProviderException
 */
@Test(expected = IllegalArgumentException.class)
public void buildOslpMessageIncorrectSignature() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    final byte[] deviceId = new byte[] { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
    final byte[] sequenceNumber = new byte[] { 0, 1 };
    final Message message = this.buildRegisterResponse();
    new OslpEnvelope.Builder().withSignature("Incorrect").withProvider(PROVIDER).withPrimaryKey(CertificateHelper.createPrivateKeyFromBase64(PRIVATE_KEY_BASE_64, KEY_TYPE, PROVIDER)).withDeviceId(deviceId).withSequenceNumber(sequenceNumber).withPayloadMessage(message).build();
}
Also used : Message(com.alliander.osgp.oslp.Oslp.Message) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope) Test(org.junit.Test)

Example 2 with Message

use of com.alliander.osgp.oslp.Oslp.Message in project Protocol-Adapter-OSLP by OSGP.

the class OslpEnvelopeRsaTest method buildOslpMessageIncorrectProvider.

/**
 * Valid must fail when decryption fails using incorrect keys
 *
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws NoSuchProviderException
 */
@Test(expected = IllegalArgumentException.class)
public void buildOslpMessageIncorrectProvider() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    final byte[] deviceId = new byte[] { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
    final byte[] sequenceNumber = new byte[] { 0, 1 };
    final Message message = this.buildRegisterResponse();
    new OslpEnvelope.Builder().withSignature(SIGNATURE).withProvider("Incorrect").withPrimaryKey(CertificateHelper.createPrivateKeyFromBase64(PRIVATE_KEY_BASE_64, KEY_TYPE, PROVIDER)).withDeviceId(deviceId).withSequenceNumber(sequenceNumber).withPayloadMessage(message).build();
}
Also used : Message(com.alliander.osgp.oslp.Oslp.Message) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope) Test(org.junit.Test)

Example 3 with Message

use of com.alliander.osgp.oslp.Oslp.Message in project Protocol-Adapter-OSLP by OSGP.

the class OslpChannelHandlerServer method messageReceived.

@Override
public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent e) throws Exception {
    final OslpEnvelope message = (OslpEnvelope) e.getMessage();
    this.logMessage(message, true);
    final Integer channelId = e.getChannel().getId();
    if (message.isValid()) {
        if (this.isOslpResponse(message)) {
            LOGGER.warn("{} Received OSLP Response, which is not expected: {}", channelId, message.getPayloadMessage());
        } else {
            LOGGER.info("{} Received OSLP Request: {}", channelId, message.getPayloadMessage());
            // Response pay-load to send to device.
            Message payload = null;
            // Check which request the device has sent and handle it.
            if (message.getPayloadMessage().hasRegisterDeviceRequest()) {
                payload = this.handleRegisterDeviceRequest(message.getDeviceId(), message.getSequenceNumber(), message.getPayloadMessage().getRegisterDeviceRequest());
            } else if (message.getPayloadMessage().hasConfirmRegisterDeviceRequest()) {
                payload = this.handleConfirmRegisterDeviceRequest(message.getDeviceId(), message.getSequenceNumber(), message.getPayloadMessage().getConfirmRegisterDeviceRequest());
            } else if (message.getPayloadMessage().hasEventNotificationRequest()) {
                payload = this.handleEventNotificationRequest(message.getDeviceId(), message.getSequenceNumber(), message.getPayloadMessage().getEventNotificationRequest());
            } else {
                LOGGER.warn("{} Received unknown payload. Received: {}.", channelId, message.getPayloadMessage().toString());
                // Optional extra: return error code to device.
                return;
            }
            // Cache the channel so we can write the response to it later.
            this.cacheChannel(channelId, e.getChannel());
            // Send message to signing server to get our response signed.
            this.oslpSigningService.buildAndSignEnvelope(message.getDeviceId(), message.getSequenceNumber(), payload, channelId, this);
        }
    } else {
        LOGGER.warn("{} Received message wasn't properly secured.", channelId);
    }
}
Also used : Message(com.alliander.osgp.oslp.Oslp.Message) OslpEnvelope(com.alliander.osgp.oslp.OslpEnvelope)

Example 4 with Message

use of com.alliander.osgp.oslp.Oslp.Message in project Protocol-Adapter-OSLP by OSGP.

the class OslpEnvelopeEcDsaTest method buildOslpMessageIncorrectProvider.

/**
 * Valid must fail when decryption fails using incorrect keys
 *
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws NoSuchProviderException
 */
@Test(expected = IllegalArgumentException.class)
public void buildOslpMessageIncorrectProvider() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    final byte[] deviceId = new byte[] { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
    final byte[] sequenceNumber = new byte[] { 0, 1 };
    final Message message = this.buildRegisterResponse();
    new OslpEnvelope.Builder().withSignature(SIGNATURE).withProvider("Incorrect").withPrimaryKey(CertificateHelper.createPrivateKeyFromBase64(PRIVATE_KEY_BASE_64, KEY_TYPE, this.provider())).withDeviceId(deviceId).withSequenceNumber(sequenceNumber).withPayloadMessage(message).build();
}
Also used : Message(com.alliander.osgp.oslp.Oslp.Message) Test(org.junit.Test)

Example 5 with Message

use of com.alliander.osgp.oslp.Oslp.Message in project Protocol-Adapter-OSLP by OSGP.

the class OslpEnvelopeEcDsaTest method buildOslpMessageIncorrectSignature.

/**
 * Valid must fail when decryption fails using incorrect keys
 *
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws NoSuchProviderException
 */
@Test(expected = IllegalArgumentException.class)
public void buildOslpMessageIncorrectSignature() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    final byte[] deviceId = new byte[] { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
    final byte[] sequenceNumber = new byte[] { 0, 1 };
    final Message message = this.buildRegisterResponse();
    new OslpEnvelope.Builder().withSignature("Incorrect").withProvider(this.provider()).withPrimaryKey(CertificateHelper.createPrivateKeyFromBase64(PRIVATE_KEY_BASE_64, KEY_TYPE, this.provider())).withDeviceId(deviceId).withSequenceNumber(sequenceNumber).withPayloadMessage(message).build();
}
Also used : Message(com.alliander.osgp.oslp.Oslp.Message) Test(org.junit.Test)

Aggregations

Message (com.alliander.osgp.oslp.Oslp.Message)7 OslpEnvelope (com.alliander.osgp.oslp.OslpEnvelope)4 Test (org.junit.Test)4 Oslp (com.alliander.osgp.oslp.Oslp)1 SignedOslpEnvelopeDto (com.alliander.osgp.oslp.SignedOslpEnvelopeDto)1 OsgpException (com.alliander.osgp.shared.exceptionhandling.OsgpException)1 ResponseMessage (com.alliander.osgp.shared.infra.jms.ResponseMessage)1 Device (com.alliander.osgp.webdevicesimulator.domain.entities.Device)1 DeviceSimulatorException (com.alliander.osgp.webdevicesimulator.exceptions.DeviceSimulatorException)1 ByteString (com.google.protobuf.ByteString)1