@Before public void initFixture() { final byte[] SALT = new byte[] { 104, -12, 112, 82, -85, -10, -11, 61, 15, 54, 44, -66, -117, -89, -64, 110, -53, 123, 33 }; Context ctx = InstrumentationRegistry.getTargetContext(); String deviceId = Settings.Secure.getString( ctx.getContentResolver(), Settings.Secure.ANDROID_ID); p = new APKExpansionPolicy(ctx, new AESObfuscator(SALT, ctx.getPackageName(), deviceId)); }
@Before public void initFixture() { final byte[] SALT = new byte[] { 104, -12, 112, 82, -85, -10, -11, 61, 15, 54, 44, -66, -117, -89, -64, 110, -53, 123, 33 }; Context ctx = InstrumentationRegistry.getTargetContext(); String deviceId = Settings.Secure.getString( ctx.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID); p = new ServerManagedPolicy(ctx.getApplicationContext(), new AESObfuscator(SALT, ctx.getPackageName(), deviceId)); }
@Before public void setUp() throws Exception { mObfuscator = new AESObfuscator(SALT, PACKAGE, DEVICE); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.main); mStatusText = (TextView) findViewById(R.id.status_text); mCheckLicenseButton = (Button) findViewById(R.id.check_license_button); mCheckLicenseButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { doCheck(); } }); mHandler = new Handler(); // Try to use more data here. ANDROID_ID is a single point of attack. String deviceId = Secure.getString(getContentResolver(), Secure.ANDROID_ID); // Library calls this when it's done. mLicenseCheckerCallback = new MyLicenseCheckerCallback(); // Construct the LicenseChecker with a policy. mChecker = new LicenseChecker( this, new ServerManagedPolicy(this, new AESObfuscator(SALT, getPackageName(), deviceId)), BASE64_PUBLIC_KEY); doCheck(); }
public static boolean check(@NonNull AppCompatActivity context, @NonNull LicensingCallback cb) { final String key = Config.get().licensingPublicKey(); if (key == null || key.trim().isEmpty()) { LOG("License checking is disabled."); return true; } else if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(KEY_VALID, false)) { LOG("License checking has already been done, and the license check was successful."); return true; } if (BuildConfig.DEBUG) { Toast.makeText(context, "License checking is disabled for this debug build.", Toast.LENGTH_SHORT).show(); return true; } if (context.getContentResolver() == null) { if (!context.isFinishing()) context.finish(); return false; } mProgress = ProgressDialogFragment.show(context, R.string.checking_license); final String deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); // Library calls this when it's done. mLicenseCheckerCallback = new MyLicenseCheckerCallback(context, cb); // Construct the LicenseChecker with a policy. mChecker = new LicenseChecker( context, new ServerManagedPolicy(context, new AESObfuscator(getSalt(context), BuildConfig.APPLICATION_ID, deviceId)), key); mChecker.checkAccess(mLicenseCheckerCallback); return false; }
@Before public void initFixture() { final byte[] SALT = new byte[] { 104, -12, 112, 82, -85, -10, -11, 61, 15, 54, 44, -66, -117, -89, -64, 110, -53, 123, 33 }; // Prepare PreferenceObfuscator instance Context ctx = InstrumentationRegistry.getTargetContext(); sp = ctx.getSharedPreferences(filename, Context.MODE_PRIVATE); String deviceId = Settings.Secure.getString( ctx.getContentResolver(), Settings.Secure.ANDROID_ID); Obfuscator o = new AESObfuscator(SALT, ctx.getPackageName(), deviceId); op = new PreferenceObfuscator(sp, o); // Populate with test data op.putString("testString", "Hello world"); op.commit(); }
new AESObfuscator(getSALT(), mContext.getPackageName(), deviceId));
@Test public void unobfuscateAvoidBadPaddingException() throws Exception { // Length should be equal to the cipher block size, to make sure that all padding lengths // are accounted for. for (int length = 0; length < 255; length++) { char[] data = new char[length]; Arrays.fill(data, '0'); String input = String.valueOf(data); Log.d(TAG, "Input: (" + length + ")" + input); String obfuscated = mObfuscator.obfuscate(input, "testKey"); Obfuscator differentSalt = new AESObfuscator(new byte[]{1}, PACKAGE, DEVICE); try { differentSalt.unobfuscate(obfuscated, "testKey"); fail("Should have thrown ValidationException"); } catch (ValidationException expected) { } } }
@Test public void unobfuscateDifferentSalt() throws Exception { String obfuscated = mObfuscator.obfuscate("test", "testKey"); Obfuscator differentSalt = new AESObfuscator(new byte[]{1}, PACKAGE, DEVICE); try { differentSalt.unobfuscate(obfuscated, "testKey"); fail("Should have thrown ValidationException"); } catch (ValidationException expected) { } }
@Test public void unobfuscateDifferentPackage() throws Exception { String obfuscated = mObfuscator.obfuscate("test", "testKey"); Obfuscator differentPackage = new AESObfuscator(SALT, "package2", DEVICE); try { differentPackage.unobfuscate(obfuscated, "testKey"); fail("Should have thrown ValidationException"); } catch (ValidationException expected) { } }
@Test public void unobfuscateDifferentDevice() throws Exception { String obfuscated = mObfuscator.obfuscate("test", "testKey"); Obfuscator differentDevice = new AESObfuscator(SALT, PACKAGE, "device2"); try { differentDevice.unobfuscate(obfuscated, "testKey"); fail("Should have thrown ValidationException"); } catch (ValidationException expected) { } }
@Test public void unobfuscateDifferentKey() throws Exception { String obfuscated = mObfuscator.obfuscate("test", "testKey"); Obfuscator differentPackage = new AESObfuscator(SALT, "package2", DEVICE); try { differentPackage.unobfuscate(obfuscated, "notMyKey"); fail("Should have thrown ValidationException"); } catch (ValidationException expected) { } }
@Test public void obfuscateSame() throws Exception { String obfuscated = mObfuscator.obfuscate("test", "testKey"); assertEquals(obfuscated, mObfuscator.obfuscate("test", "testKey")); Obfuscator same = new AESObfuscator(SALT, PACKAGE, DEVICE); assertEquals(obfuscated, same.obfuscate("test", "testKey")); assertEquals("test", same.unobfuscate(obfuscated, "testKey")); }