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);
}
}
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);
}
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);
}
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();
}
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);
}
Aggregations