Path: blob/master/src/java.smartcardio/unix/classes/sun/security/smartcardio/PlatformPCSC.java
41159 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.smartcardio;2627import java.io.File;28import java.io.IOException;2930import java.security.AccessController;31import java.security.PrivilegedAction;3233import sun.security.util.Debug;3435/**36* Platform specific code and functions for Unix / MUSCLE based PC/SC37* implementations.38*39* @since 1.640* @author Andreas Sterbenz41*/42class PlatformPCSC {4344static final Debug debug = Debug.getInstance("pcsc");4546private final static String PROP_NAME = "sun.security.smartcardio.library";4748private final static String LIB1 = "/usr/$LIBISA/libpcsclite.so";49private final static String LIB2 = "/usr/local/$LIBISA/libpcsclite.so";50private final static String PCSC_FRAMEWORK = "/System/Library/Frameworks/PCSC.framework/Versions/Current/PCSC";5152PlatformPCSC() {53// empty54}5556@SuppressWarnings("removal")57static final Throwable initException58= AccessController.doPrivileged(new PrivilegedAction<Throwable>() {59public Throwable run() {60try {61System.loadLibrary("j2pcsc");62String library = getLibraryName();63if (debug != null) {64debug.println("Using PC/SC library: " + library);65}66initialize(library);67return null;68} catch (Throwable e) {69return e;70}71}72});7374// expand $LIBISA to the system specific directory name for libraries75private static String expand(String lib) {76int k = lib.indexOf("$LIBISA");77if (k == -1) {78return lib;79}80String s1 = lib.substring(0, k);81String s2 = lib.substring(k + 7);82String libDir;83if ("64".equals(System.getProperty("sun.arch.data.model"))) {84// assume Linux convention85libDir = "lib64";86} else {87// must be 32-bit88libDir = "lib";89}90String s = s1 + libDir + s2;91return s;92}9394private static String getLibraryName() throws IOException {95// if system property is set, use that library96String lib = expand(System.getProperty(PROP_NAME, "").trim());97if (lib.length() != 0) {98return lib;99}100lib = expand(LIB1);101if (new File(lib).isFile()) {102// if LIB1 exists, use that103return lib;104}105lib = expand(LIB2);106if (new File(lib).isFile()) {107// if LIB2 exists, use that108return lib;109}110111// As of macos 11, framework libraries have been removed from the file112// system, but in such a way that they can still be dlopen()ed, even113// though they can no longer be open()ed.114//115// https://developer.apple.com/documentation/macos-release-notes/macos-big-sur-11_0_1-release-notes116//117// """New in macOS Big Sur 11.0.1, the system ships with a built-in118// dynamic linker cache of all system-provided libraries. As part of119// this change, copies of dynamic libraries are no longer present on120// the filesystem. Code that attempts to check for dynamic library121// presence by looking for a file at a path or enumerating a directory122// will fail. Instead, check for library presence by attempting to123// dlopen() the path, which will correctly check for the library in the124// cache."""125//126// The directory structure remains otherwise intact, so check for127// existence of the containing directory instead of the file.128lib = PCSC_FRAMEWORK;129if (new File(lib).getParentFile().isDirectory()) {130// if PCSC.framework exists, use that131return lib;132}133throw new IOException("No PC/SC library found on this system");134}135136private static native void initialize(String libraryName);137138// PCSC constants defined differently under Windows and MUSCLE139// MUSCLE version140final static int SCARD_PROTOCOL_T0 = 0x0001;141final static int SCARD_PROTOCOL_T1 = 0x0002;142final static int SCARD_PROTOCOL_RAW = 0x0004;143144final static int SCARD_UNKNOWN = 0x0001;145final static int SCARD_ABSENT = 0x0002;146final static int SCARD_PRESENT = 0x0004;147final static int SCARD_SWALLOWED = 0x0008;148final static int SCARD_POWERED = 0x0010;149final static int SCARD_NEGOTIABLE = 0x0020;150final static int SCARD_SPECIFIC = 0x0040;151152}153154155