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