Fix the Prevent problems
[platform/core/connectivity/smartcard-service.git] / common / PKCS15DODF.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 "PKCS15DODF.h"
24 #include "SimpleTLV.h"
25
26 namespace smartcard_service_api
27 {
28         PKCS15DODF::PKCS15DODF(unsigned int fid, Channel *channel) :
29                 PKCS15Object(channel)
30         {
31                 int ret = 0;
32
33                 if ((ret = select(fid)) >= SCARD_ERROR_OK)
34                 {
35                         ByteArray dodfData, extra;
36
37                         _DBG("response : %s", selectResponse.toString().c_str());
38
39                         if ((ret = readBinary(0, 0, getFCP()->getFileSize(), dodfData)) == 0)
40                         {
41                                 _DBG("odfData : %s", dodfData.toString().c_str());
42
43                                 parseData(dodfData);
44                         }
45                         else
46                         {
47                                 _ERR("readBinary failed, [%d]", ret);
48                         }
49                 }
50                 else
51                 {
52                         _ERR("select failed, [%d]", ret);
53                 }
54         }
55
56         PKCS15DODF::PKCS15DODF(const ByteArray &path, Channel *channel) :
57                 PKCS15Object(channel)
58         {
59                 int ret = 0;
60
61                 if ((ret = select(path)) >= SCARD_ERROR_OK)
62                 {
63                         ByteArray dodfData, extra;
64
65                         _DBG("response : %s", selectResponse.toString().c_str());
66
67                         if ((ret = readBinary(0, 0, getFCP()->getFileSize(), dodfData)) == 0)
68                         {
69                                 _DBG("dodfData : %s", dodfData.toString().c_str());
70
71                                 parseData(dodfData);
72                         }
73                         else
74                         {
75                                 _ERR("readBinary failed, [%d]", ret);
76                         }
77                 }
78                 else
79                 {
80                         _ERR("select failed, [%d]", ret);
81                 }
82         }
83
84         PKCS15DODF::~PKCS15DODF()
85         {
86         }
87
88         bool PKCS15DODF::parseData(const 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)0xA1 : /* CHOICE 1 : OidDO */
98                                 {
99                                         PKCS15OID oid(tlv.getValue());
100
101                                         _DBG("OID DataObject : %s", oid.getOID().toString().c_str());
102
103                                         mapOID.insert(make_pair(oid.getOID(), oid));
104                                 }
105                                 break;
106
107                         default :
108                                 _DBG("Unknown tlv : t [%X], l [%d], v %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
109                                 break;
110                         }
111                 }
112
113                 _INFO("dataList.size() = %d", mapOID.size());
114
115                 return result;
116         }
117
118         int PKCS15DODF::searchOID(const ByteArray &oid, ByteArray &data) const
119         {
120                 int result = -1;
121                 map<ByteArray, PKCS15OID>::const_iterator item;
122
123                 item = mapOID.find(oid);
124                 if (item != mapOID.end())
125                 {
126                         data = item->second.getPath();
127                         result = 0;
128                 }
129
130                 return result;
131         }
132
133 } /* namespace smartcard_service_api */