From 37969ba0a0b355f4b137f392928fc9af25f700ec Mon Sep 17 00:00:00 2001 From: ayyoob Date: Sat, 7 Jan 2017 14:54:46 +0530 Subject: [PATCH] removed scep impl from virtual fire alarm --- .../mqtt/FireAlarmMQTTCommunicator.java | 5 +- .../xmpp/FireAlarmXMPPCommunicator.java | 6 +- .../agent/core/AgentConstants.java | 2 +- .../agent/core/AgentManager.java | 18 ++-- .../agent/core/AgentUtilOperations.java | 90 ++++++++++--------- .../agent/enrollment/EnrollmentManager.java | 2 +- 6 files changed, 68 insertions(+), 55 deletions(-) diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/mqtt/FireAlarmMQTTCommunicator.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/mqtt/FireAlarmMQTTCommunicator.java index 5b24783e7..8f89750fd 100644 --- a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/mqtt/FireAlarmMQTTCommunicator.java +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/mqtt/FireAlarmMQTTCommunicator.java @@ -208,7 +208,10 @@ public class FireAlarmMQTTCommunicator extends MQTTTransportHandler { @Override public void run() { int currentTemperature = agentManager.getTemperature(); - String message = "PUBLISHER:" + AgentConstants.TEMPERATURE_CONTROL + ":" + currentTemperature; + String message = "{\"event\": {\"metaData\": {\"owner\": \"" + AgentManager + .getInstance().getAgentConfigs().getDeviceOwner() + "\",\"deviceId\": \"" + AgentManager + .getInstance().getAgentConfigs().getDeviceId() + "\",\"time\": " + + "0},\"payloadData\": { \"temperature\": " + currentTemperature + "} }}"; try { String payLoad = AgentUtilOperations.prepareSecurePayLoad(message); diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/xmpp/FireAlarmXMPPCommunicator.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/xmpp/FireAlarmXMPPCommunicator.java index abb8d7965..0780f2d84 100644 --- a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/xmpp/FireAlarmXMPPCommunicator.java +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/communication/xmpp/FireAlarmXMPPCommunicator.java @@ -189,8 +189,10 @@ public class FireAlarmXMPPCommunicator extends XMPPTransportHandler { try { int currentTemperature = agentManager.getTemperature(); - - String message = AgentConstants.TEMPERATURE_CONTROL + ":" + currentTemperature; + String message = "{\"event\": {\"metaData\": {\"owner\": \"" + AgentManager + .getInstance().getAgentConfigs().getDeviceOwner() + "\",\"deviceId\": \"" + AgentManager + .getInstance().getAgentConfigs().getDeviceId() + "\",\"time\": " + + "0},\"payloadData\": { \"temperature\": " + currentTemperature + "} }}"; String payLoad = AgentUtilOperations.prepareSecurePayLoad(message); xmppMessage.setTo(xmppAdminJID); diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentConstants.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentConstants.java index 566e5cfc0..f1372b25a 100644 --- a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentConstants.java +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentConstants.java @@ -62,7 +62,7 @@ public class AgentConstants { public static final int DEFAULT_MQTT_RECONNECTION_INTERVAL = 2; // time in seconds public static final int DEFAULT_MQTT_QUALITY_OF_SERVICE = 0; public static final String MQTT_SUBSCRIBE_TOPIC = "%s/" + DEVICE_TYPE + "/%s"; - public static final String MQTT_PUBLISH_TOPIC = "%s/" + DEVICE_TYPE + "/%s/publisher"; + public static final String MQTT_PUBLISH_TOPIC = "%s/" + DEVICE_TYPE + "/%s/temperature"; /* --------------------------------------------------------------------------------------- Device/Agent specific properties to be read from the 'deviceConfig.properties' file diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentManager.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentManager.java index 2e7634054..6ee95dceb 100644 --- a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentManager.java +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentManager.java @@ -153,15 +153,15 @@ public class AgentManager { } } - try { - if (!EnrollmentManager.getInstance().isEnrolled()) { - EnrollmentManager.getInstance().beginEnrollmentFlow(); - } - } catch (AgentCoreOperationException e) { - log.error("Device Enrollment Failed:\n"); - log.error(e); - System.exit(0); - } +// try { +// if (!EnrollmentManager.getInstance().isEnrolled()) { +// EnrollmentManager.getInstance().beginEnrollmentFlow(); +// } +// } catch (AgentCoreOperationException e) { +// log.error("Device Enrollment Failed:\n"); +// log.error(e); +// System.exit(0); +// } //Start agent communication agentCommunicator.get(protocol).connect(); diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentUtilOperations.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentUtilOperations.java index 60c3941b8..7a365174b 100644 --- a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentUtilOperations.java +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/core/AgentUtilOperations.java @@ -250,57 +250,65 @@ public class AgentUtilOperations { } public static String prepareSecurePayLoad(String message) throws AgentCoreOperationException { - PrivateKey devicePrivateKey = EnrollmentManager.getInstance().getPrivateKey(); - String encodedMessage = Base64.encodeBase64String(message.getBytes()); - String signedPayload; - try { - signedPayload = CommunicationUtils.signMessage(encodedMessage, devicePrivateKey); - } catch (TransportHandlerException e) { - String errorMsg = "Error occurred whilst trying to sign encrypted message of: [" + message + "]"; - log.error(errorMsg); - throw new AgentCoreOperationException(errorMsg, e); - } + if (EnrollmentManager.getInstance().isEnrolled()) { + PrivateKey devicePrivateKey = EnrollmentManager.getInstance().getPrivateKey(); + String encodedMessage = Base64.encodeBase64String(message.getBytes()); + String signedPayload; + try { + signedPayload = CommunicationUtils.signMessage(encodedMessage, devicePrivateKey); + } catch (TransportHandlerException e) { + String errorMsg = "Error occurred whilst trying to sign encrypted message of: [" + message + "]"; + log.error(errorMsg); + throw new AgentCoreOperationException(errorMsg, e); + } - JSONObject jsonPayload = new JSONObject(); - jsonPayload.put(JSON_MESSAGE_KEY, encodedMessage); - jsonPayload.put(JSON_SIGNATURE_KEY, signedPayload); - //below statements are temporary fix. - jsonPayload.put(JSON_SERIAL_KEY, EnrollmentManager.getInstance().getSCEPCertificate().getSerialNumber()); - return jsonPayload.toString(); + JSONObject jsonPayload = new JSONObject(); + jsonPayload.put(JSON_MESSAGE_KEY, encodedMessage); + jsonPayload.put(JSON_SIGNATURE_KEY, signedPayload); + //below statements are temporary fix. + jsonPayload.put(JSON_SERIAL_KEY, EnrollmentManager.getInstance().getSCEPCertificate().getSerialNumber()); + return jsonPayload.toString(); + } else { + return message; + } } public static String extractMessageFromPayload(String message) throws AgentCoreOperationException { - String actualMessage; + if (EnrollmentManager.getInstance().isEnrolled()) { + String actualMessage; - PublicKey serverPublicKey = EnrollmentManager.getInstance().getServerPublicKey(); - JSONObject jsonPayload = new JSONObject(message); - Object encodedMessage = jsonPayload.get(JSON_MESSAGE_KEY); - Object signedPayload = jsonPayload.get(JSON_SIGNATURE_KEY); - boolean verification; + PublicKey serverPublicKey = EnrollmentManager.getInstance().getServerPublicKey(); + JSONObject jsonPayload = new JSONObject(message); + Object encodedMessage = jsonPayload.get(JSON_MESSAGE_KEY); + Object signedPayload = jsonPayload.get(JSON_SIGNATURE_KEY); + boolean verification; - if (encodedMessage != null && signedPayload != null) { - try { - verification = CommunicationUtils.verifySignature( - encodedMessage.toString(), signedPayload.toString(), serverPublicKey); - } catch (TransportHandlerException e) { - String errorMsg = - "Error occurred whilst trying to verify signature on received message: [" + message + "]"; + if (encodedMessage != null && signedPayload != null) { + try { + verification = CommunicationUtils.verifySignature( + encodedMessage.toString(), signedPayload.toString(), serverPublicKey); + } catch (TransportHandlerException e) { + String errorMsg = + "Error occurred whilst trying to verify signature on received message: [" + message + "]"; + log.error(errorMsg); + throw new AgentCoreOperationException(errorMsg, e); + } + } else { + String errorMsg = "The received message is in an INVALID format. " + + "Need to be JSON - {\"Msg\":\"\", \"Sig\":\"\"}."; + throw new AgentCoreOperationException(errorMsg); + } + if (verification) { + actualMessage = new String(Base64.decodeBase64(encodedMessage.toString()), StandardCharsets.UTF_8); + } else { + String errorMsg = "Could not verify payload signature. The message was not signed by a valid client"; log.error(errorMsg); - throw new AgentCoreOperationException(errorMsg, e); + throw new AgentCoreOperationException(errorMsg); } + return actualMessage; } else { - String errorMsg = "The received message is in an INVALID format. " + - "Need to be JSON - {\"Msg\":\"\", \"Sig\":\"\"}."; - throw new AgentCoreOperationException(errorMsg); - } - if (verification) { - actualMessage = new String(Base64.decodeBase64(encodedMessage.toString()), StandardCharsets.UTF_8); - } else { - String errorMsg = "Could not verify payload signature. The message was not signed by a valid client"; - log.error(errorMsg); - throw new AgentCoreOperationException(errorMsg); + return message; } - return actualMessage; } public static String getAuthenticationMethod() { diff --git a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/enrollment/EnrollmentManager.java b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/enrollment/EnrollmentManager.java index 6059483bc..deaa5ac24 100644 --- a/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/enrollment/EnrollmentManager.java +++ b/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/agent/enrollment/EnrollmentManager.java @@ -111,7 +111,7 @@ public class EnrollmentManager { */ private EnrollmentManager() { this.SCEPUrl = AgentManager.getInstance().getEnrollmentEP(); - setEnrollmentStatus(); + //setEnrollmentStatus(); } /**