Change the authorization sequence
[platform/core/connectivity/smartcard-service.git] / common / FileObject.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
18 /* standard library header */
19 #include <stdio.h>
20
21 /* SLP library header */
22
23 /* local header */
24 #include "Debug.h"
25 #include "FileObject.h"
26 #include "APDUHelper.h"
27
28 namespace smartcard_service_api
29 {
30         FileObject::FileObject(Channel *channel):ProviderHelper(channel)
31         {
32                 opened = false;
33         }
34
35         FileObject::FileObject(Channel *channel, ByteArray selectResponse):ProviderHelper(channel)
36         {
37                 opened = false;
38                 setSelectResponse(selectResponse);
39         }
40
41         FileObject::~FileObject()
42         {
43         }
44
45         bool FileObject::setSelectResponse(ByteArray &response)
46         {
47                 bool result = false;
48
49                 if (response.getLength() >= 2)
50                 {
51                         ResponseHelper resp(response);
52                         selectResponse = response;
53
54                         if (resp.getStatus() == 0)
55                         {
56                                 fcp.releaseFCP();
57
58                                 fcp.setFCP(resp.getDataField());
59
60                                 SCARD_DEBUG("FCP : %s", fcp.toString());
61
62                                 opened = true;
63                                 result = true;
64                         }
65                         else
66                         {
67                                 SCARD_DEBUG_ERR("status word [%d][ %02X %02X ]", resp.getStatus(), resp.getSW1(), resp.getSW2());
68                         }
69                 }
70                 else
71                 {
72                         SCARD_DEBUG_ERR("invalid response : %s", response.toString());
73                 }
74
75                 return result;
76         }
77
78         int FileObject::_select(ByteArray command)
79         {
80                 int ret = ERROR_ILLEGAL_STATE;
81                 ByteArray result;
82
83                 if (channel == NULL || channel->isClosed())
84                 {
85                         SCARD_DEBUG_ERR("channel is not open");
86
87                         return ret;
88                 }
89
90                 opened = false;
91
92                 ret = channel->transmitSync(command, result);
93                 if (ret == 0)
94                 {
95                         if (setSelectResponse(result) == true)
96                         {
97                                 ret = SUCCESS;
98                         }
99                         else
100                         {
101                                 ret = ERROR_ILLEGAL_STATE;
102                         }
103                 }
104                 else
105                 {
106                         SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, result.getLength());
107
108                         ret = ERROR_ILLEGAL_STATE;
109                 }
110
111                 return ret;
112         }
113
114         int FileObject::select(ByteArray aid)
115         {
116                 int ret = ERROR_ILLEGAL_STATE;
117                 ByteArray command;
118
119                 /* make apdu command */
120                 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_DF_NAME, 0, aid);
121                 SCARD_DEBUG("command : %s", command.toString());
122
123                 ret = _select(command);
124
125                 return ret;
126         }
127
128         int FileObject::select(ByteArray path, bool fromCurrentDF)
129         {
130                 int ret = ERROR_ILLEGAL_STATE;
131                 ByteArray command;
132
133                 /* make apdu command */
134                 if (fromCurrentDF == true)
135                 {
136                         command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_PATH_FROM_CURRENT_DF, 0, path);
137                 }
138                 else
139                 {
140                         command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_PATH, 0, path);
141                 }
142                 SCARD_DEBUG("command : %s", command.toString());
143
144                 ret = _select(command);
145
146                 return ret;
147         }
148
149         int FileObject::select(unsigned int fid)
150         {
151                 int ret = ERROR_ILLEGAL_STATE;
152                 ByteArray command, fidData((unsigned char *)&fid, 2);
153
154                 /* make apdu command */
155                 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_ID, 0, fidData);
156                 SCARD_DEBUG("command : %s", command.toString());
157
158                 ret = _select(command);
159
160                 return ret;
161         }
162
163         int FileObject::selectParent()
164         {
165                 int ret = ERROR_ILLEGAL_STATE;
166                 ByteArray command;
167
168                 /* make apdu command */
169                 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_PARENT_DF, 0, ByteArray::EMPTY);
170                 SCARD_DEBUG("command : %s", command.toString());
171
172                 ret = _select(command);
173
174                 return ret;
175         }
176
177         FCI *FileObject::getFCI()
178         {
179                 return NULL;
180         }
181
182         FCP *FileObject::getFCP()
183         {
184                 return &fcp;
185         }
186
187         int FileObject::readRecord(unsigned int sfi, unsigned int recordId, Record &result)
188         {
189                 ByteArray command, response;
190                 APDUCommand apdu;
191                 int ret;
192
193                 apdu.setCommand(0, APDUCommand::INS_READ_RECORD, recordId, 4, ByteArray::EMPTY, 0);
194                 apdu.getBuffer(command);
195                 SCARD_DEBUG("command : %s", command.toString());
196
197                 ret = channel->transmitSync(command, response);
198                 if (ret == 0 && response.getLength() >= 2)
199                 {
200                         ResponseHelper resp(response);
201
202                         if (resp.getStatus() == 0)
203                         {
204                                 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
205
206 //                              result = resp.getDataField();
207
208                                 ret = SUCCESS;
209                         }
210                         else
211                         {
212                                 SCARD_DEBUG_ERR("status word [%d][ %02X %02X ]", resp.getStatus(), resp.getSW1(), resp.getSW2());
213                         }
214                 }
215                 else
216                 {
217                         SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
218                 }
219
220                 return ret;
221         }
222
223         int FileObject::writeRecord(unsigned int sfi, Record record)
224         {
225                 return 0;
226         }
227
228         int FileObject::searchRecord(unsigned int sfi, ByteArray searchParam, vector<int> &result)
229         {
230                 return 0;
231         }
232
233         int FileObject::readBinary(unsigned int sfi, unsigned int offset, unsigned int length, ByteArray &result)
234         {
235                 ByteArray command, response;
236                 APDUCommand apdu;
237                 int ret;
238
239                 apdu.setCommand(0, APDUCommand::INS_READ_BINARY, offset, 0, ByteArray::EMPTY, length);
240                 apdu.getBuffer(command);
241                 SCARD_DEBUG("command : %s", command.toString());
242
243                 ret = channel->transmitSync(command, response);
244                 if (ret == 0 && response.getLength() >= 2)
245                 {
246                         ResponseHelper resp(response);
247
248                         if (resp.getStatus() == 0)
249                         {
250                                 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
251
252                                 result = resp.getDataField();
253
254                                 ret = SUCCESS;
255                         }
256                         else
257                         {
258                                 SCARD_DEBUG_ERR("status word [%d][ %02X %02X ]", resp.getStatus(), resp.getSW1(), resp.getSW2());
259                         }
260                 }
261                 else
262                 {
263                         SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
264                 }
265
266                 return ret;
267         }
268
269         int FileObject::writeBinary(unsigned int sfi, ByteArray data, unsigned int offset, unsigned int length)
270         {
271                 ByteArray command, response;
272                 APDUCommand apdu;
273                 int ret;
274
275                 apdu.setCommand(0, APDUCommand::INS_WRITE_BINARY, offset, 0, data, 0);
276                 apdu.getBuffer(command);
277                 SCARD_DEBUG("command : %s", command.toString());
278
279                 ret = channel->transmitSync(command, response);
280                 if (ret == 0 && response.getLength() >= 2)
281                 {
282                         ResponseHelper resp(response);
283
284                         if (resp.getStatus() == 0)
285                         {
286                                 SCARD_DEBUG("response [%d] : %s", response.getLength(), response.toString());
287
288                                 ret = SUCCESS;
289                         }
290                         else
291                         {
292                                 SCARD_DEBUG_ERR("status word [%d][ %02X %02X ]", resp.getStatus(), resp.getSW1(), resp.getSW2());
293                         }
294                 }
295                 else
296                 {
297                         SCARD_DEBUG_ERR("select apdu is failed, rv [%d], length [%d]", ret, response.getLength());
298                 }
299
300                 return ret;
301         }
302
303 } /* namespace smartcard_service_api */