Search in sources :

Example 1 with InvalidEncryptionDataException

use of com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException in project braintree_android by braintree.

the class AuthorizationRequest method parseBrowserResponse.

@Override
public Result parseBrowserResponse(ContextInspector contextInspector, Uri uri) {
    String status = uri.getLastPathSegment();
    String payloadEnc = uri.getQueryParameter("payloadEnc");
    JSONObject payload;
    try {
        payload = new JSONObject(new String(Base64.decode(uri.getQueryParameter("payload"), Base64.DEFAULT)));
    } catch (NullPointerException | IllegalArgumentException | JSONException e) {
        payload = new JSONObject();
    }
    if (Uri.parse(getSuccessUrl()).getLastPathSegment().equals(status)) {
        if (!payload.has("msg_GUID")) {
            return new Result(new ResponseParsingException("Response incomplete"));
        }
        if (TextUtils.isEmpty(payloadEnc) || !isValidResponse(Json.optString(payload, "msg_GUID", ""))) {
            return new Result(new ResponseParsingException("Response invalid"));
        }
        try {
            JSONObject decryptedPayloadEnc = getDecryptedPayload(payloadEnc);
            String error = Json.optString(payload, "error", "");
            // the string 'null' is coming back in production
            if (!TextUtils.isEmpty(error) && !"null".equals(error)) {
                return new Result(new BrowserSwitchException(error));
            }
            return new Result(Json.optString(payload, "environment", ""), ResponseType.authorization_code, new JSONObject().put("code", decryptedPayloadEnc.getString("payment_code")), decryptedPayloadEnc.getString("email"));
        } catch (JSONException | InvalidAlgorithmParameterException | NoSuchAlgorithmException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | InvalidKeyException | InvalidEncryptionDataException | IllegalArgumentException e) {
            return new Result(new ResponseParsingException(e));
        }
    } else if (Uri.parse(getCancelUrl()).getLastPathSegment().equals(status)) {
        String error = Json.optString(payload, "error", "");
        // the string 'null' is coming back in production
        if (!TextUtils.isEmpty(error) && !"null".equals(error)) {
            return new Result(new BrowserSwitchException(error));
        } else {
            return new Result();
        }
    } else {
        return new Result(new ResponseParsingException("Response uri invalid"));
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ResponseParsingException(com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException) JSONException(org.json.JSONException) BrowserSwitchException(com.paypal.android.sdk.onetouch.core.exception.BrowserSwitchException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) JSONObject(org.json.JSONObject) InvalidEncryptionDataException(com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException)

Example 2 with InvalidEncryptionDataException

use of com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException in project braintree_android by braintree.

the class OtcCrypto method decryptAESCTRData.

public byte[] decryptAESCTRData(byte[] cipherData, byte[] key) throws IllegalBlockSizeException, InvalidKeyException, NoSuchAlgorithmException, IllegalArgumentException, InvalidAlgorithmParameterException, NoSuchPaddingException, BadPaddingException, InvalidEncryptionDataException {
    // we should have at least 1 byte of data
    if (cipherData.length < DIGEST_SIZE + NONCE_SIZE) {
        throw new InvalidEncryptionDataException("data is too small");
    }
    // first 16 bytes is encryption key, 2nd 16 bytes is digest key
    byte[] encryptionKey = new byte[AES_KEY_SIZE];
    System.arraycopy(key, 0, encryptionKey, 0, AES_KEY_SIZE);
    byte[] digestKey = new byte[AES_KEY_SIZE];
    System.arraycopy(key, AES_KEY_SIZE, digestKey, 0, AES_KEY_SIZE);
    // extract signature it is 32 bytes
    byte[] signature = new byte[DIGEST_SIZE];
    System.arraycopy(cipherData, 0, signature, 0, DIGEST_SIZE);
    // extract the rest to calculate digest and compare it to the signature
    byte[] signedData = new byte[cipherData.length - DIGEST_SIZE];
    System.arraycopy(cipherData, DIGEST_SIZE, signedData, 0, cipherData.length - DIGEST_SIZE);
    byte[] digest = dataDigest(signedData, digestKey);
    if (!EncryptionUtils.isEqual(digest, signature)) {
        throw new IllegalArgumentException("Signature mismatch");
    }
    // read nonce
    byte[] nonceData = new byte[NONCE_SIZE];
    System.arraycopy(signedData, 0, nonceData, 0, NONCE_SIZE);
    // init nonce and decrypt
    IvParameterSpec nonceSpec = new IvParameterSpec(nonceData);
    SecretKeySpec keySpec = new SecretKeySpec(encryptionKey, "AES");
    Cipher cipher = Cipher.getInstance(AES_CTR_ALGO);
    cipher.init(Cipher.DECRYPT_MODE, keySpec, nonceSpec);
    return cipher.doFinal(signedData, NONCE_SIZE, signedData.length - NONCE_SIZE);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) InvalidEncryptionDataException(com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException)

Example 3 with InvalidEncryptionDataException

use of com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException in project braintree_android by braintree.

the class BrowserSwitchHelper method getBrowserSwitchIntent.

public static Intent getBrowserSwitchIntent(ContextInspector contextInspector, ConfigManager configManager, Request request) {
    OtcConfiguration configuration = configManager.getConfig();
    try {
        String url = request.getBrowserSwitchUrl(contextInspector.getContext(), configuration);
        Recipe<?> recipe = request.getBrowserSwitchRecipe(configuration);
        for (String allowedBrowserPackage : recipe.getTargetPackagesInReversePriorityOrder()) {
            boolean canIntentBeResolved = Recipe.isValidBrowserTarget(contextInspector.getContext(), url, allowedBrowserPackage);
            if (canIntentBeResolved) {
                request.trackFpti(contextInspector.getContext(), TrackingPoint.SwitchToBrowser, recipe.getProtocol());
                return Recipe.getBrowserIntent(contextInspector.getContext(), url, allowedBrowserPackage);
            }
        }
    } catch (CertificateException | UnsupportedEncodingException | NoSuchPaddingException | NoSuchAlgorithmException | IllegalBlockSizeException | JSONException | BadPaddingException | InvalidEncryptionDataException | InvalidKeyException ignored) {
    }
    return null;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) JSONException(org.json.JSONException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) OtcConfiguration(com.paypal.android.sdk.onetouch.core.config.OtcConfiguration) InvalidEncryptionDataException(com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException)

Example 4 with InvalidEncryptionDataException

use of com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException in project braintree_android by braintree.

the class OtcCrypto method encryptRSAData.

public byte[] encryptRSAData(byte[] plainData, Certificate certificate) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidEncryptionDataException {
    if (plainData.length > MAX_RSA_ENCRYPTABLE_BYTES) {
        throw new InvalidEncryptionDataException("Data is too large for public key encryption: " + plainData.length + " > " + MAX_RSA_ENCRYPTABLE_BYTES);
    }
    PublicKey publicKey = certificate.getPublicKey();
    Cipher rsaCipher = Cipher.getInstance(RSA_ALGO);
    rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
    return rsaCipher.doFinal(plainData);
}
Also used : PublicKey(java.security.PublicKey) Cipher(javax.crypto.Cipher) InvalidEncryptionDataException(com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException)

Aggregations

InvalidEncryptionDataException (com.paypal.android.sdk.onetouch.core.exception.InvalidEncryptionDataException)4 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 BadPaddingException (javax.crypto.BadPaddingException)2 Cipher (javax.crypto.Cipher)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 JSONException (org.json.JSONException)2 OtcConfiguration (com.paypal.android.sdk.onetouch.core.config.OtcConfiguration)1 BrowserSwitchException (com.paypal.android.sdk.onetouch.core.exception.BrowserSwitchException)1 ResponseParsingException (com.paypal.android.sdk.onetouch.core.exception.ResponseParsingException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 PublicKey (java.security.PublicKey)1 CertificateException (java.security.cert.CertificateException)1 IvParameterSpec (javax.crypto.spec.IvParameterSpec)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 JSONObject (org.json.JSONObject)1