001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.security.realm.providers;
018    
019    import java.io.IOException;
020    import java.security.cert.X509Certificate;
021    import java.security.cert.Certificate;
022    
023    import javax.security.auth.callback.Callback;
024    import javax.security.auth.callback.UnsupportedCallbackException;
025    import javax.security.auth.callback.CallbackHandler;
026    
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    /**
031     * @version $Rev: 483636 $ $Date: 2006-12-07 15:03:43 -0500 (Thu, 07 Dec 2006) $
032     */
033    public class CertificateChainCallbackHandler implements CallbackHandler {
034        private static final Log log = LogFactory.getLog(CertificateChainCallbackHandler.class);
035        Certificate[] certificateChain;
036    
037        public CertificateChainCallbackHandler(Certificate[] certificateChain) {
038            this.certificateChain = certificateChain;
039        }
040    
041        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
042            for (int i = 0; i < callbacks.length; i++) {
043                Callback callback = callbacks[i];
044                if (callback instanceof CertificateChainCallback) {
045                    CertificateChainCallback cc = (CertificateChainCallback) callback;
046                    cc.setCertificateChain(certificateChain);
047                } else if (callback instanceof CertificateCallback) {
048                    if (certificateChain != null
049                            && certificateChain.length > 0
050                            && certificateChain[0] instanceof X509Certificate) {
051                        CertificateCallback cc = (CertificateCallback) callback;
052                        cc.setCertificate((X509Certificate) certificateChain[0]);
053                    } else {
054                        StringBuffer buf = new StringBuffer("Invalid certificate chain: \n");
055                        if (certificateChain == null) {
056                            buf.append("certificate chain is null");
057                        } else {
058                            buf.append("certificate chain length: ").append(certificateChain.length).append("\n");
059                            if (certificateChain.length > 0) {
060                                buf.append("first certificate is a: ").append(certificateChain[0].getClass()).append("\n");
061                                buf.append("certificate is an X509Certificate: ").append(certificateChain[0] instanceof X509Certificate).append("\n");
062                            }
063                        }
064                        throw new UnsupportedCallbackException(callback, buf.toString());
065                    }
066                } else {
067                    throw new UnsupportedCallbackException(callback, "Wrong callback type: " + callback.getClass());
068                }
069            }
070        }
071    
072    }