Apply coding rule
[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                         ByteArray dodfData, extra;
35
36                         _DBG("response : %s", selectResponse.toString().c_str());
37
38                         if ((ret = readBinaryAll(0, dodfData)) == 0) {
39                                 _DBG("dodfData : %s", dodfData.toString().c_str());
40
41                                 parseData(dodfData);
42                         } else {
43                                 _ERR("readBinary failed, [%d]", ret);
44                         }
45                 } else {
46                         _ERR("select failed, [%d]", ret);
47                 }
48         }
49
50         PKCS15DODF::PKCS15DODF(const ByteArray &path, Channel *channel) :
51                 PKCS15Object(channel)
52         {
53                 int ret = 0;
54
55                 if ((ret = select(path)) >= SCARD_ERROR_OK) {
56                         ByteArray dodfData, extra;
57
58                         _DBG("response : %s", selectResponse.toString().c_str());
59
60                         if ((ret = readBinaryAll(0, dodfData)) == 0) {
61                                 _DBG("dodfData : %s", dodfData.toString().c_str());
62
63                                 parseData(dodfData);
64                         } else {
65                                 _ERR("readBinary failed, [%d]", ret);
66                         }
67                 } else {
68                         _ERR("select failed, [%d]", ret);
69                 }
70         }
71
72         PKCS15DODF::~PKCS15DODF()
73         {
74         }
75
76         bool PKCS15DODF::parseData(const ByteArray &data)
77         {
78                 bool result = false;
79                 SimpleTLV tlv(data);
80
81                 while (tlv.decodeTLV()) {
82                         switch (tlv.getTag()) {
83                         case (unsigned int)0xA1 : /* CHOICE 1 : OidDO */
84                                 {
85                                         PKCS15OID oid(tlv.getValue());
86
87                                         _DBG("OID DataObject : %s", oid.getOID().toString().c_str());
88
89                                         mapOID.insert(make_pair(oid.getOID(), oid));
90                                 }
91                                 break;
92
93                         default :
94                                 _DBG("Unknown tlv : t [%X], l [%d], v %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
95                                 break;
96                         }
97                 }
98
99                 _INFO("dataList.size() = %d", mapOID.size());
100
101                 return result;
102         }
103
104         int PKCS15DODF::searchOID(const ByteArray &oid, ByteArray &data) const
105         {
106                 int result = -1;
107                 map<ByteArray, PKCS15OID>::const_iterator item;
108
109                 item = mapOID.find(oid);
110                 if (item != mapOID.end()) {
111                         data = item->second.getPath();
112                         result = 0;
113                 }
114
115                 return result;
116         }
117
118 } /* namespace smartcard_service_api */