• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 "key_manager.h"
17 
18 #include "alg_defs.h"
19 #include "alg_loader.h"
20 #include "common_defs.h"
21 #include "device_auth_defines.h"
22 #include "hc_dev_info.h"
23 #include "hc_log.h"
24 #include "hc_types.h"
25 #include "json_utils.h"
26 #include "pseudonym_manager.h"
27 #include "uint8buff_utils.h"
28 
29 #define PAKE_X25519_KEY_PAIR_LEN 32
30 #define MK_LEN 32
31 #define MK_DERIVE_INFO "dev_auth_mk_derivation"
32 #define PAKE_KEY_ALIAS_LEN 64
33 #define MK_ALIAS_PREFIX "MK_"
34 #define PSEUDONYM_PSK_ALIAS_PREFIX "PSEUDONYM_"
35 
ConvertHashToAlias(const Uint8Buff * keyAliasHash,Uint8Buff * outKeyAlias)36 static int32_t ConvertHashToAlias(const Uint8Buff *keyAliasHash, Uint8Buff *outKeyAlias)
37 {
38     uint32_t keyAliasHexLen = keyAliasHash->length * BYTE_TO_HEX_OPER_LENGTH + 1;
39     char *keyAliasHex = (char *)HcMalloc(keyAliasHexLen, 0);
40     if (keyAliasHex == NULL) {
41         LOGE("Failed to alloc key alias hex memory!");
42         return HC_ERR_ALLOC_MEMORY;
43     }
44     int32_t res = ByteToHexString(keyAliasHash->val, keyAliasHash->length, keyAliasHex, keyAliasHexLen);
45     if (res != HC_SUCCESS) {
46         LOGE("Failed to convert key alias hash to hex!");
47         HcFree(keyAliasHex);
48         return res;
49     }
50     if (memcpy_s(outKeyAlias->val, outKeyAlias->length, keyAliasHex, HcStrlen(keyAliasHex)) != EOK) {
51         LOGE("Failed to copy key alias hex!");
52         HcFree(keyAliasHex);
53         return HC_ERR_MEMORY_COPY;
54     }
55     HcFree(keyAliasHex);
56     return HC_SUCCESS;
57 }
58 
ConvertHashToAliasWithPrefix(const char * prefix,const Uint8Buff * keyAliasHash,Uint8Buff * keyAlias)59 static int32_t ConvertHashToAliasWithPrefix(const char *prefix, const Uint8Buff *keyAliasHash, Uint8Buff *keyAlias)
60 {
61     uint32_t keyAliasHexLen = keyAliasHash->length * BYTE_TO_HEX_OPER_LENGTH + 1;
62     char *keyAliasHex = (char *)HcMalloc(keyAliasHexLen, 0);
63     if (keyAliasHex == NULL) {
64         LOGE("Failed to alloc key alias hex memory!");
65         return HC_ERR_ALLOC_MEMORY;
66     }
67     int32_t res = ByteToHexString(keyAliasHash->val, keyAliasHash->length, keyAliasHex, keyAliasHexLen);
68     if (res != HC_SUCCESS) {
69         LOGE("Failed to convert key alias hash to hex!");
70         HcFree(keyAliasHex);
71         return res;
72     }
73     uint32_t prefixLen = HcStrlen(prefix);
74     if (memcpy_s(keyAlias->val, keyAlias->length, prefix, prefixLen) != EOK) {
75         LOGE("Failed to copy key alias prefix!");
76         HcFree(keyAliasHex);
77         return HC_ERR_MEMORY_COPY;
78     }
79     // The remaining key alias len is less than keyAliasHexLen len after substract prefixLen,
80     // so copy the remaining len other than keyAliasHexLen in order that the key alias len is 64.
81     if (memcpy_s(keyAlias->val + prefixLen, keyAlias->length - prefixLen, keyAliasHex,
82         keyAlias->length - prefixLen) != EOK) {
83         LOGE("Failed to copy key alias hex!");
84         HcFree(keyAliasHex);
85         return HC_ERR_MEMORY_COPY;
86     }
87     HcFree(keyAliasHex);
88     return HC_SUCCESS;
89 }
90 
GenerateDevKeyAlias(Uint8Buff * outKeyAlias)91 static int32_t GenerateDevKeyAlias(Uint8Buff *outKeyAlias)
92 {
93     if (outKeyAlias == NULL) {
94         LOGE("Input key alias is null!");
95         return HC_ERR_INVALID_PARAMS;
96     }
97     char selfUdid[INPUT_UDID_LEN] = { 0 };
98     int32_t res = HcGetUdid((uint8_t *)selfUdid, INPUT_UDID_LEN);
99     if (res != HC_SUCCESS) {
100         LOGE("Failed to get local udid!");
101         return res;
102     }
103     uint8_t hashValue[SHA256_LEN] = { 0 };
104     Uint8Buff keyAliasHash = { hashValue, SHA256_LEN };
105     Uint8Buff msgBuff = { (uint8_t *)selfUdid, HcStrlen(selfUdid) };
106     res = GetLoaderInstance()->sha256(&msgBuff, &keyAliasHash);
107     if (res != HC_SUCCESS) {
108         LOGE("Failed to generate key alias hash!");
109         return res;
110     }
111     res = ConvertHashToAlias(&keyAliasHash, outKeyAlias);
112     if (res != HC_SUCCESS) {
113         LOGE("Failed to convert hash to alias!");
114     }
115     return res;
116 }
117 
GenerateMkAlias(const char * peerDeviceId,Uint8Buff * keyAlias)118 static int32_t GenerateMkAlias(const char *peerDeviceId, Uint8Buff *keyAlias)
119 {
120     Uint8Buff peerDevIdBuff = { (uint8_t *)peerDeviceId, HcStrlen(peerDeviceId) };
121     uint8_t hashValue[SHA256_LEN] = { 0 };
122     Uint8Buff keyAliasHash = { hashValue, SHA256_LEN };
123     int32_t res = GetLoaderInstance()->sha256(&peerDevIdBuff, &keyAliasHash);
124     if (res != HC_SUCCESS) {
125         LOGE("Failed to generate key alias hash!");
126         return res;
127     }
128     res = ConvertHashToAliasWithPrefix(MK_ALIAS_PREFIX, &keyAliasHash, keyAlias);
129     if (res != HC_SUCCESS) {
130         LOGE("Failed to convert hash to alias!");
131     }
132     return res;
133 }
134 
GeneratePseudonymPskAlias(const char * peerDeviceId,Uint8Buff * keyAlias)135 static int32_t GeneratePseudonymPskAlias(const char *peerDeviceId, Uint8Buff *keyAlias)
136 {
137     uint8_t hashValue[SHA256_LEN] = { 0 };
138     Uint8Buff keyAliasHash = { hashValue, SHA256_LEN };
139     Uint8Buff peerDevIdBuff = { (uint8_t *)peerDeviceId, HcStrlen(peerDeviceId) };
140     int32_t res = GetLoaderInstance()->sha256(&peerDevIdBuff, &keyAliasHash);
141     if (res != HC_SUCCESS) {
142         LOGE("Failed to generate key alias hash!");
143         return res;
144     }
145 
146     res = ConvertHashToAliasWithPrefix(PSEUDONYM_PSK_ALIAS_PREFIX, &keyAliasHash, keyAlias);
147     if (res != HC_SUCCESS) {
148         LOGE("Failed to convert hash to alias!");
149     }
150     return res;
151 }
152 
KeyDerivation(const Uint8Buff * baseAlias,const Uint8Buff * salt,bool isAlias,Uint8Buff * returnKey)153 static int32_t KeyDerivation(const Uint8Buff *baseAlias, const Uint8Buff *salt, bool isAlias,
154     Uint8Buff *returnKey)
155 {
156     Uint8Buff keyInfo = { (uint8_t *)MK_DERIVE_INFO, HcStrlen(MK_DERIVE_INFO) };
157     int32_t res = GetLoaderInstance()->computeHkdf(baseAlias, salt, &keyInfo, returnKey, isAlias);
158     if (res != HC_SUCCESS) {
159         LOGE("Failed to compute hkdf!");
160     }
161     return res;
162 }
163 
GenerateDeviceKeyPair(void)164 int32_t GenerateDeviceKeyPair(void)
165 {
166     uint8_t keyAlias[PAKE_KEY_ALIAS_LEN] = { 0 };
167     Uint8Buff keyAliasBuff = { keyAlias, PAKE_KEY_ALIAS_LEN };
168     int32_t res = GenerateDevKeyAlias(&keyAliasBuff);
169     if (res != HC_SUCCESS) {
170         LOGE("Failed to generate device key alias!");
171         return res;
172     }
173     if (GetLoaderInstance()->checkKeyExist(&keyAliasBuff) == HC_SUCCESS) {
174         LOGI("Device Key pair already exists!");
175         return HC_SUCCESS;
176     }
177 
178     char selfUdid[INPUT_UDID_LEN] = { 0 };
179     res = HcGetUdid((uint8_t *)selfUdid, INPUT_UDID_LEN);
180     if (res != HC_SUCCESS) {
181         LOGE("Failed to get local udid!");
182         return res;
183     }
184     Uint8Buff authIdBuff = { (uint8_t *)selfUdid, HcStrlen(selfUdid) };
185     ExtraInfo exInfo = { authIdBuff, -1, -1 };
186     res = GetLoaderInstance()->generateKeyPairWithStorage(&keyAliasBuff, PAKE_X25519_KEY_PAIR_LEN, X25519,
187         KEY_PURPOSE_SIGN_VERIFY, &exInfo);
188     if (res != HC_SUCCESS) {
189         LOGE("Failed to generate device key pair!");
190         return res;
191     }
192     LOGI("Generate device key pair successfully!");
193     return HC_SUCCESS;
194 }
195 
GenerateMk(const char * peerDeviceId,const Uint8Buff * peerPubKey)196 int32_t GenerateMk(const char *peerDeviceId, const Uint8Buff *peerPubKey)
197 {
198     if (peerDeviceId == NULL || peerPubKey == NULL) {
199         LOGE("Invalid input params!");
200         return HC_ERR_INVALID_PARAMS;
201     }
202     uint8_t mkAlias[PAKE_KEY_ALIAS_LEN] = { 0 };
203     Uint8Buff mkAliasBuff = { mkAlias, PAKE_KEY_ALIAS_LEN };
204     int32_t res = GenerateMkAlias(peerDeviceId, &mkAliasBuff);
205     if (res != HC_SUCCESS) {
206         LOGE("Failed to generate mk alias!");
207         return res;
208     }
209     uint8_t devKeyAlias[PAKE_KEY_ALIAS_LEN] = { 0 };
210     Uint8Buff devKeyAliasBuff = { devKeyAlias, PAKE_KEY_ALIAS_LEN };
211     res = GenerateDevKeyAlias(&devKeyAliasBuff);
212     if (res != HC_SUCCESS) {
213         LOGE("Failed to generate device key alias!");
214         return res;
215     }
216     KeyBuff selfKeyAliasBuff = { devKeyAliasBuff.val, devKeyAliasBuff.length, true };
217     KeyBuff peerPubKeyBuff = { peerPubKey->val, peerPubKey->length, false };
218     res = GetLoaderInstance()->agreeSharedSecretWithStorage(&selfKeyAliasBuff, &peerPubKeyBuff, X25519,
219         MK_LEN, &mkAliasBuff);
220     if (res != HC_SUCCESS) {
221         LOGE("Failed to agree sharedSecret!");
222         return res;
223     }
224     LOGI("Generate mk successfully!");
225     return HC_SUCCESS;
226 }
227 
DeleteMk(const char * peerDeviceId)228 int32_t DeleteMk(const char *peerDeviceId)
229 {
230     if (peerDeviceId == NULL) {
231         LOGE("Invalid input param!");
232         return HC_ERR_INVALID_PARAMS;
233     }
234     uint8_t mkAlias[PAKE_KEY_ALIAS_LEN] = { 0 };
235     Uint8Buff mkAliasBuff = { mkAlias, PAKE_KEY_ALIAS_LEN };
236     int32_t res = GenerateMkAlias(peerDeviceId, &mkAliasBuff);
237     if (res != HC_SUCCESS) {
238         LOGE("Failed to generate mk alias!");
239         return res;
240     }
241     if (GetLoaderInstance()->checkKeyExist(&mkAliasBuff) != HC_SUCCESS) {
242         LOGI("mk does not exist, no need to delete!");
243         return HC_SUCCESS;
244     }
245     res = GetLoaderInstance()->deleteKey(&mkAliasBuff);
246     if (res != HC_SUCCESS) {
247         LOGE("Failed to delete mk!");
248         return res;
249     }
250     LOGI("Delete mk successfully!");
251     return HC_SUCCESS;
252 }
253 
GeneratePseudonymPsk(const char * peerDeviceId,const Uint8Buff * salt)254 int32_t GeneratePseudonymPsk(const char *peerDeviceId, const Uint8Buff *salt)
255 {
256     if (peerDeviceId == NULL || salt == NULL) {
257         LOGE("Invalid input params!");
258         return HC_ERR_INVALID_PARAMS;
259     }
260     uint8_t pseudonymPskAlias[PAKE_KEY_ALIAS_LEN] = { 0 };
261     Uint8Buff pskAliasBuff = { pseudonymPskAlias, PAKE_KEY_ALIAS_LEN };
262     int32_t res = GeneratePseudonymPskAlias(peerDeviceId, &pskAliasBuff);
263     if (res != HC_SUCCESS) {
264         LOGE("Failed to generate pseudonym psk alias!");
265         return res;
266     }
267     uint8_t mkAlias[PAKE_KEY_ALIAS_LEN] = { 0 };
268     Uint8Buff mkAliasBuff = { mkAlias, PAKE_KEY_ALIAS_LEN };
269     res = GenerateMkAlias(peerDeviceId, &mkAliasBuff);
270     if (res != HC_SUCCESS) {
271         LOGE("Failed to generate mk alias!");
272         return res;
273     }
274     Uint8Buff pskBuff = { NULL, 0 };
275     if (InitUint8Buff(&pskBuff, MK_LEN) != HC_SUCCESS) {
276         LOGE("Failed to init pseudonym psk!");
277         return HC_ERR_ALLOC_MEMORY;
278     }
279     res = KeyDerivation(&mkAliasBuff, salt, true, &pskBuff);
280     if (res != HC_SUCCESS) {
281         LOGE("Failed to derive pseudonym psk!");
282         FreeUint8Buff(&pskBuff);
283         return res;
284     }
285     res = GetLoaderInstance()->importSymmetricKey(&pskAliasBuff, &pskBuff, KEY_PURPOSE_MAC, NULL);
286     ClearFreeUint8Buff(&pskBuff);
287     if (res != HC_SUCCESS) {
288         LOGE("Failed to import pseudonym psk!");
289         return res;
290     }
291     LOGI("Generate and save pseudonym psk successfully!");
292     return HC_SUCCESS;
293 }
294 
DeletePseudonymPsk(const char * peerDeviceId)295 int32_t DeletePseudonymPsk(const char *peerDeviceId)
296 {
297     if (peerDeviceId == NULL) {
298         LOGE("Invalid input param!");
299         return HC_ERR_INVALID_PARAMS;
300     }
301     uint8_t pseudonymPskAlias[PAKE_KEY_ALIAS_LEN] = { 0 };
302     Uint8Buff pskAliasBuff = { pseudonymPskAlias, PAKE_KEY_ALIAS_LEN };
303     int32_t res = GeneratePseudonymPskAlias(peerDeviceId, &pskAliasBuff);
304     if (res != HC_SUCCESS) {
305         LOGE("Failed to generate psk alias!");
306         return res;
307     }
308     if (GetLoaderInstance()->checkKeyExist(&pskAliasBuff) != HC_SUCCESS) {
309         LOGI("Pseudonym psk does not exist, no need to delete!");
310         return HC_SUCCESS;
311     }
312     res = GetLoaderInstance()->deleteKey(&pskAliasBuff);
313     if (res != HC_SUCCESS) {
314         LOGE("Failed to delete pseudonym psk!");
315         return res;
316     }
317     LOGI("Delete pseudonym psk successfully!");
318     return HC_SUCCESS;
319 }
320 
GenerateAndSavePseudonymId(int32_t osAccountId,const char * peerDeviceId,const PseudonymKeyInfo * info,const Uint8Buff * saltBuff,Uint8Buff * returnHmac)321 int32_t GenerateAndSavePseudonymId(int32_t osAccountId, const char *peerDeviceId, const PseudonymKeyInfo *info,
322     const Uint8Buff *saltBuff, Uint8Buff *returnHmac)
323 {
324     if (peerDeviceId == NULL || info == NULL || saltBuff == NULL || returnHmac == NULL) {
325         LOGE("Invalid input params!");
326         return HC_ERR_INVALID_PARAMS;
327     }
328     uint8_t pskAliasVal[PAKE_KEY_ALIAS_LEN] = { 0 };
329     Uint8Buff pskAliasBuff = { pskAliasVal, PAKE_KEY_ALIAS_LEN };
330     int32_t res = GeneratePseudonymPskAlias(peerDeviceId, &pskAliasBuff);
331     if (res != HC_SUCCESS) {
332         LOGE("Failed to generate pseudonym psk alias!");
333         return res;
334     }
335     uint8_t pseudonymIdVal[MK_LEN] = { 0 };
336     Uint8Buff pseudonymIdBuff = { pseudonymIdVal, MK_LEN };
337     res = GetLoaderInstance()->computeHmac(&pskAliasBuff, saltBuff, &pseudonymIdBuff, true);
338     if (res != HC_SUCCESS) {
339         LOGE("Failed to compute hmac!");
340         return res;
341     }
342     if (DeepCopyUint8Buff(&pseudonymIdBuff, returnHmac) != HC_SUCCESS) {
343         LOGE("Failed to copy hmac!");
344         return HC_ERR_ALLOC_MEMORY;
345     }
346     uint32_t pdidLen = pseudonymIdBuff.length * BYTE_TO_HEX_OPER_LENGTH + 1;
347     char *pdid = (char *)HcMalloc(pdidLen, 0);
348     if (pdid == NULL) {
349         LOGE("Failed to alloc memory for pdid!");
350         ClearFreeUint8Buff(returnHmac);
351         return HC_ERR_ALLOC_MEMORY;
352     }
353     res = ByteToHexString(pseudonymIdBuff.val, pseudonymIdBuff.length, pdid, pdidLen);
354     if (res != HC_SUCCESS) {
355         LOGE("Failed to convert pdid from byte to hex string!");
356         ClearFreeUint8Buff(returnHmac);
357         HcFree(pdid);
358         return res;
359     }
360     res = GetPseudonymInstance()->savePseudonymId(osAccountId, pdid, info->peerInfo, peerDeviceId, info->pdidIndex);
361     HcFree(pdid);
362     if (res != HC_SUCCESS) {
363         LOGE("Failed to save pdid!");
364         ClearFreeUint8Buff(returnHmac);
365         return res;
366     }
367     LOGI("Generate and save pdid successfully!");
368     return HC_SUCCESS;
369 }
370 
GetDevicePubKey(Uint8Buff * devicePk)371 int32_t GetDevicePubKey(Uint8Buff *devicePk)
372 {
373     if (devicePk == NULL) {
374         LOGE("Invalid input param!");
375         return HC_ERR_INVALID_PARAMS;
376     }
377     uint8_t keyAlias[PAKE_KEY_ALIAS_LEN] = { 0 };
378     Uint8Buff keyAliasBuff = { keyAlias, PAKE_KEY_ALIAS_LEN };
379     int32_t res = GenerateDevKeyAlias(&keyAliasBuff);
380     if (res != HC_SUCCESS) {
381         LOGE("Failed to generate device key alias!");
382         return res;
383     }
384     res = GetLoaderInstance()->exportPublicKey(&keyAliasBuff, devicePk);
385     if (res != HC_SUCCESS) {
386         LOGE("Failed to export device pk!");
387     }
388     return res;
389 }