13 #include "pkgmgr-info.h"
14 #include "pkgmgrinfo_debug.h"
15 #include "pkgmgrinfo_private.h"
16 #include "pkgmgr_parser.h"
18 typedef struct _pkgmgr_certinfo_x {
22 char *cert_info[MAX_CERT_TYPE]; /*certificate info*/
23 int cert_id[MAX_CERT_TYPE]; /*certificate ID in index table*/
26 typedef struct _pkgmgr_instcertinfo_x {
28 char *cert_info[MAX_CERT_TYPE]; /*certificate data*/
29 int is_new[MAX_CERT_TYPE]; /*whether already exist in table or not*/
30 int ref_count[MAX_CERT_TYPE]; /*reference count of certificate data*/
31 int cert_id[MAX_CERT_TYPE]; /*certificate ID in index table*/
32 } pkgmgr_instcertinfo_x;
34 typedef struct _pkgmgr_certindexinfo_x {
37 } pkgmgr_certindexinfo_x;
39 typedef struct _pkgmgr_cert_x {
44 static int __cert_cb(void *data, int ncols, char **coltxt, char **colname)
46 pkgmgr_cert_x *info = (pkgmgr_cert_x *)data;
49 for(i = 0; i < ncols; i++)
51 if (strcmp(colname[i], "author_signer_cert") == 0) {
53 info->cert_id = atoi(coltxt[i]);
56 } else if (strcmp(colname[i], "package") == 0) {
58 info->pkgid= strdup(coltxt[i]);
67 static int __certinfo_cb(void *data, int ncols, char **coltxt, char **colname)
69 pkgmgr_certinfo_x *info = (pkgmgr_certinfo_x *)data;
71 for(i = 0; i < ncols; i++)
73 if (strcmp(colname[i], "package") == 0) {
75 info->pkgid = strdup(coltxt[i]);
78 } else if (strcmp(colname[i], "author_signer_cert") == 0) {
80 (info->cert_id)[PMINFO_AUTHOR_SIGNER_CERT] = atoi(coltxt[i]);
82 (info->cert_id)[PMINFO_AUTHOR_SIGNER_CERT] = 0;
83 } else if (strcmp(colname[i], "author_im_cert") == 0) {
85 (info->cert_id)[PMINFO_AUTHOR_INTERMEDIATE_CERT] = atoi(coltxt[i]);
87 (info->cert_id)[PMINFO_AUTHOR_INTERMEDIATE_CERT] = 0;
88 } else if (strcmp(colname[i], "author_root_cert") == 0) {
90 (info->cert_id)[PMINFO_AUTHOR_ROOT_CERT] = atoi(coltxt[i]);
92 (info->cert_id)[PMINFO_AUTHOR_ROOT_CERT] = 0;
93 } else if (strcmp(colname[i], "dist_signer_cert") == 0 ){
95 (info->cert_id)[PMINFO_DISTRIBUTOR_SIGNER_CERT] = atoi(coltxt[i]);
97 (info->cert_id)[PMINFO_DISTRIBUTOR_SIGNER_CERT] = 0;
98 } else if (strcmp(colname[i], "dist_im_cert") == 0 ){
100 (info->cert_id)[PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT] = atoi(coltxt[i]);
102 (info->cert_id)[PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT] = 0;
103 } else if (strcmp(colname[i], "dist_root_cert") == 0 ){
105 (info->cert_id)[PMINFO_DISTRIBUTOR_ROOT_CERT] = atoi(coltxt[i]);
107 (info->cert_id)[PMINFO_DISTRIBUTOR_ROOT_CERT] = 0;
108 } else if (strcmp(colname[i], "dist2_signer_cert") == 0 ){
110 (info->cert_id)[PMINFO_DISTRIBUTOR2_SIGNER_CERT] = atoi(coltxt[i]);
112 (info->cert_id)[PMINFO_DISTRIBUTOR2_SIGNER_CERT] = 0;
113 } else if (strcmp(colname[i], "dist2_im_cert") == 0 ){
115 (info->cert_id)[PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT] = atoi(coltxt[i]);
117 (info->cert_id)[PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT] = 0;
118 } else if (strcmp(colname[i], "dist2_root_cert") == 0 ){
120 (info->cert_id)[PMINFO_DISTRIBUTOR2_ROOT_CERT] = atoi(coltxt[i]);
122 (info->cert_id)[PMINFO_DISTRIBUTOR2_ROOT_CERT] = 0;
123 } else if (strcmp(colname[i], "cert_info") == 0 ){
125 info->cert_value = strdup(coltxt[i]);
127 info->cert_value = NULL;
128 } else if (strcmp(colname[i], "for_all_users") == 0 ){
130 info->for_all_users = atoi(coltxt[i]);
132 info->for_all_users = 0;
139 static int __exec_certinfo_query(char *query, void *data)
141 char *error_message = NULL;
143 sqlite3_exec(GET_DB(cert_db), query, __certinfo_cb, data, &error_message)) {
144 _LOGE("Don't execute query = %s error message = %s\n", query,
146 sqlite3_free(error_message);
149 sqlite3_free(error_message);
153 static int __certindexinfo_cb(void *data, int ncols, char **coltxt, char **colname)
155 pkgmgr_certindexinfo_x *info = (pkgmgr_certindexinfo_x *)data;
157 for(i = 0; i < ncols; i++) {
158 if (strcmp(colname[i], "cert_id") == 0) {
160 info->cert_id = atoi(coltxt[i]);
163 } else if (strcmp(colname[i], "cert_ref_count") == 0) {
165 info->cert_ref_count = atoi(coltxt[i]);
167 info->cert_ref_count = 0;
174 static int __exec_certindexinfo_query(char *query, void *data)
176 char *error_message = NULL;
178 sqlite3_exec(GET_DB(cert_db), query, __certindexinfo_cb, data, &error_message)) {
179 _LOGE("Don't execute query = %s error message = %s\n", query,
181 sqlite3_free(error_message);
184 sqlite3_free(error_message);
188 static int __delete_certinfo(const char *pkgid, uid_t uid)
194 int unique_id[MAX_CERT_TYPE] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
195 char *error_message = NULL;
196 char query[MAX_QUERY_LEN] = {'\0'};
197 pkgmgr_certinfo_x *certinfo = NULL;
198 pkgmgr_certindexinfo_x *indexinfo = NULL;
199 certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
200 retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
201 indexinfo = calloc(1, sizeof(pkgmgr_certindexinfo_x));
202 if (indexinfo == NULL) {
203 _LOGE("Out of Memory!!!");
204 ret = PMINFO_R_ERROR;
208 __open_cert_db(uid, false);
209 /*populate certinfo from DB*/
210 snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid);
211 ret = __exec_certinfo_query(query, (void *)certinfo);
213 _LOGE("Package Cert Info DB Information retrieval failed\n");
214 ret = PMINFO_R_ERROR;
217 /*Update cert index table*/
218 for (i = 0; i < MAX_CERT_TYPE; i++) {
219 if ((certinfo->cert_id)[i]) {
220 for (j = 0; j < MAX_CERT_TYPE; j++) {
221 if ((certinfo->cert_id)[i] == unique_id[j]) {
222 /*Ref count has already been updated. Just continue*/
226 if (j == MAX_CERT_TYPE)
227 unique_id[c++] = (certinfo->cert_id)[i];
230 memset(query, '\0', MAX_QUERY_LEN);
231 snprintf(query, MAX_QUERY_LEN, "select * from package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
232 ret = __exec_certindexinfo_query(query, (void *)indexinfo);
234 _LOGE("Cert Info DB Information retrieval failed\n");
235 ret = PMINFO_R_ERROR;
238 memset(query, '\0', MAX_QUERY_LEN);
239 if (indexinfo->cert_ref_count > 1) {
240 /*decrease ref count*/
241 snprintf(query, MAX_QUERY_LEN, "update package_cert_index_info set cert_ref_count=%d where cert_id=%d ",
242 indexinfo->cert_ref_count - 1, (certinfo->cert_id)[i]);
244 /*delete this certificate as ref count is 1 and it will become 0*/
245 snprintf(query, MAX_QUERY_LEN, "delete from package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
248 sqlite3_exec(GET_DB(cert_db), query, NULL, NULL, &error_message)) {
249 _LOGE("Don't execute query = %s error message = %s\n", query,
251 sqlite3_free(error_message);
252 ret = PMINFO_R_ERROR;
257 /*Now delete the entry from db*/
258 snprintf(query, MAX_QUERY_LEN, "delete from package_cert_info where package='%s'", pkgid);
260 sqlite3_exec(GET_DB(cert_db), query, NULL, NULL, &error_message)) {
261 _LOGE("Don't execute query = %s error message = %s\n", query,
263 sqlite3_free(error_message);
264 ret = PMINFO_R_ERROR;
273 if (certinfo->pkgid) {
274 free(certinfo->pkgid);
275 certinfo->pkgid = NULL;
277 for (i = 0; i < MAX_CERT_TYPE; i++) {
278 if ((certinfo->cert_info)[i]) {
279 free((certinfo->cert_info)[i]);
280 (certinfo->cert_info)[i] = NULL;
289 static int __validate_cb(void *data, int ncols, char **coltxt, char **colname)
292 *p = atoi(coltxt[0]);
296 static int __maxid_cb(void *data, int ncols, char **coltxt, char **colname)
300 *p = atoi(coltxt[0]);
304 API int pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(const char *lhs_package_id, const char *rhs_package_id, uid_t uid, pkgmgrinfo_cert_compare_result_type_e *compare_result)
306 int ret = PMINFO_R_OK;
307 char query[MAX_QUERY_LEN] = {'\0'};
308 char *error_message = NULL;
309 sqlite3_stmt *stmt = NULL;
310 char *lhs_certinfo = NULL;
311 char *rhs_certinfo = NULL;
317 *compare_result = PMINFO_CERT_COMPARE_ERROR;
319 retvm_if(lhs_package_id == NULL, PMINFO_R_EINVAL, "lhs package ID is NULL");
320 retvm_if(rhs_package_id == NULL, PMINFO_R_EINVAL, "rhs package ID is NULL");
321 retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
323 ret = __open_cert_db(uid, true);
325 ret = PMINFO_R_ERROR;
328 _check_create_cert_db(GET_DB(cert_db));
329 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", lhs_package_id);
331 sqlite3_exec(GET_DB(cert_db), query, __validate_cb, (void *)&exist, &error_message)) {
332 _LOGE("Don't execute query = %s error message = %s\n", query,
334 ret = PMINFO_R_ERROR;
339 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", rhs_package_id);
341 sqlite3_exec(GET_DB(cert_db), query, __validate_cb, (void *)&exist, &error_message)) {
342 _LOGE("Don't execute query = %s error message = %s\n", query,
344 ret = PMINFO_R_ERROR;
349 if (uid == GLOBAL_USER || uid == ROOT_UID) {
350 snprintf(query, MAX_QUERY_LEN, "select cert_info from package_cert_index_info where cert_id=(select author_signer_cert from package_cert_info where package=?)");
353 snprintf(query, MAX_QUERY_LEN, "select cert_info from package_cert_index_info where cert_id=(select author_signer_cert from package_cert_info where package=?) and for_all_users=(select for_all_users from package_cert_info where package=?)");
354 if (SQLITE_OK != sqlite3_prepare_v2(GET_DB(cert_db), query, strlen(query), &stmt, NULL)) {
355 _LOGE("sqlite3_prepare_v2 error: %s", sqlite3_errmsg(GET_DB(cert_db)));
356 ret = PMINFO_R_ERROR;
360 for (i = 1; i <= 2 - is_global; i++) {
361 if (SQLITE_OK != sqlite3_bind_text(stmt, i, lhs_package_id, -1, SQLITE_STATIC)) {
362 _LOGE("sqlite3_bind_text error: %s", sqlite3_errmsg(GET_DB(cert_db)));
363 ret = PMINFO_R_ERROR;
367 if (SQLITE_ROW != sqlite3_step(stmt) || sqlite3_column_text(stmt, 0) == NULL) {
368 _LOGE("sqlite3_step error: %s", sqlite3_errmsg(GET_DB(cert_db)));
369 ret = PMINFO_R_ERROR;
373 lhs_certinfo = strdup((const char *)sqlite3_column_text(stmt, 0));
375 sqlite3_clear_bindings(stmt);
377 for (i = 1; i <= 2 - is_global; i++) {
378 if (SQLITE_OK != sqlite3_bind_text(stmt, i, rhs_package_id, -1, SQLITE_STATIC)) {
379 _LOGE("sqlite3_bind_text error: %s", sqlite3_errmsg(GET_DB(cert_db)));
380 ret = PMINFO_R_ERROR;
384 if (SQLITE_ROW != sqlite3_step(stmt) || sqlite3_column_text(stmt, 0) == NULL) {
385 _LOGE("sqlite3_step error: %s", sqlite3_errmsg(GET_DB(cert_db)));
386 ret = PMINFO_R_ERROR;
390 rhs_certinfo = strdup((const char *)sqlite3_column_text(stmt, 0));
392 if ((lcert == 0) || (rcert == 0)) {
393 if ((lcert == 0) && (rcert == 0))
394 *compare_result = PMINFO_CERT_COMPARE_BOTH_NO_CERT;
396 *compare_result = PMINFO_CERT_COMPARE_LHS_NO_CERT;
398 *compare_result = PMINFO_CERT_COMPARE_RHS_NO_CERT;
400 if (lhs_certinfo && rhs_certinfo && !strcmp(lhs_certinfo, rhs_certinfo))
401 *compare_result = PMINFO_CERT_COMPARE_MATCH;
403 *compare_result = PMINFO_CERT_COMPARE_MISMATCH;
408 sqlite3_finalize(stmt);
413 sqlite3_free(error_message);
419 API int pkgmgrinfo_pkginfo_compare_pkg_cert_info(const char *lhs_package_id, const char *rhs_package_id, pkgmgrinfo_cert_compare_result_type_e *compare_result)
421 return pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(lhs_package_id, rhs_package_id, GLOBAL_USER, compare_result);
424 API int pkgmgrinfo_pkginfo_compare_app_cert_info(const char *lhs_app_id, const char *rhs_app_id, pkgmgrinfo_cert_compare_result_type_e *compare_result)
426 int ret = PMINFO_R_OK;
427 char query[MAX_QUERY_LEN] = {'\0'};
428 char *error_message = NULL;
433 const char* user_pkg_parser = getUserPkgParserDBPath();
435 retvm_if(lhs_app_id == NULL, PMINFO_R_EINVAL, "lhs app ID is NULL");
436 retvm_if(rhs_app_id == NULL, PMINFO_R_EINVAL, "rhs app ID is NULL");
437 retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
439 info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
440 retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
442 ret = db_util_open_with_options(user_pkg_parser, &GET_DB(manifest_db),
443 SQLITE_OPEN_READONLY, NULL);
444 if (ret != SQLITE_OK) {
445 _LOGE("connect db [%s] failed!\n", user_pkg_parser);
446 ret = PMINFO_R_ERROR;
450 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", lhs_app_id);
452 sqlite3_exec(GET_DB(manifest_db), query, __validate_cb, (void *)&exist, &error_message)) {
453 _LOGE("Don't execute query = %s error message = %s\n", query,
455 ret = PMINFO_R_ERROR;
462 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", lhs_app_id);
464 sqlite3_exec(GET_DB(manifest_db), query, __cert_cb, (void *)info, &error_message)) {
465 _LOGE("Don't execute query = %s error message = %s\n", query,
467 ret = PMINFO_R_ERROR;
470 lpkgid = strdup(info->pkgid);
471 if (lpkgid == NULL) {
472 _LOGE("Out of Memory\n");
473 ret = PMINFO_R_ERROR;
480 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", rhs_app_id);
482 sqlite3_exec(GET_DB(manifest_db), query, __validate_cb, (void *)&exist, &error_message)) {
483 _LOGE("Don't execute query = %s error message = %s\n", query,
485 ret = PMINFO_R_ERROR;
492 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", rhs_app_id);
494 sqlite3_exec(GET_DB(manifest_db), query, __cert_cb, (void *)info, &error_message)) {
495 _LOGE("Don't execute query = %s error message = %s\n", query,
497 ret = PMINFO_R_ERROR;
500 rpkgid = strdup(info->pkgid);
501 if (rpkgid == NULL) {
502 _LOGE("Out of Memory\n");
503 ret = PMINFO_R_ERROR;
509 ret = pkgmgrinfo_pkginfo_compare_pkg_cert_info(lpkgid, rpkgid, compare_result);
512 sqlite3_free(error_message);
513 __close_manifest_db();
533 API int pkgmgrinfo_pkginfo_compare_usr_app_cert_info(const char *lhs_app_id, const char *rhs_app_id, uid_t uid, pkgmgrinfo_cert_compare_result_type_e *compare_result)
535 int ret = PMINFO_R_OK;
536 char query[MAX_QUERY_LEN] = {'\0'};
537 char *error_message = NULL;
543 retvm_if(lhs_app_id == NULL, PMINFO_R_EINVAL, "lhs app ID is NULL");
544 retvm_if(rhs_app_id == NULL, PMINFO_R_EINVAL, "rhs app ID is NULL");
545 retvm_if(compare_result == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL");
547 info = (pkgmgr_cert_x *)calloc(1, sizeof(pkgmgr_cert_x));
548 retvm_if(info == NULL, PMINFO_R_ERROR, "Out of Memory!!!");
550 ret = __open_manifest_db(uid, true);
551 if (ret != SQLITE_OK) {
552 _LOGE("connect db [%s] failed!\n", getUserPkgParserDBPathUID(uid));
553 ret = PMINFO_R_ERROR;
557 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", lhs_app_id);
559 sqlite3_exec(GET_DB(manifest_db), query, __validate_cb, (void *)&exist, &error_message)) {
560 _LOGE("Don't execute query = %s error message = %s\n", query,
562 ret = PMINFO_R_ERROR;
569 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", lhs_app_id);
571 sqlite3_exec(GET_DB(manifest_db), query, __cert_cb, (void *)info, &error_message)) {
572 _LOGE("Don't execute query = %s error message = %s\n", query,
574 ret = PMINFO_R_ERROR;
577 lpkgid = strdup(info->pkgid);
578 if (lpkgid == NULL) {
579 _LOGE("Out of Memory\n");
580 ret = PMINFO_R_ERROR;
587 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_app_info where app_id='%s')", rhs_app_id);
589 sqlite3_exec(GET_DB(manifest_db), query, __validate_cb, (void *)&exist, &error_message)) {
590 _LOGE("Don't execute query = %s error message = %s\n", query,
592 ret = PMINFO_R_ERROR;
599 snprintf(query, MAX_QUERY_LEN, "select package from package_app_info where app_id='%s' ", rhs_app_id);
601 sqlite3_exec(GET_DB(manifest_db), query, __cert_cb, (void *)info, &error_message)) {
602 _LOGE("Don't execute query = %s error message = %s\n", query,
604 ret = PMINFO_R_ERROR;
607 rpkgid = strdup(info->pkgid);
608 if (rpkgid == NULL) {
609 _LOGE("Out of Memory\n");
610 ret = PMINFO_R_ERROR;
616 ret = pkgmgrinfo_pkginfo_compare_usr_pkg_cert_info(lpkgid, rpkgid, uid, compare_result);
619 sqlite3_free(error_message);
620 __close_manifest_db();
640 API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle)
642 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
643 pkgmgr_certinfo_x *certinfo = NULL;
644 certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
646 retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
647 *handle = (void *)certinfo;
651 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_h handle, uid_t uid)
653 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
654 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
655 pkgmgr_certinfo_x *certinfo = NULL;
656 char *error_message = NULL;
657 int ret = PMINFO_R_OK;
658 char query[MAX_QUERY_LEN] = {'\0'};
663 ret = __open_cert_db(uid, true);
664 if (ret != SQLITE_OK) {
665 _LOGE("connect db [%s] failed!\n");
666 ret = PMINFO_R_ERROR;
669 _check_create_cert_db(GET_DB(cert_db));
671 snprintf(query, MAX_QUERY_LEN, "select exists(select * from package_cert_info where package='%s')", pkgid);
673 sqlite3_exec(GET_DB(cert_db), query, __validate_cb, (void *)&exist, &error_message)) {
674 _LOGE("Don't execute query = %s error message = %s\n", query,
676 sqlite3_free(error_message);
677 ret = PMINFO_R_ERROR;
681 _LOGE("Package for user[%d] is not found in DB\n", uid);
682 ret = PMINFO_R_ERROR;
685 certinfo = (pkgmgr_certinfo_x *)handle;
686 /*populate certinfo from DB*/
687 snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid);
688 ret = __exec_certinfo_query(query, (void *)certinfo);
690 _LOGE("Package Cert Info DB Information retrieval failed\n");
691 ret = PMINFO_R_ERROR;
694 for (i = 0; i < MAX_CERT_TYPE; i++) {
695 memset(query, '\0', MAX_QUERY_LEN);
696 if (uid == GLOBAL_USER || uid == ROOT_UID)
697 snprintf(query, MAX_QUERY_LEN, "select cert_info from package_cert_index_info where cert_id=%d", (certinfo->cert_id)[i]);
699 snprintf(query, MAX_QUERY_LEN, "select cert_info from package_cert_index_info where cert_id=%d and for_all_users=%d", (certinfo->cert_id)[i], certinfo->for_all_users);
700 ret = __exec_certinfo_query(query, (void *)certinfo);
702 _LOGE("Cert Info DB Information retrieval failed\n");
703 ret = PMINFO_R_ERROR;
706 if (certinfo->cert_value) {
707 (certinfo->cert_info)[i] = strdup(certinfo->cert_value);
708 free(certinfo->cert_value);
709 certinfo->cert_value = NULL;
717 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle, pkgmgrinfo_cert_type cert_type, const char **cert_value)
719 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
720 retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
721 retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
722 retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
723 pkgmgr_certinfo_x *certinfo = NULL;
724 certinfo = (pkgmgr_certinfo_x *)handle;
725 if ((certinfo->cert_info)[cert_type])
726 *cert_value = (certinfo->cert_info)[cert_type];
732 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle)
734 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
736 pkgmgr_certinfo_x *certinfo = NULL;
737 certinfo = (pkgmgr_certinfo_x *)handle;
738 if (certinfo->pkgid) {
739 free(certinfo->pkgid);
740 certinfo->pkgid = NULL;
742 for (i = 0; i < MAX_CERT_TYPE; i++) {
743 if ((certinfo->cert_info)[i]) {
744 free((certinfo->cert_info)[i]);
745 (certinfo->cert_info)[i] = NULL;
753 API int pkgmgrinfo_create_certinfo_set_handle(pkgmgrinfo_instcertinfo_h *handle)
755 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
756 pkgmgr_instcertinfo_x *certinfo = NULL;
758 certinfo = calloc(1, sizeof(pkgmgr_instcertinfo_x));
759 retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
760 *handle = (void *)certinfo;
764 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle, pkgmgrinfo_instcert_type cert_type, char *cert_value)
766 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
767 retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
768 retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
769 retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
770 pkgmgr_instcertinfo_x *certinfo = NULL;
771 certinfo = (pkgmgr_instcertinfo_x *)handle;
772 (certinfo->cert_info)[cert_type] = strdup(cert_value);
776 API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h handle, uid_t uid)
778 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
779 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
780 char *error_message = NULL;
781 char query[MAX_QUERY_LEN] = {'\0'};
782 char vquery[MAX_QUERY_LEN] = {'\0'};
786 int unique_id[MAX_CERT_TYPE] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
793 pkgmgr_instcertinfo_x *info = (pkgmgr_instcertinfo_x *)handle;
794 pkgmgr_certindexinfo_x *indexinfo = NULL;
795 indexinfo = calloc(1, sizeof(pkgmgr_certindexinfo_x));
796 if (indexinfo == NULL) {
797 _LOGE("Out of Memory!!!");
798 return PMINFO_R_ERROR;
800 info->pkgid = strdup(pkgid);
803 ret =__open_cert_db(uid, false);
805 ret = PMINFO_R_ERROR;
806 _LOGE("Failed to open cert db \n");
809 _check_create_cert_db(GET_DB(cert_db));
810 /*Begin Transaction*/
811 ret = sqlite3_exec(GET_DB(cert_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
813 _LOGE("Failed to begin transaction %s\n");
814 ret = PMINFO_R_ERROR;
818 /*Check if request is to insert/update*/
819 snprintf(query, sizeof(query), "select exists(select * from package_cert_info where package='%s')", pkgid);
821 sqlite3_exec(GET_DB(cert_db), query, __validate_cb, (void *)&exist, &error_message)) {
822 _LOGE("Don't execute query = %s error message = %s\n", query,
824 sqlite3_free(error_message);
825 ret = PMINFO_R_ERROR;
830 We cant just issue update query directly. We need to manage index table also.
831 Hence it is better to delete and insert again in case of update*/
832 ret = __delete_certinfo(pkgid, uid);
834 _LOGE("Certificate Deletion Failed\n");
836 for (i = 0; i < MAX_CERT_TYPE; i++) {
837 if ((info->cert_info)[i]) {
838 for (j = 0; j < i; j++) {
839 if ( (info->cert_info)[j]) {
840 if (strcmp((info->cert_info)[i], (info->cert_info)[j]) == 0) {
841 (info->cert_id)[i] = (info->cert_id)[j];
842 (info->is_new)[i] = 0;
843 (info->ref_count)[i] = (info->ref_count)[j];
850 snprintf(query, sizeof(query), "select * from package_cert_index_info " \
851 "where cert_info='%s'",(info->cert_info)[i]);
852 ret = __exec_certindexinfo_query(query, (void *)indexinfo);
854 _LOGE("Cert Info DB Information retrieval failed\n");
855 ret = PMINFO_R_ERROR;
858 if (indexinfo->cert_id == 0) {
859 /*New certificate. Get newid*/
860 snprintf(query, sizeof(query), "select MAX(cert_id) from package_cert_index_info ");
862 sqlite3_exec(GET_DB(cert_db), query, __maxid_cb, (void *)&newid, &error_message)) {
863 _LOGE("Don't execute query = %s error message = %s\n", query,
865 sqlite3_free(error_message);
866 ret = PMINFO_R_ERROR;
874 indexinfo->cert_id = maxid;
875 indexinfo->cert_ref_count = 1;
879 (info->cert_id)[i] = indexinfo->cert_id;
880 (info->is_new)[i] = is_new;
881 (info->ref_count)[i] = indexinfo->cert_ref_count;
882 indexinfo->cert_id = 0;
883 indexinfo->cert_ref_count = 0;
888 snprintf(vquery, sizeof(vquery),
889 "insert into package_cert_info(package, author_root_cert, author_im_cert, author_signer_cert, dist_root_cert, " \
890 "dist_im_cert, dist_signer_cert, dist2_root_cert, dist2_im_cert, dist2_signer_cert) " \
891 "values('%s', %d, %d, %d, %d, %d, %d, %d, %d, %d)",\
892 info->pkgid,(info->cert_id)[PMINFO_SET_AUTHOR_ROOT_CERT],(info->cert_id)[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT],
893 (info->cert_id)[PMINFO_SET_AUTHOR_SIGNER_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_ROOT_CERT],
894 (info->cert_id)[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT],
895 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT],(info->cert_id)[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT],
896 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT]);
898 sqlite3_exec(GET_DB(cert_db), vquery, NULL, NULL, &error_message)) {
899 _LOGE("Don't execute query = %s error message = %s\n", vquery,
901 sqlite3_free(error_message);
902 ret = PMINFO_R_ERROR;
905 /*Update index table info*/
906 /*If cert_id exists and is repeated for current package, ref count should only be increased once*/
907 for (i = 0; i < MAX_CERT_TYPE; i++) {
908 if ((info->cert_info)[i]) {
909 if ((info->is_new)[i]) {
910 snprintf(vquery, sizeof(vquery), "insert into package_cert_index_info(cert_info, cert_id, cert_ref_count) " \
911 "values('%s', '%d', '%d') ", (info->cert_info)[i], (info->cert_id)[i], 1);
912 unique_id[c++] = (info->cert_id)[i];
915 for (j = 0; j < MAX_CERT_TYPE; j++) {
916 if ((info->cert_id)[i] == unique_id[j]) {
917 /*Ref count has already been increased. Just continue*/
921 if (j == MAX_CERT_TYPE)
922 unique_id[c++] = (info->cert_id)[i];
925 snprintf(vquery, sizeof(vquery), "update package_cert_index_info set cert_ref_count=%d " \
926 "where cert_id=%d", (info->ref_count)[i] + 1, (info->cert_id)[i]);
929 sqlite3_exec(GET_DB(cert_db), vquery, NULL, NULL, &error_message)) {
930 _LOGE("Don't execute query = %s error message = %s\n", vquery,
932 sqlite3_free(error_message);
933 ret = PMINFO_R_ERROR;
938 /*Commit transaction*/
939 ret = sqlite3_exec(GET_DB(cert_db), "COMMIT", NULL, NULL, NULL);
940 if (ret != SQLITE_OK) {
941 _LOGE("Failed to commit transaction, Rollback now\n");
942 sqlite3_exec(GET_DB(cert_db), "ROLLBACK", NULL, NULL, NULL);
943 ret = PMINFO_R_ERROR;
957 API int pkgmgrinfo_destroy_certinfo_set_handle(pkgmgrinfo_instcertinfo_h handle)
959 retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
961 pkgmgr_instcertinfo_x *certinfo = NULL;
962 certinfo = (pkgmgr_instcertinfo_x *)handle;
963 if (certinfo->pkgid) {
964 free(certinfo->pkgid);
965 certinfo->pkgid = NULL;
967 for (i = 0; i < MAX_CERT_TYPE; i++) {
968 if ((certinfo->cert_info)[i]) {
969 free((certinfo->cert_info)[i]);
970 (certinfo->cert_info)[i] = NULL;
978 API int pkgmgrinfo_delete_usr_certinfo(const char *pkgid, uid_t uid)
980 retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
983 ret = __open_cert_db(uid, false);
985 _LOGE("connect db [%s] failed!\n", getUserPkgCertDBPathUID(uid));
986 ret = PMINFO_R_ERROR;
989 _check_create_cert_db(GET_DB(cert_db));
990 /*Begin Transaction*/
991 ret = sqlite3_exec(GET_DB(cert_db), "BEGIN EXCLUSIVE", NULL, NULL, NULL);
992 if (ret != SQLITE_OK) {
993 _LOGE("Failed to begin transaction\n");
994 ret = PMINFO_R_ERROR;
997 _LOGE("Transaction Begin\n");
998 ret = __delete_certinfo(pkgid, uid);
1000 _LOGE("Certificate Deletion Failed\n");
1002 _LOGE("Certificate Deletion Success\n");
1004 /*Commit transaction*/
1005 ret = sqlite3_exec(GET_DB(cert_db), "COMMIT", NULL, NULL, NULL);
1006 if (ret != SQLITE_OK) {
1007 _LOGE("Failed to commit transaction, Rollback now\n");
1008 sqlite3_exec(GET_DB(cert_db), "ROLLBACK", NULL, NULL, NULL);
1009 ret = PMINFO_R_ERROR;
1012 _LOGE("Transaction Commit and End\n");
1020 API int pkgmgrinfo_delete_certinfo(const char *pkgid)
1022 return pkgmgrinfo_delete_usr_certinfo(pkgid, GLOBAL_USER);