1 /*
   2  * Copyright (c) 2005, 2006, 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.security.AccessController;
  29 
  30 /**
  31  * Access to native PC/SC functions and definition of PC/SC constants.
  32  * Initialization and platform specific PC/SC constants are handled in
  33  * the platform specific superclass.
  34  *
  35  * @since   1.6
  36  * @author  Andreas Sterbenz
  37  */
  38 final class PCSC extends PlatformPCSC {
  39 
  40     private PCSC() {
  41         // no instantiation
  42     }
  43 
  44     static void checkAvailable() throws RuntimeException {
  45         if (initException != null) {
  46             throw new UnsupportedOperationException
  47                     ("PC/SC not available on this platform", initException);
  48         }
  49     }
  50 
  51     // returns SCARDCONTEXT (contextId)
  52     static native long SCardEstablishContext
  53             (int scope)
  54             throws PCSCException;
  55 
  56     static native String[] SCardListReaders
  57             (long contextId)
  58             throws PCSCException;
  59 
  60     // returns SCARDHANDLE (cardId)
  61     static native long SCardConnect
  62             (long contextId, String readerName, int shareMode, int preferredProtocols)
  63             throws PCSCException;
  64 
  65     static native byte[] SCardTransmit
  66             (long cardId, int protocol, byte[] buf, int ofs, int len)
  67             throws PCSCException;
  68 
  69     // returns the ATR of the card, updates status[] with reader state and protocol
  70     static native byte[] SCardStatus
  71             (long cardId, byte[] status)
  72             throws PCSCException;
  73 
  74     static native void SCardDisconnect
  75             (long cardId, int disposition)
  76             throws PCSCException;
  77 
  78     // returns dwEventState[] of the same size and order as readerNames[]
  79     static native int[] SCardGetStatusChange
  80             (long contextId, long timeout, int[] currentState, String[] readerNames)
  81             throws PCSCException;
  82 
  83     static native void SCardBeginTransaction
  84             (long cardId)
  85             throws PCSCException;
  86 
  87     static native void SCardEndTransaction
  88             (long cardId, int disposition)
  89             throws PCSCException;
  90 
  91     static native byte[] SCardControl
  92             (long cardId, int controlCode, byte[] sendBuffer)
  93             throws PCSCException;
  94 
  95     // PCSC success/error/failure/warning codes
  96     final static int SCARD_S_SUCCESS             = 0x00000000;
  97     final static int SCARD_E_CANCELLED           = 0x80100002;
  98     final static int SCARD_E_CANT_DISPOSE        = 0x8010000E;
  99     final static int SCARD_E_INSUFFICIENT_BUFFER = 0x80100008;
 100     final static int SCARD_E_INVALID_ATR         = 0x80100015;
 101     final static int SCARD_E_INVALID_HANDLE      = 0x80100003;
 102     final static int SCARD_E_INVALID_PARAMETER   = 0x80100004;
 103     final static int SCARD_E_INVALID_TARGET      = 0x80100005;
 104     final static int SCARD_E_INVALID_VALUE       = 0x80100011;
 105     final static int SCARD_E_NO_MEMORY           = 0x80100006;
 106     final static int SCARD_F_COMM_ERROR          = 0x80100013;
 107     final static int SCARD_F_INTERNAL_ERROR      = 0x80100001;
 108     final static int SCARD_F_UNKNOWN_ERROR       = 0x80100014;
 109     final static int SCARD_F_WAITED_TOO_LONG     = 0x80100007;
 110     final static int SCARD_E_UNKNOWN_READER      = 0x80100009;
 111     final static int SCARD_E_TIMEOUT             = 0x8010000A;
 112     final static int SCARD_E_SHARING_VIOLATION   = 0x8010000B;
 113     final static int SCARD_E_NO_SMARTCARD        = 0x8010000C;
 114     final static int SCARD_E_UNKNOWN_CARD        = 0x8010000D;
 115     final static int SCARD_E_PROTO_MISMATCH      = 0x8010000F;
 116     final static int SCARD_E_NOT_READY           = 0x80100010;
 117     final static int SCARD_E_SYSTEM_CANCELLED    = 0x80100012;
 118     final static int SCARD_E_NOT_TRANSACTED      = 0x80100016;
 119     final static int SCARD_E_READER_UNAVAILABLE  = 0x80100017;
 120 
 121     final static int SCARD_W_UNSUPPORTED_CARD    = 0x80100065;
 122     final static int SCARD_W_UNRESPONSIVE_CARD   = 0x80100066;
 123     final static int SCARD_W_UNPOWERED_CARD      = 0x80100067;
 124     final static int SCARD_W_RESET_CARD          = 0x80100068;
 125     final static int SCARD_W_REMOVED_CARD        = 0x80100069;
 126     final static int SCARD_W_INSERTED_CARD       = 0x8010006A;
 127 
 128     final static int SCARD_E_UNSUPPORTED_FEATURE = 0x8010001F;
 129     final static int SCARD_E_PCI_TOO_SMALL       = 0x80100019;
 130     final static int SCARD_E_READER_UNSUPPORTED  = 0x8010001A;
 131     final static int SCARD_E_DUPLICATE_READER    = 0x8010001B;
 132     final static int SCARD_E_CARD_UNSUPPORTED    = 0x8010001C;
 133     final static int SCARD_E_NO_SERVICE          = 0x8010001D;
 134     final static int SCARD_E_SERVICE_STOPPED     = 0x8010001E;
 135 
 136     // MS undocumented
 137     final static int SCARD_E_NO_READERS_AVAILABLE = 0x8010002E;
 138     // std. Windows invalid handle return code, used instead of SCARD code
 139     final static int WINDOWS_ERROR_INVALID_HANDLE = 6;
 140     final static int WINDOWS_ERROR_INVALID_PARAMETER = 87;
 141 
 142     //
 143     final static int SCARD_SCOPE_USER      =  0x0000;
 144     final static int SCARD_SCOPE_TERMINAL  =  0x0001;
 145     final static int SCARD_SCOPE_SYSTEM    =  0x0002;
 146     final static int SCARD_SCOPE_GLOBAL    =  0x0003;
 147 
 148     final static int SCARD_SHARE_EXCLUSIVE =  0x0001;
 149     final static int SCARD_SHARE_SHARED    =  0x0002;
 150     final static int SCARD_SHARE_DIRECT    =  0x0003;
 151 
 152     final static int SCARD_LEAVE_CARD      =  0x0000;
 153     final static int SCARD_RESET_CARD      =  0x0001;
 154     final static int SCARD_UNPOWER_CARD    =  0x0002;
 155     final static int SCARD_EJECT_CARD      =  0x0003;
 156 
 157     final static int SCARD_STATE_UNAWARE     = 0x0000;
 158     final static int SCARD_STATE_IGNORE      = 0x0001;
 159     final static int SCARD_STATE_CHANGED     = 0x0002;
 160     final static int SCARD_STATE_UNKNOWN     = 0x0004;
 161     final static int SCARD_STATE_UNAVAILABLE = 0x0008;
 162     final static int SCARD_STATE_EMPTY       = 0x0010;
 163     final static int SCARD_STATE_PRESENT     = 0x0020;
 164     final static int SCARD_STATE_ATRMATCH    = 0x0040;
 165     final static int SCARD_STATE_EXCLUSIVE   = 0x0080;
 166     final static int SCARD_STATE_INUSE       = 0x0100;
 167     final static int SCARD_STATE_MUTE        = 0x0200;
 168     final static int SCARD_STATE_UNPOWERED   = 0x0400;
 169 
 170     final static int TIMEOUT_INFINITE = 0xffffffff;
 171 
 172     private final static char[] hexDigits = "0123456789abcdef".toCharArray();
 173 
 174     public static String toString(byte[] b) {
 175         StringBuffer sb = new StringBuffer(b.length * 3);
 176         for (int i = 0; i < b.length; i++) {
 177             int k = b[i] & 0xff;
 178             if (i != 0) {
 179                 sb.append(':');
 180             }
 181             sb.append(hexDigits[k >>> 4]);
 182             sb.append(hexDigits[k & 0xf]);
 183         }
 184         return sb.toString();
 185     }
 186 
 187 }