J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.security.jgss.spnego; |
| 27 | |
| 28 | import org.ietf.jgss.*; |
| 29 | import sun.security.jgss.*; |
| 30 | import sun.security.jgss.spi.*; |
| 31 | import sun.security.jgss.krb5.Krb5MechFactory; |
| 32 | import sun.security.jgss.krb5.Krb5InitCredential; |
| 33 | import sun.security.jgss.krb5.Krb5AcceptCredential; |
| 34 | import sun.security.jgss.krb5.Krb5NameElement; |
| 35 | import java.security.Provider; |
| 36 | import java.util.Vector; |
| 37 | |
| 38 | /** |
| 39 | * SpNego Mechanism plug in for JGSS |
| 40 | * This is the properties object required by the JGSS framework. |
| 41 | * All mechanism specific information is defined here. |
| 42 | * |
| 43 | * @author Seema Malkani |
| 44 | * @since 1.6 |
| 45 | */ |
| 46 | |
| 47 | public final class SpNegoMechFactory implements MechanismFactory { |
| 48 | |
| 49 | static final Provider PROVIDER = |
| 50 | new sun.security.jgss.SunProvider(); |
| 51 | |
| 52 | static final Oid GSS_SPNEGO_MECH_OID = |
| 53 | GSSUtil.createOid("1.3.6.1.5.5.2"); |
| 54 | |
| 55 | private static Oid[] nameTypes = |
| 56 | new Oid[] { GSSName.NT_USER_NAME, |
| 57 | GSSName.NT_HOSTBASED_SERVICE, |
| 58 | GSSName.NT_EXPORT_NAME}; |
| 59 | |
| 60 | // Use an instance of a GSSManager whose provider list |
| 61 | // does not include native provider |
| 62 | final GSSManagerImpl manager; |
| 63 | final Oid[] availableMechs; |
| 64 | |
| 65 | private static SpNegoCredElement getCredFromSubject(GSSNameSpi name, |
| 66 | boolean initiate) |
| 67 | throws GSSException { |
| 68 | Vector<SpNegoCredElement> creds = |
| 69 | GSSUtil.searchSubject(name, GSS_SPNEGO_MECH_OID, |
| 70 | initiate, SpNegoCredElement.class); |
| 71 | |
| 72 | SpNegoCredElement result = ((creds == null || creds.isEmpty()) ? |
| 73 | null : creds.firstElement()); |
| 74 | |
| 75 | // Force permission check before returning the cred to caller |
| 76 | if (result != null) { |
| 77 | GSSCredentialSpi cred = result.getInternalCred(); |
| 78 | if (GSSUtil.isKerberosMech(cred.getMechanism())) { |
| 79 | if (initiate) { |
| 80 | Krb5InitCredential krbCred = (Krb5InitCredential) cred; |
| 81 | Krb5MechFactory.checkInitCredPermission |
| 82 | ((Krb5NameElement) krbCred.getName()); |
| 83 | } else { |
| 84 | Krb5AcceptCredential krbCred = (Krb5AcceptCredential) cred; |
| 85 | Krb5MechFactory.checkAcceptCredPermission |
| 86 | ((Krb5NameElement) krbCred.getName(), name); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | public SpNegoMechFactory(int caller) { |
| 94 | manager = new GSSManagerImpl(caller, false); |
| 95 | Oid[] mechs = manager.getMechs(); |
| 96 | availableMechs = new Oid[mechs.length-1]; |
| 97 | for (int i = 0, j = 0; i < mechs.length; i++) { |
| 98 | // Skip SpNego mechanism |
| 99 | if (!mechs[i].equals(GSS_SPNEGO_MECH_OID)) { |
| 100 | availableMechs[j++] = mechs[i]; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | public GSSNameSpi getNameElement(String nameStr, Oid nameType) |
| 106 | throws GSSException { |
| 107 | // get NameElement for the default Mechanism |
| 108 | return manager.getNameElement(nameStr, nameType, null); |
| 109 | } |
| 110 | |
| 111 | public GSSNameSpi getNameElement(byte[] name, Oid nameType) |
| 112 | throws GSSException { |
| 113 | // get NameElement for the default Mechanism |
| 114 | return manager.getNameElement(name, nameType, null); |
| 115 | } |
| 116 | |
| 117 | public GSSCredentialSpi getCredentialElement(GSSNameSpi name, |
| 118 | int initLifetime, int acceptLifetime, |
| 119 | int usage) throws GSSException { |
| 120 | |
| 121 | SpNegoCredElement credElement = getCredFromSubject |
| 122 | (name, (usage != GSSCredential.ACCEPT_ONLY)); |
| 123 | |
| 124 | if (credElement == null) { |
| 125 | // get CredElement for the default Mechanism |
| 126 | credElement = new SpNegoCredElement |
| 127 | (manager.getCredentialElement(name, initLifetime, |
| 128 | acceptLifetime, null, usage)); |
| 129 | } |
| 130 | return credElement; |
| 131 | } |
| 132 | |
| 133 | public GSSContextSpi getMechanismContext(GSSNameSpi peer, |
| 134 | GSSCredentialSpi myInitiatorCred, int lifetime) |
| 135 | throws GSSException { |
| 136 | // get SpNego mechanism context |
| 137 | if (myInitiatorCred == null) { |
| 138 | myInitiatorCred = getCredFromSubject(null, true); |
| 139 | } else if (!(myInitiatorCred instanceof SpNegoCredElement)) { |
| 140 | // convert to SpNegoCredElement |
| 141 | SpNegoCredElement cred = new SpNegoCredElement(myInitiatorCred); |
| 142 | return new SpNegoContext(this, peer, cred, lifetime); |
| 143 | } |
| 144 | return new SpNegoContext(this, peer, myInitiatorCred, lifetime); |
| 145 | } |
| 146 | |
| 147 | public GSSContextSpi getMechanismContext(GSSCredentialSpi myAcceptorCred) |
| 148 | throws GSSException { |
| 149 | // get SpNego mechanism context |
| 150 | if (myAcceptorCred == null) { |
| 151 | myAcceptorCred = getCredFromSubject(null, false); |
| 152 | } else if (!(myAcceptorCred instanceof SpNegoCredElement)) { |
| 153 | // convert to SpNegoCredElement |
| 154 | SpNegoCredElement cred = new SpNegoCredElement(myAcceptorCred); |
| 155 | return new SpNegoContext(this, cred); |
| 156 | } |
| 157 | return new SpNegoContext(this, myAcceptorCred); |
| 158 | } |
| 159 | |
| 160 | public GSSContextSpi getMechanismContext(byte[] exportedContext) |
| 161 | throws GSSException { |
| 162 | // get SpNego mechanism context |
| 163 | return new SpNegoContext(this, exportedContext); |
| 164 | } |
| 165 | |
| 166 | public final Oid getMechanismOid() { |
| 167 | return GSS_SPNEGO_MECH_OID; |
| 168 | } |
| 169 | |
| 170 | public Provider getProvider() { |
| 171 | return PROVIDER; |
| 172 | } |
| 173 | |
| 174 | public Oid[] getNameTypes() { |
| 175 | // nameTypes is cloned in GSSManager.getNamesForMech |
| 176 | return nameTypes; |
| 177 | } |
| 178 | } |