use of com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException in project azure-iot-sdk-java by Azure.
the class StatusTask method getRegistrationStatus.
private RegistrationOperationStatusParser getRegistrationStatus(String operationId, Authorization authorization) throws ProvisioningDeviceClientException {
try {
// SRS_StatusTask_25_003: [ This method shall throw ProvisioningDeviceClientException if registration id is null or empty. ]
String registrationId = this.securityProvider.getRegistrationId();
if (registrationId == null || registrationId.isEmpty()) {
throw new ProvisioningDeviceSecurityException("registrationId cannot be null or empty");
}
// SRS_StatusTask_25_004: [ This method shall retrieve the SSL context from Authorization and throw ProvisioningDeviceClientException if it is null. ]
SSLContext sslContext = authorization.getSslContext();
if (sslContext == null) {
throw new ProvisioningDeviceSecurityException("SSL context cannot be null");
}
RequestData requestData = new RequestData(registrationId, operationId, authorization.getSslContext(), authorization.getSasToken(), null);
// SRS_StatusTask_25_005: [ This method shall trigger getRegistrationState on the contract API and wait for response and return it. ]
ResponseData responseData = new ResponseData();
provisioningDeviceClientContract.getRegistrationStatus(requestData, new ResponseCallbackImpl(), responseData);
if (responseData.getResponseData() == null || responseData.getContractState() != ContractState.DPS_REGISTRATION_RECEIVED) {
Thread.sleep(MAX_WAIT_FOR_STATUS_RESPONSE);
}
if (responseData.getResponseData() != null && responseData.getContractState() == ContractState.DPS_REGISTRATION_RECEIVED) {
String jsonBody = new String(responseData.getResponseData(), StandardCharsets.UTF_8);
try {
return RegistrationOperationStatusParser.createFromJson(jsonBody);
} catch (IllegalArgumentException e) {
// SRS_StatusTask_34_007: [ If the response data cannot be parsed into a RegistrationOperationStatusParser,
// this function shall parse it into a ProvisioningErrorParser and throw a ProvisioningDeviceClientException with the parsed message. ]
ProvisioningErrorParser provisioningErrorParser = ProvisioningErrorParser.createFromJson(jsonBody);
throw new ProvisioningDeviceClientException(provisioningErrorParser.getExceptionMessage());
}
} else {
// SRS_StatusTask_25_006: [ This method shall throw ProvisioningDeviceClientException if null response or no response is received in maximum time of 90 seconds. ]
throw new ProvisioningDeviceClientException("Did not receive DPS Status information");
}
} catch (InterruptedException | SecurityProviderException e) {
throw new ProvisioningDeviceClientException(e);
}
}
use of com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException in project azure-iot-sdk-java by Azure.
the class SecurityProviderTPMHsm method clearPersistent.
@SuppressWarnings("SameParameterValue")
private void clearPersistent(Tpm tpm, TPM_HANDLE hPersistent, String keyRole) throws SecurityProviderException {
tpm._allowErrors().ReadPublic(hPersistent);
TPM_RC rc = tpm._getLastResponseCode();
if (rc == TPM_RC.SUCCESS) {
tpm.EvictControl(TPM_HANDLE.from(TPM_RH.OWNER), hPersistent, hPersistent);
} else if (rc != TPM_RC.HANDLE) {
throw new SecurityProviderException("Unexpected failure for {" + rc.name() + "} of TPM2_ReadPublic for " + keyRole + " 0x" + hPersistent.handle);
}
}
use of com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException in project azure-iot-sdk-java by Azure.
the class IotHubSasTokenHardwareAuthenticationProvider method generateSasTokenSignatureFromSecurityProvider.
private String generateSasTokenSignatureFromSecurityProvider(long secondsToLive) throws IOException {
try {
// token scope is formatted as "<hostName>/devices/<deviceId>"
String tokenScope = String.format(TOKEN_SCOPE_FORMAT, this.hostname, this.deviceId);
String encodedTokenScope = URLEncoder.encode(tokenScope, ENCODING_FORMAT_NAME);
if (encodedTokenScope == null || encodedTokenScope.isEmpty()) {
// Codes_SRS_IOTHUBSASTOKENHARDWAREAUTHENTICATION_34_009: [If the token scope cannot be encoded, this function shall throw an IOException.]
throw new IOException("Could not construct token scope");
}
Long expiryTimeUTC = (System.currentTimeMillis() / 1000) + secondsToLive;
byte[] token = this.securityProvider.signWithIdentity(encodedTokenScope.concat("\n" + expiryTimeUTC).getBytes(StandardCharsets.UTF_8));
if (token == null || token.length == 0) {
// Codes_SRS_IOTHUBSASTOKENHARDWAREAUTHENTICATION_34_010: [If the call for the saved security provider to sign with identity returns null or empty bytes, this function shall throw an IOException.]
throw new IOException("Security provider could not sign data successfully");
}
byte[] base64Signature = encodeBase64(token);
String base64UrlEncodedSignature = URLEncoder.encode(new String(base64Signature, StandardCharsets.UTF_8), ENCODING_FORMAT_NAME);
return String.format(SASTOKEN_FORMAT, encodedTokenScope, base64UrlEncodedSignature, expiryTimeUTC);
} catch (UnsupportedEncodingException | SecurityProviderException e) {
// Codes_SRS_IOTHUBSASTOKENHARDWAREAUTHENTICATION_34_011: [When generating the sas token signature from the security provider, if an UnsupportedEncodingException or SecurityProviderException is thrown, this function shall throw an IOException.]
throw new IOException(e);
}
}
use of com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException in project azure-iot-sdk-java by Azure.
the class IotHubX509HardwareIotHubAuthenticationProviderTest method getSSLContextThrowsIOExceptionIfExceptionEncountered.
// Tests_SRS_IOTHUBX509HARDWAREAUTHENTICATION_34_004: [If the security provider throws a SecurityProviderException while generating an SSLContext, this function shall throw an IOException.]
@Test(expected = IOException.class)
public void getSSLContextThrowsIOExceptionIfExceptionEncountered() throws SecurityProviderException, IOException, TransportException {
// arrange
IotHubAuthenticationProvider authentication = new IotHubX509HardwareAuthenticationProvider(hostname, gatewayHostname, deviceId, moduleId, mockSecurityProviderX509);
new NonStrictExpectations() {
{
mockSecurityProviderX509.getSSLContext();
result = new SecurityProviderException("");
}
};
// act
authentication.getSSLContext();
}
use of com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException in project azure-iot-sdk-java by Azure.
the class IotHubSasTokenHardwareAuthenticationProviderTest method generateSasTokenSignatureFromSecurityProviderThrowsDuringSignWithIdentityThrowsIOException.
// Tests_SRS_IOTHUBSASTOKENHARDWAREAUTHENTICATION_34_011: [When generating the sas token signature from the security provider, if an UnsupportedEncodingException or SecurityProviderException is thrown, this function shall throw an IOException.]
@Test(expected = IOException.class)
public void generateSasTokenSignatureFromSecurityProviderThrowsDuringSignWithIdentityThrowsIOException() throws IOException, InvalidKeyException, SecurityProviderException {
// arrange
new NonStrictExpectations() {
{
URLEncoder.encode(anyString, encodingName);
result = "some token";
mockSecurityProviderTpm.signWithIdentity((byte[]) any);
result = new SecurityProviderException("");
}
};
// act
new IotHubSasTokenHardwareAuthenticationProvider(expectedHostname, expectedGatewayHostname, expectedDeviceId, expectedModuleId, mockSecurityProviderTpm);
}
Aggregations