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 ByteArray SignatureHelper::getCertificationHash(const char *packageName)
46 pkgmgr_certinfo_h handle = NULL;
47 pkgmgrinfo_appinfo_h handle_appinfo;
50 if(pkgmgrinfo_appinfo_get_appinfo(packageName, &handle_appinfo) != PMINFO_R_OK)
52 _ERR("pkgmgrinfo_appinfo_get_appinfo fail");
56 if(pkgmgrinfo_appinfo_get_pkgid(handle_appinfo, &pkgid) != PMINFO_R_OK)
58 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
59 _ERR("pkgmgrinfo_appinfo_get_pkgid fail");
62 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
64 SCARD_DEBUG("package name : %s", pkgid);
66 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
68 if ((ret = pkgmgr_pkginfo_load_certinfo(pkgid, handle)) == 0)
72 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
74 const char *value = NULL;
76 if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
78 if (value != NULL && strlen(value) > 0)
80 OpensslHelper::decodeBase64String(value, result, false);
81 if (result.getLength() > 0)
83 _DBG("type [%d] hash [%d] : %s", type, result.getLength(), result.toString());
92 _ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
95 pkgmgr_pkginfo_destroy_certinfo(handle);
99 _ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
105 ByteArray SignatureHelper::getCertificationHash(int pid)
109 char pkgName[256] = { 0, };
111 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
113 result = getCertificationHash(pkgName);
117 _ERR("aul_app_get_pkgname_bypid failed [%d]", error);
123 bool SignatureHelper::getCertificationHashes(int pid, vector<ByteArray> &certHashes)
127 char pkgName[256] = { 0, };
129 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
131 result = getCertificationHashes(pkgName, certHashes);
135 _ERR("aul_app_get_pkgname_bypid failed [%d]", error);
141 bool SignatureHelper::getCertificationHashes(const char *packageName, vector<ByteArray> &certHashes)
145 pkgmgr_certinfo_h handle = NULL;
146 pkgmgrinfo_appinfo_h handle_appinfo;
149 if(pkgmgrinfo_appinfo_get_appinfo(packageName, &handle_appinfo) != PMINFO_R_OK)
151 _ERR("pkgmgrinfo_appinfo_get_appinfo fail");
155 if(pkgmgrinfo_appinfo_get_pkgid(handle_appinfo, &pkgid) != PMINFO_R_OK)
157 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
158 _ERR("pkgmgrinfo_appinfo_get_pkgid fail");
162 SCARD_DEBUG("package name : %s", pkgid);
164 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
166 if ((ret = pkgmgr_pkginfo_load_certinfo(pkgid, handle)) == 0)
170 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
172 const char *value = NULL;
174 if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
176 if (value != NULL && strlen(value) > 0)
178 ByteArray decodeValue, hash;
180 OpensslHelper::decodeBase64String(value, decodeValue, false);
181 if (decodeValue.getLength() > 0)
183 OpensslHelper::digestBuffer("sha1", decodeValue.getBuffer(), decodeValue.getLength(), hash);
184 if(hash.getLength() > 0)
186 _DBG("type [%d] hash [%d] : %s", type, hash.getLength(), hash.toString());
187 certHashes.push_back(hash);
198 _ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
201 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
203 pkgmgr_pkginfo_destroy_certinfo(handle);
207 _ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
212 } /* namespace smartcard_service_api */
215 using namespace smartcard_service_api;
217 certiHash *__signature_helper_vector_to_linked_list(vector<ByteArray> &certHashes)
219 vector<ByteArray>::iterator item;
220 certiHash *head, *tail, *tmp;
224 for (item = certHashes.begin(); item != certHashes.end(); item++)
226 if ((tmp = (certiHash *)calloc(1, sizeof(certiHash))) == NULL)
229 tmp->length = (*item).getLength();
231 if ((tmp->value = (uint8_t *)calloc(tmp->length, sizeof(uint8_t))) == NULL)
237 memcpy(tmp->value, (*item).getBuffer(), tmp->length);
259 if (tmp->value != NULL)
267 EXTERN_API int signature_helper_get_certificate_hashes(const char *packageName, certiHash **hash)
270 vector<ByteArray> hashes;
272 if (packageName == NULL)
275 if (SignatureHelper::getCertificationHashes(packageName, hashes) == true)
277 *hash = __signature_helper_vector_to_linked_list(hashes);
284 EXTERN_API int signature_helper_get_certificate_hashes_by_pid(int pid, certiHash **hash)
287 vector<ByteArray> hashes;
292 if (SignatureHelper::getCertificationHashes(pid, hashes) == true)
294 *hash = __signature_helper_vector_to_linked_list(hashes);