//use the Alias selector if present.
//NOTE: the keystoreCertSelector is actually an AliasSelector it
// cannot be a certSelector (especially JSR 196 does not allow browsing
// a keystore) to do cert selection.
if (this.keystoreCertSelectorClass != null) {
AliasSelector selector = null;
try {
selector = (AliasSelector)
this.keystoreCertSelectorClass.newInstance();
} catch (IllegalAccessException ex) {
log.log(Level.SEVERE,LogStringsMessages.WSS_0811_EXCEPTION_INSTANTIATING_ALIASSELECTOR(), ex);
throw new RuntimeException(ex);
} catch (InstantiationException ex) {
log.log(Level.SEVERE,LogStringsMessages.WSS_0811_EXCEPTION_INSTANTIATING_ALIASSELECTOR(), ex);
throw new RuntimeException(ex);
}
actualAlias = selector.select(context);
}
}
} else {
//for encryption
if (context != null) {
Object obj = context.get(XWSSConstants.SERVER_CERTIFICATE_PROPERTY);
if (obj instanceof X509Certificate) {
return (X509Certificate) obj;
}
}
if (this.peerEntityAlias != null) {
actualAlias = this.peerEntityAlias;
}
}
}
PrivateKeyCallback pkCallback = null;
if (forSigning) {
try {
Subject subject = getSubject(context);
if (subject != null) {
Set set = subject.getPrivateCredentials(X500PrivateCredential.class);
if (set != null) {
Iterator it = set.iterator();
while (it.hasNext()) {
X500PrivateCredential cred = (X500PrivateCredential)it.next();
if (cred.getAlias().equals(actualAlias))
return cred.getCertificate();
}
}
}
PrivateKeyCallback.Request request = new PrivateKeyCallback.AliasRequest(actualAlias);
pkCallback = new PrivateKeyCallback(request);
Callback[] callbacks = null;
if (this.useXWSSCallbacks) {
RuntimeProperties props = new RuntimeProperties(context);
callbacks = new Callback[]{props, pkCallback};
} else {
callbacks = new Callback[]{pkCallback};
}
_handler.handle(callbacks);
} catch (Exception e) {
log.log(Level.SEVERE,LogStringsMessages.WSS_0221_CANNOT_LOCATE_CERT(alias), new Object[] {alias});
throw new XWSSecurityException(e);
}
Certificate[] chain = pkCallback.getChain();
if (chain != null){
cert = (X509Certificate)chain[0];
} else {
if (log.isLoggable(Level.FINE)){
log.log(Level.SEVERE,LogStringsMessages.WSS_0296_NULL_CHAIN_CERT());
}
}
} else {
//for encryption
if (actualAlias != null && !"".equals(actualAlias)) {
TrustStoreCallback tsCallback = new TrustStoreCallback();
Callback[] _callbacks = null;
if (this.useXWSSCallbacks) {
RuntimeProperties props = new RuntimeProperties(context);
_callbacks = new Callback[]{props, tsCallback};
} else {
_callbacks = new Callback[]{tsCallback};
}
try {
_handler.handle(_callbacks);
} catch (IOException ex) {
log.log(Level.SEVERE,LogStringsMessages.WSS_0221_CANNOT_LOCATE_CERT(alias), new Object[] {alias});
throw new XWSSecurityException(ex);
} catch (UnsupportedCallbackException ex) {
log.log(Level.SEVERE,LogStringsMessages.WSS_0221_CANNOT_LOCATE_CERT(alias), new Object[] {alias});
throw new XWSSecurityException(ex);
}
if (tsCallback.getTrustStore() != null) {
try {
cert = (X509Certificate)tsCallback.getTrustStore().getCertificate(actualAlias);
} catch (KeyStoreException ex) {
log.log(Level.SEVERE,LogStringsMessages.WSS_0221_CANNOT_LOCATE_CERT(alias), new Object[] {alias});
throw new XWSSecurityException(ex);
}
}
} else {
//actualAlias == null || "".equals(actualAlias)
// first if certStore configured then give it a chance
if (this.certSelectorClass != null) {
CertStoreCallback csCallback = new CertStoreCallback();
Callback[] _callbacks = null;
if (this.useXWSSCallbacks) {
RuntimeProperties props = new RuntimeProperties(context);
_callbacks = new Callback[]{props, csCallback};
} else {
_callbacks = new Callback[]{csCallback};
}
try {
_handler.handle(_callbacks);
} catch (IOException ex) {
log.log(Level.SEVERE,LogStringsMessages.WSS_0221_CANNOT_LOCATE_CERT(alias), new Object[] {alias});
throw new XWSSecurityException(ex);
} catch (UnsupportedCallbackException ex) {
log.log(Level.SEVERE,LogStringsMessages.WSS_0221_CANNOT_LOCATE_CERT(alias), new Object[] {alias});
throw new XWSSecurityException(ex);
}
if (csCallback.getCertStore() != null) {
CertSelector selector = XWSSUtil.getCertSelector(certSelectorClass, context);
if (selector != null) {
Collection certs = null;
try {
certs = csCallback.getCertStore().getCertificates(selector);
} catch (CertStoreException ex) {
log.log(Level.SEVERE, LogStringsMessages.WSS_0813_FAILEDTO_GETCERTIFICATE(), ex);
throw new RuntimeException(ex);
}
if (certs.size() > 0) {
cert = (X509Certificate)certs.iterator().next();
}
}
}
}
if (cert == null && this.truststoreCertSelectorClass != null) {
TrustStoreCallback tsCallback = new TrustStoreCallback();
Callback[] _callbacks = null;
if (this.useXWSSCallbacks) {
RuntimeProperties props = new RuntimeProperties(context);
_callbacks = new Callback[]{props, tsCallback};
} else {
_callbacks = new Callback[]{tsCallback};
}
try {
_handler.handle(_callbacks);
} catch (IOException ex) {
log.log(Level.SEVERE,LogStringsMessages.WSS_0221_CANNOT_LOCATE_CERT(alias), new Object[] {alias});
throw new XWSSecurityException(ex);
} catch (UnsupportedCallbackException ex) {
log.log(Level.SEVERE,LogStringsMessages.WSS_0221_CANNOT_LOCATE_CERT(alias), new Object[] {alias});
throw new XWSSecurityException(ex);
}
KeyStore trustStore = tsCallback.getTrustStore();
if (trustStore != null) {
if (this.truststoreCertSelectorClass != null) {
CertSelector selector = XWSSUtil.getCertSelector(truststoreCertSelectorClass, context);
if (selector != null) {
Enumeration aliases=null;
try {
aliases = trustStore.aliases();
} catch (KeyStoreException ex) {
log.log(Level.SEVERE, LogStringsMessages.WSS_0813_FAILEDTO_GETCERTIFICATE(), ex);
throw new RuntimeException(ex);
}
while (aliases.hasMoreElements()) {
String currAlias = (String) aliases.nextElement();
Certificate thisCertificate = null;
try {
thisCertificate = trustStore.getCertificate(currAlias);
} catch (KeyStoreException ex) {
log.log(Level.SEVERE, LogStringsMessages.WSS_0813_FAILEDTO_GETCERTIFICATE(), ex);
throw new RuntimeException(ex);
}
if ((thisCertificate instanceof X509Certificate)
&& selector.match(thisCertificate)) {
return (X509Certificate)thisCertificate;
}
}
}
}