src/java.base/share/classes/sun/security/x509/PolicyMappingsExtension.java
author igerasim
Tue, 02 Oct 2018 10:19:07 -0700
changeset 51986 c1db377f6300
parent 47216 71c04702a3d5
permissions -rw-r--r--
8200381: Typos in javadoc - missing verb "be" and alike Reviewed-by: lancea, darcy, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28420
8fe17d073b54 8059916: Change default criticality of policy mappings and policy constraints certificate extensions
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.x509;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Represent the Policy Mappings Extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This extension, if present, identifies the certificate policies considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * identical between the issuing and the subject CA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>Extensions are addiitonal attributes which can be inserted in a X509
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * v3 certificate. For example a "Driving License Certificate" could have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the driving license number as a extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>Extensions are represented as a sequence of the extension identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * (Object Identifier), a boolean flag stating whether the extension is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * be treated as being critical and the extension value itself (this is again
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * a DER encoding of the extension value).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see CertAttrSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class PolicyMappingsExtension extends Extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
implements CertAttrSet<String> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Identifier for this attribute, to be used with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * get, set, delete methods of Certificate, x509 type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final String IDENT = "x509.info.extensions.PolicyMappings";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Attribute names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final String NAME = "PolicyMappings";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static final String MAP = "map";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // Private data members
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private List<CertificatePolicyMap> maps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // Encode this extension value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private void encodeThis() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (maps == null || maps.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            this.extensionValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        DerOutputStream os = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        for (CertificatePolicyMap map : maps) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            map.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        os.write(DerValue.tag_Sequence, tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.extensionValue = os.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Create a PolicyMappings with the List of CertificatePolicyMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param maps the List of CertificatePolicyMap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28420
diff changeset
    91
    public PolicyMappingsExtension(List<CertificatePolicyMap> maps)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throws IOException {
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28420
diff changeset
    93
        this.maps = maps;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.extensionId = PKIXExtensions.PolicyMappings_Id;
28420
8fe17d073b54 8059916: Change default criticality of policy mappings and policy constraints certificate extensions
juh
parents: 25859
diff changeset
    95
        this.critical = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Create a default PolicyMappingsExtension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public PolicyMappingsExtension() {
28420
8fe17d073b54 8059916: Change default criticality of policy mappings and policy constraints certificate extensions
juh
parents: 25859
diff changeset
   103
        extensionId = PKIXExtensions.PolicyMappings_Id;
8fe17d073b54 8059916: Change default criticality of policy mappings and policy constraints certificate extensions
juh
parents: 25859
diff changeset
   104
        critical = true;
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   105
        maps = Collections.<CertificatePolicyMap>emptyList();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Create the extension from the passed DER encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28420
diff changeset
   111
     * @param critical true if the extension is to be treated as critical.
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 28420
diff changeset
   112
     * @param value an array of DER encoded bytes of the actual value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @exception ClassCastException if value is not an array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public PolicyMappingsExtension(Boolean critical, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.extensionId = PKIXExtensions.PolicyMappings_Id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.critical = critical.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.extensionValue = (byte[]) value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        DerValue val = new DerValue(this.extensionValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (val.tag != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new IOException("Invalid encoding for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                  "PolicyMappingsExtension.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        maps = new ArrayList<CertificatePolicyMap>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        while (val.data.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            DerValue seq = val.data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            CertificatePolicyMap map = new CertificatePolicyMap(seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            maps.add(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Returns a printable representation of the policy map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (maps == null) return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        String s = super.toString() + "PolicyMappings [\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                 + maps.toString() + "]\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Write the extension to the OutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param out the OutputStream to write the extension to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void encode(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        DerOutputStream tmp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (extensionValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            extensionId = PKIXExtensions.PolicyMappings_Id;
28420
8fe17d073b54 8059916: Change default criticality of policy mappings and policy constraints certificate extensions
juh
parents: 25859
diff changeset
   156
            critical = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        super.encode(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        out.write(tmp.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Set the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   166
    @SuppressWarnings("unchecked") // Checked with instanceof
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public void set(String name, Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (name.equalsIgnoreCase(MAP)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (!(obj instanceof List)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
              throw new IOException("Attribute value should be of" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                    " type List.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            maps = (List<CertificatePolicyMap>)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
          throw new IOException("Attribute name not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        "CertAttrSet:PolicyMappingsExtension.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Get the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   184
    public List<CertificatePolicyMap> get(String name) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (name.equalsIgnoreCase(MAP)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return (maps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
          throw new IOException("Attribute name not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        "CertAttrSet:PolicyMappingsExtension.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Delete the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public void delete(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (name.equalsIgnoreCase(MAP)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            maps = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
          throw new IOException("Attribute name not recognized by " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        "CertAttrSet:PolicyMappingsExtension.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        encodeThis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Return an enumeration of names of attributes existing within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public Enumeration<String> getElements () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        AttributeNameEnumeration elements = new AttributeNameEnumeration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        elements.addElement(MAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return elements.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Return the name of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public String getName () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return (NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
}