/** Do action. */
  @Override
  protected void doAction() {
    try {
      KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();

      KeyStoreState currentState = history.getCurrentState();
      KeyStoreState newState = currentState.createBasisForNextState(this);

      KeyStore keyStore = newState.getKeyStore();
      String alias = kseFrame.getSelectedEntryAlias();

      DGetAlias dGetAlias =
          new DGetAlias(
              frame, res.getString("RenameTrustedCertificateAction.NewEntryAlias.Title"), alias);
      dGetAlias.setLocationRelativeTo(frame);
      dGetAlias.setVisible(true);
      String newAlias = dGetAlias.getAlias();

      if (newAlias == null) {
        return;
      }

      if (newAlias.equalsIgnoreCase(alias)) {
        JOptionPane.showMessageDialog(
            frame,
            MessageFormat.format(
                res.getString("RenameTrustedCertificateAction.RenameAliasIdentical.message"),
                alias),
            res.getString("RenameTrustedCertificateAction.RenameEntry.Title"),
            JOptionPane.WARNING_MESSAGE);
        return;
      }

      if (keyStore.containsAlias(newAlias)) {
        String message =
            MessageFormat.format(
                res.getString("RenameTrustedCertificateAction.OverWriteEntry.message"), newAlias);

        int selected =
            JOptionPane.showConfirmDialog(
                frame,
                message,
                res.getString("RenameTrustedCertificateAction.RenameEntry.Title"),
                JOptionPane.YES_NO_OPTION);
        if (selected != JOptionPane.YES_OPTION) {
          return;
        }

        keyStore.deleteEntry(newAlias);
        newState.removeEntryPassword(newAlias);
      }

      Certificate cert = keyStore.getCertificate(alias);
      keyStore.setCertificateEntry(newAlias, cert);

      keyStore.deleteEntry(alias);

      currentState.append(newState);

      kseFrame.updateControls(true);
    } catch (Exception ex) {
      DError.displayError(frame, ex);
    }
  }
コード例 #2
0
  /** Generate a secret key in the currently opened KeyStore. */
  public void generateSecret() {
    try {
      int secretKeySize = applicationSettings.getGenerateSecretKeySize();
      SecretKeyType secretKeyType = applicationSettings.getGenerateSecretKeyType();

      DGenerateSecretKey dGenerateSecretKey =
          new DGenerateSecretKey(frame, secretKeyType, secretKeySize);
      dGenerateSecretKey.setLocationRelativeTo(frame);
      dGenerateSecretKey.setVisible(true);

      if (!dGenerateSecretKey.isSuccessful()) {
        return;
      }

      secretKeySize = dGenerateSecretKey.getSecretKeySize();
      secretKeyType = dGenerateSecretKey.getSecretKeyType();

      applicationSettings.setGenerateSecretKeySize(secretKeySize);
      applicationSettings.setGenerateSecretKeyType(secretKeyType);

      SecretKey secretKey = SecretKeyUtil.generateSecretKey(secretKeyType, secretKeySize);

      KeyStoreHistory history = kseFrame.getActiveKeyStoreHistory();

      KeyStoreState currentState = history.getCurrentState();
      KeyStoreState newState = currentState.createBasisForNextState(this);

      KeyStore keyStore = newState.getKeyStore();

      DGetAlias dGetAlias =
          new DGetAlias(
              frame, res.getString("GenerateSecretKeyAction.NewSecretKeyEntryAlias.Title"), null);
      dGetAlias.setLocationRelativeTo(frame);
      dGetAlias.setVisible(true);
      String alias = dGetAlias.getAlias();

      if (alias == null) {
        return;
      }

      if (keyStore.containsAlias(alias)) {
        String message =
            MessageFormat.format(
                res.getString("GenerateSecretKeyAction.OverWriteEntry.message"), alias);

        int selected =
            JOptionPane.showConfirmDialog(
                frame,
                message,
                res.getString("GenerateSecretKeyAction.NewSecretKeyEntryAlias.Title"),
                JOptionPane.YES_NO_OPTION);
        if (selected != JOptionPane.YES_OPTION) {
          return;
        }
      }

      Password password = new Password((char[]) null);
      KeyStoreType type = KeyStoreType.resolveJce(keyStore.getType());

      if (type.hasEntryPasswords()) {
        DGetNewPassword dGetNewPassword =
            new DGetNewPassword(
                frame,
                res.getString("GenerateSecretKeyAction.NewSecretKeyEntryPassword.Title"),
                applicationSettings.getPasswordQualityConfig());
        dGetNewPassword.setLocationRelativeTo(frame);
        dGetNewPassword.setVisible(true);
        password = dGetNewPassword.getPassword();

        if (password == null) {
          return;
        }
      }

      if (keyStore.containsAlias(alias)) {
        keyStore.deleteEntry(alias);
        newState.removeEntryPassword(alias);
      }

      keyStore.setKeyEntry(alias, secretKey, password.toCharArray(), null);
      newState.setEntryPassword(alias, password);

      currentState.append(newState);

      kseFrame.updateControls(true);

      JOptionPane.showMessageDialog(
          frame,
          res.getString("GenerateSecretKeyAction.SecretKeyGenerationSuccessful.message"),
          res.getString("GenerateSecretKeyAction.GenerateSecretKey.Title"),
          JOptionPane.INFORMATION_MESSAGE);
    } catch (Exception ex) {
      DError.displayError(frame, ex);
    }
  }