001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.security.smartcardio;
027:
028: import java.io.File;
029: import java.io.IOException;
030:
031: import java.security.AccessController;
032: import java.security.PrivilegedAction;
033:
034: import sun.security.util.Debug;
035:
036: /**
037: * Platform specific code and functions for Unix / MUSCLE based PC/SC
038: * implementations.
039: *
040: * @since 1.6
041: * @version 1.9, 05/05/07
042: * @author Andreas Sterbenz
043: */
044: class PlatformPCSC {
045:
046: static final Debug debug = Debug.getInstance("pcsc");
047:
048: static final Throwable initException;
049:
050: private final static String PROP_NAME = "sun.security.smartcardio.library";
051:
052: private final static String LIB1 = "/usr/$LIBISA/libpcsclite.so";
053: private final static String LIB2 = "/usr/local/$LIBISA/libpcsclite.so";
054:
055: PlatformPCSC() {
056: // empty
057: }
058:
059: static {
060: initException = AccessController
061: .doPrivileged(new PrivilegedAction<Throwable>() {
062: public Throwable run() {
063: try {
064: System.loadLibrary("j2pcsc");
065: String library = getLibraryName();
066: if (debug != null) {
067: debug.println("Using PC/SC library: "
068: + library);
069: }
070: initialize(library);
071: return null;
072: } catch (Throwable e) {
073: return e;
074: }
075: }
076: });
077: }
078:
079: // expand $LIBISA to the system specific directory name for libraries
080: private static String expand(String lib) {
081: int k = lib.indexOf("$LIBISA");
082: if (k == -1) {
083: return lib;
084: }
085: String s1 = lib.substring(0, k);
086: String s2 = lib.substring(k + 7);
087: String libDir;
088: if ("64".equals(System.getProperty("sun.arch.data.model"))) {
089: if ("SunOS".equals(System.getProperty("os.name"))) {
090: libDir = "lib/64";
091: } else {
092: // assume Linux convention
093: libDir = "lib64";
094: }
095: } else {
096: // must be 32-bit
097: libDir = "lib";
098: }
099: String s = s1 + libDir + s2;
100: return s;
101: }
102:
103: private static String getLibraryName() throws IOException {
104: // if system property is set, use that library
105: String lib = expand(System.getProperty(PROP_NAME, "").trim());
106: if (lib.length() != 0) {
107: return lib;
108: }
109: lib = expand(LIB1);
110: if (new File(lib).isFile()) {
111: // if LIB1 exists, use that
112: return lib;
113: }
114: lib = expand(LIB2);
115: if (new File(lib).isFile()) {
116: // if LIB2 exists, use that
117: return lib;
118: }
119: throw new IOException("No PC/SC library found on this system");
120: }
121:
122: private static native void initialize(String libraryName);
123:
124: // PCSC constants defined differently under Windows and MUSCLE
125: // MUSCLE version
126: final static int SCARD_PROTOCOL_T0 = 0x0001;
127: final static int SCARD_PROTOCOL_T1 = 0x0002;
128: final static int SCARD_PROTOCOL_RAW = 0x0004;
129:
130: final static int SCARD_UNKNOWN = 0x0001;
131: final static int SCARD_ABSENT = 0x0002;
132: final static int SCARD_PRESENT = 0x0004;
133: final static int SCARD_SWALLOWED = 0x0008;
134: final static int SCARD_POWERED = 0x0010;
135: final static int SCARD_NEGOTIABLE = 0x0020;
136: final static int SCARD_SPECIFIC = 0x0040;
137:
138: }
|