/**
   * The action will take after another activity reply to this one
   *
   * @param requestCode -- the key to get result
   * @param resultCode -- result state
   * @param data -- intent to get data
   */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i(thisClass, "in FileChooser result");
    if (resultCode == RESULT_OK) {
      switch (requestCode) {
        case PICK_FILE_REQUEST_CODE:
          Uri fileUri = data.getData();

          //                    if (BuildConfig.DEBUG) {
          //                FileUtility.deleteAllFiles(currentSelectedDir());
          //                    }

          // create a file save the content under the related folder
          String path = UriUtility.getPath(this, fileUri);
          String name = FileUtility.getNameFromPath(path);
          FileEncryption file;
          File saveFile;
          try {
            saveFile = fileChooserBL.getEncryptedFilePath(currentSelectedDir(), name);
            file = new FileEncryption(saveFile, path, fileUri.toString(), password);
          } catch (IOException | NoSuchAlgorithmException e) {
            Log.e(thisClass, "File write failed: " + e.toString());
            return;
          }
          try {
            boolean succ = file.encrypt();
            if (!succ) {
              Toast.makeText(
                      getApplicationContext(), getString(R.string.encryptFail), Toast.LENGTH_SHORT)
                  .show();
              return;
            } else {
              Toast.makeText(
                      getApplicationContext(), getString(R.string.encryptSucc), Toast.LENGTH_SHORT)
                  .show();
            }
            if (!file.deleteOriginal()) {
              throw new EncryptionException("fail to delete original file");
            }
          } catch (IOException e) {
            Log.e(thisClass, "File encrypt failed: " + e.toString());
          } catch (NoSuchAlgorithmException | UnrecoverableEntryException | InvalidKeyException e) {
            Log.e(thisClass, " failed: " + e.toString());
          }
          // update the view as if it is clicked
          folderFragmentClick(fatherIndex);
          break;
      }
    } else {
      Log.e(thisClass, " fail to start activity " + data);
    }
  }
Esempio n. 2
0
  private static void validateSigner(
      final ConcurrentContentSigner signer,
      final X509Certificate[] certificateChain,
      final String signerType,
      final String signerConf)
      throws SignerException {
    X509Certificate cert = signer.getCertificate();
    if (certificateChain == null) {
      return;
    }

    String signatureAlgoName;
    try {
      signatureAlgoName = AlgorithmUtil.getSignatureAlgoName(signer.getAlgorithmIdentifier());
    } catch (NoSuchAlgorithmException e) {
      throw new SignerException(e.getMessage(), e);
    }

    ContentSigner csigner;
    try {
      csigner = signer.borrowContentSigner();
    } catch (NoIdleSignerException e) {
      throw new SignerException(e.getMessage(), e);
    }

    try {
      byte[] dummyContent = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      Signature verifier = Signature.getInstance(signatureAlgoName, "BC");

      OutputStream signatureStream = csigner.getOutputStream();
      signatureStream.write(dummyContent);
      byte[] signatureValue = csigner.getSignature();

      verifier.initVerify(cert.getPublicKey());
      verifier.update(dummyContent);
      boolean valid = verifier.verify(signatureValue);
      if (valid == false) {
        String subject = X509Util.getRFC4519Name(cert.getSubjectX500Principal());

        StringBuilder sb = new StringBuilder();
        sb.append("key and certificate not match. ");
        sb.append("key type='").append(signerType).append("'; ");

        CmpUtf8Pairs keyValues = new CmpUtf8Pairs(signerConf);
        String pwd = keyValues.getValue("password");
        if (pwd != null) {
          keyValues.putUtf8Pair("password", "****");
        }
        keyValues.putUtf8Pair("algo", signatureAlgoName);
        sb.append("conf='").append(keyValues.getEncoded()).append("', ");
        sb.append("certificate subject='").append(subject).append("'");

        throw new SignerException(sb.toString());
      }
    } catch (IOException
        | NoSuchAlgorithmException
        | InvalidKeyException
        | SignatureException
        | NoSuchProviderException e) {
      throw new SignerException(e.getMessage(), e);
    } finally {
      if (csigner != null) {
        signer.returnContentSigner(csigner);
      }
    }
  }
Esempio n. 3
0
  public static synchronized void setGlobalSSLAuth(
      String keypath, String keypassword, String trustpath, String trustpassword) {
    // load the stores if defined
    try {
      if (trustpath != null && trustpassword != null) {
        truststore = KeyStore.getInstance(KeyStore.getDefaultType());
        try (FileInputStream instream = new FileInputStream(new File(trustpath))) {
          truststore.load(instream, trustpassword.toCharArray());
        }
      } else truststore = null;
      if (keypath != null && keypassword != null) {
        keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        try (FileInputStream instream = new FileInputStream(new File(keypath))) {
          keystore.load(instream, keypassword.toCharArray());
        }
      } else keystore = null;
    } catch (IOException | NoSuchAlgorithmException | CertificateException | KeyStoreException ex) {
      log.error("Illegal -D keystore parameters: " + ex.getMessage());
      truststore = null;
      keystore = null;
    }
    try {
      // set up the context
      SSLContext scxt = null;
      if (IGNORECERTS) {
        scxt = SSLContext.getInstance("TLS");
        TrustManager[] trust_mgr =
            new TrustManager[] {
              new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                  return null;
                }

                public void checkClientTrusted(X509Certificate[] certs, String t) {}

                public void checkServerTrusted(X509Certificate[] certs, String t) {}
              }
            };
        scxt.init(
            null, // key manager
            trust_mgr, // trust manager
            new SecureRandom()); // random number generator
      } else {
        SSLContextBuilder sslbuilder = SSLContexts.custom();
        TrustStrategy strat = new LooseTrustStrategy();
        if (truststore != null) sslbuilder.loadTrustMaterial(truststore, strat);
        else sslbuilder.loadTrustMaterial(strat);
        sslbuilder.loadTrustMaterial(truststore, new LooseTrustStrategy());
        if (keystore != null) sslbuilder.loadKeyMaterial(keystore, keypassword.toCharArray());
        scxt = sslbuilder.build();
      }
      globalsslfactory = new SSLConnectionSocketFactory(scxt, new NoopHostnameVerifier());

      RegistryBuilder rb = RegistryBuilder.<ConnectionSocketFactory>create();
      rb.register("https", globalsslfactory);
      sslregistry = rb.build();
    } catch (KeyStoreException
        | NoSuchAlgorithmException
        | KeyManagementException
        | UnrecoverableEntryException e) {
      log.error("Failed to set key/trust store(s): " + e.getMessage());
      sslregistry = null;
      globalsslfactory = null;
    }
  }