Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/x509/InhibitAnyPolicyExtension.java
38831 views
/*1* Copyright (c) 2000, 2011, 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.Enumeration;3031import sun.security.util.Debug;32import sun.security.util.DerOutputStream;33import sun.security.util.DerValue;34import sun.security.util.ObjectIdentifier;3536/**37* This class represents the Inhibit Any-Policy Extension.38*39* <p>The inhibit any-policy extension can be used in certificates issued40* to CAs. The inhibit any-policy indicates that the special any-policy41* OID, with the value {2 5 29 32 0}, is not considered an explicit42* match for other certificate policies. The value indicates the number43* of additional certificates that may appear in the path before any-44* policy is no longer permitted. For example, a value of one indicates45* that any-policy may be processed in certificates issued by the sub-46* ject of this certificate, but not in additional certificates in the47* path.48* <p>49* This extension MUST be critical.50* <p>51* The ASN.1 syntax for this extension is:52* <code><pre>53* id-ce-inhibitAnyPolicy OBJECT IDENTIFIER ::= { id-ce 54 }54*55* InhibitAnyPolicy ::= SkipCerts56*57* SkipCerts ::= INTEGER (0..MAX)58* </pre></code>59* @author Anne Anderson60* @see CertAttrSet61* @see Extension62*/63public class InhibitAnyPolicyExtension extends Extension64implements CertAttrSet<String> {6566private static final Debug debug = Debug.getInstance("certpath");6768/**69* Identifier for this attribute, to be used with the70* get, set, delete methods of Certificate, x509 type.71*/72public static final String IDENT = "x509.info.extensions.InhibitAnyPolicy";7374/**75* Object identifier for "any-policy"76*/77public static ObjectIdentifier AnyPolicy_Id;78static {79try {80AnyPolicy_Id = new ObjectIdentifier("2.5.29.32.0");81} catch (IOException ioe) {82// Should not happen83}84}8586/**87* Attribute names.88*/89public static final String NAME = "InhibitAnyPolicy";90public static final String SKIP_CERTS = "skip_certs";9192// Private data members93private int skipCerts = Integer.MAX_VALUE;9495// Encode this extension value96private void encodeThis() throws IOException {97DerOutputStream out = new DerOutputStream();98out.putInteger(skipCerts);99this.extensionValue = out.toByteArray();100}101102/**103* Default constructor for this object.104*105* @param skipCerts specifies the depth of the certification path.106* Use value of -1 to request unlimited depth.107*/108public InhibitAnyPolicyExtension(int skipCerts) throws IOException {109if (skipCerts < -1)110throw new IOException("Invalid value for skipCerts");111if (skipCerts == -1)112this.skipCerts = Integer.MAX_VALUE;113else114this.skipCerts = skipCerts;115this.extensionId = PKIXExtensions.InhibitAnyPolicy_Id;116critical = true;117encodeThis();118}119120/**121* Create the extension from the passed DER encoded value of the same.122*123* @param critical criticality flag to use. Must be true for this124* extension.125* @param value a byte array holding the DER-encoded extension value.126* @exception ClassCastException if value is not an array of bytes127* @exception IOException on error.128*/129public InhibitAnyPolicyExtension(Boolean critical, Object value)130throws IOException {131132this.extensionId = PKIXExtensions.InhibitAnyPolicy_Id;133134if (!critical.booleanValue())135throw new IOException("Criticality cannot be false for " +136"InhibitAnyPolicy");137this.critical = critical.booleanValue();138139this.extensionValue = (byte[]) value;140DerValue val = new DerValue(this.extensionValue);141if (val.tag != DerValue.tag_Integer)142throw new IOException("Invalid encoding of InhibitAnyPolicy: "143+ "data not integer");144145if (val.data == null)146throw new IOException("Invalid encoding of InhibitAnyPolicy: "147+ "null data");148int skipCertsValue = val.getInteger();149if (skipCertsValue < -1)150throw new IOException("Invalid value for skipCerts");151if (skipCertsValue == -1) {152this.skipCerts = Integer.MAX_VALUE;153} else {154this.skipCerts = skipCertsValue;155}156}157158/**159* Return user readable form of extension.160*/161public String toString() {162String s = super.toString() + "InhibitAnyPolicy: " + skipCerts + "\n";163return s;164}165166/**167* Encode this extension value to the output stream.168*169* @param out the DerOutputStream to encode the extension to.170*/171public void encode(OutputStream out) throws IOException {172DerOutputStream tmp = new DerOutputStream();173if (extensionValue == null) {174this.extensionId = PKIXExtensions.InhibitAnyPolicy_Id;175critical = true;176encodeThis();177}178super.encode(tmp);179180out.write(tmp.toByteArray());181}182183/**184* Set the attribute value.185*186* @param name name of attribute to set. Must be SKIP_CERTS.187* @param obj value to which attribute is to be set. Must be Integer188* type.189* @throws IOException on error190*/191public void set(String name, Object obj) throws IOException {192if (name.equalsIgnoreCase(SKIP_CERTS)) {193if (!(obj instanceof Integer))194throw new IOException("Attribute value should be of type Integer.");195int skipCertsValue = ((Integer)obj).intValue();196if (skipCertsValue < -1)197throw new IOException("Invalid value for skipCerts");198if (skipCertsValue == -1) {199skipCerts = Integer.MAX_VALUE;200} else {201skipCerts = skipCertsValue;202}203} else204throw new IOException("Attribute name not recognized by " +205"CertAttrSet:InhibitAnyPolicy.");206encodeThis();207}208209/**210* Get the attribute value.211*212* @param name name of attribute to get. Must be SKIP_CERTS.213* @returns value of the attribute. In this case it will be of type214* Integer.215* @throws IOException on error216*/217public Integer get(String name) throws IOException {218if (name.equalsIgnoreCase(SKIP_CERTS))219return (new Integer(skipCerts));220else221throw new IOException("Attribute name not recognized by " +222"CertAttrSet:InhibitAnyPolicy.");223}224225/**226* Delete the attribute value.227*228* @param name name of attribute to delete. Must be SKIP_CERTS.229* @throws IOException on error. In this case, IOException will always be230* thrown, because the only attribute, SKIP_CERTS, is231* required.232*/233public void delete(String name) throws IOException {234if (name.equalsIgnoreCase(SKIP_CERTS))235throw new IOException("Attribute " + SKIP_CERTS +236" may not be deleted.");237else238throw new IOException("Attribute name not recognized by " +239"CertAttrSet:InhibitAnyPolicy.");240}241242/**243* Return an enumeration of names of attributes existing within this244* attribute.245*246* @returns enumeration of elements247*/248public Enumeration<String> getElements() {249AttributeNameEnumeration elements = new AttributeNameEnumeration();250elements.addElement(SKIP_CERTS);251return (elements.elements());252}253254/**255* Return the name of this attribute.256*257* @returns name of attribute.258*/259public String getName() {260return (NAME);261}262}263264265