2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / security / x509 / X509CRLEntry.java
blob4057c60a22afd6c0753c5883eeddf1768d9cd36e
1 /* X509CRLEntry.java -- entry in a X.509 CRL.
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.java.security.x509;
41 import java.io.InputStream;
42 import java.io.IOException;
44 import java.math.BigInteger;
46 import java.security.cert.CRLException;
48 import java.util.Collections;
49 import java.util.Date;
50 import java.util.HashMap;
51 import java.util.HashSet;
52 import java.util.Set;
54 import gnu.java.io.ASN1ParsingException;
55 import gnu.java.security.OID;
56 import gnu.java.security.der.DERReader;
57 import gnu.java.security.der.DERValue;
58 import gnu.java.security.der.DERWriter;
60 /**
61 * A single entry in a X.509 certificate revocation list.
63 * @see X509CRL
64 * @author Casey Marshall
66 class X509CRLEntry extends java.security.cert.X509CRLEntry
69 // Constants and fields.
70 // ------------------------------------------------------------------------
72 /** The DER encoded form of this CRL entry. */
73 private byte[] encoded;
75 /** The revoked certificate's serial number. */
76 private BigInteger serialNo;
78 /** The date the certificate was revoked. */
79 private Date revocationDate;
81 /** The encoded extensions. */
82 private HashMap extensions;
84 /** The set of critical extension OIDs. */
85 private HashSet critOids;
87 /** the set of non-critical extension OIDs. */
88 private HashSet nonCritOids;
90 // Constructor.
91 // ------------------------------------------------------------------------
93 /**
94 * Create a new X.509 certificate revocation list entry from the given
95 * input stream and CRL version number.
97 * @param version The CRL version.
98 * @param encoded The stream of DER bytes.
99 * @throws CRLException If the ASN.1 structure is invalid.
100 * @throws IOException If the bytes cannot be read.
102 X509CRLEntry(int version, InputStream encoded)
103 throws CRLException, IOException
105 super();
106 extensions = new HashMap();
107 critOids = new HashSet();
108 nonCritOids = new HashSet();
111 parse(version, encoded);
113 catch (IOException ioe)
115 throw ioe;
117 catch (Exception x)
119 throw new CRLException(x.toString());
123 // X509CRLEntry methods.
124 // ------------------------------------------------------------------------
126 public boolean equals(Object o)
128 return ((X509CRLEntry) o).serialNo.equals(serialNo) &&
129 ((X509CRLEntry) o).revocationDate.equals(revocationDate);
132 public int hashCode()
134 return serialNo.hashCode();
137 public byte[] getEncoded() throws CRLException
139 return (byte[]) encoded.clone();
142 public BigInteger getSerialNumber()
144 return serialNo;
147 public Date getRevocationDate()
149 return (Date) revocationDate.clone();
152 public boolean hasExtensions()
154 return ! extensions.isEmpty();
157 public String toString()
159 return "X509CRLEntry serial=" + serialNo + " revocation date="
160 + revocationDate + " critExt=" + critOids + " ext=" + nonCritOids;
163 // X509Extension methods.
164 // ------------------------------------------------------------------------
166 public boolean hasUnsupportedCriticalExtension()
168 return false; // XXX
171 public Set getCriticalExtensionOIDs()
173 return Collections.unmodifiableSet(critOids);
176 public Set getNonCriticalExtensionOIDs()
178 return Collections.unmodifiableSet(nonCritOids);
181 public byte[] getExtensionValue(String oid)
183 byte[] ext = (byte[]) extensions.get(oid);
184 if (ext != null)
185 return (byte[]) ext.clone();
186 return null;
189 // Own methods.
190 // ------------------------------------------------------------------------
192 private void parse(int version, InputStream in) throws Exception
194 DERReader der = new DERReader(in);
195 DERValue entry = der.read();
196 if (!entry.isConstructed())
197 throw new ASN1ParsingException("malformed revokedCertificate");
198 encoded = entry.getEncoded();
199 int len = 0;
200 DERValue val = der.read();
201 serialNo = (BigInteger) val.getValue();
202 len += DERWriter.definiteEncodingSize(val.getLength())
203 + val.getLength() + 1;
204 val = der.read();
205 revocationDate = (Date) val.getValue();
206 len += DERWriter.definiteEncodingSize(val.getLength())
207 + val.getLength() + 1;
209 if (len < entry.getLength())
211 if (version < 2)
212 throw new ASN1ParsingException("extra data in CRL entry");
213 while (len < entry.getLength())
215 val = der.read();
216 if (!val.isConstructed())
217 throw new ASN1ParsingException("malformed Extension");
218 OID extOid = (OID) der.read().getValue();
219 Boolean critical = Boolean.valueOf(false);
220 DERValue val2 = der.read();
221 if (val2.getValue() instanceof Boolean)
223 critical = (Boolean) val2.getValue();
224 val2 = der.read();
226 byte[] ext = (byte[]) val2.getValue();
227 extensions.put(extOid.toString(), ext);
228 if (critical.booleanValue())
229 critOids.add(extOid.toString());
230 else
231 nonCritOids.add(extOid.toString());
232 len += val.getEncodedLength();