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.util.Arrays;
0N/Aimport sun.security.util.*;
0N/A
0N/A/**
0N/A * Represent a X509 Extension Attribute.
0N/A *
0N/A * <p>Extensions are additional attributes which can be inserted in a X509
0N/A * v3 certificate. For example a "Driving License Certificate" could have
0N/A * the driving license number as a extension.
0N/A *
0N/A * <p>Extensions are represented as a sequence of the extension identifier
0N/A * (Object Identifier), a boolean flag stating whether the extension is to
0N/A * be treated as being critical and the extension value itself (this is again
0N/A * a DER encoding of the extension value).
0N/A * <pre>
0N/A * ASN.1 definition of Extension:
0N/A * Extension ::= SEQUENCE {
0N/A * ExtensionId OBJECT IDENTIFIER,
0N/A * critical BOOLEAN DEFAULT FALSE,
0N/A * extensionValue OCTET STRING
0N/A * }
0N/A * </pre>
0N/A * All subclasses need to implement a constructor of the form
0N/A * <pre>
0N/A * <subclass> (Boolean, Object)
0N/A * </pre>
0N/A * where the Object is typically an array of DER encoded bytes.
0N/A * <p>
0N/A * @author Amit Kapoor
0N/A * @author Hemma Prafullchandra
0N/A */
0N/Apublic class Extension implements java.security.cert.Extension {
0N/A
0N/A protected ObjectIdentifier extensionId = null;
0N/A protected boolean critical = false;
0N/A protected byte[] extensionValue = null;
0N/A
0N/A /**
0N/A * Default constructor. Used only by sub-classes.
0N/A */
0N/A public Extension() { }
0N/A
0N/A /**
0N/A * Constructs an extension from a DER encoded array of bytes.
0N/A */
0N/A public Extension(DerValue derVal) throws IOException {
0N/A
0N/A DerInputStream in = derVal.toDerInputStream();
0N/A
0N/A // Object identifier
0N/A extensionId = in.getOID();
0N/A
0N/A // If the criticality flag was false, it will not have been encoded.
0N/A DerValue val = in.getDerValue();
0N/A if (val.tag == DerValue.tag_Boolean) {
0N/A critical = val.getBoolean();
0N/A
0N/A // Extension value (DER encoded)
0N/A val = in.getDerValue();
0N/A extensionValue = val.getOctetString();
0N/A } else {
0N/A critical = false;
0N/A extensionValue = val.getOctetString();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constructs an Extension from individual components of ObjectIdentifier,
0N/A * criticality and the DER encoded OctetString.
0N/A *
0N/A * @param extensionId the ObjectIdentifier of the extension
0N/A * @param critical the boolean indicating if the extension is critical
0N/A * @param extensionValue the DER encoded octet string of the value.
0N/A */
0N/A public Extension(ObjectIdentifier extensionId, boolean critical,
0N/A byte[] extensionValue) throws IOException {
0N/A this.extensionId = extensionId;
0N/A this.critical = critical;
0N/A // passed in a DER encoded octet string, strip off the tag
0N/A // and length
0N/A DerValue inDerVal = new DerValue(extensionValue);
0N/A this.extensionValue = inDerVal.getOctetString();
0N/A }
0N/A
0N/A /**
0N/A * Constructs an Extension from another extension. To be used for
0N/A * creating decoded subclasses.
0N/A *
0N/A * @param ext the extension to create from.
0N/A */
0N/A public Extension(Extension ext) {
0N/A this.extensionId = ext.extensionId;
0N/A this.critical = ext.critical;
0N/A this.extensionValue = ext.extensionValue;
0N/A }
0N/A
0N/A /**
0N/A * Constructs an Extension from individual components of ObjectIdentifier,
0N/A * criticality and the raw encoded extension value.
0N/A *
0N/A * @param extensionId the ObjectIdentifier of the extension
0N/A * @param critical the boolean indicating if the extension is critical
0N/A * @param rawExtensionValue the raw DER-encoded extension value (this
0N/A * is not the encoded OctetString).
0N/A */
0N/A public static Extension newExtension(ObjectIdentifier extensionId,
0N/A boolean critical, byte[] rawExtensionValue) throws IOException {
0N/A Extension ext = new Extension();
0N/A ext.extensionId = extensionId;
0N/A ext.critical = critical;
0N/A ext.extensionValue = rawExtensionValue;
0N/A return ext;
0N/A }
0N/A
0N/A public void encode(OutputStream out) throws IOException {
0N/A if (out == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A
0N/A DerOutputStream dos1 = new DerOutputStream();
0N/A DerOutputStream dos2 = new DerOutputStream();
0N/A
0N/A dos1.putOID(extensionId);
0N/A if (critical) {
0N/A dos1.putBoolean(critical);
0N/A }
0N/A dos1.putOctetString(extensionValue);
0N/A
0N/A dos2.write(DerValue.tag_Sequence, dos1);
0N/A out.write(dos2.toByteArray());
0N/A }
0N/A
0N/A /**
0N/A * Write the extension to the DerOutputStream.
0N/A *
0N/A * @param out the DerOutputStream to write the extension to.
0N/A * @exception IOException on encoding errors
0N/A */
0N/A public void encode(DerOutputStream out) throws IOException {
0N/A
0N/A if (extensionId == null)
0N/A throw new IOException("Null OID to encode for the extension!");
0N/A if (extensionValue == null)
0N/A throw new IOException("No value to encode for the extension!");
0N/A
0N/A DerOutputStream dos = new DerOutputStream();
0N/A
0N/A dos.putOID(extensionId);
0N/A if (critical)
0N/A dos.putBoolean(critical);
0N/A dos.putOctetString(extensionValue);
0N/A
0N/A out.write(DerValue.tag_Sequence, dos);
0N/A }
0N/A
0N/A /**
0N/A * Returns true if extension is critical.
0N/A */
0N/A public boolean isCritical() {
0N/A return critical;
0N/A }
0N/A
0N/A /**
0N/A * Returns the ObjectIdentifier of the extension.
0N/A */
0N/A public ObjectIdentifier getExtensionId() {
0N/A return extensionId;
0N/A }
0N/A
0N/A public byte[] getValue() {
0N/A return extensionValue.clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns the extension value as an byte array for further processing.
0N/A * Note, this is the raw DER value of the extension, not the DER
0N/A * encoded octet string which is in the certificate.
0N/A * This method does not return a clone; it is the responsibility of the
0N/A * caller to clone the array if necessary.
0N/A */
0N/A public byte[] getExtensionValue() {
0N/A return extensionValue;
0N/A }
0N/A
0N/A public String getId() {
0N/A return extensionId.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns the Extension in user readable form.
0N/A */
0N/A public String toString() {
0N/A String s = "ObjectId: " + extensionId.toString();
0N/A if (critical) {
0N/A s += " Criticality=true\n";
0N/A } else {
0N/A s += " Criticality=false\n";
0N/A }
0N/A return (s);
0N/A }
0N/A
0N/A // Value to mix up the hash
0N/A private static final int hashMagic = 31;
0N/A
0N/A /**
0N/A * Returns a hashcode value for this Extension.
0N/A *
0N/A * @return the hashcode value.
0N/A */
0N/A public int hashCode() {
0N/A int h = 0;
0N/A if (extensionValue != null) {
0N/A byte[] val = extensionValue;
0N/A int len = val.length;
0N/A while (len > 0)
0N/A h += len * val[--len];
0N/A }
0N/A h = h * hashMagic + extensionId.hashCode();
0N/A h = h * hashMagic + (critical?1231:1237);
0N/A return h;
0N/A }
0N/A
0N/A /**
0N/A * Compares this Extension for equality with the specified
0N/A * object. If the <code>other</code> object is an
0N/A * <code>instanceof</code> <code>Extension</code>, then
0N/A * its encoded form is retrieved and compared with the
0N/A * encoded form of this Extension.
0N/A *
0N/A * @param other the object to test for equality with this Extension.
0N/A * @return true iff the other object is of type Extension, and the
0N/A * criticality flag, object identifier and encoded extension value of
0N/A * the two Extensions match, 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 Extension))
0N/A return false;
0N/A Extension otherExt = (Extension) other;
0N/A if (critical != otherExt.critical)
0N/A return false;
0N/A if (!extensionId.equals(otherExt.extensionId))
0N/A return false;
0N/A return Arrays.equals(extensionValue, otherExt.extensionValue);
0N/A }
0N/A}