Minor change
[platform/core/connectivity/smartcard-service.git] / common / include / APDUHelper.h
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef APDUHELPER_H_
18 #define APDUHELPER_H_
19
20 /* standard library header */
21
22 /* SLP library header */
23
24 /* local header */
25 #include "ByteArray.h"
26
27 namespace smartcard_service_api
28 {
29         class ResponseHelper
30         {
31         private:
32                 ByteArray response;
33                 unsigned char sw[2];
34                 int status;
35                 ByteArray dataField;
36
37                 static int parseStatusWord(const unsigned char *sw);
38
39         public:
40                 static const int SUCCESS = 0;
41
42                 static const int ERROR_UNKNOWN = -1;
43
44                 static const int ERROR_NO_INFORMATION = -(0x6900);
45                 static const int ERROR_COMMAND_INCOMPATIBLE = -(0x6981);
46                 static const int ERROR_SECURITY_NOT_SATISFIED = -(0x6982);
47                 static const int ERROR_AUTH_PIN_BLOCKED = -(0x6983);
48                 static const int ERROR_REF_DATA_INVALID = -(0x6984);
49                 static const int ERROR_CONDITION_NOT_SATIFIED = -(0x6985);
50                 static const int ERROR_COMMAND_NOT_ALLOW = -(0x6986);
51
52                 static const int ERROR_INCORRECT_PARAMETER = -(0x6a80);
53                 static const int ERROR_FUNCTION_NOT_SUPPORT = -(0x6a81);
54                 static const int ERROR_FILE_NOT_FOUND = -(0x6a82);
55                 static const int ERROR_RECORD_NOT_FOUND = -(0x6a83);
56                 static const int ERROR_NOT_ENOUGH_MEMORY = -(0x6a84);
57                 static const int ERROR_INCORRECT_P1_P2 = -(0x6a86);
58                 static const int ERROR_LC_INCONSISTENT = -(0x6a87);
59                 static const int ERROR_REF_DATA_NOT_FOUND = -(0x6a88);
60
61                 ResponseHelper();
62                 ResponseHelper(const ByteArray &response);
63                 ~ResponseHelper();
64
65                 bool setResponse(const ByteArray &response);
66                 inline int getStatus() const { return status; }
67                 inline unsigned char getSW1() const { return sw[0]; }
68                 inline unsigned char getSW2() const { return sw[1]; }
69
70 //              char *getErrorString();
71                 inline const ByteArray getDataField() const { return dataField; }
72
73                 static int getStatus(const ByteArray &response);
74                 static const ByteArray getDataField(const ByteArray &response);
75 //              static char *getErrorString();
76         };
77
78         class APDUCommand
79         {
80         private:
81                 typedef struct _command_header_t
82                 {
83                         unsigned char cla;
84                         unsigned char ins;
85                         unsigned char param[2];
86                 } command_header_t;
87
88                 command_header_t header;
89                 ByteArray commandData;
90                 unsigned int maxResponseSize;
91                 bool isExtendedLength;
92
93         public:
94                 static const unsigned char INS_DEACTIVATE_FILE = (unsigned char)0x04;
95                 static const unsigned char INS_TERMINAL_PROFILE = (unsigned char)0x10;
96                 static const unsigned char INS_FETCH = (unsigned char)0x12;
97                 static const unsigned char INS_TERMINAL_RESPONSE = (unsigned char)0x14;
98                 static const unsigned char INS_VERIFY = (unsigned char)0x20;
99                 static const unsigned char INS_CHANGE_PIN = (unsigned char)0x24;
100                 static const unsigned char INS_DISABLE_PIN = (unsigned char)0x26;
101                 static const unsigned char INS_ENABLE_PIN = (unsigned char)0x28;
102                 static const unsigned char INS_UNBLOCK_PIN = (unsigned char)0x2C;
103                 static const unsigned char INS_INCREASE = (unsigned char)0x32;
104                 static const unsigned char INS_ACTIVATE_FILE = (unsigned char)0x44;
105                 static const unsigned char INS_GET_CHALLENGE = (unsigned char)0x84;
106                 static const unsigned char INS_AUTHENTICATE = (unsigned char)0x88;
107                 static const unsigned char INS_AUTHENTICATE2 = (unsigned char)0x89;
108                 static const unsigned char INS_MANAGE_CHANNEL = (unsigned char)0x70;
109                 static const unsigned char INS_MANAGE_SECURE_CHANNEL = (unsigned char)0x73;
110                 static const unsigned char INS_TRANSACT_DATA = (unsigned char)0x75;
111                 static const unsigned char INS_SEARCH_RECORD = (unsigned char)0xA2;
112                 static const unsigned char INS_SELECT_FILE = (unsigned char)0xA4;
113                 static const unsigned char INS_TERMINAL_CAPABILITY = (unsigned char)0xAA;
114                 static const unsigned char INS_READ_BINARY = (unsigned char)0xB0;
115                 static const unsigned char INS_READ_RECORD = (unsigned char)0xB2;
116                 static const unsigned char INS_GET_RESPONSE = (unsigned char)0xC0;
117                 static const unsigned char INS_ENVELOPE = (unsigned char)0xC2;
118                 static const unsigned char INS_RETRIEVE_DATA = (unsigned char)0xCB;
119                 static const unsigned char INS_WRITE_BINARY = (unsigned char)0xD0;
120                 static const unsigned char INS_UPDATE_BINARY = (unsigned char)0xD6;
121                 static const unsigned char INS_UPDATE_RECORD = (unsigned char)0xDC;
122                 static const unsigned char INS_SET_DATA = (unsigned char)0xDB;
123                 static const unsigned char INS_CREATE_FILE = (unsigned char)0xE0;
124                 static const unsigned char INS_APPEND_RECORD = (unsigned char)0xE2;
125                 static const unsigned char INS_DELETE_FILE = (unsigned char)0xE4;
126                 static const unsigned char INS_STATUS = (unsigned char)0xF2;
127 //              static const unsigned char INS_ = (unsigned char)0x;
128
129                 static const unsigned char P1_SELECT_BY_ID = (unsigned char)0x00;
130                 static const unsigned char P1_SELECT_PARENT_DF = (unsigned char)0x03;
131                 static const unsigned char P1_SELECT_BY_DF_NAME = (unsigned char)0x04;
132                 static const unsigned char P1_SELECT_BY_PATH = (unsigned char)0x08;
133                 static const unsigned char P1_SELECT_BY_PATH_FROM_CURRENT_DF = (unsigned char)0x09;
134 //              static const unsigned char P1_ = (unsigned char)0x;
135
136                 static const unsigned char P2_SELECT_GET_FCP = (unsigned char)0x04;
137 //              static const unsigned char P2_ = (unsigned char)0x;
138
139                 static const unsigned char CLA_CHANNEL_STANDARD = (unsigned char)0x00;
140                 static const unsigned char CLA_CHANNEL_EXTENDED = (unsigned char)0x01;
141
142                 static const unsigned int LE_MAX = -1;
143
144                 APDUCommand();
145                 ~APDUCommand();
146
147                 bool setCommand(unsigned char cla, unsigned char ins, unsigned char p1,
148                         unsigned char p2, const ByteArray &commandData, unsigned int maxResponseSize);
149                 bool setCommand(const ByteArray &command);
150
151                 bool setChannel(int type, int channelNum);
152
153                 void setCLA(unsigned char cla);
154                 unsigned char getCLA() const;
155
156                 void setINS(unsigned char ins);
157                 unsigned char getINS() const;
158
159                 void setP1(unsigned char p1);
160                 unsigned char getP1() const;
161
162                 void setP2(unsigned char p2);
163                 unsigned char getP2() const;
164
165                 void setCommandData(const ByteArray &data);
166                 const ByteArray getCommandData() const;
167
168                 void setMaxResponseSize(unsigned int maxResponseSize);
169                 unsigned int getMaxResponseSize() const;
170
171                 bool getBuffer(ByteArray &array) const;
172         };
173
174         class APDUHelper
175         {
176         public:
177                 static const int COMMAND_OPEN_LOGICAL_CHANNEL = 1;
178                 static const int COMMAND_CLOSE_LOGICAL_CHANNEL = 2;
179                 static const int COMMAND_SELECT_BY_ID = 3;
180                 static const int COMMAND_SELECT_PARENT_DF = 4;
181                 static const int COMMAND_SELECT_BY_DF_NAME = 5;
182                 static const int COMMAND_SELECT_BY_PATH = 6;
183                 static const int COMMAND_SELECT_BY_PATH_FROM_CURRENT_DF = 7;
184                 static const int COMMAND_READ_BINARY = 8;
185                 static const int COMMAND_READ_RECORD = 9;
186                 static const int COMMAND_WRITE_BINARY = 10;
187                 static const int COMMAND_WRITE_RECORD = 11;
188
189                 static const ByteArray generateAPDU(int command,
190                         int channel, const ByteArray &data);
191         };
192
193 } /* namespace smartcard_service_api */
194 #endif /* APDUHELPER_H_ */