Path: blob/master/src/java.base/share/classes/sun/security/x509/CertificateIssuerExtension.java
41159 views
/*1* Copyright (c) 2003, 2014, 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*/24package sun.security.x509;2526import java.io.IOException;27import java.io.OutputStream;28import java.util.Enumeration;2930import sun.security.util.DerValue;31import sun.security.util.DerOutputStream;3233/**34* Represents the CRL Certificate Issuer Extension (OID = 2.5.29.29).35* <p>36* The CRL certificate issuer extension identifies the certificate issuer37* associated with an entry in an indirect CRL, i.e. a CRL that has the38* indirectCRL indicator set in its issuing distribution point extension. If39* this extension is not present on the first entry in an indirect CRL, the40* certificate issuer defaults to the CRL issuer. On subsequent entries41* in an indirect CRL, if this extension is not present, the certificate42* issuer for the entry is the same as that for the preceding entry.43* <p>44* If used by conforming CRL issuers, this extension is always45* critical. If an implementation ignored this extension it could not46* correctly attribute CRL entries to certificates. PKIX (RFC 5280)47* RECOMMENDS that implementations recognize this extension.48* <p>49* The ASN.1 definition for this is:50* <pre>51* id-ce-certificateIssuer OBJECT IDENTIFIER ::= { id-ce 29 }52*53* certificateIssuer ::= GeneralNames54* </pre>55*56* @author Anne Anderson57* @author Sean Mullan58* @since 1.559* @see Extension60* @see CertAttrSet61*/62public class CertificateIssuerExtension extends Extension63implements CertAttrSet<String> {6465/**66* Attribute names.67*/68public static final String NAME = "CertificateIssuer";69public static final String ISSUER = "issuer";7071private GeneralNames names;7273/**74* Encode this extension75*/76private void encodeThis() throws IOException {77if (names == null || names.isEmpty()) {78this.extensionValue = null;79return;80}81DerOutputStream os = new DerOutputStream();82names.encode(os);83this.extensionValue = os.toByteArray();84}8586/**87* Create a CertificateIssuerExtension containing the specified issuer name.88* Criticality is automatically set to true.89*90* @param issuer the certificate issuer91* @throws IOException on error92*/93public CertificateIssuerExtension(GeneralNames issuer) throws IOException {94this.extensionId = PKIXExtensions.CertificateIssuer_Id;95this.critical = true;96this.names = issuer;97encodeThis();98}99100/**101* Create a CertificateIssuerExtension from the specified DER encoded102* value of the same.103*104* @param critical true if the extension is to be treated as critical.105* @param value an array of DER encoded bytes of the actual value106* @throws ClassCastException if value is not an array of bytes107* @throws IOException on error108*/109public CertificateIssuerExtension(Boolean critical, Object value)110throws IOException {111this.extensionId = PKIXExtensions.CertificateIssuer_Id;112this.critical = critical.booleanValue();113114this.extensionValue = (byte[]) value;115DerValue val = new DerValue(this.extensionValue);116this.names = new GeneralNames(val);117}118119/**120* Set the attribute value.121*122* @throws IOException on error123*/124public void set(String name, Object obj) throws IOException {125if (name.equalsIgnoreCase(ISSUER)) {126if (!(obj instanceof GeneralNames)) {127throw new IOException("Attribute value must be of type " +128"GeneralNames");129}130this.names = (GeneralNames)obj;131} else {132throw new IOException("Attribute name not recognized by " +133"CertAttrSet:CertificateIssuer");134}135encodeThis();136}137138/**139* Gets the attribute value.140*141* @throws IOException on error142*/143public GeneralNames get(String name) throws IOException {144if (name.equalsIgnoreCase(ISSUER)) {145return names;146} else {147throw new IOException("Attribute name not recognized by " +148"CertAttrSet:CertificateIssuer");149}150}151152/**153* Deletes the attribute value.154*155* @throws IOException on error156*/157public void delete(String name) throws IOException {158if (name.equalsIgnoreCase(ISSUER)) {159names = null;160} else {161throw new IOException("Attribute name not recognized by " +162"CertAttrSet:CertificateIssuer");163}164encodeThis();165}166167/**168* Returns a printable representation of the certificate issuer.169*/170public String toString() {171return super.toString() + "Certificate Issuer [\n" +172String.valueOf(names) + "]\n";173}174175/**176* Write the extension to the OutputStream.177*178* @param out the OutputStream to write the extension to179* @exception IOException on encoding errors180*/181public void encode(OutputStream out) throws IOException {182DerOutputStream tmp = new DerOutputStream();183if (extensionValue == null) {184extensionId = PKIXExtensions.CertificateIssuer_Id;185critical = true;186encodeThis();187}188super.encode(tmp);189out.write(tmp.toByteArray());190}191192/**193* Return an enumeration of names of attributes existing within this194* attribute.195*/196public Enumeration<String> getElements() {197AttributeNameEnumeration elements = new AttributeNameEnumeration();198elements.addElement(ISSUER);199return elements.elements();200}201202/**203* Return the name of this attribute.204*/205public String getName() {206return NAME;207}208}209210211