1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright 2019 Joyent, Inc. 14 */ 15 16 #ifndef _WINSCARD_H 17 #define _WINSCARD_H 18 19 /* 20 * This library provides a compatibility interface with programs designed 21 * against the PC SmartCard Library. This originates from Microsoft and has been 22 * used on a few different forms over the years by folks. The purpose of this 23 * library is for compatibility. 24 * 25 * At the time of this writing, Microsofts API documentation can be found here: 26 * https://docs.microsoft.com/en-us/windows/win32/api/winscard/ 27 * 28 * New consumers should not use this library and instead should leverage 29 * ccid(4D) instead. 30 */ 31 32 #include <stdint.h> 33 #include <wintypes.h> 34 35 #ifdef __cplusplus 36 extern "C" { 37 #endif 38 39 /* 40 * This is a departure from the PCSC system which defines this as a LONG, 41 * which is the same size on 32bit and 64bit Windows (ILP32 and LLP64). 42 * We need to use the real native pointer size for the context handle as 43 * it wouldn't fit into a LONG on our LP64 platform. 44 */ 45 typedef void *SCARDCONTEXT; 46 typedef void **PSCARDCONTEXT; 47 typedef void **LPSCARDCONTEXT; 48 typedef void *SCARDHANDLE; 49 typedef void **PSCARDHANDLE; 50 typedef void **LPSCARDHANDLE; 51 52 /* 53 * Conventionally this is supposed to be packed. 54 */ 55 #pragma pack(1) 56 typedef struct { 57 unsigned long dwProtocol; 58 unsigned long cbPciLength; 59 } SCARD_IO_REQUEST, *PSCARD_IO_REQUEST, *LPSCARD_IO_REQUEST; 60 #pragma pack() 61 62 extern SCARD_IO_REQUEST g_rgSCardT0Pci, g_rgSCardT1Pci, g_rgSCardRawPci; 63 #define SCARD_PCI_T0 (&g_rgSCardT0Pci) 64 #define SCARD_PCI_T1 (&g_rgSCardT1Pci) 65 #define SCARD_PCI_RAW (&g_rgSCardRawPci) 66 67 /* 68 * Return values and error codes. We strive to use the same error codes as 69 * Microsoft. 70 */ 71 #define SCARD_S_SUCCESS ((LONG)0x00000000) 72 #define SCARD_F_INTERNAL_ERROR ((LONG)0x80100001) 73 #define SCARD_E_CANCELLED ((LONG)0x80100002) 74 #define SCARD_E_INVALID_HANDLE ((LONG)0x80100003) 75 #define SCARD_E_INVALID_PARAMETER ((LONG)0x80100004) 76 #define SCARD_E_NO_MEMORY ((LONG)0x80100006) 77 #define SCARD_E_INSUFFICIENT_BUFFER ((LONG)0x80100008) 78 #define SCARD_E_UNKNOWN_READER ((LONG)0x80100009) 79 #define SCARD_E_TIMEOUT ((LONG)0x8010000a) 80 #define SCARD_E_SHARING_VIOLATION ((LONG)0x8010000b) 81 #define SCARD_E_NO_SMARTCARD ((LONG)0x8010000c) 82 #define SCARD_E_UNKNOWN_CARD ((LONG)0x8010000d) 83 #define SCARD_E_PROTO_MISMATCH ((LONG)0x8010000f) 84 #define SCARD_E_INVALID_VALUE ((LONG)0x80100011) 85 #define SCARD_F_COMM_ERROR ((LONG)0x80100013) 86 #define SCARD_F_UNKNOWN_ERROR ((LONG)0x80100014) 87 #define SCARD_E_READER_UNAVAILABLE ((LONG)0x80100017) 88 #define SCARD_E_NO_SERVICE ((LONG)0x8010001D) 89 #define SCARD_E_UNSUPPORTED_FEATURE ((LONG)0x80100022) 90 #define SCARD_E_NO_READERS_AVAILABLE ((LONG)0x8010002E) 91 #define SCARD_W_UNSUPPORTED_CARD ((LONG)0x80100065) 92 #define SCARD_W_UNPOWERED_CARD ((LONG)0x80100067) 93 #define SCARD_W_RESET_CARD ((LONG)0x80100068) 94 #define SCARD_W_REMOVED_CARD ((LONG)0x80100069) 95 96 #define SCARD_SCOPE_USER 0x0000 97 #define SCARD_SCOPE_TERMINAL 0x0001 98 #define SCARD_SCOPE_SYSTEM 0x0002 99 #define SCARD_SCOPE_GLOBAL 0x0003 100 101 #define SCARD_SHARE_EXCLUSIVE 0x0001 102 #define SCARD_SHARE_SHARED 0x0002 103 #define SCARD_SHARE_DIRECT 0x0003 104 105 #define SCARD_PROTOCOL_T0 0x0001 106 #define SCARD_PROTOCOL_T1 0x0002 107 #define SCARD_PROTOCOL_RAW 0x0004 108 #define SCARD_PROTOCOL_T15 0x0008 109 110 #define SCARD_LEAVE_CARD 0x0000 111 #define SCARD_RESET_CARD 0x0001 112 #define SCARD_UNPOWER_CARD 0x0002 113 #define SCARD_EJECT_CARD 0x0003 114 115 /* 116 * This is used to indicate that the framework should allocate memory. 117 */ 118 #define SCARD_AUTOALLOCATE UINT32_MAX 119 120 extern LONG SCardEstablishContext(DWORD, LPCVOID, LPCVOID, LPSCARDCONTEXT); 121 extern LONG SCardReleaseContext(SCARDCONTEXT); 122 123 extern LONG SCardListReaders(SCARDCONTEXT, LPCSTR, LPSTR, LPDWORD); 124 125 extern LONG SCardFreeMemory(SCARDCONTEXT, LPCVOID); 126 127 extern LONG SCardConnect(SCARDCONTEXT, LPCSTR, DWORD, DWORD, LPSCARDHANDLE, 128 LPDWORD); 129 extern LONG SCardDisconnect(SCARDHANDLE, DWORD); 130 131 extern LONG SCardBeginTransaction(SCARDHANDLE); 132 extern LONG SCardEndTransaction(SCARDHANDLE, DWORD); 133 extern LONG SCardReconnect(SCARDHANDLE, DWORD, DWORD, DWORD, LPDWORD); 134 135 extern LONG SCardTransmit(SCARDHANDLE, const SCARD_IO_REQUEST *, LPCBYTE, 136 DWORD, SCARD_IO_REQUEST *, LPBYTE, LPDWORD); 137 138 extern const char *pcsc_stringify_error(const LONG); 139 140 #ifdef __cplusplus 141 } 142 #endif 143 144 #endif /* _WINSCARD_H */ 145