Path: blob/master/src/java.base/share/classes/sun/security/x509/IssuingDistributionPointExtension.java
41159 views
/*1* Copyright (c) 2005, 2020, 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;2930import java.util.*;3132import sun.security.util.DerInputStream;33import sun.security.util.DerOutputStream;34import sun.security.util.DerValue;3536/**37* Represents the CRL Issuing Distribution Point Extension (OID = 2.5.29.28).38*39* <p>40* The issuing distribution point is a critical CRL extension that41* identifies the CRL distribution point and scope for a particular CRL,42* and it indicates whether the CRL covers revocation for end entity43* certificates only, CA certificates only, attribute certificates only,44* or a limited set of reason codes.45*46* <p>47* The extension is defined in Section 5.2.5 of48* <a href="http://tools.ietf.org/html/rfc5280">Internet X.509 PKI49* Certificate and Certificate Revocation List (CRL) Profile</a>.50*51* <p>52* Its ASN.1 definition is as follows:53* <pre>54* id-ce-issuingDistributionPoint OBJECT IDENTIFIER ::= { id-ce 28 }55*56* issuingDistributionPoint ::= SEQUENCE {57* distributionPoint [0] DistributionPointName OPTIONAL,58* onlyContainsUserCerts [1] BOOLEAN DEFAULT FALSE,59* onlyContainsCACerts [2] BOOLEAN DEFAULT FALSE,60* onlySomeReasons [3] ReasonFlags OPTIONAL,61* indirectCRL [4] BOOLEAN DEFAULT FALSE,62* onlyContainsAttributeCerts [5] BOOLEAN DEFAULT FALSE }63* </pre>64*65* @see DistributionPoint66* @since 1.667*/68public class IssuingDistributionPointExtension extends Extension69implements CertAttrSet<String> {7071/**72* Identifier for this attribute, to be used with the73* get, set, delete methods of Certificate, x509 type.74*/75public static final String IDENT =76"x509.info.extensions.IssuingDistributionPoint";7778/**79* Attribute names.80*/81public static final String NAME = "IssuingDistributionPoint";82public static final String POINT = "point";83public static final String REASONS = "reasons";84public static final String ONLY_USER_CERTS = "only_user_certs";85public static final String ONLY_CA_CERTS = "only_ca_certs";86public static final String ONLY_ATTRIBUTE_CERTS = "only_attribute_certs";87public static final String INDIRECT_CRL = "indirect_crl";8889/*90* The distribution point name for the CRL.91*/92private DistributionPointName distributionPoint = null;9394/*95* The scope settings for the CRL.96*/97private ReasonFlags revocationReasons = null;98private boolean hasOnlyUserCerts = false;99private boolean hasOnlyCACerts = false;100private boolean hasOnlyAttributeCerts = false;101private boolean isIndirectCRL = false;102103/*104* ASN.1 context specific tag values105*/106private static final byte TAG_DISTRIBUTION_POINT = 0;107private static final byte TAG_ONLY_USER_CERTS = 1;108private static final byte TAG_ONLY_CA_CERTS = 2;109private static final byte TAG_ONLY_SOME_REASONS = 3;110private static final byte TAG_INDIRECT_CRL = 4;111private static final byte TAG_ONLY_ATTRIBUTE_CERTS = 5;112113/**114* Creates a critical IssuingDistributionPointExtension.115*116* @param distributionPoint the name of the distribution point, or null for117* none.118* @param revocationReasons the revocation reasons associated with the119* distribution point, or null for none.120* @param hasOnlyUserCerts if <code>true</code> then scope of the CRL121* includes only user certificates.122* @param hasOnlyCACerts if <code>true</code> then scope of the CRL123* includes only CA certificates.124* @param hasOnlyAttributeCerts if <code>true</code> then scope of the CRL125* includes only attribute certificates.126* @param isIndirectCRL if <code>true</code> then the scope of the CRL127* includes certificates issued by authorities other than the CRL128* issuer. The responsible authority is indicated by a certificate129* issuer CRL entry extension.130* @throws IllegalArgumentException if more than one of131* <code>hasOnlyUserCerts</code>, <code>hasOnlyCACerts</code>,132* <code>hasOnlyAttributeCerts</code> is set to <code>true</code>.133* @throws IOException on encoding error.134*/135public IssuingDistributionPointExtension(136DistributionPointName distributionPoint, ReasonFlags revocationReasons,137boolean hasOnlyUserCerts, boolean hasOnlyCACerts,138boolean hasOnlyAttributeCerts, boolean isIndirectCRL)139throws IOException {140141if ((hasOnlyUserCerts && (hasOnlyCACerts || hasOnlyAttributeCerts)) ||142(hasOnlyCACerts && (hasOnlyUserCerts || hasOnlyAttributeCerts)) ||143(hasOnlyAttributeCerts && (hasOnlyUserCerts || hasOnlyCACerts))) {144throw new IllegalArgumentException(145"Only one of hasOnlyUserCerts, hasOnlyCACerts, " +146"hasOnlyAttributeCerts may be set to true");147}148this.extensionId = PKIXExtensions.IssuingDistributionPoint_Id;149this.critical = true;150this.distributionPoint = distributionPoint;151this.revocationReasons = revocationReasons;152this.hasOnlyUserCerts = hasOnlyUserCerts;153this.hasOnlyCACerts = hasOnlyCACerts;154this.hasOnlyAttributeCerts = hasOnlyAttributeCerts;155this.isIndirectCRL = isIndirectCRL;156encodeThis();157}158159/**160* Creates a critical IssuingDistributionPointExtension from its161* DER-encoding.162*163* @param critical true if the extension is to be treated as critical.164* @param value the DER-encoded value. It must be a <code>byte[]</code>.165* @exception IOException on decoding error.166*/167public IssuingDistributionPointExtension(Boolean critical, Object value)168throws IOException {169this.extensionId = PKIXExtensions.IssuingDistributionPoint_Id;170this.critical = critical.booleanValue();171172if (!(value instanceof byte[])) {173throw new IOException("Illegal argument type");174}175176extensionValue = (byte[])value;177DerValue val = new DerValue(extensionValue);178if (val.tag != DerValue.tag_Sequence) {179throw new IOException("Invalid encoding for " +180"IssuingDistributionPointExtension.");181}182183// All the elements in issuingDistributionPoint are optional184if ((val.data == null) || (val.data.available() == 0)) {185return;186}187188DerInputStream in = val.data;189while (in != null && in.available() != 0) {190DerValue opt = in.getDerValue();191192if (opt.isContextSpecific(TAG_DISTRIBUTION_POINT) &&193opt.isConstructed()) {194distributionPoint =195new DistributionPointName(opt.data.getDerValue());196} else if (opt.isContextSpecific(TAG_ONLY_USER_CERTS) &&197!opt.isConstructed()) {198opt.resetTag(DerValue.tag_Boolean);199hasOnlyUserCerts = opt.getBoolean();200} else if (opt.isContextSpecific(TAG_ONLY_CA_CERTS) &&201!opt.isConstructed()) {202opt.resetTag(DerValue.tag_Boolean);203hasOnlyCACerts = opt.getBoolean();204} else if (opt.isContextSpecific(TAG_ONLY_SOME_REASONS) &&205!opt.isConstructed()) {206revocationReasons = new ReasonFlags(opt); // expects tag implicit207} else if (opt.isContextSpecific(TAG_INDIRECT_CRL) &&208!opt.isConstructed()) {209opt.resetTag(DerValue.tag_Boolean);210isIndirectCRL = opt.getBoolean();211} else if (opt.isContextSpecific(TAG_ONLY_ATTRIBUTE_CERTS) &&212!opt.isConstructed()) {213opt.resetTag(DerValue.tag_Boolean);214hasOnlyAttributeCerts = opt.getBoolean();215} else {216throw new IOException217("Invalid encoding of IssuingDistributionPoint");218}219}220}221222/**223* Returns the name of this attribute.224*/225public String getName() {226return NAME;227}228229/**230* Encodes the issuing distribution point extension and writes it to the231* DerOutputStream.232*233* @param out the output stream.234* @exception IOException on encoding error.235*/236public void encode(OutputStream out) throws IOException {237DerOutputStream tmp = new DerOutputStream();238if (this.extensionValue == null) {239this.extensionId = PKIXExtensions.IssuingDistributionPoint_Id;240this.critical = false;241encodeThis();242}243super.encode(tmp);244out.write(tmp.toByteArray());245}246247/**248* Sets the attribute value.249*/250public void set(String name, Object obj) throws IOException {251if (name.equalsIgnoreCase(POINT)) {252if (!(obj instanceof DistributionPointName)) {253throw new IOException(254"Attribute value should be of type DistributionPointName.");255}256distributionPoint = (DistributionPointName)obj;257258} else if (name.equalsIgnoreCase(REASONS)) {259if (!(obj instanceof ReasonFlags)) {260throw new IOException(261"Attribute value should be of type ReasonFlags.");262}263revocationReasons = (ReasonFlags)obj;264265} else if (name.equalsIgnoreCase(INDIRECT_CRL)) {266if (!(obj instanceof Boolean)) {267throw new IOException(268"Attribute value should be of type Boolean.");269}270isIndirectCRL = ((Boolean)obj).booleanValue();271272} else if (name.equalsIgnoreCase(ONLY_USER_CERTS)) {273if (!(obj instanceof Boolean)) {274throw new IOException(275"Attribute value should be of type Boolean.");276}277hasOnlyUserCerts = ((Boolean)obj).booleanValue();278279} else if (name.equalsIgnoreCase(ONLY_CA_CERTS)) {280if (!(obj instanceof Boolean)) {281throw new IOException(282"Attribute value should be of type Boolean.");283}284hasOnlyCACerts = ((Boolean)obj).booleanValue();285286} else if (name.equalsIgnoreCase(ONLY_ATTRIBUTE_CERTS)) {287if (!(obj instanceof Boolean)) {288throw new IOException(289"Attribute value should be of type Boolean.");290}291hasOnlyAttributeCerts = ((Boolean)obj).booleanValue();292293} else {294throw new IOException("Attribute name [" + name +295"] not recognized by " +296"CertAttrSet:IssuingDistributionPointExtension.");297}298encodeThis();299}300301/**302* Gets the attribute value.303*/304public Object get(String name) throws IOException {305if (name.equalsIgnoreCase(POINT)) {306return distributionPoint;307308} else if (name.equalsIgnoreCase(INDIRECT_CRL)) {309return Boolean.valueOf(isIndirectCRL);310311} else if (name.equalsIgnoreCase(REASONS)) {312return revocationReasons;313314} else if (name.equalsIgnoreCase(ONLY_USER_CERTS)) {315return Boolean.valueOf(hasOnlyUserCerts);316317} else if (name.equalsIgnoreCase(ONLY_CA_CERTS)) {318return Boolean.valueOf(hasOnlyCACerts);319320} else if (name.equalsIgnoreCase(ONLY_ATTRIBUTE_CERTS)) {321return Boolean.valueOf(hasOnlyAttributeCerts);322323} else {324throw new IOException("Attribute name [" + name +325"] not recognized by " +326"CertAttrSet:IssuingDistributionPointExtension.");327}328}329330/**331* Deletes the attribute value.332*/333public void delete(String name) throws IOException {334if (name.equalsIgnoreCase(POINT)) {335distributionPoint = null;336337} else if (name.equalsIgnoreCase(INDIRECT_CRL)) {338isIndirectCRL = false;339340} else if (name.equalsIgnoreCase(REASONS)) {341revocationReasons = null;342343} else if (name.equalsIgnoreCase(ONLY_USER_CERTS)) {344hasOnlyUserCerts = false;345346} else if (name.equalsIgnoreCase(ONLY_CA_CERTS)) {347hasOnlyCACerts = false;348349} else if (name.equalsIgnoreCase(ONLY_ATTRIBUTE_CERTS)) {350hasOnlyAttributeCerts = false;351352} else {353throw new IOException("Attribute name [" + name +354"] not recognized by " +355"CertAttrSet:IssuingDistributionPointExtension.");356}357encodeThis();358}359360/**361* Returns an enumeration of names of attributes existing within this362* attribute.363*/364public Enumeration<String> getElements() {365AttributeNameEnumeration elements = new AttributeNameEnumeration();366elements.addElement(POINT);367elements.addElement(REASONS);368elements.addElement(ONLY_USER_CERTS);369elements.addElement(ONLY_CA_CERTS);370elements.addElement(ONLY_ATTRIBUTE_CERTS);371elements.addElement(INDIRECT_CRL);372return elements.elements();373}374375// Encodes this extension value376private void encodeThis() throws IOException {377378if (distributionPoint == null &&379revocationReasons == null &&380!hasOnlyUserCerts &&381!hasOnlyCACerts &&382!hasOnlyAttributeCerts &&383!isIndirectCRL) {384385this.extensionValue = null;386return;387388}389390DerOutputStream tagged = new DerOutputStream();391392if (distributionPoint != null) {393DerOutputStream tmp = new DerOutputStream();394distributionPoint.encode(tmp);395tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, true,396TAG_DISTRIBUTION_POINT), tmp);397}398399if (hasOnlyUserCerts) {400DerOutputStream tmp = new DerOutputStream();401tmp.putBoolean(hasOnlyUserCerts);402tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, false,403TAG_ONLY_USER_CERTS), tmp);404}405406if (hasOnlyCACerts) {407DerOutputStream tmp = new DerOutputStream();408tmp.putBoolean(hasOnlyCACerts);409tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, false,410TAG_ONLY_CA_CERTS), tmp);411}412413if (revocationReasons != null) {414DerOutputStream tmp = new DerOutputStream();415revocationReasons.encode(tmp);416tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, false,417TAG_ONLY_SOME_REASONS), tmp);418}419420if (isIndirectCRL) {421DerOutputStream tmp = new DerOutputStream();422tmp.putBoolean(isIndirectCRL);423tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, false,424TAG_INDIRECT_CRL), tmp);425}426427if (hasOnlyAttributeCerts) {428DerOutputStream tmp = new DerOutputStream();429tmp.putBoolean(hasOnlyAttributeCerts);430tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, false,431TAG_ONLY_ATTRIBUTE_CERTS), tmp);432}433434DerOutputStream seq = new DerOutputStream();435seq.write(DerValue.tag_Sequence, tagged);436this.extensionValue = seq.toByteArray();437}438439/**440* Returns the extension as user readable string.441*/442public String toString() {443StringBuilder sb = new StringBuilder();444sb.append(super.toString())445.append("IssuingDistributionPoint [\n ");446447if (distributionPoint != null) {448sb.append(distributionPoint);449}450451if (revocationReasons != null) {452sb.append(revocationReasons);453}454455sb.append(" Only contains user certs: ")456.append(hasOnlyUserCerts)457.append('\n')458.append(" Only contains CA certs: ")459.append(hasOnlyCACerts)460.append('\n')461.append(" Only contains attribute certs: ")462.append(hasOnlyAttributeCerts)463.append('\n')464.append(" Indirect CRL: ")465.append(isIndirectCRL)466.append("\n]\n");467468return sb.toString();469}470471}472473474