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"));
}
}
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);
}
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;
}
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);
}
Aggregations