use of javax.security.auth.callback.PasswordCallback in project Bytecoder by mirkosertic.
the class ConsoleCallbackHandler method handle.
/**
* Handles the specified set of callbacks.
*
* @param callbacks the callbacks to handle
* @throws IOException if an input or output error occurs.
* @throws UnsupportedCallbackException if the callback is not an
* instance of NameCallback or PasswordCallback
*/
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
ConfirmationCallback confirmation = null;
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof TextOutputCallback) {
TextOutputCallback tc = (TextOutputCallback) callbacks[i];
String text;
switch(tc.getMessageType()) {
case TextOutputCallback.INFORMATION:
text = "";
break;
case TextOutputCallback.WARNING:
text = "Warning: ";
break;
case TextOutputCallback.ERROR:
text = "Error: ";
break;
default:
throw new UnsupportedCallbackException(callbacks[i], "Unrecognized message type");
}
String message = tc.getMessage();
if (message != null) {
text += message;
}
if (text != null) {
System.err.println(text);
}
} else if (callbacks[i] instanceof NameCallback) {
NameCallback nc = (NameCallback) callbacks[i];
if (nc.getDefaultName() == null) {
System.err.print(nc.getPrompt());
} else {
System.err.print(nc.getPrompt() + " [" + nc.getDefaultName() + "] ");
}
System.err.flush();
String result = readLine();
if (result.equals("")) {
result = nc.getDefaultName();
}
nc.setName(result);
} else if (callbacks[i] instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback) callbacks[i];
System.err.print(pc.getPrompt());
System.err.flush();
pc.setPassword(Password.readPassword(System.in, pc.isEchoOn()));
} else if (callbacks[i] instanceof ConfirmationCallback) {
confirmation = (ConfirmationCallback) callbacks[i];
} else {
throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
}
}
/* Do the confirmation callback last. */
if (confirmation != null) {
doConfirmation(confirmation);
}
}
use of javax.security.auth.callback.PasswordCallback in project taskana by Taskana.
the class SampleLoginModule method initialize.
@Override
public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {
this.subject = subject;
try {
nameCallback = new NameCallback("prompt");
passwordCallback = new PasswordCallback("prompt", false);
callbackHandler.handle(new Callback[] { nameCallback, passwordCallback });
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of javax.security.auth.callback.PasswordCallback in project ecf by eclipse.
the class XMPPConnection method proceedTLSReceived.
/**
* The server has indicated that TLS negotiation can start. We now need to secure the
* existing plain connection and perform a handshake. This method won't return until the
* connection has finished the handshake or an error occured while securing the connection.
*
* @throws Exception if an exception occurs.
*/
void proceedTLSReceived() throws Exception {
SSLContext context = this.config.getCustomSSLContext();
KeyStore ks = null;
KeyManager[] kms = null;
PasswordCallback pcb = null;
if (config.getCallbackHandler() == null) {
ks = null;
} else if (context == null) {
// System.out.println("Keystore type: "+configuration.getKeystoreType());
if (config.getKeystoreType().equals("NONE")) {
ks = null;
pcb = null;
} else if (config.getKeystoreType().equals("PKCS11")) {
try {
Constructor<?> c = Class.forName("sun.security.pkcs11.SunPKCS11").getConstructor(InputStream.class);
String pkcs11Config = "name = SmartCard\nlibrary = " + config.getPKCS11Library();
ByteArrayInputStream config = new ByteArrayInputStream(pkcs11Config.getBytes());
Provider p = (Provider) c.newInstance(config);
Security.addProvider(p);
ks = KeyStore.getInstance("PKCS11", p);
pcb = new PasswordCallback("PKCS11 Password: ", false);
this.config.getCallbackHandler().handle(new Callback[] { pcb });
ks.load(null, pcb.getPassword());
} catch (Exception e) {
ks = null;
pcb = null;
}
} else if (config.getKeystoreType().equals("Apple")) {
ks = KeyStore.getInstance("KeychainStore", "Apple");
ks.load(null, null);
// pcb = new PasswordCallback("Apple Keychain",false);
// pcb.setPassword(null);
} else {
ks = KeyStore.getInstance(config.getKeystoreType());
try {
pcb = new PasswordCallback("Keystore Password: ", false);
config.getCallbackHandler().handle(new Callback[] { pcb });
ks.load(new FileInputStream(config.getKeystorePath()), pcb.getPassword());
} catch (Exception e) {
ks = null;
pcb = null;
}
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
try {
if (pcb == null) {
kmf.init(ks, null);
} else {
kmf.init(ks, pcb.getPassword());
pcb.clearPassword();
}
kms = kmf.getKeyManagers();
} catch (NullPointerException npe) {
kms = null;
}
}
// Verify certificate presented by the server
if (context == null) {
context = SSLContext.getInstance("TLS");
context.init(kms, new javax.net.ssl.TrustManager[] { new ServerTrustManager(getServiceName(), config) }, new java.security.SecureRandom());
}
Socket plain = socket;
// Secure the plain connection
socket = context.getSocketFactory().createSocket(plain, plain.getInetAddress().getHostAddress(), plain.getPort(), true);
socket.setSoTimeout(0);
socket.setKeepAlive(true);
// Initialize the reader and writer with the new secured version
initReaderAndWriter();
// Proceed to do the handshake
((SSLSocket) socket).startHandshake();
// if (((SSLSocket) socket).getWantClientAuth()) {
// System.err.println("Connection wants client auth");
// }
// else if (((SSLSocket) socket).getNeedClientAuth()) {
// System.err.println("Connection needs client auth");
// }
// else {
// System.err.println("Connection does not require client auth");
// }
// Set that TLS was successful
usingTLS = true;
// Set the new writer to use
packetWriter.setWriter(writer);
// Send a new opening stream to the server
packetWriter.openStream();
}
use of javax.security.auth.callback.PasswordCallback in project wildfly-swarm by wildfly-swarm.
the class AuthCallbackHandler method handle.
@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 RealmCallback) {
RealmCallback rcb = (RealmCallback) current;
rcb.setText(rcb.getDefaultText());
} else if (current instanceof CredentialCallback) {
CredentialCallback ccb = (CredentialCallback) current;
try {
DigestPasswordAlgorithmSpec algoSpec = new DigestPasswordAlgorithmSpec(this.userName, this.realm);
EncryptablePasswordSpec passwordSpec = new EncryptablePasswordSpec(this.password.toCharArray(), algoSpec);
Password passwd = PasswordFactory.getInstance(ALGORITHM_DIGEST_MD5).generatePassword(passwordSpec);
Credential creds = new PasswordCredential(passwd);
ccb.setCredential(creds);
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
} else if (current instanceof PasswordCallback) {
PasswordCallback pcb = (PasswordCallback) current;
pcb.setPassword(this.password.toCharArray());
} else {
throw new UnsupportedCallbackException(current);
}
}
}
use of javax.security.auth.callback.PasswordCallback in project jmulticard by ctt-gob-es.
the class SmartCafeKeyStoreImpl method engineLoad.
/**
* {@inheritDoc}
*/
@Override
public void engineLoad(final KeyStore.LoadStoreParameter param) throws IOException {
final ApduConnection conn = new es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection();
this.cryptoCard = new SmartCafePkcs15Applet(conn, new JseCryptoHelper());
if (param != null) {
final ProtectionParameter pp = param.getProtectionParameter();
if (pp instanceof KeyStore.CallbackHandlerProtection) {
if (((KeyStore.CallbackHandlerProtection) pp).getCallbackHandler() == null) {
// $NON-NLS-1$
throw new IllegalArgumentException("El CallbackHandler no puede ser nulo");
}
this.cryptoCard.setCallbackHandler(((KeyStore.CallbackHandlerProtection) pp).getCallbackHandler());
} else if (pp instanceof KeyStore.PasswordProtection) {
final PasswordCallback pwc = new CachePasswordCallback(((PasswordProtection) pp).getPassword());
this.cryptoCard.setPasswordCallback(pwc);
} else {
LOGGER.warning(// $NON-NLS-1$ //$NON-NLS-2$
"Se ha proporcionado un LoadStoreParameter de tipo no soportado, se ignorara: " + (pp != null ? pp.getClass().getName() : "NULO"));
}
}
this.aliases = Arrays.asList(this.cryptoCard.getAliases());
}
Aggregations