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    
018    package org.apache.geronimo.util.asn1.x509;
019    
020    import java.util.Enumeration;
021    
022    import org.apache.geronimo.util.asn1.ASN1Encodable;
023    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
024    import org.apache.geronimo.util.asn1.ASN1Sequence;
025    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
026    import org.apache.geronimo.util.asn1.DERObject;
027    import org.apache.geronimo.util.asn1.DERSequence;
028    import org.apache.geronimo.util.asn1.DERTaggedObject;
029    
030    public class NameConstraints
031        extends ASN1Encodable
032    {
033        ASN1Sequence    permitted, excluded;
034    
035        public NameConstraints(
036            ASN1Sequence    seq)
037        {
038            Enumeration e = seq.getObjects();
039            while (e.hasMoreElements())
040            {
041                ASN1TaggedObject    o = (ASN1TaggedObject)e.nextElement();
042                switch (o.getTagNo())
043                {
044                case 0:
045                    permitted = ASN1Sequence.getInstance(o, false);
046                    break;
047                case 1:
048                    excluded = ASN1Sequence.getInstance(o, false);
049                    break;
050                }
051            }
052        }
053    
054        public ASN1Sequence getPermittedSubtrees()
055        {
056            return permitted;
057        }
058    
059        public ASN1Sequence getExcludedSubtrees()
060        {
061            return excluded;
062        }
063    
064        /*
065         * NameConstraints ::= SEQUENCE {
066         *      permittedSubtrees       [0]     GeneralSubtrees OPTIONAL,
067         *      excludedSubtrees        [1]     GeneralSubtrees OPTIONAL }
068         */
069        public DERObject toASN1Object()
070        {
071            ASN1EncodableVector   v = new ASN1EncodableVector();
072    
073            if (permitted != null)
074            {
075                v.add(new DERTaggedObject(false, 0, permitted));
076            }
077    
078            if (excluded != null)
079            {
080                v.add(new DERTaggedObject(false, 1, excluded));
081            }
082    
083            return new DERSequence(v);
084        }
085    }