PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/AndroidBillingLibrary/src/net/robotmedia/billing/security/DefaultSignatureValidator.java

https://github.com/lujop/AndroidBillingLibrary
Java | 107 lines | 73 code | 11 blank | 23 comment | 6 complexity | 64ebe5b9392df1bd5dd77ec272dda736 MD5 | raw file
  1. /* Copyright 2011 Robot Media SL (http://www.robotmedia.net)
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package net.robotmedia.billing.security;
  16. import java.security.InvalidKeyException;
  17. import java.security.KeyFactory;
  18. import java.security.NoSuchAlgorithmException;
  19. import java.security.PublicKey;
  20. import java.security.Signature;
  21. import java.security.SignatureException;
  22. import java.security.spec.InvalidKeySpecException;
  23. import java.security.spec.X509EncodedKeySpec;
  24. import android.text.TextUtils;
  25. import android.util.Log;
  26. import net.robotmedia.billing.BillingController;
  27. import net.robotmedia.billing.utils.Base64;
  28. import net.robotmedia.billing.utils.Base64DecoderException;
  29. public class DefaultSignatureValidator implements ISignatureValidator {
  30. protected static final String KEY_FACTORY_ALGORITHM = "RSA";
  31. protected static final String SIGNATURE_ALGORITHM = "SHA1withRSA";
  32. /**
  33. * Generates a PublicKey instance from a string containing the
  34. * Base64-encoded public key.
  35. *
  36. * @param encodedPublicKey
  37. * Base64-encoded public key
  38. * @throws IllegalArgumentException
  39. * if encodedPublicKey is invalid
  40. */
  41. protected PublicKey generatePublicKey(String encodedPublicKey) {
  42. try {
  43. byte[] decodedKey = Base64.decode(encodedPublicKey);
  44. KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM);
  45. return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey));
  46. } catch (NoSuchAlgorithmException e) {
  47. throw new RuntimeException(e);
  48. } catch (InvalidKeySpecException e) {
  49. Log.e(BillingController.LOG_TAG, "Invalid key specification.");
  50. throw new IllegalArgumentException(e);
  51. } catch (Base64DecoderException e) {
  52. Log.e(BillingController.LOG_TAG, "Base64 decoding failed.");
  53. throw new IllegalArgumentException(e);
  54. }
  55. }
  56. private BillingController.IConfiguration configuration;
  57. public DefaultSignatureValidator(BillingController.IConfiguration configuration) {
  58. this.configuration = configuration;
  59. }
  60. protected boolean validate(PublicKey publicKey, String signedData, String signature) {
  61. Signature sig;
  62. try {
  63. sig = Signature.getInstance(SIGNATURE_ALGORITHM);
  64. sig.initVerify(publicKey);
  65. sig.update(signedData.getBytes());
  66. if (!sig.verify(Base64.decode(signature))) {
  67. Log.e(BillingController.LOG_TAG, "Signature verification failed.");
  68. return false;
  69. }
  70. return true;
  71. } catch (NoSuchAlgorithmException e) {
  72. Log.e(BillingController.LOG_TAG, "NoSuchAlgorithmException");
  73. } catch (InvalidKeyException e) {
  74. Log.e(BillingController.LOG_TAG, "Invalid key specification");
  75. } catch (SignatureException e) {
  76. Log.e(BillingController.LOG_TAG, "Signature exception");
  77. } catch (Base64DecoderException e) {
  78. Log.e(BillingController.LOG_TAG, "Base64 decoding failed");
  79. }
  80. return false;
  81. }
  82. @Override
  83. public boolean validate(String signedData, String signature) {
  84. final String publicKey;
  85. if (configuration == null || TextUtils.isEmpty(publicKey = configuration.getPublicKey())) {
  86. Log.w(BillingController.LOG_TAG, "Please set the public key or turn on debug mode");
  87. return false;
  88. }
  89. if (signedData == null) {
  90. Log.e(BillingController.LOG_TAG, "Data is null");
  91. return false;
  92. }
  93. PublicKey key = generatePublicKey(publicKey);
  94. return validate(key, signedData, signature);
  95. }
  96. }