ipaddressname.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 493 行 · 第 1/2 页

JAVA
493
字号
/* * @(#)IPAddressName.java	1.15 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * */package sun.security.x509;import java.io.IOException;import java.lang.Integer;import java.net.InetAddress;import java.util.Arrays;import sun.misc.HexDumpEncoder;import sun.security.util.BitArray;import sun.security.util.DerOutputStream;import sun.security.util.DerValue;/** * This class implements the IPAddressName as required by the GeneralNames * ASN.1 object.  Both IPv4 and IPv6 addresses are supported using the * formats specified in IETF PKIX RFC2459. * <p> * [RFC2459 4.2.1.7 Subject Alternative Name] * When the subjectAltName extension contains a iPAddress, the address * MUST be stored in the octet string in "network byte order," as * specified in RFC 791. The least significant bit (LSB) of * each octet is the LSB of the corresponding byte in the network * address. For IP Version 4, as specified in RFC 791, the octet string * MUST contain exactly four octets.  For IP Version 6, as specified in * RFC 1883, the octet string MUST contain exactly sixteen octets. * <p> * [RFC2459 4.2.1.11 Name Constraints] * The syntax of iPAddress MUST be as described in section 4.2.1.7 with * the following additions specifically for Name Constraints.  For IPv4 * addresses, the ipAddress field of generalName MUST contain eight (8) * octets, encoded in the style of RFC 1519 (CIDR) to represent an * address range.[RFC 1519]  For IPv6 addresses, the ipAddress field * MUST contain 32 octets similarly encoded.  For example, a name * constraint for "class C" subnet 10.9.8.0 shall be represented as the * octets 0A 09 08 00 FF FF FF 00, representing the CIDR notation * 10.9.8.0/255.255.255.0. * <p> * @see GeneralName * @see GeneralNameInterface * @see GeneralNames * * @version 1.8 * * @author Amit Kapoor * @author Hemma Prafullchandra */public class IPAddressName implements GeneralNameInterface {    private byte[] address;    private boolean isIPv4;    private String name;    /**     * Create the IPAddressName object from the passed encoded Der value.     *     * @params derValue the encoded DER IPAddressName.     * @exception IOException on error.     */    public IPAddressName(DerValue derValue) throws IOException {        this(derValue.getOctetString());    }    /**     * Create the IPAddressName object with the specified octets.     *     * @params address the IP address     * @throws IOException if address is not a valid IPv4 or IPv6 address     */    public IPAddressName(byte[] address) throws IOException {	/*	 * A valid address must consist of 4 bytes of address and	 * optional 4 bytes of 4 bytes of mask, or 16 bytes of address	 * and optional 16 bytes of mask.	 */	if (address.length == 4 || address.length == 8) {	    isIPv4 = true;	} else if (address.length == 16 || address.length == 32) {	    isIPv4 = false;	} else {	    throw new IOException("Invalid IPAddressName");	}        this.address = address;    }    /**     * Create an IPAddressName from a String.     * [IETF RFC1338 Supernetting & IETF RFC1519 Classless Inter-Domain     * Routing (CIDR)] For IPv4 addresses, the forms are     * "b1.b2.b3.b4" or "b1.b2.b3.b4/m1.m2.m3.m4", where b1 - b4 are decimal     * byte values 0-255 and m1 - m4 are decimal mask values     * 0 - 255.     * <p>     * [IETF RFC2373 IP Version 6 Addressing Architecture]     * For IPv6 addresses, the forms are "a1:a2:...:a8" or "a1:a2:...:a8/n",     * where a1-a8 are hexadecimal values representing the eight 16-bit pieces      * of the address. If /n is used, n is a decimal number indicating how many      * of the leftmost contiguous bits of the address comprise the prefix for      * this subnet. Internally, a mask value is created using the prefix length.     * <p>     * @param name String form of IPAddressName     * @throws IOException if name can not be converted to a valid IPv4 or IPv6     *     address     */    public IPAddressName(String name) throws IOException {	if (name == null || name.length() == 0) {	    throw new IOException("IPAddress cannot be null or empty");	}	if (name.charAt(name.length() - 1) == '/') {	    throw new IOException("Invalid IPAddress: " + name);	}	if (name.indexOf(':') >= 0) {	    // name is IPv6: uses colons as value separators	    // Parse name into byte-value address components and optional 	    // prefix	    parseIPv6(name);	    isIPv4 = false;	} else if (name.indexOf('.') >= 0) {	    //name is IPv4: uses dots as value separators	    parseIPv4(name);	    isIPv4 = true;	} else {	    throw new IOException("Invalid IPAddress: " + name);	}    }    /**     * Parse an IPv4 address.     *     * @param name IPv4 address with optional mask values     * @throws IOException on error     */    private void parseIPv4(String name) throws IOException {        // Parse name into byte-value address components        int slashNdx = name.indexOf('/');        if (slashNdx == -1) {	    address = InetAddress.getByName(name).getAddress();        } else {	    address = new byte[8];	    // parse mask	    byte[] mask = InetAddress.getByName		(name.substring(slashNdx+1)).getAddress();	    // parse base address	    byte[] host = InetAddress.getByName		(name.substring(0, slashNdx)).getAddress();	    System.arraycopy(host, 0, address, 0, 4);	    System.arraycopy(mask, 0, address, 4, 4);        }    }    /**     * Parse an IPv6 address.     *     * @param name String IPv6 address with optional /<prefix length>     *             If /<prefix length> is present, address[] array will     *             be 32 bytes long, otherwise 16.     * @throws IOException on error     */    private final static int MASKSIZE = 16;    private void parseIPv6(String name) throws IOException {	int slashNdx = name.indexOf('/');	if (slashNdx == -1) {	    address = InetAddress.getByName(name).getAddress();	} else {	    address = new byte[32];	    byte[] base = InetAddress.getByName		(name.substring(0, slashNdx)).getAddress();	    System.arraycopy(base, 0, address, 0, 16);	     	    // append a mask corresponding to the num of prefix bits specified	    int prefixLen = Integer.parseInt(name.substring(slashNdx+1));	    if (prefixLen > 128)		throw new IOException("IPv6Address prefix is longer than 128");            // create new bit array initialized to zeros            BitArray bitArray = new BitArray(MASKSIZE * 8);            // set all most significant bits up to prefix length            for (int i = 0; i < prefixLen; i++)                bitArray.set(i, true);            byte[] maskArray = bitArray.toByteArray();            // copy mask bytes into mask portion of address            for (int i = 0; i < MASKSIZE; i++)                address[MASKSIZE+i] = maskArray[i];	}    }    /**     * Return the type of the GeneralName.     */    public int getType() {        return NAME_IP;    }    /**     * Encode the IPAddress name into the DerOutputStream.     *     * @params out the DER stream to encode the IPAddressName to.     * @exception IOException on encoding errors.     */    public void encode(DerOutputStream out) throws IOException {        out.putOctetString(address);    }    /**     * Return a printable string of IPaddress     */    public String toString() {        try {	    return "IPAddress: " + getName();	} catch (IOException ioe) {	    // dump out hex rep for debugging purposes	    HexDumpEncoder enc = new HexDumpEncoder();	    return "IPAddress: " + enc.encodeBuffer(address);	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?