Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

CordovaClientAuth.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.silkimen.cordovahttp;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.security.KeyChain;
  5. import android.security.KeyChainAliasCallback;
  6. import android.util.Log;
  7. import java.io.ByteArrayInputStream;
  8. import java.io.InputStream;
  9. import java.net.URI;
  10. import java.security.KeyStore;
  11. import java.security.PrivateKey;
  12. import java.security.cert.X509Certificate;
  13. import javax.net.ssl.KeyManager;
  14. import javax.net.ssl.KeyManagerFactory;
  15. import org.apache.cordova.CallbackContext;
  16. import com.silkimen.http.KeyChainKeyManager;
  17. import com.silkimen.http.TLSConfiguration;
  18. class CordovaClientAuth implements Runnable, KeyChainAliasCallback {
  19. private static final String TAG = "Cordova-Plugin-HTTP";
  20. private String mode;
  21. private String aliasString;
  22. private byte[] rawPkcs;
  23. private String pkcsPassword;
  24. private Activity activity;
  25. private Context context;
  26. private TLSConfiguration tlsConfiguration;
  27. private CallbackContext callbackContext;
  28. public CordovaClientAuth(final String mode, final String aliasString, final byte[] rawPkcs,
  29. final String pkcsPassword, final Activity activity, final Context context, final TLSConfiguration configContainer,
  30. final CallbackContext callbackContext) {
  31. this.mode = mode;
  32. this.aliasString = aliasString;
  33. this.rawPkcs = rawPkcs;
  34. this.pkcsPassword = pkcsPassword;
  35. this.activity = activity;
  36. this.tlsConfiguration = configContainer;
  37. this.context = context;
  38. this.callbackContext = callbackContext;
  39. }
  40. @Override
  41. public void run() {
  42. if ("systemstore".equals(this.mode)) {
  43. this.loadFromSystemStore();
  44. } else if ("buffer".equals(this.mode)) {
  45. this.loadFromBuffer();
  46. } else {
  47. this.disableClientAuth();
  48. }
  49. }
  50. private void loadFromSystemStore() {
  51. if (this.aliasString == null) {
  52. KeyChain.choosePrivateKeyAlias(this.activity, this, null, null, null, -1, null);
  53. } else {
  54. this.alias(this.aliasString);
  55. }
  56. }
  57. private void loadFromBuffer() {
  58. try {
  59. KeyStore keyStore = KeyStore.getInstance("PKCS12");
  60. String keyManagerFactoryAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
  61. KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(keyManagerFactoryAlgorithm);
  62. ByteArrayInputStream stream = new ByteArrayInputStream(this.rawPkcs);
  63. keyStore.load(stream, this.pkcsPassword.toCharArray());
  64. keyManagerFactory.init(keyStore, this.pkcsPassword.toCharArray());
  65. this.tlsConfiguration.setKeyManagers(keyManagerFactory.getKeyManagers());
  66. this.callbackContext.success();
  67. } catch (Exception e) {
  68. Log.e(TAG, "Couldn't load given PKCS12 container for authentication", e);
  69. this.callbackContext.error("Couldn't load given PKCS12 container for authentication");
  70. }
  71. }
  72. private void disableClientAuth() {
  73. this.tlsConfiguration.setKeyManagers(null);
  74. this.callbackContext.success();
  75. }
  76. @Override
  77. public void alias(final String alias) {
  78. try {
  79. if (alias == null) {
  80. throw new Exception("Couldn't get a consent for private key access");
  81. }
  82. PrivateKey key = KeyChain.getPrivateKey(this.context, alias);
  83. X509Certificate[] chain = KeyChain.getCertificateChain(this.context, alias);
  84. KeyManager keyManager = new KeyChainKeyManager(alias, key, chain);
  85. this.tlsConfiguration.setKeyManagers(new KeyManager[] { keyManager });
  86. this.callbackContext.success(alias);
  87. } catch (Exception e) {
  88. Log.e(TAG, "Couldn't load private key and certificate pair with given alias \"" + alias + "\" for authentication",
  89. e);
  90. this.callbackContext.error(
  91. "Couldn't load private key and certificate pair with given alias \"" + alias + "\" for authentication");
  92. }
  93. }
  94. }