Search in sources :

Example 1 with Base64DecoderException

use of com.google.android.vending.licensing.util.Base64DecoderException in project Klyph by jonathangerbaud.

the class LicenseChecker method generatePublicKey.

/**
 * 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
 */
private static 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) {
        // This won't happen in an Android-compatible environment.
        throw new RuntimeException(e);
    } catch (Base64DecoderException e) {
        Log.e(TAG, "Could not decode from Base64.");
        throw new IllegalArgumentException(e);
    } catch (InvalidKeySpecException e) {
        Log.e(TAG, "Invalid key specification.");
        throw new IllegalArgumentException(e);
    }
}
Also used : Base64DecoderException(com.google.android.vending.licensing.util.Base64DecoderException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 2 with Base64DecoderException

use of com.google.android.vending.licensing.util.Base64DecoderException in project Klyph by jonathangerbaud.

the class LicenseValidator method verify.

/**
 * Verifies the response from server and calls appropriate callback method.
 *
 * @param publicKey public key associated with the developer account
 * @param responseCode server response code
 * @param signedData signed data from server
 * @param signature server signature
 */
public void verify(PublicKey publicKey, int responseCode, String signedData, String signature) {
    String userId = null;
    // Skip signature check for unsuccessful requests
    ResponseData data = null;
    if (responseCode == LICENSED || responseCode == NOT_LICENSED || responseCode == LICENSED_OLD_KEY) {
        // Verify signature.
        try {
            Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM);
            sig.initVerify(publicKey);
            sig.update(signedData.getBytes());
            if (!sig.verify(Base64.decode(signature))) {
                Log.e(TAG, "Signature verification failed.");
                handleInvalidResponse();
                return;
            }
        } catch (NoSuchAlgorithmException e) {
            // This can't happen on an Android compatible device.
            throw new RuntimeException(e);
        } catch (InvalidKeyException e) {
            handleApplicationError(LicenseCheckerCallback.ERROR_INVALID_PUBLIC_KEY);
            return;
        } catch (SignatureException e) {
            throw new RuntimeException(e);
        } catch (Base64DecoderException e) {
            Log.e(TAG, "Could not Base64-decode signature.");
            handleInvalidResponse();
            return;
        }
        // Parse and validate response.
        try {
            data = ResponseData.parse(signedData);
        } catch (IllegalArgumentException e) {
            Log.e(TAG, "Could not parse response.");
            handleInvalidResponse();
            return;
        }
        if (data.responseCode != responseCode) {
            Log.e(TAG, "Response codes don't match.");
            handleInvalidResponse();
            return;
        }
        if (data.nonce != mNonce) {
            Log.e(TAG, "Nonce doesn't match.");
            handleInvalidResponse();
            return;
        }
        if (!data.packageName.equals(mPackageName)) {
            Log.e(TAG, "Package name doesn't match.");
            handleInvalidResponse();
            return;
        }
        if (!data.versionCode.equals(mVersionCode)) {
            Log.e(TAG, "Version codes don't match.");
            handleInvalidResponse();
            return;
        }
        // Application-specific user identifier.
        userId = data.userId;
        if (TextUtils.isEmpty(userId)) {
            Log.e(TAG, "User identifier is empty.");
            handleInvalidResponse();
            return;
        }
    }
    switch(responseCode) {
        case LICENSED:
        case LICENSED_OLD_KEY:
            int limiterResponse = mDeviceLimiter.isDeviceAllowed(userId);
            handleResponse(limiterResponse, data);
            break;
        case NOT_LICENSED:
            handleResponse(Policy.NOT_LICENSED, data);
            break;
        case ERROR_CONTACTING_SERVER:
            Log.w(TAG, "Error contacting licensing server.");
            handleResponse(Policy.RETRY, data);
            break;
        case ERROR_SERVER_FAILURE:
            Log.w(TAG, "An error has occurred on the licensing server.");
            handleResponse(Policy.RETRY, data);
            break;
        case ERROR_OVER_QUOTA:
            Log.w(TAG, "Licensing server is refusing to talk to this device, over quota.");
            handleResponse(Policy.RETRY, data);
            break;
        case ERROR_INVALID_PACKAGE_NAME:
            handleApplicationError(LicenseCheckerCallback.ERROR_INVALID_PACKAGE_NAME);
            break;
        case ERROR_NON_MATCHING_UID:
            handleApplicationError(LicenseCheckerCallback.ERROR_NON_MATCHING_UID);
            break;
        case ERROR_NOT_MARKET_MANAGED:
            handleApplicationError(LicenseCheckerCallback.ERROR_NOT_MARKET_MANAGED);
            break;
        default:
            Log.e(TAG, "Unknown response code for license check.");
            handleInvalidResponse();
    }
}
Also used : Base64DecoderException(com.google.android.vending.licensing.util.Base64DecoderException) Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 3 with Base64DecoderException

use of com.google.android.vending.licensing.util.Base64DecoderException in project baker-android by bakerframework.

the class LicenseChecker method generatePublicKey.

/**
 * 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
 */
private static 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) {
        // This won't happen in an Android-compatible environment.
        throw new RuntimeException(e);
    } catch (Base64DecoderException e) {
        Log.e(TAG, "Could not decode from Base64.");
        throw new IllegalArgumentException(e);
    } catch (InvalidKeySpecException e) {
        Log.e(TAG, "Invalid key specification.");
        throw new IllegalArgumentException(e);
    }
}
Also used : Base64DecoderException(com.google.android.vending.licensing.util.Base64DecoderException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 4 with Base64DecoderException

use of com.google.android.vending.licensing.util.Base64DecoderException in project Klyph by jonathangerbaud.

the class LicenseChecker method checkAccess.

/**
 * Checks if the user should have access to the app.  Binds the service if necessary.
 * <p>
 * NOTE: This call uses a trivially obfuscated string (base64-encoded).  For best security,
 * we recommend obfuscating the string that is passed into bindService using another method
 * of your own devising.
 * <p>
 * source string: "com.android.vending.licensing.ILicensingService"
 * <p>
 * @param callback
 */
public synchronized void checkAccess(LicenseCheckerCallback callback) {
    // Market.
    if (mPolicy.allowAccess()) {
        Log.i(TAG, "Using cached license response");
        callback.allow(Policy.LICENSED);
    } else {
        LicenseValidator validator = new LicenseValidator(mPolicy, new NullDeviceLimiter(), callback, generateNonce(), mPackageName, mVersionCode);
        if (mService == null) {
            Log.i(TAG, "Binding to licensing service.");
            try {
                boolean bindResult = mContext.bindService(new Intent(new String(Base64.decode("Y29tLmFuZHJvaWQudmVuZGluZy5saWNlbnNpbmcuSUxpY2Vuc2luZ1NlcnZpY2U="))), // ServiceConnection.
                this, Context.BIND_AUTO_CREATE);
                if (bindResult) {
                    mPendingChecks.offer(validator);
                } else {
                    Log.e(TAG, "Could not bind to service.");
                    handleServiceConnectionError(validator);
                }
            } catch (SecurityException e) {
                callback.applicationError(LicenseCheckerCallback.ERROR_MISSING_PERMISSION);
            } catch (Base64DecoderException e) {
                e.printStackTrace();
            }
        } else {
            mPendingChecks.offer(validator);
            runChecks();
        }
    }
}
Also used : Base64DecoderException(com.google.android.vending.licensing.util.Base64DecoderException) Intent(android.content.Intent)

Example 5 with Base64DecoderException

use of com.google.android.vending.licensing.util.Base64DecoderException in project baker-android by bakerframework.

the class LicenseChecker method checkAccess.

/**
 * Checks if the user should have access to the app.  Binds the service if necessary.
 * <p>
 * NOTE: This call uses a trivially obfuscated string (base64-encoded).  For best security,
 * we recommend obfuscating the string that is passed into bindService using another method
 * of your own devising.
 * <p>
 * source string: "com.android.vending.licensing.ILicensingService"
 * <p>
 * @param callback
 */
public synchronized void checkAccess(LicenseCheckerCallback callback) {
    // Market.
    if (mPolicy.allowAccess()) {
        Log.i(TAG, "Using cached license response");
        callback.allow(Policy.LICENSED);
    } else {
        LicenseValidator validator = new LicenseValidator(mPolicy, new NullDeviceLimiter(), callback, generateNonce(), mPackageName, mVersionCode);
        if (mService == null) {
            Log.i(TAG, "Binding to licensing service.");
            try {
                boolean bindResult = mContext.bindService(new Intent(new String(Base64.decode("Y29tLmFuZHJvaWQudmVuZGluZy5saWNlbnNpbmcuSUxpY2Vuc2luZ1NlcnZpY2U="))), // ServiceConnection.
                this, Context.BIND_AUTO_CREATE);
                if (bindResult) {
                    mPendingChecks.offer(validator);
                } else {
                    Log.e(TAG, "Could not bind to service.");
                    handleServiceConnectionError(validator);
                }
            } catch (SecurityException e) {
                callback.applicationError(LicenseCheckerCallback.ERROR_MISSING_PERMISSION);
            } catch (Base64DecoderException e) {
                e.printStackTrace();
            }
        } else {
            mPendingChecks.offer(validator);
            runChecks();
        }
    }
}
Also used : Base64DecoderException(com.google.android.vending.licensing.util.Base64DecoderException) Intent(android.content.Intent)

Aggregations

Base64DecoderException (com.google.android.vending.licensing.util.Base64DecoderException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Intent (android.content.Intent)2 InvalidKeyException (java.security.InvalidKeyException)2 KeyFactory (java.security.KeyFactory)2 Signature (java.security.Signature)2 SignatureException (java.security.SignatureException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)2