Search in sources :

Example 1 with Destroyable

use of javax.security.auth.Destroyable in project jdk8u_jdk by JetBrains.

the class KeyStoreLoginModule method logoutInternal.

private void logoutInternal() throws LoginException {
    if (debug) {
        debugPrint("Entering logoutInternal");
    }
    // assumption is that KeyStore.load did a login -
    // perform explicit logout if possible
    LoginException logoutException = null;
    Provider provider = keyStore.getProvider();
    if (provider instanceof AuthProvider) {
        AuthProvider ap = (AuthProvider) provider;
        try {
            ap.logout();
            if (debug) {
                debugPrint("logged out of KeyStore AuthProvider");
            }
        } catch (LoginException le) {
            // save but continue below
            logoutException = le;
        }
    }
    if (subject.isReadOnly()) {
        // attempt to destroy the private credential
        // even if the Subject is read-only
        principal = null;
        certP = null;
        status = INITIALIZED;
        // destroy the private credential
        Iterator<Object> it = subject.getPrivateCredentials().iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            if (privateCredential.equals(obj)) {
                privateCredential = null;
                try {
                    ((Destroyable) obj).destroy();
                    if (debug)
                        debugPrint("Destroyed private credential, " + obj.getClass().getName());
                    break;
                } catch (DestroyFailedException dfe) {
                    LoginException le = new LoginException("Unable to destroy private credential, " + obj.getClass().getName());
                    le.initCause(dfe);
                    throw le;
                }
            }
        }
        // read-only Subject
        throw new LoginException("Unable to remove Principal (" + "X500Principal " + ") and public credential (certificatepath) " + "from read-only Subject");
    }
    if (principal != null) {
        subject.getPrincipals().remove(principal);
        principal = null;
    }
    if (certP != null) {
        subject.getPublicCredentials().remove(certP);
        certP = null;
    }
    if (privateCredential != null) {
        subject.getPrivateCredentials().remove(privateCredential);
        privateCredential = null;
    }
    // throw pending logout exception if there is one
    if (logoutException != null) {
        throw logoutException;
    }
    status = INITIALIZED;
}
Also used : Destroyable(javax.security.auth.Destroyable) DestroyFailedException(javax.security.auth.DestroyFailedException) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException)

Example 2 with Destroyable

use of javax.security.auth.Destroyable in project jdk8u_jdk by JetBrains.

the class MyDestroyablePrivateKey method destroyKey.

private static void destroyKey(Key key) throws Exception {
    String klass = key.getClass().getName();
    if (!(key instanceof Destroyable)) {
        throw new UnsupportedOperationException();
    }
    Destroyable dKey = (Destroyable) key;
    if (dKey.isDestroyed()) {
        throw new Exception("error: a " + klass + " key has already been destroyed");
    }
    dKey.destroy();
    if (!dKey.isDestroyed()) {
        throw new Exception("error: a " + klass + " key has NOT been destroyed");
    }
}
Also used : Destroyable(javax.security.auth.Destroyable) DestroyFailedException(javax.security.auth.DestroyFailedException)

Aggregations

DestroyFailedException (javax.security.auth.DestroyFailedException)2 Destroyable (javax.security.auth.Destroyable)2 FailedLoginException (javax.security.auth.login.FailedLoginException)1 LoginException (javax.security.auth.login.LoginException)1