Search in sources :

Example 11 with SecurityProviderSymmetricKey

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...");
}
Also used : Scanner(java.util.Scanner) SecurityProviderSymmetricKey(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey) IOException(java.io.IOException) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)

Example 12 with SecurityProviderSymmetricKey

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;
        }
    };
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecurityProviderSymmetricKey(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey) Test(org.junit.Test)

Example 13 with SecurityProviderSymmetricKey

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());
}
Also used : SecurityProviderSymmetricKey(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey) Test(org.junit.Test)

Example 14 with SecurityProviderSymmetricKey

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;
        }
    };
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecurityProviderSymmetricKey(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey) Test(org.junit.Test)

Example 15 with SecurityProviderSymmetricKey

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;
        }
    };
}
Also used : SecurityProviderSymmetricKey(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey) KeyManager(javax.net.ssl.KeyManager) TrustManager(javax.net.ssl.TrustManager) Test(org.junit.Test)

Aggregations

SecurityProviderSymmetricKey (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey)17 Test (org.junit.Test)10 SecretKeySpec (javax.crypto.spec.SecretKeySpec)6 ProvisioningDeviceClientException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)4 SecurityProviderTpm (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm)3 IOException (java.io.IOException)3 ProvisioningDeviceSecurityException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceSecurityException)2 Scanner (java.util.Scanner)2 UrlPathBuilder (com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder)1 SecurityProvider (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider)1 SecurityProviderX509 (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderX509)1 SecurityProviderException (com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException)1 SecurityProviderTPMEmulator (com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator)1 SecurityProviderX509Cert (com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderX509Cert)1 KeyManager (javax.net.ssl.KeyManager)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManager (javax.net.ssl.TrustManager)1