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