1 /*
2  * Copyright (c) 2020-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 "token.h"
17 #include <pthread.h>
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include "hal_token.h"
22 #include "log.h"
23 #include "ohos_errno.h"
24 
25 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
26 
UidVerify(void)27 static int UidVerify(void)
28 {
29     uid_t uid;
30 
31     uid = getuid();
32     if (uid >= KIT_FRAMEWORK_UID_MAX) {
33         HILOG_ERROR(HILOG_MODULE_HIVIEW, "uid verify failed, get uid:%d", uid);
34         return EC_FAILURE;
35     }
36 
37     return EC_SUCCESS;
38 }
39 
ReadToken(char * token,unsigned int len)40 int ReadToken(char *token, unsigned int len)
41 {
42     int ret;
43 
44     if (token == NULL) {
45         HILOG_ERROR(HILOG_MODULE_HIVIEW, "token is nullptr");
46         return EC_FAILURE;
47     }
48     ret = UidVerify();
49     if (ret != EC_SUCCESS) {
50         return EC_FAILURE;
51     }
52 
53     pthread_mutex_lock(&g_mutex);
54     ret = HalReadToken(token, len);
55     pthread_mutex_unlock(&g_mutex);
56 
57     return ret;
58 }
59 
WriteToken(const char * token,unsigned int len)60 int WriteToken(const char *token, unsigned int len)
61 {
62     int ret;
63 
64     if (token == NULL) {
65         HILOG_ERROR(HILOG_MODULE_HIVIEW, "token is nullptr");
66         return EC_FAILURE;
67     }
68     ret = UidVerify();
69     if (ret != EC_SUCCESS) {
70         return EC_FAILURE;
71     }
72 
73     pthread_mutex_lock(&g_mutex);
74     ret = HalWriteToken(token, len);
75     pthread_mutex_unlock(&g_mutex);
76 
77     return ret;
78 }
79 
GetAcKey(char * acKey,unsigned int len)80 int GetAcKey(char *acKey, unsigned int len)
81 {
82     if (acKey == NULL) {
83         HILOG_ERROR(HILOG_MODULE_HIVIEW, "acKey is nullptr");
84         return EC_FAILURE;
85     }
86     int ret = UidVerify();
87     if (ret != EC_SUCCESS) {
88         return EC_FAILURE;
89     }
90 
91     return HalGetAcKey(acKey, len);
92 }
93 
GetProdId(char * productId,unsigned int len)94 int GetProdId(char *productId, unsigned int len)
95 {
96     if (productId == NULL) {
97         HILOG_ERROR(HILOG_MODULE_HIVIEW, "productId is nullptr");
98         return EC_FAILURE;
99     }
100 
101     return HalGetProdId(productId, len);
102 }
103 
GetProdKey(char * productKey,unsigned int len)104 int GetProdKey(char *productKey, unsigned int len)
105 {
106     if (productKey == NULL) {
107         HILOG_ERROR(HILOG_MODULE_HIVIEW, "productKey is nullptr");
108         return EC_FAILURE;
109     }
110 
111     int ret = UidVerify();
112     if (ret != EC_SUCCESS) {
113         return EC_FAILURE;
114     }
115 
116     return HalGetProdKey(productKey, len);
117 }