src/java.security.jgss/share/classes/sun/security/krb5/internal/HostAddresses.java
author mbalao
Wed, 05 Jun 2019 01:42:11 -0300
changeset 55258 d65d3c37232c
parent 47216 71c04702a3d5
child 58331 e4ce29f6094e
permissions -rw-r--r--
8215032: Support Kerberos cross-realm referrals (RFC 6806) Reviewed-by: weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7977
diff changeset
     2
 * Copyright (c) 2000, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public HostAddresses(HostAddress[] new_addresses) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (new_addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
           addresses = new HostAddress[new_addresses.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
           for (int i = 0; i < new_addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                if (new_addresses[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                   throw new IOException("Cannot create a HostAddress");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                   addresses[i] = (HostAddress)new_addresses[i].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }
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
    public HostAddresses() throws UnknownHostException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        addresses = new HostAddress[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        addresses[0] = new HostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private HostAddresses(int dummy) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public HostAddresses(PrincipalName serverPrincipal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        throws UnknownHostException, KrbException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        String[] components = serverPrincipal.getNameStrings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (serverPrincipal.getNameType() != PrincipalName.KRB_NT_SRV_HST ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            components.length < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new KrbException(Krb5.KRB_ERR_GENERIC, "Bad name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String host = components[1];
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30959
diff changeset
   101
        InetAddress[] addr = InetAddress.getAllByName(host);
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30959
diff changeset
   102
        HostAddress[] hAddrs = new HostAddress[addr.length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (int i = 0; i < addr.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            hAddrs[i] = new HostAddress(addr[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        addresses = hAddrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        HostAddresses new_hostAddresses = new HostAddresses(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            new_hostAddresses.addresses = new HostAddress[addresses.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            for (int i = 0; i < addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                new_hostAddresses.addresses[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        (HostAddress)addresses[i].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return new_hostAddresses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public boolean inList(HostAddress addr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            for (int i = 0; i < addresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (addresses[i].equals(addr))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (hashCode == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                for (int i=0; i < addresses.length; i++)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    result = 37*result + addresses[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            hashCode = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return hashCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
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
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (!(obj instanceof HostAddresses)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        HostAddresses addrs = (HostAddresses)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if ((addresses == null && addrs.addresses != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            (addresses != null && addrs.addresses == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (addresses != null && addrs.addresses != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if (addresses.length != addrs.addresses.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            for (int i = 0; i < addresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (!addresses[i].equals(addrs.addresses[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    * Constructs a new <code>HostAddresses</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    * @exception IOException if an I/O error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public HostAddresses(DerValue encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        throws  Asn1Exception, IOException {
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 5506
diff changeset
   180
        Vector<HostAddress> tempAddresses = new Vector<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        DerValue der = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        while (encoding.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            tempAddresses.addElement(new HostAddress(der));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (tempAddresses.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            addresses = new HostAddress[tempAddresses.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            tempAddresses.copyInto(addresses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
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
    * Encodes a <code>HostAddresses</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    * @return byte array of encoded <code>HostAddresses</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    * @exception IOException if an I/O error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (addresses != null && addresses.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            for (int i = 0; i < addresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                bytes.write(addresses[i].asn1Encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Parse (unmarshal) a <code>HostAddresses</code> from a DER input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @exception Asn1Exception if an Asn1Exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param data the Der input stream value, which contains one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param optional indicates if this data field is optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @return an instance of <code>HostAddresses</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public static HostAddresses parse(DerInputStream data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                      byte explicitTag, boolean optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if ((optional) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            (((byte)data.peekByte() & (byte)0x1F) != explicitTag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return new HostAddresses(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
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
         * Writes data field values in <code>HostAddresses</code> in FCC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         * format to a <code>CCacheOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * @param cos a <code>CCacheOutputStream</code> to be written to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * @exception IOException if an I/O exception occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * @see sun.security.krb5.internal.ccache.CCacheOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    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
   251
        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
   252
            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
   253
            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
   254
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        cos.write32(addresses.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        for (int i = 0; i < addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            cos.write16(addresses[i].addrType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            cos.write32(addresses[i].address.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            cos.write(addresses[i].address, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                      addresses[i].address.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
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
    public InetAddress[] getInetAddresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (addresses == null || addresses.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 5506
diff changeset
   270
        ArrayList<InetAddress> ipAddrs = new ArrayList<>(addresses.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        for (int i = 0; i < addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                if ((addresses[i].addrType == Krb5.ADDRTYPE_INET) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    (addresses[i].addrType == Krb5.ADDRTYPE_INET6)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    ipAddrs.add(addresses[i].getInetAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            } catch (java.net.UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                // Should not happen since IP address given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        InetAddress[] retVal = new InetAddress[ipAddrs.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return ipAddrs.toArray(retVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
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
     * Returns all the IP addresses of the local host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public static HostAddresses getLocalAddresses() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    {
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   294
        Set<InetAddress> all = new LinkedHashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (DEBUG) {
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   297
                System.out.println(">>> KrbKdcReq local addresses are:");
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   298
            }
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   299
            String extra = Config.getInstance().getAll(
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   300
                    "libdefaults", "extra_addresses");
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   301
            if (extra != null) {
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   302
                for (String s: extra.split("\\s+")) {
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   303
                    all.add(InetAddress.getByName(s));
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   304
                    if (DEBUG) {
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   305
                        System.out.println("   extra_addresses: "
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   306
                                + InetAddress.getByName(s));
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   307
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   310
            for (NetworkInterface ni:
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   311
                    Collections.list(NetworkInterface.getNetworkInterfaces())) {
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   312
                if (DEBUG) {
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   313
                    System.out.println("   NetworkInterface " + ni + ":");
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   314
                    System.out.println("      "
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   315
                            + Collections.list(ni.getInetAddresses()));
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   316
                }
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   317
                all.addAll(Collections.list(ni.getInetAddresses()));
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   318
            }
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 27946
diff changeset
   319
            return new HostAddresses(all.toArray(new InetAddress[all.size()]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            throw new IOException(exc.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
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
     * Creates a new HostAddresses instance from the supplied list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * of InetAddresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public HostAddresses(InetAddress[] inetAddresses)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (inetAddresses == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                addresses = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        addresses = new HostAddress[inetAddresses.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        for (int i = 0; i < inetAddresses.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            addresses[i] = new HostAddress(inetAddresses[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
32013
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   341
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   342
    @Override
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   343
    public String toString() {
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   344
        return Arrays.toString(addresses);
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 32003
diff changeset
   345
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}