1/*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package java.security.cert;
27
28import java.util.Set;
29
30/**
31 * Interface for an X.509 extension.
32 *
33 * <p>The extensions defined for X.509 v3
34 * {@link X509Certificate Certificates} and v2
35 * {@link X509CRL CRLs} (Certificate Revocation
36 * Lists) provide methods
37 * for associating additional attributes with users or public keys,
38 * for managing the certification hierarchy, and for managing CRL
39 * distribution. The X.509 extensions format also allows communities
40 * to define private extensions to carry information unique to those
41 * communities.
42 *
43 * <p>Each extension in a certificate/CRL may be designated as
44 * critical or non-critical.  A certificate/CRL-using system (an application
45 * validating a certificate/CRL) must reject the certificate/CRL if it
46 * encounters a critical extension it does not recognize.  A non-critical
47 * extension may be ignored if it is not recognized.
48 * <p>
49 * The ASN.1 definition for this is:
50 * <pre>
51 * Extensions  ::=  SEQUENCE SIZE (1..MAX) OF Extension
52 *
53 * Extension  ::=  SEQUENCE  {
54 *     extnId        OBJECT IDENTIFIER,
55 *     critical      BOOLEAN DEFAULT FALSE,
56 *     extnValue     OCTET STRING
57 *                   -- contains a DER encoding of a value
58 *                   -- of the type registered for use with
59 *                   -- the extnId object identifier value
60 * }
61 * </pre>
62 * Since not all extensions are known, the {@code getExtensionValue}
63 * method returns the DER-encoded OCTET STRING of the
64 * extension value (i.e., the {@code extnValue}). This can then
65 * be handled by a <em>Class</em> that understands the extension.
66 *
67 * @author Hemma Prafullchandra
68 * @since 1.2
69 */
70
71public interface X509Extension {
72
73    /**
74     * Check if there is a critical extension that is not supported.
75     *
76     * @return {@code true} if a critical extension is found that is
77     * not supported, otherwise {@code false}.
78     */
79    public boolean hasUnsupportedCriticalExtension();
80
81    /**
82     * Gets a Set of the OID strings for the extension(s) marked
83     * CRITICAL in the certificate/CRL managed by the object
84     * implementing this interface.
85     *
86     * Here is sample code to get a Set of critical extensions from an
87     * X509Certificate and print the OIDs:
88     * <pre>{@code
89     * X509Certificate cert = null;
90     * try (InputStream inStrm = new FileInputStream("DER-encoded-Cert")) {
91     *     CertificateFactory cf = CertificateFactory.getInstance("X.509");
92     *     cert = (X509Certificate)cf.generateCertificate(inStrm);
93     * }
94     *
95     * Set<String> critSet = cert.getCriticalExtensionOIDs();
96     * if (critSet != null && !critSet.isEmpty()) {
97     *     System.out.println("Set of critical extensions:");
98     *     for (String oid : critSet) {
99     *         System.out.println(oid);
100     *     }
101     * }
102     * }</pre>
103     * @return a Set (or an empty Set if none are marked critical) of
104     * the extension OID strings for extensions that are marked critical.
105     * If there are no extensions present at all, then this method returns
106     * null.
107     */
108    public Set<String> getCriticalExtensionOIDs();
109
110    /**
111     * Gets a Set of the OID strings for the extension(s) marked
112     * NON-CRITICAL in the certificate/CRL managed by the object
113     * implementing this interface.
114     *
115     * Here is sample code to get a Set of non-critical extensions from an
116     * X509CRL revoked certificate entry and print the OIDs:
117     * <pre>{@code
118     * CertificateFactory cf = null;
119     * X509CRL crl = null;
120     * try (InputStream inStrm = new FileInputStream("DER-encoded-CRL")) {
121     *     cf = CertificateFactory.getInstance("X.509");
122     *     crl = (X509CRL)cf.generateCRL(inStrm);
123     * }
124     *
125     * byte[] certData = <DER-encoded certificate data>
126     * ByteArrayInputStream bais = new ByteArrayInputStream(certData);
127     * X509Certificate cert = (X509Certificate)cf.generateCertificate(bais);
128     * X509CRLEntry badCert =
129     *              crl.getRevokedCertificate(cert.getSerialNumber());
130     *
131     * if (badCert != null) {
132     *     Set<String> nonCritSet = badCert.getNonCriticalExtensionOIDs();
133     *     if (nonCritSet != null)
134     *         for (String oid : nonCritSet) {
135     *             System.out.println(oid);
136     *         }
137     * }
138     * }</pre>
139     *
140     * @return a Set (or an empty Set if none are marked non-critical) of
141     * the extension OID strings for extensions that are marked non-critical.
142     * If there are no extensions present at all, then this method returns
143     * null.
144     */
145    public Set<String> getNonCriticalExtensionOIDs();
146
147    /**
148     * Gets the DER-encoded OCTET string for the extension value
149     * (<em>extnValue</em>) identified by the passed-in {@code oid}
150     * String.
151     * The {@code oid} string is
152     * represented by a set of nonnegative whole numbers separated
153     * by periods.
154     *
155     * <p>For example:<br>
156     * <table class="striped">
157     * <caption style="display:none">Examples of OIDs and extension names</caption>
158     * <thead>
159     * <tr>
160     * <th scope="col">OID <em>(Object Identifier)</em></th>
161     * <th scope="col">Extension Name</th></tr>
162     * </thead>
163     * <tbody style="text-align:left">
164     * <tr><th scope="row">2.5.29.14</th>
165     * <td>SubjectKeyIdentifier</td></tr>
166     * <tr><th scope="row">2.5.29.15</th>
167     * <td>KeyUsage</td></tr>
168     * <tr><th scope="row">2.5.29.16</th>
169     * <td>PrivateKeyUsage</td></tr>
170     * <tr><th scope="row">2.5.29.17</th>
171     * <td>SubjectAlternativeName</td></tr>
172     * <tr><th scope="row">2.5.29.18</th>
173     * <td>IssuerAlternativeName</td></tr>
174     * <tr><th scope="row">2.5.29.19</th>
175     * <td>BasicConstraints</td></tr>
176     * <tr><th scope="row">2.5.29.30</th>
177     * <td>NameConstraints</td></tr>
178     * <tr><th scope="row">2.5.29.33</th>
179     * <td>PolicyMappings</td></tr>
180     * <tr><th scope="row">2.5.29.35</th>
181     * <td>AuthorityKeyIdentifier</td></tr>
182     * <tr><th scope="row">2.5.29.36</th>
183     * <td>PolicyConstraints</td></tr>
184     * </tbody>
185     * </table>
186     *
187     * @param oid the Object Identifier value for the extension.
188     * @return the DER-encoded octet string of the extension value or
189     * null if it is not present.
190     */
191    public byte[] getExtensionValue(String oid);
192}
193