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 org.apache.geronimo.util.asn1.ASN1Encodable;
021    import org.apache.geronimo.util.asn1.ASN1EncodableVector;
022    import org.apache.geronimo.util.asn1.ASN1Sequence;
023    import org.apache.geronimo.util.asn1.ASN1TaggedObject;
024    import org.apache.geronimo.util.asn1.DERObject;
025    import org.apache.geronimo.util.asn1.DERSequence;
026    import org.apache.geronimo.util.asn1.DERTaggedObject;
027    
028    /**
029     * The Holder object.
030     * <pre>
031     *  Holder ::= SEQUENCE {
032     *        baseCertificateID   [0] IssuerSerial OPTIONAL,
033     *                 -- the issuer and serial number of
034     *                 -- the holder's Public Key Certificate
035     *        entityName          [1] GeneralNames OPTIONAL,
036     *                 -- the name of the claimant or role
037     *        objectDigestInfo    [2] ObjectDigestInfo OPTIONAL
038     *                 -- used to directly authenticate the holder,
039     *                 -- for example, an executable
040     *  }
041     * </pre>
042     */
043    public class Holder
044        extends ASN1Encodable
045    {
046        IssuerSerial        baseCertificateID;
047        GeneralNames        entityName;
048        ObjectDigestInfo    objectDigestInfo;
049    
050        public static Holder getInstance(
051                Object  obj)
052        {
053            if (obj instanceof Holder)
054            {
055                return (Holder)obj;
056            }
057            else if (obj instanceof ASN1Sequence)
058            {
059                return new Holder((ASN1Sequence)obj);
060            }
061    
062            throw new IllegalArgumentException("unknown object in factory");
063        }
064    
065        public Holder(
066            ASN1Sequence    seq)
067        {
068            for (int i = 0; i != seq.size(); i++)
069            {
070                ASN1TaggedObject    tObj = (ASN1TaggedObject)seq.getObjectAt(i);
071    
072                switch (tObj.getTagNo())
073                {
074                case 0:
075                    baseCertificateID = IssuerSerial.getInstance(tObj, false);
076                    break;
077                case 1:
078                    entityName = GeneralNames.getInstance(tObj, false);
079                    break;
080                case 2:
081                    objectDigestInfo = ObjectDigestInfo.getInstance(tObj, false);
082                    break;
083                default:
084                    throw new IllegalArgumentException("unknown tag in Holder");
085                }
086            }
087        }
088    
089        public Holder(
090            IssuerSerial    baseCertificateID)
091        {
092            this.baseCertificateID = baseCertificateID;
093        }
094    
095        public Holder(
096            GeneralNames    entityName)
097        {
098            this.entityName = entityName;
099        }
100    
101        public IssuerSerial getBaseCertificateID()
102        {
103            return baseCertificateID;
104        }
105    
106        public GeneralNames getEntityName()
107        {
108            return entityName;
109        }
110    
111        public ObjectDigestInfo getObjectDigestInfo()
112        {
113            return objectDigestInfo;
114        }
115    
116        public DERObject toASN1Object()
117        {
118            ASN1EncodableVector  v = new ASN1EncodableVector();
119    
120            if (baseCertificateID != null)
121            {
122                v.add(new DERTaggedObject(false, 0, baseCertificateID));
123            }
124    
125            if (entityName != null)
126            {
127                v.add(new DERTaggedObject(false, 1, entityName));
128            }
129    
130            if (objectDigestInfo != null)
131            {
132                v.add(new DERTaggedObject(false, 2, objectDigestInfo));
133            }
134    
135            return new DERSequence(v);
136        }
137    }