1 /*
2 * Copyright (C) 2022 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
16 #include "account_version_util.h"
17 #include "common_defs.h"
18 #include "hc_log.h"
19 #include "hc_types.h"
20 #include "iso_auth_task_common.h"
21 #include "pake_defs.h"
22 #include "pake_protocol_dl_common.h"
23 #include "pake_protocol_ec_common.h"
24 #include "pake_v2_auth_task_common.h"
25
26 #define IS_SUPPORT_CURVE_256 true
27
28 DECLARE_HC_VECTOR(AccountVersionInfoVec, void *)
29 IMPLEMENT_HC_VECTOR(AccountVersionInfoVec, void *, 1)
30
31 static AccountVersionInfoVec g_authVersionInfoVec;
32
33 static uint64_t g_authVersionNo = 0;
34
IsAuthPakeV2EcP256Supported(void)35 static bool IsAuthPakeV2EcP256Supported(void)
36 {
37 return IsPakeV2AuthTaskSupported() && (GetPakeEcAlg() == PAKE_ALG_EC) && IS_SUPPORT_CURVE_256;
38 }
39
IsAuthIsoSupported(void)40 static bool IsAuthIsoSupported(void)
41 {
42 return IsIsoAuthTaskSupported();
43 }
44
45 static AccountVersionInfo g_authVersionInfoAll[] = {
46 { AUTH_PAKE_V2_EC_P256, PAKE_V2, PAKE_ALG_EC, CURVE_256, false, IsAuthPakeV2EcP256Supported, CreatePakeV2AuthTask},
47 { AUTH_ISO, ISO, PAKE_ALG_NONE, CURVE_NONE, false, IsAuthIsoSupported, CreateIsoAuthTask }
48 };
49
InitVersionInfos(void)50 void InitVersionInfos(void)
51 {
52 g_authVersionInfoVec = CREATE_HC_VECTOR(AccountVersionInfoVec);
53 uint32_t size = sizeof(g_authVersionInfoAll) / sizeof(AccountVersionInfo);
54 for (uint32_t i = 0; i < size; i++) {
55 if (!g_authVersionInfoAll[i].isTaskSupported()) {
56 continue;
57 }
58 if (g_authVersionInfoVec.pushBackT(&g_authVersionInfoVec, (void *)(&g_authVersionInfoAll[i])) == NULL) {
59 LOGE("Failed to push g_authVersionInfoAll!");
60 continue;
61 }
62 g_authVersionNo |= g_authVersionInfoAll[i].versionNo;
63 }
64 }
65
DestroyVersionInfos(void)66 void DestroyVersionInfos(void)
67 {
68 DESTROY_HC_VECTOR(AccountVersionInfoVec, &g_authVersionInfoVec);
69 }
70
NegotiateForAuth(int32_t credentialType)71 static const AccountVersionInfo *NegotiateForAuth(int32_t credentialType)
72 {
73 uint64_t versionNo;
74 if (credentialType == SYMMETRIC_CRED) {
75 versionNo = AUTH_ISO;
76 } else if (credentialType == ASYMMETRIC_CRED) {
77 versionNo = AUTH_PAKE_V2_EC_P256;
78 } else {
79 LOGE("Invalid credential type for auth: %" LOG_PUB "d.", credentialType);
80 return NULL;
81 }
82 uint32_t index;
83 void **ptr = NULL;
84 FOR_EACH_HC_VECTOR(g_authVersionInfoVec, index, ptr) {
85 AccountVersionInfo *temp = (AccountVersionInfo *)(*ptr);
86 if ((temp->versionNo & versionNo) == versionNo) {
87 return temp;
88 }
89 }
90 LOGE("Version is not matched, failed to negotiate for account auth.");
91 return NULL;
92 }
93
GetNegotiatedVersionInfo(int32_t operationCode,int32_t credentialType)94 const AccountVersionInfo *GetNegotiatedVersionInfo(int32_t operationCode, int32_t credentialType)
95 {
96 // Now, only support auth negotiate.
97 if (operationCode != AUTHENTICATE) {
98 LOGE("operationCode is not auth, not supported.");
99 return NULL;
100 }
101 return NegotiateForAuth(credentialType);
102 }
103
GetSupportedVersionNo(int32_t operationCode)104 uint64_t GetSupportedVersionNo(int32_t operationCode)
105 {
106 (void)operationCode; // Now, only support auth negotiate.
107 return g_authVersionNo;
108 }
109