1 /*
   2  * Copyright (c) 2005, 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 
  54     PlatformPCSC() {
  55         // empty
  56     }
  57 
  58     static {
  59         initException = AccessController.doPrivileged(new PrivilegedAction<Throwable>() {
  60             public Throwable run() {
  61                 try {
  62                     System.loadLibrary("j2pcsc");
  63                     String library = getLibraryName();
  64                     if (debug != null) {
  65                         debug.println("Using PC/SC library: " + library);
  66                     }
  67                     initialize(library);
  68                     return null;
  69                 } catch (Throwable e) {
  70                     return e;
  71                 }
  72             }
  73         });
  74     }
  75 
  76     // expand $LIBISA to the system specific directory name for libraries
  77     private static String expand(String lib) {
  78         int k = lib.indexOf("$LIBISA");
  79         if (k == -1) {
  80             return lib;
  81         }
  82         String s1 = lib.substring(0, k);
  83         String s2 = lib.substring(k + 7);
  84         String libDir;
  85         if ("64".equals(System.getProperty("sun.arch.data.model"))) {
  86             if ("SunOS".equals(System.getProperty("os.name"))) {
  87                 libDir = "lib/64";
  88             } else {
  89                 // assume Linux convention
  90                 libDir = "lib64";
  91             }
  92         } else {
  93             // must be 32-bit
  94             libDir = "lib";
  95         }
  96         String s = s1 + libDir + s2;
  97         return s;
  98     }
  99 
 100     private static String getLibraryName() throws IOException {
 101         // if system property is set, use that library
 102         String lib = expand(System.getProperty(PROP_NAME, "").trim());
 103         if (lib.length() != 0) {
 104             return lib;
 105         }
 106         lib = expand(LIB1);
 107         if (new File(lib).isFile()) {
 108             // if LIB1 exists, use that
 109             return lib;
 110         }
 111         lib = expand(LIB2);
 112         if (new File(lib).isFile()) {
 113             // if LIB2 exists, use that
 114             return lib;
 115         }
 116         throw new IOException("No PC/SC library found on this system");
 117     }
 118 
 119     private static native void initialize(String libraryName);
 120 
 121     // PCSC constants defined differently under Windows and MUSCLE
 122     // MUSCLE version
 123     final static int SCARD_PROTOCOL_T0     =  0x0001;
 124     final static int SCARD_PROTOCOL_T1     =  0x0002;
 125     final static int SCARD_PROTOCOL_RAW    =  0x0004;
 126 
 127     final static int SCARD_UNKNOWN         =  0x0001;
 128     final static int SCARD_ABSENT          =  0x0002;
 129     final static int SCARD_PRESENT         =  0x0004;
 130     final static int SCARD_SWALLOWED       =  0x0008;
 131     final static int SCARD_POWERED         =  0x0010;
 132     final static int SCARD_NEGOTIABLE      =  0x0020;
 133     final static int SCARD_SPECIFIC        =  0x0040;
 134 
 135 }
--- EOF ---