src/java.base/share/classes/sun/security/x509/IPAddressName.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 47216 71c04702a3d5
child 53082 4c539cb11633
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2002, 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.lang.Integer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Arrays;
34687
d302ed125dc9 8144995: Move sun.misc.HexDumpEncoder to sun.security.util
chegar
parents: 32649
diff changeset
    32
import sun.security.util.HexDumpEncoder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.util.BitArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This class implements the IPAddressName as required by the GeneralNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * ASN.1 object.  Both IPv4 and IPv6 addresses are supported using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * formats specified in IETF PKIX RFC2459.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * [RFC2459 4.2.1.7 Subject Alternative Name]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * When the subjectAltName extension contains a iPAddress, the address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * MUST be stored in the octet string in "network byte order," as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * specified in RFC 791. The least significant bit (LSB) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * each octet is the LSB of the corresponding byte in the network
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * address. For IP Version 4, as specified in RFC 791, the octet string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * MUST contain exactly four octets.  For IP Version 6, as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * RFC 1883, the octet string MUST contain exactly sixteen octets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * [RFC2459 4.2.1.11 Name Constraints]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The syntax of iPAddress MUST be as described in section 4.2.1.7 with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * the following additions specifically for Name Constraints.  For IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * addresses, the ipAddress field of generalName MUST contain eight (8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * octets, encoded in the style of RFC 1519 (CIDR) to represent an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * address range.[RFC 1519]  For IPv6 addresses, the ipAddress field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * MUST contain 32 octets similarly encoded.  For example, a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * constraint for "class C" subnet 10.9.8.0 shall be represented as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * octets 0A 09 08 00 FF FF FF 00, representing the CIDR notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * 10.9.8.0/255.255.255.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @see GeneralName
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see GeneralNameInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @see GeneralNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class IPAddressName implements GeneralNameInterface {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private byte[] address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private boolean isIPv4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Create the IPAddressName object from the passed encoded Der value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
    78
     * @param derValue the encoded DER IPAddressName.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @exception IOException on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public IPAddressName(DerValue derValue) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this(derValue.getOctetString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Create the IPAddressName object with the specified octets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
    88
     * @param address the IP address
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @throws IOException if address is not a valid IPv4 or IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public IPAddressName(byte[] address) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         * A valid address must consist of 4 bytes of address and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         * optional 4 bytes of 4 bytes of mask, or 16 bytes of address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
         * and optional 16 bytes of mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (address.length == 4 || address.length == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            isIPv4 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } else if (address.length == 16 || address.length == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            isIPv4 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IOException("Invalid IPAddressName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Create an IPAddressName from a String.
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
   109
     * [IETF RFC1338 Supernetting {@literal &} IETF RFC1519 Classless Inter-Domain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Routing (CIDR)] For IPv4 addresses, the forms are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * "b1.b2.b3.b4" or "b1.b2.b3.b4/m1.m2.m3.m4", where b1 - b4 are decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * byte values 0-255 and m1 - m4 are decimal mask values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * 0 - 255.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * [IETF RFC2373 IP Version 6 Addressing Architecture]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * For IPv6 addresses, the forms are "a1:a2:...:a8" or "a1:a2:...:a8/n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * where a1-a8 are hexadecimal values representing the eight 16-bit pieces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * of the address. If /n is used, n is a decimal number indicating how many
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * of the leftmost contiguous bits of the address comprise the prefix for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * this subnet. Internally, a mask value is created using the prefix length.
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
   121
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param name String form of IPAddressName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IOException if name can not be converted to a valid IPv4 or IPv6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *     address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public IPAddressName(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   128
        if (name == null || name.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new IOException("IPAddress cannot be null or empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (name.charAt(name.length() - 1) == '/') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new IOException("Invalid IPAddress: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (name.indexOf(':') >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            // name is IPv6: uses colons as value separators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            // Parse name into byte-value address components and optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            // prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            parseIPv6(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            isIPv4 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } else if (name.indexOf('.') >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            //name is IPv4: uses dots as value separators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            parseIPv4(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            isIPv4 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new IOException("Invalid IPAddress: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Parse an IPv4 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param name IPv4 address with optional mask values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private void parseIPv4(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // Parse name into byte-value address components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int slashNdx = name.indexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (slashNdx == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            address = InetAddress.getByName(name).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            address = new byte[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // parse mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            byte[] mask = InetAddress.getByName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                (name.substring(slashNdx+1)).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // parse base address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            byte[] host = InetAddress.getByName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                (name.substring(0, slashNdx)).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            System.arraycopy(host, 0, address, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            System.arraycopy(mask, 0, address, 4, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Parse an IPv6 address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param name String IPv6 address with optional /<prefix length>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *             If /<prefix length> is present, address[] array will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *             be 32 bytes long, otherwise 16.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @throws IOException on error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30649
diff changeset
   186
    private static final int MASKSIZE = 16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private void parseIPv6(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int slashNdx = name.indexOf('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (slashNdx == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            address = InetAddress.getByName(name).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            address = new byte[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            byte[] base = InetAddress.getByName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                (name.substring(0, slashNdx)).getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            System.arraycopy(base, 0, address, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // append a mask corresponding to the num of prefix bits specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            int prefixLen = Integer.parseInt(name.substring(slashNdx+1));
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
   200
            if (prefixLen < 0 || prefixLen > 128) {
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
   201
                throw new IOException("IPv6Address prefix length (" +
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
   202
                        prefixLen + ") in out of valid range [0,128]");
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
   203
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // create new bit array initialized to zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            BitArray bitArray = new BitArray(MASKSIZE * 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            // set all most significant bits up to prefix length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            for (int i = 0; i < prefixLen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                bitArray.set(i, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            byte[] maskArray = bitArray.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // copy mask bytes into mask portion of address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            for (int i = 0; i < MASKSIZE; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                address[MASKSIZE+i] = maskArray[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Return the type of the GeneralName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return NAME_IP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Encode the IPAddress name into the DerOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
   229
     * @param out the DER stream to encode the IPAddressName to.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @exception IOException on encoding errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public void encode(DerOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        out.putOctetString(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Return a printable string of IPaddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            return "IPAddress: " + getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            // dump out hex rep for debugging purposes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            HexDumpEncoder enc = new HexDumpEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return "IPAddress: " + enc.encodeBuffer(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Return a standard String representation of IPAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * See IPAddressName(String) for the formats used for IPv4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * and IPv6 addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @throws IOException if the IPAddress cannot be converted to a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public String getName() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (name != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (isIPv4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            //IPv4 address or subdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            byte[] host = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            System.arraycopy(address, 0, host, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            name = InetAddress.getByAddress(host).getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (address.length == 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                byte[] mask = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                System.arraycopy(address, 4, mask, 0, 4);
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   268
                name = name + '/' +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                       InetAddress.getByAddress(mask).getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            //IPv6 address or subdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            byte[] host = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            System.arraycopy(address, 0, host, 0, 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            name = InetAddress.getByAddress(host).getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (address.length == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                // IPv6 subdomain: display prefix length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                // copy subdomain into new array and convert to BitArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                byte[] maskBytes = new byte[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                for (int i=16; i < 32; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    maskBytes[i-16] = address[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                BitArray ba = new BitArray(16*8, maskBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                // Find first zero bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                for (; i < 16*8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    if (!ba.get(i))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
30649
e7cc8f48f616 8080522: Optimize string operations in java.base/share/classes/sun/security/x509/
igerasim
parents: 30374
diff changeset
   290
                name = name + '/' + i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                // Verify remaining bits 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                for (; i < 16*8; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    if (ba.get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        throw new IOException("Invalid IPv6 subdomain - set " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            "bit " + i + " not contiguous");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Returns this IPAddress name as a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return address.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Compares this name with another, for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return true iff the names are identical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (this == obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (!(obj instanceof IPAddressName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
   322
        IPAddressName otherName = (IPAddressName)obj;
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
   323
        byte[] other = otherName.address;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (other.length != address.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        if (address.length == 8 || address.length == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // Two subnet addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // Mask each and compare masked values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            int maskLen = address.length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            for (int i=0; i < maskLen; i++) {
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
   333
                byte maskedThis = (byte)(address[i] & address[i+maskLen]);
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
   334
                byte maskedOther = (byte)(other[i] & other[i+maskLen]);
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
   335
                if (maskedThis != maskedOther) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            // Now compare masks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            for (int i=maskLen; i < address.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                if (address[i] != other[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // Two IPv4 host addresses or two IPv6 host addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            // Compare bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            return Arrays.equals(other, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        for (int i=0; i<address.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            retval += address[i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Return type of constraint inputName places on this name:<ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *   <li>NAME_DIFF_TYPE = -1: input name is different type from name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *       (i.e. does not constrain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *   <li>NAME_MATCH = 0: input name matches name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *   <li>NAME_NARROWS = 1: input name narrows name (is lower in the naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *       subtree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *   <li>NAME_WIDENS = 2: input name widens name (is higher in the naming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *       subtree)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *   <li>NAME_SAME_TYPE = 3: input name does not match or narrow name, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *       is same type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * </ul>.  These results are used in checking NameConstraints during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * certification path verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * [RFC2459] The syntax of iPAddress MUST be as described in section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * 4.2.1.7 with the following additions specifically for Name Constraints.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * For IPv4 addresses, the ipAddress field of generalName MUST contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * eight (8) octets, encoded in the style of RFC 1519 (CIDR) to represent an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * address range.[RFC 1519]  For IPv6 addresses, the ipAddress field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * MUST contain 32 octets similarly encoded.  For example, a name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * constraint for "class C" subnet 10.9.8.0 shall be represented as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * octets 0A 09 08 00 FF FF FF 00, representing the CIDR notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * 10.9.8.0/255.255.255.0.
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
   388
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param inputName to be checked for being constrained
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
   390
     * @return constraint type above
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @throws UnsupportedOperationException if name is not exact match, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * narrowing and widening are not supported for this name type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public int constrains(GeneralNameInterface inputName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        int constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (inputName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        else if (inputName.getType() != NAME_IP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            constraintType = NAME_DIFF_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        else if (((IPAddressName)inputName).equals(this))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            constraintType = NAME_MATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        else {
35283
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
   404
            IPAddressName otherName = (IPAddressName)inputName;
c5082624b79f 8074068: Cleanup in java.base/share/classes/sun/security/x509/
igerasim
parents: 34687
diff changeset
   405
            byte[] otherAddress = otherName.address;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            if (otherAddress.length == 4 && address.length == 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                // Two host addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            else if ((otherAddress.length == 8 && address.length == 8) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                     (otherAddress.length == 32 && address.length == 32)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                // Two subnet addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                // See if one address fully encloses the other address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                boolean otherSubsetOfThis = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                boolean thisSubsetOfOther = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                boolean thisEmpty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                boolean otherEmpty = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                int maskOffset = address.length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                for (int i=0; i < maskOffset; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    if ((byte)(address[i] & address[i+maskOffset]) != address[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        thisEmpty=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    if ((byte)(otherAddress[i] & otherAddress[i+maskOffset]) != otherAddress[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        otherEmpty=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    if (!(((byte)(address[i+maskOffset] & otherAddress[i+maskOffset]) == address[i+maskOffset]) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                          ((byte)(address[i]   & address[i+maskOffset])      == (byte)(otherAddress[i] & address[i+maskOffset])))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        otherSubsetOfThis = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    if (!(((byte)(otherAddress[i+maskOffset] & address[i+maskOffset])      == otherAddress[i+maskOffset]) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                          ((byte)(otherAddress[i]   & otherAddress[i+maskOffset]) == (byte)(address[i] & otherAddress[i+maskOffset])))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        thisSubsetOfOther = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                if (thisEmpty || otherEmpty) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    if (thisEmpty && otherEmpty)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        constraintType = NAME_MATCH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    else if (thisEmpty)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                } else if (otherSubsetOfThis)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                else if (thisSubsetOfOther)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            } else if (otherAddress.length == 8 || otherAddress.length == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                //Other is a subnet, this is a host address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                int maskOffset = otherAddress.length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                for (; i < maskOffset; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    // Mask this address by other address mask and compare to other address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    // If all match, then this address is in other address subnet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    if ((address[i] & otherAddress[i+maskOffset]) != otherAddress[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                if (i == maskOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    constraintType = NAME_WIDENS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            } else if (address.length == 8 || address.length == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                //This is a subnet, other is a host address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                int maskOffset = address.length/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                for (; i < maskOffset; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                    // Mask other address by this address mask and compare to this address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    if ((otherAddress[i] & address[i+maskOffset]) != address[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                if (i == maskOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    constraintType = NAME_NARROWS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                constraintType = NAME_SAME_TYPE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return constraintType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Return subtree depth of this name for purposes of determining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * NameConstraints minimum and maximum bounds and for calculating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * path lengths in name subtrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 25859
diff changeset
   484
     * @return distance of name from root
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @throws UnsupportedOperationException if not supported for this name type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public int subtreeDepth() throws UnsupportedOperationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        throw new UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            ("subtreeDepth() not defined for IPAddressName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
}