2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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.
18 /* standard library header */
22 /* SLP library header */
23 #include "dpl/wrt-dao-ro/WrtDatabase.h"
24 #include "dpl/wrt-dao-ro/widget_dao_read_only.h"
25 #include "dpl/wrt-dao-ro/wrt_db_types.h"
26 #include "dpl/db/sql_connection.h"
31 #include "SignatureHelper.h"
32 #include "OpensslHelper.h"
35 #define EXTERN_API __attribute__((visibility("default")))
38 using namespace WrtDB;
41 namespace smartcard_service_api
43 int SignatureHelper::getProcessName(int pid, char *processName, uint32_t length)
47 char filename[1024] = { 0, };
49 if (pid < 0 || processName == NULL || length == 0)
52 snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
53 SCARD_DEBUG("pid : %d, file name : %s", pid, filename);
55 if ((file = fopen(filename, "r")) != NULL)
58 ByteArray hash, result;
61 memset(filename, 0, sizeof(filename));
62 len = fread(filename, 1, sizeof(filename) - 1, file);
65 name = basename(filename);
66 SCARD_DEBUG("file name : %s", name);
68 OpensslHelper::digestBuffer("sha256", (uint8_t *)name, strlen(name), hash);
69 SCARD_DEBUG("digest [%d] : %s", hash.getLength(), hash.toString());
71 OpensslHelper::encodeBase64String(hash, result, false);
73 memset(processName, 0, length);
74 memcpy(processName, result.getBuffer(), (result.getLength() < length - 1) ? result.getLength() : length - 1);
86 ByteArray SignatureHelper::getCertificationHash(const char *packageName)
89 list<string>::iterator item;
90 CertificateChainList certList;
92 SCARD_DEBUG("package name : %s", packageName);
96 WrtDatabase::attachToThreadRO();
98 int handle = WidgetDAOReadOnly::getHandle(DPL::FromUTF8String(packageName));
99 WidgetDAOReadOnly widget(handle);
100 certList = widget.getWidgetCertificate();
102 SCARD_DEBUG("certList.size [%d]", certList.size());
104 WrtDatabase::detachFromThread();
108 SCARD_DEBUG_ERR("exception occurs!!!");
112 if (certList.size() > 0)
118 for (item = certList.begin(); item != certList.end(); item++)
120 SCARD_DEBUG("certList : %s", item->data());
123 certString = certList.back();
124 SCARD_DEBUG("certString[%d] :\n%s", certString.size(), certString.data());
126 /* base64 decoding */
127 if (OpensslHelper::decodeBase64String(certString.data(), certArray) == true)
129 int count = 0, offset = 0, length;
133 SCARD_DEBUG("decoded[%d] : %s", certArray.getLength(), certArray.toString());
136 count = *(int *)certArray.getBuffer();
137 offset += sizeof(int);
138 SCARD_DEBUG("certificate count [%d]", count);
140 // for (i = 0; i < count; i++)
143 /* certificate length */
144 length = *(int *)certArray.getBuffer(offset);
145 offset += sizeof(int);
146 SCARD_DEBUG("certificate length [%d]", length);
148 /* certificate byte stream */
149 cert.setBuffer(certArray.getBuffer(offset), length);
152 SCARD_DEBUG("certificate buffer [%d] : %s", cert.getLength(), cert.toString());
155 if (OpensslHelper::digestBuffer("sha1", cert, result) == true)
157 SCARD_DEBUG("digest[%d] : %s", result.getLength(), result.toString());
161 SCARD_DEBUG_ERR("digestBuffer failed");
166 SCARD_DEBUG_ERR("invalid certificate count [%d]", count);
171 SCARD_DEBUG_ERR("decodeBase64String failed");
176 SCARD_DEBUG_ERR("certList.size is zero");
182 ByteArray SignatureHelper::getCertificationHash(int pid)
186 char pkgName[256] = { 0, };
188 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
190 result = getCertificationHash(pkgName);
194 SCARD_DEBUG_ERR("aul_app_get_pkgname_bypid failed [%d]", error);
200 } /* namespace smartcard_service_api */
203 using namespace smartcard_service_api;
205 EXTERN_API int signature_helper_get_certificate_hash(const char *packageName, uint8_t *hash, uint32_t *length)
210 if (packageName == NULL || strlen(packageName) == 0 || hash == NULL || length == NULL || *length < 20)
213 result = SignatureHelper::getCertificationHash(packageName);
215 if (result.isEmpty() == false)
217 memcpy(hash, result.getBuffer(), (result.getLength() < *length) ? result.getLength() : *length);
218 *length = result.getLength();
230 EXTERN_API int signature_helper_get_certificate_hash_by_pid(int pid, uint8_t *hash, uint32_t *length)
235 if (pid < 0 || hash == NULL || length == NULL || *length < 20)
238 result = SignatureHelper::getCertificationHash(pid);
240 if (result.isEmpty() == false && result.getLength() < *length)
242 memcpy(hash, result.getBuffer(), result.getLength());
243 *length = result.getLength();
255 EXTERN_API int signature_helper_get_process_name(int pid, char *processName, uint32_t length)
259 if (pid < 0 || processName == NULL || length == 0)
262 ret = SignatureHelper::getProcessName(pid, processName, length);