Search in sources :

Example 1 with SecurityProviderTPMEmulator

use of com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator in project azure-iot-sdk-java by Azure.

the class ProvisioningTpmSample method main.

public static void main(String[] args) throws Exception {
    System.out.println("Starting...");
    System.out.println("Beginning setup.");
    SecurityProviderTpm securityClientTPMEmulator = null;
    Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name());
    DeviceClient deviceClient = null;
    try {
        securityClientTPMEmulator = new SecurityProviderTPMEmulator();
        System.out.println("Endorsement Key : \n" + new String(encodeBase64(securityClientTPMEmulator.getEndorsementKey()), StandardCharsets.UTF_8));
        System.out.println("Registration Id : \n" + securityClientTPMEmulator.getRegistrationId());
        System.out.println("Please visit Azure Portal (https://portal.azure.com/) and create a TPM Individual Enrollment with the information above i.e EndorsementKey and RegistrationId \n" + "Press enter when you are ready to run registration after enrolling with the service");
        scanner.nextLine();
    } catch (SecurityProviderException e) {
        e.printStackTrace();
    }
    ProvisioningDeviceClient provisioningDeviceClient = null;
    try {
        ProvisioningStatus provisioningStatus = new ProvisioningStatus();
        provisioningDeviceClient = ProvisioningDeviceClient.create(GLOBAL_ENDPOINT, SCOPE_ID, PROVISIONING_DEVICE_CLIENT_TRANSPORT_PROTOCOL, securityClientTPMEmulator);
        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, securityClientTPMEmulator, 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) SecurityProviderTpm(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm) SecurityProviderException(com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException) IOException(java.io.IOException) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException) SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator)

Example 2 with SecurityProviderTPMEmulator

use of com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator in project azure-iot-sdk-java by Azure.

the class SecurityProviderTPMEmulatorTest method signWithIdentityThrowsOnNullDeviceIdData.

// SRS_SecurityProviderTPMEmulator_25_029: [ This method shall throw IllegalArgumentException if `deviceIdData` is null or empty. ]
@Test(expected = IllegalArgumentException.class)
public void signWithIdentityThrowsOnNullDeviceIdData() throws Exception {
    // arrange
    final byte[] deviceIdData = null;
    createPersistentPrimaryExpectations();
    createPersistentPrimaryExpectations();
    SecurityProviderTPMEmulator securityProviderTPMEmulator = new SecurityProviderTPMEmulator();
    // act
    securityProviderTPMEmulator.signWithIdentity(deviceIdData);
}
Also used : SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) Test(org.junit.Test)

Example 3 with SecurityProviderTPMEmulator

use of com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator in project azure-iot-sdk-java by Azure.

the class SecurityProviderTPMEmulatorTest method activateIdentityKeyThrowsOnInnerWrapKeyNull.

// SRS_SecurityProviderTPMEmulator_25_013: [ This method shall throw SecurityProviderException if activating the credential for the session fails. ]
@Test(expected = SecurityProviderException.class)
public void activateIdentityKeyThrowsOnInnerWrapKeyNull() throws Exception {
    // arrange
    final byte[] testKey = "testKey".getBytes(StandardCharsets.UTF_8);
    createPersistentPrimaryExpectations();
    createPersistentPrimaryExpectations();
    SecurityProviderTPMEmulator securityProviderTPMEmulator = new SecurityProviderTPMEmulator();
    new NonStrictExpectations() {

        {
            TPM2B_PUBLIC.fromTpm((TpmBuffer) any);
            result = mockedTpm2BPublic;
            TPM2B_DATA.fromTpm((TpmBuffer) any);
            result = mockedTpm2BData;
            mockedTpm.StartAuthSession((TPM_HANDLE) any, (TPM_HANDLE) any, (byte[]) any, (byte[]) any, (TPM_SE) any, (TPMT_SYM_DEF) any, (TPM_ALG_ID) any);
            result = mockedStartAuthSessionResponse;
            mockedTpm.PolicySecret((TPM_HANDLE) any, (TPM_HANDLE) any, (byte[]) any, (byte[]) any, (byte[]) any, anyInt);
            mockedTpm._withSessions((TPM_HANDLE) any, mockedStartAuthSessionResponse.handle);
            mockedTpm.ActivateCredential((TPM_HANDLE) any, (TPM_HANDLE) any, (TPMS_ID_OBJECT) any, (byte[]) any);
            result = null;
        }
    };
    // act
    securityProviderTPMEmulator.activateIdentityKey(testKey);
}
Also used : SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) Test(org.junit.Test)

Example 4 with SecurityProviderTPMEmulator

use of com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator in project azure-iot-sdk-java by Azure.

the class SecurityProviderTPMEmulatorTest method getterCallsSuperRegistrationIdIfNotFound.

// SRS_SecurityProviderTPMEmulator_25_007: [ This method shall call its super method if registration Id was not provided. ]
@Test
public void getterCallsSuperRegistrationIdIfNotFound() throws Exception {
    // arrange
    createPersistentPrimaryExpectations();
    createPersistentPrimaryExpectations();
    SecurityProviderTPMEmulator securityProviderTPMEmulator = new SecurityProviderTPMEmulator();
    // act
    String testRegId = securityProviderTPMEmulator.getRegistrationId();
    // assert
    TestCase.assertNotNull(testRegId);
}
Also used : SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) Test(org.junit.Test)

Example 5 with SecurityProviderTPMEmulator

use of com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator in project azure-iot-sdk-java by Azure.

the class SecurityProviderTPMEmulatorTest method activateIdentityKeyThrowsOnIdKeyPrivateNull.

// SRS_SecurityProviderTPMEmulator_25_015: [ This method shall throw SecurityProviderException if importing the activated credential onto TPM fails. ]
@Test(expected = SecurityProviderException.class)
public void activateIdentityKeyThrowsOnIdKeyPrivateNull() throws Exception {
    // arrange
    final byte[] testKey = "testKey".getBytes(StandardCharsets.UTF_8);
    createPersistentPrimaryExpectations();
    createPersistentPrimaryExpectations();
    SecurityProviderTPMEmulator securityProviderTPMEmulator = new SecurityProviderTPMEmulator();
    new NonStrictExpectations() {

        {
            TPM2B_PUBLIC.fromTpm((TpmBuffer) any);
            result = mockedTpm2BPublic;
            TPM2B_DATA.fromTpm((TpmBuffer) any);
            result = mockedTpm2BData;
            mockedTpm.StartAuthSession((TPM_HANDLE) any, (TPM_HANDLE) any, (byte[]) any, (byte[]) any, (TPM_SE) any, (TPMT_SYM_DEF) any, (TPM_ALG_ID) any);
            result = mockedStartAuthSessionResponse;
            mockedTpm.PolicySecret((TPM_HANDLE) any, (TPM_HANDLE) any, (byte[]) any, (byte[]) any, (byte[]) any, anyInt);
            mockedTpm._withSessions((TPM_HANDLE) any, mockedStartAuthSessionResponse.handle);
            mockedTpm.ActivateCredential((TPM_HANDLE) any, (TPM_HANDLE) any, (TPMS_ID_OBJECT) any, (byte[]) any);
            result = "innerWrapKey".getBytes(StandardCharsets.UTF_8);
            mockedTpm.Import((TPM_HANDLE) any, (byte[]) any, (TPMT_PUBLIC) any, (TPM2B_PRIVATE) any, (byte[]) any, (TPMT_SYM_DEF_OBJECT) any);
            result = null;
        }
    };
    // act
    securityProviderTPMEmulator.activateIdentityKey(testKey);
}
Also used : SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) Test(org.junit.Test)

Aggregations

SecurityProviderTPMEmulator (com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator)32 Test (org.junit.Test)29 SecurityProvider (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider)3 SecurityProviderTpm (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm)2 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)2 ProvisioningDeviceClient (com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient)1 ProvisioningDeviceClientStatus (com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClientStatus)1 ProvisioningDeviceClientException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)1 SecurityProviderSymmetricKey (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey)1 SecurityProviderException (com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException)1 SecurityProviderX509Cert (com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderX509Cert)1 ProvisioningServiceClient (com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient)1 Attestation (com.microsoft.azure.sdk.iot.provisioning.service.configs.Attestation)1 IndividualEnrollment (com.microsoft.azure.sdk.iot.provisioning.service.configs.IndividualEnrollment)1 TpmAttestation (com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation)1 IOException (java.io.IOException)1 Scanner (java.util.Scanner)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)1 DeviceProvisioningServiceTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DeviceProvisioningServiceTest)1