1 | /*
|
---|
2 | apdu.h
|
---|
3 | Definitions for ISO 7816-4 Application Layer PDU's handling
|
---|
4 |
|
---|
5 | This file is part of the Unix driver for Towitoko smartcard readers
|
---|
6 | Copyright (C) 2000 2001 Carlos Prados <cprados@yahoo.com>
|
---|
7 |
|
---|
8 | This version is modified by doz21 to work in a special manner ;)
|
---|
9 |
|
---|
10 | This library is free software; you can redistribute it and/or
|
---|
11 | modify it under the terms of the GNU Lesser General Public
|
---|
12 | License as published by the Free Software Foundation; either
|
---|
13 | version 2 of the License, or (at your option) any later version.
|
---|
14 |
|
---|
15 | This library is distributed in the hope that it will be useful,
|
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
18 | Lesser General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU Lesser General Public
|
---|
21 | License along with this library; if not, write to the Free Software
|
---|
22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
23 | */
|
---|
24 |
|
---|
25 | #ifndef _APDU_
|
---|
26 | #define _APDU_
|
---|
27 |
|
---|
28 | #include "defines.h"
|
---|
29 |
|
---|
30 | /*
|
---|
31 | * Exported constants definiton
|
---|
32 | */
|
---|
33 |
|
---|
34 | /* Return codes */
|
---|
35 | #define APDU_OK 0 /* Parsing of APDU OK */
|
---|
36 | #define APDU_MALFORMED 5 /* Malformed APDU */
|
---|
37 |
|
---|
38 | /* Types of APDU's */
|
---|
39 | #define APDU_CASE_1 0x0001 /* Nor send neither receive data */
|
---|
40 | #define APDU_CASE_2S 0x0002 /* Receive data (1..256) */
|
---|
41 | #define APDU_CASE_3S 0x0003 /* Send data (1..255) */
|
---|
42 | #define APDU_CASE_4S 0x0004 /* Send data (1..255) and receive data (1..256) */
|
---|
43 | #define APDU_CASE_2E 0x0102 /* Receive data (1..65536) */
|
---|
44 | #define APDU_CASE_3E 0x0103 /* Send data (1..65535) */
|
---|
45 | #define APDU_CASE_4E 0x0104 /* Send data (1..65535) and receive data (1..65536) */
|
---|
46 |
|
---|
47 | /* Maximum sizes of buffers */
|
---|
48 | #define APDU_MAX_CMD_SIZE 65545 /* Max command size */
|
---|
49 | #define APDU_MAX_RSP_SIZE 65538 /* Max response size */
|
---|
50 |
|
---|
51 | /*
|
---|
52 | * Exported macros definition
|
---|
53 | */
|
---|
54 | #define APDU_CASE_IS_EXTENDED(c) (((c) & 0x0100) == 0x0100)
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Exported data types definition
|
---|
58 | */
|
---|
59 |
|
---|
60 | /* Command APDU */
|
---|
61 | typedef struct
|
---|
62 | {
|
---|
63 | BYTE *command;
|
---|
64 | unsigned long length;
|
---|
65 | }
|
---|
66 | APDU_Cmd;
|
---|
67 |
|
---|
68 | /* Response APDU */
|
---|
69 | typedef struct
|
---|
70 | {
|
---|
71 | BYTE *response;
|
---|
72 | unsigned long length;
|
---|
73 | }
|
---|
74 | APDU_Rsp;
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * Exported functions declaration
|
---|
78 | */
|
---|
79 |
|
---|
80 | /* Create a APDU_Cmd */
|
---|
81 | extern APDU_Cmd *APDU_Cmd_New (BYTE * data, unsigned long length);
|
---|
82 |
|
---|
83 | /* Delete a APDU_Cmd */
|
---|
84 | extern void APDU_Cmd_Delete (APDU_Cmd * apdu);
|
---|
85 |
|
---|
86 | /* Return the case of command */
|
---|
87 | extern int APDU_Cmd_Case (APDU_Cmd * apdu);
|
---|
88 |
|
---|
89 | /* Return class of command */
|
---|
90 | extern BYTE APDU_Cmd_Cla (APDU_Cmd * apdu);
|
---|
91 |
|
---|
92 | /* Return command instruction */
|
---|
93 | extern BYTE APDU_Cmd_Ins (APDU_Cmd * apdu);
|
---|
94 |
|
---|
95 | /* Return first paramenter of command */
|
---|
96 | extern BYTE APDU_Cmd_P1 (APDU_Cmd * apdu);
|
---|
97 |
|
---|
98 | /* Return second parameter of command */
|
---|
99 | extern BYTE APDU_Cmd_P2 (APDU_Cmd * apdu);
|
---|
100 |
|
---|
101 | /* Return length of data sent */
|
---|
102 | extern unsigned long APDU_Cmd_Lc (APDU_Cmd * apdu);
|
---|
103 |
|
---|
104 | /* Return length of data expected */
|
---|
105 | extern unsigned long APDU_Cmd_Le (APDU_Cmd * apdu);
|
---|
106 |
|
---|
107 | /* Return TRUE if the command requires all data available */
|
---|
108 | extern bool APDU_Cmd_Le_Available (APDU_Cmd * apdu);
|
---|
109 |
|
---|
110 | /* Return a pointer to the header of the command */
|
---|
111 | extern BYTE *APDU_Cmd_Header (APDU_Cmd * apdu);
|
---|
112 |
|
---|
113 | /* Return a pointer to the data of the comamnd */
|
---|
114 | extern BYTE *APDU_Cmd_Data (APDU_Cmd * apdu);
|
---|
115 |
|
---|
116 | /* Return a pointer to the whole command */
|
---|
117 | extern BYTE *APDU_Cmd_Raw (APDU_Cmd * apdu);
|
---|
118 |
|
---|
119 | /* Return the length of the whole command */
|
---|
120 | extern unsigned long APDU_Cmd_RawLen (APDU_Cmd * apdu);
|
---|
121 |
|
---|
122 | /* Create a APDU_Rsp */
|
---|
123 | extern APDU_Rsp *APDU_Rsp_New (BYTE * data, unsigned long length);
|
---|
124 |
|
---|
125 | /* Delete a APDU_Rsp */
|
---|
126 | extern void APDU_Rsp_Delete (APDU_Rsp * apdu);
|
---|
127 |
|
---|
128 | /* Return fitst status byte */
|
---|
129 | extern BYTE APDU_Rsp_SW1 (APDU_Rsp * apdu);
|
---|
130 |
|
---|
131 | /* Return second status byte */
|
---|
132 | extern BYTE APDU_Rsp_SW2 (APDU_Rsp * apdu);
|
---|
133 |
|
---|
134 | /* Return length of the data of the response */
|
---|
135 | extern unsigned long APDU_Rsp_DataLen (APDU_Rsp * apdu);
|
---|
136 |
|
---|
137 | /* Return a pointer to the data of the response */
|
---|
138 | extern BYTE *APDU_Rsp_Data (APDU_Rsp * apdu);
|
---|
139 |
|
---|
140 | /* Return a pointer to the whole response */
|
---|
141 | extern BYTE *APDU_Rsp_Raw (APDU_Rsp * apdu);
|
---|
142 |
|
---|
143 | /* Return the length of the whole response */
|
---|
144 | extern unsigned long APDU_Rsp_RawLen (APDU_Rsp * apdu);
|
---|
145 |
|
---|
146 | /* Truncate size of response APDU */
|
---|
147 | extern void APDU_Rsp_TruncateData (APDU_Rsp * apdu, unsigned long length);
|
---|
148 |
|
---|
149 | /* Adds one APDU at the end of the data bytes of an APDU */
|
---|
150 | extern int APDU_Rsp_AppendData (APDU_Rsp * apdu1, APDU_Rsp * apdu2);
|
---|
151 |
|
---|
152 | #endif
|
---|
153 |
|
---|