117fd14ceSopenharmony_ci/*
217fd14ceSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
317fd14ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
417fd14ceSopenharmony_ci * you may not use this file except in compliance with the License.
517fd14ceSopenharmony_ci * You may obtain a copy of the License at
617fd14ceSopenharmony_ci *
717fd14ceSopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
817fd14ceSopenharmony_ci *
917fd14ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1017fd14ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1117fd14ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1217fd14ceSopenharmony_ci * See the License for the specific language governing permissions and
1317fd14ceSopenharmony_ci * limitations under the License.
1417fd14ceSopenharmony_ci */
1517fd14ceSopenharmony_ci
1617fd14ceSopenharmony_ci#include "alg_defs.h"
1717fd14ceSopenharmony_ci#include "alg_loader.h"
1817fd14ceSopenharmony_ci#include "hc_log.h"
1917fd14ceSopenharmony_ci#include "identity_manager.h"
2017fd14ceSopenharmony_ci
2117fd14ceSopenharmony_cistatic int32_t SetDlSpekeProtocol(IdentityInfo *info)
2217fd14ceSopenharmony_ci{
2317fd14ceSopenharmony_ci#ifdef ENABLE_P2P_BIND_DL_SPEKE
2417fd14ceSopenharmony_ci    ProtocolEntity *dlSpekeEntity = (ProtocolEntity *)HcMalloc(sizeof(ProtocolEntity), 0);
2517fd14ceSopenharmony_ci    if (dlSpekeEntity == NULL) {
2617fd14ceSopenharmony_ci        LOGE("Failed to alloc memory for dl speke entity!");
2717fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
2817fd14ceSopenharmony_ci    }
2917fd14ceSopenharmony_ci    dlSpekeEntity->protocolType = ALG_DL_SPEKE;
3017fd14ceSopenharmony_ci    dlSpekeEntity->expandProcessCmds = CMD_IMPORT_AUTH_CODE | CMD_ADD_TRUST_DEVICE;
3117fd14ceSopenharmony_ci    if (info->protocolVec.pushBack(&info->protocolVec, (const ProtocolEntity **)&dlSpekeEntity) == NULL) {
3217fd14ceSopenharmony_ci        LOGE("Failed to push dl speke entity!");
3317fd14ceSopenharmony_ci        HcFree(dlSpekeEntity);
3417fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
3517fd14ceSopenharmony_ci    }
3617fd14ceSopenharmony_ci    return HC_SUCCESS;
3717fd14ceSopenharmony_ci#else
3817fd14ceSopenharmony_ci    (void)info;
3917fd14ceSopenharmony_ci    return HC_SUCCESS;
4017fd14ceSopenharmony_ci#endif
4117fd14ceSopenharmony_ci}
4217fd14ceSopenharmony_ci
4317fd14ceSopenharmony_cistatic int32_t SetIsoProtocol(IdentityInfo *info)
4417fd14ceSopenharmony_ci{
4517fd14ceSopenharmony_ci#ifdef ENABLE_P2P_BIND_ISO
4617fd14ceSopenharmony_ci    ProtocolEntity *isoEntity = (ProtocolEntity *)HcMalloc(sizeof(ProtocolEntity), 0);
4717fd14ceSopenharmony_ci    if (isoEntity == NULL) {
4817fd14ceSopenharmony_ci        LOGE("Failed to alloc memory for iso entity!");
4917fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
5017fd14ceSopenharmony_ci    }
5117fd14ceSopenharmony_ci    isoEntity->protocolType = ALG_ISO;
5217fd14ceSopenharmony_ci    isoEntity->expandProcessCmds = CMD_IMPORT_AUTH_CODE | CMD_ADD_TRUST_DEVICE;
5317fd14ceSopenharmony_ci    if (info->protocolVec.pushBack(&info->protocolVec, (const ProtocolEntity **)&isoEntity) == NULL) {
5417fd14ceSopenharmony_ci        LOGE("Failed to push iso entity!");
5517fd14ceSopenharmony_ci        HcFree(isoEntity);
5617fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
5717fd14ceSopenharmony_ci    }
5817fd14ceSopenharmony_ci    return HC_SUCCESS;
5917fd14ceSopenharmony_ci#else
6017fd14ceSopenharmony_ci    (void)info;
6117fd14ceSopenharmony_ci    return HC_SUCCESS;
6217fd14ceSopenharmony_ci#endif
6317fd14ceSopenharmony_ci}
6417fd14ceSopenharmony_ci
6517fd14ceSopenharmony_cistatic int32_t SetLiteProtocols(IdentityInfo *info)
6617fd14ceSopenharmony_ci{
6717fd14ceSopenharmony_ci    int32_t res = SetDlSpekeProtocol(info);
6817fd14ceSopenharmony_ci    if (res != HC_SUCCESS) {
6917fd14ceSopenharmony_ci        return res;
7017fd14ceSopenharmony_ci    }
7117fd14ceSopenharmony_ci    return SetIsoProtocol(info);
7217fd14ceSopenharmony_ci}
7317fd14ceSopenharmony_ci
7417fd14ceSopenharmony_cistatic int32_t SetLiteProtocolsForPinType(const CJson *in, IdentityInfo *info)
7517fd14ceSopenharmony_ci{
7617fd14ceSopenharmony_ci#ifndef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
7717fd14ceSopenharmony_ci    (void)in;
7817fd14ceSopenharmony_ci    return SetLiteProtocols(info);
7917fd14ceSopenharmony_ci#else
8017fd14ceSopenharmony_ci    int32_t protocolExpandVal = INVALID_PROTOCOL_EXPAND_VALUE;
8117fd14ceSopenharmony_ci    (void)GetIntFromJson(in, FIELD_PROTOCOL_EXPAND, &protocolExpandVal);
8217fd14ceSopenharmony_ci    int32_t res = HC_SUCCESS;
8317fd14ceSopenharmony_ci    if (protocolExpandVal == LITE_PROTOCOL_STANDARD_MODE ||
8417fd14ceSopenharmony_ci        protocolExpandVal == LITE_PROTOCOL_COMPATIBILITY_MODE) {
8517fd14ceSopenharmony_ci        res = SetLiteProtocols(info);
8617fd14ceSopenharmony_ci    }
8717fd14ceSopenharmony_ci    return res;
8817fd14ceSopenharmony_ci#endif
8917fd14ceSopenharmony_ci}
9017fd14ceSopenharmony_ci
9117fd14ceSopenharmony_cistatic int32_t SetProtocolsForPinType(const CJson *in, IdentityInfo *info)
9217fd14ceSopenharmony_ci{
9317fd14ceSopenharmony_ci#ifdef ENABLE_P2P_BIND_EC_SPEKE
9417fd14ceSopenharmony_ci    ProtocolEntity *ecSpekeEntity = (ProtocolEntity *)HcMalloc(sizeof(ProtocolEntity), 0);
9517fd14ceSopenharmony_ci    if (ecSpekeEntity == NULL) {
9617fd14ceSopenharmony_ci        LOGE("Failed to alloc memory for ec speke entity!");
9717fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
9817fd14ceSopenharmony_ci    }
9917fd14ceSopenharmony_ci    ecSpekeEntity->protocolType = ALG_EC_SPEKE;
10017fd14ceSopenharmony_ci    ecSpekeEntity->expandProcessCmds = CMD_EXCHANGE_PK | CMD_ADD_TRUST_DEVICE;
10117fd14ceSopenharmony_ci    if (info->protocolVec.pushBack(&info->protocolVec, (const ProtocolEntity **)&ecSpekeEntity) == NULL) {
10217fd14ceSopenharmony_ci        LOGE("Failed to push ec speke entity!");
10317fd14ceSopenharmony_ci        HcFree(ecSpekeEntity);
10417fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
10517fd14ceSopenharmony_ci    }
10617fd14ceSopenharmony_ci#endif
10717fd14ceSopenharmony_ci
10817fd14ceSopenharmony_ci    return SetLiteProtocolsForPinType(in, info);
10917fd14ceSopenharmony_ci}
11017fd14ceSopenharmony_ci
11117fd14ceSopenharmony_cistatic bool IsDirectAuth(const CJson *context)
11217fd14ceSopenharmony_ci{
11317fd14ceSopenharmony_ci    bool isDirectAuth = false;
11417fd14ceSopenharmony_ci    (void)GetBoolFromJson(context, FIELD_IS_DIRECT_AUTH, &isDirectAuth);
11517fd14ceSopenharmony_ci    return isDirectAuth;
11617fd14ceSopenharmony_ci}
11717fd14ceSopenharmony_ci
11817fd14ceSopenharmony_cistatic int32_t SetProtocolsForDirectAuth(IdentityInfo *info)
11917fd14ceSopenharmony_ci{
12017fd14ceSopenharmony_ci#ifdef ENABLE_P2P_AUTH_EC_SPEKE
12117fd14ceSopenharmony_ci    ProtocolEntity *ecSpekeEntity = (ProtocolEntity *)HcMalloc(sizeof(ProtocolEntity), 0);
12217fd14ceSopenharmony_ci    if (ecSpekeEntity == NULL) {
12317fd14ceSopenharmony_ci        LOGE("Failed to alloc memory for ec speke entity!");
12417fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
12517fd14ceSopenharmony_ci    }
12617fd14ceSopenharmony_ci    ecSpekeEntity->protocolType = ALG_EC_SPEKE;
12717fd14ceSopenharmony_ci    ecSpekeEntity->expandProcessCmds = 0;
12817fd14ceSopenharmony_ci    if (info->protocolVec.pushBack(&info->protocolVec, (const ProtocolEntity **)&ecSpekeEntity) == NULL) {
12917fd14ceSopenharmony_ci        LOGE("Failed to push ecspeke entity!");
13017fd14ceSopenharmony_ci        HcFree(ecSpekeEntity);
13117fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
13217fd14ceSopenharmony_ci    }
13317fd14ceSopenharmony_ci#else
13417fd14ceSopenharmony_ci#endif
13517fd14ceSopenharmony_ci
13617fd14ceSopenharmony_ci    return HC_SUCCESS;
13717fd14ceSopenharmony_ci}
13817fd14ceSopenharmony_ci
13917fd14ceSopenharmony_cistatic int32_t GetCredInfosByPeerIdentity(const CJson *in, IdentityInfoVec *vec)
14017fd14ceSopenharmony_ci{
14117fd14ceSopenharmony_ci    IdentityInfo *info = CreateIdentityInfo();
14217fd14ceSopenharmony_ci    if (info == NULL) {
14317fd14ceSopenharmony_ci        LOGE("Failed to create identity info!");
14417fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
14517fd14ceSopenharmony_ci    }
14617fd14ceSopenharmony_ci    CJson *urlJson = CreateCredUrlJson(PRE_SHARED, KEY_TYPE_SYM, TRUST_TYPE_PIN);
14717fd14ceSopenharmony_ci    if (!urlJson) {
14817fd14ceSopenharmony_ci        LOGE("Failed to create CredUrlJson info!");
14917fd14ceSopenharmony_ci        DestroyIdentityInfo(info);
15017fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
15117fd14ceSopenharmony_ci    }
15217fd14ceSopenharmony_ci    if (IsDirectAuth(in) && AddBoolToJson(urlJson, FIELD_IS_DIRECT_AUTH, true) != HC_SUCCESS) {
15317fd14ceSopenharmony_ci        LOGE("Failed to isDirectAuth to preshared url!");
15417fd14ceSopenharmony_ci        FreeJson(urlJson);
15517fd14ceSopenharmony_ci        DestroyIdentityInfo(info);
15617fd14ceSopenharmony_ci        return HC_ERR_JSON_ADD;
15717fd14ceSopenharmony_ci    }
15817fd14ceSopenharmony_ci    char *urlStr = PackJsonToString(urlJson);
15917fd14ceSopenharmony_ci    FreeJson(urlJson);
16017fd14ceSopenharmony_ci    if (urlStr == NULL) {
16117fd14ceSopenharmony_ci        LOGE("Failed to pack url json to string!");
16217fd14ceSopenharmony_ci        DestroyIdentityInfo(info);
16317fd14ceSopenharmony_ci        return HC_ERR_PACKAGE_JSON_TO_STRING_FAIL;
16417fd14ceSopenharmony_ci    }
16517fd14ceSopenharmony_ci    int32_t ret = SetPreSharedUrlForProof(urlStr, &info->proof.preSharedUrl);
16617fd14ceSopenharmony_ci    FreeJsonString(urlStr);
16717fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
16817fd14ceSopenharmony_ci        LOGE("Failed to set preSharedUrl of proof!");
16917fd14ceSopenharmony_ci        DestroyIdentityInfo(info);
17017fd14ceSopenharmony_ci        return ret;
17117fd14ceSopenharmony_ci    }
17217fd14ceSopenharmony_ci    if (IsDirectAuth(in)) {
17317fd14ceSopenharmony_ci        ret = SetProtocolsForDirectAuth(info);
17417fd14ceSopenharmony_ci        info->IdInfoType = P2P_DIRECT_AUTH;
17517fd14ceSopenharmony_ci    } else {
17617fd14ceSopenharmony_ci        ret = SetProtocolsForPinType(in, info);
17717fd14ceSopenharmony_ci    }
17817fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
17917fd14ceSopenharmony_ci        LOGE("Failed to set protocols!");
18017fd14ceSopenharmony_ci        DestroyIdentityInfo(info);
18117fd14ceSopenharmony_ci        return ret;
18217fd14ceSopenharmony_ci    }
18317fd14ceSopenharmony_ci    info->proofType = PRE_SHARED;
18417fd14ceSopenharmony_ci    vec->pushBack(vec, (const IdentityInfo **)&info);
18517fd14ceSopenharmony_ci    return HC_SUCCESS;
18617fd14ceSopenharmony_ci}
18717fd14ceSopenharmony_ci
18817fd14ceSopenharmony_cistatic int32_t GetCredInfoByPeerUrl(const CJson *in, const Uint8Buff *presharedUrl, IdentityInfo **returnInfo)
18917fd14ceSopenharmony_ci{
19017fd14ceSopenharmony_ci    if (in == NULL || presharedUrl == NULL || returnInfo == NULL) {
19117fd14ceSopenharmony_ci        LOGE("Invalid input params!");
19217fd14ceSopenharmony_ci        return HC_ERR_INVALID_PARAMS;
19317fd14ceSopenharmony_ci    }
19417fd14ceSopenharmony_ci    IdentityInfo *info = CreateIdentityInfo();
19517fd14ceSopenharmony_ci    if (info == NULL) {
19617fd14ceSopenharmony_ci        LOGE("Failed to create identity info!");
19717fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
19817fd14ceSopenharmony_ci    }
19917fd14ceSopenharmony_ci    CJson *urlJson = CreateJsonFromString((const char *)presharedUrl->val);
20017fd14ceSopenharmony_ci    if (urlJson == NULL) {
20117fd14ceSopenharmony_ci        LOGE("Failed to create url json!");
20217fd14ceSopenharmony_ci        DestroyIdentityInfo(info);
20317fd14ceSopenharmony_ci        return HC_ERR_JSON_CREATE;
20417fd14ceSopenharmony_ci    }
20517fd14ceSopenharmony_ci    int32_t credentialType = PRE_SHARED;
20617fd14ceSopenharmony_ci    if (GetIntFromJson(urlJson, PRESHARED_URL_CREDENTIAL_TYPE, &credentialType) != HC_SUCCESS) {
20717fd14ceSopenharmony_ci        LOGE("Failed to get credential type!");
20817fd14ceSopenharmony_ci        DestroyIdentityInfo(info);
20917fd14ceSopenharmony_ci        FreeJson(urlJson);
21017fd14ceSopenharmony_ci        return HC_ERR_JSON_GET;
21117fd14ceSopenharmony_ci    }
21217fd14ceSopenharmony_ci    FreeJson(urlJson);
21317fd14ceSopenharmony_ci    int32_t ret = SetPreSharedUrlForProof((const char *)presharedUrl->val, &info->proof.preSharedUrl);
21417fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
21517fd14ceSopenharmony_ci        LOGE("Failed to set preSharedUrl of proof!");
21617fd14ceSopenharmony_ci        DestroyIdentityInfo(info);
21717fd14ceSopenharmony_ci        return ret;
21817fd14ceSopenharmony_ci    }
21917fd14ceSopenharmony_ci    if (IsDirectAuth(in)) {
22017fd14ceSopenharmony_ci        ret = SetProtocolsForDirectAuth(info);
22117fd14ceSopenharmony_ci        info->IdInfoType = P2P_DIRECT_AUTH;
22217fd14ceSopenharmony_ci    } else {
22317fd14ceSopenharmony_ci        ret = SetProtocolsForPinType(in, info);
22417fd14ceSopenharmony_ci    }
22517fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
22617fd14ceSopenharmony_ci        LOGE("Failed to set protocols!");
22717fd14ceSopenharmony_ci        DestroyIdentityInfo(info);
22817fd14ceSopenharmony_ci        return ret;
22917fd14ceSopenharmony_ci    }
23017fd14ceSopenharmony_ci    info->proofType = credentialType;
23117fd14ceSopenharmony_ci    *returnInfo = info;
23217fd14ceSopenharmony_ci    return HC_SUCCESS;
23317fd14ceSopenharmony_ci}
23417fd14ceSopenharmony_ci
23517fd14ceSopenharmony_cistatic int32_t AuthGeneratePskUsePin(const CJson *in, const Uint8Buff *seed, const char *pinCode,
23617fd14ceSopenharmony_ci    Uint8Buff *sharedSecret)
23717fd14ceSopenharmony_ci{
23817fd14ceSopenharmony_ci    int32_t osAccountId;
23917fd14ceSopenharmony_ci    if (GetIntFromJson(in, FIELD_OS_ACCOUNT_ID, &osAccountId) != HC_SUCCESS) {
24017fd14ceSopenharmony_ci        LOGE("Failed to get osAccountId!");
24117fd14ceSopenharmony_ci        return HC_ERR_JSON_GET;
24217fd14ceSopenharmony_ci    }
24317fd14ceSopenharmony_ci    Uint8Buff messageBuf = { (uint8_t *)pinCode, (uint32_t)HcStrlen(pinCode) };
24417fd14ceSopenharmony_ci    uint8_t hash[SHA256_LEN] = { 0 };
24517fd14ceSopenharmony_ci    Uint8Buff hashBuf = { hash, sizeof(hash) };
24617fd14ceSopenharmony_ci    int ret = GetLoaderInstance()->sha256(&messageBuf, &hashBuf);
24717fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
24817fd14ceSopenharmony_ci        LOGE("sha256 failed, ret:%d", ret);
24917fd14ceSopenharmony_ci        return ret;
25017fd14ceSopenharmony_ci    }
25117fd14ceSopenharmony_ci    KeyParams keyParams = { { hashBuf.val, hashBuf.length, false }, false, osAccountId };
25217fd14ceSopenharmony_ci    return GetLoaderInstance()->computeHmac(&keyParams, seed, sharedSecret);
25317fd14ceSopenharmony_ci}
25417fd14ceSopenharmony_ci
25517fd14ceSopenharmony_ci#ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
25617fd14ceSopenharmony_cistatic bool CheckPinLenForStandardIso(const CJson *in, const char *pinCode)
25717fd14ceSopenharmony_ci{
25817fd14ceSopenharmony_ci    int32_t protocolExpandVal = INVALID_PROTOCOL_EXPAND_VALUE;
25917fd14ceSopenharmony_ci    (void)GetIntFromJson(in, FIELD_PROTOCOL_EXPAND, &protocolExpandVal);
26017fd14ceSopenharmony_ci    if (protocolExpandVal != LITE_PROTOCOL_STANDARD_MODE) {
26117fd14ceSopenharmony_ci        LOGI("not standard iso, no need to check.");
26217fd14ceSopenharmony_ci        return true;
26317fd14ceSopenharmony_ci    }
26417fd14ceSopenharmony_ci    return HcStrlen(pinCode) >= PIN_CODE_LEN_LONG;
26517fd14ceSopenharmony_ci}
26617fd14ceSopenharmony_ci#endif
26717fd14ceSopenharmony_ci
26817fd14ceSopenharmony_cistatic int32_t GetSharedSecretForPinInIso(const CJson *in, Uint8Buff *sharedSecret)
26917fd14ceSopenharmony_ci{
27017fd14ceSopenharmony_ci    const char *pinCode = GetStringFromJson(in, FIELD_PIN_CODE);
27117fd14ceSopenharmony_ci    if (pinCode == NULL) {
27217fd14ceSopenharmony_ci        LOGE("Failed to get pinCode!");
27317fd14ceSopenharmony_ci        return HC_ERR_JSON_GET;
27417fd14ceSopenharmony_ci    }
27517fd14ceSopenharmony_ci    if (HcStrlen(pinCode) < PIN_CODE_LEN_SHORT) {
27617fd14ceSopenharmony_ci        LOGE("Pin code is too short!");
27717fd14ceSopenharmony_ci        return HC_ERR_INVALID_LEN;
27817fd14ceSopenharmony_ci    }
27917fd14ceSopenharmony_ci#ifdef ENABLE_P2P_BIND_LITE_PROTOCOL_CHECK
28017fd14ceSopenharmony_ci    if (!CheckPinLenForStandardIso(in, pinCode)) {
28117fd14ceSopenharmony_ci        LOGE("Invalid pin code len!");
28217fd14ceSopenharmony_ci        return HC_ERR_INVALID_LEN;
28317fd14ceSopenharmony_ci    }
28417fd14ceSopenharmony_ci#endif
28517fd14ceSopenharmony_ci    uint8_t *seedVal = (uint8_t *)HcMalloc(SEED_LEN, 0);
28617fd14ceSopenharmony_ci    if (seedVal == NULL) {
28717fd14ceSopenharmony_ci        LOGE("Failed to alloc seed memory!");
28817fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
28917fd14ceSopenharmony_ci    }
29017fd14ceSopenharmony_ci    Uint8Buff seedBuff = { seedVal, SEED_LEN };
29117fd14ceSopenharmony_ci    int32_t ret = GetByteFromJson(in, FIELD_SEED, seedBuff.val, seedBuff.length);
29217fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
29317fd14ceSopenharmony_ci        LOGE("Failed to get seed!");
29417fd14ceSopenharmony_ci        HcFree(seedVal);
29517fd14ceSopenharmony_ci        return HC_ERR_JSON_GET;
29617fd14ceSopenharmony_ci    }
29717fd14ceSopenharmony_ci    uint8_t *pskVal = (uint8_t *)HcMalloc(ISO_PSK_LEN, 0);
29817fd14ceSopenharmony_ci    if (pskVal == NULL) {
29917fd14ceSopenharmony_ci        LOGE("Failed to alloc psk memory!");
30017fd14ceSopenharmony_ci        HcFree(seedVal);
30117fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
30217fd14ceSopenharmony_ci    }
30317fd14ceSopenharmony_ci    sharedSecret->val = pskVal;
30417fd14ceSopenharmony_ci    sharedSecret->length = ISO_PSK_LEN;
30517fd14ceSopenharmony_ci    ret = AuthGeneratePskUsePin(in, &seedBuff, pinCode, sharedSecret);
30617fd14ceSopenharmony_ci    HcFree(seedVal);
30717fd14ceSopenharmony_ci    if (ret != HC_SUCCESS) {
30817fd14ceSopenharmony_ci        LOGE("Failed to generate psk use pin!");
30917fd14ceSopenharmony_ci        FreeBuffData(sharedSecret);
31017fd14ceSopenharmony_ci    }
31117fd14ceSopenharmony_ci    return ret;
31217fd14ceSopenharmony_ci}
31317fd14ceSopenharmony_ci
31417fd14ceSopenharmony_cistatic int32_t GetSharedSecretForPinInPake(const CJson *in, Uint8Buff *sharedSecret)
31517fd14ceSopenharmony_ci{
31617fd14ceSopenharmony_ci    const char *pinCode = GetStringFromJson(in, FIELD_PIN_CODE);
31717fd14ceSopenharmony_ci    if (pinCode == NULL) {
31817fd14ceSopenharmony_ci        LOGE("Failed to get pinCode!");
31917fd14ceSopenharmony_ci        return HC_ERR_JSON_GET;
32017fd14ceSopenharmony_ci    }
32117fd14ceSopenharmony_ci    uint32_t pinLen = HcStrlen(pinCode);
32217fd14ceSopenharmony_ci    if (pinLen < PIN_CODE_LEN_SHORT) {
32317fd14ceSopenharmony_ci        LOGE("Invalid pin code len!");
32417fd14ceSopenharmony_ci        return HC_ERR_INVALID_LEN;
32517fd14ceSopenharmony_ci    }
32617fd14ceSopenharmony_ci    sharedSecret->val = (uint8_t *)HcMalloc(pinLen, 0);
32717fd14ceSopenharmony_ci    if (sharedSecret->val == NULL) {
32817fd14ceSopenharmony_ci        LOGE("Failed to alloc sharedSecret memory!");
32917fd14ceSopenharmony_ci        return HC_ERR_ALLOC_MEMORY;
33017fd14ceSopenharmony_ci    }
33117fd14ceSopenharmony_ci    if (memcpy_s(sharedSecret->val, pinLen, pinCode, pinLen) != HC_SUCCESS) {
33217fd14ceSopenharmony_ci        LOGE("Failed to memcpy pinCode!");
33317fd14ceSopenharmony_ci        FreeBuffData(sharedSecret);
33417fd14ceSopenharmony_ci        return HC_ERR_MEMORY_COPY;
33517fd14ceSopenharmony_ci    }
33617fd14ceSopenharmony_ci    sharedSecret->length = pinLen;
33717fd14ceSopenharmony_ci    return HC_SUCCESS;
33817fd14ceSopenharmony_ci}
33917fd14ceSopenharmony_ci
34017fd14ceSopenharmony_cistatic int32_t GetSharedSecretByUrl(
34117fd14ceSopenharmony_ci    const CJson *in, const Uint8Buff *presharedUrl, ProtocolAlgType protocolType, Uint8Buff *sharedSecret)
34217fd14ceSopenharmony_ci{
34317fd14ceSopenharmony_ci    if (in == NULL || presharedUrl == NULL || sharedSecret == NULL) {
34417fd14ceSopenharmony_ci        LOGE("Invalid input params!");
34517fd14ceSopenharmony_ci        return HC_ERR_INVALID_PARAMS;
34617fd14ceSopenharmony_ci    }
34717fd14ceSopenharmony_ci    int32_t ret;
34817fd14ceSopenharmony_ci    if (protocolType == ALG_ISO) {
34917fd14ceSopenharmony_ci        ret = GetSharedSecretForPinInIso(in, sharedSecret);
35017fd14ceSopenharmony_ci        LOGI("get shared secret for pin in iso result: %d", ret);
35117fd14ceSopenharmony_ci    } else {
35217fd14ceSopenharmony_ci        ret = GetSharedSecretForPinInPake(in, sharedSecret);
35317fd14ceSopenharmony_ci        LOGI("get shared secret for pin in pake result: %d", ret);
35417fd14ceSopenharmony_ci    }
35517fd14ceSopenharmony_ci
35617fd14ceSopenharmony_ci    return ret;
35717fd14ceSopenharmony_ci}
35817fd14ceSopenharmony_ci
35917fd14ceSopenharmony_cistatic int32_t GetCredInfoByPeerCert(const CJson *in, const CertInfo *certInfo, IdentityInfo **returnInfo)
36017fd14ceSopenharmony_ci{
36117fd14ceSopenharmony_ci    // NOT SUPPORT FOR PIN
36217fd14ceSopenharmony_ci    (void)in;
36317fd14ceSopenharmony_ci    (void)certInfo;
36417fd14ceSopenharmony_ci    (void)returnInfo;
36517fd14ceSopenharmony_ci    return HC_ERR_ALG_FAIL;
36617fd14ceSopenharmony_ci}
36717fd14ceSopenharmony_ci
36817fd14ceSopenharmony_cistatic int32_t GetSharedSecretByPeerCert(
36917fd14ceSopenharmony_ci    const CJson *in, const CertInfo *peerCertInfo, ProtocolAlgType protocolType, Uint8Buff *sharedSecret)
37017fd14ceSopenharmony_ci{
37117fd14ceSopenharmony_ci    // NOT SUPPORT FOR PIN
37217fd14ceSopenharmony_ci    (void)in;
37317fd14ceSopenharmony_ci    (void)peerCertInfo;
37417fd14ceSopenharmony_ci    (void)protocolType;
37517fd14ceSopenharmony_ci    (void)sharedSecret;
37617fd14ceSopenharmony_ci    return HC_ERR_ALG_FAIL;
37717fd14ceSopenharmony_ci}
37817fd14ceSopenharmony_ci
37917fd14ceSopenharmony_cistatic const AuthIdentity g_authIdentity = {
38017fd14ceSopenharmony_ci    .getCredInfosByPeerIdentity = GetCredInfosByPeerIdentity,
38117fd14ceSopenharmony_ci    .getCredInfoByPeerUrl = GetCredInfoByPeerUrl,
38217fd14ceSopenharmony_ci    .getSharedSecretByUrl = GetSharedSecretByUrl,
38317fd14ceSopenharmony_ci    .getCredInfoByPeerCert = GetCredInfoByPeerCert,
38417fd14ceSopenharmony_ci    .getSharedSecretByPeerCert = GetSharedSecretByPeerCert,
38517fd14ceSopenharmony_ci};
38617fd14ceSopenharmony_ci
38717fd14ceSopenharmony_ciconst AuthIdentity *GetPinAuthIdentity(void)
38817fd14ceSopenharmony_ci{
38917fd14ceSopenharmony_ci    return &g_authIdentity;
39017fd14ceSopenharmony_ci}