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.
18 /* standard library header */
27 /* SLP library header */
28 #include "package-manager.h"
29 #include "pkgmgr-info.h"
34 #include "SignatureHelper.h"
35 #include "OpensslHelper.h"
38 #define EXTERN_API __attribute__((visibility("default")))
41 namespace smartcard_service_api
43 int SignatureHelper::getProcessName(int pid, char *processName, uint32_t length)
46 char buffer[1024] = { 0, };
47 char filename[2048] = { 0, };
50 if (pid < 0 || processName == NULL || length == 0)
53 snprintf(buffer, sizeof(buffer), "/proc/%d/exe", pid);
54 SCARD_DEBUG("pid : %d, exe : %s", pid, buffer);
56 if ((len = readlink(buffer, filename, sizeof(filename) - 1)) < sizeof(filename) - 1)
59 ByteArray hash, result;
61 name = basename(filename);
62 SCARD_DEBUG("file name : %s", name);
64 OpensslHelper::digestBuffer("sha256", (uint8_t *)name, strlen(name), hash);
65 SCARD_DEBUG("digest [%d] : %s", hash.getLength(), hash.toString());
67 OpensslHelper::encodeBase64String(hash, result, false);
69 memset(processName, 0, length);
70 memcpy(processName, result.getBuffer(), (result.getLength() < length - 1) ? result.getLength() : length - 1);
76 SCARD_DEBUG_ERR("readlink failed");
82 ByteArray SignatureHelper::getCertificationHash(const char *packageName)
86 pkgmgr_certinfo_h handle = NULL;
87 pkgmgrinfo_appinfo_h handle_appinfo;
90 if(pkgmgrinfo_appinfo_get_appinfo(packageName, &handle_appinfo) != PMINFO_R_OK)
92 SCARD_DEBUG_ERR("pkgmgrinfo_appinfo_get_appinfo fail");
96 if(pkgmgrinfo_appinfo_get_pkgid(handle_appinfo, &pkgid) != PMINFO_R_OK)
98 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
99 SCARD_DEBUG_ERR("pkgmgrinfo_appinfo_get_pkgid fail");
102 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
104 SCARD_DEBUG("package name : %s", pkgid);
106 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
108 if ((ret = pkgmgr_pkginfo_load_certinfo(pkgid, handle)) == 0)
112 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
114 const char *value = NULL;
116 if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
118 if (value != NULL && strlen(value) > 0)
120 OpensslHelper::decodeBase64String(value, result, false);
121 if (result.getLength() > 0)
123 SCARD_DEBUG("type [%d] hash [%d] : %s", type, result.getLength(), result.toString());
132 SCARD_DEBUG_ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
135 pkgmgr_pkginfo_destroy_certinfo(handle);
139 SCARD_DEBUG_ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
145 ByteArray SignatureHelper::getCertificationHash(int pid)
149 char pkgName[256] = { 0, };
151 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
153 result = getCertificationHash(pkgName);
157 SCARD_DEBUG_ERR("aul_app_get_pkgname_bypid failed [%d]", error);
163 bool SignatureHelper::getCertificationHashes(int pid, vector<ByteArray> &certHashes)
167 char pkgName[256] = { 0, };
169 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
171 result = getCertificationHashes(pkgName, certHashes);
175 SCARD_DEBUG_ERR("aul_app_get_pkgname_bypid failed [%d]", error);
181 bool SignatureHelper::getCertificationHashes(const char *packageName, vector<ByteArray> &certHashes)
185 pkgmgr_certinfo_h handle = NULL;
186 pkgmgrinfo_appinfo_h handle_appinfo;
189 if(pkgmgrinfo_appinfo_get_appinfo(packageName, &handle_appinfo) != PMINFO_R_OK)
191 SCARD_DEBUG_ERR("pkgmgrinfo_appinfo_get_appinfo fail");
195 if(pkgmgrinfo_appinfo_get_pkgid(handle_appinfo, &pkgid) != PMINFO_R_OK)
197 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
198 SCARD_DEBUG_ERR("pkgmgrinfo_appinfo_get_pkgid fail");
201 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
203 SCARD_DEBUG("package name : %s", pkgid);
205 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
207 if ((ret = pkgmgr_pkginfo_load_certinfo(pkgid, handle)) == 0)
211 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
213 const char *value = NULL;
215 if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
217 if (value != NULL && strlen(value) > 0)
219 ByteArray decodeValue, hash;
221 OpensslHelper::decodeBase64String(value, decodeValue, false);
222 if (decodeValue.getLength() > 0)
224 OpensslHelper::digestBuffer("sha1", decodeValue.getBuffer(), decodeValue.getLength(), hash);
225 if(hash.getLength() > 0)
227 SCARD_DEBUG("type [%d] hash [%d] : %s", type, hash.getLength(), hash.toString());
228 certHashes.push_back(hash);
239 SCARD_DEBUG_ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
242 pkgmgr_pkginfo_destroy_certinfo(handle);
246 SCARD_DEBUG_ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
251 } /* namespace smartcard_service_api */
254 using namespace smartcard_service_api;
256 certiHash *__signature_helper_vector_to_linked_list(vector<ByteArray> &certHashes)
258 vector<ByteArray>::iterator item;
259 certiHash *head, *tail, *tmp;
263 for (item = certHashes.begin(); item != certHashes.end(); item++)
265 if ((tmp = (certiHash *)calloc(1, sizeof(certiHash))) == NULL)
268 tmp->length = (*item).getLength();
270 if ((tmp->value = (uint8_t *)calloc(tmp->length, sizeof(uint8_t))) == NULL)
276 memcpy(tmp->value, (*item).getBuffer(), tmp->length);
292 SCARD_DEBUG_ERR("mem alloc fail");
298 if (tmp->value != NULL)
306 EXTERN_API int signature_helper_get_process_name(int pid, char *processName, uint32_t length)
310 if (pid < 0 || processName == NULL || length == 0)
313 ret = SignatureHelper::getProcessName(pid, processName, length);
318 EXTERN_API int signature_helper_get_certificate_hashes(const char *packageName, certiHash **hash)
321 vector<ByteArray> hashes;
323 if (packageName == NULL)
326 if (SignatureHelper::getCertificationHashes(packageName, hashes) == true)
328 *hash = __signature_helper_vector_to_linked_list(hashes);
335 EXTERN_API int signature_helper_get_certificate_hashes_by_pid(int pid, certiHash **hash)
338 vector<ByteArray> hashes;
343 if (SignatureHelper::getCertificationHashes(pid, hashes) == true)
345 *hash = __signature_helper_vector_to_linked_list(hashes);