Created
January 20, 2016 20:27
-
-
Save MrOnlineCoder/03d3171a474cf02cee10 to your computer and use it in GitHub Desktop.
Updated LBL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
java.util.zip.CRC32 crc32 = new java.util.zip.CRC32(); | |
crc32.update(responseCode); | |
int transformedResponseCode = crc32.getValue(); | |
// ... put unrelated application code here ... | |
// crc32(LICENSED) == 3523407757 | |
if (transformedResponse == 3523407757) { | |
LicenseResponse limiterResponse = mDeviceLimiter.isDeviceAllowed(userId); | |
handleResponse(limiterResponse, data); | |
return; | |
} | |
// ... put unrelated application code here ... | |
// crc32(LICENSED_OLD_KEY) == 1007455905 | |
if (transformedResponseCode == 1007455905) { | |
LicenseResponse limiterResponse = mDeviceLimiter.isDeviceAllowed(userId); | |
handleResponse(limiterResponse, data); | |
return; | |
} | |
// ... put unrelated application code here ... | |
// crc32(NOT_LICENSED) == 2768625435 | |
if (transformedResponseCode == 2768625435): | |
userIsntLicensed(); | |
return; | |
} | |
if (responseCode == ERROR_CONTACTING_SERVER) { | |
Log.w(TAG, "Error contacting licensing server."); | |
handleResponse(Policy.RETRY, data); | |
return; | |
} | |
switch (responseCode) { | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Для чего это?