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