update the latest source
[platform/core/connectivity/smartcard-service.git] / common / PKCS15ODF.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
18 /* standard library header */
19
20 /* SLP library header */
21
22 /* local header */
23 #include "Debug.h"
24 #include "PKCS15ODF.h"
25 #include "SimpleTLV.h"
26 #include "NumberStream.h"
27
28 namespace smartcard_service_api
29 {
30 //      PKCS15ODF::PKCS15ODF():PKCS15Object()
31 //      {
32 //
33 //      }
34
35         PKCS15ODF::PKCS15ODF(Channel *channel):PKCS15Object(channel), dodf(NULL)
36         {
37                 int ret = 0;
38
39                 if ((ret = select(PKCS15ODF::ODF_FID)) == 0)
40                 {
41                         ByteArray odfData, extra;
42
43                         SCARD_DEBUG("response : %s", selectResponse.toString());
44
45                         if ((ret = readBinary(0, 0, getFCP()->getFileSize(), odfData)) == 0)
46                         {
47                                 SCARD_DEBUG("odfData : %s", odfData.toString());
48
49                                 parseData(odfData);
50                         }
51                         else
52                         {
53                                 SCARD_DEBUG_ERR("readBinary failed, [%d]", ret);
54                         }
55                 }
56                 else
57                 {
58                         SCARD_DEBUG_ERR("select failed, [%d]", ret);
59                 }
60         }
61
62         PKCS15ODF::PKCS15ODF(Channel *channel, ByteArray selectResponse):PKCS15Object(channel, selectResponse), dodf(NULL)
63         {
64                 int ret = 0;
65                 ByteArray odfData;
66
67                 if ((ret = readBinary(0, 0, 0, odfData)) == 0)
68                 {
69                         SCARD_DEBUG("odfData : %s", odfData.toString());
70
71                         parseData(odfData);
72                 }
73                 else
74                 {
75                         SCARD_DEBUG_ERR("readBinary failed, [%d]", ret);
76                 }
77         }
78
79         PKCS15ODF::~PKCS15ODF()
80         {
81                 if (dodf != NULL)
82                 {
83                         delete dodf;
84                         dodf = NULL;
85                 }
86         }
87
88         bool PKCS15ODF::parseData(ByteArray data)
89         {
90                 bool result = false;
91                 SimpleTLV tlv(data);
92
93                 while (tlv.decodeTLV())
94                 {
95                         switch (tlv.getTag())
96                         {
97                         case (unsigned int)0xA7 ://PKCS15ODF::TAG_DODF :
98                                 {
99                                         ByteArray dodf;
100
101                                         SCARD_DEBUG("TAG_DODF");
102
103                                         dodf = PKCS15Object::getOctetStream(tlv.getValue());
104
105                                         SCARD_DEBUG("path : %s", dodf.toString());
106
107                                         pair<unsigned int, ByteArray> newPair(tlv.getTag(), dodf);
108                                         dataList.insert(newPair);
109                                 }
110                                 break;
111
112                         case (unsigned int)0xA5 ://PKCS15ODF::TAG_TOKENINFO :
113                                 {
114                                         ByteArray tokeninfo;
115
116                                         SCARD_DEBUG("TAG_TOKENINFO");
117
118                                         tokeninfo = PKCS15Object::getOctetStream(tlv.getValue());
119
120                                         SCARD_DEBUG("path : %s", tokeninfo.toString());
121
122                                         pair<unsigned int, ByteArray> newPair(tlv.getTag(), tokeninfo);
123                                         dataList.insert(newPair);
124                                 }
125                                 break;
126
127                         default :
128                                 SCARD_DEBUG("Unknown tlv : t [%X], l [%d], v %s", tlv.getTag(), tlv.getLength(), tlv.getValue().toString());
129                                 break;
130                         }
131
132                 }
133
134                 SCARD_DEBUG("dataList.size() = %d", dataList.size());
135
136                 return result;
137         }
138
139         PKCS15DODF *PKCS15ODF::getDODF()
140         {
141                 map<unsigned int, ByteArray>::iterator item;
142
143                 if (dodf == NULL)
144                 {
145                         item = dataList.find((unsigned int)0xA7/*PKCS15ODF::TAG_DODF*/);
146                         if (item != dataList.end())
147                         {
148                                 NumberStream num(item->second);
149                                 unsigned int fid = num.getLittleEndianNumber();
150
151                                 SCARD_DEBUG("fid [%X]", fid);
152
153                                 dodf = new PKCS15DODF(fid, channel);
154                         }
155                         else
156                         {
157                                 SCARD_DEBUG_ERR("[%02X] is not found. total [%d]", TAG_DODF, dataList.size());
158                         }
159                 }
160
161                 SCARD_DEBUG("dodf [%p]", dodf);
162
163                 return dodf;
164         }
165
166 } /* namespace smartcard_service_api */