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