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