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