Example usage for java.security KeyStore.CallbackHandlerProtection KeyStore.CallbackHandlerProtection

List of usage examples for java.security KeyStore.CallbackHandlerProtection KeyStore.CallbackHandlerProtection

Introduction

In this page you can find the example usage for java.security KeyStore.CallbackHandlerProtection KeyStore.CallbackHandlerProtection.

Prototype

public CallbackHandlerProtection(CallbackHandler handler) 

Source Link

Document

Constructs a new CallbackHandlerProtection from a CallbackHandler.

Usage

From source file:davmail.http.DavGatewaySSLProtocolSocketFactory.java

private KeyStore.ProtectionParameter getProtectionParameter(String password) {
    if (password != null && password.length() > 0) {
        // password provided: create a PasswordProtection
        return new KeyStore.PasswordProtection(password.toCharArray());
    } else {//from   w  ww  .j av a 2 s.  com
        // request password at runtime through a callback
        return new KeyStore.CallbackHandlerProtection(new CallbackHandler() {
            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                if (callbacks.length > 0 && callbacks[0] instanceof PasswordCallback) {
                    PasswordPromptDialog passwordPromptDialog = new PasswordPromptDialog(
                            ((PasswordCallback) callbacks[0]).getPrompt());
                    ((PasswordCallback) callbacks[0]).setPassword(passwordPromptDialog.getPassword());
                }
            }
        });
    }
}

From source file:davmail.util.ClientCertificateTest.java

protected KeyStore.ProtectionParameter getProtectionParameter(String password) {
    if (password != null && password.length() > 0) {
        // password provided: create a PasswordProtection
        return new KeyStore.PasswordProtection(password.toCharArray());
    } else {/*from   w  w w  . j  a  v  a2s  .c o  m*/
        // request password at runtime through a callback
        return new KeyStore.CallbackHandlerProtection(new CallbackHandler() {
            public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                if (callbacks.length > 0 && callbacks[0] instanceof PasswordCallback) {
                    PasswordPromptDialog passwordPromptDialog = new PasswordPromptDialog(
                            ((PasswordCallback) callbacks[0]).getPrompt());
                    ((PasswordCallback) callbacks[0]).setPassword(passwordPromptDialog.getPassword());
                }
            }
        });
    }
}

From source file:net.java.sip.communicator.impl.certificate.CertificateServiceImpl.java

private Builder loadKeyStore(final CertificateConfigEntry entry) throws KeyStoreException {
    final File f = new File(entry.getKeyStore());
    final KeyStoreType kt = entry.getKeyStoreType();
    if ("PKCS11".equals(kt.getName())) {
        String config = "name=" + f.getName() + "\nlibrary=" + f.getAbsoluteFile();
        try {/*from w w w .  j a v  a 2s  .  c  om*/
            Class<?> pkcs11c = Class.forName("sun.security.pkcs11.SunPKCS11");
            Constructor<?> c = pkcs11c.getConstructor(InputStream.class);
            Provider p = (Provider) c.newInstance(new ByteArrayInputStream(config.getBytes()));
            Security.insertProviderAt(p, 0);
        } catch (Exception e) {
            logger.error(
                    "Tried to access the PKCS11 provider on an " + "unsupported platform or the load failed",
                    e);
        }
    }
    KeyStore.Builder ksBuilder = KeyStore.Builder.newInstance(kt.getName(), null, f,
            new KeyStore.CallbackHandlerProtection(new CallbackHandler() {
                public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
                    for (Callback cb : callbacks) {
                        if (!(cb instanceof PasswordCallback))
                            throw new UnsupportedCallbackException(cb);

                        PasswordCallback pwcb = (PasswordCallback) cb;
                        if (entry.isSavePassword()) {
                            pwcb.setPassword(entry.getKeyStorePassword().toCharArray());
                            return;
                        } else {
                            AuthenticationWindowService authenticationWindowService = CertificateVerificationActivator
                                    .getAuthenticationWindowService();

                            if (authenticationWindowService == null) {
                                logger.error("No AuthenticationWindowService " + "implementation");
                                throw new IOException("User cancel");
                            }

                            AuthenticationWindowService.AuthenticationWindow aw = authenticationWindowService
                                    .create(f.getName(), null, kt.getName(), false, false, null, null, null,
                                            null, null, null, null);

                            aw.setAllowSavePassword(false);
                            aw.setVisible(true);
                            if (!aw.isCanceled())
                                pwcb.setPassword(aw.getPassword());
                            else
                                throw new IOException("User cancel");
                        }
                    }
                }
            }));
    return ksBuilder;
}