0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.x509;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.io.OutputStream;
0N/Aimport java.lang.reflect.Constructor;
0N/Aimport java.lang.reflect.InvocationTargetException;
0N/Aimport java.security.cert.CRLException;
0N/Aimport java.security.cert.CertificateException;
0N/Aimport java.util.Collection;
5090N/Aimport java.util.Collections;
0N/Aimport java.util.Enumeration;
5090N/Aimport java.util.Map;
5090N/Aimport java.util.TreeMap;
0N/A
0N/Aimport sun.security.util.*;
0N/Aimport sun.misc.HexDumpEncoder;
0N/A
0N/A/**
0N/A * This class defines the CRL Extensions.
0N/A * It is used for both CRL Extensions and CRL Entry Extensions,
0N/A * which are defined are follows:
0N/A * <pre>
0N/A * TBSCertList ::= SEQUENCE {
0N/A * version Version OPTIONAL, -- if present, must be v2
0N/A * signature AlgorithmIdentifier,
0N/A * issuer Name,
0N/A * thisUpdate Time,
0N/A * nextUpdate Time OPTIONAL,
0N/A * revokedCertificates SEQUENCE OF SEQUENCE {
0N/A * userCertificate CertificateSerialNumber,
0N/A * revocationDate Time,
0N/A * crlEntryExtensions Extensions OPTIONAL -- if present, must be v2
0N/A * } OPTIONAL,
0N/A * crlExtensions [0] EXPLICIT Extensions OPTIONAL -- if present, must be v2
0N/A * }
0N/A * </pre>
0N/A *
0N/A * @author Hemma Prafullchandra
0N/A */
0N/Apublic class CRLExtensions {
0N/A
5090N/A private Map<String,Extension> map = Collections.synchronizedMap(
5090N/A new TreeMap<String,Extension>());
0N/A private boolean unsupportedCritExt = false;
0N/A
0N/A /**
0N/A * Default constructor.
0N/A */
0N/A public CRLExtensions() { }
0N/A
0N/A /**
0N/A * Create the object, decoding the values from the passed DER stream.
0N/A *
0N/A * @param in the DerInputStream to read the Extension from, i.e. the
0N/A * sequence of extensions.
0N/A * @exception CRLException on decoding errors.
0N/A */
0N/A public CRLExtensions(DerInputStream in) throws CRLException {
0N/A init(in);
0N/A }
0N/A
0N/A // helper routine
0N/A private void init(DerInputStream derStrm) throws CRLException {
0N/A try {
0N/A DerInputStream str = derStrm;
0N/A
0N/A byte nextByte = (byte)derStrm.peekByte();
0N/A // check for context specific byte 0; skip it
0N/A if (((nextByte & 0x0c0) == 0x080) &&
0N/A ((nextByte & 0x01f) == 0x000)) {
0N/A DerValue val = str.getDerValue();
0N/A str = val.data;
0N/A }
0N/A
0N/A DerValue[] exts = str.getSequence(5);
0N/A for (int i = 0; i < exts.length; i++) {
0N/A Extension ext = new Extension(exts[i]);
0N/A parseExtension(ext);
0N/A }
0N/A } catch (IOException e) {
0N/A throw new CRLException("Parsing error: " + e.toString());
0N/A }
0N/A }
0N/A
0N/A private static final Class[] PARAMS = {Boolean.class, Object.class};
0N/A
0N/A // Parse the encoded extension
0N/A private void parseExtension(Extension ext) throws CRLException {
0N/A try {
0N/A Class extClass = OIDMap.getClass(ext.getExtensionId());
0N/A if (extClass == null) { // Unsupported extension
0N/A if (ext.isCritical())
0N/A unsupportedCritExt = true;
0N/A if (map.put(ext.getExtensionId().toString(), ext) != null)
0N/A throw new CRLException("Duplicate extensions not allowed");
0N/A return;
0N/A }
0N/A Constructor cons = ((Class<?>)extClass).getConstructor(PARAMS);
0N/A Object[] passed = new Object[] {Boolean.valueOf(ext.isCritical()),
0N/A ext.getExtensionValue()};
0N/A CertAttrSet crlExt = (CertAttrSet)cons.newInstance(passed);
0N/A if (map.put(crlExt.getName(), (Extension)crlExt) != null) {
0N/A throw new CRLException("Duplicate extensions not allowed");
0N/A }
0N/A } catch (InvocationTargetException invk) {
0N/A throw new CRLException(invk.getTargetException().getMessage());
0N/A } catch (Exception e) {
0N/A throw new CRLException(e.toString());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Encode the extensions in DER form to the stream.
0N/A *
0N/A * @param out the DerOutputStream to marshal the contents to.
0N/A * @param isExplicit the tag indicating whether this is an entry
0N/A * extension (false) or a CRL extension (true).
0N/A * @exception CRLException on encoding errors.
0N/A */
0N/A public void encode(OutputStream out, boolean isExplicit)
0N/A throws CRLException {
0N/A try {
0N/A DerOutputStream extOut = new DerOutputStream();
0N/A Collection<Extension> allExts = map.values();
0N/A Object[] objs = allExts.toArray();
0N/A
0N/A for (int i = 0; i < objs.length; i++) {
0N/A if (objs[i] instanceof CertAttrSet)
0N/A ((CertAttrSet)objs[i]).encode(extOut);
0N/A else if (objs[i] instanceof Extension)
0N/A ((Extension)objs[i]).encode(extOut);
0N/A else
0N/A throw new CRLException("Illegal extension object");
0N/A }
0N/A
0N/A DerOutputStream seq = new DerOutputStream();
0N/A seq.write(DerValue.tag_Sequence, extOut);
0N/A
0N/A DerOutputStream tmp = new DerOutputStream();
0N/A if (isExplicit)
0N/A tmp.write(DerValue.createTag(DerValue.TAG_CONTEXT,
0N/A true, (byte)0), seq);
0N/A else
0N/A tmp = seq;
0N/A
0N/A out.write(tmp.toByteArray());
0N/A } catch (IOException e) {
0N/A throw new CRLException("Encoding error: " + e.toString());
0N/A } catch (CertificateException e) {
0N/A throw new CRLException("Encoding error: " + e.toString());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the extension with this alias.
0N/A *
0N/A * @param alias the identifier string for the extension to retrieve.
0N/A */
0N/A public Extension get(String alias) {
0N/A X509AttributeName attr = new X509AttributeName(alias);
0N/A String name;
0N/A String id = attr.getPrefix();
0N/A if (id.equalsIgnoreCase(X509CertImpl.NAME)) { // fully qualified
0N/A int index = alias.lastIndexOf(".");
0N/A name = alias.substring(index + 1);
0N/A } else
0N/A name = alias;
0N/A return map.get(name);
0N/A }
0N/A
0N/A /**
0N/A * Set the extension value with this alias.
0N/A *
0N/A * @param alias the identifier string for the extension to set.
0N/A * @param obj the Object to set the extension identified by the
0N/A * alias.
0N/A */
0N/A public void set(String alias, Object obj) {
0N/A map.put(alias, (Extension)obj);
0N/A }
0N/A
0N/A /**
0N/A * Delete the extension value with this alias.
0N/A *
0N/A * @param alias the identifier string for the extension to delete.
0N/A */
0N/A public void delete(String alias) {
0N/A map.remove(alias);
0N/A }
0N/A
0N/A /**
0N/A * Return an enumeration of the extensions.
0N/A * @return an enumeration of the extensions in this CRL.
0N/A */
0N/A public Enumeration<Extension> getElements() {
5090N/A return Collections.enumeration(map.values());
0N/A }
0N/A
0N/A /**
0N/A * Return a collection view of the extensions.
0N/A * @return a collection view of the extensions in this CRL.
0N/A */
0N/A public Collection<Extension> getAllExtensions() {
0N/A return map.values();
0N/A }
0N/A
0N/A /**
0N/A * Return true if a critical extension is found that is
0N/A * not supported, otherwise return false.
0N/A */
0N/A public boolean hasUnsupportedCriticalExtension() {
0N/A return unsupportedCritExt;
0N/A }
0N/A
0N/A /**
0N/A * Compares this CRLExtensions for equality with the specified
0N/A * object. If the <code>other</code> object is an
0N/A * <code>instanceof</code> <code>CRLExtensions</code>, then
0N/A * all the entries are compared with the entries from this.
0N/A *
0N/A * @param other the object to test for equality with this CRLExtensions.
0N/A * @return true iff all the entries match that of the Other,
0N/A * false otherwise.
0N/A */
0N/A public boolean equals(Object other) {
0N/A if (this == other)
0N/A return true;
0N/A if (!(other instanceof CRLExtensions))
0N/A return false;
0N/A Collection<Extension> otherC =
0N/A ((CRLExtensions)other).getAllExtensions();
0N/A Object[] objs = otherC.toArray();
0N/A
0N/A int len = objs.length;
0N/A if (len != map.size())
0N/A return false;
0N/A
0N/A Extension otherExt, thisExt;
0N/A String key = null;
0N/A for (int i = 0; i < len; i++) {
0N/A if (objs[i] instanceof CertAttrSet)
0N/A key = ((CertAttrSet)objs[i]).getName();
0N/A otherExt = (Extension)objs[i];
0N/A if (key == null)
0N/A key = otherExt.getExtensionId().toString();
0N/A thisExt = map.get(key);
0N/A if (thisExt == null)
0N/A return false;
0N/A if (! thisExt.equals(otherExt))
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Returns a hashcode value for this CRLExtensions.
0N/A *
0N/A * @return the hashcode value.
0N/A */
0N/A public int hashCode() {
0N/A return map.hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this <tt>CRLExtensions</tt> object
0N/A * in the form of a set of entries, enclosed in braces and separated
0N/A * by the ASCII characters "<tt>,&nbsp;</tt>" (comma and space).
0N/A * <p>Overrides to <tt>toString</tt> method of <tt>Object</tt>.
0N/A *
0N/A * @return a string representation of this CRLExtensions.
0N/A */
0N/A public String toString() {
0N/A return map.toString();
0N/A }
0N/A}