Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/krb5/ServiceCredsCombination.java
38838 views
/*1* Copyright (c) 2012, 2013, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 800544725* @compile -XDignore.symbol.file ServiceCredsCombination.java26* @run main ServiceCredsCombination27* @summary default principal can act as anyone28*/2930import java.security.PrivilegedActionException;31import java.security.PrivilegedExceptionAction;32import java.util.Objects;33import javax.security.auth.Subject;34import javax.security.auth.kerberos.KerberosKey;35import javax.security.auth.kerberos.KerberosPrincipal;36import javax.security.auth.kerberos.KeyTab;37import org.ietf.jgss.GSSCredential;38import org.ietf.jgss.GSSException;39import org.ietf.jgss.GSSManager;40import org.ietf.jgss.GSSName;41import sun.security.jgss.GSSUtil;4243public class ServiceCredsCombination {4445public static void main(String[] args) throws Exception {46// pass47check("a", "a", princ("a"), key("a"));48check(null, "a", princ("a"), key("a"));49check("x", "NOCRED", princ("a"), key("a"));50// two pass51check("a", "a", princ("a"), key("a"), princ("b"), key("b"));52check("b", "b", princ("a"), key("a"), princ("b"), key("b"));53check(null, null, princ("a"), key("a"), princ("b"), key("b"));54check("x", "NOCRED", princ("a"), key("a"), princ("b"), key("b"));55// old ktab56check("b", "b", princ("b"), oldktab());57check("x", "NOCRED", princ("b"), oldktab());58check(null, "b", princ("b"), oldktab());59// Two old ktab60check("a", "a", princ("a"), princ("b"), oldktab(), oldktab());61check("b", "b", princ("a"), princ("b"), oldktab(), oldktab());62check(null, null, princ("a"), princ("b"), oldktab(), oldktab());63check("x", "NOCRED", princ("a"), princ("b"), oldktab(), oldktab());64// bound ktab65check("c", "c", princ("c"), ktab("c"));66check(null, "c", princ("c"), ktab("c"));67// unbound ktab68check("x", "x", ktab());69check(null, null, ktab());70// Two bound ktab71check("c1", "c1", princ("c1"), princ("c2"), ktab("c1"), ktab("c2"));72check("c2", "c2", princ("c1"), princ("c2"), ktab("c1"), ktab("c2"));73check("x", "NOCRED", princ("c1"), princ("c2"), ktab("c1"), ktab("c2"));74check(null, null, princ("c1"), princ("c2"), ktab("c1"), ktab("c2"));75// One bound, one unbound76check("c1", "c1", princ("c1"), ktab("c1"), ktab());77check("x", "x", princ("c1"), ktab("c1"), ktab());78check(null, null, princ("c1"), ktab("c1"), ktab());79// Two unbound ktab80check("x", "x", ktab(), ktab());81check(null, null, ktab(), ktab());82// pass + old ktab83check("a", "a", princ("a"), princ("b"), key("a"), oldktab());84check("b", "b", princ("a"), princ("b"), key("a"), oldktab());85check(null, null, princ("a"), princ("b"), key("a"), oldktab());86check("x", "NOCRED", princ("a"), princ("b"), key("a"), oldktab());87// pass + bound ktab88check("a", "a", princ("a"), princ("c"), key("a"), ktab("c"));89check("c", "c", princ("a"), princ("c"), key("a"), ktab("c"));90check("x", "NOCRED", princ("a"), princ("c"), key("a"), ktab("c"));91check(null, null, princ("a"), princ("c"), key("a"), ktab("c"));92// pass + unbound ktab93check("a", "a", princ("a"), key("a"), ktab());94check("x", "x", princ("a"), key("a"), ktab());95check(null, null, princ("a"), key("a"), ktab());96// Compatibility, automatically add princ for keys97check(null, "a", key("a"));98check("x", "NOCRED", key("a"));99check(null, "a", key("a"), oldktab());100check("x", "NOCRED", key("a"), oldktab());101// Limitation, "a" has no key, but we don't know oldktab() is for "b"102check("a", "a", princ("a"), princ("b"), oldktab());103}104105/**106* Checks the correct bound107* @param a get a creds for this principal, null for default one108* @param b expected name, null for still unbound, "NOCRED" for no creds109* @param objs princs, keys and keytabs in the subject110*/111private static void check(final String a, String b, Object... objs)112throws Exception {113Subject subj = new Subject();114for (Object obj: objs) {115if (obj instanceof KerberosPrincipal) {116subj.getPrincipals().add((KerberosPrincipal)obj);117} else if (obj instanceof KerberosKey || obj instanceof KeyTab) {118subj.getPrivateCredentials().add(obj);119}120}121final GSSManager man = GSSManager.getInstance();122try {123String result = Subject.doAs(124subj, new PrivilegedExceptionAction<String>() {125@Override126public String run() throws GSSException {127GSSCredential cred = man.createCredential(128a == null ? null : man.createName(r(a), null),129GSSCredential.INDEFINITE_LIFETIME,130GSSUtil.GSS_KRB5_MECH_OID,131GSSCredential.ACCEPT_ONLY);132GSSName name = cred.getName();133return name == null ? null : name.toString();134}135});136if (!Objects.equals(result, r(b))) {137throw new Exception("Check failed: getInstance(" + a138+ ") has name " + result + ", not " + b);139}140} catch (PrivilegedActionException e) {141if (!"NOCRED".equals(b)) {142throw new Exception("Check failed: getInstance(" + a143+ ") is null " + ", but not one with name " + b);144}145}146}147private static String r(String s) {148return s == null ? null : (s+"@REALM");149}150private static KerberosPrincipal princ(String s) {151return new KerberosPrincipal(r(s));152}153private static KerberosKey key(String s) {154return new KerberosKey(princ(s), new byte[0], 0, 0);155}156private static KeyTab oldktab() {157return KeyTab.getInstance();158}159static KeyTab ktab(String s) {160return KeyTab.getInstance(princ(s));161}162static KeyTab ktab() {163return KeyTab.getUnboundInstance();164}165}166167168