Path: blob/master/src/java.security.jgss/share/classes/sun/security/jgss/wrapper/SunNativeProvider.java
41161 views
/*1* Copyright (c) 2005, 2021, 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*/2425package sun.security.jgss.wrapper;2627import java.util.HashMap;28import java.security.Provider;29import java.security.AccessController;30import java.security.PrivilegedAction;31import org.ietf.jgss.Oid;32import sun.security.action.PutAllAction;33import static sun.security.util.SecurityConstants.PROVIDER_VER;3435/**36* Defines the Sun NativeGSS provider for plugging in the37* native GSS mechanisms to Java GSS.38*39* List of supported mechanisms depends on the local40* machine configuration.41*42* @author Yu-Ching Valerie Peng43*/4445public final class SunNativeProvider extends Provider {4647private static final long serialVersionUID = -238911724858694204L;4849private static final String NAME = "SunNativeGSS";50private static final String INFO = "Sun Native GSS provider";51private static final String MF_CLASS =52"sun.security.jgss.wrapper.NativeGSSFactory";53static boolean DEBUG;54static void debug(String message) {55if (DEBUG) {56if (message == null) {57throw new NullPointerException();58}59System.out.println(NAME + ": " + message);60}61}6263@SuppressWarnings("removal")64private static final HashMap<String, String> MECH_MAP =65AccessController.doPrivileged(66new PrivilegedAction<>() {67public HashMap<String, String> run() {68DEBUG = Boolean.parseBoolean(69System.getProperty("sun.security.nativegss.debug"));70try {71// Ensure the InetAddress class is loaded before72// loading j2gss. The library will access this class73// and a deadlock might happen. See JDK-8210373.74Class.forName("java.net.InetAddress");75System.loadLibrary("j2gss");76} catch (ClassNotFoundException | Error err) {77debug("No j2gss library found!");78if (DEBUG) err.printStackTrace();79return null;80}81String[] gssLibs;82String defaultLib83= System.getProperty("sun.security.jgss.lib");84if (defaultLib == null || defaultLib.trim().equals("")) {85String osname = System.getProperty("os.name");86if (osname.startsWith("Linux")) {87gssLibs = new String[]{88"libgssapi.so",89"libgssapi_krb5.so",90"libgssapi_krb5.so.2",91};92} else if (osname.contains("OS X")) {93gssLibs = new String[]{94"libgssapi_krb5.dylib",95"/usr/lib/sasl2/libgssapiv2.2.so",96};97} else if (osname.contains("Windows")) {98// Full path needed, DLL is in jre/bin99gssLibs = new String[]{ System.getProperty("java.home")100+ "\\bin\\sspi_bridge.dll" };101} else {102gssLibs = new String[0];103}104} else {105gssLibs = new String[]{ defaultLib };106}107for (String libName: gssLibs) {108if (GSSLibStub.init(libName, DEBUG)) {109debug("Loaded GSS library: " + libName);110Oid[] mechs = GSSLibStub.indicateMechs();111HashMap<String,String> map = new HashMap<>();112for (int i = 0; i < mechs.length; i++) {113debug("Native MF for " + mechs[i]);114map.put("GssApiMechanism." + mechs[i],115MF_CLASS);116}117return map;118}119}120return null;121}122});123124// initialize INSTANCE after MECH_MAP is constructed125static final Provider INSTANCE = new SunNativeProvider();126127@SuppressWarnings("removal")128public SunNativeProvider() {129/* We are the Sun NativeGSS provider */130super(NAME, PROVIDER_VER, INFO);131132if (MECH_MAP != null) {133AccessController.doPrivileged(new PutAllAction(this, MECH_MAP));134}135}136}137138139