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    
019    package org.apache.geronimo.util.jce.provider;
020    
021    import java.io.ByteArrayOutputStream;
022    import java.io.IOException;
023    import java.math.BigInteger;
024    import java.security.cert.CRLException;
025    import java.security.cert.X509CRLEntry;
026    import java.util.Date;
027    import java.util.Enumeration;
028    import java.util.HashSet;
029    import java.util.Set;
030    
031    import org.apache.geronimo.util.asn1.DERObjectIdentifier;
032    import org.apache.geronimo.util.asn1.DEROutputStream;
033    import org.apache.geronimo.util.asn1.x509.TBSCertList;
034    import org.apache.geronimo.util.asn1.x509.X509Extension;
035    import org.apache.geronimo.util.asn1.x509.X509Extensions;
036    
037    /**
038     * The following extensions are listed in RFC 2459 as relevant to CRL Entries
039     *
040     * ReasonCode
041     * Hode Instruction Code
042     * Invalidity Date
043     * Certificate Issuer (critical)
044     */
045    public class X509CRLEntryObject extends X509CRLEntry
046    {
047        private TBSCertList.CRLEntry c;
048    
049        public X509CRLEntryObject(
050            TBSCertList.CRLEntry c)
051        {
052            this.c = c;
053        }
054    
055        /**
056         * Will return true if any extensions are present and marked
057         * as critical as we currently dont handle any extensions!
058         */
059        public boolean hasUnsupportedCriticalExtension()
060        {
061            Set extns = getCriticalExtensionOIDs();
062            if ( extns != null && !extns.isEmpty() )
063            {
064                return true;
065            }
066    
067            return false;
068        }
069    
070        private Set getExtensionOIDs(boolean critical)
071        {
072            X509Extensions extensions = c.getExtensions();
073    
074            if ( extensions != null )
075            {
076                HashSet            set = new HashSet();
077                Enumeration        e = extensions.oids();
078    
079                while (e.hasMoreElements())
080                {
081                    DERObjectIdentifier    oid = (DERObjectIdentifier)e.nextElement();
082                    X509Extension        ext = extensions.getExtension(oid);
083    
084                    if (critical == ext.isCritical())
085                    {
086                        set.add(oid.getId());
087                    }
088                }
089    
090                return set;
091            }
092    
093            return null;
094        }
095    
096        public Set getCriticalExtensionOIDs()
097        {
098            return getExtensionOIDs(true);
099        }
100    
101        public Set getNonCriticalExtensionOIDs()
102        {
103            return getExtensionOIDs(false);
104        }
105    
106        public byte[] getExtensionValue(String oid)
107        {
108            X509Extensions exts = c.getExtensions();
109    
110            if (exts != null)
111            {
112                X509Extension ext = exts.getExtension(new DERObjectIdentifier(oid));
113    
114                if (ext != null)
115                {
116                    try
117                    {
118                        return ext.getValue().getEncoded();
119                    }
120                    catch (Exception e)
121                    {
122                        throw new RuntimeException("error encoding " + e.getMessage(), e);
123                    }
124                }
125            }
126    
127            return null;
128        }
129    
130        public byte[] getEncoded()
131            throws CRLException
132        {
133            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
134            DEROutputStream            dOut = new DEROutputStream(bOut);
135    
136            try
137            {
138                dOut.writeObject(c);
139    
140                return bOut.toByteArray();
141            }
142            catch (IOException e)
143            {
144                throw (CRLException)new CRLException(e.getMessage()).initCause(e);
145            }
146        }
147    
148        public BigInteger getSerialNumber()
149        {
150            return c.getUserCertificate().getValue();
151        }
152    
153        public Date getRevocationDate()
154        {
155            return c.getRevocationDate().getDate();
156        }
157    
158        public boolean hasExtensions()
159        {
160            return c.getExtensions() != null;
161        }
162    
163        public String toString()
164        {
165            StringBuffer buf = new StringBuffer();
166            String nl = System.getProperty("line.separator");
167    
168            buf.append("      userCertificate: " + this.getSerialNumber() + nl);
169            buf.append("       revocationDate: " + this.getRevocationDate() + nl);
170    
171    
172            X509Extensions extensions = c.getExtensions();
173    
174            if ( extensions != null )
175            {
176                Enumeration e = extensions.oids();
177                if ( e.hasMoreElements() )
178                {
179                    buf.append("   crlEntryExtensions:" + nl);
180    
181                    while ( e.hasMoreElements() )
182                    {
183                        DERObjectIdentifier oid = (DERObjectIdentifier)e.nextElement();
184                        X509Extension ext = extensions.getExtension(oid);
185                        buf.append(ext);
186                    }
187                }
188            }
189    
190            return buf.toString();
191        }
192    }