update the latest source
[platform/core/connectivity/smartcard-service.git] / common / PKCS15DODF.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
18 /* standard library header */
19
20 /* SLP library header */
21
22 /* local header */
23 #include "Debug.h"
24 #include "PKCS15DODF.h"
25 #include "SimpleTLV.h"
26
27 namespace smartcard_service_api
28 {
29 //      PKCS15DODF::PKCS15DODF():PKCS15Object()
30 //      {
31 //
32 //      }
33
34         PKCS15DODF::PKCS15DODF(unsigned int fid, Channel *channel):PKCS15Object(channel)
35         {
36                 int ret = 0;
37
38                 if ((ret = select(fid)) == 0)
39                 {
40                         ByteArray dodfData, extra;
41
42                         SCARD_DEBUG("response : %s", selectResponse.toString());
43
44                         if ((ret = readBinary(0, 0, getFCP()->getFileSize(), dodfData)) == 0)
45                         {
46                                 SCARD_DEBUG("odfData : %s", dodfData.toString());
47
48                                 parseData(dodfData);
49                         }
50                         else
51                         {
52                                 SCARD_DEBUG_ERR("readBinary failed, [%d]", ret);
53                         }
54                 }
55                 else
56                 {
57                         SCARD_DEBUG_ERR("select failed, [%d]", ret);
58                 }
59         }
60
61         PKCS15DODF::PKCS15DODF(ByteArray path, Channel *channel):PKCS15Object(channel)
62         {
63                 int ret = 0;
64
65                 if ((ret = select(path)) == 0)
66                 {
67                         ByteArray dodfData, extra;
68
69                         SCARD_DEBUG("response : %s", selectResponse.toString());
70
71                         if ((ret = readBinary(0, 0, getFCP()->getFileSize(), dodfData)) == 0)
72                         {
73                                 SCARD_DEBUG("dodfData : %s", dodfData.toString());
74
75                                 parseData(dodfData);
76                         }
77                         else
78                         {
79                                 SCARD_DEBUG_ERR("readBinary failed, [%d]", ret);
80                         }
81                 }
82                 else
83                 {
84                         SCARD_DEBUG_ERR("select failed, [%d]", ret);
85                 }
86         }
87
88         PKCS15DODF::~PKCS15DODF()
89         {
90         }
91
92         bool PKCS15DODF::parseData(ByteArray data)
93         {
94                 bool result = false;
95                 SimpleTLV tlv(data);
96
97                 while (tlv.decodeTLV())
98                 {
99                         switch (tlv.getTag())
100                         {
101                         case (unsigned int)0xA1 : /* CHOICE 1 : OidDO */
102                                 {
103                                         PKCS15OID oid(tlv.getValue());
104
105                                         SCARD_DEBUG("OID DataObject");
106
107                                         pair<ByteArray, PKCS15OID> newPair(oid.getOID(), oid);
108                                         mapOID.insert(newPair);
109                                 }
110                                 break;
111
112                         default :
113                                 SCARD_DEBUG("Unknown tlv : t [%X], l [%d], v %s", tlv.getTag(), tlv.getLength(), tlv.getValue().toString());
114                                 break;
115                         }
116                 }
117
118                 SCARD_DEBUG("dataList.size() = %d", mapOID.size());
119
120                 return result;
121         }
122
123         int PKCS15DODF::searchOID(ByteArray oid, ByteArray &data)
124         {
125                 int result = -1;
126                 map<ByteArray, PKCS15OID>::iterator item;
127
128                 item = mapOID.find(oid);
129                 if (item != mapOID.end())
130                 {
131                         data = item->second.getPath();
132                         result = 0;
133                 }
134
135                 return result;
136         }
137
138 } /* namespace smartcard_service_api */