Add rule for 'All devices apps' when access rule is empty.
[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 <cstdio>
19 #include <cstring>
20 #include <cstdlib>
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         int SignatureHelper::getPackageName(int pid, char *package, size_t length)
43         {
44                 return aul_app_get_pkgname_bypid(pid, package, length);
45         }
46
47         const ByteArray SignatureHelper::getCertificationHash(const char *packageName)
48         {
49                 ByteArray result;
50                 int ret = 0;
51                 pkgmgr_certinfo_h handle = NULL;
52                 pkgmgrinfo_appinfo_h handle_appinfo;
53                 char *pkgid = NULL;
54
55                 if(pkgmgrinfo_appinfo_get_appinfo(packageName, &handle_appinfo) != PMINFO_R_OK)
56                 {
57                         _ERR("pkgmgrinfo_appinfo_get_appinfo fail");
58                         return result;
59                 }
60
61                 if(pkgmgrinfo_appinfo_get_pkgid(handle_appinfo, &pkgid) != PMINFO_R_OK)
62                 {
63                         pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
64                         _ERR("pkgmgrinfo_appinfo_get_pkgid fail");
65                         return result;
66                 }
67                 pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
68
69                 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
70                 {
71                         if ((ret = pkgmgr_pkginfo_load_certinfo(pkgid, handle)) == 0)
72                         {
73                                 int type;
74
75                                 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
76                                 {
77                                         const char *value = NULL;
78
79                                         if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
80                                         {
81                                                 if (value != NULL && strlen(value) > 0)
82                                                 {
83                                                         OpensslHelper::decodeBase64String(value, result, false);
84                                                         if (result.size() > 0)
85                                                         {
86                                                                 _DBG("type [%d] hash [%d] : %s", type, result.size(), result.toString().c_str());
87                                                                 break;
88                                                         }
89                                                 }
90                                         }
91                                 }
92                         }
93                         else
94                         {
95                                 _ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
96                         }
97
98                         pkgmgr_pkginfo_destroy_certinfo(handle);
99                 }
100                 else
101                 {
102                         _ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
103                 }
104
105                 return result;
106         }
107
108         const ByteArray SignatureHelper::getCertificationHash(int pid)
109         {
110                 ByteArray result;
111                 int error = 0;
112                 char pkgName[256] = { 0, };
113
114                 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
115                 {
116                         result = getCertificationHash(pkgName);
117                 }
118                 else
119                 {
120                         _ERR("aul_app_get_pkgname_bypid failed [%d]", error);
121                 }
122
123                 return result;
124         }
125
126         bool SignatureHelper::getCertificationHashes(int pid, vector<ByteArray> &certHashes)
127         {
128                 bool result = false;
129                 int error = 0;
130                 char pkgName[256] = { 0, };
131
132                 if ((error = aul_app_get_pkgname_bypid(pid, pkgName, sizeof(pkgName))) == 0)
133                 {
134                         result = getCertificationHashes(pkgName, certHashes);
135                 }
136                 else
137                 {
138                         _ERR("aul_app_get_pkgname_bypid failed [%d]", error);
139                 }
140
141                 return result;
142         }
143
144         bool SignatureHelper::getCertificationHashes(const char *packageName, vector<ByteArray> &certHashes)
145         {
146                 bool result = false;
147                 int ret = 0;
148                 pkgmgr_certinfo_h handle = NULL;
149                 pkgmgrinfo_appinfo_h handle_appinfo;
150                 char *pkgid = NULL;
151
152                 if(pkgmgrinfo_appinfo_get_appinfo(packageName, &handle_appinfo) != PMINFO_R_OK)
153                 {
154                         _ERR("pkgmgrinfo_appinfo_get_appinfo fail");
155                         return result;
156                 }
157
158                 if(pkgmgrinfo_appinfo_get_pkgid(handle_appinfo, &pkgid) != PMINFO_R_OK)
159                 {
160                         pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
161                         _ERR("pkgmgrinfo_appinfo_get_pkgid fail");
162                         return result;
163                 }
164
165                 if ((ret = pkgmgr_pkginfo_create_certinfo(&handle)) == 0)
166                 {
167                         if ((ret = pkgmgr_pkginfo_load_certinfo(pkgid, handle)) == 0)
168                         {
169                                 int type;
170
171                                 for (type = (int)PM_AUTHOR_ROOT_CERT; type <= (int)PM_DISTRIBUTOR2_SIGNER_CERT; type++)
172                                 {
173                                         const char *value = NULL;
174
175                                         if ((ret = pkgmgr_pkginfo_get_cert_value(handle, (pkgmgr_cert_type)type, &value)) == 0)
176                                         {
177                                                 if (value != NULL && strlen(value) > 0)
178                                                 {
179                                                         ByteArray decodeValue, hash;
180
181                                                         OpensslHelper::decodeBase64String(value, decodeValue, false);
182                                                         if (decodeValue.size() > 0)
183                                                         {
184                                                                 OpensslHelper::digestBuffer("sha1", decodeValue.getBuffer(), decodeValue.size(), hash);
185                                                                 if(hash.size() > 0)
186                                                                 {
187                                                                         _DBG("type [%d] hash [%d] : %s", type, hash.size(), hash.toString().c_str());
188                                                                         certHashes.push_back(hash);
189                                                                 }
190                                                         }
191                                                 }
192                                         }
193                                 }
194
195                                 result = true;
196                         }
197                         else
198                         {
199                                 _ERR("pkgmgr_pkginfo_load_certinfo failed [%d]", ret);
200                         }
201
202                         pkgmgrinfo_appinfo_destroy_appinfo(handle_appinfo);
203
204                         pkgmgr_pkginfo_destroy_certinfo(handle);
205                 }
206                 else
207                 {
208                         _ERR("pkgmgr_pkginfo_create_certinfo failed [%d]", ret);
209                 }
210
211                 return result;
212         }
213 } /* namespace smartcard_service_api */
214
215 /* export C API */
216 using namespace smartcard_service_api;
217
218 certiHash *__signature_helper_vector_to_linked_list(vector<ByteArray> &certHashes)
219 {
220         vector<ByteArray>::iterator item;
221         certiHash *head, *tail, *tmp;
222
223         head = tail = NULL;
224
225         for (item = certHashes.begin(); item != certHashes.end(); item++)
226         {
227                 if ((tmp = (certiHash *)calloc(1, sizeof(certiHash))) == NULL)
228                         goto ERROR;
229
230                 tmp->length = (*item).size();
231
232                 if ((tmp->value = (uint8_t *)calloc(tmp->length, sizeof(uint8_t))) == NULL)
233                 {
234                         free(tmp);
235                         goto ERROR;
236                 }
237
238                 memcpy(tmp->value, (*item).getBuffer(), tmp->length);
239                 tmp->next = NULL;
240
241                 if (head == NULL)
242                 {
243                         head = tail = tmp;
244                 }
245                 else
246                 {
247                         tail->next = tmp;
248                         tail = tmp;
249                 }
250         }
251         return head;
252
253 ERROR :
254         _ERR("alloc fail");
255
256         while (head)
257         {
258                 tmp = head;
259                 head = head->next;
260                 if (tmp->value != NULL)
261                         free(tmp->value);
262                 free(tmp);
263         }
264
265         return NULL;
266 }
267
268 EXTERN_API int signature_helper_get_certificate_hashes(const char *packageName, certiHash **hash)
269 {
270         int ret = -1;
271         vector<ByteArray> hashes;
272
273         if (packageName == NULL)
274                 return ret;
275
276         if (SignatureHelper::getCertificationHashes(packageName, hashes) == true)
277         {
278                 *hash = __signature_helper_vector_to_linked_list(hashes);
279                 ret = 0;
280         }
281
282         return ret;
283 }
284
285 EXTERN_API int signature_helper_get_certificate_hashes_by_pid(int pid, certiHash **hash)
286 {
287         int ret = -1;
288         vector<ByteArray> hashes;
289
290         if (pid <= 0)
291                 return ret;
292
293         if (SignatureHelper::getCertificationHashes(pid, hashes) == true)
294         {
295                 *hash = __signature_helper_vector_to_linked_list(hashes);
296                 ret = 0;
297         }
298
299         return ret;
300 }
301