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 "hks_api.h"
17 
18 #include "hks_api_mock_helper.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace CodeSign {
23 int g_count = 0;
HksKeyExist(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet)24 int32_t HksKeyExist(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet)
25 {
26     LOG_INFO("Mock HksKeyExist");
27     if (g_count == KEYEXIST) {
28         return -1;
29     }
30     if (g_count == ERROR) {
31         return HKS_ERROR_NOT_EXIST;
32     }
33     return HKS_SUCCESS;
34 }
35 
HksAttestKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet, struct HksCertChain *certChain)36 int32_t HksAttestKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
37     struct HksCertChain *certChain)
38 {
39     LOG_INFO("Mock HksAttestKey");
40     if (g_count == ATTESTKEY) {
41         return -1;
42     }
43 
44     bool ret = GetCertInDer(certChain->certs[0].data, certChain->certs[0].size);
45     if (!ret) {
46         LOG_ERROR("Failed to convert PEM to DER.\n");
47         return HKS_FAILURE;
48     }
49     return HKS_SUCCESS;
50 }
51 
HksGenerateKey(const struct HksBlob *keyAlias, const struct HksParamSet *paramSetIn, struct HksParamSet *paramSetOut)52 int32_t HksGenerateKey(const struct HksBlob *keyAlias,
53     const struct HksParamSet *paramSetIn, struct HksParamSet *paramSetOut)
54 {
55     LOG_INFO("Mock HksGenerateKey");
56     if (g_count == GENERATEKEY || g_count == ERROR) {
57         return -1;
58     }
59     return HKS_SUCCESS;
60 }
61 
HksInit(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet, struct HksBlob *handle, struct HksBlob *token)62 int32_t HksInit(const struct HksBlob *keyAlias, const struct HksParamSet *paramSet,
63     struct HksBlob *handle, struct HksBlob *token)
64 {
65     LOG_INFO("Mock HksInit");
66     if (g_count == INIT) {
67         return -1;
68     }
69     return HKS_SUCCESS;
70 }
71 
72 
HksUpdate(const struct HksBlob *handle, const struct HksParamSet *paramSet, const struct HksBlob *inData, struct HksBlob *outData)73 int32_t HksUpdate(const struct HksBlob *handle, const struct HksParamSet *paramSet,
74     const struct HksBlob *inData, struct HksBlob *outData)
75 {
76     LOG_INFO("Mock HksUpdate");
77     if (g_count == UPDATE) {
78         return -1;
79     }
80     return HKS_SUCCESS;
81 }
82 
HksFinish(const struct HksBlob *handle, const struct HksParamSet *paramSet, const struct HksBlob *inData, struct HksBlob *outData)83 int32_t HksFinish(const struct HksBlob *handle, const struct HksParamSet *paramSet,
84     const struct HksBlob *inData, struct HksBlob *outData)
85 {
86     LOG_INFO("Mock HksFinish");
87     if (g_count == FINISH) {
88         return -1;
89     }
90     return HKS_SUCCESS;
91 }
92 }
93 }
94 }