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 */
26 /* SLP library header */
27 #include "package-manager.h"
28 #include "pkgmgr-info.h"
33 #include "SignatureHelper.h"
34 #include "OpensslHelper.h"
37 #define EXTERN_API __attribute__((visibility("default")))
40 namespace smartcard_service_api
42 int SignatureHelper::getPackageName(int pid, char *package, size_t length)
44 return aul_app_get_pkgname_bypid(pid, package, length);
47 const ByteArray SignatureHelper::getCertificationHash(const char *packageName)
51 pkgmgr_certinfo_h handle = NULL;
52 pkgmgrinfo_appinfo_h handle_appinfo;
55 if(pkgmgrinfo_appinfo_get_appinfo(packageName, &handle_appinfo) != PMINFO_R_OK)
57 _ERR("pkgmgrinfo_appinfo_get_appinfo fail");
61 if(pkgmgrinfo_appinfo_get_pkgid(handle_appinfo, &pkgid) != PMINFO_R_OK)
63 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
64 _ERR("pkgmgrinfo_appinfo_get_pkgid fail");
67 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
69 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
71 if ((ret = pkgmgr_pkginfo_load_certinfo(pkgid, handle)) == 0)
75 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
77 const char *value = NULL;
79 if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
81 if (value != NULL && strlen(value) > 0)
83 OpensslHelper::decodeBase64String(value, result, false);
84 if (result.size() > 0)
86 _DBG("type [%d] hash [%d] : %s", type, result.size(), result.toString().c_str());
95 _ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
98 pkgmgr_pkginfo_destroy_certinfo(handle);
102 _ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
108 const ByteArray SignatureHelper::getCertificationHash(int pid)
112 char pkgName[256] = { 0, };
114 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
116 result = getCertificationHash(pkgName);
120 _ERR("aul_app_get_pkgname_bypid failed [%d]", error);
126 bool SignatureHelper::getCertificationHashes(int pid, vector<ByteArray> &certHashes)
130 char pkgName[256] = { 0, };
132 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
134 result = getCertificationHashes(pkgName, certHashes);
138 _ERR("aul_app_get_pkgname_bypid failed [%d]", error);
144 bool SignatureHelper::getCertificationHashes(const char *packageName, vector<ByteArray> &certHashes)
148 pkgmgr_certinfo_h handle = NULL;
149 pkgmgrinfo_appinfo_h handle_appinfo;
152 if(pkgmgrinfo_appinfo_get_appinfo(packageName, &handle_appinfo) != PMINFO_R_OK)
154 _ERR("pkgmgrinfo_appinfo_get_appinfo fail");
158 if(pkgmgrinfo_appinfo_get_pkgid(handle_appinfo, &pkgid) != PMINFO_R_OK)
160 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
161 _ERR("pkgmgrinfo_appinfo_get_pkgid fail");
165 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
167 if ((ret = pkgmgr_pkginfo_load_certinfo(pkgid, handle)) == 0)
171 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
173 const char *value = NULL;
175 if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
177 if (value != NULL && strlen(value) > 0)
179 ByteArray decodeValue, hash;
181 OpensslHelper::decodeBase64String(value, decodeValue, false);
182 if (decodeValue.size() > 0)
184 OpensslHelper::digestBuffer("sha1", decodeValue.getBuffer(), decodeValue.size(), hash);
187 _DBG("type [%d] hash [%d] : %s", type, hash.size(), hash.toString().c_str());
188 certHashes.push_back(hash);
199 _ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
202 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
204 pkgmgr_pkginfo_destroy_certinfo(handle);
208 _ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
213 } /* namespace smartcard_service_api */
216 using namespace smartcard_service_api;
218 certiHash *__signature_helper_vector_to_linked_list(vector<ByteArray> &certHashes)
220 vector<ByteArray>::iterator item;
221 certiHash *head, *tail, *tmp;
225 for (item = certHashes.begin(); item != certHashes.end(); item++)
227 if ((tmp = (certiHash *)calloc(1, sizeof(certiHash))) == NULL)
230 tmp->length = (*item).size();
232 if ((tmp->value = (uint8_t *)calloc(tmp->length, sizeof(uint8_t))) == NULL)
238 memcpy(tmp->value, (*item).getBuffer(), tmp->length);
260 if (tmp->value != NULL)
268 EXTERN_API int signature_helper_get_certificate_hashes(const char *packageName, certiHash **hash)
271 vector<ByteArray> hashes;
273 if (packageName == NULL)
276 if (SignatureHelper::getCertificationHashes(packageName, hashes) == true)
278 *hash = __signature_helper_vector_to_linked_list(hashes);
285 EXTERN_API int signature_helper_get_certificate_hashes_by_pid(int pid, certiHash **hash)
288 vector<ByteArray> hashes;
293 if (SignatureHelper::getCertificationHashes(pid, hashes) == true)
295 *hash = __signature_helper_vector_to_linked_list(hashes);