use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.
the class SecurityProviderSymmetricKeyTest method getSSLContextThrowsUnderlyingException.
// SRS_SecurityClientSymmetricKey_25_005: [ This method shall throw SecurityProviderException if any of the underlying API's in generating SSL context fails. ]
@Test(expected = SecurityProviderException.class)
public void getSSLContextThrowsUnderlyingException() throws SecurityProviderException, KeyStoreException {
// arrange
SecurityProviderSymmetricKey securityProviderSymmetricKey = new SecurityProviderSymmetricKey(testSymKey, testRegId);
new NonStrictExpectations() {
{
mockedKeyStore.setCertificateEntry(anyString, (Certificate) any);
result = new KeyStoreException();
}
};
// act
securityProviderSymmetricKey.getSSLContext();
}
use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.
the class RegisterTask method authenticateWithDPS.
private RegistrationOperationStatusParser authenticateWithDPS() throws ProvisioningDeviceClientException, SecurityProviderException {
if (securityProvider.getRegistrationId() == null) {
throw new ProvisioningDeviceClientException(new IllegalArgumentException("registration id cannot be null"));
}
try {
SSLContext sslContext = securityProvider.getSSLContext();
if (sslContext == null) {
throw new ProvisioningDeviceSecurityException("Null SSL Context received from security client");
}
authorization.setSslContext(sslContext);
if (this.securityProvider instanceof SecurityProviderX509) {
RequestData requestData = new RequestData(securityProvider.getRegistrationId(), sslContext, true, this.provisioningDeviceClientConfig.getPayload());
log.info("Authenticating with device provisioning service using x509 certificates");
return this.authenticateWithX509(requestData);
} else if (this.securityProvider instanceof SecurityProviderTpm) {
SecurityProviderTpm securityProviderTpm = (SecurityProviderTpm) securityProvider;
if (securityProviderTpm.getEndorsementKey() == null || securityProviderTpm.getStorageRootKey() == null) {
throw new ProvisioningDeviceSecurityException(new IllegalArgumentException("Ek or SRK cannot be null"));
}
// SRS_RegisterTask_25_009: [ If the provided security client is for Key then, this method shall save the SSL context to Authorization if it is not null and throw ProvisioningDeviceClientException otherwise. ]
RequestData requestData = new RequestData(securityProviderTpm.getEndorsementKey(), securityProviderTpm.getStorageRootKey(), securityProvider.getRegistrationId(), sslContext, null, this.provisioningDeviceClientConfig.getPayload());
log.info("Authenticating with device provisioning service using tpm");
return this.authenticateWithTPM(requestData);
} else if (this.securityProvider instanceof SecurityProviderSymmetricKey) {
RequestData requestData = new RequestData(securityProvider.getRegistrationId(), sslContext, null, this.provisioningDeviceClientConfig.getPayload());
log.info("Authenticating with device provisioning service using symmetric key");
return this.authenticateWithSasToken(requestData);
} else {
throw new ProvisioningDeviceSecurityException("Unknown Security client received");
}
} catch (SecurityProviderException | IOException | InterruptedException e) {
throw new ProvisioningDeviceSecurityException(e);
}
}
use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.
the class ProvisioningSymmetricKeyIndividualEnrollmentSample method main.
public static void main(String[] args) throws Exception {
System.out.println("Starting...");
System.out.println("Beginning setup.");
SecurityProviderSymmetricKey securityClientSymmetricKey;
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8.name());
DeviceClient deviceClient = null;
securityClientSymmetricKey = new SecurityProviderSymmetricKey(SYMMETRIC_KEY.getBytes(StandardCharsets.UTF_8), REGISTRATION_ID);
ProvisioningDeviceClient provisioningDeviceClient = null;
try {
ProvisioningStatus provisioningStatus = new ProvisioningStatus();
provisioningDeviceClient = ProvisioningDeviceClient.create(GLOBAL_ENDPOINT, SCOPE_ID, PROVISIONING_DEVICE_CLIENT_TRANSPORT_PROTOCOL, securityClientSymmetricKey);
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, securityClientSymmetricKey, 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.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.
the class Thermostat method initializeAndProvisionDevice.
private static void initializeAndProvisionDevice() throws ProvisioningDeviceClientException, IOException, URISyntaxException, InterruptedException {
SecurityProviderSymmetricKey securityClientSymmetricKey = new SecurityProviderSymmetricKey(deviceSymmetricKey.getBytes(StandardCharsets.UTF_8), registrationId);
ProvisioningDeviceClient provisioningDeviceClient;
ProvisioningStatus provisioningStatus = new ProvisioningStatus();
provisioningDeviceClient = ProvisioningDeviceClient.create(globalEndpoint, scopeId, provisioningProtocol, securityClientSymmetricKey);
AdditionalData additionalData = new AdditionalData();
additionalData.setProvisioningPayload(String.format("{\"modelId\": \"%s\"}", MODEL_ID));
provisioningDeviceClient.registerDevice(new ProvisioningDeviceClientRegistrationCallbackImpl(), provisioningStatus, additionalData);
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);
}
ClientOptions options = new ClientOptions();
options.setModelId(MODEL_ID);
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());
String iotHubUri = provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getIothubUri();
String deviceId = provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getDeviceId();
log.debug("Opening the device client.");
deviceClient = DeviceClient.createFromSecurityProvider(iotHubUri, deviceId, securityClientSymmetricKey, IotHubClientProtocol.MQTT, options);
deviceClient.open();
}
}
use of com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey in project azure-iot-sdk-java by Azure.
the class TemperatureController method initializeAndProvisionDevice.
private static void initializeAndProvisionDevice() throws ProvisioningDeviceClientException, IOException, URISyntaxException, InterruptedException {
SecurityProviderSymmetricKey securityClientSymmetricKey = new SecurityProviderSymmetricKey(deviceSymmetricKey.getBytes(StandardCharsets.UTF_8), registrationId);
ProvisioningDeviceClient provisioningDeviceClient;
ProvisioningStatus provisioningStatus = new ProvisioningStatus();
provisioningDeviceClient = ProvisioningDeviceClient.create(globalEndpoint, scopeId, provisioningProtocol, securityClientSymmetricKey);
AdditionalData additionalData = new AdditionalData();
additionalData.setProvisioningPayload(com.microsoft.azure.sdk.iot.provisioning.device.plugandplay.PnpHelper.createDpsPayload(MODEL_ID));
provisioningDeviceClient.registerDevice(new ProvisioningDeviceClientRegistrationCallbackImpl(), provisioningStatus, additionalData);
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);
}
ClientOptions options = new ClientOptions();
options.setModelId(MODEL_ID);
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());
String iotHubUri = provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getIothubUri();
String deviceId = provisioningStatus.provisioningDeviceClientRegistrationInfoClient.getDeviceId();
log.debug("Opening the device client.");
deviceClient = DeviceClient.createFromSecurityProvider(iotHubUri, deviceId, securityClientSymmetricKey, IotHubClientProtocol.MQTT, options);
deviceClient.open();
}
}
Aggregations