use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.
the class ProvisioningSymmetricKeyEnrollmentGroupSample method main.
public static void main(String[] args) throws Exception {
System.out.println("Starting...");
System.out.println("Beginning setup.");
SecurityProviderSymmetricKey securityClientSymmetricKey;
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name());
DeviceClient deviceClient = null;
// For the sake of security, you shouldn't save keys into String variables as that places them in heap memory. For the sake
// of simplicity within this sample, though, we will save it as a string. Typically this key would be loaded as byte[] so that
// it can be removed from stack memory.
byte[] derivedSymmetricKey = DERIVED_ENROLLMENT_GROUP_SYMMETRIC_KEY.getBytes(StandardCharsets.UTF_8);
securityClientSymmetricKey = new SecurityProviderSymmetricKey(derivedSymmetricKey, PROVISIONED_DEVICE_ID);
ProvisioningDeviceClient provisioningDeviceClient = null;
try {
ProvisioningStatus provisioningStatus = new ProvisioningStatus();
provisioningDeviceClient = ProvisioningDeviceClient.create(GLOBAL_ENDPOINT, SCOPE_ID, PROVISIONING_DEVICE_CLIENT_TRANSPORT_PROTOCOL, securityClientSymmetricKey);
provisioningDeviceClient.registerDevice(new ProvisioningDeviceClientRegistrationCallbackImpl(), provisioningStatus);
while (provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getProvisioningDeviceClientStatus() != ProvisioningDeviceClientStatus.PROVISIONING_DEVICE_STATUS_ASSIGNED) {
if (provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getProvisioningDeviceClientStatus() == ProvisioningDeviceClientStatus.PROVISIONING_DEVICE_STATUS_ERROR || provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getProvisioningDeviceClientStatus() == ProvisioningDeviceClientStatus.PROVISIONING_DEVICE_STATUS_DISABLED || provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getProvisioningDeviceClientStatus() == ProvisioningDeviceClientStatus.PROVISIONING_DEVICE_STATUS_FAILED) {
provisioningStatus.exception.printStackTrace();
System.out.println("Registration error, bailing out");
break;
}
System.out.println("Waiting for Provisioning Service to register");
Thread.sleep(MAX_TIME_TO_WAIT_FOR_REGISTRATION);
}
if (provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getProvisioningDeviceClientStatus() == ProvisioningDeviceClientStatus.PROVISIONING_DEVICE_STATUS_ASSIGNED) {
System.out.println("IotHUb Uri : " + provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getIothubUri());
System.out.println("Device ID : " + provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getDeviceId());
// connect to iothub
String iotHubUri = provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getIothubUri();
String deviceId = provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getDeviceId();
try {
deviceClient = DeviceClient.createFromSecurityProvider(iotHubUri, deviceId, securityClientSymmetricKey, IotHubClientProtocol.MQTT);
deviceClient.open();
Message messageToSendFromDeviceToHub = new Message("Whatever message you would like to send");
System.out.println("Sending message from device to IoT Hub...");
deviceClient.sendEventAsync(messageToSendFromDeviceToHub, new IotHubEventCallbackImpl(), null);
} catch (IOException e) {
System.out.println("Device client threw an exception: " + e.getMessage());
if (deviceClient != null) {
deviceClient.closeNow();
}
}
}
} catch (ProvisioningDeviceClientException | InterruptedException e) {
System.out.println("Provisioning Device Client threw an exception" + e.getMessage());
if (provisioningDeviceClient != null) {
provisioningDeviceClient.closeNow();
}
}
System.out.println("Press any key to exit...");
scanner.nextLine();
if (provisioningDeviceClient != null) {
provisioningDeviceClient.closeNow();
}
if (deviceClient != null) {
deviceClient.closeNow();
}
System.out.println("Shutting down...");
}
use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.
the class SecurityProviderSymmetricKeyTest method testSignDataThrowsSecurityProviderExceptionOnEmptyKey.
@Test(expected = SecurityProviderException.class)
public void testSignDataThrowsSecurityProviderExceptionOnEmptyKey() throws SecurityProviderException {
final String TEST_SIGNATURE = "testSignature";
final String TEST_BASE64_DECODED_KEY = "";
final String HMAC_SHA_256 = "HmacSHA256";
// arrange
SecurityProviderSymmetricKey securityProviderSymmetricKey = new SecurityProviderSymmetricKey(testSymKey, testRegId);
// act
securityProviderSymmetricKey.HMACSignData(TEST_SIGNATURE.getBytes(StandardCharsets.UTF_8), "".getBytes(StandardCharsets.UTF_8));
// assert
new Verifications() {
{
new SecretKeySpec((byte[]) any, HMAC_SHA_256);
times = 1;
mockedMac.doFinal(TEST_SIGNATURE.getBytes(StandardCharsets.UTF_8));
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.
the class SecurityProviderSymmetricKeyTest method testConstructorWithBothKeysSucceeds.
@Test
public void testConstructorWithBothKeysSucceeds() throws SecurityProviderException {
// act
SecurityProviderSymmetricKey securityProviderSymmetricKey = new SecurityProviderSymmetricKey(testPrimaryKey, testSecondaryKey, testRegId);
// assert
assertEquals(testPrimaryKey, new String(securityProviderSymmetricKey.getSymmetricKey(), StandardCharsets.UTF_8));
assertEquals(testSecondaryKey, new String(securityProviderSymmetricKey.getSecondaryKey(), StandardCharsets.UTF_8));
assertEquals(testRegId, securityProviderSymmetricKey.getRegistrationId());
}
use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.
the class SecurityProviderSymmetricKeyTest method testSignDataThrowsSecurityProviderExceptionOnNullKey.
@Test(expected = SecurityProviderException.class)
public void testSignDataThrowsSecurityProviderExceptionOnNullKey() throws SecurityProviderException {
final String TEST_SIGNATURE = "testSignature";
final String TEST_BASE64_DECODED_KEY = "";
final String HMAC_SHA_256 = "HmacSHA256";
// arrange
SecurityProviderSymmetricKey securityProviderSymmetricKey = new SecurityProviderSymmetricKey(testSymKey, testRegId);
// act
securityProviderSymmetricKey.HMACSignData(TEST_SIGNATURE.getBytes(StandardCharsets.UTF_8), null);
// assert
new Verifications() {
{
new SecretKeySpec((byte[]) any, HMAC_SHA_256);
times = 1;
mockedMac.doFinal(TEST_SIGNATURE.getBytes(StandardCharsets.UTF_8));
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.
the class SecurityProviderSymmetricKeyTest method getSSLContextSucceeds.
@Test
public void getSSLContextSucceeds() throws SecurityProviderException, KeyManagementException, KeyStoreException, CertificateException {
// arrange
SecurityProviderSymmetricKey securityProviderSymmetricKey = new SecurityProviderSymmetricKey(testSymKey, testRegId);
// act
securityProviderSymmetricKey.getSSLContext();
new Verifications() {
{
mockedKeyStore.setCertificateEntry(anyString, (Certificate) any);
times = 4;
mockedSslContext.init((KeyManager[]) any, (TrustManager[]) any, (SecureRandom) any);
times = 1;
}
};
}
Aggregations