253f05ef50015014d4773d1c9dd26524b544a73d
[platform/core/connectivity/smartcard-service.git] / common / FCI.cpp
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 /* standard library header */
18 #include <cstdio>
19 #include <cstring>
20 #include <sstream>
21
22 /* SLP library header */
23
24 /* local header */
25 #include "Debug.h"
26 #include "FCI.h"
27 #include "SimpleTLV.h"
28 //#include "ISO7816BERTLV.h"
29 #include "NumberStream.h"
30
31 namespace smartcard_service_api
32 {
33         /* FCP class method */
34         FCP::FCP()
35         {
36                 resetMemberVar();
37         }
38
39         FCP::FCP(const ByteArray &array)
40         {
41                 resetMemberVar();
42
43                 setFCP(array);
44         }
45
46         FCP::~FCP()
47         {
48         }
49
50         void FCP::resetMemberVar()
51         {
52                 fileSize = FCI::INFO_NOT_AVAILABLE;
53                 totalFileSize = FCI::INFO_NOT_AVAILABLE;
54                 fid = FCI::INFO_NOT_AVAILABLE;
55                 sfi = FCI::INFO_NOT_AVAILABLE;
56                 maxRecordSize = FCI::INFO_NOT_AVAILABLE;
57                 numberOfRecord = FCI::INFO_NOT_AVAILABLE;
58                 fileType = FCI::INFO_NOT_AVAILABLE;
59                 fileStructure = FCI::INFO_NOT_AVAILABLE;
60                 lcs = FCI::INFO_NOT_AVAILABLE;
61         }
62
63         bool FCP::setFCP(const ByteArray &array)
64         {
65                 bool result = false;
66                 SimpleTLV tlv;
67
68                 _BEGIN();
69
70                 releaseFCP();
71
72                 if (array.size() == 0)
73                         return false;
74
75                 fcpBuffer = array;
76
77                 if (fcpBuffer[0] != 0x62)
78                 {
79                         _ERR("it is not FCP response [%02X]", fcpBuffer[0]);
80                         return false;
81                 }
82
83                 /* parse... */
84                 tlv.setTLVBuffer(fcpBuffer);
85
86                 if (tlv.decodeTLV())
87                 {
88                         tlv.enterToValueTLV();
89
90                         while (tlv.decodeTLV())
91                         {
92                                 switch (tlv.getTag())
93                                 {
94                                 case 0x80 : /* file length without structural information */
95                                         {
96                                                 _DBG("0x%02X : file length without structural information : %s", tlv.getTag(), tlv.getValue().toString().c_str());
97                                                 if (tlv.size() > 0)
98                                                 {
99                                                         fileSize = NumberStream::getBigEndianNumber(tlv.getValue());
100                                                 }
101                                         }
102                                         break;
103
104                                 case 0x81 : /* file length with structural information */
105                                         {
106                                                 _DBG("0x%02X : file length with structural information : %s", tlv.getTag(), tlv.getValue().toString().c_str());
107                                                 if (tlv.size() > 0)
108                                                 {
109                                                         maxRecordSize = NumberStream::getBigEndianNumber(tlv.getValue());
110                                                 }
111                                         }
112                                         break;
113
114                                 case 0x82 : /* file descriptor bytes */
115                                         {
116                                                 _DBG("0x%02X : file descriptor bytes : %s", tlv.getTag(), tlv.getValue().toString().c_str());
117         //                                      ByteArray value = tlv.getValue();
118                                         }
119                                         break;
120
121                                 case 0x83 : /* file identifier */
122                                         {
123                                                 _DBG("0x%02X : file identifier : %s", tlv.getTag(), tlv.getValue().toString().c_str());
124                                                 if (tlv.size() > 0)
125                                                 {
126                                                         ByteArray value = tlv.getValue();
127
128                                                         fid = 0;
129
130                                                         memcpy(&fid, value.getBuffer(), value.size());
131                                                 }
132                                         }
133                                         break;
134
135                                 case 0x84 : /* DF name */
136                                         {
137                                                 SECURE_LOGD("0x%02X : DF name : %s", tlv.getTag(), tlv.getValue().toString().c_str());
138         //                                      ByteArray value = tlv.getValue();
139                                         }
140                                         break;
141
142                                 case 0x85 : /* proprietary information not encoded in BER-TLV */
143                                         {
144                                                 _DBG("0x%02X : proprietary information not encoded in BER-TLV : %s", tlv.getTag(), tlv.getValue().toString().c_str());
145         //                                      ByteArray value = tlv.getValue();
146                                         }
147                                         break;
148
149                                 case 0x86 : /* Security attribute in proprietary format */
150                                         {
151                                                 SECURE_LOGD("0x%02X : Security attribute in proprietary format : %s", tlv.getTag(), tlv.getValue().toString().c_str());
152         //                                      ByteArray value = tlv.getValue();
153                                         }
154                                         break;
155
156                                 case 0x87 : /* Identifier of an EF containing an extension of the file control information */
157                                         {
158                                                 _DBG("0x%02X : Identifier of an EF containing an extension of the file control information : %s", tlv.getTag(), tlv.getValue().toString().c_str());
159         //                                      ByteArray value = tlv.getValue();
160                                         }
161                                         break;
162
163                                 case 0x88 : /* Short EF identifier */
164                                         {
165                                                 _DBG("0x%02X : Short EF identifier : %s", tlv.getTag(), tlv.getValue().toString().c_str());
166
167                                                 if (tlv.size() > 0)
168                                                 {
169                                                         ByteArray value = tlv.getValue();
170
171                                                         sfi = 0;
172
173                                                         memcpy(&sfi, value.getBuffer(), value.size());
174                                                 }
175                                         }
176                                         break;
177
178                                 case 0x8A : /* life cycle status byte */
179                                         {
180                                                 _DBG("0x%02X : life cycle status byte : %s", tlv.getTag(), tlv.getValue().toString().c_str());
181                                                 if (tlv.size() > 0)
182                                                 {
183                                                         ByteArray value = tlv.getValue();
184
185                                                         lcs = 0;
186
187                                                         memcpy(&lcs, value.getBuffer(), value.size());
188                                                 }
189                                         }
190                                         break;
191
192                                 case 0x8B : /* Security attribute referencing the expanded format */
193                                         {
194                                                 SECURE_LOGD("0x%02X : Security attribute referencing the expanded format : %s", tlv.getTag(), tlv.getValue().toString().c_str());
195         //                                      ByteArray value = tlv.getValue();
196                                         }
197                                         break;
198
199                                 case 0x8C : /* Security attribute in compact format */
200                                         {
201                                                 SECURE_LOGD("0x%02X : Security attribute in compact format : %s", tlv.getTag(), tlv.getValue().toString().c_str());
202         //                                      ByteArray value = tlv.getValue();
203                                         }
204                                         break;
205
206                                 case 0x8D : /* Identifier of an EF containing security environment templates */
207                                         {
208                                                 SECURE_LOGD("0x%02X : Identifier of an EF containing security environment templates : %s", tlv.getTag(), tlv.getValue().toString().c_str());
209         //                                      ByteArray value = tlv.getValue();
210                                         }
211                                         break;
212
213                                 case 0x8E : /* Channel security attribute */
214                                         {
215                                                 SECURE_LOGD("0x%02X : Channel security attribute : %s", tlv.getTag(), tlv.getValue().toString().c_str());
216         //                                      ByteArray value = tlv.getValue();
217                                         }
218                                         break;
219
220                                 case 0xA0 : /* Security attribute template for data objects */
221                                         {
222                                                 SECURE_LOGD("0x%02X : Security attribute template for data objects : %s", tlv.getTag(), tlv.getValue().toString().c_str());
223         //                                      ByteArray value = tlv.getValue();
224                                         }
225                                         break;
226
227                                 case 0xA1 : /* Security attribute template in proprietary format */
228                                         {
229                                                 SECURE_LOGD("0x%02X : Security attribute template in proprietary format : %s", tlv.getTag(), tlv.getValue().toString().c_str());
230         //                                      ByteArray value = tlv.getValue();
231                                         }
232                                         break;
233
234                                 case 0xA2 : /* Template consisting of one or more pairs of data objects */
235                                         {
236                                                 _DBG("0x%02X : Template consisting of one or more pairs of data objects : %s", tlv.getTag(), tlv.getValue().toString().c_str());
237         //                                      ByteArray value = tlv.getValue();
238                                         }
239                                         break;
240
241                                 case 0xA5 : /* proprietary information encoded in BER-TLV */
242                                         {
243                                                 _DBG("0x%02X : proprietary information encoded in BER-TLV : %s", tlv.getTag(), tlv.getValue().toString().c_str());
244         //                                      ByteArray value = tlv.getValue();
245                                         }
246                                         break;
247
248                                 case 0xAB : /* Security attribute template in expanded format */
249                                         {
250                                                 SECURE_LOGD("0x%02X : Security attribute template in expanded format : %s", tlv.getTag(), tlv.getValue().toString().c_str());
251         //                                      ByteArray value = tlv.getValue();
252                                         }
253                                         break;
254
255                                 case 0xAC : /* Cryptographic mechanism identifier template */
256                                         {
257                                                 _DBG("0x%02X : Cryptographic mechanism identifier template : %s", tlv.getTag(), tlv.getValue().toString().c_str());
258         //                                      ByteArray value = tlv.getValue();
259                                         }
260                                         break;
261
262                                 case 0xC6 : /* PIN status template DO */
263                                         {
264                                                 SECURE_LOGD("0x%02X : PIN status template DO : %s", tlv.getTag(), tlv.getValue().toString().c_str());
265         //                                      ByteArray value = tlv.getValue();
266                                         }
267                                         break;
268
269                                 default :
270                                         {
271                                                 _DBG("0x%02X : unknown : %s", tlv.getTag(), tlv.getValue().toString().c_str());
272                                         }
273                                         break;
274                                 }
275                         }
276                         tlv.returnToParentTLV();
277                 }
278                 else
279                 {
280                         _ERR("tlv.decodeTLV failed");
281                 }
282
283                 _END();
284
285                 return result;
286         }
287
288         const ByteArray FCP::getFCP() const
289         {
290                 return fcpBuffer;
291         }
292
293         void FCP::releaseFCP()
294         {
295                 fcpBuffer.clear();
296
297                 resetMemberVar();
298         }
299
300         int FCP::getFileSize() const
301         {
302                 return fileSize;
303         }
304
305         int FCP::getTotalFileSize() const
306         {
307                 return totalFileSize;
308         }
309
310         int FCP::getFID() const
311         {
312                 return fid;
313         }
314
315         int FCP::getSFI() const
316         {
317                 return sfi;
318         }
319
320         int FCP::getMaxRecordSize() const
321         {
322                 return maxRecordSize;
323         }
324
325         int FCP::getNumberOfRecord() const
326         {
327                 return numberOfRecord;
328         }
329
330         int FCP::getFileType() const
331         {
332                 return fileType;
333         }
334
335         int FCP::getFileStructure() const
336         {
337                 return fileStructure;
338         }
339
340         int FCP::getLCS() const
341         {
342                 return lcs;
343         }
344
345         const string FCP::toString() const
346         {
347                 stringstream ss;
348
349                 if (fileSize != FCI::INFO_NOT_AVAILABLE)
350                         ss << "size [" << fileSize << "], ";
351
352                 if (totalFileSize != FCI::INFO_NOT_AVAILABLE)
353                         ss << "total size [" << totalFileSize << "], ";
354
355                 if (fid != FCI::INFO_NOT_AVAILABLE)
356                         ss << "fid [" << fid << "], ";
357
358                 if (sfi != FCI::INFO_NOT_AVAILABLE)
359                         ss << "sfi [" << sfi << "], ";
360
361                 if (maxRecordSize != FCI::INFO_NOT_AVAILABLE)
362                         ss << "max rec. [" << maxRecordSize << "], ";
363
364                 if (numberOfRecord != FCI::INFO_NOT_AVAILABLE)
365                         ss << "n of rec [" << numberOfRecord << "], ";
366
367                 if (fileType != FCI::INFO_NOT_AVAILABLE)
368                         ss << "type [" << fileType << "], ";
369
370                 if (fileStructure != FCI::INFO_NOT_AVAILABLE)
371                         ss << "struct [" << fileStructure << "], ";
372
373                 if (lcs != FCI::INFO_NOT_AVAILABLE)
374                         ss << "lcs [" << lcs << "], ";
375
376                 return ss.str();
377         }
378
379
380         /* FCM class method */
381         FCM::FCM()
382         {
383         }
384
385         FCM::~FCM()
386         {
387         }
388
389
390         /* FCI class method */
391         FCI::FCI()
392         {
393         }
394
395         FCI::~FCI()
396         {
397         }
398
399         bool FCI::setFCIBuffer(const ByteArray &array)
400         {
401                 bool result = false;
402
403                 return result;
404         }
405
406 } /* namespace smartcard_service_api */