Fix the Prevent problems
[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.getBuffer(), fcpBuffer.size());
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         //                                      ByteArray value = tlv.getValue();
138                                         }
139                                         break;
140
141                                 case 0x85 : /* proprietary information not encoded in BER-TLV */
142                                         {
143                                                 _DBG("0x%02X : proprietary information not encoded in BER-TLV : %s", tlv.getTag(), tlv.getValue().toString().c_str());
144         //                                      ByteArray value = tlv.getValue();
145                                         }
146                                         break;
147
148                                 case 0x86 : /* Security attribute in proprietary format */
149                                         {
150         //                                      ByteArray value = tlv.getValue();
151                                         }
152                                         break;
153
154                                 case 0x87 : /* Identifier of an EF containing an extension of the file control information */
155                                         {
156                                                 _DBG("0x%02X : Identifier of an EF containing an extension of the file control information : %s", tlv.getTag(), tlv.getValue().toString().c_str());
157         //                                      ByteArray value = tlv.getValue();
158                                         }
159                                         break;
160
161                                 case 0x88 : /* Short EF identifier */
162                                         {
163                                                 _DBG("0x%02X : Short EF identifier : %s", tlv.getTag(), tlv.getValue().toString().c_str());
164
165                                                 if (tlv.size() > 0)
166                                                 {
167                                                         ByteArray value = tlv.getValue();
168
169                                                         sfi = 0;
170
171                                                         memcpy(&sfi, value.getBuffer(), value.size());
172                                                 }
173                                         }
174                                         break;
175
176                                 case 0x8A : /* life cycle status byte */
177                                         {
178                                                 _DBG("0x%02X : life cycle status byte : %s", tlv.getTag(), tlv.getValue().toString().c_str());
179                                                 if (tlv.size() > 0)
180                                                 {
181                                                         ByteArray value = tlv.getValue();
182
183                                                         lcs = 0;
184
185                                                         memcpy(&lcs, value.getBuffer(), value.size());
186                                                 }
187                                         }
188                                         break;
189
190                                 case 0x8B : /* Security attribute referencing the expanded format */
191                                         {
192         //                                      ByteArray value = tlv.getValue();
193                                         }
194                                         break;
195
196                                 case 0x8C : /* Security attribute in compact format */
197                                         {
198         //                                      ByteArray value = tlv.getValue();
199                                         }
200                                         break;
201
202                                 case 0x8D : /* Identifier of an EF containing security environment templates */
203                                         {
204         //                                      ByteArray value = tlv.getValue();
205                                         }
206                                         break;
207
208                                 case 0x8E : /* Channel security attribute */
209                                         {
210         //                                      ByteArray value = tlv.getValue();
211                                         }
212                                         break;
213
214                                 case 0xA0 : /* Security attribute template for data objects */
215                                         {
216         //                                      ByteArray value = tlv.getValue();
217                                         }
218                                         break;
219
220                                 case 0xA1 : /* Security attribute template in proprietary format */
221                                         {
222         //                                      ByteArray value = tlv.getValue();
223                                         }
224                                         break;
225
226                                 case 0xA2 : /* Template consisting of one or more pairs of data objects */
227                                         {
228                                                 _DBG("0x%02X : Template consisting of one or more pairs of data objects : %s", tlv.getTag(), tlv.getValue().toString().c_str());
229         //                                      ByteArray value = tlv.getValue();
230                                         }
231                                         break;
232
233                                 case 0xA5 : /* proprietary information encoded in BER-TLV */
234                                         {
235                                                 _DBG("0x%02X : proprietary information encoded in BER-TLV : %s", tlv.getTag(), tlv.getValue().toString().c_str());
236         //                                      ByteArray value = tlv.getValue();
237                                         }
238                                         break;
239
240                                 case 0xAB : /* Security attribute template in expanded format */
241                                         {
242         //                                      ByteArray value = tlv.getValue();
243                                         }
244                                         break;
245
246                                 case 0xAC : /* Cryptographic mechanism identifier template */
247                                         {
248                                                 _DBG("0x%02X : Cryptographic mechanism identifier template : %s", tlv.getTag(), tlv.getValue().toString().c_str());
249         //                                      ByteArray value = tlv.getValue();
250                                         }
251                                         break;
252
253                                 case 0xC6 : /* PIN status template DO */
254                                         {
255         //                                      ByteArray value = tlv.getValue();
256                                         }
257                                         break;
258
259                                 default :
260                                         {
261                                                 _DBG("0x%02X : unknown : %s", tlv.getTag(), tlv.getValue().toString().c_str());
262                                         }
263                                         break;
264                                 }
265                         }
266                         tlv.returnToParentTLV();
267                 }
268                 else
269                 {
270                         _ERR("tlv.decodeTLV failed");
271                 }
272
273                 _END();
274
275                 return result;
276         }
277
278         const ByteArray FCP::getFCP() const
279         {
280                 return fcpBuffer;
281         }
282
283         void FCP::releaseFCP()
284         {
285                 fcpBuffer.clear();
286
287                 resetMemberVar();
288         }
289
290         int FCP::getFileSize() const
291         {
292                 return fileSize;
293         }
294
295         int FCP::getTotalFileSize() const
296         {
297                 return totalFileSize;
298         }
299
300         int FCP::getFID() const
301         {
302                 return fid;
303         }
304
305         int FCP::getSFI() const
306         {
307                 return sfi;
308         }
309
310         int FCP::getMaxRecordSize() const
311         {
312                 return maxRecordSize;
313         }
314
315         int FCP::getNumberOfRecord() const
316         {
317                 return numberOfRecord;
318         }
319
320         int FCP::getFileType() const
321         {
322                 return fileType;
323         }
324
325         int FCP::getFileStructure() const
326         {
327                 return fileStructure;
328         }
329
330         int FCP::getLCS() const
331         {
332                 return lcs;
333         }
334
335         const string FCP::toString() const
336         {
337                 stringstream ss;
338
339                 if (fileSize != FCI::INFO_NOT_AVAILABLE)
340                         ss << "size [" << fileSize << "], ";
341
342                 if (totalFileSize != FCI::INFO_NOT_AVAILABLE)
343                         ss << "total size [" << totalFileSize << "], ";
344
345                 if (fid != FCI::INFO_NOT_AVAILABLE)
346                         ss << "fid [" << fid << "], ";
347
348                 if (sfi != FCI::INFO_NOT_AVAILABLE)
349                         ss << "sfi [" << sfi << "], ";
350
351                 if (maxRecordSize != FCI::INFO_NOT_AVAILABLE)
352                         ss << "max rec. [" << maxRecordSize << "], ";
353
354                 if (numberOfRecord != FCI::INFO_NOT_AVAILABLE)
355                         ss << "n of rec [" << numberOfRecord << "], ";
356
357                 if (fileType != FCI::INFO_NOT_AVAILABLE)
358                         ss << "type [" << fileType << "], ";
359
360                 if (fileStructure != FCI::INFO_NOT_AVAILABLE)
361                         ss << "struct [" << fileStructure << "], ";
362
363                 if (lcs != FCI::INFO_NOT_AVAILABLE)
364                         ss << "lcs [" << lcs << "], ";
365
366                 return ss.str();
367         }
368
369
370         /* FCM class method */
371         FCM::FCM()
372         {
373         }
374
375         FCM::~FCM()
376         {
377         }
378
379
380         /* FCI class method */
381         FCI::FCI()
382         {
383         }
384
385         FCI::~FCI()
386         {
387         }
388
389         bool FCI::setFCIBuffer(const ByteArray &array)
390         {
391                 bool result = false;
392
393                 return result;
394         }
395
396 } /* namespace smartcard_service_api */