Path: blob/master/src/java.smartcardio/share/classes/sun/security/smartcardio/PCSC.java
41159 views
/*1* Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.smartcardio;2627import java.security.AccessController;2829/**30* Access to native PC/SC functions and definition of PC/SC constants.31* Initialization and platform specific PC/SC constants are handled in32* the platform specific superclass.33*34* @since 1.635* @author Andreas Sterbenz36*/37final class PCSC extends PlatformPCSC {3839private PCSC() {40// no instantiation41}4243static void checkAvailable() throws RuntimeException {44if (initException != null) {45throw new UnsupportedOperationException46("PC/SC not available on this platform", initException);47}48}4950// returns SCARDCONTEXT (contextId)51static native long SCardEstablishContext52(int scope)53throws PCSCException;5455static native String[] SCardListReaders56(long contextId)57throws PCSCException;5859// returns SCARDHANDLE (cardId)60static native long SCardConnect61(long contextId, String readerName, int shareMode, int preferredProtocols)62throws PCSCException;6364static native byte[] SCardTransmit65(long cardId, int protocol, byte[] buf, int ofs, int len)66throws PCSCException;6768// returns the ATR of the card, updates status[] with reader state and protocol69static native byte[] SCardStatus70(long cardId, byte[] status)71throws PCSCException;7273static native void SCardDisconnect74(long cardId, int disposition)75throws PCSCException;7677// returns dwEventState[] of the same size and order as readerNames[]78static native int[] SCardGetStatusChange79(long contextId, long timeout, int[] currentState, String[] readerNames)80throws PCSCException;8182static native void SCardBeginTransaction83(long cardId)84throws PCSCException;8586static native void SCardEndTransaction87(long cardId, int disposition)88throws PCSCException;8990static native byte[] SCardControl91(long cardId, int controlCode, byte[] sendBuffer)92throws PCSCException;9394// PCSC success/error/failure/warning codes95final static int SCARD_S_SUCCESS = 0x00000000;96final static int SCARD_E_CANCELLED = 0x80100002;97final static int SCARD_E_CANT_DISPOSE = 0x8010000E;98final static int SCARD_E_INSUFFICIENT_BUFFER = 0x80100008;99final static int SCARD_E_INVALID_ATR = 0x80100015;100final static int SCARD_E_INVALID_HANDLE = 0x80100003;101final static int SCARD_E_INVALID_PARAMETER = 0x80100004;102final static int SCARD_E_INVALID_TARGET = 0x80100005;103final static int SCARD_E_INVALID_VALUE = 0x80100011;104final static int SCARD_E_NO_MEMORY = 0x80100006;105final static int SCARD_F_COMM_ERROR = 0x80100013;106final static int SCARD_F_INTERNAL_ERROR = 0x80100001;107final static int SCARD_F_UNKNOWN_ERROR = 0x80100014;108final static int SCARD_F_WAITED_TOO_LONG = 0x80100007;109final static int SCARD_E_UNKNOWN_READER = 0x80100009;110final static int SCARD_E_TIMEOUT = 0x8010000A;111final static int SCARD_E_SHARING_VIOLATION = 0x8010000B;112final static int SCARD_E_NO_SMARTCARD = 0x8010000C;113final static int SCARD_E_UNKNOWN_CARD = 0x8010000D;114final static int SCARD_E_PROTO_MISMATCH = 0x8010000F;115final static int SCARD_E_NOT_READY = 0x80100010;116final static int SCARD_E_SYSTEM_CANCELLED = 0x80100012;117final static int SCARD_E_NOT_TRANSACTED = 0x80100016;118final static int SCARD_E_READER_UNAVAILABLE = 0x80100017;119120final static int SCARD_W_UNSUPPORTED_CARD = 0x80100065;121final static int SCARD_W_UNRESPONSIVE_CARD = 0x80100066;122final static int SCARD_W_UNPOWERED_CARD = 0x80100067;123final static int SCARD_W_RESET_CARD = 0x80100068;124final static int SCARD_W_REMOVED_CARD = 0x80100069;125final static int SCARD_W_INSERTED_CARD = 0x8010006A;126127final static int SCARD_E_UNSUPPORTED_FEATURE = 0x8010001F;128final static int SCARD_E_PCI_TOO_SMALL = 0x80100019;129final static int SCARD_E_READER_UNSUPPORTED = 0x8010001A;130final static int SCARD_E_DUPLICATE_READER = 0x8010001B;131final static int SCARD_E_CARD_UNSUPPORTED = 0x8010001C;132final static int SCARD_E_NO_SERVICE = 0x8010001D;133final static int SCARD_E_SERVICE_STOPPED = 0x8010001E;134135// MS undocumented136final static int SCARD_E_NO_READERS_AVAILABLE = 0x8010002E;137// std. Windows invalid handle return code, used instead of SCARD code138final static int WINDOWS_ERROR_INVALID_HANDLE = 6;139final static int WINDOWS_ERROR_INVALID_PARAMETER = 87;140141//142final static int SCARD_SCOPE_USER = 0x0000;143final static int SCARD_SCOPE_TERMINAL = 0x0001;144final static int SCARD_SCOPE_SYSTEM = 0x0002;145final static int SCARD_SCOPE_GLOBAL = 0x0003;146147final static int SCARD_SHARE_EXCLUSIVE = 0x0001;148final static int SCARD_SHARE_SHARED = 0x0002;149final static int SCARD_SHARE_DIRECT = 0x0003;150151final static int SCARD_LEAVE_CARD = 0x0000;152final static int SCARD_RESET_CARD = 0x0001;153final static int SCARD_UNPOWER_CARD = 0x0002;154final static int SCARD_EJECT_CARD = 0x0003;155156final static int SCARD_STATE_UNAWARE = 0x0000;157final static int SCARD_STATE_IGNORE = 0x0001;158final static int SCARD_STATE_CHANGED = 0x0002;159final static int SCARD_STATE_UNKNOWN = 0x0004;160final static int SCARD_STATE_UNAVAILABLE = 0x0008;161final static int SCARD_STATE_EMPTY = 0x0010;162final static int SCARD_STATE_PRESENT = 0x0020;163final static int SCARD_STATE_ATRMATCH = 0x0040;164final static int SCARD_STATE_EXCLUSIVE = 0x0080;165final static int SCARD_STATE_INUSE = 0x0100;166final static int SCARD_STATE_MUTE = 0x0200;167final static int SCARD_STATE_UNPOWERED = 0x0400;168169final static int TIMEOUT_INFINITE = 0xffffffff;170171private final static char[] hexDigits = "0123456789abcdef".toCharArray();172173public static String toString(byte[] b) {174StringBuilder sb = new StringBuilder(b.length * 3);175for (int i = 0; i < b.length; i++) {176int k = b[i] & 0xff;177if (i != 0) {178sb.append(':');179}180sb.append(hexDigits[k >>> 4]);181sb.append(hexDigits[k & 0xf]);182}183return sb.toString();184}185186}187188189