src/java.security.jgss/share/classes/sun/security/krb5/internal/HostAddress.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 32013 jdk/src/java.security.jgss/share/classes/sun/security/krb5/internal/HostAddress.java@e7ad0380f7be
child 58331 e4ce29f6094e
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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) 2000, 2006, 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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param new_addrType the value of the address type which matches the defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *                       address family constants in the Berkeley Standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *                       Distributions of Unix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param new_address network address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @exception KrbApErrException if address type and address length do not match defined value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public HostAddress(int new_addrType, byte[] new_address)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        throws KrbApErrException, UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        switch(new_addrType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        case Krb5.ADDRTYPE_INET:        //Internet address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (new_address.length != 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                throw new KrbApErrException(0, "Invalid Internet address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        case Krb5.ADDRTYPE_CHAOS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (new_address.length != 2) //CHAOSnet address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                throw new KrbApErrException(0, "Invalid CHAOSnet address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        case Krb5.ADDRTYPE_ISO:   // ISO address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        case Krb5.ADDRTYPE_IPX:   // XNS address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (new_address.length != 6)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                throw new KrbApErrException(0, "Invalid XNS address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        case Krb5.ADDRTYPE_APPLETALK:  //AppleTalk DDP address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if (new_address.length != 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                throw new KrbApErrException(0, "Invalid DDP address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        case Krb5.ADDRTYPE_DECNET:    //DECnet Phase IV address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (new_address.length != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                throw new KrbApErrException(0, "Invalid DECnet Phase IV address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        case Krb5.ADDRTYPE_INET6:     //Internet IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (new_address.length != 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                throw new KrbApErrException(0, "Invalid Internet IPv6 address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        addrType = new_addrType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (new_address != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
           address = new_address.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (addrType == Krb5.ADDRTYPE_INET ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                addrType == Krb5.ADDRTYPE_INET6) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                System.out.println("Host address is " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        InetAddress.getByAddress(address));
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public HostAddress(InetAddress inetAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        addrType = getAddrType(inetAddress);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        address = inetAddress.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Constructs a host address from a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public HostAddress(DerValue encoding) throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        DerValue der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if ((der.getTag() & (byte)0x1F) == (byte)0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            addrType = der.getData().getBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if ((der.getTag() & (byte)0x1F) == (byte)0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            address = der.getData().getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (encoding.getData().available() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
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
         * Encodes a HostAddress object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         * @return a byte array of encoded HostAddress object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
         * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
         * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        temp.putInteger(this.addrType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        temp.putOctetString(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Parses (unmarshal) a host address from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
         * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception Asn1Exception on error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param data the Der input stream value, which contains one or more marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param optional indicates if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return an instance of HostAddress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public static HostAddress parse(DerInputStream data, byte explicitTag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                    boolean optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        throws Asn1Exception, IOException{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if ((optional) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            (((byte)data.peekByte() & (byte)0x1F) != explicitTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return new HostAddress(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
32013
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   299
    @Override
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   300
    public String toString() {
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   301
        StringBuilder sb = new StringBuilder();
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   302
        sb.append(Arrays.toString(address));
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   303
        sb.append('(').append(addrType).append(')');
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   304
        return sb.toString();
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   305
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
}