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