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.corba.security.config.css;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    import org.omg.CORBA.Any;
024    import org.omg.CORBA.UserException;
025    import org.omg.CSI.EstablishContext;
026    import org.omg.CSI.SASContextBody;
027    import org.omg.CSI.SASContextBodyHelper;
028    import org.omg.IOP.SecurityAttributeService;
029    import org.omg.IOP.ServiceContext;
030    
031    import org.apache.geronimo.corba.security.config.ConfigUtil;
032    import org.apache.geronimo.corba.security.config.tss.TSSCompoundSecMechConfig;
033    import org.apache.geronimo.corba.util.Util;
034    
035    
036    /**
037     * @version $Rev: 503274 $ $Date: 2007-02-03 10:19:18 -0800 (Sat, 03 Feb 2007) $
038     */
039    public class CSSCompoundSecMechConfig implements Serializable {
040    
041        private final static Log log = LogFactory.getLog(CSSCompoundSecMechConfig.class);
042    
043        private short supports;
044        private short requires;
045        private CSSTransportMechConfig transport_mech;
046        private CSSASMechConfig as_mech;
047        private CSSSASMechConfig sas_mech;
048    
049        public CSSTransportMechConfig getTransport_mech() {
050            return transport_mech;
051        }
052    
053        public void setTransport_mech(CSSTransportMechConfig transport_mech) {
054            this.transport_mech = transport_mech;
055            this.supports |= transport_mech.getSupports();
056            this.requires |= transport_mech.getRequires();
057        }
058    
059        public CSSASMechConfig getAs_mech() {
060            return as_mech;
061        }
062    
063        public void setAs_mech(CSSASMechConfig as_mech) {
064            this.as_mech = as_mech;
065            this.supports |= as_mech.getSupports();
066            this.requires |= as_mech.getRequires();
067        }
068    
069        public CSSSASMechConfig getSas_mech() {
070            return sas_mech;
071        }
072    
073        public void setSas_mech(CSSSASMechConfig sas_mech) {
074            this.sas_mech = sas_mech;
075            this.supports |= sas_mech.getSupports();
076            this.requires |= sas_mech.getRequires();
077        }
078    
079        public boolean canHandle(TSSCompoundSecMechConfig requirement) {
080    
081            if (log.isDebugEnabled()) {
082                log.debug("canHandle()");
083                log.debug("    CSS SUPPORTS: " + ConfigUtil.flags(supports));
084                log.debug("    CSS REQUIRES: " + ConfigUtil.flags(requires));
085                log.debug("    TSS SUPPORTS: " + ConfigUtil.flags(requirement.getSupports()));
086                log.debug("    TSS REQUIRES: " + ConfigUtil.flags(requirement.getRequires()));
087            }
088    
089            if ((supports & requirement.getRequires()) != requirement.getRequires()) return false;
090            if ((requires & requirement.getSupports()) != requires) return false;
091    
092            if (!transport_mech.canHandle(requirement.getTransport_mech())) return false;
093            if (!as_mech.canHandle(requirement.getAs_mech())) return false;
094            if (!sas_mech.canHandle(requirement.getSas_mech())) return false;
095    
096            return true;
097        }
098    
099        public ServiceContext generateServiceContext() throws UserException {
100    
101            if (as_mech instanceof CSSNULLASMechConfig && sas_mech.getIdentityToken() instanceof CSSSASITTAbsent) return null;
102    
103            EstablishContext msg = new EstablishContext();
104    
105            msg.client_context_id = 0;
106            msg.client_authentication_token = as_mech.encode();
107            msg.authorization_token = sas_mech.encodeAuthorizationElement();
108            msg.identity_token = sas_mech.encodeIdentityToken();
109    
110            ServiceContext context = new ServiceContext();
111    
112            SASContextBody sas = new SASContextBody();
113            sas.establish_msg(msg);
114            Any sas_any = Util.getORB().create_any();
115            SASContextBodyHelper.insert(sas_any, sas);
116            context.context_data = Util.getCodec().encode_value(sas_any);
117    
118            context.context_id = SecurityAttributeService.value;
119    
120            return context;
121        }
122    
123        public String toString() {
124            StringBuffer buf = new StringBuffer();
125            toString("", buf);
126            return buf.toString();
127        }
128    
129        void toString(String spaces, StringBuffer buf) {
130            String moreSpaces = spaces + "  ";
131            buf.append(spaces).append("CSSCompoundSecMechConfig: [\n");
132            buf.append(moreSpaces).append("SUPPORTS: ").append(ConfigUtil.flags(supports)).append("\n");
133            buf.append(moreSpaces).append("REQUIRES: ").append(ConfigUtil.flags(requires)).append("\n");
134            transport_mech.toString(moreSpaces, buf);
135            as_mech.toString(moreSpaces, buf);
136            sas_mech.toString(moreSpaces, buf);
137            buf.append(spaces).append("]\n");
138        }
139        
140    }