Search in sources :

Example 41 with ClientException

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

the class DevicePopManager method getAsymmetricKeyThumbprint.

@Override
public String getAsymmetricKeyThumbprint() throws ClientException {
    final Exception exception;
    final String errCode;
    try {
        final KeyStore.PrivateKeyEntry entry = mKeyManager.getEntry();
        return getRsaThumbprint(entry);
    } catch (final KeyStoreException e) {
        exception = e;
        errCode = KEYSTORE_NOT_INITIALIZED;
    } catch (final NoSuchAlgorithmException e) {
        exception = e;
        errCode = NO_SUCH_ALGORITHM;
    } catch (final UnrecoverableEntryException e) {
        exception = e;
        errCode = INVALID_PROTECTION_PARAMS;
    } catch (final JOSEException e) {
        exception = e;
        errCode = THUMBPRINT_COMPUTATION_FAILURE;
    }
    throw new ClientException(errCode, exception.getMessage(), exception);
}
Also used : UnrecoverableEntryException(java.security.UnrecoverableEntryException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientException(com.microsoft.identity.common.exception.ClientException) KeyStore(java.security.KeyStore) JOSEException(com.nimbusds.jose.JOSEException) JOSEException(com.nimbusds.jose.JOSEException) KeyStoreException(java.security.KeyStoreException) JSONException(org.json.JSONException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) StrongBoxUnavailableException(android.security.keystore.StrongBoxUnavailableException) SignatureException(java.security.SignatureException) ProviderException(java.security.ProviderException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) KeyPermanentlyInvalidatedException(android.security.keystore.KeyPermanentlyInvalidatedException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 42 with ClientException

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

the class DevicePopManager method getRequestConfirmation.

@Override
public void getRequestConfirmation(@NonNull final TaskCompletedCallbackWithError<String, ClientException> callback) {
    sThreadExecutor.submit(new Runnable() {

        @Override
        public void run() {
            // Vars for error handling...
            final Exception exception;
            final String errCode;
            try {
                final KeyStore.PrivateKeyEntry keyEntry = mKeyManager.getEntry();
                final KeyPair rsaKeyPair = getKeyPairForEntry(keyEntry);
                final RSAKey rsaKey = getRsaKeyForKeyPair(rsaKeyPair);
                final String base64UrlEncodedJwkJsonStr = getReqCnfForRsaKey(rsaKey);
                callback.onTaskCompleted(base64UrlEncodedJwkJsonStr);
                // We're done.
                return;
            } catch (final KeyStoreException e) {
                exception = e;
                errCode = KEYSTORE_NOT_INITIALIZED;
            } catch (final NoSuchAlgorithmException e) {
                exception = e;
                errCode = NO_SUCH_ALGORITHM;
            } catch (final UnrecoverableEntryException e) {
                exception = e;
                errCode = INVALID_PROTECTION_PARAMS;
            } catch (final JOSEException e) {
                exception = e;
                errCode = THUMBPRINT_COMPUTATION_FAILURE;
            } catch (final JSONException e) {
                exception = e;
                errCode = JSON_CONSTRUCTION_FAILED;
            }
            final ClientException clientException = new ClientException(errCode, exception.getMessage(), exception);
            Logger.error(TAG, clientException.getMessage(), clientException);
            callback.onError(clientException);
        }
    });
}
Also used : KeyPair(java.security.KeyPair) RSAKey(com.nimbusds.jose.jwk.RSAKey) JSONException(org.json.JSONException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JOSEException(com.nimbusds.jose.JOSEException) KeyStoreException(java.security.KeyStoreException) JSONException(org.json.JSONException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) StrongBoxUnavailableException(android.security.keystore.StrongBoxUnavailableException) SignatureException(java.security.SignatureException) ProviderException(java.security.ProviderException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) KeyPermanentlyInvalidatedException(android.security.keystore.KeyPermanentlyInvalidatedException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchProviderException(java.security.NoSuchProviderException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) ClientException(com.microsoft.identity.common.exception.ClientException) JOSEException(com.nimbusds.jose.JOSEException)

Example 43 with ClientException

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

the class DevicePopManager method getJwkPublicKey.

@NonNull
private String getJwkPublicKey() throws ClientException {
    final Exception exception;
    final String errCode;
    try {
        final Map<String, Object> jwkMap = getDevicePopJwkMinifiedJson();
        return GSON.toJson(jwkMap.get(SignedHttpRequestJwtClaims.JWK), MAP_STRING_STRING_TYPE);
    } catch (final UnrecoverableEntryException e) {
        exception = e;
        errCode = INVALID_PROTECTION_PARAMS;
    } catch (final NoSuchAlgorithmException e) {
        exception = e;
        errCode = NO_SUCH_ALGORITHM;
    } catch (final KeyStoreException e) {
        exception = e;
        errCode = KEYSTORE_NOT_INITIALIZED;
    }
    final ClientException clientException = new ClientException(errCode, exception.getMessage(), exception);
    Logger.error(TAG, clientException.getMessage(), clientException);
    throw clientException;
}
Also used : UnrecoverableEntryException(java.security.UnrecoverableEntryException) JSONObject(org.json.JSONObject) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) ClientException(com.microsoft.identity.common.exception.ClientException) JOSEException(com.nimbusds.jose.JOSEException) KeyStoreException(java.security.KeyStoreException) JSONException(org.json.JSONException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) StrongBoxUnavailableException(android.security.keystore.StrongBoxUnavailableException) SignatureException(java.security.SignatureException) ProviderException(java.security.ProviderException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) KeyPermanentlyInvalidatedException(android.security.keystore.KeyPermanentlyInvalidatedException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchProviderException(java.security.NoSuchProviderException) NonNull(androidx.annotation.NonNull)

Example 44 with ClientException

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

the class DevicePopManager method sign.

@Override
public byte[] sign(@NonNull SigningAlgorithm alg, @NonNull final byte[] inputBytesToSign) throws ClientException {
    Exception exception;
    String errCode;
    final String methodName = ":sign";
    try {
        final KeyStore.Entry keyEntry = mKeyManager.getEntry();
        if (!(keyEntry instanceof KeyStore.PrivateKeyEntry)) {
            Logger.warn(TAG + methodName, PRIVATE_KEY_NOT_FOUND);
            throw new ClientException(INVALID_KEY_MISSING);
        }
        final Signature signature = Signature.getInstance(alg.toString());
        signature.initSign(((KeyStore.PrivateKeyEntry) keyEntry).getPrivateKey());
        signature.update(inputBytesToSign);
        return signature.sign();
    } catch (final KeyStoreException e) {
        exception = e;
        errCode = KEYSTORE_NOT_INITIALIZED;
    } catch (final NoSuchAlgorithmException e) {
        exception = e;
        errCode = NO_SUCH_ALGORITHM;
    } catch (final UnrecoverableEntryException e) {
        exception = e;
        errCode = INVALID_PROTECTION_PARAMS;
    } catch (final InvalidKeyException e) {
        exception = e;
        errCode = INVALID_KEY;
    } catch (final SignatureException e) {
        exception = e;
        errCode = SIGNING_FAILURE;
    }
    final ClientException clientException = new ClientException(errCode, exception.getMessage(), exception);
    Logger.error(TAG + methodName, clientException.getMessage(), clientException);
    throw clientException;
}
Also used : Signature(java.security.Signature) UnrecoverableEntryException(java.security.UnrecoverableEntryException) ClientException(com.microsoft.identity.common.exception.ClientException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) KeyStore(java.security.KeyStore) JOSEException(com.nimbusds.jose.JOSEException) KeyStoreException(java.security.KeyStoreException) JSONException(org.json.JSONException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) StrongBoxUnavailableException(android.security.keystore.StrongBoxUnavailableException) SignatureException(java.security.SignatureException) ProviderException(java.security.ProviderException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) KeyPermanentlyInvalidatedException(android.security.keystore.KeyPermanentlyInvalidatedException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 45 with ClientException

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

the class DevicePopManager method decrypt.

@Override
public byte[] decrypt(@NonNull Cipher cipher, byte[] ciphertext) throws ClientException {
    String errCode;
    Exception exception;
    final String methodName = ":decrypt";
    try {
        // Load our key material
        final KeyStore.PrivateKeyEntry privateKeyEntry = mKeyManager.getEntry();
        // Get a reference to our private key (will not be loaded into app process)
        final PrivateKey privateKey = privateKeyEntry.getPrivateKey();
        // Init our cipher instance, don't use a named provider as there seems to be a mix of
        // BoringSSL & AndroidOpenSSL
        // https://issuetracker.google.com/issues/37091211
        final javax.crypto.Cipher outputCipher = javax.crypto.Cipher.getInstance(cipher.toString());
        if (cipher.getParameters() != null) {
            outputCipher.init(javax.crypto.Cipher.DECRYPT_MODE, privateKey, cipher.getParameters());
        } else {
            outputCipher.init(javax.crypto.Cipher.DECRYPT_MODE, privateKey);
        }
        return outputCipher.doFinal(ciphertext);
    } catch (final NoSuchAlgorithmException e) {
        errCode = NO_SUCH_ALGORITHM;
        exception = e;
    } catch (final InvalidKeyException e) {
        errCode = INVALID_KEY;
        exception = e;
    } catch (final UnrecoverableEntryException e) {
        errCode = INVALID_PROTECTION_PARAMS;
        exception = e;
    } catch (final NoSuchPaddingException e) {
        errCode = NO_SUCH_ALGORITHM;
        exception = e;
    } catch (final KeyStoreException e) {
        errCode = KEYSTORE_NOT_INITIALIZED;
        exception = e;
    } catch (final BadPaddingException e) {
        errCode = BAD_PADDING;
        exception = e;
    } catch (final IllegalBlockSizeException e) {
        errCode = INVALID_BLOCK_SIZE;
        exception = e;
    } catch (final InvalidAlgorithmParameterException e) {
        errCode = INVALID_ALG_PARAMETER;
        exception = e;
    }
    final ClientException clientException = new ClientException(errCode, exception.getMessage(), exception);
    Logger.error(TAG + methodName, errCode, exception);
    throw clientException;
}
Also used : PrivateKey(java.security.PrivateKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) KeyStore(java.security.KeyStore) JOSEException(com.nimbusds.jose.JOSEException) KeyStoreException(java.security.KeyStoreException) JSONException(org.json.JSONException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) StrongBoxUnavailableException(android.security.keystore.StrongBoxUnavailableException) SignatureException(java.security.SignatureException) ProviderException(java.security.ProviderException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) KeyPermanentlyInvalidatedException(android.security.keystore.KeyPermanentlyInvalidatedException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ClientException(com.microsoft.identity.common.exception.ClientException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchProviderException(java.security.NoSuchProviderException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) 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