Path: blob/master/src/java.base/share/classes/sun/security/x509/Extension.java
41159 views
/*1* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.x509;2627import java.io.IOException;28import java.io.OutputStream;29import java.util.Arrays;30import sun.security.util.*;3132/**33* Represent a X509 Extension Attribute.34*35* <p>Extensions are additional attributes which can be inserted in a X50936* v3 certificate. For example a "Driving License Certificate" could have37* the driving license number as a extension.38*39* <p>Extensions are represented as a sequence of the extension identifier40* (Object Identifier), a boolean flag stating whether the extension is to41* be treated as being critical and the extension value itself (this is again42* a DER encoding of the extension value).43* <pre>44* ASN.1 definition of Extension:45* Extension ::= SEQUENCE {46* ExtensionId OBJECT IDENTIFIER,47* critical BOOLEAN DEFAULT FALSE,48* extensionValue OCTET STRING49* }50* </pre>51* All subclasses need to implement a constructor of the form52* <pre>{@code53* <subclass> (Boolean, Object)54* }</pre>55* where the Object is typically an array of DER encoded bytes.56*57* @author Amit Kapoor58* @author Hemma Prafullchandra59*/60public class Extension implements java.security.cert.Extension {6162protected ObjectIdentifier extensionId = null;63protected boolean critical = false;64protected byte[] extensionValue = null;6566/**67* Default constructor. Used only by sub-classes.68*/69public Extension() { }7071/**72* Constructs an extension from a DER encoded array of bytes.73*/74public Extension(DerValue derVal) throws IOException {7576DerInputStream in = derVal.toDerInputStream();7778// Object identifier79extensionId = in.getOID();8081// If the criticality flag was false, it will not have been encoded.82DerValue val = in.getDerValue();83if (val.tag == DerValue.tag_Boolean) {84critical = val.getBoolean();8586// Extension value (DER encoded)87val = in.getDerValue();88extensionValue = val.getOctetString();89} else {90critical = false;91extensionValue = val.getOctetString();92}93}9495/**96* Constructs an Extension from individual components of ObjectIdentifier,97* criticality and the DER encoded OctetString.98*99* @param extensionId the ObjectIdentifier of the extension100* @param critical the boolean indicating if the extension is critical101* @param extensionValue the DER encoded octet string of the value.102*/103public Extension(ObjectIdentifier extensionId, boolean critical,104byte[] extensionValue) throws IOException {105this.extensionId = extensionId;106this.critical = critical;107// passed in a DER encoded octet string, strip off the tag108// and length109DerValue inDerVal = new DerValue(extensionValue);110this.extensionValue = inDerVal.getOctetString();111}112113/**114* Constructs an Extension from another extension. To be used for115* creating decoded subclasses.116*117* @param ext the extension to create from.118*/119public Extension(Extension ext) {120this.extensionId = ext.extensionId;121this.critical = ext.critical;122this.extensionValue = ext.extensionValue;123}124125/**126* Constructs an Extension from individual components of ObjectIdentifier,127* criticality and the raw encoded extension value.128*129* @param extensionId the ObjectIdentifier of the extension130* @param critical the boolean indicating if the extension is critical131* @param rawExtensionValue the raw DER-encoded extension value (this132* is not the encoded OctetString).133*/134public static Extension newExtension(ObjectIdentifier extensionId,135boolean critical, byte[] rawExtensionValue) throws IOException {136Extension ext = new Extension();137ext.extensionId = extensionId;138ext.critical = critical;139ext.extensionValue = rawExtensionValue;140return ext;141}142143public void encode(OutputStream out) throws IOException {144if (out == null) {145throw new NullPointerException();146}147148DerOutputStream dos1 = new DerOutputStream();149DerOutputStream dos2 = new DerOutputStream();150151dos1.putOID(extensionId);152if (critical) {153dos1.putBoolean(critical);154}155dos1.putOctetString(extensionValue);156157dos2.write(DerValue.tag_Sequence, dos1);158out.write(dos2.toByteArray());159}160161/**162* Write the extension to the DerOutputStream.163*164* @param out the DerOutputStream to write the extension to.165* @exception IOException on encoding errors166*/167public void encode(DerOutputStream out) throws IOException {168169if (extensionId == null)170throw new IOException("Null OID to encode for the extension!");171if (extensionValue == null)172throw new IOException("No value to encode for the extension!");173174DerOutputStream dos = new DerOutputStream();175176dos.putOID(extensionId);177if (critical)178dos.putBoolean(critical);179dos.putOctetString(extensionValue);180181out.write(DerValue.tag_Sequence, dos);182}183184/**185* Returns true if extension is critical.186*/187public boolean isCritical() {188return critical;189}190191/**192* Returns the ObjectIdentifier of the extension.193*/194public ObjectIdentifier getExtensionId() {195return extensionId;196}197198public byte[] getValue() {199return extensionValue.clone();200}201202/**203* Returns the extension value as an byte array for further processing.204* Note, this is the raw DER value of the extension, not the DER205* encoded octet string which is in the certificate.206* This method does not return a clone; it is the responsibility of the207* caller to clone the array if necessary.208*/209public byte[] getExtensionValue() {210return extensionValue;211}212213public String getId() {214return extensionId.toString();215}216217/**218* Returns the Extension in user readable form.219*/220public String toString() {221return "ObjectId: " + extensionId +222" Criticality=" + critical + '\n';223}224225// Value to mix up the hash226private static final int hashMagic = 31;227228/**229* Returns a hashcode value for this Extension.230*231* @return the hashcode value.232*/233public int hashCode() {234int h = 0;235if (extensionValue != null) {236byte[] val = extensionValue;237int len = val.length;238while (len > 0)239h += len * val[--len];240}241h = h * hashMagic + extensionId.hashCode();242h = h * hashMagic + (critical?1231:1237);243return h;244}245246/**247* Compares this Extension for equality with the specified248* object. If the <code>other</code> object is an249* <code>instanceof</code> <code>Extension</code>, then250* its encoded form is retrieved and compared with the251* encoded form of this Extension.252*253* @param other the object to test for equality with this Extension.254* @return true iff the other object is of type Extension, and the255* criticality flag, object identifier and encoded extension value of256* the two Extensions match, false otherwise.257*/258public boolean equals(Object other) {259if (this == other)260return true;261if (!(other instanceof Extension))262return false;263Extension otherExt = (Extension) other;264if (critical != otherExt.critical)265return false;266if (!extensionId.equals(otherExt.extensionId))267return false;268return Arrays.equals(extensionValue, otherExt.extensionValue);269}270}271272273