Path: blob/master/test/jdk/sun/security/krb5/ServiceCredsCombination.java
41152 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* @modules java.security.jgss/sun.security.jgss26* @compile -XDignore.symbol.file ServiceCredsCombination.java27* @run main ServiceCredsCombination28* @summary default principal can act as anyone29*/3031import java.security.PrivilegedActionException;32import java.security.PrivilegedExceptionAction;33import java.util.Objects;34import javax.security.auth.Subject;35import javax.security.auth.kerberos.KerberosKey;36import javax.security.auth.kerberos.KerberosPrincipal;37import javax.security.auth.kerberos.KeyTab;38import org.ietf.jgss.GSSCredential;39import org.ietf.jgss.GSSException;40import org.ietf.jgss.GSSManager;41import org.ietf.jgss.GSSName;42import sun.security.jgss.GSSUtil;4344public class ServiceCredsCombination {4546public static void main(String[] args) throws Exception {47// pass48check("a", "a", princ("a"), key("a"));49check(null, "a", princ("a"), key("a"));50check("x", "NOCRED", princ("a"), key("a"));51// two pass52check("a", "a", princ("a"), key("a"), princ("b"), key("b"));53check("b", "b", princ("a"), key("a"), princ("b"), key("b"));54check(null, null, princ("a"), key("a"), princ("b"), key("b"));55check("x", "NOCRED", princ("a"), key("a"), princ("b"), key("b"));56// old ktab57check("b", "b", princ("b"), oldktab());58check("x", "NOCRED", princ("b"), oldktab());59check(null, "b", princ("b"), oldktab());60// Two old ktab61check("a", "a", princ("a"), princ("b"), oldktab(), oldktab());62check("b", "b", princ("a"), princ("b"), oldktab(), oldktab());63check(null, null, princ("a"), princ("b"), oldktab(), oldktab());64check("x", "NOCRED", princ("a"), princ("b"), oldktab(), oldktab());65// bound ktab66check("c", "c", princ("c"), ktab("c"));67check(null, "c", princ("c"), ktab("c"));68// unbound ktab69check("x", "x", ktab());70check(null, null, ktab());71// Two bound ktab72check("c1", "c1", princ("c1"), princ("c2"), ktab("c1"), ktab("c2"));73check("c2", "c2", princ("c1"), princ("c2"), ktab("c1"), ktab("c2"));74check("x", "NOCRED", princ("c1"), princ("c2"), ktab("c1"), ktab("c2"));75check(null, null, princ("c1"), princ("c2"), ktab("c1"), ktab("c2"));76// One bound, one unbound77check("c1", "c1", princ("c1"), ktab("c1"), ktab());78check("x", "x", princ("c1"), ktab("c1"), ktab());79check(null, null, princ("c1"), ktab("c1"), ktab());80// Two unbound ktab81check("x", "x", ktab(), ktab());82check(null, null, ktab(), ktab());83// pass + old ktab84check("a", "a", princ("a"), princ("b"), key("a"), oldktab());85check("b", "b", princ("a"), princ("b"), key("a"), oldktab());86check(null, null, princ("a"), princ("b"), key("a"), oldktab());87check("x", "NOCRED", princ("a"), princ("b"), key("a"), oldktab());88// pass + bound ktab89check("a", "a", princ("a"), princ("c"), key("a"), ktab("c"));90check("c", "c", princ("a"), princ("c"), key("a"), ktab("c"));91check("x", "NOCRED", princ("a"), princ("c"), key("a"), ktab("c"));92check(null, null, princ("a"), princ("c"), key("a"), ktab("c"));93// pass + unbound ktab94check("a", "a", princ("a"), key("a"), ktab());95check("x", "x", princ("a"), key("a"), ktab());96check(null, null, princ("a"), key("a"), ktab());97// Compatibility, automatically add princ for keys98check(null, "a", key("a"));99check("x", "NOCRED", key("a"));100check(null, "a", key("a"), oldktab());101check("x", "NOCRED", key("a"), oldktab());102// Limitation, "a" has no key, but we don't know oldktab() is for "b"103check("a", "a", princ("a"), princ("b"), oldktab());104}105106/**107* Checks the correct bound108* @param a get a creds for this principal, null for default one109* @param b expected name, null for still unbound, "NOCRED" for no creds110* @param objs princs, keys and keytabs in the subject111*/112private static void check(final String a, String b, Object... objs)113throws Exception {114Subject subj = new Subject();115for (Object obj: objs) {116if (obj instanceof KerberosPrincipal) {117subj.getPrincipals().add((KerberosPrincipal)obj);118} else if (obj instanceof KerberosKey || obj instanceof KeyTab) {119subj.getPrivateCredentials().add(obj);120}121}122final GSSManager man = GSSManager.getInstance();123try {124String result = Subject.doAs(125subj, new PrivilegedExceptionAction<String>() {126@Override127public String run() throws GSSException {128GSSCredential cred = man.createCredential(129a == null ? null : man.createName(r(a), null),130GSSCredential.INDEFINITE_LIFETIME,131GSSUtil.GSS_KRB5_MECH_OID,132GSSCredential.ACCEPT_ONLY);133GSSName name = cred.getName();134return name == null ? null : name.toString();135}136});137if (!Objects.equals(result, r(b))) {138throw new Exception("Check failed: getInstance(" + a139+ ") has name " + result + ", not " + b);140}141} catch (PrivilegedActionException e) {142if (!"NOCRED".equals(b)) {143throw new Exception("Check failed: getInstance(" + a144+ ") is null " + ", but not one with name " + b);145}146}147}148private static String r(String s) {149return s == null ? null : (s+"@REALM");150}151private static KerberosPrincipal princ(String s) {152return new KerberosPrincipal(r(s));153}154private static KerberosKey key(String s) {155return new KerberosKey(princ(s), new byte[0], 0, 0);156}157private static KeyTab oldktab() {158return KeyTab.getInstance();159}160static KeyTab ktab(String s) {161return KeyTab.getInstance(princ(s));162}163static KeyTab ktab() {164return KeyTab.getUnboundInstance();165}166}167168169