/* (non-Javadoc)
   * @see org.eclipse.sequoyah.tfm.sign.core.extension.security.ISecurityManagement#importSignedCert(java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
   */
  public boolean importSignedCert(String certFile, IProgressMonitor monitor) throws SignException {

    boolean cmdSuccessful = true;
    initializeKeytool();

    String[] cmdArgs =
        keytool.generateImportSignedCertCmd(
            certFile, aliaskey, ksPasswrd, ksType, ksLocation, ksPasswrd, getConsoleEncoding());
    Process p = keytool.execute(cmdArgs);

    BufferedReader cmdOutputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String cmdOutput;
    try {
      while ((cmdOutput = cmdOutputStream.readLine()) != null) {

        if (cmdOutput.indexOf("error") >= 0) { // $NON-NLS-1$
          throw new SignException(
              NLS.bind(
                  Messages.SunSecurityManagement_defaultErrorMessage,
                  new String[] {
                    SignErrors.getErrorMessage(SignErrors.GENERIC_SECURITY_ERROR), cmdOutput
                  }));
        }
      }
    } catch (IOException ee) {
      throw new SignException(SignErrors.getErrorMessage(SignErrors.GENERIC_SECURITY_ERROR), ee);
    }
    return cmdSuccessful;
  }
  /* (non-Javadoc)
   * @see org.eclipse.sequoyah.tfm.sign.core.extension.security.ISecurityManagement#openKeyStore(java.lang.String, java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
   */
  public String[] openKeyStore(IPath keyStore, String storePass, IProgressMonitor monitor)
      throws SignException {
    monitor.beginTask(Messages.SunSecurityManagement_Opening_key_store, 100);
    monitor.worked(10);

    initializeKeytool();

    String[] cmdArgs =
        keytool.generateOpenKeyStoreCmd(ksType, keyStore, storePass, getConsoleEncoding());
    Process p = keytool.execute(cmdArgs);

    BufferedReader cmdOutputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String cmdOutput;
    List<String> aliases = new ArrayList<String>();

    try {
      while ((cmdOutput = cmdOutputStream.readLine()) != null) {
        monitor.subTask(cmdOutput);
        monitor.worked(25);

        if (cmdOutput.toLowerCase().indexOf("error") >= 0) { // $NON-NLS-1$
          monitor.done();
          if (cmdOutput.toLowerCase().indexOf("invalid keystore format") >= 0) { // $NON-NLS-1$
            throw new SignException(
                NLS.bind(
                    Messages.SunSecurityManagement_defaultErrorMessage,
                    new String[] {
                      SignErrors.getErrorMessage(SignErrors.SECURITY_BAD_KEY_TYPE), cmdOutput
                    }));
          } else if (cmdOutput.toLowerCase().indexOf("password was incorrect")
              >= 0) { //$NON-NLS-1$
            throw new SignException(
                NLS.bind(
                    Messages.SunSecurityManagement_defaultErrorMessage,
                    new String[] {
                      SignErrors.getErrorMessage(SignErrors.SECURITY_BAD_KEYSTORE_OR_PASSWORD),
                      cmdOutput
                    }));
          } else {
            throw new SignException(
                NLS.bind(
                    Messages.SunSecurityManagement_defaultErrorMessage,
                    new String[] {
                      SignErrors.getErrorMessage(SignErrors.GENERIC_SECURITY_ERROR), cmdOutput
                    }));
          }

        } else if (cmdOutput.indexOf(",") >= 0) { // $NON-NLS-1$
          StringTokenizer strtok = new StringTokenizer(cmdOutput, ".,"); // $NON-NLS-1$
          aliases.add(strtok.nextToken());
        }
      }
    } catch (IOException ee) {
      throw new SignException(SignErrors.getErrorMessage(SignErrors.GENERIC_SECURITY_ERROR), ee);
    }
    return aliases.toArray(new String[aliases.size()]);
  }
  /* (non-Javadoc)
   * @see org.eclipse.sequoyah.tfm.sign.core.extension.security.ISecurityManagement#getCertificateInfo(org.eclipse.core.runtime.IProgressMonitor)
   */
  public String getCertificateInfo(IProgressMonitor monitor) throws SignException {

    String certInfo = ""; // $NON-NLS-1$

    if ((aliaskey != null) && (aliaskey.length() > 0)) {

      try {
        initializeKeytool();

        String[] cmdArgs =
            keytool.generateDisplayCertifcates(
                aliaskey, ksType, ksLocation, ksPasswrd, getConsoleEncoding());
        Process p = keytool.execute(cmdArgs);

        BufferedReader cmdOutputStream =
            new BufferedReader(new InputStreamReader(p.getInputStream()));
        monitor.worked(20);

        String cmdOutput;

        while ((cmdOutput = cmdOutputStream.readLine()) != null) {

          if (cmdOutput.toLowerCase().indexOf("error") >= 0) { // $NON-NLS-1$
            throw new SignException(
                SignErrors.getErrorMessage(SignErrors.GENERIC_SECURITY_ERROR)
                    + " "
                    + cmdOutput); //$NON-NLS-1$
          } else if (cmdOutput.length() >= 0) {
            certInfo = certInfo + cmdOutput;
          }
        }

      } catch (IOException ee) {
        certInfo = ""; // $NON-NLS-1$
        throw new SignException(SignErrors.getErrorMessage(SignErrors.GENERIC_SECURITY_ERROR), ee);
      } catch (Exception e) {
        certInfo = ""; // $NON-NLS-1$
        throw new SignException(SignErrors.getErrorMessage(SignErrors.GENERIC_SECURITY_ERROR), e);
      }
    } // if aliaskey

    return certInfo;
  }
  /* (non-Javadoc)
   * @see org.eclipse.sequoyah.tfm.sign.core.extension.security.ISecurityManagement#createNewKey(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, org.eclipse.core.runtime.IProgressMonitor)
   */
  public boolean createNewKey(
      String alias,
      String commonName,
      String orgUnit,
      String orgName,
      String localityName,
      String stateName,
      String country,
      IProgressMonitor monitor)
      throws SignException {

    monitor.beginTask(Messages.SunSecurityManagement_Creating_key_alias, 100);

    boolean cmdSuccessful = true;
    initializeKeytool();

    X500DName dName = new X500DName(commonName, orgUnit, orgName, localityName, stateName, country);

    String[] cmdArgs =
        keytool.generateNewKeyCmd(
            dName,
            "RSA",
            "SHA1withRSA",
            ksCertfValidity,
            alias,
            ksPasswrd,
            ksType,
            ksLocation,
            ksPasswrd,
            getConsoleEncoding()); //$NON-NLS-1$ //$NON-NLS-2$

    Process p = keytool.execute(cmdArgs);
    monitor.worked(30);

    BufferedReader cmdOutputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String cmdOutput;
    try {
      while ((cmdOutput = cmdOutputStream.readLine()) != null) {
        monitor.worked(40);
        if (cmdOutput.toLowerCase().indexOf("error") >= 0) { // $NON-NLS-1$

          monitor.done();
          if (cmdOutput.toLowerCase().indexOf("alias <" + alias + "> already exists")
              >= 0) { //$NON-NLS-1$ //$NON-NLS-2$
            throw new SignException(
                NLS.bind(
                    Messages.SunSecurityManagement_defaultErrorMessage,
                    new String[] {
                      SignErrors.getErrorMessage(SignErrors.SECURITY_ALIAS_DUPLICATE), cmdOutput
                    }));
          } else {
            throw new SignException(
                NLS.bind(
                    Messages.SunSecurityManagement_defaultErrorMessage,
                    new String[] {
                      SignErrors.getErrorMessage(SignErrors.GENERIC_SECURITY_ERROR), cmdOutput
                    }));
          }
        }
      }
    } catch (IOException ee) {
      throw new SignException(SignErrors.getErrorMessage(SignErrors.GENERIC_SECURITY_ERROR), ee);
    }
    monitor.worked(100);
    monitor.done();

    return cmdSuccessful;
  }