62b9b76b04b550092bd08188eac0131fb475a4b5
[framework/appfw/pkgmgr-info.git] / src / pkgmgrinfo_certinfo.c
1 /*
2  * pkgmgrinfo-certinfo
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Junsuk Oh <junsuk77.oh@samsung.com>,
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <sqlite3.h>
25 #include <db-util.h>
26
27 #include "pkgmgr-info-internal.h"
28 #include "pkgmgr-info-debug.h"
29 #include "pkgmgr-info.h"
30
31 #ifdef LOG_TAG
32 #undef LOG_TAG
33 #endif
34 #define LOG_TAG "PKGMGR_CERT"
35
36
37 typedef struct _pkgmgr_instcertinfo_x {
38         char *pkgid;
39         char *cert_info[MAX_CERT_TYPE]; /*certificate data*/
40         int is_new[MAX_CERT_TYPE];              /*whether already exist in table or not*/
41         int ref_count[MAX_CERT_TYPE];           /*reference count of certificate data*/
42         int cert_id[MAX_CERT_TYPE];             /*certificate ID in index table*/
43 } pkgmgr_instcertinfo_x;
44
45 typedef struct _pkgmgr_certindexinfo_x {
46         int cert_id;
47         int cert_ref_count;
48 } pkgmgr_certindexinfo_x;
49
50 typedef struct _pkgmgr_certinfo_x {
51         char *pkgid;
52         char *cert_value;
53         char *cert_info[MAX_CERT_TYPE]; /*certificate info*/
54         int cert_id[MAX_CERT_TYPE];             /*certificate ID in index table*/
55 } pkgmgr_certinfo_x;
56
57 __thread sqlite3 *cert_db = NULL;
58
59 static int __maxid_cb(void *data, int ncols, char **coltxt, char **colname)
60 {
61         int *p = (int*)data;
62         if (coltxt[0])
63                 *p = atoi(coltxt[0]);
64         return 0;
65 }
66
67 static int __certinfo_cb(void *data, int ncols, char **coltxt, char **colname)
68 {
69         pkgmgr_certinfo_x *info = (pkgmgr_certinfo_x *)data;
70         int i = 0;
71         for(i = 0; i < ncols; i++)
72         {
73                 if (strcmp(colname[i], "package") == 0) {
74                         if (coltxt[i])
75                                 info->pkgid = strdup(coltxt[i]);
76                         else
77                                 info->pkgid = NULL;
78                 } else if (strcmp(colname[i], "author_signer_cert") == 0) {
79                         if (coltxt[i])
80                                 (info->cert_id)[PMINFO_AUTHOR_SIGNER_CERT] = atoi(coltxt[i]);
81                         else
82                                 (info->cert_id)[PMINFO_AUTHOR_SIGNER_CERT] = 0;
83                 } else if (strcmp(colname[i], "author_im_cert") == 0) {
84                         if (coltxt[i])
85                                 (info->cert_id)[PMINFO_AUTHOR_INTERMEDIATE_CERT] = atoi(coltxt[i]);
86                         else
87                                 (info->cert_id)[PMINFO_AUTHOR_INTERMEDIATE_CERT] = 0;
88                 } else if (strcmp(colname[i], "author_root_cert") == 0) {
89                         if (coltxt[i])
90                                 (info->cert_id)[PMINFO_AUTHOR_ROOT_CERT] = atoi(coltxt[i]);
91                         else
92                                 (info->cert_id)[PMINFO_AUTHOR_ROOT_CERT] = 0;
93                 } else if (strcmp(colname[i], "dist_signer_cert") == 0 ){
94                         if (coltxt[i])
95                                 (info->cert_id)[PMINFO_DISTRIBUTOR_SIGNER_CERT] = atoi(coltxt[i]);
96                         else
97                                 (info->cert_id)[PMINFO_DISTRIBUTOR_SIGNER_CERT] = 0;
98                 } else if (strcmp(colname[i], "dist_im_cert") == 0 ){
99                         if (coltxt[i])
100                                 (info->cert_id)[PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT] = atoi(coltxt[i]);
101                         else
102                                 (info->cert_id)[PMINFO_DISTRIBUTOR_INTERMEDIATE_CERT] = 0;
103                 } else if (strcmp(colname[i], "dist_root_cert") == 0 ){
104                         if (coltxt[i])
105                                 (info->cert_id)[PMINFO_DISTRIBUTOR_ROOT_CERT] = atoi(coltxt[i]);
106                         else
107                                 (info->cert_id)[PMINFO_DISTRIBUTOR_ROOT_CERT] = 0;
108                 } else if (strcmp(colname[i], "dist2_signer_cert") == 0 ){
109                         if (coltxt[i])
110                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_SIGNER_CERT] = atoi(coltxt[i]);
111                         else
112                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_SIGNER_CERT] = 0;
113                 } else if (strcmp(colname[i], "dist2_im_cert") == 0 ){
114                         if (coltxt[i])
115                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT] = atoi(coltxt[i]);
116                         else
117                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_INTERMEDIATE_CERT] = 0;
118                 } else if (strcmp(colname[i], "dist2_root_cert") == 0 ){
119                         if (coltxt[i])
120                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_ROOT_CERT] = atoi(coltxt[i]);
121                         else
122                                 (info->cert_id)[PMINFO_DISTRIBUTOR2_ROOT_CERT] = 0;
123                 } else if (strcmp(colname[i], "cert_info") == 0 ){
124                         if (coltxt[i])
125                                 info->cert_value = strdup(coltxt[i]);
126                         else
127                                 info->cert_value = NULL;
128                 } else
129                         continue;
130         }
131         return 0;
132 }
133
134 static int __certindexinfo_cb(void *data, int ncols, char **coltxt, char **colname)
135 {
136         pkgmgr_certindexinfo_x *info = (pkgmgr_certindexinfo_x *)data;
137         int i = 0;
138         for(i = 0; i < ncols; i++) {
139                 if (strcmp(colname[i], "cert_id") == 0) {
140                         if (coltxt[i])
141                                 info->cert_id = atoi(coltxt[i]);
142                         else
143                                 info->cert_id = 0;
144                 } else if (strcmp(colname[i], "cert_ref_count") == 0) {
145                         if (coltxt[i])
146                                 info->cert_ref_count = atoi(coltxt[i]);
147                         else
148                                 info->cert_ref_count = 0;
149                 } else
150                         continue;
151         }
152         return 0;
153 }
154
155 static int __exec_certinfo_query(char *query, void *data)
156 {
157         char *error_message = NULL;
158         if (SQLITE_OK !=
159             sqlite3_exec(cert_db, query, __certinfo_cb, data, &error_message)) {
160                 _LOGE("Don't execute query = %s error message = %s\n", query,
161                        error_message);
162                 sqlite3_free(error_message);
163                 return -1;
164         }
165         sqlite3_free(error_message);
166         return 0;
167 }
168
169 static int __exec_certindexinfo_query(char *query, void *data)
170 {
171         char *error_message = NULL;
172         if (SQLITE_OK !=
173             sqlite3_exec(cert_db, query, __certindexinfo_cb, data, &error_message)) {
174                 _LOGE("Don't execute query = %s error message = %s\n", query,
175                        error_message);
176                 sqlite3_free(error_message);
177                 return -1;
178         }
179         sqlite3_free(error_message);
180         return 0;
181 }
182
183 static int __delete_certinfo(const char *pkgid)
184 {
185         int ret = -1;
186         int i = 0;
187         int j = 0;
188         int c = 0;
189         int unique_id[MAX_CERT_TYPE] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
190         char *error_message = NULL;
191         char query[MAX_QUERY_LEN] = {'\0'};
192         pkgmgr_certinfo_x *certinfo = NULL;
193         pkgmgr_certindexinfo_x *indexinfo = NULL;
194         certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
195         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
196         indexinfo = calloc(1, sizeof(pkgmgr_certindexinfo_x));
197         if (indexinfo == NULL) {
198                 _LOGE("Out of Memory!!!");
199                 ret = PMINFO_R_ERROR;
200                 goto err;
201         }
202         /*populate certinfo from DB*/
203         char *sel_query = sqlite3_mprintf("select * from package_cert_info where package=%Q ", pkgid);
204         ret = __exec_certinfo_query(sel_query, (void *)certinfo);
205         sqlite3_free(sel_query);
206         if (ret == -1) {
207                 _LOGE("Package Cert Info DB Information retrieval failed\n");
208                 ret = PMINFO_R_ERROR;
209                 goto err;
210         }
211         /*Update cert index table*/
212         for (i = 0; i < MAX_CERT_TYPE; i++) {
213                 if ((certinfo->cert_id)[i]) {
214                         for (j = 0; j < MAX_CERT_TYPE; j++) {
215                                 if ((certinfo->cert_id)[i] == unique_id[j]) {
216                                         /*Ref count has already been updated. Just continue*/
217                                         break;
218                                 }
219                         }
220                         if (j == MAX_CERT_TYPE)
221                                 unique_id[c++] = (certinfo->cert_id)[i];
222                         else
223                                 continue;
224                         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
225                         ret = __exec_certindexinfo_query(query, (void *)indexinfo);
226                         if (ret == -1) {
227                                 _LOGE("Cert Info DB Information retrieval failed\n");
228                                 ret = PMINFO_R_ERROR;
229                                 goto err;
230                         }
231                         memset(query, '\0', MAX_QUERY_LEN);
232                         if (indexinfo->cert_ref_count > 1) {
233                                 /*decrease ref count*/
234                                 snprintf(query, MAX_QUERY_LEN, "update package_cert_index_info set cert_ref_count=%d where cert_id=%d ",
235                                 indexinfo->cert_ref_count - 1, (certinfo->cert_id)[i]);
236                         } else {
237                                 /*delete this certificate as ref count is 1 and it will become 0*/
238                                 snprintf(query, MAX_QUERY_LEN, "delete from  package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
239                         }
240                         if (SQLITE_OK !=
241                             sqlite3_exec(cert_db, query, NULL, NULL, &error_message)) {
242                                 _LOGE("Don't execute query = %s error message = %s\n", query,
243                                        error_message);
244                                 sqlite3_free(error_message);
245                                 ret = PMINFO_R_ERROR;
246                                 goto err;
247                         }
248                 }
249         }
250         /*Now delete the entry from db*/
251         char *del_query = sqlite3_mprintf("delete from package_cert_info where package=%Q", pkgid);
252     if (SQLITE_OK !=
253         sqlite3_exec(cert_db, del_query, NULL, NULL, &error_message)) {
254             _LOGE("Don't execute query = %s error message = %s\n", del_query,
255                    error_message);
256                 sqlite3_free(error_message);
257                 sqlite3_free(del_query);
258                 ret = PMINFO_R_ERROR;
259                 goto err;
260     }
261         sqlite3_free(del_query);
262         ret = PMINFO_R_OK;
263 err:
264         if (indexinfo) {
265                 free(indexinfo);
266                 indexinfo = NULL;
267         }
268         if (certinfo->pkgid) {
269                 free(certinfo->pkgid);
270                 certinfo->pkgid = NULL;
271         }
272         for (i = 0; i < MAX_CERT_TYPE; i++) {
273                 if ((certinfo->cert_info)[i]) {
274                         free((certinfo->cert_info)[i]);
275                         (certinfo->cert_info)[i] = NULL;
276                 }
277         }
278         free(certinfo);
279         certinfo = NULL;
280         return ret;
281 }
282
283 API int pkgmgrinfo_pkginfo_create_certinfo(pkgmgrinfo_certinfo_h *handle)
284 {
285         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
286         pkgmgr_certinfo_x *certinfo = NULL;
287         certinfo = calloc(1, sizeof(pkgmgr_certinfo_x));
288         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
289         *handle = (void *)certinfo;
290         return PMINFO_R_OK;
291 }
292
293 API int pkgmgrinfo_pkginfo_load_certinfo(const char *pkgid, pkgmgrinfo_certinfo_h handle)
294 {
295         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
296         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
297         pkgmgr_certinfo_x *certinfo = NULL;
298         int ret = PMINFO_R_OK;
299         char query[MAX_QUERY_LEN] = {'\0'};
300         int i = 0;
301
302         /*Open db.*/
303         ret = db_util_open_with_options(CERT_DB, &cert_db, SQLITE_OPEN_READONLY, NULL);
304         if (ret != SQLITE_OK) {
305                 _LOGE("connect db [%s] failed!\n", CERT_DB);
306                 return PMINFO_R_ERROR;
307         }
308
309         certinfo = (pkgmgr_certinfo_x *)handle;
310         /*populate certinfo from DB*/
311         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_info where package='%s' ", pkgid);
312         ret = __exec_certinfo_query(query, (void *)certinfo);
313         if (ret == -1) {
314                 _LOGE("Package Cert Info DB Information retrieval failed\n");
315                 ret = PMINFO_R_ERROR;
316                 goto err;
317         }
318
319         if (certinfo->pkgid == NULL) {
320                 _LOGE("Package not found in DB\n");
321                 ret = PMINFO_R_ERROR;
322                 goto err;
323         }
324
325         for (i = 0; i < MAX_CERT_TYPE; i++) {
326                 memset(query, '\0', MAX_QUERY_LEN);
327                 snprintf(query, MAX_QUERY_LEN, "select cert_info from package_cert_index_info where cert_id=%d ", (certinfo->cert_id)[i]);
328                 ret = __exec_certinfo_query(query, (void *)certinfo);
329                 if (ret == -1) {
330                         _LOGE("Cert Info DB Information retrieval failed\n");
331                         ret = PMINFO_R_ERROR;
332                         goto err;
333                 }
334                 if (certinfo->cert_value) {
335                         (certinfo->cert_info)[i] = strdup(certinfo->cert_value);
336                         free(certinfo->cert_value);
337                         certinfo->cert_value = NULL;
338                 }
339         }
340 err:
341         sqlite3_close(cert_db);
342         return ret;
343 }
344
345 API int pkgmgrinfo_pkginfo_get_cert_value(pkgmgrinfo_certinfo_h handle, pkgmgrinfo_cert_type cert_type, const char **cert_value)
346 {
347         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
348         retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
349         retvm_if(cert_type < PMINFO_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
350         retvm_if(cert_type > PMINFO_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
351         pkgmgr_certinfo_x *certinfo = NULL;
352         certinfo = (pkgmgr_certinfo_x *)handle;
353         if ((certinfo->cert_info)[cert_type])
354                 *cert_value = (certinfo->cert_info)[cert_type];
355         else
356                 *cert_value = NULL;
357         return PMINFO_R_OK;
358 }
359
360 API int pkgmgrinfo_pkginfo_destroy_certinfo(pkgmgrinfo_certinfo_h handle)
361 {
362         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
363         int i = 0;
364         pkgmgr_certinfo_x *certinfo = NULL;
365         certinfo = (pkgmgr_certinfo_x *)handle;
366         if (certinfo->pkgid) {
367                 free(certinfo->pkgid);
368                 certinfo->pkgid = NULL;
369         }
370         for (i = 0; i < MAX_CERT_TYPE; i++) {
371                 if ((certinfo->cert_info)[i]) {
372                         free((certinfo->cert_info)[i]);
373                         (certinfo->cert_info)[i] = NULL;
374                 }
375         }
376         free(certinfo);
377         certinfo = NULL;
378         return PMINFO_R_OK;
379 }
380
381 API int pkgmgrinfo_create_certinfo_set_handle(pkgmgrinfo_instcertinfo_h *handle)
382 {
383         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied to hold return value is NULL\n");
384         pkgmgr_instcertinfo_x *certinfo = NULL;
385         certinfo = calloc(1, sizeof(pkgmgr_instcertinfo_x));
386         retvm_if(certinfo == NULL, PMINFO_R_ERROR, "Malloc Failed\n");
387         *handle = (void *)certinfo;
388         return PMINFO_R_OK;
389 }
390
391 API int pkgmgrinfo_set_cert_value(pkgmgrinfo_instcertinfo_h handle, pkgmgrinfo_instcert_type cert_type, char *cert_value)
392 {
393         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
394         retvm_if(cert_value == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
395         retvm_if(cert_type < PMINFO_SET_AUTHOR_ROOT_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
396         retvm_if(cert_type > PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT, PMINFO_R_EINVAL, "Invalid certificate type\n");
397         pkgmgr_instcertinfo_x *certinfo = NULL;
398         certinfo = (pkgmgr_instcertinfo_x *)handle;
399         (certinfo->cert_info)[cert_type] = strdup(cert_value);
400         return PMINFO_R_OK;
401 }
402
403 API int pkgmgrinfo_save_certinfo(const char *pkgid, pkgmgrinfo_instcertinfo_h handle)
404 {
405         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "package ID is NULL\n");
406         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Certinfo handle is NULL\n");
407         char *error_message = NULL;
408         char query[MAX_QUERY_LEN] = {'\0'};
409         char *vquery = NULL;
410         int len = 0;
411         int i = 0;
412         int j = 0;
413         int c = 0;
414         int unique_id[MAX_CERT_TYPE] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
415         int newid = 0;
416         int is_new = 0;
417         int exist = -1;
418         int ret = -1;
419         int maxid = 0;
420         int flag = 0;
421         pkgmgr_instcertinfo_x *info = (pkgmgr_instcertinfo_x *)handle;
422         pkgmgr_certindexinfo_x *indexinfo = NULL;
423         indexinfo = calloc(1, sizeof(pkgmgr_certindexinfo_x));
424         if (indexinfo == NULL) {
425                 _LOGE("Out of Memory!!!");
426                 return PMINFO_R_ERROR;
427         }
428         info->pkgid = strdup(pkgid);
429
430         /*Open db.*/
431         ret = db_util_open_with_options(CERT_DB, &cert_db, SQLITE_OPEN_READWRITE, NULL);
432         if (ret != SQLITE_OK) {
433                 _LOGE("connect db [%s] failed!\n", CERT_DB);
434                 ret = PMINFO_R_ERROR;
435                 goto err;
436         }
437         /*Begin Transaction*/
438         ret = sqlite3_exec(cert_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
439         if (ret != SQLITE_OK) {
440                 _LOGE("Failed to begin transaction\n");
441                 ret = PMINFO_R_ERROR;
442                 goto err;
443         }
444         _LOGE("Transaction Begin\n");
445         /*Check if request is to insert/update*/
446         char *cert_query = sqlite3_mprintf("select exists(select * from package_cert_info where package=%Q)", pkgid);
447         if (SQLITE_OK !=
448             sqlite3_exec(cert_db, cert_query, _pkgmgrinfo_validate_cb, (void *)&exist, &error_message)) {
449                 _LOGE("Don't execute query = %s error message = %s\n", cert_query,
450                        error_message);
451                 sqlite3_free(cert_query);
452                 sqlite3_free(error_message);
453                 ret = PMINFO_R_ERROR;
454                 goto err;
455         }
456         sqlite3_free(cert_query);
457
458         if (exist) {
459                 /*Update request.
460                 We cant just issue update query directly. We need to manage index table also.
461                 Hence it is better to delete and insert again in case of update*/
462                 ret = __delete_certinfo(pkgid);
463                 if (ret < 0)
464                         _LOGE("Certificate Deletion Failed\n");
465         }
466         for (i = 0; i < MAX_CERT_TYPE; i++) {
467                 if ((info->cert_info)[i]) {
468                         for (j = 0; j < i; j++) {
469                                 if ( (info->cert_info)[j]) {
470                                         if (strcmp((info->cert_info)[i], (info->cert_info)[j]) == 0) {
471                                                 (info->cert_id)[i] = (info->cert_id)[j];
472                                                 (info->is_new)[i] = 0;
473                                                 (info->ref_count)[i] = (info->ref_count)[j];
474                                                 break;
475                                         }
476                                 }
477                         }
478                         if (j < i)
479                                 continue;
480                         snprintf(query, MAX_QUERY_LEN, "select * from package_cert_index_info " \
481                                 "where cert_info='%s'",(info->cert_info)[i]);
482                         ret = __exec_certindexinfo_query(query, (void *)indexinfo);
483                         if (ret == -1) {
484                                 _LOGE("Cert Info DB Information retrieval failed\n");
485                                 ret = PMINFO_R_ERROR;
486                                 goto err;
487                         }
488                         if (indexinfo->cert_id == 0) {
489                                 /*New certificate. Get newid*/
490                                 memset(query, '\0', MAX_QUERY_LEN);
491                                 snprintf(query, MAX_QUERY_LEN, "select MAX(cert_id) from package_cert_index_info ");
492                                 if (SQLITE_OK !=
493                                     sqlite3_exec(cert_db, query, __maxid_cb, (void *)&newid, &error_message)) {
494                                         _LOGE("Don't execute query = %s error message = %s\n", query,
495                                                error_message);
496                                         sqlite3_free(error_message);
497                                         ret = PMINFO_R_ERROR;
498                                         goto err;
499                                 }
500                                 newid = newid + 1;
501                                 if (flag == 0) {
502                                         maxid = newid;
503                                         flag = 1;
504                                 }
505                                 indexinfo->cert_id = maxid;
506                                 indexinfo->cert_ref_count = 1;
507                                 is_new = 1;
508                                 maxid = maxid + 1;
509                         }
510                         (info->cert_id)[i] = indexinfo->cert_id;
511                         (info->is_new)[i] = is_new;
512                         (info->ref_count)[i] = indexinfo->cert_ref_count;
513                         _LOGE("Id:Count = %d %d\n", indexinfo->cert_id, indexinfo->cert_ref_count);
514                         indexinfo->cert_id = 0;
515                         indexinfo->cert_ref_count = 0;
516                         is_new = 0;
517                 }
518         }
519         len = MAX_QUERY_LEN;
520         for (i = 0; i < MAX_CERT_TYPE; i++) {
521                 if ((info->cert_info)[i])
522                         len+= strlen((info->cert_info)[i]);
523         }
524         vquery = (char *)calloc(1, len);
525         /*insert*/
526         snprintf(vquery, len,
527                  "insert into package_cert_info(package, author_root_cert, author_im_cert, author_signer_cert, dist_root_cert, " \
528                 "dist_im_cert, dist_signer_cert, dist2_root_cert, dist2_im_cert, dist2_signer_cert) " \
529                 "values('%s', %d, %d, %d, %d, %d, %d, %d, %d, %d)",\
530                  info->pkgid,(info->cert_id)[PMINFO_SET_AUTHOR_ROOT_CERT],(info->cert_id)[PMINFO_SET_AUTHOR_INTERMEDIATE_CERT],
531                 (info->cert_id)[PMINFO_SET_AUTHOR_SIGNER_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_ROOT_CERT],
532                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR_INTERMEDIATE_CERT], (info->cert_id)[PMINFO_SET_DISTRIBUTOR_SIGNER_CERT],
533                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_ROOT_CERT],(info->cert_id)[PMINFO_SET_DISTRIBUTOR2_INTERMEDIATE_CERT],
534                 (info->cert_id)[PMINFO_SET_DISTRIBUTOR2_SIGNER_CERT]);
535         if (SQLITE_OK !=
536             sqlite3_exec(cert_db, vquery, NULL, NULL, &error_message)) {
537                 _LOGE("Don't execute query = %s error message = %s\n", vquery,
538                        error_message);
539                 sqlite3_free(error_message);
540                 ret = PMINFO_R_ERROR;
541                 goto err;
542         }
543         /*Update index table info*/
544         /*If cert_id exists and is repeated for current package, ref count should only be increased once*/
545         for (i = 0; i < MAX_CERT_TYPE; i++) {
546                 if ((info->cert_info)[i]) {
547                         memset(vquery, '\0', len);
548                         if ((info->is_new)[i]) {
549                                 snprintf(vquery, len, "insert into package_cert_index_info(cert_info, cert_id, cert_ref_count) " \
550                                 "values('%s', '%d', '%d') ", (info->cert_info)[i], (info->cert_id)[i], 1);
551                                 unique_id[c++] = (info->cert_id)[i];
552                         } else {
553                                 /*Update*/
554                                 for (j = 0; j < MAX_CERT_TYPE; j++) {
555                                         if ((info->cert_id)[i] == unique_id[j]) {
556                                                 /*Ref count has already been increased. Just continue*/
557                                                 break;
558                                         }
559                                 }
560                                 if (j == MAX_CERT_TYPE)
561                                         unique_id[c++] = (info->cert_id)[i];
562                                 else
563                                         continue;
564                                 snprintf(vquery, len, "update package_cert_index_info set cert_ref_count=%d " \
565                                 "where cert_id=%d",  (info->ref_count)[i] + 1, (info->cert_id)[i]);
566                         }
567                         if (SQLITE_OK !=
568                             sqlite3_exec(cert_db, vquery, NULL, NULL, &error_message)) {
569                                 _LOGE("Don't execute query = %s error message = %s\n", vquery,
570                                        error_message);
571                                 sqlite3_free(error_message);
572                                 ret = PMINFO_R_ERROR;
573                                 goto err;
574                         }
575                 }
576         }
577         /*Commit transaction*/
578         ret = sqlite3_exec(cert_db, "COMMIT", NULL, NULL, NULL);
579         if (ret != SQLITE_OK) {
580                 _LOGE("Failed to commit transaction, Rollback now\n");
581                 ret = sqlite3_exec(cert_db, "ROLLBACK", NULL, NULL, NULL);
582                 if (ret != SQLITE_OK)
583                         _LOGE("Failed to commit transaction, Rollback now\n");
584
585                 ret = PMINFO_R_ERROR;
586                 goto err;
587         }
588         _LOGE("Transaction Commit and End\n");
589         ret =  PMINFO_R_OK;
590 err:
591         sqlite3_close(cert_db);
592         if (vquery) {
593                 free(vquery);
594                 vquery = NULL;
595         }
596         if (indexinfo) {
597                 free(indexinfo);
598                 indexinfo = NULL;
599         }
600         return ret;
601 }
602
603 API int pkgmgrinfo_destroy_certinfo_set_handle(pkgmgrinfo_instcertinfo_h handle)
604 {
605         retvm_if(handle == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
606         int i = 0;
607         pkgmgr_instcertinfo_x *certinfo = NULL;
608         certinfo = (pkgmgr_instcertinfo_x *)handle;
609         if (certinfo->pkgid) {
610                 free(certinfo->pkgid);
611                 certinfo->pkgid = NULL;
612         }
613         for (i = 0; i < MAX_CERT_TYPE; i++) {
614                 if ((certinfo->cert_info)[i]) {
615                         free((certinfo->cert_info)[i]);
616                         (certinfo->cert_info)[i] = NULL;
617                 }
618         }
619         free(certinfo);
620         certinfo = NULL;
621         return PMINFO_R_OK;
622 }
623
624 API int pkgmgrinfo_delete_certinfo(const char *pkgid)
625 {
626         retvm_if(pkgid == NULL, PMINFO_R_EINVAL, "Argument supplied is NULL\n");
627         int ret = -1;
628         /*Open db.*/
629         ret = db_util_open_with_options(CERT_DB, &cert_db, SQLITE_OPEN_READWRITE, NULL);
630         if (ret != SQLITE_OK) {
631                 _LOGE("connect db [%s] failed!\n", CERT_DB);
632                 ret = PMINFO_R_ERROR;
633                 goto err;
634         }
635         /*Begin Transaction*/
636         ret = sqlite3_exec(cert_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
637         if (ret != SQLITE_OK) {
638                 _LOGE("Failed to begin transaction\n");
639                 ret = PMINFO_R_ERROR;
640                 goto err;
641         }
642         _LOGE("Transaction Begin\n");
643         ret = __delete_certinfo(pkgid);
644         if (ret < 0) {
645                 _LOGE("Certificate Deletion Failed\n");
646         } else {
647                 _LOGE("Certificate Deletion Success\n");
648         }
649         /*Commit transaction*/
650         ret = sqlite3_exec(cert_db, "COMMIT", NULL, NULL, NULL);
651         if (ret != SQLITE_OK) {
652                 _LOGE("Failed to commit transaction, Rollback now\n");
653                 ret = sqlite3_exec(cert_db, "ROLLBACK", NULL, NULL, NULL);
654                 if (ret != SQLITE_OK)
655                         _LOGE("Failed to commit transaction, Rollback now\n");
656
657                 ret = PMINFO_R_ERROR;
658                 goto err;
659         }
660         _LOGE("Transaction Commit and End\n");
661         ret = PMINFO_R_OK;
662 err:
663         sqlite3_close(cert_db);
664         return ret;
665 }
666