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 */
27 /* SLP library header */
28 #include "package-manager.h"
33 #include "SignatureHelper.h"
34 #include "OpensslHelper.h"
37 #define EXTERN_API __attribute__((visibility("default")))
40 namespace smartcard_service_api
42 int SignatureHelper::getProcessName(int pid, char *processName, uint32_t length)
45 char buffer[1024] = { 0, };
46 char filename[2048] = { 0, };
49 if (pid < 0 || processName == NULL || length == 0)
52 snprintf(buffer, sizeof(buffer), "/proc/%d/exe", pid);
53 SCARD_DEBUG("pid : %d, exe : %s", pid, buffer);
55 if ((len = readlink(buffer, filename, sizeof(filename) - 1)) < sizeof(filename) - 1)
58 ByteArray hash, result;
60 name = basename(filename);
61 SCARD_DEBUG("file name : %s", name);
63 OpensslHelper::digestBuffer("sha256", (uint8_t *)name, strlen(name), hash);
64 SCARD_DEBUG("digest [%d] : %s", hash.getLength(), hash.toString());
66 OpensslHelper::encodeBase64String(hash, result, false);
68 memset(processName, 0, length);
69 memcpy(processName, result.getBuffer(), (result.getLength() < length - 1) ? result.getLength() : length - 1);
75 SCARD_DEBUG_ERR("readlink failed");
81 ByteArray SignatureHelper::getCertificationHash(const char *packageName)
85 pkgmgr_certinfo_h handle = NULL;
88 SCARD_DEBUG("package name : %s", packageName);
90 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
92 if ((ret = pkgmgr_pkginfo_load_certinfo(packageName, handle)) == 0)
96 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
98 const char *value = NULL;
100 if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
102 if (value != NULL && strlen(value) > 0)
104 OpensslHelper::decodeBase64String(value, result, false);
105 if (result.getLength() > 0)
107 SCARD_DEBUG("type [%d] hash [%d] : %s", type, result.getLength(), result.toString());
116 SCARD_DEBUG_ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
119 pkgmgr_pkginfo_destroy_certinfo(handle);
123 SCARD_DEBUG_ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
129 ByteArray SignatureHelper::getCertificationHash(int pid)
133 char pkgName[256] = { 0, };
135 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
137 result = getCertificationHash(pkgName);
141 SCARD_DEBUG_ERR("aul_app_get_pkgname_bypid failed [%d]", error);
147 bool SignatureHelper::getCertificationHashes(int pid, vector<ByteArray> &certHashes)
151 char pkgName[256] = { 0, };
153 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
155 result = getCertificationHashes(pkgName, certHashes);
159 SCARD_DEBUG_ERR("aul_app_get_pkgname_bypid failed [%d]", error);
165 bool SignatureHelper::getCertificationHashes(const char *packageName, vector<ByteArray> &certHashes)
169 pkgmgr_certinfo_h handle = NULL;
171 SCARD_DEBUG("package name : %s", packageName);
173 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
175 if ((ret = pkgmgr_pkginfo_load_certinfo(packageName, handle)) == 0)
179 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
181 const char *value = NULL;
183 if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
185 if (value != NULL && strlen(value) > 0)
187 ByteArray decodeValue, hash;
189 OpensslHelper::decodeBase64String(value, decodeValue, false);
190 if (decodeValue.getLength() > 0)
192 OpensslHelper::digestBuffer("sha1", decodeValue.getBuffer(), decodeValue.getLength(), hash);
193 if(hash.getLength() > 0)
195 SCARD_DEBUG("type [%d] hash [%d] : %s", type, hash.getLength(), hash.toString());
196 certHashes.push_back(hash);
207 SCARD_DEBUG_ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
210 pkgmgr_pkginfo_destroy_certinfo(handle);
214 SCARD_DEBUG_ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
219 } /* namespace smartcard_service_api */
222 using namespace smartcard_service_api;
224 certiHash *__signature_helper_vector_to_linked_list(vector<ByteArray> &certHashes)
226 vector<ByteArray>::iterator item;
227 certiHash *head, *tail, *tmp;
231 for (item = certHashes.begin(); item != certHashes.end(); item++)
233 if ((tmp = (certiHash *)calloc(1, sizeof(certiHash))) == NULL)
236 tmp->length = (*item).getLength();
238 if ((tmp->value = (uint8_t *)calloc(tmp->length, sizeof(uint8_t))) == NULL)
244 memcpy(tmp->value, (*item).getBuffer(), tmp->length);
260 SCARD_DEBUG_ERR("mem alloc fail");
266 if (tmp->value != NULL)
274 EXTERN_API int signature_helper_get_process_name(int pid, char *processName, uint32_t length)
278 if (pid < 0 || processName == NULL || length == 0)
281 ret = SignatureHelper::getProcessName(pid, processName, length);
286 EXTERN_API int signature_helper_get_certificate_hashes(const char *packageName, certiHash **hash)
289 vector<ByteArray> hashes;
291 if (packageName == NULL)
294 if (SignatureHelper::getCertificationHashes(packageName, hashes) == true)
296 *hash = __signature_helper_vector_to_linked_list(hashes);
303 EXTERN_API int signature_helper_get_certificate_hashes_by_pid(int pid, certiHash **hash)
306 vector<ByteArray> hashes;
311 if (SignatureHelper::getCertificationHashes(pid, hashes) == true)
313 *hash = __signature_helper_vector_to_linked_list(hashes);