1 17 package org.apache.geronimo.security.realm.providers; 18 19 import java.io.IOException ; 20 import java.security.cert.X509Certificate ; 21 import java.security.cert.Certificate ; 22 23 import javax.security.auth.callback.Callback ; 24 import javax.security.auth.callback.UnsupportedCallbackException ; 25 import javax.security.auth.callback.CallbackHandler ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 33 public class CertificateChainCallbackHandler implements CallbackHandler { 34 private static final Log log = LogFactory.getLog(CertificateChainCallbackHandler.class); 35 Certificate [] certificateChain; 36 37 public CertificateChainCallbackHandler(Certificate [] certificateChain) { 38 this.certificateChain = certificateChain; 39 } 40 41 public void handle(Callback [] callbacks) throws IOException , UnsupportedCallbackException { 42 for (int i = 0; i < callbacks.length; i++) { 43 Callback callback = callbacks[i]; 44 if (callback instanceof CertificateChainCallback) { 45 CertificateChainCallback cc = (CertificateChainCallback) callback; 46 cc.setCertificateChain(certificateChain); 47 } else if (callback instanceof CertificateCallback) { 48 if (certificateChain != null 49 && certificateChain.length > 0 50 && certificateChain[0] instanceof X509Certificate ) { 51 CertificateCallback cc = (CertificateCallback) callback; 52 cc.setCertificate((X509Certificate ) certificateChain[0]); 53 } else { 54 StringBuffer buf = new StringBuffer ("Invalid certificate chain: \n"); 55 if (certificateChain == null) { 56 buf.append("certificate chain is null"); 57 } else { 58 buf.append("certificate chain length: ").append(certificateChain.length).append("\n"); 59 if (certificateChain.length > 0) { 60 buf.append("first certificate is a: ").append(certificateChain[0].getClass()).append("\n"); 61 buf.append("certificate is an X509Certificate: ").append(certificateChain[0] instanceof X509Certificate ).append("\n"); 62 } 63 } 64 throw new UnsupportedCallbackException (callback, buf.toString()); 65 } 66 } else { 67 throw new UnsupportedCallbackException (callback, "Wrong callback type: " + callback.getClass()); 68 } 69 } 70 } 71 72 } 73 | Popular Tags |