/jamsMusicPlayer/src/main/java/com/jams/music/player/InAppBilling/Security.java

https://gitlab.com/manoj-makkuboy/JamsMusicPlayer · Java · 134 lines · 62 code · 9 blank · 63 comment · 3 complexity · a93b47d2f62231751819a196b66ff880 MD5 · raw file

  1. /*
  2. * Copyright (C) 2014 Saravan Pantham
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* Copyright (c) 2012 Google Inc.
  17. *
  18. * Licensed under the Apache License, Version 2.0 (the "License");
  19. * you may not use this file except in compliance with the License.
  20. * You may obtain a copy of the License at
  21. *
  22. * http://www.apache.org/licenses/LICENSE-2.0
  23. *
  24. * Unless required by applicable law or agreed to in writing, software
  25. * distributed under the License is distributed on an "AS IS" BASIS,
  26. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. * See the License for the specific language governing permissions and
  28. * limitations under the License.
  29. */
  30. package com.jams.music.player.InAppBilling;
  31. import java.security.InvalidKeyException;
  32. import java.security.KeyFactory;
  33. import java.security.NoSuchAlgorithmException;
  34. import java.security.PublicKey;
  35. import java.security.Signature;
  36. import java.security.SignatureException;
  37. import java.security.spec.InvalidKeySpecException;
  38. import java.security.spec.X509EncodedKeySpec;
  39. import android.text.TextUtils;
  40. import android.util.Log;
  41. /**
  42. * Security-related methods. For a secure implementation, all of this code
  43. * should be implemented on a server that communicates with the
  44. * application on the device. For the sake of simplicity and clarity of this
  45. * example, this code is included here and is executed on the device. If you
  46. * must verify the purchases on the phone, you should obfuscate this code to
  47. * make it harder for an attacker to replace the code with stubs that treat all
  48. * purchases as verified.
  49. */
  50. public class Security {
  51. private static final String TAG = "IABUtil/Security";
  52. private static final String KEY_FACTORY_ALGORITHM = "RSA";
  53. private static final String SIGNATURE_ALGORITHM = "SHA1withRSA";
  54. /**
  55. * Verifies that the data was signed with the given signature, and returns
  56. * the verified purchase. The data is in JSON format and signed
  57. * with a private key. The data also contains the {@link PurchaseState}
  58. * and product ID of the purchase.
  59. * @param base64PublicKey the base64-encoded public key to use for verifying.
  60. * @param signedData the signed JSON string (signed, not encrypted)
  61. * @param signature the signature for the data, signed with the private key
  62. */
  63. public static boolean verifyPurchase(String base64PublicKey, String signedData, String signature) {
  64. if (TextUtils.isEmpty(signedData) || TextUtils.isEmpty(base64PublicKey) ||
  65. TextUtils.isEmpty(signature)) {
  66. Log.e(TAG, "Purchase verification failed: missing data.");
  67. return false;
  68. }
  69. PublicKey key = Security.generatePublicKey(base64PublicKey);
  70. return Security.verify(key, signedData, signature);
  71. }
  72. /**
  73. * Generates a PublicKey instance from a string containing the
  74. * Base64-encoded public key.
  75. *
  76. * @param encodedPublicKey Base64-encoded public key
  77. * @throws IllegalArgumentException if encodedPublicKey is invalid
  78. */
  79. public static PublicKey generatePublicKey(String encodedPublicKey) {
  80. try {
  81. byte[] decodedKey = Base64.decode(encodedPublicKey);
  82. KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
  83. return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
  84. } catch (NoSuchAlgorithmException e) {
  85. throw new RuntimeException(e);
  86. } catch (InvalidKeySpecException e) {
  87. Log.e(TAG, "Invalid key specification.");
  88. throw new IllegalArgumentException(e);
  89. } catch (Base64DecoderException e) {
  90. Log.e(TAG, "Base64 decoding failed.");
  91. throw new IllegalArgumentException(e);
  92. }
  93. }
  94. /**
  95. * Verifies that the signature from the server matches the computed
  96. * signature on the data. Returns true if the data is correctly signed.
  97. *
  98. * @param publicKey public key associated with the developer account
  99. * @param signedData signed data from server
  100. * @param signature server signature
  101. * @return true if the data and signature match
  102. */
  103. public static boolean verify(PublicKey publicKey, String signedData, String signature) {
  104. Signature sig;
  105. try {
  106. sig = Signature.getInstance(SIGNATURE_ALGORITHM);
  107. sig.initVerify(publicKey);
  108. sig.update(signedData.getBytes());
  109. if (!sig.verify(Base64.decode(signature))) {
  110. Log.e(TAG, "Signature verification failed.");
  111. return false;
  112. }
  113. return true;
  114. } catch (NoSuchAlgorithmException e) {
  115. Log.e(TAG, "NoSuchAlgorithmException.");
  116. } catch (InvalidKeyException e) {
  117. Log.e(TAG, "Invalid key specification.");
  118. } catch (SignatureException e) {
  119. Log.e(TAG, "Signature exception.");
  120. } catch (Base64DecoderException e) {
  121. Log.e(TAG, "Base64 decoding failed.");
  122. }
  123. return false;
  124. }
  125. }