Search in sources :

Example 31 with SecurityProviderTPMEmulator

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

the class ProvisioningTests method individualEnrollmentGetAttestationMechanismTPM.

@Test
public void individualEnrollmentGetAttestationMechanismTPM() throws ProvisioningServiceClientException, SecurityProviderException {
    // with the other TPM tests, so it lives here with them
    if (testInstance.attestationType != AttestationType.TPM) {
        return;
    }
    if (testInstance.protocol != HTTPS) {
        // The test protocol has no bearing on this test since it only uses the provisioning service client, so the test should only run once.
        return;
    }
    SecurityProvider securityProvider = new SecurityProviderTPMEmulator(testInstance.registrationId, MAX_TPM_CONNECT_RETRY_ATTEMPTS);
    Attestation attestation = new TpmAttestation(new String(encodeBase64(((SecurityProviderTpm) securityProvider).getEndorsementKey())));
    IndividualEnrollment individualEnrollment = new IndividualEnrollment(testInstance.registrationId, attestation);
    testInstance.provisioningServiceClient.createOrUpdateIndividualEnrollment(individualEnrollment);
    AttestationMechanism retrievedAttestationMechanism = testInstance.provisioningServiceClient.getIndividualEnrollmentAttestationMechanism(testInstance.registrationId);
    assertEquals(retrievedAttestationMechanism.getType(), AttestationMechanismType.TPM);
    assertTrue(retrievedAttestationMechanism.getAttestation() instanceof TpmAttestation);
    TpmAttestation retrievedTpmAttestation = (TpmAttestation) retrievedAttestationMechanism.getAttestation();
    assertNotNull(retrievedTpmAttestation.getEndorsementKey());
}
Also used : SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) SecurityProvider(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test) DeviceProvisioningServiceTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DeviceProvisioningServiceTest)

Example 32 with SecurityProviderTPMEmulator

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

the class ProvisioningTPMTests method provisioningTpmFlow.

@Test
public void provisioningTpmFlow() throws SecurityProviderException, ProvisioningServiceClientException, ProvisioningDeviceClientException, InterruptedException {
    ProvisioningServiceClient provisioningServiceClient = ProvisioningServiceClient.createFromConnectionString(provisioningServiceConnectionString);
    String registrationId = UUID.randomUUID().toString();
    String provisionedDeviceId = "Some-Provisioned-Device-" + TPM + "-" + UUID.randomUUID().toString();
    SecurityProvider securityProvider = new SecurityProviderTPMEmulator(registrationId);
    Attestation attestation = new TpmAttestation(new String(encodeBase64(((SecurityProviderTpm) securityProvider).getEndorsementKey())));
    IndividualEnrollment individualEnrollment = new IndividualEnrollment(registrationId, attestation);
    individualEnrollment.setDeviceIdFinal(provisionedDeviceId);
    provisioningServiceClient.createOrUpdateIndividualEnrollment(individualEnrollment);
    ProvisioningDeviceClient provisioningDeviceClient = ProvisioningDeviceClient.create(provisioningServiceGlobalEndpoint, provisioningServiceIdScope, ProvisioningDeviceClientTransportProtocol.AMQPS, securityProvider);
    AtomicBoolean registrationCompleted = new AtomicBoolean(false);
    AtomicBoolean registrationCompletedSuccessfully = new AtomicBoolean(false);
    provisioningDeviceClient.registerDevice((provisioningDeviceClientRegistrationResult, e, context) -> {
        log.debug("Provisioning registration callback fired with result {}", provisioningDeviceClientRegistrationResult.getProvisioningDeviceClientStatus());
        if (e != null) {
            log.error("Provisioning registration callback fired with exception {}", e);
        }
        ProvisioningDeviceClientStatus status = provisioningDeviceClientRegistrationResult.getProvisioningDeviceClientStatus();
        if (status == ProvisioningDeviceClientStatus.PROVISIONING_DEVICE_STATUS_ASSIGNED) {
            registrationCompletedSuccessfully.set(true);
        }
        registrationCompleted.set(true);
    }, null);
    long startTime = System.currentTimeMillis();
    while (!registrationCompleted.get()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > REGISTRATION_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for device registration to complete.");
        }
    }
    assertTrue("Registration completed, but not successfully", registrationCompletedSuccessfully.get());
    provisioningDeviceClient.closeNow();
}
Also used : IndividualEnrollment(com.microsoft.azure.sdk.iot.provisioning.service.configs.IndividualEnrollment) ProvisioningDeviceClient(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) ProvisioningDeviceClientStatus(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClientStatus) TpmAttestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation) SecurityProvider(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider) TpmAttestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation) Attestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.Attestation) ProvisioningServiceClient(com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient) 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