Remove gcov rpm package
[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         /* LCOV_EXCL_START */
35         FCP::FCP()
36         {
37                 resetMemberVar();
38         }
39
40         FCP::FCP(const ByteArray &array)
41         {
42                 resetMemberVar();
43
44                 setFCP(array);
45         }
46
47         FCP::~FCP()
48         {
49         }
50
51         void FCP::resetMemberVar()
52         {
53                 fileSize = FCI::INFO_NOT_AVAILABLE;
54                 totalFileSize = FCI::INFO_NOT_AVAILABLE;
55                 fid = FCI::INFO_NOT_AVAILABLE;
56                 sfi = FCI::INFO_NOT_AVAILABLE;
57                 maxRecordSize = FCI::INFO_NOT_AVAILABLE;
58                 numberOfRecord = FCI::INFO_NOT_AVAILABLE;
59                 fileType = FCI::INFO_NOT_AVAILABLE;
60                 fileStructure = FCI::INFO_NOT_AVAILABLE;
61                 lcs = FCI::INFO_NOT_AVAILABLE;
62         }
63
64         bool FCP::setFCP(const ByteArray &array)
65         {
66                 bool result = false;
67                 SimpleTLV tlv;
68
69                 _BEGIN();
70
71                 releaseFCP();
72
73                 if (array.size() == 0)
74                         return false;
75
76                 fcpBuffer = array;
77
78                 if (fcpBuffer[0] != 0x62)
79                 {
80                         _ERR("it is not FCP response [%02X]", fcpBuffer[0]);
81                         return false;
82                 }
83
84                 /* parse... */
85                 tlv.setTLVBuffer(fcpBuffer);
86
87                 if (tlv.decodeTLV()) {
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()); //LCOV_EXCL_LINE
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()); //LCOV_EXCL_LINE
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()); //LCOV_EXCL_LINE
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()); //LCOV_EXCL_LINE
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()); //LCOV_EXCL_LINE
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()); //LCOV_EXCL_LINE
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()); //LCOV_EXCL_LINE
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()); //LCOV_EXCL_LINE
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()); //LCOV_EXCL_LINE
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()); //LCOV_EXCL_LINE
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                 } else {
278                         _ERR("tlv.decodeTLV failed");
279                 }
280
281                 _END();
282
283                 return result;
284         }
285
286         const ByteArray FCP::getFCP() const
287         {
288                 return fcpBuffer;
289         }
290
291         void FCP::releaseFCP()
292         {
293                 fcpBuffer.clear();
294
295                 resetMemberVar();
296         }
297
298         int FCP::getFileSize() const
299         {
300                 return fileSize;
301         }
302
303         int FCP::getTotalFileSize() const
304         {
305                 return totalFileSize;
306         }
307
308         int FCP::getFID() const
309         {
310                 return fid;
311         }
312
313         int FCP::getSFI() const
314         {
315                 return sfi;
316         }
317
318         int FCP::getMaxRecordSize() const
319         {
320                 return maxRecordSize;
321         }
322
323         int FCP::getNumberOfRecord() const
324         {
325                 return numberOfRecord;
326         }
327
328         int FCP::getFileType() const
329         {
330                 return fileType;
331         }
332
333         int FCP::getFileStructure() const
334         {
335                 return fileStructure;
336         }
337
338         int FCP::getLCS() const
339         {
340                 return lcs;
341         }
342
343         const string FCP::toString() const
344         {
345                 stringstream ss;
346
347                 if (fileSize != FCI::INFO_NOT_AVAILABLE)
348                         ss << "size [" << fileSize << "], ";
349
350                 if (totalFileSize != FCI::INFO_NOT_AVAILABLE)
351                         ss << "total size [" << totalFileSize << "], ";
352
353                 if (fid != FCI::INFO_NOT_AVAILABLE)
354                         ss << "fid [" << fid << "], ";
355
356                 if (sfi != FCI::INFO_NOT_AVAILABLE)
357                         ss << "sfi [" << sfi << "], ";
358
359                 if (maxRecordSize != FCI::INFO_NOT_AVAILABLE)
360                         ss << "max rec. [" << maxRecordSize << "], ";
361
362                 if (numberOfRecord != FCI::INFO_NOT_AVAILABLE)
363                         ss << "n of rec [" << numberOfRecord << "], ";
364
365                 if (fileType != FCI::INFO_NOT_AVAILABLE)
366                         ss << "type [" << fileType << "], ";
367
368                 if (fileStructure != FCI::INFO_NOT_AVAILABLE)
369                         ss << "struct [" << fileStructure << "], ";
370
371                 if (lcs != FCI::INFO_NOT_AVAILABLE)
372                         ss << "lcs [" << lcs << "], ";
373
374                 return ss.str();
375         }
376
377
378         /* FCM class method */
379         FCM::FCM()
380         {
381         }
382
383         FCM::~FCM()
384         {
385         }
386
387
388         /* FCI class method */
389         FCI::FCI()
390         {
391         }
392
393         FCI::~FCI()
394         {
395         }
396
397         bool FCI::setFCIBuffer(const ByteArray &array)
398         {
399                 bool result = false;
400
401                 return result;
402         }
403
404         /* LCOV_EXCL_STOP */
405 } /* namespace smartcard_service_api */