src/java.security.jgss/share/classes/sun/security/krb5/internal/HostAddresses.java
author weijun
Wed, 25 Sep 2019 17:54:21 +0800
changeset 58331 e4ce29f6094e
parent 47216 71c04702a3d5
permissions -rw-r--r--
8228659: Record which Java methods are called by native codes in JGSS and JAAS Reviewed-by: mullan
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
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
    34
import sun.security.krb5.Config;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.PrincipalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.Asn1Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.util.*;
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
    39
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
    40
import java.net.*;
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
    41
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.security.krb5.internal.ccache.CCacheOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Implements the ASN.1 HostAddresses type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
    48
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * HostAddresses   -- NOTE: subtly different from rfc1510,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *                 -- but has a value mapping and encodes the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *         ::= SEQUENCE OF HostAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * HostAddress     ::= SEQUENCE  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *         addr-type       [0] Int32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *         address         [1] OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * }
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 31538
diff changeset
    57
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class HostAddresses implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private HostAddress[] addresses = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private volatile int hashCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
58331
e4ce29f6094e 8228659: Record which Java methods are called by native codes in JGSS and JAAS
weijun
parents: 47216
diff changeset
    71
    // Warning: called by nativeccache.c
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public HostAddresses(HostAddress[] new_addresses) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (new_addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
           addresses = new HostAddress[new_addresses.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
           for (int i = 0; i < new_addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                if (new_addresses[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                   throw new IOException("Cannot create a HostAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                   addresses[i] = (HostAddress)new_addresses[i].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public HostAddresses() throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        addresses = new HostAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        addresses[0] = new HostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private HostAddresses(int dummy) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public HostAddresses(PrincipalName serverPrincipal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        throws UnknownHostException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        String[] components = serverPrincipal.getNameStrings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (serverPrincipal.getNameType() != PrincipalName.KRB_NT_SRV_HST ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            components.length < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new KrbException(Krb5.KRB_ERR_GENERIC, "Bad name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String host = components[1];
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30959
diff changeset
   102
        InetAddress[] addr = InetAddress.getAllByName(host);
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30959
diff changeset
   103
        HostAddress[] hAddrs = new HostAddress[addr.length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        for (int i = 0; i < addr.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            hAddrs[i] = new HostAddress(addr[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        addresses = hAddrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        HostAddresses new_hostAddresses = new HostAddresses(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            new_hostAddresses.addresses = new HostAddress[addresses.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            for (int i = 0; i < addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                new_hostAddresses.addresses[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        (HostAddress)addresses[i].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return new_hostAddresses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public boolean inList(HostAddress addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            for (int i = 0; i < addresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (addresses[i].equals(addr))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (hashCode == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                for (int i=0; i < addresses.length; i++)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    result = 37*result + addresses[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            hashCode = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (!(obj instanceof HostAddresses)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        HostAddresses addrs = (HostAddresses)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if ((addresses == null && addrs.addresses != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            (addresses != null && addrs.addresses == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (addresses != null && addrs.addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (addresses.length != addrs.addresses.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            for (int i = 0; i < addresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                if (!addresses[i].equals(addrs.addresses[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    * Constructs a new <code>HostAddresses</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    * @exception IOException if an I/O error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public HostAddresses(DerValue encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        throws  Asn1Exception, IOException {
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 5506
diff changeset
   181
        Vector<HostAddress> tempAddresses = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        DerValue der = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        while (encoding.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            tempAddresses.addElement(new HostAddress(der));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (tempAddresses.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            addresses = new HostAddress[tempAddresses.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            tempAddresses.copyInto(addresses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    * Encodes a <code>HostAddresses</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    * @return byte array of encoded <code>HostAddresses</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    * @exception IOException if an I/O error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (addresses != null && addresses.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            for (int i = 0; i < addresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                bytes.write(addresses[i].asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Parse (unmarshal) a <code>HostAddresses</code> from a DER input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @exception Asn1Exception if an Asn1Exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param data the Der input stream value, which contains one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @param optional indicates if this data field is optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return an instance of <code>HostAddresses</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static HostAddresses parse(DerInputStream data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                      byte explicitTag, boolean optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if ((optional) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            (((byte)data.peekByte() & (byte)0x1F) != explicitTag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return new HostAddresses(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         * Writes data field values in <code>HostAddresses</code> in FCC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * format to a <code>CCacheOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * @param cos a <code>CCacheOutputStream</code> to be written to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * @exception IOException if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         * @see sun.security.krb5.internal.ccache.CCacheOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public void writeAddrs(CCacheOutputStream cos) throws IOException {
27946
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   252
        if (addresses == null || addresses.length == 0) {
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   253
            cos.write32(0);
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   254
            return;
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 25859
diff changeset
   255
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        cos.write32(addresses.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        for (int i = 0; i < addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            cos.write16(addresses[i].addrType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            cos.write32(addresses[i].address.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            cos.write(addresses[i].address, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                      addresses[i].address.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public InetAddress[] getInetAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (addresses == null || addresses.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 5506
diff changeset
   271
        ArrayList<InetAddress> ipAddrs = new ArrayList<>(addresses.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        for (int i = 0; i < addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if ((addresses[i].addrType == Krb5.ADDRTYPE_INET) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    (addresses[i].addrType == Krb5.ADDRTYPE_INET6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    ipAddrs.add(addresses[i].getInetAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } catch (java.net.UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                // Should not happen since IP address given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        InetAddress[] retVal = new InetAddress[ipAddrs.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return ipAddrs.toArray(retVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns all the IP addresses of the local host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public static HostAddresses getLocalAddresses() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    {
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   295
        Set<InetAddress> all = new LinkedHashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (DEBUG) {
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   298
                System.out.println(">>> KrbKdcReq local addresses are:");
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   299
            }
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   300
            String extra = Config.getInstance().getAll(
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   301
                    "libdefaults", "extra_addresses");
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   302
            if (extra != null) {
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   303
                for (String s: extra.split("\\s+")) {
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   304
                    all.add(InetAddress.getByName(s));
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   305
                    if (DEBUG) {
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   306
                        System.out.println("   extra_addresses: "
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   307
                                + InetAddress.getByName(s));
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   308
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   311
            for (NetworkInterface ni:
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   312
                    Collections.list(NetworkInterface.getNetworkInterfaces())) {
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   313
                if (DEBUG) {
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   314
                    System.out.println("   NetworkInterface " + ni + ":");
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   315
                    System.out.println("      "
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   316
                            + Collections.list(ni.getInetAddresses()));
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   317
                }
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   318
                all.addAll(Collections.list(ni.getInetAddresses()));
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   319
            }
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   320
            return new HostAddresses(all.toArray(new InetAddress[all.size()]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            throw new IOException(exc.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Creates a new HostAddresses instance from the supplied list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * of InetAddresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public HostAddresses(InetAddress[] inetAddresses)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (inetAddresses == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                addresses = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        addresses = new HostAddress[inetAddresses.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        for (int i = 0; i < inetAddresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            addresses[i] = new HostAddress(inetAddresses[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
32013
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   342
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   343
    @Override
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   344
    public String toString() {
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   345
        return Arrays.toString(addresses);
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   346
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
}