예제 #1
0
  private List<KeyStore> initApple(final InputStream store)
      throws AOKeyStoreManagerException, IOException {
    if (!Platform.OS.MACOSX.equals(Platform.getOS())) {
      throw new InvalidOSException("Apple Mac OS X"); // $NON-NLS-1$
    }

    // Inicializamos
    try {
      this.ks = KeyStore.getInstance(this.ksType.getProviderName());
    } catch (final Exception e) {
      throw new AOKeyStoreManagerException(
          "No se ha podido obtener el almacen Apple.KeychainStore", e); // $NON-NLS-1$
    }

    try {
      this.ks.load(store, null);
    } catch (final CertificateException e) {
      throw new AOKeyStoreManagerException(
          "No se han podido cargar los certificados del almacen Apple.KeychainStore",
          e); //$NON-NLS-1$
    } catch (final NoSuchAlgorithmException e) {
      throw new AOKeyStoreManagerException(
          "No se ha podido verificar la integridad del almacen Apple.KeychainStore",
          e); //$NON-NLS-1$
    }
    final List<KeyStore> ret = new ArrayList<KeyStore>(1);
    ret.add(this.ks);
    return ret;
  }
  private static File getAppConfigDir() {

    final File appConfigDir = new File(Platform.getUserHome(), AFIRMA_DIR);
    if (!appConfigDir.exists()) {
      appConfigDir.mkdirs();
    }
    return appConfigDir;
  }
예제 #3
0
 /**
  * Devuelve el directorio principal de bibliotecas del sistema.
  *
  * @return Directorio principal de bibliotecas
  */
 public static String getSystemLibDir() {
   if (Platform.getOS().equals(Platform.OS.WINDOWS)) {
     String systemRoot = getSystemRoot();
     if (systemRoot == null) {
       LOGGER.warning(
           "No se ha podido determinar el directorio de Windows accediendo al registro, se usara 'C:\\WINDOWS\\'"); //$NON-NLS-1$
       systemRoot = "c:\\windows\\"; // $NON-NLS-1$
     }
     if (!systemRoot.endsWith("\\")) { // $NON-NLS-1$
       systemRoot += "\\"; // $NON-NLS-1$
     }
     return systemRoot + "System32"; // $NON-NLS-1$
   }
   return "/usr/lib"; //$NON-NLS-1$
 }
예제 #4
0
  private List<KeyStore> initCAPIAddressBook() throws AOKeyStoreManagerException {
    if (!Platform.getOS().equals(Platform.OS.WINDOWS)) {
      throw new InvalidOSException("Microsoft Windows"); // $NON-NLS-1$
    }

    // Nos aseguramos de que SunMSCAPI este cargado, para que la DLL
    // sunmscapi.dll tambien lo este
    if (Security.getProvider("SunMSCAPI") == null) { // $NON-NLS-1$
      try {
        Security.addProvider(
            (Provider) Class.forName("sun.security.mscapi.SunMSCAPI").newInstance()); // $NON-NLS-1$
      } catch (final Exception e) {
        throw new MissingSunMSCAPIException(e);
      }
    }
    Provider p = Security.getProvider("MSCAPIAddressBook"); // $NON-NLS-1$
    if (p == null) {
      try {
        p =
            (Provider)
                Class.forName("es.gob.afirma.keystores.capiaddressbook.MSCAPIAddressBook")
                    .newInstance(); //$NON-NLS-1$
      } catch (final Exception e) {
        throw new MissingLibraryException(
            "No se ha podido instanciar el proveedor MSCAPIAddressBook", e); // $NON-NLS-1$
      }
      Security.addProvider(p);
    }

    try {
      this.ks = KeyStore.getInstance(this.ksType.getProviderName(), p);
    } catch (final Exception e) {
      throw new AOKeyStoreManagerException(
          "No se ha podido obtener el almacen MSCAPIAddressBook.ADDRESSBOOK", e); // $NON-NLS-1$
    }

    try {
      this.ks.load(null, null);
    } catch (final Exception e) {
      throw new AOKeyStoreManagerException(
          "No se ha podido abrir el almacen MSCAPIAddressBook.ADDRESSBOOK", e); // $NON-NLS-1$
    }

    final List<KeyStore> ret = new ArrayList<KeyStore>(1);
    ret.add(this.ks);
    return ret;
  }
예제 #5
0
 /**
  * Obtiene el directorio principal del sistema operativo del sistema.
  *
  * @return Directorio principal del sistema operativo
  */
 private static String getSystemRoot() {
   if (!Platform.getOS().equals(Platform.OS.WINDOWS)) {
     return File.separator;
   }
   String systemRoot = System.getProperty("SystemRoot"); // $NON-NLS-1$
   if (systemRoot == null) {
     final String defaultSystemRoot = "C:\\WINDOWS"; // $NON-NLS-1$
     final File winSys32 = new File(defaultSystemRoot + "\\SYSTEM32"); // $NON-NLS-1$
     if (winSys32.exists() && winSys32.isDirectory()) {
       return defaultSystemRoot;
     }
   }
   if (systemRoot == null) {
     LOGGER.warning(
         "No se ha encontrado el directorio ra&iacute;z del sistema, se devolver&aacute;: "
             + File.separator); // $NON-NLS-1$
     systemRoot = File.separator;
   }
   return systemRoot;
 }
예제 #6
0
  /**
   * Abre un certificado con la aplicaci&oacute;n por defecto del sistema. Si no puede hacerlo,
   * permite que el usuario lo almacene en la ruta que desee.
   *
   * @param parent Componente padre sobre el que se muestran los di&aacute;logos.
   * @param certificate Certificado que deseamos abrir.
   */
  static void openCert(final Component parent, final X509Certificate certificate) {

    // Tratamos de abrir el certificado en Java 6
    Class<?> desktopClass;
    try {
      desktopClass = Class.forName("java.awt.Desktop"); // $NON-NLS-1$
    } catch (final ClassNotFoundException e) {
      desktopClass = null;
    }

    if (desktopClass != null) {
      try {
        final File certFile = saveTemp(certificate.getEncoded(), CERTIFICATE_DEFAULT_EXTENSION);
        final Method getDesktopMethod =
            desktopClass.getDeclaredMethod("getDesktop", (Class[]) null); // $NON-NLS-1$
        final Object desktopObject = getDesktopMethod.invoke(null, (Object[]) null);
        final Method openMethod = desktopClass.getDeclaredMethod("open", File.class); // $NON-NLS-1$
        openMethod.invoke(desktopObject, certFile);
        return;
      } catch (final Exception e) {
        Logger.getLogger("es.gob.afirma")
            .warning("No ha sido posible abrir el certificado: " + e); // $NON-NLS-1$ //$NON-NLS-2$
      }
    }

    // En entornos Java 5 intentamos abrirlo manualmente en Windows
    if (Platform.getOS() == OS.WINDOWS) {
      try {
        final File certFile = saveTemp(certificate.getEncoded(), CERTIFICATE_DEFAULT_EXTENSION);
        new ProcessBuilder(
                new String[] {
                  "cmd",
                  "/C",
                  "start", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                  "\""
                      + CertificateSelectionDialogMessages.getString("CertificateUtils.0")
                      + "\"", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                  "\"" + certFile.getAbsolutePath() + "\""
                } //$NON-NLS-1$ //$NON-NLS-2$
                )
            .start();
        return;
      } catch (final Exception e) {
        Logger.getLogger("es.gob.afirma")
            .warning("No ha sido posible abrir el certificado: " + e); // $NON-NLS-1$ //$NON-NLS-2$
      }
    }

    // Si no podemos abrirlo, lo guardamos en disco
    try {
      AOUIFactory.getSaveDataToFile(
          certificate.getEncoded(),
          CertificateSelectionDialogMessages.getString("CertificateUtils.1"), // $NON-NLS-1$
          null,
          CertificateSelectionDialogMessages.getString("CertificateUtils.5")
              + CERTIFICATE_DEFAULT_EXTENSION, //$NON-NLS-1$
          new String[] {CERTIFICATE_DEFAULT_EXTENSION},
          CertificateSelectionDialogMessages.getString("CertificateUtils.3"), // $NON-NLS-1$
          parent);
    } catch (final IOException e) {
      new JSEUIManager()
          .showConfirmDialog(
              parent,
              CertificateSelectionDialogMessages.getString("CertificateUtils.2"), // $NON-NLS-1$
              CertificateSelectionDialogMessages.getString("CertificateUtils.3"), // $NON-NLS-1$
              JOptionPane.CLOSED_OPTION,
              JOptionPane.ERROR_MESSAGE);
    } catch (final CertificateEncodingException e) {
      new JSEUIManager()
          .showConfirmDialog(
              parent,
              CertificateSelectionDialogMessages.getString("CertificateUtils.4"), // $NON-NLS-1$
              CertificateSelectionDialogMessages.getString("CertificateUtils.3"), // $NON-NLS-1$
              JOptionPane.CLOSED_OPTION,
              JOptionPane.ERROR_MESSAGE);
    } catch (final AOCancelledOperationException e) {
      // El usuario ha cancelado la operacion, no hacemos nada
    }
  }