Search in sources :

Example 6 with SecurityProviderException

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);
    }
}
Also used : ProvisioningDeviceSecurityException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceSecurityException) ProvisioningErrorParser(com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser) SecurityProviderException(com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException) SSLContext(javax.net.ssl.SSLContext) ProvisioningDeviceClientException(com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)

Example 7 with SecurityProviderException

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);
    }
}
Also used : SecurityProviderException(com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException)

Example 8 with SecurityProviderException

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);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) SecurityProviderException(com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException) IOException(java.io.IOException)

Example 9 with SecurityProviderException

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();
}
Also used : IotHubAuthenticationProvider(com.microsoft.azure.sdk.iot.device.auth.IotHubAuthenticationProvider) IotHubX509HardwareAuthenticationProvider(com.microsoft.azure.sdk.iot.device.auth.IotHubX509HardwareAuthenticationProvider) SecurityProviderException(com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 10 with SecurityProviderException

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);
}
Also used : SecurityProviderException(com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException) Test(org.junit.Test)

Aggregations

SecurityProviderException (com.microsoft.azure.sdk.iot.provisioning.security.exceptions.SecurityProviderException)15 ProvisioningDeviceClientException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceClientException)4 IOException (java.io.IOException)4 SecurityProviderTpm (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderTpm)3 Test (org.junit.Test)3 ProvisioningDeviceSecurityException (com.microsoft.azure.sdk.iot.provisioning.device.internal.exceptions.ProvisioningDeviceSecurityException)2 SecurityProviderX509 (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderX509)2 Scanner (java.util.Scanner)2 SSLContext (javax.net.ssl.SSLContext)2 IotHubAuthenticationProvider (com.microsoft.azure.sdk.iot.device.auth.IotHubAuthenticationProvider)1 IotHubX509HardwareAuthenticationProvider (com.microsoft.azure.sdk.iot.device.auth.IotHubX509HardwareAuthenticationProvider)1 ProvisioningErrorParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.ProvisioningErrorParser)1 RegistrationOperationStatusParser (com.microsoft.azure.sdk.iot.provisioning.device.internal.parser.RegistrationOperationStatusParser)1 SecurityProviderSymmetricKey (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProviderSymmetricKey)1 SecurityProviderDiceEmulator (com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderDiceEmulator)1 SecurityProviderTPMEmulator (com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UnknownHostException (java.net.UnknownHostException)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 X509Certificate (java.security.cert.X509Certificate)1