2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* standard library header */
19 /* SLP library header */
23 #include "NumberStream.h"
24 #include "PKCS15CDF.h"
25 #include "SimpleTLV.h"
27 namespace smartcard_service_api
29 PKCS15CDF::PKCS15CDF(unsigned int fid, Channel *channel) :
34 if ((ret = select(fid)) >= SCARD_ERROR_OK) {
35 ByteArray cdfData, extra;
37 _DBG("response : %s", selectResponse.toString().c_str());
39 ret = readBinaryAll(0, cdfData);
40 if (ret == SCARD_ERROR_OK) {
41 _DBG("cdfData : %s", cdfData.toString().c_str());
45 _ERR("readBinary failed, [%d]", ret);
48 _ERR("select failed, [%d]", ret);
52 PKCS15CDF::PKCS15CDF(const ByteArray &path, Channel *channel) :
57 if ((ret = select(path)) >= SCARD_ERROR_OK) {
58 ByteArray cdfData, extra;
60 _DBG("response : %s", selectResponse.toString().c_str());
62 ret = readBinaryAll(0, cdfData);
63 if (ret == SCARD_ERROR_OK) {
64 _DBG("cdfData : %s", cdfData.toString().c_str());
68 _ERR("readBinary failed, [%d]", ret);
71 _ERR("select failed, [%d]", ret);
75 PKCS15CDF::~PKCS15CDF()
79 bool PKCS15CDF::parseData(const ByteArray &data)
85 while (tlv.decodeTLV())
87 CertificateType *cert;
89 _DBG("0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
91 cert = new CertificateType();
93 tlv.enterToValueTLV();
96 _DBG("Common Object Attributes");
98 /* Common Object Attributes */
99 tlv.enterToValueTLV();
100 while (tlv.decodeTLV())
102 switch (tlv.getTag())
104 case (unsigned int)0x0C : /* label : OCTET STRING */
105 buffer = (char *)tlv.getValue().getBuffer();
108 _DBG("label : %s", buffer);
109 cert->label.assign(buffer, tlv.getValue().getLength());
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);
120 _DBG("0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
124 tlv.returnToParentTLV();
129 _DBG("Common Certificate Attributes");
131 /* Common Certificate Attributes */
132 tlv.enterToValueTLV();
133 while (tlv.decodeTLV())
135 switch (tlv.getTag())
137 case (unsigned int)0x04 : /* iD : OCTET STRING */
138 _DBG("id : %s", tlv.getValue().toString().c_str());
139 cert->id = tlv.getValue();
142 case (unsigned int)0x01 : /* Authority : BOOLEAN */
143 _DBG("authority : %s", tlv.getValue().toString().c_str());
144 cert->authority = tlv.getValue()[0];
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());
161 tlv.returnToParentTLV();
163 if (tlv.decodeTLV()) {
164 _DBG(" 0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
166 tlv.returnToParentTLV();
168 tlv.returnToParentTLV();
170 tlv.returnToParentTLV();
174 _DBG("0x%02X [%d] : %s", tlv.getTag(), tlv.size(), tlv.getValue().toString().c_str());
178 tlv.returnToParentTLV();
183 _DBG("Certificate Attributes");
186 tlv.enterToValueTLV();
189 tlv.enterToValueTLV();
193 tlv.enterToValueTLV();
194 while (tlv.decodeTLV())
196 switch (tlv.getTag())
198 case (unsigned int)0x04 : /* path : OCTET STRING */
199 cert->path = tlv.getValue();
200 _DBG("path : %s", cert->path.toString().c_str());
203 case (unsigned int)0x02 : /* index : INTEGER */
204 cert->index = NumberStream::getBigEndianNumber(tlv.getValue());
205 _DBG("index : %d", cert->index);
208 case (unsigned int)0x80 : /* length : INTEGER */
209 cert->length = NumberStream::getBigEndianNumber(tlv.getValue());
210 _DBG("length : %d", cert->length);
214 tlv.returnToParentTLV();
216 FileObject file(channel);
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());
224 _ERR("readBinary failed, [%x]", result);
227 _ERR("select failed, [%x]", result);
230 tlv.returnToParentTLV();
232 tlv.returnToParentTLV();
234 tlv.returnToParentTLV();
236 listCertType.push_back(cert);
239 _INFO("listCertType.size() = %d", listCertType.size());
241 return (listCertType.size() > 0);
244 const CertificateType *PKCS15CDF::getCertificateType(int index) const
246 if (index < 0 || index >= (int)listCertType.size())
249 return listCertType[index];
251 } /* namespace smartcard_service_api */