1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License. 5d9f0492fSopenharmony_ci * You may obtain a copy of the License at 6d9f0492fSopenharmony_ci * 7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8d9f0492fSopenharmony_ci * 9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and 13d9f0492fSopenharmony_ci * limitations under the License. 14d9f0492fSopenharmony_ci */ 15d9f0492fSopenharmony_ci#include "udid.h" 16d9f0492fSopenharmony_ci#ifndef OHOS_LITE 17d9f0492fSopenharmony_ci#include "init_module_engine.h" 18d9f0492fSopenharmony_ci#endif 19d9f0492fSopenharmony_ci#include "init_param.h" 20d9f0492fSopenharmony_ci#include "param_comm.h" 21d9f0492fSopenharmony_ci#include "securec.h" 22d9f0492fSopenharmony_ci#include "sysparam_errno.h" 23d9f0492fSopenharmony_ci 24d9f0492fSopenharmony_ci#include "mbedtls/sha256.h" 25d9f0492fSopenharmony_ci 26d9f0492fSopenharmony_ci#define BUF_MAX_LEN 132 27d9f0492fSopenharmony_ci#define PARAM_VALUE_MAX_LEN 36 28d9f0492fSopenharmony_ci 29d9f0492fSopenharmony_ciINIT_UDID_LOCAL_API int GetSha256Value(const char *input, char *udid, uint32_t udidSize) 30d9f0492fSopenharmony_ci{ 31d9f0492fSopenharmony_ci if (input == NULL) { 32d9f0492fSopenharmony_ci return EC_FAILURE; 33d9f0492fSopenharmony_ci } 34d9f0492fSopenharmony_ci char buf[DEV_BUF_LENGTH] = { 0 }; 35d9f0492fSopenharmony_ci unsigned char hash[HASH_LENGTH] = { 0 }; 36d9f0492fSopenharmony_ci 37d9f0492fSopenharmony_ci mbedtls_sha256_context context; 38d9f0492fSopenharmony_ci mbedtls_sha256_init(&context); 39d9f0492fSopenharmony_ci mbedtls_sha256_starts(&context, 0); 40d9f0492fSopenharmony_ci mbedtls_sha256_update(&context, (const unsigned char *)input, strlen(input)); 41d9f0492fSopenharmony_ci mbedtls_sha256_finish(&context, hash); 42d9f0492fSopenharmony_ci 43d9f0492fSopenharmony_ci for (size_t i = 0; i < HASH_LENGTH; i++) { 44d9f0492fSopenharmony_ci unsigned char value = hash[i]; 45d9f0492fSopenharmony_ci memset_s(buf, DEV_BUF_LENGTH, 0, DEV_BUF_LENGTH); 46d9f0492fSopenharmony_ci int len = sprintf_s(buf, sizeof(buf), "%02X", value); 47d9f0492fSopenharmony_ci if (len > 0 && strcat_s(udid, udidSize, buf) != 0) { 48d9f0492fSopenharmony_ci return EC_FAILURE; 49d9f0492fSopenharmony_ci } 50d9f0492fSopenharmony_ci } 51d9f0492fSopenharmony_ci return EC_SUCCESS; 52d9f0492fSopenharmony_ci} 53d9f0492fSopenharmony_ci 54d9f0492fSopenharmony_ciINIT_UDID_LOCAL_API int CalcDevUdid(char *udid, uint32_t size) 55d9f0492fSopenharmony_ci{ 56d9f0492fSopenharmony_ci char tmp[BUF_MAX_LEN] = {0}; 57d9f0492fSopenharmony_ci uint32_t manufactureLen = PARAM_VALUE_MAX_LEN; 58d9f0492fSopenharmony_ci int ret = SystemReadParam("const.product.manufacturer", tmp, &manufactureLen); 59d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(ret == 0, return -1, "Read param const.product.manufacturer failed!"); 60d9f0492fSopenharmony_ci 61d9f0492fSopenharmony_ci uint32_t modelLen = PARAM_VALUE_MAX_LEN; 62d9f0492fSopenharmony_ci ret = SystemReadParam("const.product.model", tmp + manufactureLen, &modelLen); 63d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(ret == 0, return -1, "Read param const.product.model failed!"); 64d9f0492fSopenharmony_ci const char *serial = GetSerial_(); 65d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(serial != NULL, return -1, "Read param serial failed!"); 66d9f0492fSopenharmony_ci ret = strcat_s(tmp, BUF_MAX_LEN, serial); 67d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(ret != -1, return -1, "Cat serial failed!"); 68d9f0492fSopenharmony_ci ret = GetSha256Value(tmp, udid, size); 69d9f0492fSopenharmony_ci return ret; 70d9f0492fSopenharmony_ci} 71d9f0492fSopenharmony_ci 72d9f0492fSopenharmony_ci#ifndef OHOS_LITE 73d9f0492fSopenharmony_ciINIT_UDID_LOCAL_API void SetDevUdid() 74d9f0492fSopenharmony_ci{ 75d9f0492fSopenharmony_ci BEGET_LOGI("Begin set udid param"); 76d9f0492fSopenharmony_ci char udid[UDID_LEN] = {0}; 77d9f0492fSopenharmony_ci uint32_t size = (uint32_t)sizeof(udid); 78d9f0492fSopenharmony_ci int ret = GetUdidFromParam(udid, size); 79d9f0492fSopenharmony_ci if (ret != 0) { 80d9f0492fSopenharmony_ci BEGET_LOGI("Get udid from param failed, calculate udid from other param"); 81d9f0492fSopenharmony_ci ret = CalcDevUdid(udid, size); 82d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(ret == 0, return, "calculate udid is failed!") 83d9f0492fSopenharmony_ci } 84d9f0492fSopenharmony_ci ret = SystemWriteParam("const.product.devUdid", udid); 85d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(ret == 0, return, "write param const.product.devUdid failed!"); 86d9f0492fSopenharmony_ci} 87d9f0492fSopenharmony_ci 88d9f0492fSopenharmony_ciMODULE_CONSTRUCTOR(void) 89d9f0492fSopenharmony_ci{ 90d9f0492fSopenharmony_ci SetDevUdid(); 91d9f0492fSopenharmony_ci} 92d9f0492fSopenharmony_ci#endif 93