Android Open Source - DroidBilling Billing Security






From Project

Back to project page DroidBilling.

License

The source code is released under:

MIT License

If you think the Android project DroidBilling listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.techery.droid.billings.utils;
//from  www.j a va2s  . c  o m
import android.text.TextUtils;
import android.util.Log;

import com.techery.droid.billings.models.PurchaseResult;

import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

public class BillingSecurity {
    private static final String TAG = "IABUtil/BillingSecurity";

    private static final String KEY_FACTORY_ALGORITHM = "RSA";
    private static final String SIGNATURE_ALGORITHM = "SHA1withRSA";

    /**
     * Verifies that the data was signed with the given signature, and returns
     * the verified purchase. The data is in JSON format and signed
     * with a private key. The data also contains the {@link PurchaseState}
     * and product ID of the purchase.
     * @param base64PublicKey the base64-encoded public key to use for verifying.
     * @param signedData the signed JSON string (signed, not encrypted)
     * @param signature the signature for the data, signed with the private key
     */
    public boolean verifyPurchase(String base64PublicKey, String signedData, String signature) {
        if (TextUtils.isEmpty(signedData) || TextUtils.isEmpty(base64PublicKey) ||
                TextUtils.isEmpty(signature)) {
            Log.e(TAG, "Purchase verification failed: missing data.");
            return false;
        }

        PublicKey key = this.generatePublicKey(base64PublicKey);
        return this.verify(key, signedData, signature);
    }

    /**
     * Generates a PublicKey instance from a string containing the
     * Base64-encoded public key.
     *
     * @param encodedPublicKey Base64-encoded public key
     * @throws IllegalArgumentException if encodedPublicKey is invalid
     */
    public PublicKey generatePublicKey(String encodedPublicKey) {
        try {
            byte[] decodedKey = Base64.decode(encodedPublicKey);
            KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
            return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (InvalidKeySpecException e) {
            Log.e(TAG, "Invalid key specification.");
            throw new IllegalArgumentException(e);
        } catch (Base64DecoderException e) {
            Log.e(TAG, "Base64 decoding failed.");
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * Verifies that the signature from the server matches the computed
     * signature on the data.  Returns true if the data is correctly signed.
     *
     * @param publicKey public key associated with the developer account
     * @param signedData signed data from server
     * @param signature server signature
     * @return true if the data and signature match
     */
    public boolean verify(PublicKey publicKey, String signedData, String signature) {
        Signature sig;
        try {
            sig = Signature.getInstance(SIGNATURE_ALGORITHM);
            sig.initVerify(publicKey);
            sig.update(signedData.getBytes());
            if (!sig.verify(Base64.decode(signature))) {
                Log.e(TAG, "Signature verification failed.");
                return false;
            }
            return true;
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "NoSuchAlgorithmException.");
        } catch (InvalidKeyException e) {
            Log.e(TAG, "Invalid key specification.");
        } catch (SignatureException e) {
            Log.e(TAG, "Signature exception.");
        } catch (Base64DecoderException e) {
            Log.e(TAG, "Base64 decoding failed.");
        }
        return false;
    }

    public boolean verifyPurchase(String signatureBase64, PurchaseResult purchaseResult) {
        return verifyPurchase(signatureBase64, purchaseResult.getPurchaseData(), purchaseResult.getDataSignature());
    }
}




Java Source Code List

com.techery.droid.billings.AbstractController.java
com.techery.droid.billings.BillingConfig.java
com.techery.droid.billings.BillingInitializationController.java
com.techery.droid.billings.BillingManager.java
com.techery.droid.billings.BillingProcessController.java
com.techery.droid.billings.Constants.java
com.techery.droid.billings.annotations.Billing.java
com.techery.droid.billings.events.IabEvent.java
com.techery.droid.billings.events.IabSetupErrorEvent.java
com.techery.droid.billings.events.IabSetupFinishedEvent.java
com.techery.droid.billings.events.OnConsumeErrorEvent.java
com.techery.droid.billings.events.OnConsumeEvent.java
com.techery.droid.billings.events.OnConsumeFinishedEvent.java
com.techery.droid.billings.events.OnConsumeMultiFinishedEvent.java
com.techery.droid.billings.events.OnIabPurchaseErrorEvent.java
com.techery.droid.billings.events.OnIabPurchaseEvent.java
com.techery.droid.billings.events.OnIabPurchaseFinishedEvent.java
com.techery.droid.billings.events.QueryInventoryErrorEvent.java
com.techery.droid.billings.events.QueryInventoryEvent.java
com.techery.droid.billings.events.QueryInventoryFinishedEvent.java
com.techery.droid.billings.events.UninitializedHelperAccess.java
com.techery.droid.billings.models.BillableItem.java
com.techery.droid.billings.models.BillingFeatureSupportingResult.java
com.techery.droid.billings.models.ConsumableItem.java
com.techery.droid.billings.models.IabException.java
com.techery.droid.billings.models.IabResult.java
com.techery.droid.billings.models.Inventory.java
com.techery.droid.billings.models.PurchaseResult.java
com.techery.droid.billings.models.Purchase.java
com.techery.droid.billings.models.SkuDetails.java
com.techery.droid.billings.models.Subscription.java
com.techery.droid.billings.modules.BillingModule.java
com.techery.droid.billings.modules.BillingServiceModule.java
com.techery.droid.billings.tasks.BillingTask.java
com.techery.droid.billings.tasks.ConsumeTask.java
com.techery.droid.billings.tasks.QueryInventoryTask.java
com.techery.droid.billings.utils.Base64DecoderException.java
com.techery.droid.billings.utils.Base64.java
com.techery.droid.billings.utils.BillingSecurity.java
com.techery.droid.billings.utils.BillingSupportingChecker.java
com.techery.droid.billings.utils.ResponseHelper.java