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