1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "cm_ipc_service.h"
17 
18 #include "cm_log.h"
19 #include "cm_mem.h"
20 #include "cm_ipc_service_serialization.h"
21 
22 #include "cm_param.h"
23 #include "cm_pfx.h"
24 #include "cm_report_wrapper.h"
25 #include "cm_response.h"
26 #include "cm_security_guard_info.h"
27 #include "cm_type.h"
28 
29 #include "cert_manager.h"
30 #include "cert_manager_check.h"
31 #include "cert_manager_key_operation.h"
32 #include "cert_manager_permission_check.h"
33 #include "cert_manager_query.h"
34 #include "cert_manager_service.h"
35 #include "cert_manager_status.h"
36 #include "cert_manager_uri.h"
37 #include "cert_manager_updateflag.h"
38 #include "cert_manager_storage.h"
39 #include "cert_manager_file_operator.h"
40 
41 #define MAX_LEN_CERTIFICATE     8196
42 
GetInputParams(const struct CmBlob * paramSetBlob,struct CmParamSet ** paramSet,struct CmContext * cmContext,struct CmParamOut * params,uint32_t paramsCount)43 static int32_t GetInputParams(const struct CmBlob *paramSetBlob, struct CmParamSet **paramSet,
44     struct CmContext *cmContext, struct CmParamOut *params, uint32_t paramsCount)
45 {
46     int32_t ret = CmGetProcessInfoForIPC(cmContext);
47     if (ret != CM_SUCCESS) {
48         CM_LOG_E("get ipc info failed, ret = %d", ret);
49         return ret;
50     }
51 
52     /* The paramSet blob pointer needs to be refreshed across processes. */
53     ret = CmGetParamSet((struct CmParamSet *)paramSetBlob->data, paramSetBlob->size, paramSet);
54     if (ret != CM_SUCCESS) {
55         CM_LOG_E("get paramSet failed, ret = %d", ret);
56         return ret;
57     }
58 
59     ret = CmParamSetToParams(*paramSet, params, paramsCount);
60     if (ret != CM_SUCCESS) {
61         CM_LOG_E("get params from paramSet failed, ret = %d", ret);
62     }
63 
64     return ret;
65 }
66 
CmIpcServiceGetCertificateList(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)67 void CmIpcServiceGetCertificateList(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
68     const struct CmContext *context)
69 {
70     int32_t ret;
71     uint32_t store;
72     struct CmContext cmContext = {0};
73     struct CmMutableBlob certFileList = { 0, NULL };
74     struct CmParamSet *paramSet = NULL;
75     struct CmParamOut params[] = {
76         { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = &store },
77     };
78 
79     do {
80         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
81         if (ret != CM_SUCCESS) {
82             CM_LOG_E("GetCaCertList get input params failed, ret = %d", ret);
83             break;
84         }
85 
86         ret = CmServiceGetSystemCertListCheck(store);
87         if (ret != CM_SUCCESS) {
88             CM_LOG_E("CmIpcServiceGetSystemCertCheck fail, ret = %d", ret);
89             break;
90         }
91 
92         ret = CmServiceGetCertList(&cmContext, store, &certFileList);
93         if (ret != CM_SUCCESS) {
94             CM_LOG_E("get cert list failed, ret = %d", ret);
95             break;
96         }
97 
98         ret = CmServiceGetCertListPack(&cmContext, store, &certFileList, outData);
99         if (ret != CM_SUCCESS) {
100             CM_LOG_E("cert list data pack fail, ret = %d", ret);
101             break;
102         }
103 
104         CmSendResponse(context, ret, outData);
105     } while (0);
106     CmReport(__func__, &cmContext, NULL, ret);
107 
108     if (ret != CM_SUCCESS) {
109         CmSendResponse(context, ret, NULL);
110     }
111 
112     if (certFileList.data != NULL) {
113         CmFreeCertFiles((struct CertFileInfo *)certFileList.data, certFileList.size);
114     }
115     CmFreeParamSet(&paramSet);
116 }
117 
CmIpcServiceGetCertificateInfo(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)118 void CmIpcServiceGetCertificateInfo(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
119     const struct CmContext *context)
120 {
121     int32_t ret;
122     uint32_t status = 0;
123     uint32_t store;
124     struct CmContext cmContext = {0};
125     struct CmBlob certificateData = { 0, NULL };
126     struct CmBlob certUri = { 0, NULL };
127     struct CmParamSet *paramSet = NULL;
128     struct CmParamOut params[] = {
129         { .tag = CM_TAG_PARAM0_BUFFER, .blob = &certUri},
130         { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = &store},
131     };
132 
133     do {
134         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
135         if (ret != CM_SUCCESS) {
136             CM_LOG_E("GetUserCertInfo get input params failed, ret = %d", ret);
137             break;
138         }
139 
140         ret = CmServiceGetSystemCertCheck(store, &certUri);
141         if (ret != CM_SUCCESS) {
142             CM_LOG_E("CmServiceGetSystemCertCheck failed, ret = %d", ret);
143             break;
144         }
145 
146         ret = CmServiceGetCertInfo(&cmContext, &certUri, store, &certificateData, &status);
147         if (ret != CM_SUCCESS) {
148             CM_LOG_E("get cert info failed, ret = %d", ret);
149             break;
150         }
151 
152         ret = CmServiceGetCertInfoPack(store, &certificateData, status, &certUri, outData);
153         if (ret != CM_SUCCESS) {
154             CM_LOG_E("cert info data pack failed, ret = %d", ret);
155             break;
156         }
157 
158         CmSendResponse(context, ret, outData);
159     } while (0);
160     CmReport(__func__, &cmContext, &certUri, ret);
161 
162     if (ret != CM_SUCCESS) {
163         CmSendResponse(context, ret, NULL);
164     }
165     CM_FREE_BLOB(certificateData);
166     CmFreeParamSet(&paramSet);
167 }
168 
CmIpcServiceSetCertStatus(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)169 void CmIpcServiceSetCertStatus(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
170     const struct CmContext *context)
171 {
172     int32_t ret;
173     uint32_t store = CM_SYSTEM_TRUSTED_STORE;
174     uint32_t status = INIT_INVALID_VALUE;
175     struct CmContext cmContext = {0};
176     struct CmBlob certUri = { 0, NULL };
177     struct CmParamSet *paramSet = NULL;
178     struct CmParamOut params[] = {
179         { .tag = CM_TAG_PARAM0_BUFFER, .blob = &certUri},
180         { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = &store},
181         { .tag = CM_TAG_PARAM1_UINT32, .uint32Param = &status},
182     };
183 
184     do {
185         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
186         if (ret != CM_SUCCESS) {
187             CM_LOG_E("SetUserCertStatus get input params failed, ret = %d", ret);
188             break;
189         }
190 
191         ret = CmServiceSetCertStatusCheck(store, &certUri, status);
192         if (ret != CM_SUCCESS) {
193             CM_LOG_E("CmServiceSetCertStatusCheck check failed, ret = %d", ret);
194             break;
195         }
196 
197         ret = CmServiceSetCertStatus(&cmContext, &certUri, store, status);
198         if (ret != CM_SUCCESS) {
199             CM_LOG_E("set system cert status failed, ret = %d", ret);
200             break;
201         }
202     } while (0);
203     CmReport(__func__, &cmContext, &certUri, ret);
204 
205     CmSendResponse(context, ret, NULL);
206     CmReportSGSetCertStatus(&certUri, store, status, ret);
207     CmFreeParamSet(&paramSet);
208     CM_LOG_D("CmIpcServiceSetCertStatus end:%d", ret);
209 }
210 
CmIpcServiceInstallAppCert(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)211 void CmIpcServiceInstallAppCert(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
212     const struct CmContext *context)
213 {
214     uint32_t store = CM_CREDENTIAL_STORE;
215     uint32_t userId = 0;
216     struct CmBlob appCert = { 0, NULL };
217     struct CmBlob appCertPwd = { 0, NULL };
218     struct CmBlob certAlias = { 0, NULL };
219     struct CmParamOut params[] = {
220         { .tag = CM_TAG_PARAM0_BUFFER, .blob = &appCert },
221         { .tag = CM_TAG_PARAM1_BUFFER, .blob = &appCertPwd },
222         { .tag = CM_TAG_PARAM2_BUFFER, .blob = &certAlias },
223         { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = &store },
224         { .tag = CM_TAG_PARAM1_UINT32, .uint32Param = &userId },
225     };
226 
227     int32_t ret;
228     struct CmContext cmContext = { 0 };
229     struct CmParamSet *paramSet = NULL;
230     do {
231         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
232         if (ret != CM_SUCCESS) {
233             CM_LOG_E("install app cert get input params failed, ret = %d", ret);
234             break;
235         }
236 
237         struct CmAppCertParam certParam = { &appCert, &appCertPwd, &certAlias, store, userId };
238         ret = CmServicInstallAppCert(&cmContext, &certParam, outData);
239         if (ret != CM_SUCCESS) {
240             CM_LOG_E("service install app cert failed, ret = %d", ret);
241             break;
242         }
243     } while (0);
244 
245     struct CmBlob tempBlob = { 0, NULL };
246     CmReport(__func__, &cmContext, &tempBlob, ret);
247 
248     CmSendResponse(context, ret, outData);
249     CmReportSGInstallAppCert(&certAlias, store, ret);
250     CmFreeParamSet(&paramSet);
251     CM_LOG_D("CmIpcServiceInstallAppCert end:%d", ret);
252 }
253 
CmIpcServiceUninstallAppCert(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)254 void CmIpcServiceUninstallAppCert(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
255     const struct CmContext *context)
256 {
257     int32_t ret;
258     (void)outData;
259     uint32_t store = CM_CREDENTIAL_STORE;
260     struct CmParamSet *paramSet = NULL;
261     struct CmBlob keyUri = { 0, NULL };
262     struct CmContext cmContext = {0};
263 
264     struct CmParamOut params[] = {
265         {
266             .tag = CM_TAG_PARAM0_BUFFER,
267             .blob = &keyUri
268         }, {
269             .tag = CM_TAG_PARAM0_UINT32,
270             .uint32Param = &store
271         },
272     };
273 
274     do {
275         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
276         if (ret != CM_SUCCESS) {
277             CM_LOG_E("UninstallAppCert get input params failed, ret = %d", ret);
278             break;
279         }
280 
281         ret = CmServiceUninstallAppCertCheck(&cmContext, store, &keyUri);
282         if (ret != CM_SUCCESS) {
283             CM_LOG_E("UninstallAppCert CmServiceGetSystemCertCheck failed, ret = %d", ret);
284             break;
285         }
286 
287         ret = CmRemoveAppCert(&cmContext, &keyUri, store);
288         if (ret != CM_SUCCESS) {
289             CM_LOG_E("CmRemoveAppCert fail");
290         }
291     } while (0);
292 
293     CmReport(__func__, &cmContext, &keyUri, ret);
294     CmSendResponse(context, ret, NULL);
295     CmReportSGUninstallAppCert(&keyUri, store, false, ret);
296     CmFreeParamSet(&paramSet);
297     CM_LOG_D("CmIpcServiceUninstallAppCert end:%d", ret);
298 }
299 
CmIpcServiceUninstallAllAppCert(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)300 void CmIpcServiceUninstallAllAppCert(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
301     const struct CmContext *context)
302 {
303     (void)outData;
304     (void)paramSetBlob;
305     int32_t ret = CM_SUCCESS;
306     struct CmContext cmContext = {0};
307 
308     ret = CmGetProcessInfoForIPC(&cmContext);
309     if (ret != CM_SUCCESS) {
310         CM_LOG_E("CmGetProcessInfoForIPC fail, ret = %d", ret);
311     }
312 
313     ret = CmRemoveAllAppCert(&cmContext);
314     if (ret != CM_SUCCESS) {
315         CM_LOG_E("CmRemoveAllAppCert fail");
316     }
317 
318     CmReport(__func__, &cmContext, NULL, ret);
319     CmSendResponse(context, ret, NULL);
320     CmReportSGUninstallAppCert(NULL, INIT_INVALID_VALUE, true, ret);
321     CM_LOG_D("CmIpcServiceUninstallAllAppCert end:%d", ret);
322 }
323 
GetAppCertInfo(const struct CmBlob * keyUri,struct CmBlob * certType,struct CmBlob * certUri,struct CmBlob * cerAlias)324 static int32_t GetAppCertInfo(const struct CmBlob *keyUri, struct CmBlob *certType,
325     struct CmBlob *certUri, struct CmBlob *cerAlias)
326 {
327     int32_t ret;
328     struct CMUri uri;
329     (void)memset_s(&uri, sizeof(struct CMUri), 0, sizeof(struct CMUri));
330 
331     do {
332         ret = CertManagerUriDecode(&uri, (char *)keyUri->data);
333         if (ret != CM_SUCCESS) {
334             CM_LOG_E("CertManagerUriDecode failed");
335             break;
336         }
337         if ((uri.type >= TYPE_COUNT) || (uri.object == NULL)) {
338             CM_LOG_E("uri's type[%u] or object is invalid after decode", uri.type);
339             ret = CMR_ERROR;
340             break;
341         }
342 
343         if (memcpy_s(certType->data, certType->size, g_types[uri.type], strlen(g_types[uri.type]) + 1) != EOK) {
344             CM_LOG_E("Failed to copy certType->data");
345             ret = CMR_ERROR;
346             break;
347         }
348         certType->size = strlen(g_types[uri.type]) + 1;
349 
350         if (memcpy_s(certUri->data, certUri->size, keyUri->data, keyUri->size) != EOK) {
351             CM_LOG_E("Failed to copy certUri->data");
352             ret = CMR_ERROR;
353             break;
354         }
355         certUri->size = keyUri->size;
356 
357         ret = CmGetDisplayNameByURI(keyUri, uri.object, cerAlias);
358         if (ret != CM_SUCCESS) {
359             CM_LOG_E("Failed to CMGetDisplayNameByURI");
360             break;
361         }
362     } while (0);
363 
364     CertManagerFreeUri(&uri);
365     return ret;
366 }
367 
CmCertListGetAppCertInfo(const struct CmBlob * fileName,struct CmBlob * certType,struct CmBlob * certUri,struct CmBlob * certAlias)368 static int32_t CmCertListGetAppCertInfo(const struct CmBlob *fileName, struct CmBlob *certType,
369     struct CmBlob *certUri,  struct CmBlob *certAlias)
370 {
371     char uriBuf[MAX_LEN_URI] = {0};
372     struct CmBlob keyUri = { sizeof(uriBuf), (uint8_t *)uriBuf };
373 
374     int32_t ret = CmGetUri((char *)fileName->data, &keyUri);
375     if (ret != CM_SUCCESS) {
376         CM_LOG_E("Get uri failed");
377         return ret;
378     }
379 
380     ret = GetAppCertInfo(&keyUri, certType, certUri, certAlias);
381     if (ret != CM_SUCCESS) {
382         CM_LOG_E("GetAppCertInfo failed");
383         return ret;
384     }
385 
386     return ret;
387 }
388 
CmServiceGetAppCertListPack(struct CmBlob * certificateList,const struct CmBlob * fileNames,const uint32_t fileCount)389 static int32_t CmServiceGetAppCertListPack(struct CmBlob *certificateList, const struct CmBlob *fileNames,
390     const uint32_t fileCount)
391 {
392     /* buff struct: cert count + (cert type +  cert uri +  cert alias) * MAX_CERT_COUNT */
393     uint32_t buffSize = sizeof(uint32_t) + (sizeof(uint32_t) + MAX_LEN_SUBJECT_NAME + sizeof(uint32_t) +
394         MAX_LEN_URI + sizeof(uint32_t) + MAX_LEN_CERT_ALIAS) * MAX_COUNT_CERTIFICATE;
395     certificateList->data = (uint8_t *)CmMalloc(buffSize);
396     if (certificateList->data == NULL) {
397         return CMR_ERROR_MALLOC_FAIL;
398     }
399     certificateList->size = buffSize;
400 
401     uint32_t offset = 0;
402     int32_t ret = CopyUint32ToBuffer(fileCount, certificateList, &offset);
403     if (ret != CM_SUCCESS) {
404         CM_LOG_E("Copy certificate count failed");
405         return ret;
406     }
407 
408     for (uint32_t i = 0; i < fileCount; i++) {
409         uint8_t typeBuf[MAX_LEN_SUBJECT_NAME] = {0};
410         struct CmBlob certType = { sizeof(typeBuf), typeBuf };
411         uint8_t certUriBuf[MAX_LEN_URI] = {0};
412         struct CmBlob certUri = { sizeof(certUriBuf), certUriBuf };
413         uint8_t aliasBuf[MAX_LEN_CERT_ALIAS] = {0};
414         struct CmBlob certAlias = { sizeof(aliasBuf), aliasBuf };
415 
416         ret = CmCertListGetAppCertInfo(&fileNames[i], &certType, &certUri, &certAlias);
417         if (ret != CM_SUCCESS) {
418             CM_LOG_E("CmCertListGetAppCertInfo failed");
419             return ret;
420         }
421 
422         ret = CopyBlobToBuffer(&certType, certificateList, &offset);
423         if (ret != CM_SUCCESS) {
424             CM_LOG_E("Copy certType failed");
425             return ret;
426         }
427 
428         ret = CopyBlobToBuffer(&certUri, certificateList, &offset);
429         if (ret != CM_SUCCESS) {
430             CM_LOG_E("Copy certUri failed");
431             return ret;
432         }
433 
434         ret = CopyBlobToBuffer(&certAlias, certificateList, &offset);
435         if (ret != CM_SUCCESS) {
436             CM_LOG_E("Copy certAlies failed");
437             return ret;
438         }
439     }
440 
441     return ret;
442 }
443 
CmIpcServiceGetAppCertList(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)444 void CmIpcServiceGetAppCertList(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
445     const struct CmContext *context)
446 {
447     int32_t ret;
448     (void)outData;
449     uint32_t store;
450     uint32_t fileCount = 0;
451     struct CmContext cmContext = {0};
452     struct CmBlob certificateList = { 0, NULL };
453     struct CmBlob fileNames[MAX_COUNT_CERTIFICATE];
454     uint32_t len = MAX_COUNT_CERTIFICATE * sizeof(struct CmBlob);
455     (void)memset_s(fileNames, len, 0, len);
456     struct CmParamSet *paramSet = NULL;
457     struct CmParamOut params[] = {
458         { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = &store },
459     };
460 
461     do {
462         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
463         if (ret != CM_SUCCESS) {
464             CM_LOG_E("CmIpcServiceGetAppCertList get input params failed, ret = %d", ret);
465             break;
466         }
467 
468         ret = CmServiceGetAppCertListCheck(&cmContext, store);
469         if (ret != CM_SUCCESS) {
470             CM_LOG_E("CmServiceGetAppCertListCheck fail, ret = %d", ret);
471             break;
472         }
473 
474         ret = CmServiceGetAppCertList(&cmContext, store, fileNames, MAX_COUNT_CERTIFICATE, &fileCount);
475         if (ret != CM_SUCCESS) {
476             CM_LOG_E("Get App cert list fail, ret = %d", ret);
477             break;
478         }
479 
480         ret = CmServiceGetAppCertListPack(&certificateList, fileNames, fileCount);
481         if (ret != CM_SUCCESS) {
482             CM_LOG_E("CmServiceGetAppCertListPack pack fail, ret = %d", ret);
483         }
484     } while (0);
485 
486     CmReport(__func__, &cmContext, NULL, ret);
487     CmSendResponse(context, ret, &certificateList);
488     CmFreeParamSet(&paramSet);
489     CmFreeFileNames(fileNames, fileCount);
490     CM_FREE_BLOB(certificateList);
491     CM_LOG_D("CmIpcServiceGetAppCertList end:%d", ret);
492 }
493 
CmIpcServiceGetCallingAppCertList(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)494 void CmIpcServiceGetCallingAppCertList(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
495     const struct CmContext *context)
496 {
497     int32_t ret;
498     (void)outData;
499     uint32_t store;
500     uint32_t fileCount = 0;
501     struct CmContext cmContext = {0};
502     struct CmBlob certificateList = { 0, NULL };
503     struct CmBlob fileNamesBlob[MAX_COUNT_CERTIFICATE];
504     uint32_t len = MAX_COUNT_CERTIFICATE * sizeof(struct CmBlob);
505     (void)memset_s(fileNamesBlob, len, 0, len);
506     struct CmParamSet *paramSets = NULL;
507     struct CmParamOut params[] = {
508         { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = &store },
509     };
510 
511     do {
512         ret = GetInputParams(paramSetBlob, &paramSets, &cmContext, params, CM_ARRAY_SIZE(params));
513         if (ret != CM_SUCCESS) {
514             CM_LOG_E("CmIpcServiceGetCallingAppCertList get input params failed, ret = %d", ret);
515             break;
516         }
517 
518         ret = CmServiceGetCallingAppCertListCheck(&cmContext, store);
519         if (ret != CM_SUCCESS) {
520             CM_LOG_E("CmServiceGetCallingAppCertListCheck fail, ret = %d", ret);
521             break;
522         }
523 
524         ret = CmServiceGetCallingAppCertList(&cmContext, store, fileNamesBlob, MAX_COUNT_CERTIFICATE, &fileCount);
525         if (ret != CM_SUCCESS) {
526             CM_LOG_E("Get calling App cert list fail, ret = %d", ret);
527             break;
528         }
529 
530         ret = CmServiceGetAppCertListPack(&certificateList, fileNamesBlob, fileCount);
531         if (ret != CM_SUCCESS) {
532             CM_LOG_E("CmServiceGetAppCertListPack pack fail, ret = %d", ret);
533         }
534     } while (0);
535 
536     CmReport(__func__, &cmContext, NULL, ret);
537     CmSendResponse(context, ret, &certificateList);
538     CmFreeParamSet(&paramSets);
539     CmFreeFileNames(fileNamesBlob, fileCount);
540     CM_FREE_BLOB(certificateList);
541     CM_LOG_D("CmServiceGetCallingAppCertListCheck end:%d", ret);
542 }
543 
CopyCertificateInfoToBuffer(const struct CmBlob * certBlob,const struct CmBlob * certificateInfo,uint32_t * offset)544 static int32_t CopyCertificateInfoToBuffer(const struct CmBlob *certBlob,
545     const struct CmBlob *certificateInfo, uint32_t *offset)
546 {
547     if (certBlob->size < (sizeof(struct AppCert) - MAX_LEN_CERTIFICATE_CHAIN)) {
548         CM_LOG_E("certInfo size[%u] invalid", certBlob->size);
549         return CMR_ERROR_INVALID_ARGUMENT;
550     }
551 
552     struct AppCert *appCert = (struct AppCert *)certBlob->data;
553     if ((certBlob->size - (sizeof(struct AppCert) - MAX_LEN_CERTIFICATE_CHAIN)) < appCert->certSize) {
554         CM_LOG_E("certInfo data size[%u] invalid, certSize[%u]", certBlob->size, appCert->certSize);
555         return CMR_ERROR_INVALID_ARGUMENT;
556     }
557 
558     int32_t ret = CopyUint32ToBuffer(appCert->certCount, certificateInfo, offset);
559     if (ret != CM_SUCCESS) {
560         CM_LOG_E("copy appcert->certCount failed");
561         return ret;
562     }
563 
564     ret = CopyUint32ToBuffer(appCert->keyCount, certificateInfo, offset);
565     if (ret != CM_SUCCESS) {
566         CM_LOG_E("get appcert->keyCount failed");
567         return ret;
568     }
569 
570     struct CmBlob appCertBlob = { appCert->certSize, appCert->appCertdata };
571     ret = CopyBlobToBuffer(&appCertBlob, certificateInfo, offset);
572     if (ret != CM_SUCCESS) {
573         CM_LOG_E("Copy appCertBlob failed");
574     }
575 
576     return ret;
577 }
578 
CopyCertSize(const struct CmBlob * certBlob,const struct CmBlob * certificateInfo,uint32_t * offset)579 static int32_t CopyCertSize(const struct CmBlob *certBlob, const struct CmBlob *certificateInfo,
580     uint32_t *offset)
581 {
582     uint32_t certCount = (((certBlob->size > 0) && (certBlob->data != NULL)) ? 1 : 0);
583 
584     int32_t ret = CopyUint32ToBuffer(certCount, certificateInfo, offset);
585     if (ret != CM_SUCCESS) {
586         CM_LOG_E("copy certificateList->size failed");
587         return ret;
588     }
589     if (certCount == 0) {
590         CM_LOG_E("app cert not exist");
591         return CMR_ERROR_NOT_EXIST;
592     }
593     return ret;
594 }
595 
CmAppCertificateInfoPack(struct CmBlob * certificateInfo,const struct CmBlob * certBlob,const struct CmBlob * keyUri)596 static int32_t CmAppCertificateInfoPack(struct CmBlob *certificateInfo,
597     const struct CmBlob *certBlob, const struct CmBlob *keyUri)
598 {
599     /* buff struct: certCount + certType + certAlias + certUri + certNum + keyNum + credData */
600     uint32_t buffSize = sizeof(uint32_t) + sizeof(uint32_t) + MAX_LEN_SUBJECT_NAME +
601         sizeof(uint32_t) + MAX_LEN_CERT_ALIAS + sizeof(uint32_t) + MAX_LEN_URI +
602         sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint32_t) + MAX_LEN_CERTIFICATE_CHAIN;
603     certificateInfo->data = (uint8_t *)CmMalloc(buffSize);
604     if (certificateInfo->data == NULL) {
605         return CMR_ERROR_MALLOC_FAIL;
606     }
607     certificateInfo->size = buffSize;
608 
609     uint32_t offset = 0;
610     if (CopyCertSize(certBlob, certificateInfo, &offset) != CM_SUCCESS) {
611         return CMR_ERROR_NOT_EXIST;
612     }
613 
614     uint8_t typeBuf[MAX_LEN_SUBJECT_NAME] = {0};
615     uint8_t certUriBuf[MAX_LEN_URI] = {0};
616     uint8_t aliasBuf[MAX_LEN_CERT_ALIAS] = {0};
617     struct CmBlob certType = { sizeof(typeBuf), typeBuf };
618     struct CmBlob certUri = { sizeof(certUriBuf), certUriBuf };
619     struct CmBlob cerAlias = { sizeof(aliasBuf), aliasBuf };
620     int32_t ret = GetAppCertInfo(keyUri, &certType, &certUri, &cerAlias);
621     if (ret != CM_SUCCESS) {
622         CM_LOG_E("GetAppCertInfo failed");
623         return ret;
624     }
625 
626     if (CopyBlobToBuffer(&certType, certificateInfo, &offset) != CM_SUCCESS) {
627         CM_LOG_E("Copy certType failed");
628         return CMR_ERROR;
629     }
630 
631     if (CopyBlobToBuffer(&certUri, certificateInfo, &offset) != CM_SUCCESS) {
632         CM_LOG_E("Copy certUri failed");
633         return CMR_ERROR;
634     }
635 
636     if (CopyBlobToBuffer(&cerAlias, certificateInfo, &offset) != CM_SUCCESS) {
637         CM_LOG_E("Copy cerAlias failed");
638         return CMR_ERROR;
639     }
640 
641     ret = CopyCertificateInfoToBuffer(certBlob, certificateInfo, &offset);
642     if (ret != CM_SUCCESS) {
643         CM_LOG_E("Copy CertificateInfo failed");
644         return ret;
645     }
646 
647     return ret;
648 }
649 
CmIpcServiceGetAppCert(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)650 void CmIpcServiceGetAppCert(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
651     const struct CmContext *context)
652 {
653     int32_t ret;
654     (void)outData;
655     uint32_t store;
656     struct CmBlob keyUri = { 0, NULL };
657     struct CmBlob certificateInfo = { 0, NULL };
658     struct CmBlob certBlob = { 0, NULL };
659     struct CmContext cmContext = {0};
660     struct CmParamSet *paramSet = NULL;
661     struct CmParamOut params[] = {
662         {
663             .tag = CM_TAG_PARAM0_BUFFER,
664             .blob = &keyUri
665         },
666         {
667             .tag = CM_TAG_PARAM0_UINT32,
668             .uint32Param = &store
669         },
670     };
671     do {
672         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
673         if (ret != CM_SUCCESS) {
674             CM_LOG_E("CmIpcServiceGetAppCert get input params failed, ret = %d", ret);
675             break;
676         }
677 
678         ret = CmServiceGetAppCertCheck(&cmContext, store, &keyUri);
679         if (ret != CM_SUCCESS) {
680             CM_LOG_E("GCmServiceGetAppCertCheck fail, ret = %d", ret);
681             break;
682         }
683 
684         ret = CmServiceGetAppCert(&cmContext, store, &keyUri, &certBlob);
685         if (ret != CM_SUCCESS) {
686             CM_LOG_E("Get App cert list fail, ret = %d", ret);
687             break;
688         }
689 
690         ret = CmAppCertificateInfoPack(&certificateInfo, &certBlob, &keyUri);
691         if (ret != CM_SUCCESS) {
692             CM_LOG_E("CmAppCertificateInfoPack fail, ret = %d", ret);
693         }
694     } while (0);
695 
696     CmReport(__func__, &cmContext, &keyUri, ret);
697     CmSendResponse(context, ret, &certificateInfo);
698     CmFreeParamSet(&paramSet);
699     CM_FREE_BLOB(certBlob);
700     CM_FREE_BLOB(certificateInfo);
701     CM_LOG_D("CmIpcServiceGetAppCert end:%d", ret);
702 }
703 
GetAuthedList(const struct CmContext * context,const struct CmBlob * keyUri,struct CmBlob * outData)704 static int32_t GetAuthedList(const struct CmContext *context, const struct CmBlob *keyUri, struct CmBlob *outData)
705 {
706     if (outData->size < sizeof(uint32_t)) { /* appUidCount size */
707         CM_LOG_E("invalid outData size[%u]", outData->size);
708         return CMR_ERROR_INVALID_ARGUMENT;
709     }
710 
711     uint32_t count = (outData->size - sizeof(uint32_t)) / sizeof(uint32_t);
712     struct CmAppUidList appUidList = { count, NULL };
713     if (count != 0) {
714         appUidList.appUid = (uint32_t *)(outData->data + sizeof(uint32_t));
715     }
716 
717     int32_t ret = CmServiceGetAuthorizedAppList(context, keyUri, &appUidList);
718     if (ret != CM_SUCCESS) {
719         CM_LOG_E("service get authed list failed, ret = %d", ret);
720         return ret;
721     }
722 
723     /* refresh outData:  1.refresh appUidCount; 2.appUidCount is no bigger than count */
724     (void)memcpy_s(outData->data, sizeof(uint32_t), &appUidList.appUidCount, sizeof(uint32_t));
725     outData->size = sizeof(uint32_t) + sizeof(uint32_t) * appUidList.appUidCount;
726 
727     return CM_SUCCESS;
728 }
729 
CmIpcServiceGrantAppCertificate(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)730 void CmIpcServiceGrantAppCertificate(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
731     const struct CmContext *context)
732 {
733     struct CmContext cmContext = { 0, 0, {0} };
734     struct CmParamSet *paramSet = NULL;
735     struct CmBlob keyUri = { 0, NULL };
736     uint32_t grantUid = INIT_INVALID_VALUE;
737     int32_t ret;
738     do {
739         struct CmParamOut params[] = {
740             { .tag = CM_TAG_PARAM0_BUFFER, .blob = &keyUri },
741             { .tag = CM_TAG_PARAM1_UINT32, .uint32Param = &grantUid },
742         };
743         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
744         if (ret != CM_SUCCESS) {
745             CM_LOG_E("get input params failed, ret = %d", ret);
746             break;
747         }
748 
749         ret = CmServiceGrantAppCertificate(&cmContext, &keyUri, grantUid, outData);
750         if (ret != CM_SUCCESS) {
751             CM_LOG_E("service grant app failed, ret = %d", ret);
752             break;
753         }
754     } while (0);
755 
756     CmReport(__func__, &cmContext, &keyUri, ret);
757 
758     CM_LOG_D("CmIpcServiceGrantAppCertificate end:%d", ret);
759     CmSendResponse(context, ret, outData);
760     CmReportSGGrantAppCert(&keyUri, grantUid, false, ret);
761     CmFreeParamSet(&paramSet);
762 }
763 
CmIpcServiceGetAuthorizedAppList(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)764 void CmIpcServiceGetAuthorizedAppList(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
765     const struct CmContext *context)
766 {
767     struct CmContext cmContext = { 0, 0, {0} };
768     struct CmParamSet *paramSet = NULL;
769     struct CmBlob keyUri = { 0, NULL };
770 
771     int32_t ret;
772     do {
773         struct CmParamOut params[] = {
774             { .tag = CM_TAG_PARAM0_BUFFER, .blob = &keyUri },
775         };
776         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
777         if (ret != CM_SUCCESS) {
778             CM_LOG_E("get input params failed, ret = %d", ret);
779             break;
780         }
781 
782         ret = GetAuthedList(&cmContext, &keyUri, outData);
783         if (ret != CM_SUCCESS) {
784             CM_LOG_E("get authed app list failed, ret = %d", ret);
785             break;
786         }
787     } while (0);
788     CmReport(__func__, &cmContext, &keyUri, ret);
789 
790     CM_LOG_D("CmIpcServiceGetAuthorizedAppList end:%d", ret);
791     CmSendResponse(context, ret, outData);
792     CmFreeParamSet(&paramSet);
793 }
794 
CmIpcServiceIsAuthorizedApp(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)795 void CmIpcServiceIsAuthorizedApp(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
796     const struct CmContext *context)
797 {
798     (void)outData;
799     struct CmContext cmContext = { 0, 0, {0} };
800     struct CmParamSet *paramSet = NULL;
801     struct CmBlob authUri = { 0, NULL };
802 
803     int32_t ret;
804     do {
805         struct CmParamOut params[] = {
806             { .tag = CM_TAG_PARAM0_BUFFER, .blob = &authUri },
807         };
808         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
809         if (ret != CM_SUCCESS) {
810             CM_LOG_E("get input params failed, ret = %d", ret);
811             break;
812         }
813 
814         ret = CmServiceIsAuthorizedApp(&cmContext, &authUri);
815         if (ret != CM_SUCCESS) {
816             CM_LOG_E("service check is authed app failed, ret = %d", ret);
817             break;
818         }
819     } while (0);
820 
821     CmReport(__func__, &cmContext, &authUri, ret);
822     CM_LOG_D("CmIpcServiceIsAuthorizedApp end:%d", ret);
823     CmSendResponse(context, ret, NULL);
824     CmFreeParamSet(&paramSet);
825 }
826 
CmIpcServiceRemoveGrantedApp(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)827 void CmIpcServiceRemoveGrantedApp(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
828     const struct CmContext *context)
829 {
830     struct CmContext cmContext = { 0, 0, {0} };
831     struct CmParamSet *paramSet = NULL;
832     (void)outData;
833     struct CmBlob keyUri = { 0, NULL };
834     uint32_t appUid = INIT_INVALID_VALUE;
835     int32_t ret;
836     do {
837         struct CmParamOut params[] = {
838             { .tag = CM_TAG_PARAM0_BUFFER, .blob = &keyUri },
839             { .tag = CM_TAG_PARAM1_UINT32, .uint32Param = &appUid },
840         };
841         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
842         if (ret != CM_SUCCESS) {
843             CM_LOG_E("get input params failed, ret = %d", ret);
844             break;
845         }
846 
847         ret = CmServiceRemoveGrantedApp(&cmContext, &keyUri, appUid);
848         if (ret != CM_SUCCESS) {
849             CM_LOG_E("service remove grant app failed, ret = %d", ret);
850             break;
851         }
852     } while (0);
853     CmReport(__func__, &cmContext, &keyUri, ret);
854 
855     CM_LOG_D("CmIpcServiceRemoveGrantedApp end:%d", ret);
856     CmSendResponse(context, ret, NULL);
857     CmReportSGGrantAppCert(&keyUri, appUid, true, ret);
858     CmFreeParamSet(&paramSet);
859 }
860 
CmIpcServiceInit(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)861 void CmIpcServiceInit(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
862     const struct CmContext *context)
863 {
864     struct CmContext cmContext = { 0, 0, {0} };
865     struct CmParamSet *paramSet = NULL;
866     struct CmBlob authUri = { 0, NULL };
867 
868     int32_t ret;
869     do {
870         struct CmBlob specBlob = { 0, NULL };
871         struct CmParamOut params[] = {
872             { .tag = CM_TAG_PARAM0_BUFFER, .blob = &authUri },
873             { .tag = CM_TAG_PARAM1_BUFFER, .blob = &specBlob },
874         };
875         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
876         if (ret != CM_SUCCESS) {
877             CM_LOG_E("get input params failed, ret = %d", ret);
878             break;
879         }
880 
881         struct CmSignatureSpec spec = { 0 };
882         if (specBlob.size < sizeof(struct CmSignatureSpec)) {
883             CM_LOG_E("invalid input spec size");
884             ret = CMR_ERROR_INVALID_ARGUMENT;
885             break;
886         }
887         (void)memcpy_s(&spec, sizeof(struct CmSignatureSpec), specBlob.data, sizeof(struct CmSignatureSpec));
888 
889         ret = CmServiceInit(&cmContext, &authUri, &spec, outData);
890         if (ret != CM_SUCCESS) {
891             CM_LOG_E("service init failed, ret = %d", ret);
892             break;
893         }
894     } while (0);
895 
896     CmReport(__func__, &cmContext, &authUri, ret);
897     CM_LOG_D("CmIpcServiceInit end:%d", ret);
898     CmSendResponse(context, ret, outData);
899     CmFreeParamSet(&paramSet);
900 }
901 
CmIpcServiceUpdate(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)902 void CmIpcServiceUpdate(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
903     const struct CmContext *context)
904 {
905     (void)outData;
906     struct CmContext cmContext = { 0, 0, {0} };
907     struct CmParamSet *paramSet = NULL;
908 
909     int32_t ret;
910     do {
911         struct CmBlob handleUpdate = { 0, NULL };
912         struct CmBlob inDataUpdate = { 0, NULL };
913         struct CmParamOut params[] = {
914             { .tag = CM_TAG_PARAM0_BUFFER, .blob = &handleUpdate },
915             { .tag = CM_TAG_PARAM1_BUFFER, .blob = &inDataUpdate },
916         };
917         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
918         if (ret != CM_SUCCESS) {
919             CM_LOG_E("get input params failed, ret = %d", ret);
920             break;
921         }
922 
923         ret = CmServiceUpdate(&cmContext, &handleUpdate, &inDataUpdate);
924         if (ret != CM_SUCCESS) {
925             CM_LOG_E("service update failed, ret = %d", ret);
926             break;
927         }
928     } while (0);
929 
930     CmReport(__func__, &cmContext, NULL, ret);
931     CM_LOG_D("CmIpcServiceUpdate end:%d", ret);
932     CmSendResponse(context, ret, NULL);
933     CmFreeParamSet(&paramSet);
934 }
935 
CmIpcServiceFinish(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)936 void CmIpcServiceFinish(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
937     const struct CmContext *context)
938 {
939     struct CmContext cmContext = { 0, 0, {0} };
940     struct CmParamSet *paramSet = NULL;
941 
942     int32_t ret;
943     do {
944         struct CmBlob handleFinish = { 0, NULL };
945         struct CmBlob inDataFinish = { 0, NULL };
946         struct CmParamOut params[] = {
947             { .tag = CM_TAG_PARAM0_BUFFER, .blob = &handleFinish },
948             { .tag = CM_TAG_PARAM1_BUFFER, .blob = &inDataFinish },
949         };
950         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
951         if (ret != CM_SUCCESS) {
952             CM_LOG_E("get input params failed, ret = %d", ret);
953             break;
954         }
955 
956         ret = CmServiceFinish(&cmContext, &handleFinish, &inDataFinish, outData);
957         if (ret != CM_SUCCESS) {
958             CM_LOG_E("service finish failed, ret = %d", ret);
959             break;
960         }
961     } while (0);
962 
963     CmReport(__func__, &cmContext, NULL, ret);
964     CM_LOG_D("CmIpcServiceFinish end:%d", ret);
965     CmSendResponse(context, ret, outData);
966     CmFreeParamSet(&paramSet);
967 }
968 
CmIpcServiceAbort(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)969 void CmIpcServiceAbort(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
970     const struct CmContext *context)
971 {
972     (void)outData;
973     struct CmContext cmContext = { 0, 0, {0} };
974     struct CmParamSet *paramSet = NULL;
975 
976     int32_t ret;
977     do {
978         struct CmBlob handle = { 0, NULL };
979         struct CmParamOut params[] = {
980             { .tag = CM_TAG_PARAM0_BUFFER, .blob = &handle },
981         };
982         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
983         if (ret != CM_SUCCESS) {
984             CM_LOG_E("get input params failed, ret = %d", ret);
985             break;
986         }
987 
988         ret = CmServiceAbort(&cmContext, &handle);
989         if (ret != CM_SUCCESS) {
990             CM_LOG_E("service abort failed, ret = %d", ret);
991             break;
992         }
993     } while (0);
994 
995     CmReport(__func__, &cmContext, NULL, ret);
996     CM_LOG_D("CmIpcServiceAbort end:%d", ret);
997     CmSendResponse(context, ret, NULL);
998     CmFreeParamSet(&paramSet);
999 }
1000 
CmIpcServiceGetUserCertList(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)1001 void CmIpcServiceGetUserCertList(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
1002     const struct CmContext *context)
1003 {
1004     int32_t ret = CM_SUCCESS;
1005     uint32_t store;
1006     struct CmContext cmContext = {0};
1007     struct CmParamSet *paramSet = NULL;
1008     struct CmMutableBlob certFileList = { 0, NULL };
1009     struct CmParamOut params[] = {
1010         { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = &store },
1011     };
1012 
1013     do {
1014         if (!CmHasCommonPermission()) {
1015             CM_LOG_E("caller no permission");
1016             ret = CMR_ERROR_PERMISSION_DENIED;
1017             break;
1018         }
1019 
1020         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
1021         if (ret != CM_SUCCESS) {
1022             CM_LOG_E("GetUserCertList get input params failed, ret = %d", ret);
1023             break;
1024         }
1025 
1026         ret = CmServiceGetCertList(&cmContext, store, &certFileList);
1027         if (ret != CM_SUCCESS) {
1028             CM_LOG_E("GetCertList failed, ret = %d", ret);
1029             break;
1030         }
1031 
1032         ret = CmServiceGetCertListPack(&cmContext, store, &certFileList, outData);
1033         if (ret != CM_SUCCESS) {
1034             CM_LOG_E("CmServiceGetCertListPack pack fail, ret = %d", ret);
1035             break;
1036         }
1037 
1038         CmSendResponse(context, ret, outData);
1039     } while (0);
1040 
1041     struct CmBlob tempBlob = { 0, NULL };
1042     CmReport(__func__, &cmContext, &tempBlob, ret);
1043 
1044     if (ret != CM_SUCCESS) {
1045         CmSendResponse(context, ret, NULL);
1046     }
1047 
1048     if (certFileList.data != NULL) {
1049         CmFreeCertFiles((struct CertFileInfo *)certFileList.data, certFileList.size);
1050     }
1051     CmFreeParamSet(&paramSet);
1052 }
1053 
CmIpcServiceGetUserCertInfo(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)1054 void CmIpcServiceGetUserCertInfo(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
1055     const struct CmContext *context)
1056 {
1057     int32_t ret = CM_SUCCESS;
1058     uint32_t store;
1059     uint32_t status = 0;
1060     struct CmBlob certUri = { 0, NULL };
1061     struct CmBlob certificateData = { 0, NULL };
1062     struct CmContext cmContext = {0};
1063     struct CmParamSet *paramSet = NULL;
1064     struct CmParamOut params[] = {
1065         { .tag = CM_TAG_PARAM0_BUFFER, .blob = &certUri},
1066         { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = &store},
1067     };
1068 
1069     do {
1070         if (!CmHasCommonPermission()) {
1071             CM_LOG_E("caller no permission");
1072             ret = CMR_ERROR_PERMISSION_DENIED;
1073             break;
1074         }
1075 
1076         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
1077         if (ret != CM_SUCCESS) {
1078             CM_LOG_E("GetUserCertInfo get input params failed, ret = %d", ret);
1079             break;
1080         }
1081 
1082         ret = CmServiceGetCertInfo(&cmContext, &certUri, store, &certificateData, &status);
1083         if (ret != CM_SUCCESS) {
1084             CM_LOG_E("GetCertInfo failed, ret = %d", ret);
1085             break;
1086         }
1087 
1088         ret = CmServiceGetCertInfoPack(store, &certificateData, status, &certUri, outData);
1089         if (ret != CM_SUCCESS) {
1090             CM_LOG_E("CmServiceGetCertInfoPack pack failed, ret = %d", ret);
1091             break;
1092         }
1093         CmSendResponse(context, ret, outData);
1094     } while (0);
1095     CmReport(__func__, &cmContext, &certUri, ret);
1096     if (ret != CM_SUCCESS) {
1097         CmSendResponse(context, ret, NULL);
1098     }
1099     CM_FREE_BLOB(certificateData);
1100     CmFreeParamSet(&paramSet);
1101 }
1102 
CmIpcServiceSetUserCertStatus(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)1103 void CmIpcServiceSetUserCertStatus(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
1104     const struct CmContext *context)
1105 {
1106     int32_t ret = CM_SUCCESS;
1107     uint32_t store = CM_USER_TRUSTED_STORE;
1108     uint32_t status = INIT_INVALID_VALUE;
1109     struct CmBlob certUri = { 0, NULL };
1110     struct CmContext cmContext = {0};
1111     struct CmParamSet *paramSet = NULL;
1112     struct CmParamOut params[] = {
1113         { .tag = CM_TAG_PARAM0_BUFFER, .blob = &certUri },
1114         { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = &store },
1115         { .tag = CM_TAG_PARAM1_UINT32, .uint32Param = &status },
1116     };
1117 
1118     do {
1119         if (!CmHasCommonPermission() || !CmHasUserTrustedPermission()) {
1120             CM_LOG_E("caller no permission");
1121             ret = CMR_ERROR_PERMISSION_DENIED;
1122             break;
1123         }
1124         if (!CmIsSystemApp()) {
1125             CM_LOG_E("set user status: caller is not system app");
1126             ret = CMR_ERROR_NOT_SYSTEMP_APP;
1127             break;
1128         }
1129 
1130         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
1131         if (ret != CM_SUCCESS) {
1132             CM_LOG_E("SetUserCertStatus get input params failed, ret = %d", ret);
1133             break;
1134         }
1135 
1136         ret = CmServiceSetCertStatus(&cmContext, &certUri, store, status);
1137         if (ret != CM_SUCCESS) {
1138             CM_LOG_E("set user cert status failed, ret = %d", ret);
1139             break;
1140         }
1141         ret = CmSetStatusBackupCert(&cmContext, &certUri, store, status);
1142         if (ret != CM_SUCCESS) {
1143             CM_LOG_E("CmSetStatusBackupCert failed, ret = %d", ret);
1144             break;
1145         }
1146     } while (0);
1147 
1148     CmReport(__func__, &cmContext, &certUri, ret);
1149     CmSendResponse(context, ret, NULL);
1150     CmReportSGSetCertStatus(&certUri, store, status, ret);
1151     CmFreeParamSet(&paramSet);
1152 }
1153 
CmIpcServiceInstallUserCert(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)1154 void CmIpcServiceInstallUserCert(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
1155     const struct CmContext *context)
1156 {
1157     int32_t ret = CM_SUCCESS;
1158     struct CmBlob userCert = { 0, NULL };
1159     struct CmBlob certAlias = { 0, NULL };
1160     uint32_t userId = 0;
1161     uint32_t status = CERT_STATUS_ENANLED;
1162     struct CmContext cmContext = {0};
1163     struct CmParamSet *paramSet = NULL;
1164     struct CmParamOut params[] = {
1165         { .tag = CM_TAG_PARAM0_BUFFER, .blob = &userCert },
1166         { .tag = CM_TAG_PARAM1_BUFFER, .blob = &certAlias },
1167         { .tag = CM_TAG_PARAM0_UINT32, .uint32Param = &userId },
1168         { .tag = CM_TAG_PARAM1_UINT32, .uint32Param = &status },
1169     };
1170 
1171     do {
1172         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
1173         if (ret != CM_SUCCESS) {
1174             CM_LOG_E("InstallUserCert get input params failed, ret = %d", ret);
1175             break;
1176         }
1177 
1178         ret = CmServiceInstallUserCertCheck(&cmContext, &userCert, &certAlias, userId);
1179         if (ret != CM_SUCCESS) {
1180             CM_LOG_E("CmServiceInstallUserCertCheck fail, ret = %d", ret);
1181             break;
1182         }
1183 
1184         ret = CmInstallUserCert(&cmContext, &userCert, &certAlias, status, outData);
1185         if (ret != CM_SUCCESS) {
1186             CM_LOG_E("CertManagerInstallUserCert fail, ret = %d", ret);
1187             break;
1188         }
1189 
1190         CmSendResponse(context, ret, outData);
1191     } while (0);
1192 
1193     struct CmBlob tempBlob = { 0, NULL };
1194     CmReport(__func__, &cmContext, &tempBlob, ret);
1195 
1196     if (ret != CM_SUCCESS) {
1197         CmSendResponse(context, ret, NULL);
1198     }
1199     CmReportSGInstallUserCert(&certAlias, ret);
1200     CmFreeParamSet(&paramSet);
1201 }
1202 
CmIpcServiceUninstallUserCert(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)1203 void CmIpcServiceUninstallUserCert(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
1204     const struct CmContext *context)
1205 {
1206     (void)outData;
1207     int32_t ret = CM_SUCCESS;
1208     struct CmBlob certUri = { 0, NULL };
1209     struct CmContext cmContext = {0};
1210     struct CmParamSet *paramSet = NULL;
1211     struct CmParamOut params[] = {
1212         { .tag = CM_TAG_PARAM0_BUFFER, .blob = &certUri },
1213     };
1214 
1215     do {
1216         ret = GetInputParams(paramSetBlob, &paramSet, &cmContext, params, CM_ARRAY_SIZE(params));
1217         if (ret != CM_SUCCESS) {
1218             CM_LOG_E("UninstallUserCert get input params failed, ret = %d", ret);
1219             break;
1220         }
1221 
1222         ret = CmServiceUninstallUserCertCheck(&cmContext, &certUri);
1223         if (ret != CM_SUCCESS) {
1224             CM_LOG_E("CmServiceUninstallUserCertCheck fail, ret = %d", ret);
1225             break;
1226         }
1227 
1228         ret = CmUninstallUserCert(&cmContext, &certUri);
1229         if (ret != CM_SUCCESS) {
1230             CM_LOG_E("CertManagerUninstallUserCert fail, ret = %d", ret);
1231             break;
1232         }
1233     } while (0);
1234 
1235     CmReport(__func__, &cmContext, &certUri, ret);
1236     CmSendResponse(context, ret, NULL);
1237     CmReportSGUninstallUserCert(&certUri, false, ret);
1238     CmFreeParamSet(&paramSet);
1239 }
1240 
CmIpcServiceUninstallAllUserCert(const struct CmBlob * paramSetBlob,struct CmBlob * outData,const struct CmContext * context)1241 void CmIpcServiceUninstallAllUserCert(const struct CmBlob *paramSetBlob, struct CmBlob *outData,
1242     const struct CmContext *context)
1243 {
1244     (void)outData;
1245     int32_t ret = CM_SUCCESS;
1246     struct CmContext cmContext = {0};
1247 
1248     do {
1249         if (!CmHasCommonPermission() || !CmHasUserTrustedPermission()) {
1250             CM_LOG_E("caller no permission");
1251             ret = CMR_ERROR_PERMISSION_DENIED;
1252             break;
1253         }
1254         if (!CmIsSystemApp()) {
1255             CM_LOG_E("uninstall all user cert: caller is not system app");
1256             ret = CMR_ERROR_NOT_SYSTEMP_APP;
1257             break;
1258         }
1259 
1260         ret = CmGetProcessInfoForIPC(&cmContext);
1261         if (ret != CM_SUCCESS) {
1262             CM_LOG_E("CmGetProcessInfoForIPC fail, ret = %d", ret);
1263             break;
1264         }
1265 
1266         ret = CmUninstallAllUserCert(&cmContext);
1267         if (ret != CM_SUCCESS) {
1268             CM_LOG_E("CertManagerUninstallAllUserCert fail, ret = %d", ret);
1269             break;
1270         }
1271     } while (0);
1272     CmReport(__func__, &cmContext, NULL, ret);
1273     CmSendResponse(context, ret, NULL);
1274     CmReportSGUninstallUserCert(NULL, true, ret);
1275 }
1276 
1277