globalplatform
connection.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2026, Karsten Ohme
3  * This file is part of GlobalPlatform.
4  *
5  * GlobalPlatform is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * GlobalPlatform is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with GlobalPlatform. If not, see <https://www.gnu.org/licenses/>.
17  *
18  * In addition, as a special exception, the copyright holders give
19  * permission to link the code of portions of this program with the
20  * OpenSSL library under certain conditions as described in each
21  * individual source file, and distribute linked combinations
22  * including the two.
23  * You must obey the GNU Lesser General Public License in all respects
24  * for all of the code used other than OpenSSL. If you modify
25  * file(s) with this exception, you may extend this exception to your
26  * version of the file(s), but you are not obligated to do so. If you
27  * do not wish to do so, delete this exception statement from your
28  * version.
29  */
30 
36 #ifndef OPGP_CONNECTION_H
37 #define OPGP_CONNECTION_H
38 
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #endif
43 
44 #include <stdio.h>
45 #ifdef WIN32
46 #include <WinSCard.h>
47 #else
48 #include <winscard.h>
49 #endif
50 #include "unicode.h"
51 #include "types.h"
52 #include "library.h"
53 #include "security.h"
54 #include "error.h"
55 
56 #if !defined __winscard_h__
57 #define MAX_ATR_SIZE 33
58 #endif
59 
60 #define OPGP_TRACE_MODE_ENABLE 1
61 #define OPGP_TRACE_MODE_DISABLE 0
62 
63 #define OPGP_CARD_PROTOCOL_T0 SCARD_PROTOCOL_T0
64 #define OPGP_CARD_PROTOCOL_T1 SCARD_PROTOCOL_T1
65 
67 typedef struct OPGP_CARD_INFO OPGP_CARD_INFO;
68 
69 typedef OPGP_ERROR_STATUS (*OPGP_ESTABLISH_CONTEXT_FN)(OPGP_CARD_CONTEXT *);
70 typedef OPGP_ERROR_STATUS (*OPGP_RELEASE_CONTEXT_FN)(OPGP_CARD_CONTEXT *);
71 typedef OPGP_ERROR_STATUS (*OPGP_CARD_CONNECT_FN)(OPGP_CARD_CONTEXT, OPGP_CSTRING, OPGP_CARD_INFO *, DWORD);
72 typedef OPGP_ERROR_STATUS (*OPGP_CARD_DISCONNECT_FN)(OPGP_CARD_CONTEXT, OPGP_CARD_INFO *);
73 typedef OPGP_ERROR_STATUS (*OPGP_LIST_READERS_FN)(OPGP_CARD_CONTEXT, OPGP_STRING, PDWORD, DWORD);
74 typedef OPGP_ERROR_STATUS (*OPGP_SEND_APDU_FN)(OPGP_CARD_CONTEXT, OPGP_CARD_INFO, PBYTE, DWORD, PBYTE, PDWORD);
75 typedef OPGP_ERROR_STATUS (*OPGP_SUPPORTS_EXTENDED_APDU_FN)(OPGP_CARD_CONTEXT, OPGP_CARD_INFO, BOOL *);
76 
80 typedef struct
81 {
82  OPGP_ESTABLISH_CONTEXT_FN establishContext;
83  OPGP_RELEASE_CONTEXT_FN releaseContext;
84  OPGP_CARD_CONNECT_FN cardConnect;
85  OPGP_CARD_DISCONNECT_FN cardDisconnect;
86  OPGP_LIST_READERS_FN listReaders;
87  OPGP_SEND_APDU_FN sendAPDU;
88  OPGP_SUPPORTS_EXTENDED_APDU_FN supportsExtendedAPDU;
89 
91 
95 typedef struct OPGP_CARD_CONTEXT {
97  TCHAR libraryName[64];
98  TCHAR libraryVersion[32];
102 
106 typedef struct OPGP_CARD_INFO {
114 
115 // functions
116 
118 OPGP_API
119 void OPGP_enable_trace_mode(DWORD enable, FILE *out);
120 
121 extern DWORD traceEnable;
122 extern FILE *traceFile;
123 
125 OPGP_API
127 
129 OPGP_API
131 
133 OPGP_API
134 OPGP_ERROR_STATUS OPGP_list_readers(OPGP_CARD_CONTEXT cardContext, OPGP_STRING readerNames, PDWORD readerNamesLength, DWORD presentOnly);
135 
137 OPGP_API
138 OPGP_ERROR_STATUS OPGP_card_connect(OPGP_CARD_CONTEXT cardContext, OPGP_CSTRING readerName, OPGP_CARD_INFO *cardInfo, DWORD protocol);
139 
141 OPGP_API
143 
145 OPGP_API
146 OPGP_ERROR_STATUS OPGP_send_APDU(OPGP_CARD_CONTEXT cardContext, OPGP_CARD_INFO cardInfo, GP211_SECURITY_INFO *secInfo, PBYTE capdu, DWORD capduLength, PBYTE rapdu, PDWORD rapduLength);
147 
149 OPGP_API
150 OPGP_ERROR_STATUS OPGP_send_chained_APDU(OPGP_CARD_CONTEXT cardContext, OPGP_CARD_INFO cardInfo, GP211_SECURITY_INFO *secInfo, PBYTE capdus[], DWORD capduLengths[], DWORD numCapdus, PBYTE rapdu, PDWORD rapduLength);
151 
152 OPGP_ERROR_STATUS OPGP_send_chained_APDU_extended(OPGP_CARD_CONTEXT cardContext, OPGP_CARD_INFO cardInfo, GP211_SECURITY_INFO *secInfo, PBYTE capdus[], DWORD capduLengths[], DWORD numCapdus, PBYTE rapdu, PDWORD rapduLength, BOOL allowExtendedApdu);
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 #endif
OPGP_API OPGP_ERROR_STATUS OPGP_card_connect(OPGP_CARD_CONTEXT cardContext, OPGP_CSTRING readerName, OPGP_CARD_INFO *cardInfo, DWORD protocol)
This function connects to a reader.
Definition: connection.c:266
OPGP_API OPGP_ERROR_STATUS OPGP_release_context(OPGP_CARD_CONTEXT *cardContext)
This function releases the context to the connection layer established by OPGP_establish_context().
Definition: connection.c:203
struct OPGP_CARD_INFO OPGP_CARD_INFO
Definition: connection.h:67
OPGP_API OPGP_ERROR_STATUS OPGP_send_APDU(OPGP_CARD_CONTEXT cardContext, OPGP_CARD_INFO cardInfo, GP211_SECURITY_INFO *secInfo, PBYTE capdu, DWORD capduLength, PBYTE rapdu, PDWORD rapduLength)
This function sends an APDU.
Definition: connection.c:317
OPGP_API OPGP_ERROR_STATUS OPGP_send_chained_APDU(OPGP_CARD_CONTEXT cardContext, OPGP_CARD_INFO cardInfo, GP211_SECURITY_INFO *secInfo, PBYTE capdus[], DWORD capduLengths[], DWORD numCapdus, PBYTE rapdu, PDWORD rapduLength)
This function sends multiple APDUs that belong to one logical command.
Definition: connection.c:403
#define MAX_ATR_SIZE
Maximum ATR size.
Definition: connection.h:57
OPGP_API void OPGP_enable_trace_mode(DWORD enable, FILE *out)
Enables the trace mode.
Definition: connection.c:58
FILE * traceFile
The trace file for trace mode.
Definition: connection.c:43
DWORD traceEnable
Enable trace mode.
Definition: connection.c:42
OPGP_API OPGP_ERROR_STATUS OPGP_card_disconnect(OPGP_CARD_CONTEXT cardContext, OPGP_CARD_INFO *cardInfo)
This function disconnects a reader.
Definition: connection.c:296
OPGP_API OPGP_ERROR_STATUS OPGP_list_readers(OPGP_CARD_CONTEXT cardContext, OPGP_STRING readerNames, PDWORD readerNamesLength, DWORD presentOnly)
This function returns a list of currently available readers.
Definition: connection.c:247
struct OPGP_CARD_CONTEXT OPGP_CARD_CONTEXT
Definition: connection.h:66
OPGP_API OPGP_ERROR_STATUS OPGP_establish_context(OPGP_CARD_CONTEXT *cardContext)
This function establishes a context to connection layer.
Definition: connection.c:71
Definition: security.h:243
Definition: connection.h:95
PVOID librarySpecific
Library specific data.
Definition: connection.h:96
TCHAR libraryVersion[32]
The version of the connection library to use.
Definition: connection.h:98
OPGP_CONNECTION_FUNCTIONS connectionFunctions
Connection functions of the connection library. Is automatically filled in if the connection library ...
Definition: connection.h:100
PVOID libraryHandle
The handle to the library.
Definition: connection.h:99
TCHAR libraryName[64]
The name of the connection library to use.
Definition: connection.h:97
Definition: connection.h:106
DWORD ATRLength
The length of the ATR buffer.
Definition: connection.h:108
BYTE logicalChannel
The current logical channel.
Definition: connection.h:109
BOOL extendedAPDUSupported
TRUE if the connection supports extended APDUs for this card.
Definition: connection.h:111
BYTE specVersion
The specification version, see OP_201 or GP_211.
Definition: connection.h:110
BYTE ATR[MAX_ATR_SIZE]
The Answer To Reset from the card.
Definition: connection.h:107
PVOID librarySpecific
Specific data for the library.
Definition: connection.h:112
Definition: connection.h:81
Definition: error.h:58
void * PVOID
Pointer definition.
Definition: types.h:52
unsigned char BYTE
A Microsoft/Muscle BYTE definition.
Definition: types.h:67
unsigned char * PBYTE
A Microsoft/Muscle LPBYTE, pointer to unsigned char.
Definition: types.h:64
unsigned long * PDWORD
A Microsoft LPDWORD/Muscle pointer to a DWORD.
Definition: types.h:66
char * OPGP_STRING
A Microsoft/Muscle LPTSTR.
Definition: types.h:62
unsigned long DWORD
A Microsoft/Muscle DWORD definition.
Definition: types.h:68
const char * OPGP_CSTRING
A Microsoft/Muscle LPCTSTR.
Definition: types.h:63
This handles smartcard reader communications.