1 /*
2  * Copyright (c) 2023 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 #include "udid.h"
16 #ifndef OHOS_LITE
17 #include "init_module_engine.h"
18 #endif
19 #include "init_param.h"
20 #include "param_comm.h"
21 #include "securec.h"
22 #include "sysparam_errno.h"
23 
24 #include "mbedtls/sha256.h"
25 
26 #define BUF_MAX_LEN 132
27 #define PARAM_VALUE_MAX_LEN 36
28 
GetSha256Value(const char * input,char * udid,uint32_t udidSize)29 INIT_UDID_LOCAL_API int GetSha256Value(const char *input, char *udid, uint32_t udidSize)
30 {
31     if (input == NULL) {
32         return EC_FAILURE;
33     }
34     char buf[DEV_BUF_LENGTH] = { 0 };
35     unsigned char hash[HASH_LENGTH] = { 0 };
36 
37     mbedtls_sha256_context context;
38     mbedtls_sha256_init(&context);
39     mbedtls_sha256_starts(&context, 0);
40     mbedtls_sha256_update(&context, (const unsigned char *)input, strlen(input));
41     mbedtls_sha256_finish(&context, hash);
42 
43     for (size_t i = 0; i < HASH_LENGTH; i++) {
44         unsigned char value = hash[i];
45         memset_s(buf, DEV_BUF_LENGTH, 0, DEV_BUF_LENGTH);
46         int len = sprintf_s(buf, sizeof(buf), "%02X", value);
47         if (len > 0 && strcat_s(udid, udidSize, buf) != 0) {
48             return EC_FAILURE;
49         }
50     }
51     return EC_SUCCESS;
52 }
53 
CalcDevUdid(char * udid,uint32_t size)54 INIT_UDID_LOCAL_API int CalcDevUdid(char *udid, uint32_t size)
55 {
56     char tmp[BUF_MAX_LEN] = {0};
57     uint32_t manufactureLen = PARAM_VALUE_MAX_LEN;
58     int ret = SystemReadParam("const.product.manufacturer", tmp, &manufactureLen);
59     BEGET_ERROR_CHECK(ret == 0, return -1, "Read param const.product.manufacturer failed!");
60 
61     uint32_t modelLen = PARAM_VALUE_MAX_LEN;
62     ret = SystemReadParam("const.product.model", tmp + manufactureLen, &modelLen);
63     BEGET_ERROR_CHECK(ret == 0, return -1, "Read param const.product.model failed!");
64     const char *serial = GetSerial_();
65     BEGET_ERROR_CHECK(serial != NULL, return -1, "Read param serial failed!");
66     ret = strcat_s(tmp, BUF_MAX_LEN, serial);
67     BEGET_ERROR_CHECK(ret != -1, return -1, "Cat serial failed!");
68     ret = GetSha256Value(tmp, udid, size);
69     return ret;
70 }
71 
72 #ifndef OHOS_LITE
SetDevUdid()73 INIT_UDID_LOCAL_API void SetDevUdid()
74 {
75     BEGET_LOGI("Begin set udid param");
76     char udid[UDID_LEN] = {0};
77     uint32_t size = (uint32_t)sizeof(udid);
78     int ret = GetUdidFromParam(udid, size);
79     if (ret != 0) {
80         BEGET_LOGI("Get udid from param failed, calculate udid from other param");
81         ret = CalcDevUdid(udid, size);
82         BEGET_ERROR_CHECK(ret == 0, return, "calculate udid is failed!")
83     }
84     ret = SystemWriteParam("const.product.devUdid", udid);
85     BEGET_ERROR_CHECK(ret == 0, return, "write param const.product.devUdid failed!");
86 }
87 
MODULE_CONSTRUCTOR(void)88 MODULE_CONSTRUCTOR(void)
89 {
90     SetDevUdid();
91 }
92 #endif
93