Search in sources :

Example 6 with ClientException

use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.

the class RawKeyAccessor method sign.

@Override
public byte[] sign(@NonNull final byte[] text) throws ClientException {
    final String errCode;
    final Exception exception;
    try {
        final SecretKeySpec key = new SecretKeySpec(this.key, suite.cipher().name());
        Mac mac = Mac.getInstance(suite.macName());
        mac.init(key);
        return mac.doFinal(text);
    } catch (final NoSuchAlgorithmException e) {
        errCode = NO_SUCH_ALGORITHM;
        exception = e;
    } catch (final InvalidKeyException e) {
        errCode = INVALID_KEY;
        exception = e;
    }
    throw new ClientException(errCode, exception.getMessage());
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientException(com.microsoft.identity.common.exception.ClientException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac)

Example 7 with ClientException

use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.

the class RawKeyAccessor method decrypt.

@Override
public byte[] decrypt(@NonNull final byte[] ciphertext) throws ClientException {
    final String errCode;
    final Exception exception;
    try {
        final SecretKeySpec key = new SecretKeySpec(this.key, suite.cipher().name());
        final Cipher c = Cipher.getInstance(key.getAlgorithm());
        final IvParameterSpec ivSpec = new IvParameterSpec(ciphertext, 0, 12);
        c.init(Cipher.DECRYPT_MODE, key, ivSpec);
        byte[] out = Arrays.copyOfRange(ciphertext, 12, ciphertext.length);
        return c.doFinal(out);
    } catch (final NoSuchAlgorithmException e) {
        errCode = NO_SUCH_ALGORITHM;
        exception = e;
    } catch (final NoSuchPaddingException e) {
        errCode = NO_SUCH_PADDING;
        exception = e;
    } catch (final IllegalBlockSizeException e) {
        errCode = INVALID_BLOCK_SIZE;
        exception = e;
    } catch (final BadPaddingException e) {
        errCode = BAD_PADDING;
        exception = e;
    } catch (final InvalidKeyException e) {
        errCode = INVALID_KEY;
        exception = e;
    } catch (final InvalidAlgorithmParameterException e) {
        errCode = INVALID_ALG_PARAMETER;
        exception = e;
    }
    throw new ClientException(errCode, exception.getMessage(), exception);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) ClientException(com.microsoft.identity.common.exception.ClientException)

Example 8 with ClientException

use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.

the class RawKeyAccessor method getThumprint.

@Override
public byte[] getThumprint() throws ClientException {
    final SecretKey keySpec = new SecretKeySpec(key, suite.cipher().name());
    final Cipher cipher;
    final String errCode;
    final Exception exception;
    try {
        cipher = Cipher.getInstance(keySpec.getAlgorithm());
        final MessageDigest digest = MessageDigest.getInstance("SHA256");
        return digest.digest(cipher.doFinal((keySpec.getAlgorithm() + cipher.getBlockSize() + cipher.getParameters()).getBytes(UTF8)));
    } catch (final NoSuchAlgorithmException e) {
        errCode = NO_SUCH_ALGORITHM;
        exception = e;
    } catch (final NoSuchPaddingException e) {
        errCode = NO_SUCH_PADDING;
        exception = e;
    } catch (final BadPaddingException e) {
        errCode = BAD_PADDING;
        exception = e;
    } catch (final IllegalBlockSizeException e) {
        errCode = INVALID_BLOCK_SIZE;
        exception = e;
    }
    throw new ClientException(errCode, exception.getMessage(), exception);
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) ClientException(com.microsoft.identity.common.exception.ClientException) MessageDigest(java.security.MessageDigest) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 9 with ClientException

use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.

the class BrowserAuthorizationFragment method onResume.

@Override
public void onResume() {
    super.onResume();
    // This check is needed when using customTabs or browser flow.
    if (!mBrowserFlowStarted) {
        mBrowserFlowStarted = true;
        if (mAuthIntent != null) {
            // We cannot start browser activity inside OnCreate().
            // Because the life cycle of the current activity will continue and onResume will be called before finishing the login in browser.
            // This is by design of Android OS.
            startActivity(mAuthIntent);
        } else {
            final Intent resultIntent = new Intent();
            resultIntent.putExtra(AuthenticationConstants.Browser.RESPONSE_AUTHENTICATION_EXCEPTION, new ClientException(ErrorStrings.AUTHORIZATION_INTENT_IS_NULL));
            sendResult(AuthenticationConstants.UIResponse.BROWSER_CODE_AUTHENTICATION_EXCEPTION, resultIntent);
            finish();
        }
    } else {
        if (!StringUtil.isEmpty(sCustomTabResponseUri)) {
            completeAuthorizationInBrowserFlow(sCustomTabResponseUri);
        } else {
            cancelAuthorization(true);
        }
        sCustomTabResponseUri = null;
    }
}
Also used : Intent(android.content.Intent) ClientException(com.microsoft.identity.common.exception.ClientException)

Example 10 with ClientException

use of com.microsoft.identity.common.exception.ClientException in project microsoft-authentication-library-common-for-android by AzureAD.

the class CurrentTaskBrowserAuthorizationFragment method onResume.

@Override
public void onResume() {
    super.onResume();
    if (mResponseReceived) {
        // The custom tab was closed without getting a result.
        finish();
    }
    // This check is needed when using customTabs or browser flow.
    if (!mBrowserFlowStarted) {
        mBrowserFlowStarted = true;
        if (mAuthIntent != null) {
            // We cannot start browser activity inside OnCreate().
            // Because the life cycle of the current activity will continue and onResume will be called before finishing the login in browser.
            // This is by design of Android OS.
            startActivity(mAuthIntent);
        } else {
            final Intent resultIntent = new Intent();
            resultIntent.putExtra(AuthenticationConstants.Browser.RESPONSE_AUTHENTICATION_EXCEPTION, new ClientException(ErrorStrings.AUTHORIZATION_INTENT_IS_NULL));
            sendResult(AuthenticationConstants.UIResponse.BROWSER_CODE_AUTHENTICATION_EXCEPTION, resultIntent);
            finish();
        }
    } else {
        cancelAuthorization(true);
    }
}
Also used : Intent(android.content.Intent) ClientException(com.microsoft.identity.common.exception.ClientException)

Aggregations

ClientException (com.microsoft.identity.common.exception.ClientException)74 IOException (java.io.IOException)23 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)23 InvalidKeyException (java.security.InvalidKeyException)18 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)17 KeyStoreException (java.security.KeyStoreException)17 BadPaddingException (javax.crypto.BadPaddingException)17 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)17 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)17 UnrecoverableEntryException (java.security.UnrecoverableEntryException)15 CertificateException (java.security.cert.CertificateException)13 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 SignatureException (java.security.SignatureException)11 KeyPermanentlyInvalidatedException (android.security.keystore.KeyPermanentlyInvalidatedException)10 StrongBoxUnavailableException (android.security.keystore.StrongBoxUnavailableException)10 NonNull (androidx.annotation.NonNull)10 JOSEException (com.nimbusds.jose.JOSEException)10 NoSuchProviderException (java.security.NoSuchProviderException)10 ProviderException (java.security.ProviderException)10 JSONException (org.json.JSONException)10