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