Path: blob/master/src/java.security.jgss/share/classes/sun/security/krb5/internal/HostAddresses.java
41161 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*27* (C) Copyright IBM Corp. 1999 All Rights Reserved.28* Copyright 1997 The Open Group Research Institute. All rights reserved.29*/3031package sun.security.krb5.internal;3233import sun.security.krb5.Config;34import sun.security.krb5.PrincipalName;35import sun.security.krb5.KrbException;36import sun.security.krb5.Asn1Exception;37import sun.security.util.*;3839import java.net.*;40import java.util.*;41import java.io.IOException;42import sun.security.krb5.internal.ccache.CCacheOutputStream;4344/**45* Implements the ASN.1 HostAddresses type.46*47* <pre>{@code48* HostAddresses -- NOTE: subtly different from rfc1510,49* -- but has a value mapping and encodes the same50* ::= SEQUENCE OF HostAddress51*52* HostAddress ::= SEQUENCE {53* addr-type [0] Int32,54* address [1] OCTET STRING55* }56* }</pre>57*58* <p>59* This definition reflects the Network Working Group RFC 412060* specification available at61* <a href="http://www.ietf.org/rfc/rfc4120.txt">62* http://www.ietf.org/rfc/rfc4120.txt</a>.63*/6465public class HostAddresses implements Cloneable {66private static boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;67private HostAddress[] addresses = null;68private volatile int hashCode = 0;6970// Warning: called by nativeccache.c71public HostAddresses(HostAddress[] new_addresses) throws IOException {72if (new_addresses != null) {73addresses = new HostAddress[new_addresses.length];74for (int i = 0; i < new_addresses.length; i++) {75if (new_addresses[i] == null) {76throw new IOException("Cannot create a HostAddress");77} else {78addresses[i] = (HostAddress)new_addresses[i].clone();79}80}81}82}8384public HostAddresses() throws UnknownHostException {85addresses = new HostAddress[1];86addresses[0] = new HostAddress();87}8889private HostAddresses(int dummy) {}9091public HostAddresses(PrincipalName serverPrincipal)92throws UnknownHostException, KrbException {9394String[] components = serverPrincipal.getNameStrings();9596if (serverPrincipal.getNameType() != PrincipalName.KRB_NT_SRV_HST ||97components.length < 2)98throw new KrbException(Krb5.KRB_ERR_GENERIC, "Bad name");99100String host = components[1];101InetAddress[] addr = InetAddress.getAllByName(host);102HostAddress[] hAddrs = new HostAddress[addr.length];103104for (int i = 0; i < addr.length; i++) {105hAddrs[i] = new HostAddress(addr[i]);106}107108addresses = hAddrs;109}110111public Object clone() {112HostAddresses new_hostAddresses = new HostAddresses(0);113if (addresses != null) {114new_hostAddresses.addresses = new HostAddress[addresses.length];115for (int i = 0; i < addresses.length; i++) {116new_hostAddresses.addresses[i] =117(HostAddress)addresses[i].clone();118}119}120return new_hostAddresses;121}122123public boolean inList(HostAddress addr) {124if (addresses != null) {125for (int i = 0; i < addresses.length; i++)126if (addresses[i].equals(addr))127return true;128}129return false;130}131132public int hashCode() {133if (hashCode == 0) {134int result = 17;135if (addresses != null) {136for (int i=0; i < addresses.length; i++) {137result = 37*result + addresses[i].hashCode();138}139}140hashCode = result;141}142return hashCode;143144}145146147public boolean equals(Object obj) {148if (this == obj) {149return true;150}151152if (!(obj instanceof HostAddresses)) {153return false;154}155156HostAddresses addrs = (HostAddresses)obj;157if ((addresses == null && addrs.addresses != null) ||158(addresses != null && addrs.addresses == null))159return false;160if (addresses != null && addrs.addresses != null) {161if (addresses.length != addrs.addresses.length)162return false;163for (int i = 0; i < addresses.length; i++)164if (!addresses[i].equals(addrs.addresses[i]))165return false;166}167return true;168}169170/**171* Constructs a new <code>HostAddresses</code> object.172* @param encoding a single DER-encoded value.173* @exception Asn1Exception if an error occurs while decoding an174* ASN1 encoded data.175* @exception IOException if an I/O error occurs while reading176* encoded data.177*/178public HostAddresses(DerValue encoding)179throws Asn1Exception, IOException {180Vector<HostAddress> tempAddresses = new Vector<>();181DerValue der = null;182while (encoding.getData().available() > 0) {183der = encoding.getData().getDerValue();184tempAddresses.addElement(new HostAddress(der));185}186if (tempAddresses.size() > 0) {187addresses = new HostAddress[tempAddresses.size()];188tempAddresses.copyInto(addresses);189}190}191192193/**194* Encodes a <code>HostAddresses</code> object.195* @return byte array of encoded <code>HostAddresses</code> object.196* @exception Asn1Exception if an error occurs while decoding an197* ASN1 encoded data.198* @exception IOException if an I/O error occurs while reading199* encoded data.200*/201public byte[] asn1Encode() throws Asn1Exception, IOException {202DerOutputStream bytes = new DerOutputStream();203DerOutputStream temp = new DerOutputStream();204205if (addresses != null && addresses.length > 0) {206for (int i = 0; i < addresses.length; i++)207bytes.write(addresses[i].asn1Encode());208}209temp.write(DerValue.tag_Sequence, bytes);210return temp.toByteArray();211}212213/**214* Parse (unmarshal) a <code>HostAddresses</code> from a DER input stream.215* This form216* parsing might be used when expanding a value which is part of217* a constructed sequence and uses explicitly tagged type.218*219* @exception Asn1Exception if an Asn1Exception occurs.220* @param data the Der input stream value, which contains one or more221* marshaled value.222* @param explicitTag tag number.223* @param optional indicates if this data field is optional.224* @return an instance of <code>HostAddresses</code>.225*/226public static HostAddresses parse(DerInputStream data,227byte explicitTag, boolean optional)228throws Asn1Exception, IOException {229if ((optional) &&230(((byte)data.peekByte() & (byte)0x1F) != explicitTag))231return null;232DerValue der = data.getDerValue();233if (explicitTag != (der.getTag() & (byte)0x1F)) {234throw new Asn1Exception(Krb5.ASN1_BAD_ID);235} else {236DerValue subDer = der.getData().getDerValue();237return new HostAddresses(subDer);238}239}240241/**242* Writes data field values in <code>HostAddresses</code> in FCC243* format to a <code>CCacheOutputStream</code>.244*245* @param cos a <code>CCacheOutputStream</code> to be written to.246* @exception IOException if an I/O exception occurs.247* @see sun.security.krb5.internal.ccache.CCacheOutputStream248*/249public void writeAddrs(CCacheOutputStream cos) throws IOException {250if (addresses == null || addresses.length == 0) {251cos.write32(0);252return;253}254cos.write32(addresses.length);255for (int i = 0; i < addresses.length; i++) {256cos.write16(addresses[i].addrType);257cos.write32(addresses[i].address.length);258cos.write(addresses[i].address, 0,259addresses[i].address.length);260}261}262263264public InetAddress[] getInetAddresses() {265266if (addresses == null || addresses.length == 0)267return null;268269ArrayList<InetAddress> ipAddrs = new ArrayList<>(addresses.length);270271for (int i = 0; i < addresses.length; i++) {272try {273if ((addresses[i].addrType == Krb5.ADDRTYPE_INET) ||274(addresses[i].addrType == Krb5.ADDRTYPE_INET6)) {275ipAddrs.add(addresses[i].getInetAddress());276}277} catch (java.net.UnknownHostException e) {278// Should not happen since IP address given279return null;280}281}282283InetAddress[] retVal = new InetAddress[ipAddrs.size()];284return ipAddrs.toArray(retVal);285286}287288/**289* Returns all the IP addresses of the local host.290*/291public static HostAddresses getLocalAddresses() throws IOException292{293Set<InetAddress> all = new LinkedHashSet<>();294try {295if (DEBUG) {296System.out.println(">>> KrbKdcReq local addresses are:");297}298String extra = Config.getInstance().getAll(299"libdefaults", "extra_addresses");300if (extra != null) {301for (String s: extra.split("\\s+")) {302all.add(InetAddress.getByName(s));303if (DEBUG) {304System.out.println(" extra_addresses: "305+ InetAddress.getByName(s));306}307}308}309for (NetworkInterface ni:310Collections.list(NetworkInterface.getNetworkInterfaces())) {311if (DEBUG) {312System.out.println(" NetworkInterface " + ni + ":");313System.out.println(" "314+ Collections.list(ni.getInetAddresses()));315}316all.addAll(Collections.list(ni.getInetAddresses()));317}318return new HostAddresses(all.toArray(new InetAddress[all.size()]));319} catch (Exception exc) {320throw new IOException(exc.toString());321}322}323324/**325* Creates a new HostAddresses instance from the supplied list326* of InetAddresses.327*/328public HostAddresses(InetAddress[] inetAddresses)329{330if (inetAddresses == null)331{332addresses = null;333return;334}335336addresses = new HostAddress[inetAddresses.length];337for (int i = 0; i < inetAddresses.length; i++)338addresses[i] = new HostAddress(inetAddresses[i]);339}340341@Override342public String toString() {343return Arrays.toString(addresses);344}345}346347348