1 /* 2 * Copyright (c) 2005, 2020, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.security.smartcardio; 27 28 import java.io.File; 29 import java.io.IOException; 30 31 import java.security.AccessController; 32 import java.security.PrivilegedAction; 33 34 import sun.security.util.Debug; 35 36 /** 37 * Platform specific code and functions for Unix / MUSCLE based PC/SC 38 * implementations. 39 * 40 * @since 1.6 41 * @author Andreas Sterbenz 42 */ 43 class PlatformPCSC { 44 45 static final Debug debug = Debug.getInstance("pcsc"); 46 47 static final Throwable initException; 48 49 private final static String PROP_NAME = "sun.security.smartcardio.library"; 50 51 private final static String LIB1 = "/usr/$LIBISA/libpcsclite.so"; 52 private final static String LIB2 = "/usr/local/$LIBISA/libpcsclite.so"; 53 private final static String PCSC_FRAMEWORK = "/System/Library/Frameworks/PCSC.framework/Versions/Current/PCSC"; 54 55 PlatformPCSC() { 56 // empty 57 } 58 59 static { 60 initException = AccessController.doPrivileged(new PrivilegedAction<Throwable>() { 61 public Throwable run() { 62 try { 63 System.loadLibrary("j2pcsc"); 64 String library = getLibraryName(); 65 if (debug != null) { 66 debug.println("Using PC/SC library: " + library); 67 } 68 initialize(library); 69 return null; 70 } catch (Throwable e) { 71 return e; 72 } 73 } 74 }); 75 } 76 77 // expand $LIBISA to the system specific directory name for libraries 78 private static String expand(String lib) { 79 int k = lib.indexOf("$LIBISA"); 80 if (k == -1) { 81 return lib; 82 } 83 String s1 = lib.substring(0, k); 84 String s2 = lib.substring(k + 7); 85 String libDir; 86 if ("64".equals(System.getProperty("sun.arch.data.model"))) { 87 // assume Linux convention 88 libDir = "lib64"; 89 } else { 90 // must be 32-bit 91 libDir = "lib"; 92 } 93 String s = s1 + libDir + s2; 94 return s; 95 } 96 97 private static String getLibraryName() throws IOException { 98 // if system property is set, use that library 99 String lib = expand(System.getProperty(PROP_NAME, "").trim()); 100 if (lib.length() != 0) { 101 return lib; 102 } 103 lib = expand(LIB1); 104 if (new File(lib).isFile()) { 105 // if LIB1 exists, use that 106 return lib; 107 } 108 lib = expand(LIB2); 109 if (new File(lib).isFile()) { 110 // if LIB2 exists, use that 111 return lib; 112 } 113 lib = PCSC_FRAMEWORK; 114 if (new File(lib).isFile()) { 115 // if PCSC.framework exists, use that 116 return lib; 117 } 118 throw new IOException("No PC/SC library found on this system"); 119 } 120 121 private static native void initialize(String libraryName); 122 123 // PCSC constants defined differently under Windows and MUSCLE 124 // MUSCLE version 125 final static int SCARD_PROTOCOL_T0 = 0x0001; 126 final static int SCARD_PROTOCOL_T1 = 0x0002; 127 final static int SCARD_PROTOCOL_RAW = 0x0004; 128 129 final static int SCARD_UNKNOWN = 0x0001; 130 final static int SCARD_ABSENT = 0x0002; 131 final static int SCARD_PRESENT = 0x0004; 132 final static int SCARD_SWALLOWED = 0x0008; 133 final static int SCARD_POWERED = 0x0010; 134 final static int SCARD_NEGOTIABLE = 0x0020; 135 final static int SCARD_SPECIFIC = 0x0040; 136 137 }