Search in sources :

Example 21 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 constructorThrowsOnInvalidRegistrationId.

// SRS_SecurityProviderTPMEmulator_25_004: [ The constructor shall validate and throw IllegalArgumentException if registration id is invalid. Valid registration Id
@Test
public void constructorThrowsOnInvalidRegistrationId() throws Exception {
    for (String regId : INVALID_REGISTRATION_IDS) {
        boolean invalidRegId = false;
        try {
            SecurityProviderTPMEmulator securityProviderTPMEmulator = new SecurityProviderTPMEmulator(regId);
        } catch (IllegalArgumentException e) {
            invalidRegId = true;
        }
        assertTrue(regId + " is invalid Registration Id but was found valid ", invalidRegId);
    }
}
Also used : SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) Test(org.junit.Test)

Example 22 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 activateIdentityKeyThrowsOnInvalidLengthOfEncUriData.

// SRS_SecurityProviderTPMEmulator_25_021: [ This method shall throw SecurityProviderException if the encoded Uri length is greater than Maximum Uri Length . ]
@Test(expected = SecurityProviderException.class)
public void activateIdentityKeyThrowsOnInvalidLengthOfEncUriData() 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 = mockedTpm2BPrivate;
            mockedTpm.Load((TPM_HANDLE) any, (TPM2B_PRIVATE) any, (TPMT_PUBLIC) any);
            result = mockedTpmHandle;
            // clearPersistentExpectations
            mockedTpm._allowErrors();
            mockedTpm.ReadPublic((TPM_HANDLE) any);
            result = mockedReadPublicResponse;
            mockedTpm._getLastResponseCode();
            result = TPM_RC.SUCCESS;
            mockedTpm.EvictControl((TPM_HANDLE) any, (TPM_HANDLE) any, (TPM_HANDLE) any);
            mockedTpm.EvictControl((TPM_HANDLE) any, (TPM_HANDLE) any, (TPM_HANDLE) any);
            mockedTpm.FlushContext((TPM_HANDLE) any);
            Deencapsulation.setField(mockedTpm2BData, "buffer", "lenGreaterThan10".getBytes(StandardCharsets.UTF_8));
            TpmHelpers.getTpmProperty(mockedTpm, TPM_PT.INPUT_BUFFER);
            result = 10;
        }
    };
    // act
    securityProviderTPMEmulator.activateIdentityKey(testKey);
}
Also used : SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) Test(org.junit.Test)

Example 23 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 activateIdentityKeyThrowsOnEncryptDecryptResponseNull.

// SRS_SecurityProviderTPMEmulator_25_0027: [ This method shall throw if Encrypt Decrypt the symmetric Key fails. ]
@Test(expected = SecurityProviderException.class)
public void activateIdentityKeyThrowsOnEncryptDecryptResponseNull() 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 = mockedTpm2BPrivate;
            mockedTpm.Load((TPM_HANDLE) any, (TPM2B_PRIVATE) any, (TPMT_PUBLIC) any);
            result = mockedTpmHandle;
            // clearPersistentExpectations
            mockedTpm._allowErrors();
            mockedTpm.ReadPublic((TPM_HANDLE) any);
            result = mockedReadPublicResponse;
            mockedTpm._getLastResponseCode();
            result = TPM_RC.SUCCESS;
            mockedTpm.EvictControl((TPM_HANDLE) any, (TPM_HANDLE) any, (TPM_HANDLE) any);
            mockedTpm.EvictControl((TPM_HANDLE) any, (TPM_HANDLE) any, (TPM_HANDLE) any);
            mockedTpm.FlushContext((TPM_HANDLE) any);
            Deencapsulation.setField(mockedTpm2BData, "buffer", "len<10".getBytes(StandardCharsets.UTF_8));
            TpmHelpers.getTpmProperty(mockedTpm, TPM_PT.INPUT_BUFFER);
            result = 10;
            mockedTpm.Create((TPM_HANDLE) any, (TPMS_SENSITIVE_CREATE) any, (TPMT_PUBLIC) any, (byte[]) any, (TPMS_PCR_SELECTION[]) any);
            result = mockedCreateResponse;
            mockedTpm.Load((TPM_HANDLE) any, (TPM2B_PRIVATE) any, (TPMT_PUBLIC) any);
            result = mockedTpmHandle;
            mockedTpm.EncryptDecrypt((TPM_HANDLE) any, anyByte, (TPM_ALG_ID) any, (byte[]) 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 24 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 constructorThrowsOnResponseCodeNotSuccessNotHandleOnClear.

@Test(expected = SecurityProviderException.class)
public void constructorThrowsOnResponseCodeNotSuccessNotHandleOnClear() throws Exception {
    // arrange
    new StrictExpectations() {

        {
            mockedTpm._allowErrors();
            mockedTpm.ReadPublic((TPM_HANDLE) any);
            result = mockedReadPublicResponse;
            mockedTpm._getLastResponseCode();
            result = TPM_RC.TESTING;
        }
    };
    // act
    SecurityProviderTPMEmulator securityProviderTPMEmulator = new SecurityProviderTPMEmulator();
}
Also used : SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) Test(org.junit.Test)

Example 25 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 signWithIdentitySucceeds.

// SRS_SecurityProviderTPMEmulator_25_031: [ This method shall sign the device ID data. ]
@Test
public void signWithIdentitySucceeds(@Mocked TPM_ALG_ID mockedTpmAlgId, @Mocked TPMU_ASYM_SCHEME mockedTpmuAsymScheme, @Mocked TPMU_PUBLIC_PARMS mockedTpmuPublicParms, @Mocked TPMS_SCHEME_HMAC mockedTpmsSchemeHmac, @Mocked TPMS_KEYEDHASH_PARMS mockedTpmsKeyedhashParms) throws Exception {
    // arrange
    final byte[] deviceIdData = "deviceIdData".getBytes(StandardCharsets.UTF_8);
    createPersistentPrimaryExpectations();
    createPersistentPrimaryExpectations();
    SecurityProviderTPMEmulator securityProviderTPMEmulator = new SecurityProviderTPMEmulator();
    new NonStrictExpectations() {

        {
            // idKeyPub
            Deencapsulation.setField(securityProviderTPMEmulator, "idKeyPub", mockedTpm2BPublic);
            // publicArea
            Deencapsulation.setField(mockedTpm2BPublic, "publicArea", mockedTpmtPublic);
            // parameters
            Deencapsulation.setField(mockedTpmtPublic, "parameters", mockedTpmsKeyedhashParms);
            // scheme
            Deencapsulation.setField(mockedTpmsKeyedhashParms, "scheme", mockedTpmsSchemeHmac);
            // hashAlg
            Deencapsulation.setField(mockedTpmsSchemeHmac, "hashAlg", mockedTpmAlgId);
            TpmHelpers.getTpmProperty(mockedTpm, TPM_PT.INPUT_BUFFER);
            result = 10;
            mockedTpm.HMAC_Start((TPM_HANDLE) any, (byte[]) any, mockedTpmAlgId);
            result = mockedTpmHandle;
            mockedTpm.SequenceUpdate((TPM_HANDLE) any, (byte[]) any);
            mockedTpm.SequenceComplete((TPM_HANDLE) any, (byte[]) any, (TPM_HANDLE) any);
        }
    };
    securityProviderTPMEmulator.signWithIdentity(deviceIdData);
}
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