param_comm.c 6.8 KB
Newer Older
M
Mupceet 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "param_comm.h"

#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#include "init_param.h"
#include "parameter.h"
#include "sysparam_errno.h"
#ifdef USE_MBEDTLS
#include "mbedtls/sha256.h"
#elif !(defined OHOS_LITE)
#include "openssl/sha.h"
#endif
#include "securec.h"

static const char *g_emptyStr = "";

M
Mupceet 已提交
34
INIT_LOCAL_API int IsValidParamValue(const char *value, uint32_t len)
M
Mupceet 已提交
35
{
M
Mupceet 已提交
36
    if ((value == NULL) || (strlen(value) + 1 > len)) {
M
Mupceet 已提交
37 38 39 40 41
        return 0;
    }
    return 1;
}

M
Mupceet 已提交
42
INIT_LOCAL_API int GetParameter_(const char *key, const char *def, char *value, uint32_t len)
M
Mupceet 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55
{
    if ((key == NULL) || (value == NULL)) {
        return EC_INVALID;
    }
    uint32_t size = len;
    int ret = SystemGetParameter(key, NULL, &size);
    if ((size > len) || (ret != 0)) {
        return strcpy_s(value, len, def);
    }
    size = len;
    return (SystemGetParameter(key, value, &size) == 0) ? EC_SUCCESS : EC_FAILURE;
}

M
Mupceet 已提交
56
INIT_LOCAL_API const char *GetProperty(const char *key, const char **paramHolder)
M
Mupceet 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
{
    if (paramHolder == NULL) {
        return NULL;
    }
    if (*paramHolder != NULL) {
        return *paramHolder;
    }
    uint32_t len = 0;
    int ret = SystemGetParameter(key, NULL, &len);
    if (ret == 0 && len > 0) {
        char *res = (char *)malloc(len + 1);
        if (res == NULL) {
            return g_emptyStr;
        }
        ret = SystemGetParameter(key, res, &len);
        if (ret != 0) {
            free(res);
            return g_emptyStr;
        }
        *paramHolder = res;
    }
    return *paramHolder;
}

M
Mupceet 已提交
81
INIT_LOCAL_API int StringToLL(const char *str, long long int *out)
M
Mupceet 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
{
    const char* s = str;
    while (isspace(*s)) {
        s++;
    }

    size_t len = strlen(str);
    int positiveHex = (len > 1 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'));
    int negativeHex = (len > 2 && s[0] == '-' && s[1] == '0' && (s[2] == 'x' || s[2] == 'X')); // 2: shorttest
    int base = (positiveHex || negativeHex) ? HEX : DECIMAL;
    char* end = NULL;
    errno = 0;
    *out = strtoll(s, &end, base);
    if (errno != 0) {
        return -1;
    }
    if (s == end || *end != '\0') {
        return -1;
    }
    return 0;
}

M
Mupceet 已提交
104
INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out)
M
Mupceet 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
{
    const char* s = str;
    while (isspace(*s)) {
        s++;
    }

    if (s[0] == '-') {
        return -1;
    }

    int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? HEX : DECIMAL;
    char* end = NULL;
    errno = 0;
    *out = strtoull(s, &end, base);
    if (errno != 0) {
        return -1;
    }
    if (end == s) {
        return -1;
    }
    if (*end != '\0') {
        return -1;
    }
    return 0;
}

M
Mupceet 已提交
131
INIT_LOCAL_API const char *GetProductModel_(void)
M
Mupceet 已提交
132 133 134 135 136
{
    static const char *productModel = NULL;
    return GetProperty("const.product.model", &productModel);
}

M
Mupceet 已提交
137
INIT_LOCAL_API const char *GetManufacture_(void)
M
Mupceet 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
{
    static const char *productManufacture = NULL;
    return GetProperty("const.product.manufacturer", &productManufacture);
}

#ifdef USE_MBEDTLS
static int GetSha256Value(const char *input, char *udid, int udidSize)
{
    if (input == NULL) {
        return EC_FAILURE;
    }
    char buf[DEV_BUF_LENGTH] = { 0 };
    unsigned char hash[HASH_LENGTH] = { 0 };

    mbedtls_sha256_context context;
    mbedtls_sha256_init(&context);
    mbedtls_sha256_starts_ret(&context, 0);
M
Mupceet 已提交
155
    mbedtls_sha256_update_ret(&context, (const unsigned char *)input, strlen(input));
M
Mupceet 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    mbedtls_sha256_finish_ret(&context, hash);

    for (size_t i = 0; i < HASH_LENGTH; i++) {
        unsigned char value = hash[i];
        memset_s(buf, DEV_BUF_LENGTH, 0, DEV_BUF_LENGTH);
        int len = sprintf_s(buf, sizeof(buf), "%02X", value);
        if (len > 0 && strcat_s(udid, udidSize, buf) != 0) {
            return EC_FAILURE;
        }
    }
    return EC_SUCCESS;
}
#elif !(defined OHOS_LITE)
static int GetSha256Value(const char *input, char *udid, int udidSize)
{
    char buf[DEV_BUF_LENGTH] = { 0 };
    unsigned char hash[SHA256_DIGEST_LENGTH] = { 0 };
    SHA256_CTX sha256;
    if ((SHA256_Init(&sha256) == 0) || (SHA256_Update(&sha256, input, strlen(input)) == 0) ||
        (SHA256_Final(hash, &sha256) == 0)) {
        return -1;
    }

    for (size_t i = 0; i < SHA256_DIGEST_LENGTH; i++) {
        unsigned char value = hash[i];
        (void)memset_s(buf, DEV_BUF_LENGTH, 0, DEV_BUF_LENGTH);
        int len = sprintf_s(buf, sizeof(buf), "%02X", value);
        if (len > 0 && strcat_s(udid, udidSize, buf) != 0) {
            return -1;
        }
    }
    return 0;
}
#else
static int GetSha256Value(const char *input, char *udid, int udidSize)
{
    return EC_FAILURE;
}
#endif

M
Mupceet 已提交
196
INIT_LOCAL_API const char *GetSerial_(void)
M
Mupceet 已提交
197
{
M
Mupceet 已提交
198 199 200 201 202 203 204 205 206 207 208
#ifdef LITEOS_SUPPORT
    return HalGetSerial();
#else
    static char ohos_serial[PARAM_VALUE_LEN_MAX]  = {0};
    uint32_t len = PARAM_VALUE_LEN_MAX;
    int ret = SystemGetParameter("ohos.boot.sn", ohos_serial, &len);
    if (ret != 0) {
        return NULL;
    }
    return ohos_serial;
#endif
M
Mupceet 已提交
209 210
}

M
Mupceet 已提交
211
INIT_LOCAL_API int GetDevUdid_(char *udid, int size)
M
Mupceet 已提交
212
{
M
Mupceet 已提交
213 214 215
    if (size < UDID_LEN || udid == NULL) {
        return EC_FAILURE;
    }
M
Mupceet 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    const char *manufacture = GetManufacture_();
    const char *model = GetProductModel_();
    const char *sn = GetSerial_();
    if (manufacture == NULL || model == NULL || sn == NULL) {
        return -1;
    }
    int tmpSize = strlen(manufacture) + strlen(model) + strlen(sn) + 1;
    if (tmpSize <= 0 || tmpSize > DEV_BUF_MAX_LENGTH) {
        return -1;
    }
    char *tmp = (char *)malloc(tmpSize);
    if (tmp == NULL) {
        return -1;
    }

    (void)memset_s(tmp, tmpSize, 0, tmpSize);
    if ((strcat_s(tmp, tmpSize, manufacture) != 0) || (strcat_s(tmp, tmpSize, model) != 0) ||
        (strcat_s(tmp, tmpSize, sn) != 0)) {
        free(tmp);
        return -1;
    }

    int ret = GetSha256Value(tmp, udid, size);
    free(tmp);
    return ret;
M
Mupceet 已提交
241 242 243 244 245 246
}

INIT_LOCAL_API const char *GetFullName_(void)
{
    static const char *fillname = NULL;
    return GetProperty("const.ohos.fullname", &fillname);
M
Mupceet 已提交
247
}