@Override
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
      NameCallback nc = null;
      PasswordCallback pc = null;
      AuthorizeCallback ac = null;

      for (Callback callback : callbacks) {
        if (callback instanceof AuthorizeCallback) {
          ac = (AuthorizeCallback) callback;
        } else if (callback instanceof NameCallback) {
          nc = (NameCallback) callback;
        } else if (callback instanceof PasswordCallback) {
          pc = (PasswordCallback) callback;
        } else if (callback instanceof RealmCallback) {
          continue; // realm is ignored
        } else {
          throw new UnsupportedCallbackException(
              callback, "handle: Unrecognized SASL DIGEST-MD5 Callback");
        }
      }

      if (nc != null) {
        LOG.debug(
            "handle: SASL server DIGEST-MD5 callback: setting "
                + "username for client: "
                + userName);

        nc.setName(userName);
      }

      if (pc != null) {
        char[] password = SaslUtils.encodePassword(userPassword);

        LOG.debug(
            "handle: SASL server DIGEST-MD5 callback: setting "
                + "password for client: "
                + userPassword);

        pc.setPassword(password);
      }
      if (ac != null) {

        String authid = ac.getAuthenticationID();
        String authzid = ac.getAuthorizationID();

        if (authid.equals(authzid)) {
          ac.setAuthorized(true);
        } else {
          ac.setAuthorized(false);
        }

        if (ac.isAuthorized()) {
          LOG.debug(
              "handle: SASL server DIGEST-MD5 callback: setting "
                  + "canonicalized client ID: "
                  + userName);
          ac.setAuthorizedID(authzid);
        }
      }
    }
Exemple #2
0
  protected AuthContext authenticate(
      String orgname, String username, String password, PrintWriter out) throws Exception {
    // Authenticate the user and obtain SSO Token
    AuthContext lc = new AuthContext(orgname);
    lc.login();
    while (lc.hasMoreRequirements()) {
      Callback[] callbacks = lc.getRequirements();
      for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
          NameCallback nc = (NameCallback) callbacks[i];
          nc.setName(username);
        } else if (callbacks[i] instanceof PasswordCallback) {
          PasswordCallback pc = (PasswordCallback) callbacks[i];
          pc.setPassword(password.toCharArray());
        } else {
          out.println("Unknow Callback: " + callbacks[i]);
          out.println("</body></html>");
          return null;
        }
      }
      lc.submitRequirements(callbacks);
    }

    if (lc.getStatus() != AuthContext.Status.SUCCESS) {
      out.println("Invalid credentials");
      out.println("</body></html>");
      return null;
    }

    return lc;
  }
  public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {

    for (Callback c : callbacks) {
      if (c instanceof NameCallback) {
        String username = userIdent.getUserName();
        NameCallback nc = (NameCallback) c;
        nc.setName(username);
      } else if (c instanceof PasswordCallback) {
        PasswordCallback pc = (PasswordCallback) c;
        char[] password = userIdent.getPassword().toCharArray();
        pc.setPassword(password);
      } else if (c instanceof UserIdentificationInfoCallback) {
        UserIdentificationInfoCallback uic = (UserIdentificationInfoCallback) c;
        uic.setUserInfo(userIdent);
      } else if (c.getClass()
          .getName()
          .equals("org.jboss.security.auth.callback.SecurityAssociationCallback")) {
        // we do nothing but do not raise error

      } else {
        throw new UnsupportedCallbackException(
            c, "Unrecognized Callback:" + c.getClass().getName());
      }
    }
  }
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback current : callbacks) {
     if (current instanceof NameCallback) {
       NameCallback ncb = (NameCallback) current;
       ncb.setName(username);
     } else if (current instanceof PasswordCallback && password != null) {
       PasswordCallback pcb = (PasswordCallback) current;
       pcb.setPassword(password);
     } else if (current instanceof DigestHashCallback && hexURPHash != null) {
       DigestHashCallback dhc = (DigestHashCallback) current;
       dhc.setHexHash(hexURPHash);
     } else if (current instanceof RealmCallback) {
       RealmCallback rcb = (RealmCallback) current;
       if (realm == null) {
         String defaultText = rcb.getDefaultText();
         if (defaultText != null && defaultText.length() > 0) {
           rcb.setText(defaultText);
         }
       } else {
         rcb.setText(realm);
       }
     } else {
       throw new UnsupportedCallbackException(
           current, current.getClass().getSimpleName() + " not supported.");
     }
   }
 }
 /**
  * If the callback passed to the 'handle' method is an instance of PasswordCallback, the
  * JaasPasswordCallbackHandler will call,
  * callback.setPassword(authentication.getCredentials().toString()).
  *
  * @param callback
  * @param auth
  * @throws IOException
  * @throws UnsupportedCallbackException
  */
 public void handle(Callback callback, Authentication auth)
     throws IOException, UnsupportedCallbackException {
   if (callback instanceof PasswordCallback) {
     PasswordCallback pc = (PasswordCallback) callback;
     pc.setPassword(auth.getCredentials().toString().toCharArray());
   }
 }
  @Override
  public boolean login() throws LoginException {
    if (callbackHandler == null) {
      throw new LoginException("Pas de callbackHandler");
    }

    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Login :"******"Mot de passe :", false);

    try {
      callbackHandler.handle(callbacks);
    } catch (IOException e) {
      throw new LoginException("IOException");
    } catch (UnsupportedCallbackException e) {
      throw new LoginException("UnsupportedCallbackException");
    }

    NameCallback nameCallback = (NameCallback) callbacks[0];
    PasswordCallback passwordCallback = (PasswordCallback) callbacks[1];

    String name = nameCallback.getName();
    String password = new String(passwordCallback.getPassword());

    return true;
  }
  protected String[] authenticate() throws IOException, UnsupportedCallbackException {

    NameCallback nameCallback = new NameCallback("name: ");
    PasswordCallback passwordCallback = new PasswordCallback("password: ", false);

    _callbackHandler.handle(new Callback[] {nameCallback, passwordCallback});

    String name = nameCallback.getName();

    String password = null;
    char[] passwordChar = passwordCallback.getPassword();

    if (passwordChar != null) {
      password = new String(passwordChar);
    }

    if (name == null) {
      return new String[] {StringPool.BLANK, StringPool.BLANK};
    }

    try {
      long userId = GetterUtil.getLong(name);

      if (UserLocalServiceUtil.authenticateForJAAS(userId, password)) {
        return new String[] {name, password};
      }
    } catch (Exception e) {
      _log.error(e, e);
    }

    return null;
  }
  // TODO:V3 trying to read system properties here
  protected void handleSupportedCallbacks(Callback[] callbacks)
      throws IOException, UnsupportedCallbackException {

    // this variable is set to true if we have used the older jaas
    // mechanisms to process the callbacks - and we will not need
    // to process further as the inside loop, just takes care
    // of processing all callbacks
    boolean processedSomeAppclientCallbacks = false;

    int i = 0;
    while (i < callbacks.length) {
      if (!processedSomeAppclientCallbacks) {
        if ((callbacks[i] instanceof NameCallback)
            || (callbacks[i] instanceof PasswordCallback)
            || (callbacks[i] instanceof ChoiceCallback)) {

          String loginName = UsernamePasswordStore.getUsername();
          char[] password = UsernamePasswordStore.getPassword();
          boolean doSet = false;
          if (loginName == null) {
            loginName = System.getProperty(LOGIN_NAME);
            doSet = true;
          }
          if (password == null) {
            password = System.getProperty(LOGIN_PASSWORD).toCharArray();
            doSet = true;
          }
          if (doSet) {
            UsernamePasswordStore.set(loginName, password);
          }
          // TODO: V3 CallbackHandler callbackHandler = AppContainer.getCallbackHandler();
          CallbackHandler callbackHandler = SecurityServicesUtil.getInstance().getCallbackHandler();
          if (loginName != null && password != null) {
            // username/password set already
            for (Callback callback : callbacks) {
              if (callback instanceof NameCallback) {
                NameCallback nc = (NameCallback) callback;
                nc.setName(loginName);
              } else if (callback instanceof PasswordCallback) {
                PasswordCallback pc = (PasswordCallback) callback;
                pc.setPassword(password);
              }
            }
          } else {
            // once this is called all callbacks will be handled by
            // callbackHandler and then we dont have to check for
            // NameCallback PasswordCallback and ChoiceCallback
            // again.
            // Let control flow to the callback processors
            callbackHandler.handle(callbacks);
          }
          processedSomeAppclientCallbacks = true;
          break;
        }
      }
      processCallback(callbacks[i]);
      i++;
    }
  }
 public Collection<Callback> get() {
   Collection<Callback> callbacks = new ArrayList<Callback>();
   NameCallback nameCallback = new NameCallback(NAME, GUEST);
   nameCallback.setName(GUEST);
   callbacks.add(nameCallback);
   PasswordCallback passwordCallback = new PasswordCallback(PASSWORD, true);
   passwordCallback.setPassword(GUEST.toCharArray());
   callbacks.add(passwordCallback);
   return callbacks;
 }
Exemple #10
0
    /** {@inheritDoc} */
    @Override
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
      NameCallback nc = null;
      PasswordCallback pc = null;
      AuthorizeCallback ac = null;
      for (Callback callback : callbacks) {
        if (callback instanceof AuthorizeCallback) {
          ac = (AuthorizeCallback) callback;
        } else if (callback instanceof NameCallback) {
          nc = (NameCallback) callback;
        } else if (callback instanceof PasswordCallback) {
          pc = (PasswordCallback) callback;
        } else if (callback instanceof RealmCallback) {
          continue; // realm is ignored
        } else {
          throw new UnsupportedCallbackException(
              callback, "handle: Unrecognized SASL DIGEST-MD5 Callback");
        }
      }
      if (pc != null) {
        JobTokenIdentifier tokenIdentifier = getIdentifier(nc.getDefaultName(), secretManager);
        char[] password = encodePassword(secretManager.retrievePassword(tokenIdentifier));

        if (LOG.isDebugEnabled()) {
          LOG.debug(
              "handle: SASL server DIGEST-MD5 callback: setting "
                  + "password for client: "
                  + tokenIdentifier.getUser());
        }
        pc.setPassword(password);
      }
      if (ac != null) {
        String authid = ac.getAuthenticationID();
        String authzid = ac.getAuthorizationID();
        if (authid.equals(authzid)) {
          ac.setAuthorized(true);
        } else {
          ac.setAuthorized(false);
        }
        if (ac.isAuthorized()) {
          if (LOG.isDebugEnabled()) {
            String username = getIdentifier(authzid, secretManager).getUser().getUserName();
            if (LOG.isDebugEnabled()) {
              LOG.debug(
                  "handle: SASL server DIGEST-MD5 callback: setting "
                      + "canonicalized client ID: "
                      + username);
            }
          }
          ac.setAuthorizedID(authzid);
        }
      }
    }
    public void setPassword(Principal principal, PasswordCallback callback)
        throws IOException, AccountNotFoundException {
      // Let the read DB set the password
      _realPricipalDatabase.setPassword(principal, callback);

      // Retrieve the setpassword
      char[] plainPassword = callback.getPassword();

      char[] hexPassword = toHex(plainPassword);

      callback.setPassword(hexPassword);
    }
    public void handle(Callback[] callbacks) throws UnsupportedCallbackException {

      for (int i = 0; i < callbacks.length; i++) {
        if (callbacks[i] instanceof NameCallback) {
          // prompt the user for a username
          NameCallback nc = (NameCallback) callbacks[i];
          nc.setName(userid);
        } else if (callbacks[i] instanceof PasswordCallback) {
          PasswordCallback pc = (PasswordCallback) callbacks[i];
          pc.setPassword(password.toCharArray());
        }
      }
    }
 @Override
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback callback : callbacks) {
     if (callback instanceof NameCallback) {
       NameCallback nameCallback = (NameCallback) callback;
       nameCallback.setName(getUserName());
     } else if (callback instanceof PasswordCallback) {
       PasswordCallback passwordCallback = (PasswordCallback) callback;
       passwordCallback.setPassword(getPassword().toCharArray());
     } else {
       throw new UnsupportedCallbackException(callback);
     }
   }
 }
 @Override
 public void handle(final Callback[] cb) throws IOException, UnsupportedCallbackException {
   for (final Callback element : cb) {
     if (element instanceof NameCallback) {
       final NameCallback nc = (NameCallback) element;
       nc.setName(this.username);
     } else if (element instanceof PasswordCallback) {
       final PasswordCallback pc = (PasswordCallback) element;
       pc.setPassword(this.password == null ? null : this.password.toCharArray());
     } else {
       throw new UnsupportedCallbackException(element, "UsernamePasswordCallbackHandler");
     }
   }
 }
 public void handle(Callback[] callbacks)
     throws java.io.IOException, UnsupportedCallbackException {
   for (int i = 0; i < callbacks.length; i++) {
     if (callbacks[i] instanceof NameCallback) {
       NameCallback nc = (NameCallback) callbacks[i];
       nc.setName(username);
     } else if (callbacks[i] instanceof PasswordCallback) {
       PasswordCallback pc = (PasswordCallback) callbacks[i];
       pc.setPassword(password.toCharArray());
     } else {
       throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
     }
   }
 }
    private Object[] getUserPass() throws LoginException {
      NameCallback nameCB = new NameCallback("username");
      PasswordCallback passwordCB = new PasswordCallback("password", false);

      try {
        callbackHandler.handle(new Callback[] {nameCB, passwordCB});
      } catch (IOException | UnsupportedCallbackException e) {
        throw new LoginException(e.toString());
      }

      String username = nameCB.getName();
      char[] password = passwordCB.getPassword();
      passwordCB.clearPassword();
      return new Object[] {username, password};
    }
 protected void setPasswordCallbackValue(Object thePass, PasswordCallback passwdCallback) {
   String tmp;
   if (thePass instanceof String) {
     tmp = (String) thePass;
     passwdCallback.setPassword(tmp.toCharArray());
   } else if (thePass instanceof char[]) {
     passwdCallback.setPassword((char[]) thePass);
   } else if (thePass instanceof byte[]) {
     byte[] theBytes = (byte[]) thePass;
     passwdCallback.setPassword((new String(theBytes).toCharArray()));
   } else {
     throw PicketBoxMessages.MESSAGES.invalidPasswordType(
         thePass != null ? thePass.getClass() : null);
   }
 }
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback cb : callbacks) {
     if (cb instanceof TextOutputCallback) {
       // Not implementing this one yet...
     } else if (cb instanceof NameCallback) {
       NameCallback nc = (NameCallback) cb;
       nc.setName(username);
     } else if (cb instanceof PasswordCallback) {
       PasswordCallback pc = (PasswordCallback) cb;
       pc.setPassword(password);
     } else {
       throw new UnsupportedCallbackException(cb);
     }
   }
 }
  public void initialize(
      Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
    this.subject = subject;

    try {
      TextInputCallback textCallback = new TextInputCallback("prompt");
      NameCallback nameCallback = new NameCallback("prompt");
      PasswordCallback passwordCallback = new PasswordCallback("prompt", false);

      callbackHandler.handle(new Callback[] {textCallback, nameCallback, passwordCallback});

      passwordFromLoginPage = new String(passwordCallback.getPassword());
      userFromLoginPage = nameCallback.getName();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
 @Override
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback current : callbacks) {
     if (current instanceof NameCallback) {
       NameCallback ncb = (NameCallback) current;
       ncb.setName(this.userName);
     } else if (current instanceof PasswordCallback) {
       PasswordCallback pcb = (PasswordCallback) current;
       pcb.setPassword(this.password.toCharArray());
     } else if (current instanceof RealmCallback) {
       RealmCallback rcb = (RealmCallback) current;
       rcb.setText(rcb.getDefaultText());
     } else {
       throw new UnsupportedCallbackException(current);
     }
   }
 }
    /**
     * The handler sets for instances of {@link NameCallBack} the given {@link #name} and for
     * instances of {@link PasswordCallBack} the given {@link #password}. {@link TextOutputCallBack}
     * instances are ignored.
     *
     * @param _callbacks callback instances to handle
     * @throws UnsupportedCallbackException for all {@link Callback} instances which are not {@link
     *     NameCallBack}, {@link PasswordCallBack} or {@link TextOutputCallBack}.
     */
    public void handle(final Callback[] _callbacks)
        throws IOException, UnsupportedCallbackException {

      for (int i = 0; i < _callbacks.length; i++) {
        if (_callbacks[i] instanceof TextOutputCallback) {
          // do nothing, TextOutputCallBack's are ignored!
        } else if (_callbacks[i] instanceof NameCallback) {
          NameCallback nc = (NameCallback) _callbacks[i];
          nc.setName(this.name);
        } else if (_callbacks[i] instanceof PasswordCallback) {
          PasswordCallback pc = (PasswordCallback) _callbacks[i];
          pc.setPassword(this.password.toCharArray());
        } else {
          throw new UnsupportedCallbackException(_callbacks[i], "Unrecognized Callback");
        }
      }
    }
  public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    List<Callback> toRespondTo = new LinkedList<Callback>();

    String userName = null;
    boolean userFound = false;

    // A single pass may be sufficient but by using a two pass approach the Callbackhandler will not
    // fail if an unexpected order is encountered.

    // First Pass - is to double check no unsupported callbacks and to retrieve
    // information from the callbacks passing in information.
    Properties users = getUsersProperties();
    for (Callback current : callbacks) {

      if (current instanceof AuthorizeCallback) {
        toRespondTo.add(current);
      } else if (current instanceof NameCallback) {
        NameCallback nameCallback = (NameCallback) current;
        userName = nameCallback.getDefaultName();
        userFound = users.containsKey(userName);
      } else if (current instanceof PasswordCallback && plainText) {
        toRespondTo.add(current);
      } else if (current instanceof DigestHashCallback && plainText == false) {
        toRespondTo.add(current);
      } else if (current instanceof RealmCallback) {
        String realm = ((RealmCallback) current).getDefaultText();
        if (this.realm.equals(realm) == false) {
          throw new IllegalStateException(
              "Invalid Realm '" + realm + "' expected '" + this.realm + "'");
        }
      } else {
        throw new UnsupportedCallbackException(current);
      }
    }

    // Second Pass - Now iterate the Callback(s) requiring a response.
    for (Callback current : toRespondTo) {
      if (current instanceof AuthorizeCallback) {
        AuthorizeCallback authorizeCallback = (AuthorizeCallback) current;
        // Don't support impersonating another identity
        authorizeCallback.setAuthorized(
            authorizeCallback.getAuthenticationID().equals(authorizeCallback.getAuthorizationID()));
      } else if (current instanceof PasswordCallback) {
        if (userFound == false) {
          throw new UserNotFoundException(userName);
        }
        String password = users.get(userName).toString();
        ((PasswordCallback) current).setPassword(password.toCharArray());
      } else if (current instanceof DigestHashCallback) {
        if (userFound == false) {
          throw new UserNotFoundException(userName);
        }
        String hash = users.get(userName).toString();
        ((DigestHashCallback) current).setHexHash(hash);
      }
    }
  }
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback current : callbacks) {
     if (current instanceof RealmCallback) {
       RealmCallback rcb = (RealmCallback) current;
       String defaultText = rcb.getDefaultText();
       rcb.setText(defaultText); // For now just use the realm suggested.
     } else if (current instanceof RealmChoiceCallback) {
       throw new UnsupportedCallbackException(current, "Realm choice not currently supported.");
     } else if (current instanceof NameCallback) {
       NameCallback ncb = (NameCallback) current;
       ncb.setName(serverName);
     } else if (current instanceof PasswordCallback) {
       PasswordCallback pcb = (PasswordCallback) current;
       pcb.setPassword(new String(authKey).toCharArray());
     } else {
       throw new UnsupportedCallbackException(current);
     }
   }
 }
Exemple #24
0
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (int i = 0; i < callbacks.length; i++) {
     if (callbacks[i] instanceof NameCallback) {
       NameCallback ncb = (NameCallback) callbacks[i];
       ncb.setName(authenticationId);
     } else if (callbacks[i] instanceof PasswordCallback) {
       PasswordCallback pcb = (PasswordCallback) callbacks[i];
       pcb.setPassword(password.toCharArray());
     } else if (callbacks[i] instanceof RealmCallback) {
       RealmCallback rcb = (RealmCallback) callbacks[i];
       rcb.setText(hostname);
     } else if (callbacks[i] instanceof RealmChoiceCallback) {
       // unused
       // RealmChoiceCallback rccb = (RealmChoiceCallback)callbacks[i];
     } else {
       throw new UnsupportedCallbackException(callbacks[i]);
     }
   }
 }
Exemple #25
0
    @Override
    public void handle(Callback[] callbacks) throws UnsupportedCallbackException {

      for (Callback callback : callbacks) {
        if (callback instanceof NameCallback) {
          NameCallback nameCallback = (NameCallback) callback;

          nameCallback.setName(_name);
        } else if (callback instanceof PasswordCallback) {
          String password = GetterUtil.getString(_password);

          PasswordCallback passwordCallback = (PasswordCallback) callback;

          passwordCallback.setPassword(password.toCharArray());
        } else {
          throw new UnsupportedCallbackException(callback);
        }
      }
    }
 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
   for (Callback callback : callbacks) {
     if (callback instanceof NameCallback) {
       ((NameCallback) callback).setName(login);
     }
     if (callback instanceof PasswordCallback) {
       ((PasswordCallback) callback).setPassword(password.toCharArray());
     }
   }
 }
  private List<KeyStore> initDnieJava(
      final PasswordCallback pssCallBack, final Object parentComponent)
      throws AOKeyStoreManagerException, IOException {
    final Provider p;
    if (Security.getProvider(AOKeyStore.DNIEJAVA.getProviderName()) == null) {
      try {
        p =
            (Provider)
                Class.forName("es.gob.jmulticard.jse.provider.DnieProvider")
                    .newInstance(); //$NON-NLS-1$
        Security.addProvider(p);
      } catch (final Exception e) {
        throw new AOKeyStoreManagerException(
            "No se ha podido instanciar e instalar el proveedor 100% Java para DNIe de Afirma: "
                + e, //$NON-NLS-1$
            e);
      }
    }

    try {
      final Class<?> managerClass =
          Class.forName(
              "es.gob.jmulticard.ui.passwordcallback.PasswordCallbackManager"); //$NON-NLS-1$
      final Method setDialogOwnerFrameMethod =
          managerClass.getMethod("setDialogOwner", Component.class); // $NON-NLS-1$
      setDialogOwnerFrameMethod.invoke(null, parentComponent);
    } catch (final Exception e) {
      LOGGER.warning(
          "No se ha podido establecer el componente padre para los dialogos del almacen: "
              + e); //$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 DNIe 100% Java: " + e, e); // $NON-NLS-1$
    }

    LOGGER.info("Cargando KeyStore DNIe 100% Java"); // $NON-NLS-1$
    try {
      this.ks.load(null, pssCallBack == null ? null : pssCallBack.getPassword());
    } catch (final NoSuchAlgorithmException e) {
      throw new AOKeyStoreManagerException(
          "Error de algoritmo al obtener el almacen DNIe 100% Java: " + e, e); // $NON-NLS-1$
    } catch (final CertificateException e) {
      throw new AOKeyStoreManagerException(
          "Error de certificado al obtener el almacen DNIe 100% Java: " + e, e); // $NON-NLS-1$
    }

    final List<KeyStore> ret = new ArrayList<KeyStore>(1);
    ret.add(this.ks);
    return ret;
  }
  private List<KeyStore> initSingle(final InputStream store, final PasswordCallback pssCallBack)
      throws AOKeyStoreManagerException, IOException {
    if (store == null) {
      throw new AOKeyStoreManagerException(
          "Es necesario proporcionar el fichero X.509 o PKCS#7"); //$NON-NLS-1$
    }

    final Provider pkcs7Provider;
    try {
      pkcs7Provider =
          (Provider)
              Class.forName("es.gob.afirma.keystores.single.SingleCertKeyStoreProvider")
                  .newInstance(); //$NON-NLS-1$
    } catch (final Exception e) {
      throw new MissingLibraryException(
          "No se ha podido instanciar el proveedor SingleCertKeyStoreProvider: " + e,
          e); //$NON-NLS-1$
    }
    Security.addProvider(pkcs7Provider);

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

    try {
      this.ks.load(store, pssCallBack != null ? pssCallBack.getPassword() : null);
    } catch (final IOException e) {
      if (e.getCause() instanceof UnrecoverableKeyException
          || e.getCause() instanceof BadPaddingException) {
        throw new IOException("Contrasena invalida: " + e, e); // $NON-NLS-1$
      }
      throw new AOKeyStoreManagerException(
          "No se ha podido abrir el almacen PKCS#7 / X.509 solicitado", e); // $NON-NLS-1$
    } catch (final CertificateException e) {
      throw new AOKeyStoreManagerException(
          "No se han podido cargar los certificados del almacen PKCS#7 / X.509 solicitado",
          e); //$NON-NLS-1$
    } catch (final NoSuchAlgorithmException e) {
      throw new AOKeyStoreManagerException(
          "No se ha podido verificar la integridad del almacen PKCS#7 / X.509 solicitado",
          e); //$NON-NLS-1$
    }
    final List<KeyStore> ret = new ArrayList<KeyStore>(1);
    ret.add(this.ks);
    try {
      store.close();
    } catch (final Exception e) {
      // Ignoramos errores en el cierre
    }
    return ret;
  }
  public ConnectionProxy connect(Properties info) throws SQLException {
    this.properties = info;

    PasswordCallback passwordCallback = this.config.getPasswordCallback();

    if (passwordCallback != null) {
      char[] chars = passwordCallback.getPassword();
      String password = new String(chars);
      info.put("password", password);
    }

    NameCallback userCallback = this.config.getUserCallback();
    if (userCallback != null) {
      String user = userCallback.getName();
      info.put("user", user);
    }

    FilterChain chain = new FilterChainImpl(this);
    return chain.connection_connect(info);
  }
Exemple #30
0
  @Override
  public byte[] evaluateResponse(byte[] response) throws SaslException {
    if (completed) {
      throw new IllegalStateException("PLAIN authentication has completed");
    }
    if (response == null) {
      throw new IllegalArgumentException("Received null response");
    }
    try {
      String payload;
      try {
        payload = new String(response, "UTF-8");
      } catch (Exception e) {
        throw new IllegalArgumentException("Received corrupt response", e);
      }
      // [ authz, authn, password ]
      String[] parts = payload.split("\u0000", 3);
      if (parts.length != 3) {
        throw new IllegalArgumentException("Received corrupt response");
      }
      if (parts[0].isEmpty()) { // authz = authn
        parts[0] = parts[1];
      }

      NameCallback nc = new NameCallback("SASL PLAIN");
      nc.setName(parts[1]);
      PasswordCallback pc = new PasswordCallback("SASL PLAIN", false);
      pc.setPassword(parts[2].toCharArray());
      AuthorizeCallback ac = new AuthorizeCallback(parts[1], parts[0]);
      cbh.handle(new Callback[] {nc, pc, ac});
      if (ac.isAuthorized()) {
        authz = ac.getAuthorizedID();
      }
    } catch (Exception e) {
      throw new SaslException("PLAIN auth failed: " + e.toString(), e);
    } finally {
      completed = true;
    }
    return null;
  }