Adjust logging level
[platform/core/connectivity/smartcard-service.git] / common / SignatureHelper.cpp
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /* standard library header */
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <list>
23 #include <string>
24 #include <vector>
25
26 /* SLP library header */
27 #include "package-manager.h"
28 #include "pkgmgr-info.h"
29 #include "aul.h"
30
31 /* local header */
32 #include "Debug.h"
33 #include "SignatureHelper.h"
34 #include "OpensslHelper.h"
35
36 #ifndef EXTERN_API
37 #define EXTERN_API __attribute__((visibility("default")))
38 #endif
39
40 namespace smartcard_service_api
41 {
42         ByteArray SignatureHelper::getCertificationHash(const char *packageName)
43         {
44                 ByteArray result;
45                 int ret = 0;
46                 pkgmgr_certinfo_h handle = NULL;
47                 pkgmgrinfo_appinfo_h handle_appinfo;
48                 char *pkgid = NULL;
49
50                 if(pkgmgrinfo_appinfo_get_appinfo(packageName, &handle_appinfo) != PMINFO_R_OK)
51                 {
52                         _ERR("pkgmgrinfo_appinfo_get_appinfo fail");
53                         return result;
54                 }
55
56                 if(pkgmgrinfo_appinfo_get_pkgid(handle_appinfo, &pkgid) != PMINFO_R_OK)
57                 {
58                         pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
59                         _ERR("pkgmgrinfo_appinfo_get_pkgid fail");
60                         return result;
61                 }
62                 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
63
64                 SCARD_DEBUG("package name : %s", pkgid);
65
66                 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
67                 {
68                         if ((ret = pkgmgr_pkginfo_load_certinfo(pkgid, handle)) == 0)
69                         {
70                                 int type;
71
72                                 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
73                                 {
74                                         const char *value = NULL;
75
76                                         if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
77                                         {
78                                                 if (value != NULL && strlen(value) > 0)
79                                                 {
80                                                         OpensslHelper::decodeBase64String(value, result, false);
81                                                         if (result.getLength() > 0)
82                                                         {
83                                                                 _DBG("type [%d] hash [%d] : %s", type, result.getLength(), result.toString());
84                                                                 break;
85                                                         }
86                                                 }
87                                         }
88                                 }
89                         }
90                         else
91                         {
92                                 _ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
93                         }
94
95                         pkgmgr_pkginfo_destroy_certinfo(handle);
96                 }
97                 else
98                 {
99                         _ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
100                 }
101
102                 return result;
103         }
104
105         ByteArray SignatureHelper::getCertificationHash(int pid)
106         {
107                 ByteArray result;
108                 int error = 0;
109                 char pkgName[256] = { 0, };
110
111                 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
112                 {
113                         result = getCertificationHash(pkgName);
114                 }
115                 else
116                 {
117                         _ERR("aul_app_get_pkgname_bypid failed [%d]", error);
118                 }
119
120                 return result;
121         }
122
123         bool SignatureHelper::getCertificationHashes(int pid, vector<ByteArray> &certHashes)
124         {
125                 bool result = false;
126                 int error = 0;
127                 char pkgName[256] = { 0, };
128
129                 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
130                 {
131                         result = getCertificationHashes(pkgName, certHashes);
132                 }
133                 else
134                 {
135                         _ERR("aul_app_get_pkgname_bypid failed [%d]", error);
136                 }
137
138                 return result;
139         }
140
141         bool SignatureHelper::getCertificationHashes(const char *packageName, vector<ByteArray> &certHashes)
142         {
143                 bool result = false;
144                 int ret = 0;
145                 pkgmgr_certinfo_h handle = NULL;
146                 pkgmgrinfo_appinfo_h handle_appinfo;
147                 char *pkgid = NULL;
148
149                 if(pkgmgrinfo_appinfo_get_appinfo(packageName, &handle_appinfo) != PMINFO_R_OK)
150                 {
151                         _ERR("pkgmgrinfo_appinfo_get_appinfo fail");
152                         return result;
153                 }
154
155                 if(pkgmgrinfo_appinfo_get_pkgid(handle_appinfo, &pkgid) != PMINFO_R_OK)
156                 {
157                         pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
158                         _ERR("pkgmgrinfo_appinfo_get_pkgid fail");
159                         return result;
160                 }
161
162                 SCARD_DEBUG("package name : %s", pkgid);
163
164                 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
165                 {
166                         if ((ret = pkgmgr_pkginfo_load_certinfo(pkgid, handle)) == 0)
167                         {
168                                 int type;
169
170                                 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
171                                 {
172                                         const char *value = NULL;
173
174                                         if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
175                                         {
176                                                 if (value != NULL && strlen(value) > 0)
177                                                 {
178                                                         ByteArray decodeValue, hash;
179
180                                                         OpensslHelper::decodeBase64String(value, decodeValue, false);
181                                                         if (decodeValue.getLength() > 0)
182                                                         {
183                                                                 OpensslHelper::digestBuffer("sha1", decodeValue.getBuffer(), decodeValue.getLength(), hash);
184                                                                 if(hash.getLength() > 0)
185                                                                 {
186                                                                         _DBG("type [%d] hash [%d] : %s", type, hash.getLength(), hash.toString());
187                                                                         certHashes.push_back(hash);
188                                                                 }
189                                                         }
190                                                 }
191                                         }
192                                 }
193
194                                 result = true;
195                         }
196                         else
197                         {
198                                 _ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
199                         }
200
201                         pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
202
203                         pkgmgr_pkginfo_destroy_certinfo(handle);
204                 }
205                 else
206                 {
207                         _ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
208                 }
209
210                 return result;
211         }
212 } /* namespace smartcard_service_api */
213
214 /* export C API */
215 using namespace smartcard_service_api;
216
217 certiHash *__signature_helper_vector_to_linked_list(vector<ByteArray> &certHashes)
218 {
219         vector<ByteArray>::iterator item;
220         certiHash *head, *tail, *tmp;
221
222         head = tail = NULL;
223
224         for (item = certHashes.begin(); item != certHashes.end(); item++)
225         {
226                 if ((tmp = (certiHash *)calloc(1, sizeof(certiHash))) == NULL)
227                         goto ERROR;
228
229                 tmp->length = (*item).getLength();
230
231                 if ((tmp->value = (uint8_t *)calloc(tmp->length, sizeof(uint8_t))) == NULL)
232                 {
233                         free(tmp);
234                         goto ERROR;
235                 }
236
237                 memcpy(tmp->value, (*item).getBuffer(), tmp->length);
238                 tmp->next = NULL;
239
240                 if (head == NULL)
241                 {
242                         head = tail = tmp;
243                 }
244                 else
245                 {
246                         tail->next = tmp;
247                         tail = tmp;
248                 }
249         }
250         return head;
251
252 ERROR :
253         _ERR("alloc fail");
254
255         while (head)
256         {
257                 tmp = head;
258                 head = head->next;
259                 if (tmp->value != NULL)
260                         free(tmp->value);
261                 free(tmp);
262         }
263
264         return NULL;
265 }
266
267 EXTERN_API int signature_helper_get_certificate_hashes(const char *packageName, certiHash **hash)
268 {
269         int ret = -1;
270         vector<ByteArray> hashes;
271
272         if (packageName == NULL)
273                 return ret;
274
275         if (SignatureHelper::getCertificationHashes(packageName, hashes) == true)
276         {
277                 *hash = __signature_helper_vector_to_linked_list(hashes);
278                 ret = 0;
279         }
280
281         return ret;
282 }
283
284 EXTERN_API int signature_helper_get_certificate_hashes_by_pid(int pid, certiHash **hash)
285 {
286         int ret = -1;
287         vector<ByteArray> hashes;
288
289         if (pid <= 0)
290                 return ret;
291
292         if (SignatureHelper::getCertificationHashes(pid, hashes) == true)
293         {
294                 *hash = __signature_helper_vector_to_linked_list(hashes);
295                 ret = 0;
296         }
297
298         return ret;
299 }
300