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