src/java.security.jgss/share/classes/sun/security/krb5/internal/HostAddress.java
author igerasim
Tue, 12 Nov 2019 01:36:17 -0800
changeset 59024 b046ba510bbc
parent 58331 e4ce29f6094e
permissions -rw-r--r--
8233884: Avoid looking up standard charsets in security libraries Reviewed-by: coffeys
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58331
e4ce29f6094e 8228659: Record which Java methods are called by native codes in JGSS and JAAS
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2019, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.security.krb5.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.Config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.Asn1Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.Inet4Address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.Inet6Address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.net.UnknownHostException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.IOException;
32013
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
    42
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Implements the ASN.1 HostAddress type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
    47
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * HostAddress     ::= SEQUENCE  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *         addr-type       [0] Int32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *         address         [1] OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * }
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
    52
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class HostAddress implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int addrType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    byte[] address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static InetAddress localInetAddress; //caches local inet address
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private volatile int hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private HostAddress(int dummy) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        HostAddress new_hostAddress = new HostAddress(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        new_hostAddress.addrType = addrType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (address != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            new_hostAddress.address = address.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return new_hostAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (hashCode == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            result = 37*result + addrType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (address != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                for (int i=0; i < address.length; i++)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    result = 37*result + address[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            hashCode = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (!(obj instanceof HostAddress)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        HostAddress h = (HostAddress)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (addrType != h.addrType ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            (address != null && h.address == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            (address == null && h.address != null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (address != null && h.address != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (address.length != h.address.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            for (int i = 0; i < address.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (address[i] != h.address[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static synchronized InetAddress getLocalInetAddress()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (localInetAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
           localInetAddress = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (localInetAddress == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new UnknownHostException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return (localInetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Gets the InetAddress of this HostAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @return the IP address for this specified host.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   135
     * @exception UnknownHostException if no IP address for the host could be found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public InetAddress getInetAddress() throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        // the type of internet addresses is 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (addrType == Krb5.ADDRTYPE_INET ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            addrType == Krb5.ADDRTYPE_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return (InetAddress.getByAddress(address));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // if it is other type (ISO address, XNS address, etc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private int getAddrType(InetAddress inetAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int addressType = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (inetAddress instanceof Inet4Address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            addressType = Krb5.ADDRTYPE_INET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        else if (inetAddress instanceof Inet6Address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            addressType = Krb5.ADDRTYPE_INET6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return (addressType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // implicit default not in Config.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public HostAddress() throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        InetAddress inetAddress = getLocalInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        addrType = getAddrType(inetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        address = inetAddress.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Creates a HostAddress from the specified address and address type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
58331
e4ce29f6094e 8228659: Record which Java methods are called by native codes in JGSS and JAAS
weijun
parents: 47216
diff changeset
   168
     * Warning: called by nativeccache.c.
e4ce29f6094e 8228659: Record which Java methods are called by native codes in JGSS and JAAS
weijun
parents: 47216
diff changeset
   169
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param new_addrType the value of the address type which matches the defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *                       address family constants in the Berkeley Standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *                       Distributions of Unix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param new_address network address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @exception KrbApErrException if address type and address length do not match defined value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public HostAddress(int new_addrType, byte[] new_address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        throws KrbApErrException, UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        switch(new_addrType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        case Krb5.ADDRTYPE_INET:        //Internet address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (new_address.length != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                throw new KrbApErrException(0, "Invalid Internet address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        case Krb5.ADDRTYPE_CHAOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if (new_address.length != 2) //CHAOSnet address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                throw new KrbApErrException(0, "Invalid CHAOSnet address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        case Krb5.ADDRTYPE_ISO:   // ISO address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        case Krb5.ADDRTYPE_IPX:   // XNS address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (new_address.length != 6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                throw new KrbApErrException(0, "Invalid XNS address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        case Krb5.ADDRTYPE_APPLETALK:  //AppleTalk DDP address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (new_address.length != 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                throw new KrbApErrException(0, "Invalid DDP address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        case Krb5.ADDRTYPE_DECNET:    //DECnet Phase IV address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (new_address.length != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                throw new KrbApErrException(0, "Invalid DECnet Phase IV address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        case Krb5.ADDRTYPE_INET6:     //Internet IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (new_address.length != 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                throw new KrbApErrException(0, "Invalid Internet IPv6 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        addrType = new_addrType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (new_address != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
           address = new_address.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (addrType == Krb5.ADDRTYPE_INET ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                addrType == Krb5.ADDRTYPE_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                System.out.println("Host address is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        InetAddress.getByAddress(address));
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public HostAddress(InetAddress inetAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        addrType = getAddrType(inetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        address = inetAddress.getAddress();
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
     * Constructs a host address from a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public HostAddress(DerValue encoding) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        DerValue der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if ((der.getTag() & (byte)0x1F) == (byte)0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            addrType = der.getData().getBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if ((der.getTag() & (byte)0x1F) == (byte)0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            address = der.getData().getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (encoding.getData().available() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         * Encodes a HostAddress object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         * @return a byte array of encoded HostAddress object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        temp.putInteger(this.addrType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        temp.putOctetString(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Parses (unmarshal) a host address from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
         * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @exception Asn1Exception on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param data the Der input stream value, which contains one or more marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param optional indicates if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @return an instance of HostAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public static HostAddress parse(DerInputStream data, byte explicitTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                    boolean optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        throws Asn1Exception, IOException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if ((optional) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            (((byte)data.peekByte() & (byte)0x1F) != explicitTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return new HostAddress(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
32013
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   301
    @Override
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   302
    public String toString() {
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   303
        StringBuilder sb = new StringBuilder();
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   304
        sb.append(Arrays.toString(address));
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   305
        sb.append('(').append(addrType).append(')');
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   306
        return sb.toString();
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   307
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}