Merge from the pain train
[official-gcc.git] / libjava / gnu / java / security / x509 / X509CRLEntry.java
blobda161153c624331fea1ce5d400ed2b13c8fffcef
1 /* X509CRLEntry.java -- an entry in a X.509 CRL.
2 Copyright (C) 2003, 2004 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.IOException;
43 import java.math.BigInteger;
45 import java.security.cert.CRLException;
47 import java.util.Collection;
48 import java.util.Collections;
49 import java.util.Date;
50 import java.util.HashMap;
51 import java.util.HashSet;
52 import java.util.Iterator;
53 import java.util.Set;
55 import gnu.java.security.OID;
56 import gnu.java.security.der.*;
57 import gnu.java.security.x509.ext.*;
59 /**
60 * A single entry in a X.509 certificate revocation list.
62 * @see X509CRL
63 * @author Casey Marshall
65 class X509CRLEntry extends java.security.cert.X509CRLEntry
66 implements GnuPKIExtension
69 // Constants and fields.
70 // ------------------------------------------------------------------------
72 private static final boolean DEBUG = false;
73 private static void debug(String msg)
75 if (DEBUG)
77 System.err.print(">> X509CRLEntry: ");
78 System.err.println(msg);
82 /** The DER encoded form of this CRL entry. */
83 private byte[] encoded;
85 /** The revoked certificate's serial number. */
86 private BigInteger serialNo;
88 /** The date the certificate was revoked. */
89 private Date revocationDate;
91 /** The CRL entry extensions. */
92 private HashMap extensions;
94 // Constructor.
95 // ------------------------------------------------------------------------
97 /**
98 * Create a new X.509 certificate revocation list entry from the given
99 * input stream and CRL version number.
101 * @param version The CRL version.
102 * @param encoded The stream of DER bytes.
103 * @throws CRLException If the ASN.1 structure is invalid.
104 * @throws IOException If the bytes cannot be read.
106 X509CRLEntry(int version, DERReader encoded)
107 throws CRLException, IOException
109 super();
110 extensions = new HashMap();
113 parse(version, encoded);
115 catch (IOException ioe)
117 throw ioe;
119 catch (Exception x)
121 throw new CRLException(x.toString());
125 // X509CRLEntry methods.
126 // ------------------------------------------------------------------------
128 public boolean equals(Object o)
130 if (!(o instanceof X509CRLEntry))
131 return false;
132 return ((X509CRLEntry) o).getSerialNumber().equals(serialNo) &&
133 ((X509CRLEntry) o).getRevocationDate().equals(revocationDate);
136 public int hashCode()
138 return serialNo.hashCode();
141 public byte[] getEncoded() throws CRLException
143 return (byte[]) encoded.clone();
146 public BigInteger getSerialNumber()
148 return serialNo;
151 public Date getRevocationDate()
153 return (Date) revocationDate.clone();
156 public boolean hasExtensions()
158 return ! extensions.isEmpty();
161 public String toString()
163 return "X509CRLEntry serial=" + serialNo + " revocation date="
164 + revocationDate + " ext=" + extensions;
167 // X509Extension methods.
168 // -------------------------------------------------------------------------
170 public boolean hasUnsupportedCriticalExtension()
172 for (Iterator it = extensions.values().iterator(); it.hasNext(); )
174 Extension e = (Extension) it.next();
175 if (e.isCritical() && !e.isSupported())
176 return true;
178 return false;
181 public Set getCriticalExtensionOIDs()
183 HashSet s = new HashSet();
184 for (Iterator it = extensions.values().iterator(); it.hasNext(); )
186 Extension e = (Extension) it.next();
187 if (e.isCritical())
188 s.add(e.getOid().toString());
190 return Collections.unmodifiableSet(s);
193 public Set getNonCriticalExtensionOIDs()
195 HashSet s = new HashSet();
196 for (Iterator it = extensions.values().iterator(); it.hasNext(); )
198 Extension e = (Extension) it.next();
199 if (!e.isCritical())
200 s.add(e.getOid().toString());
202 return Collections.unmodifiableSet(s);
205 public byte[] getExtensionValue(String oid)
207 Extension e = getExtension(new OID(oid));
208 if (e != null)
210 return e.getValue().getEncoded();
212 return null;
215 // GnuPKIExtension method.
216 // -------------------------------------------------------------------------
218 public Extension getExtension(OID oid)
220 return (Extension) extensions.get(oid);
223 public Collection getExtensions()
225 return extensions.values();
228 // Own methods.
229 // -------------------------------------------------------------------------
231 private void parse(int version, DERReader der) throws Exception
233 // RevokedCertificate ::= SEQUENCE {
234 DERValue entry = der.read();
235 debug("start CRL entry len == " + entry.getLength());
236 if (!entry.isConstructed())
237 throw new IOException("malformed revokedCertificate");
238 encoded = entry.getEncoded();
239 int len = 0;
241 debug("encoded entry:\n" + Util.hexDump(encoded, ">>>> "));
243 // userCertificate CertificateSerialNumber,
244 DERValue val = der.read();
245 serialNo = (BigInteger) val.getValue();
246 len += val.getEncodedLength();
247 debug("userCertificate == " + serialNo + " current count == " + len);
249 // revocationDate Time,
250 val = der.read();
251 revocationDate = (Date) val.getValue();
252 len += val.getEncodedLength();
253 debug("revocationDate == " + revocationDate + " current count == " + len);
255 // crlEntryExtensions Extensions OPTIONAL
256 // -- if present MUST be v2
257 if (len < entry.getLength())
259 if (version < 2)
260 throw new IOException("extra data in CRL entry");
261 DERValue exts = der.read();
262 if (!exts.isConstructed())
263 throw new IOException("malformed Extensions");
264 debug("start Extensions len == " + exts.getLength());
265 len = 0;
266 while (len < exts.getLength())
268 val = der.read();
269 if (!val.isConstructed())
270 throw new IOException("malformed Extension");
271 debug("start Extension len == " + val.getLength());
272 Extension e = new Extension(val.getEncoded());
273 extensions.put(e.getOid(), e);
274 der.skip(val.getLength());
275 len += val.getEncodedLength();
276 debug("current count == " + len);