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 "group_auth_data_operation.h" 1717fd14ceSopenharmony_ci#include "hc_dev_info.h" 1817fd14ceSopenharmony_ci#include "hc_log.h" 1917fd14ceSopenharmony_ci#include "hc_vector.h" 2017fd14ceSopenharmony_ci#include "identity_manager.h" 2117fd14ceSopenharmony_ci 2217fd14ceSopenharmony_cistatic void UpperToLowercase(Uint8Buff *hex) 2317fd14ceSopenharmony_ci{ 2417fd14ceSopenharmony_ci for (uint32_t i = 0; i < hex->length; i++) { 2517fd14ceSopenharmony_ci if (hex->val[i] >= 'A' && hex->val[i] <= 'F') { 2617fd14ceSopenharmony_ci hex->val[i] += ASCII_CASE_DIFFERENCE_VALUE; 2717fd14ceSopenharmony_ci } 2817fd14ceSopenharmony_ci } 2917fd14ceSopenharmony_ci} 3017fd14ceSopenharmony_ci 3117fd14ceSopenharmony_ciint32_t ConvertPsk(const Uint8Buff *srcPsk, Uint8Buff *sharedSecret) 3217fd14ceSopenharmony_ci{ 3317fd14ceSopenharmony_ci uint32_t len = PAKE_PSK_LEN * BYTE_TO_HEX_OPER_LENGTH; 3417fd14ceSopenharmony_ci sharedSecret->val = (uint8_t *)HcMalloc(len + 1, 0); 3517fd14ceSopenharmony_ci if (sharedSecret->val == NULL) { 3617fd14ceSopenharmony_ci LOGE("Failed to alloc memory for sharedSecret!"); 3717fd14ceSopenharmony_ci return HC_ERR_ALLOC_MEMORY; 3817fd14ceSopenharmony_ci } 3917fd14ceSopenharmony_ci 4017fd14ceSopenharmony_ci if (ByteToHexString(srcPsk->val, srcPsk->length, (char *)sharedSecret->val, len + 1) != HC_SUCCESS) { 4117fd14ceSopenharmony_ci LOGE("Convert psk from byte to hex string failed!"); 4217fd14ceSopenharmony_ci HcFree(sharedSecret->val); 4317fd14ceSopenharmony_ci return HC_ERR_CONVERT_FAILED; 4417fd14ceSopenharmony_ci } 4517fd14ceSopenharmony_ci sharedSecret->length = len; 4617fd14ceSopenharmony_ci (void)UpperToLowercase(sharedSecret); 4717fd14ceSopenharmony_ci return HC_SUCCESS; 4817fd14ceSopenharmony_ci} 4917fd14ceSopenharmony_ci 5017fd14ceSopenharmony_ciint32_t SetPreSharedUrlForProof(const char *urlStr, Uint8Buff *preSharedUrl) 5117fd14ceSopenharmony_ci{ 5217fd14ceSopenharmony_ci uint32_t urlLen = HcStrlen(urlStr); 5317fd14ceSopenharmony_ci preSharedUrl->val = (uint8_t *)HcMalloc(urlLen + 1, 0); 5417fd14ceSopenharmony_ci if (preSharedUrl->val == NULL) { 5517fd14ceSopenharmony_ci LOGE("Failed to alloc preSharedUrl memory!"); 5617fd14ceSopenharmony_ci return HC_ERR_ALLOC_MEMORY; 5717fd14ceSopenharmony_ci } 5817fd14ceSopenharmony_ci if (memcpy_s(preSharedUrl->val, urlLen + 1, urlStr, urlLen) != EOK) { 5917fd14ceSopenharmony_ci LOGE("Failed to copy url string to preSharedUrl"); 6017fd14ceSopenharmony_ci HcFree(preSharedUrl->val); 6117fd14ceSopenharmony_ci preSharedUrl->val = NULL; 6217fd14ceSopenharmony_ci return HC_ERR_MEMORY_COPY; 6317fd14ceSopenharmony_ci } 6417fd14ceSopenharmony_ci preSharedUrl->length = urlLen + 1; 6517fd14ceSopenharmony_ci return HC_SUCCESS; 6617fd14ceSopenharmony_ci} 6717fd14ceSopenharmony_ci 6817fd14ceSopenharmony_ciCJson *CreateCredUrlJson(int32_t credentailType, int32_t keyType, int32_t trustType) 6917fd14ceSopenharmony_ci{ 7017fd14ceSopenharmony_ci CJson *urlJson = CreateJson(); 7117fd14ceSopenharmony_ci if (urlJson == NULL) { 7217fd14ceSopenharmony_ci LOGE("Failed to create url json!"); 7317fd14ceSopenharmony_ci return NULL; 7417fd14ceSopenharmony_ci } 7517fd14ceSopenharmony_ci if (AddIntToJson(urlJson, PRESHARED_URL_CREDENTIAL_TYPE, credentailType) != HC_SUCCESS) { 7617fd14ceSopenharmony_ci LOGE("Failed to add credential type!"); 7717fd14ceSopenharmony_ci FreeJson(urlJson); 7817fd14ceSopenharmony_ci return NULL; 7917fd14ceSopenharmony_ci } 8017fd14ceSopenharmony_ci if (AddIntToJson(urlJson, PRESHARED_URL_KEY_TYPE, keyType) != HC_SUCCESS) { 8117fd14ceSopenharmony_ci LOGE("Failed to add key type!"); 8217fd14ceSopenharmony_ci FreeJson(urlJson); 8317fd14ceSopenharmony_ci return NULL; 8417fd14ceSopenharmony_ci } 8517fd14ceSopenharmony_ci if (AddIntToJson(urlJson, PRESHARED_URL_TRUST_TYPE, trustType) != HC_SUCCESS) { 8617fd14ceSopenharmony_ci LOGE("Failed to add trust type!"); 8717fd14ceSopenharmony_ci FreeJson(urlJson); 8817fd14ceSopenharmony_ci return NULL; 8917fd14ceSopenharmony_ci } 9017fd14ceSopenharmony_ci 9117fd14ceSopenharmony_ci return urlJson; 9217fd14ceSopenharmony_ci} 9317fd14ceSopenharmony_ci 9417fd14ceSopenharmony_ci#if 1 9517fd14ceSopenharmony_ci 9617fd14ceSopenharmony_ciIMPLEMENT_HC_VECTOR(ProtocolEntityVec, ProtocolEntity *, 1) 9717fd14ceSopenharmony_ciIMPLEMENT_HC_VECTOR(IdentityInfoVec, IdentityInfo *, 1) 9817fd14ceSopenharmony_ci 9917fd14ceSopenharmony_ciint32_t GetSelfDeviceEntry(int32_t osAccountId, const char *groupId, TrustedDeviceEntry *deviceEntry) 10017fd14ceSopenharmony_ci{ 10117fd14ceSopenharmony_ci char selfUdid[INPUT_UDID_LEN] = { 0 }; 10217fd14ceSopenharmony_ci int32_t ret = HcGetUdid((uint8_t *)selfUdid, INPUT_UDID_LEN); 10317fd14ceSopenharmony_ci if (ret != HC_SUCCESS) { 10417fd14ceSopenharmony_ci LOGE("Failed to get local udid!"); 10517fd14ceSopenharmony_ci return ret; 10617fd14ceSopenharmony_ci } 10717fd14ceSopenharmony_ci return GaGetTrustedDeviceEntryById(osAccountId, selfUdid, true, groupId, deviceEntry); 10817fd14ceSopenharmony_ci} 10917fd14ceSopenharmony_ci 11017fd14ceSopenharmony_ciconst char *GetPeerDevIdFromJson(const CJson *in, bool *isUdid) 11117fd14ceSopenharmony_ci{ 11217fd14ceSopenharmony_ci const char *deviceId = GetStringFromJson(in, FIELD_PEER_UDID); 11317fd14ceSopenharmony_ci if (deviceId != NULL) { 11417fd14ceSopenharmony_ci *isUdid = true; 11517fd14ceSopenharmony_ci return deviceId; 11617fd14ceSopenharmony_ci } 11717fd14ceSopenharmony_ci return GetStringFromJson(in, FIELD_PEER_AUTH_ID); 11817fd14ceSopenharmony_ci} 11917fd14ceSopenharmony_ci 12017fd14ceSopenharmony_ciint32_t GetPeerDeviceEntry( 12117fd14ceSopenharmony_ci int32_t osAccountId, const CJson *in, const char *groupId, TrustedDeviceEntry *returnDeviceEntry) 12217fd14ceSopenharmony_ci{ 12317fd14ceSopenharmony_ci bool isUdid = false; 12417fd14ceSopenharmony_ci const char *peerDeviceId = GetPeerDevIdFromJson(in, &isUdid); 12517fd14ceSopenharmony_ci if (peerDeviceId == NULL) { 12617fd14ceSopenharmony_ci LOGE("Failed to get peer deviceId!"); 12717fd14ceSopenharmony_ci return HC_ERR_JSON_GET; 12817fd14ceSopenharmony_ci } 12917fd14ceSopenharmony_ci return GaGetTrustedDeviceEntryById(osAccountId, peerDeviceId, isUdid, groupId, returnDeviceEntry); 13017fd14ceSopenharmony_ci} 13117fd14ceSopenharmony_ci 13217fd14ceSopenharmony_civoid FreeBuffData(Uint8Buff *buff) 13317fd14ceSopenharmony_ci{ 13417fd14ceSopenharmony_ci if (buff == NULL) { 13517fd14ceSopenharmony_ci return; 13617fd14ceSopenharmony_ci } 13717fd14ceSopenharmony_ci HcFree(buff->val); 13817fd14ceSopenharmony_ci buff->val = NULL; 13917fd14ceSopenharmony_ci buff->length = 0; 14017fd14ceSopenharmony_ci} 14117fd14ceSopenharmony_ci 14217fd14ceSopenharmony_ciIdentityInfo *CreateIdentityInfo(void) 14317fd14ceSopenharmony_ci{ 14417fd14ceSopenharmony_ci IdentityInfo *info = (IdentityInfo *)HcMalloc(sizeof(IdentityInfo), 0); 14517fd14ceSopenharmony_ci if (info == NULL) { 14617fd14ceSopenharmony_ci LOGE("Failed to alloc memory for identity info!"); 14717fd14ceSopenharmony_ci return NULL; 14817fd14ceSopenharmony_ci } 14917fd14ceSopenharmony_ci info->protocolVec = CreateProtocolEntityVec(); 15017fd14ceSopenharmony_ci return info; 15117fd14ceSopenharmony_ci} 15217fd14ceSopenharmony_ci 15317fd14ceSopenharmony_civoid DestroyIdentityInfo(IdentityInfo *info) 15417fd14ceSopenharmony_ci{ 15517fd14ceSopenharmony_ci if (info == NULL) { 15617fd14ceSopenharmony_ci return; 15717fd14ceSopenharmony_ci } 15817fd14ceSopenharmony_ci 15917fd14ceSopenharmony_ci FreeBuffData(&info->proof.preSharedUrl); 16017fd14ceSopenharmony_ci FreeBuffData(&info->proof.certInfo.pkInfoStr); 16117fd14ceSopenharmony_ci FreeBuffData(&info->proof.certInfo.pkInfoSignature); 16217fd14ceSopenharmony_ci ClearProtocolEntityVec(&info->protocolVec); 16317fd14ceSopenharmony_ci 16417fd14ceSopenharmony_ci HcFree(info); 16517fd14ceSopenharmony_ci} 16617fd14ceSopenharmony_ci 16717fd14ceSopenharmony_civoid ClearIdentityInfoVec(IdentityInfoVec *vec) 16817fd14ceSopenharmony_ci{ 16917fd14ceSopenharmony_ci uint32_t index; 17017fd14ceSopenharmony_ci IdentityInfo **info; 17117fd14ceSopenharmony_ci FOR_EACH_HC_VECTOR(*vec, index, info) 17217fd14ceSopenharmony_ci { 17317fd14ceSopenharmony_ci DestroyIdentityInfo(*info); 17417fd14ceSopenharmony_ci } 17517fd14ceSopenharmony_ci DESTROY_HC_VECTOR(IdentityInfoVec, vec); 17617fd14ceSopenharmony_ci} 17717fd14ceSopenharmony_ci 17817fd14ceSopenharmony_civoid ClearProtocolEntityVec(ProtocolEntityVec *vec) 17917fd14ceSopenharmony_ci{ 18017fd14ceSopenharmony_ci uint32_t index; 18117fd14ceSopenharmony_ci ProtocolEntity **entity; 18217fd14ceSopenharmony_ci FOR_EACH_HC_VECTOR(*vec, index, entity) 18317fd14ceSopenharmony_ci { 18417fd14ceSopenharmony_ci HcFree(*entity); 18517fd14ceSopenharmony_ci } 18617fd14ceSopenharmony_ci DESTROY_HC_VECTOR(ProtocolEntityVec, vec); 18717fd14ceSopenharmony_ci} 18817fd14ceSopenharmony_ci 18917fd14ceSopenharmony_ci#endif