• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 <string.h>
17 
18 #include "cj_cert_manager_ffi.h"
19 
FfiCertManagerInstallAppCert(const struct CmBlob * appCert,const struct CmBlob * appCertPwd,const struct CmBlob * certAlias,const uint32_t store,struct CmBlob * keyUri)20 int32_t FfiCertManagerInstallAppCert(const struct CmBlob *appCert, const struct CmBlob *appCertPwd,
21                                      const struct CmBlob *certAlias, const uint32_t store, struct CmBlob *keyUri)
22 {
23     return CmInstallAppCert(appCert, appCertPwd, certAlias, store, keyUri);
24 }
25 
FfiCertManagerUninstallAppCert(const struct CmBlob * keyUri,const uint32_t store)26 int32_t FfiCertManagerUninstallAppCert(const struct CmBlob *keyUri, const uint32_t store)
27 {
28     return CmUninstallAppCert(keyUri, store);
29 }
30 
FfiCertManagerGetAppCert(const struct CmBlob * keyUri,const uint32_t store,struct CjCredential * retObj)31 int32_t FfiCertManagerGetAppCert(const struct CmBlob *keyUri, const uint32_t store, struct CjCredential *retObj)
32 {
33     struct Credential credential = {0};
34     credential.credData.data = malloc(MAX_LEN_CERTIFICATE_CHAIN);
35     if (credential.credData.data == NULL) {
36         return CMR_ERROR_MALLOC_FAIL;
37     }
38     credential.credData.size = MAX_LEN_CERTIFICATE_CHAIN;
39     const int32_t errCode = CmGetAppCert(keyUri, store, &credential);
40     if (errCode != CM_SUCCESS) {
41         free(credential.credData.data);
42         return errCode;
43     }
44     /* ATTENTION:
45      * 1. Resource will be released by caller.
46      * 2. strdup may return nullptr, but caller will handle nullptr
47      * 3. Caller will ensure `retObj` is always not null.
48      */
49     retObj->isExist = credential.isExist;
50     retObj->type = strdup(credential.type);
51     retObj->alias = strdup(credential.alias);
52     retObj->keyUri = strdup(credential.keyUri);
53     retObj->certNum = credential.certNum;
54     retObj->keyNum = credential.keyNum;
55     retObj->credData.data = credential.credData.data;
56     retObj->credData.size = credential.credData.size;
57     return CM_SUCCESS;
58 }
59 
FfiCertManagerInit(const struct CmBlob * authUri,const struct CjSignatureSpec * spec,struct CmBlob * handle)60 int32_t FfiCertManagerInit(const struct CmBlob *authUri, const struct CjSignatureSpec *spec, struct CmBlob *handle)
61 {
62     // Caller will ensure `spec` is always not null.
63     const struct CmSignatureSpec cmSpec = {
64         .purpose = spec->purpose,
65         .padding = spec->padding,
66         .digest = spec->digest,
67     };
68     return CmInit(authUri, &cmSpec, handle);
69 }
70 
FfiCertManagerUpdate(const struct CmBlob * handle,const struct CmBlob * inData)71 int32_t FfiCertManagerUpdate(const struct CmBlob *handle, const struct CmBlob *inData)
72 {
73     return CmUpdate(handle, inData);
74 }
75 
FfiCertManagerFinish(const struct CmBlob * handle,const struct CmBlob * inData,struct CmBlob * outData)76 int32_t FfiCertManagerFinish(const struct CmBlob *handle, const struct CmBlob *inData, struct CmBlob *outData)
77 {
78     return CmFinish(handle, inData, outData);
79 }
80 
FfiCertManagerAbort(const struct CmBlob * handle)81 int32_t FfiCertManagerAbort(const struct CmBlob *handle)
82 {
83     return CmAbort(handle);
84 }
85 
FfiCertManagerIsAuthorizedApp(const struct CmBlob * authUri)86 int32_t FfiCertManagerIsAuthorizedApp(const struct CmBlob *authUri)
87 {
88     return CmIsAuthorizedApp(authUri);
89 }
90 
FfiCertManagerGetUserCertList(const uint32_t store,uint32_t * retCount,struct CjCertAbstract ** retObj)91 int32_t FfiCertManagerGetUserCertList(const uint32_t store, uint32_t *retCount, struct CjCertAbstract **retObj)
92 {
93     struct CertList certificateList = {0};
94     uint32_t buffSize = MAX_COUNT_CERTIFICATE * sizeof(struct CertAbstract);
95     certificateList.certAbstract = (struct CertAbstract *) malloc(buffSize);
96     if (certificateList.certAbstract == NULL) {
97         return CMR_ERROR_MALLOC_FAIL;
98     }
99     certificateList.certsCount = MAX_COUNT_CERTIFICATE;
100 
101     const int32_t errCode = CmGetUserCertList(store, &certificateList);
102     if (errCode == CM_SUCCESS) {
103         // Caller will ensure `retObj` is always not null.
104         *retObj = malloc(sizeof(struct CjCertAbstract) * certificateList.certsCount);
105         if (*retObj == NULL) {
106             free(certificateList.certAbstract);
107             return CMR_ERROR_MALLOC_FAIL;
108         }
109         *retCount = certificateList.certsCount;
110         for (uint32_t i = 0; i < certificateList.certsCount; ++i) {
111             /* ATTENTION:
112              * 1. Resource will be released by caller.
113              * 2. strdup may return nullptr, but caller will handle nullptr
114              */
115             (*retObj)->uri = strdup(certificateList.certAbstract[i].uri);
116             (*retObj)->certAlias = strdup(certificateList.certAbstract[i].certAlias);
117             (*retObj)[i].status = certificateList.certAbstract[i].status;
118             (*retObj)->subjectName = strdup(certificateList.certAbstract[i].subjectName);
119         }
120     }
121     free(certificateList.certAbstract);
122     return errCode;
123 }
124 
FfiCertManagerGetUserCertInfo(const struct CmBlob * certUri,const uint32_t store,struct CjCertInfo * retObj)125 int32_t FfiCertManagerGetUserCertInfo(const struct CmBlob *certUri, const uint32_t store, struct CjCertInfo *retObj)
126 {
127     struct CertInfo info = {0};
128     info.certInfo.data = malloc(MAX_LEN_CERTIFICATE);
129     if (info.certInfo.data == NULL) {
130         return CMR_ERROR_MALLOC_FAIL;
131     }
132     info.certInfo.size = MAX_LEN_CERTIFICATE;
133 
134     const int32_t errCode = CmGetUserCertInfo(certUri, store, &info);
135     if (errCode != CM_SUCCESS) {
136         free(info.certInfo.data);
137         return errCode;
138     }
139     /* ATTENTION:
140      * 1. Resource will be released by caller.
141      * 2. strdup may return nullptr, but caller will handle nullptr
142      * 3. Caller will ensure `retObj` is always not null.
143      */
144     retObj->uri = strdup(info.uri);
145     retObj->certAlias = strdup(info.certAlias);
146     retObj->status = info.status;
147     retObj->issuerName = strdup(info.issuerName);
148     retObj->subjectName = strdup(info.subjectName);
149     retObj->serial = strdup(info.serial);
150     retObj->notBefore = strdup(info.notBefore);
151     retObj->notAfter = strdup(info.notAfter);
152     retObj->fingerprintSha256 = strdup(info.fingerprintSha256);
153     retObj->certInfo.data = info.certInfo.data;
154     retObj->certInfo.size = info.certInfo.size;
155     return CM_SUCCESS;
156 }
157