Android Open Source - LocalStorage Security






From Project

Back to project page LocalStorage.

License

The source code is released under:

BSD 3-Clause License Copyright (c) 2013, Ian Lake All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following cond...

If you think the Android project LocalStorage 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

/* Copyright (c) 2012 Google Inc.
 *// w  w w.j  av a2  s  .c  o  m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ianhanniballake.localstorage.inappbilling;

import android.text.TextUtils;
import android.util.Log;

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;

/**
 * Security-related methods. For a secure implementation, all of this code should be implemented on a server that
 * communicates with the application on the device. For the sake of simplicity and clarity of this example, this code is
 * included here and is executed on the device. If you must verify the purchases on the phone, you should obfuscate this
 * code to make it harder for an attacker to replace the code with stubs that treat all purchases as verified.
 */
@SuppressWarnings("javadoc")
public class Security {
    private static final String KEY_FACTORY_ALGORITHM = "RSA";
    private static final String SIGNATURE_ALGORITHM = "SHA1withRSA";
    private static final String TAG = "IABUtil/Security";

    /**
     * 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 static PublicKey generatePublicKey(final String encodedPublicKey) {
        try {
            final byte[] decodedKey = Base64.decode(encodedPublicKey);
            final KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
            return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
        } catch (final NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (final InvalidKeySpecException e) {
            Log.e(TAG, "Invalid key specification.");
            throw new IllegalArgumentException(e);
        } catch (final 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 static boolean verify(final PublicKey publicKey, final String signedData, final 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 (final NoSuchAlgorithmException e) {
            Log.e(TAG, "NoSuchAlgorithmException.");
        } catch (final InvalidKeyException e) {
            Log.e(TAG, "Invalid key specification.");
        } catch (final SignatureException e) {
            Log.e(TAG, "Signature exception.");
        } catch (final Base64DecoderException e) {
            Log.e(TAG, "Base64 decoding failed.");
        }
        return false;
    }

    /**
     * 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 static boolean verifyPurchase(final String base64PublicKey, final String signedData, final String signature) {
        if (TextUtils.isEmpty(signedData) || TextUtils.isEmpty(base64PublicKey) ||
                TextUtils.isEmpty(signature)) {
            Log.e(TAG, "Purchase verification failed: missing data.");
            return false;
        }
        PublicKey key = Security.generatePublicKey(base64PublicKey);
        return Security.verify(key, signedData, signature);
    }
}




Java Source Code List

com.ianhanniballake.localstorage.DonateActivity.java
com.ianhanniballake.localstorage.LocalStorageProvider.java
com.ianhanniballake.localstorage.MainActivity.java
com.ianhanniballake.localstorage.MediaAvailabilityBroadcastReceiver.java
com.ianhanniballake.localstorage.inappbilling.Base64DecoderException.java
com.ianhanniballake.localstorage.inappbilling.Base64.java
com.ianhanniballake.localstorage.inappbilling.Inventory.java
com.ianhanniballake.localstorage.inappbilling.Purchase.java
com.ianhanniballake.localstorage.inappbilling.Security.java
com.ianhanniballake.localstorage.inappbilling.SkuDetails.java