Apply coding rule
[platform/core/connectivity/smartcard-service.git] / common / PKCS15CDF.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 "NumberStream.h"
24 #include "PKCS15CDF.h"
25 #include "SimpleTLV.h"
26
27 namespace smartcard_service_api
28 {
29         PKCS15CDF::PKCS15CDF(unsigned int fid, Channel *channel) :
30                 PKCS15Object(channel)
31         {
32                 int ret = 0;
33
34                 if ((ret = select(fid)) >= SCARD_ERROR_OK) {
35                         ByteArray cdfData, extra;
36
37                         _DBG("response : %s", selectResponse.toString().c_str());
38
39                         ret = readBinaryAll(0, cdfData);
40                         if (ret == SCARD_ERROR_OK) {
41                                 _DBG("cdfData : %s", cdfData.toString().c_str());
42
43                                 parseData(cdfData);
44                         } else {
45                                 _ERR("readBinary failed, [%d]", ret);
46                         }
47                 } else {
48                         _ERR("select failed, [%d]", ret);
49                 }
50         }
51
52         PKCS15CDF::PKCS15CDF(const ByteArray &path, Channel *channel) :
53                 PKCS15Object(channel)
54         {
55                 int ret = 0;
56
57                 if ((ret = select(path)) >= SCARD_ERROR_OK) {
58                         ByteArray cdfData, extra;
59
60                         _DBG("response : %s", selectResponse.toString().c_str());
61
62                         ret = readBinaryAll(0, cdfData);
63                         if (ret == SCARD_ERROR_OK) {
64                                 _DBG("cdfData : %s", cdfData.toString().c_str());
65
66                                 parseData(cdfData);
67                         } else {
68                                 _ERR("readBinary failed, [%d]", ret);
69                         }
70                 } else {
71                         _ERR("select failed, [%d]", ret);
72                 }
73         }
74
75         PKCS15CDF::~PKCS15CDF()
76         {
77         }
78
79         bool PKCS15CDF::parseData(const ByteArray &data)
80         {
81                 int result;
82                 char* buffer;
83                 SimpleTLV tlv(data);
84
85                 while (tlv.decodeTLV())
86                 {
87                         CertificateType *cert;
88
89                         _DBG("0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
90
91                         cert = new CertificateType();
92
93                         tlv.enterToValueTLV();
94                         if (tlv.decodeTLV())
95                         {
96                                 _DBG("Common Object Attributes");
97
98                                 /* Common Object Attributes */
99                                 tlv.enterToValueTLV();
100                                 while (tlv.decodeTLV())
101                                 {
102                                         switch (tlv.getTag())
103                                         {
104                                         case (unsigned int)0x0C : /* label : OCTET STRING */
105                                                 buffer = (char *)tlv.getValue().getBuffer();
106                                                 if(buffer != NULL)
107                                                 {
108                                                         _DBG("label : %s", buffer);
109                                                         cert->label.assign(buffer, tlv.getValue().getLength());
110                                                 }
111                                                 break;
112
113                                         case (unsigned int)0x03 : /* flags : BIT STRING */
114                                                 /* 0 : private, 1 : modifiable */
115                                                 _DBG("flag : %s", tlv.getValue()[0] ? "modifiable" : "private");
116                                                 cert->modifiable = (tlv.getValue()[0] == 1);
117                                                 break;
118
119                                         default :
120                                                 _DBG("0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
121                                                 break;
122                                         }
123                                 }
124                                 tlv.returnToParentTLV();
125                         }
126
127                         if (tlv.decodeTLV())
128                         {
129                                 _DBG("Common Certificate Attributes");
130
131                                 /* Common Certificate Attributes */
132                                 tlv.enterToValueTLV();
133                                 while (tlv.decodeTLV())
134                                 {
135                                         switch (tlv.getTag())
136                                         {
137                                         case (unsigned int)0x04 : /* iD : OCTET STRING */
138                                                 _DBG("id : %s", tlv.getValue().toString().c_str());
139                                                 cert->id = tlv.getValue();
140                                                 break;
141
142                                         case (unsigned int)0x01 : /* Authority : BOOLEAN */
143                                                 _DBG("authority : %s", tlv.getValue().toString().c_str());
144                                                 cert->authority = tlv.getValue()[0];
145                                                 break;
146
147                                         case (unsigned int)0xA1 : /* ??? : ??? */
148                                                 tlv.enterToValueTLV();
149                                                 if (tlv.decodeTLV()) {
150                                                         _DBG("    0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
151                                                         tlv.enterToValueTLV();
152                                                         if (tlv.decodeTLV()) {
153                                                                 _DBG("      0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
154                                                                 tlv.enterToValueTLV();
155                                                                 if (tlv.decodeTLV()) {
156                                                                         _DBG("        0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
157                                                                         tlv.enterToValueTLV();
158                                                                         if (tlv.decodeTLV()) {
159                                                                                 _DBG("          0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
160                                                                         }
161                                                                         tlv.returnToParentTLV();
162                                                                 }
163                                                                 if (tlv.decodeTLV()) {
164                                                                         _DBG("        0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
165                                                                 }
166                                                                 tlv.returnToParentTLV();
167                                                         }
168                                                         tlv.returnToParentTLV();
169                                                 }
170                                                 tlv.returnToParentTLV();
171                                                 break;
172
173                                         default :
174                                                 _DBG("0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
175                                                 break;
176                                         }
177                                 }
178                                 tlv.returnToParentTLV();
179                         }
180
181                         if (tlv.decodeTLV())
182                         {
183                                 _DBG("Certificate Attributes");
184
185                                 /* Path or Object */
186                                 tlv.enterToValueTLV();
187                                 if (tlv.decodeTLV())
188                                 {
189                                         tlv.enterToValueTLV();
190                                         if (tlv.decodeTLV())
191                                         {
192                                                 /* PATH */
193                                                 tlv.enterToValueTLV();
194                                                 while (tlv.decodeTLV())
195                                                 {
196                                                         switch (tlv.getTag())
197                                                         {
198                                                         case (unsigned int)0x04 : /* path : OCTET STRING */
199                                                                 cert->path = tlv.getValue();
200                                                                 _DBG("path : %s", cert->path.toString().c_str());
201                                                                 break;
202
203                                                         case (unsigned int)0x02 : /* index : INTEGER */
204                                                                 cert->index = NumberStream::getBigEndianNumber(tlv.getValue());
205                                                                 _DBG("index : %d", cert->index);
206                                                                 break;
207
208                                                         case (unsigned int)0x80 : /* length : INTEGER */
209                                                                 cert->length = NumberStream::getBigEndianNumber(tlv.getValue());
210                                                                 _DBG("length : %d", cert->length);
211                                                                 break;
212                                                         }
213                                                 }
214                                                 tlv.returnToParentTLV();
215
216                                                 FileObject file(channel);
217
218                                                 result = file.select(cert->path, true);
219                                                 if (result >= SCARD_ERROR_OK) {
220                                                         result = file.readBinary(0, cert->length, cert->certificate);
221                                                         if (result >= SCARD_ERROR_OK) {
222                                                                 _DBG("certificate[%d] : %s", cert->certificate.size(), cert->certificate.toString().c_str());
223                                                         } else {
224                                                                 _ERR("readBinary failed, [%x]", result);
225                                                         }
226                                                 } else {
227                                                         _ERR("select failed, [%x]", result);
228                                                 }
229                                         }
230                                         tlv.returnToParentTLV();
231                                 }
232                                 tlv.returnToParentTLV();
233                         }
234                         tlv.returnToParentTLV();
235
236                         listCertType.push_back(cert);
237                 }
238
239                 _INFO("listCertType.size() = %d", listCertType.size());
240
241                 return (listCertType.size() > 0);
242         }
243
244         const CertificateType *PKCS15CDF::getCertificateType(int index) const
245         {
246                 if (index < 0 || index >= (int)listCertType.size())
247                         return NULL;
248
249                 return listCertType[index];
250         }
251 } /* namespace smartcard_service_api */