1 /*
   2  * Copyright (c) 2005, 2012, 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             if ("SunOS".equals(System.getProperty("os.name"))) {
  88                 libDir = "lib/64";
  89             } else {
  90                 // assume Linux convention
  91                 libDir = "lib64";
  92             }
  93         } else {
  94             // must be 32-bit
  95             libDir = "lib";
  96         }
  97         String s = s1 + libDir + s2;
  98         return s;
  99     }
 100 
 101     private static String getLibraryName() throws IOException {
 102         // if system property is set, use that library
 103         String lib = expand(System.getProperty(PROP_NAME, "").trim());
 104         if (lib.length() != 0) {
 105             return lib;
 106         }
 107         lib = expand(LIB1);
 108         if (new File(lib).isFile()) {
 109             // if LIB1 exists, use that
 110             return lib;
 111         }
 112         lib = expand(LIB2);
 113         if (new File(lib).isFile()) {
 114             // if LIB2 exists, use that
 115             return lib;
 116         }
 117         lib = PCSC_FRAMEWORK;
 118         if (new File(lib).isFile()) {
 119             // if PCSC.framework exists, use that
 120             return lib;
 121         }
 122         throw new IOException("No PC/SC library found on this system");
 123     }
 124 
 125     private static native void initialize(String libraryName);
 126 
 127     // PCSC constants defined differently under Windows and MUSCLE
 128     // MUSCLE version
 129     final static int SCARD_PROTOCOL_T0     =  0x0001;
 130     final static int SCARD_PROTOCOL_T1     =  0x0002;
 131     final static int SCARD_PROTOCOL_RAW    =  0x0004;
 132 
 133     final static int SCARD_UNKNOWN         =  0x0001;
 134     final static int SCARD_ABSENT          =  0x0002;
 135     final static int SCARD_PRESENT         =  0x0004;
 136     final static int SCARD_SWALLOWED       =  0x0008;
 137     final static int SCARD_POWERED         =  0x0010;
 138     final static int SCARD_NEGOTIABLE      =  0x0020;
 139     final static int SCARD_SPECIFIC        =  0x0040;
 140 
 141 }