001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.activemq.jaas;
020    
021    
022    import javax.security.auth.callback.Callback;
023    import javax.security.auth.callback.CallbackHandler;
024    import javax.security.auth.callback.NameCallback;
025    import javax.security.auth.callback.PasswordCallback;
026    import javax.security.auth.callback.UnsupportedCallbackException;
027    
028    import java.io.IOException;
029    import java.security.cert.X509Certificate;
030    
031    /**
032     * A Standard JAAS callback handler for SSL certificate requests.
033     * 
034     * Will only handle callbacks of type CertificateCallback.
035     * 
036     * @author sepandm@gmail.com (Sepand)
037     *
038     */
039    public class JaasCertificateCallbackHandler implements CallbackHandler {
040        final X509Certificate certificates[];
041        
042        /**
043         * Basic constructor.
044         * 
045         * @param cert The certificate returned when calling back.
046         */
047        public JaasCertificateCallbackHandler(X509Certificate certs[]) {
048            certificates = certs;
049        }
050        
051        /**
052         * Overriding handle method to handle certificates.
053         * 
054         * @param callbacks The callbacks requested.
055         * @throws IOException
056         * @throws UnsupportedCallbackException Thrown if an unkown Callback type is encountered.
057         */
058        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
059            for (int i = 0; i < callbacks.length; i++) {
060                Callback callback = callbacks[i];
061                if (callback instanceof CertificateCallback) {
062                    CertificateCallback certCallback = (CertificateCallback) callback;
063                    
064                    certCallback.setCertificates(certificates);
065                    
066                } else {
067                    throw new UnsupportedCallbackException(callback);
068                }
069            }
070        }
071    }