1 /*
2 * Copyright (c) 2021 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 "dslm_credential.h"
17
18 #include <dlfcn.h>
19 #include <stdbool.h>
20 #include <string.h>
21
22 #include "securec.h"
23
24 #include "utils_log.h"
25 #include "utils_mem.h"
26
GetFunctionCb()27 static inline ProcessDslmCredFunctions *GetFunctionCb()
28 {
29 static ProcessDslmCredFunctions cb = {NULL, NULL, NULL, 0, {0}};
30 return &cb;
31 }
32
InitDslmCredentialFunctions(const ProcessDslmCredFunctions * funcs)33 bool InitDslmCredentialFunctions(const ProcessDslmCredFunctions *funcs)
34 {
35 if (funcs == NULL) {
36 return false;
37 }
38 ProcessDslmCredFunctions *cb = GetFunctionCb();
39 memcpy_s(cb, sizeof(ProcessDslmCredFunctions), funcs, sizeof(ProcessDslmCredFunctions));
40 SECURITY_LOG_INFO("InitDslmCredentialFunctions success");
41 return true;
42 }
43
DefaultRequestDslmCred(const DeviceIdentify * device,const RequestObject * obj,DslmCredBuff ** credBuff)44 int32_t DefaultRequestDslmCred(const DeviceIdentify *device, const RequestObject *obj, DslmCredBuff **credBuff)
45 {
46 ProcessDslmCredFunctions *cb = GetFunctionCb();
47 RequestDslmCredFunc *request = cb->requestFunc;
48 if (request != NULL) {
49 return request(device, obj, credBuff);
50 }
51 SECURITY_LOG_INFO("invoke DefaultRequestDslmCred");
52 return -1;
53 }
54
DefaultVerifyDslmCred(const DeviceIdentify * device,uint64_t challenge,const DslmCredBuff * credBuff,DslmCredInfo * credInfo)55 int32_t DefaultVerifyDslmCred(const DeviceIdentify *device, uint64_t challenge, const DslmCredBuff *credBuff,
56 DslmCredInfo *credInfo)
57 {
58 ProcessDslmCredFunctions *cb = GetFunctionCb();
59 VerifyDslmCredFunc *verify = cb->verifyFunc;
60 if (verify != NULL) {
61 return verify(device, challenge, credBuff, credInfo);
62 }
63 SECURITY_LOG_INFO("invoke DefaultVerifyDslmCred");
64 return -1;
65 }
66
DefaultInitDslmCred(DslmCredInfo * credInfo)67 int32_t DefaultInitDslmCred(DslmCredInfo *credInfo)
68 {
69 ProcessDslmCredFunctions *cb = GetFunctionCb();
70 InitDslmCredFunc *init = cb->initFunc;
71 if (init != NULL) {
72 return init(credInfo);
73 }
74 SECURITY_LOG_INFO("invoke DefaultInitDslmCred");
75 return -1;
76 }
77
GetSupportedCredTypes(CredType * list,uint32_t len)78 int32_t GetSupportedCredTypes(CredType *list, uint32_t len)
79 {
80 if ((list == NULL) || len == 0) {
81 return 0;
82 }
83 ProcessDslmCredFunctions *cb = GetFunctionCb();
84 uint32_t outLen = len;
85 if (len > cb->credTypeCnt) {
86 outLen = cb->credTypeCnt;
87 }
88 for (uint32_t i = 0; i < outLen; i++) {
89 *(list + i) = cb->credTypeArray[i];
90 }
91 return outLen;
92 }
93
CreateDslmCred(CredType type,uint32_t len,uint8_t * value)94 DslmCredBuff *CreateDslmCred(CredType type, uint32_t len, uint8_t *value)
95 {
96 if ((len == 0) || (value == NULL)) {
97 return NULL;
98 }
99 DslmCredBuff *outBuff = (DslmCredBuff *)MALLOC(sizeof(DslmCredBuff));
100 if (outBuff == NULL) {
101 return NULL;
102 }
103 uint8_t *outValue = (uint8_t *)MALLOC(len);
104 if (outValue == NULL) {
105 FREE(outBuff);
106 return NULL;
107 }
108 memset_s(outValue, len, 0, len);
109 if (memcpy_s(outValue, len, value, len) != EOK) {
110 FREE(outBuff);
111 FREE(outValue);
112 return NULL;
113 }
114 outBuff->credVal = outValue;
115 outBuff->type = type;
116 outBuff->credLen = len;
117 return outBuff;
118 }
119
DestroyDslmCred(DslmCredBuff * credBuff)120 void DestroyDslmCred(DslmCredBuff *credBuff)
121 {
122 if (credBuff == NULL) {
123 return;
124 }
125 if (credBuff->credVal != NULL) {
126 FREE(credBuff->credVal);
127 credBuff->credVal = NULL;
128 }
129 FREE(credBuff);
130 }