• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-2024 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 "dh_asy_key_generator_openssl.h"
17 #include <string.h>
18 
19 #include "dh_openssl_common.h"
20 #include "detailed_dh_key_params.h"
21 #include "log.h"
22 #include "memory.h"
23 #include "openssl_adapter.h"
24 #include "openssl_class.h"
25 #include "openssl_common.h"
26 
27 #define OPENSSL_DH_GENERATOR_CLASS "OPENSSL.DH.KEYGENERATOR"
28 #define OPENSSL_DH_PUBKEY_FORMAT "X.509"
29 #define OPENSSL_DH_PRIKEY_FORMAT "PKCS#8"
30 #define ALGORITHM_NAME_DH "DH"
31 #define PARAMS_NUM_TWO 2
32 #define BIT8 8
33 
34 typedef struct {
35     HcfAsyKeyGeneratorSpi base;
36 
37     int32_t pBits;
38 } HcfAsyKeyGeneratorSpiDhOpensslImpl;
39 
FreeCtx(EVP_PKEY_CTX * paramsCtx,EVP_PKEY * paramsPkey,EVP_PKEY_CTX * pkeyCtx)40 static void FreeCtx(EVP_PKEY_CTX *paramsCtx, EVP_PKEY *paramsPkey, EVP_PKEY_CTX *pkeyCtx)
41 {
42     if (paramsCtx != NULL) {
43         OpensslEvpPkeyCtxFree(paramsCtx);
44     }
45     if (paramsPkey != NULL) {
46         OpensslEvpPkeyFree(paramsPkey);
47     }
48     if (pkeyCtx != NULL) {
49         OpensslEvpPkeyCtxFree(pkeyCtx);
50     }
51 }
52 
FreeCommSpecBn(BIGNUM * p,BIGNUM * g)53 static void FreeCommSpecBn(BIGNUM *p, BIGNUM *g)
54 {
55     if (p != NULL) {
56         OpensslBnFree(p);
57     }
58     if (g != NULL) {
59         OpensslBnFree(g);
60     }
61 }
62 
GetDhKeyGeneratorSpiClass(void)63 static const char *GetDhKeyGeneratorSpiClass(void)
64 {
65     return OPENSSL_DH_GENERATOR_CLASS;
66 }
67 
GetDhKeyPairClass(void)68 static const char *GetDhKeyPairClass(void)
69 {
70     return OPENSSL_DH_KEYPAIR_CLASS;
71 }
72 
GetDhPubKeyClass(void)73 static const char *GetDhPubKeyClass(void)
74 {
75     return OPENSSL_DH_PUBKEY_CLASS;
76 }
77 
GetDhPriKeyClass(void)78 static const char *GetDhPriKeyClass(void)
79 {
80     return OPENSSL_DH_PRIKEY_CLASS;
81 }
82 
DestroyDhKeyGeneratorSpiImpl(HcfObjectBase * self)83 static void DestroyDhKeyGeneratorSpiImpl(HcfObjectBase *self)
84 {
85     if (self == NULL) {
86         LOGE("Class is null.");
87         return;
88     }
89     if (!HcfIsClassMatch(self, GetDhKeyGeneratorSpiClass())) {
90         LOGE("Class not match.");
91         return;
92     }
93     HcfFree(self);
94 }
95 
DestroyDhPubKey(HcfObjectBase * self)96 static void DestroyDhPubKey(HcfObjectBase *self)
97 {
98     if (self == NULL) {
99         LOGE("Class is null.");
100         return;
101     }
102     if (!HcfIsClassMatch(self, GetDhPubKeyClass())) {
103         LOGE("Class not match.");
104         return;
105     }
106     HcfOpensslDhPubKey *impl = (HcfOpensslDhPubKey *)self;
107     OpensslDhFree(impl->pk);
108     impl->pk = NULL;
109     HcfFree(impl);
110 }
111 
DestroyDhPriKey(HcfObjectBase * self)112 static void DestroyDhPriKey(HcfObjectBase *self)
113 {
114     if (self == NULL) {
115         LOGE("Class is null.");
116         return;
117     }
118     if (!HcfIsClassMatch(self, GetDhPriKeyClass())) {
119         LOGE("Class not match.");
120         return;
121     }
122     HcfOpensslDhPriKey *impl = (HcfOpensslDhPriKey *)self;
123     OpensslDhFree(impl->sk);
124     impl->sk = NULL;
125     HcfFree(impl);
126 }
127 
DestroyDhKeyPair(HcfObjectBase * self)128 static void DestroyDhKeyPair(HcfObjectBase *self)
129 {
130     if (self == NULL) {
131         LOGE("Class is null.");
132         return;
133     }
134     if (!HcfIsClassMatch(self, GetDhKeyPairClass())) {
135         LOGE("Class not match.");
136         return;
137     }
138     HcfOpensslDhKeyPair *impl = (HcfOpensslDhKeyPair *)self;
139     DestroyDhPubKey((HcfObjectBase *)impl->base.pubKey);
140     impl->base.pubKey = NULL;
141     DestroyDhPriKey((HcfObjectBase *)impl->base.priKey);
142     impl->base.priKey = NULL;
143     HcfFree(self);
144 }
145 
GetDhPubKeyAlgorithm(HcfKey * self)146 static const char *GetDhPubKeyAlgorithm(HcfKey *self)
147 {
148     if (self == NULL) {
149         LOGE("Invalid input parameter.");
150         return NULL;
151     }
152     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhPubKeyClass())) {
153         LOGE("Class not match.");
154         return NULL;
155     }
156     return ALGORITHM_NAME_DH;
157 }
158 
GetDhPriKeyAlgorithm(HcfKey * self)159 static const char *GetDhPriKeyAlgorithm(HcfKey *self)
160 {
161     if (self == NULL) {
162         LOGE("Invalid input parameter.");
163         return NULL;
164     }
165     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) {
166         LOGE("Class not match.");
167         return NULL;
168     }
169     return ALGORITHM_NAME_DH;
170 }
171 
GetDhPubKeyEncoded(HcfKey * self,HcfBlob * returnBlob)172 static HcfResult GetDhPubKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
173 {
174     if ((self == NULL) || (returnBlob == NULL)) {
175         LOGE("Invalid input parameter.");
176         return HCF_INVALID_PARAMS;
177     }
178     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhPubKeyClass())) {
179         LOGE("Class not match.");
180         return HCF_INVALID_PARAMS;
181     }
182     HcfOpensslDhPubKey *impl = (HcfOpensslDhPubKey *)self;
183     unsigned char *returnData = NULL;
184     EVP_PKEY *pKey = NewEvpPkeyByDh(impl->pk, true);
185     if (pKey == NULL) {
186         LOGD("[error] New pKey by dh fail.");
187         return HCF_ERR_CRYPTO_OPERATION;
188     }
189     int len = OpensslI2dPubKey(pKey, &returnData);
190     if (len <= 0) {
191         LOGD("[error] Call i2d_PUBKEY failed");
192         HcfPrintOpensslError();
193         OpensslEvpPkeyFree(pKey);
194         return HCF_ERR_CRYPTO_OPERATION;
195     }
196     returnBlob->data = returnData;
197     returnBlob->len = len;
198     OpensslEvpPkeyFree(pKey);
199     return HCF_SUCCESS;
200 }
201 
GetDhPubKeyEncodedPem(HcfKey * self,const char * format,char ** returnString)202 static HcfResult GetDhPubKeyEncodedPem(HcfKey *self, const char *format, char **returnString)
203 {
204     (void)self;
205     (void)format;
206     (void)returnString;
207     return HCF_INVALID_PARAMS;
208 }
209 
GetDhPriKeyEncoded(HcfKey * self,HcfBlob * returnBlob)210 static HcfResult GetDhPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
211 {
212     if ((self == NULL) || (returnBlob == NULL)) {
213         LOGE("Invalid input parameter.");
214         return HCF_INVALID_PARAMS;
215     }
216     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) {
217         LOGE("Class not match.");
218         return HCF_INVALID_PARAMS;
219     }
220     HcfOpensslDhPriKey *impl = (HcfOpensslDhPriKey *)self;
221     unsigned char *returnData = NULL;
222     EVP_PKEY *pKey = NewEvpPkeyByDh(impl->sk, true);
223     if (pKey == NULL) {
224         LOGD("[error] New pKey by dh fail.");
225         HcfPrintOpensslError();
226         return HCF_ERR_CRYPTO_OPERATION;
227     }
228     int len = OpensslI2dPrivateKey(pKey, &returnData);
229     if (len <= 0) {
230         LOGD("[error] Call i2d_PrivateKey failed.");
231         HcfPrintOpensslError();
232         OpensslEvpPkeyFree(pKey);
233         return HCF_ERR_CRYPTO_OPERATION;
234     }
235     returnBlob->data = returnData;
236     returnBlob->len = len;
237     OpensslEvpPkeyFree(pKey);
238     return HCF_SUCCESS;
239 }
240 
GetDhPriKeyEncodedPem(const HcfPriKey * self,HcfParamsSpec * paramsSpec,const char * format,char ** returnString)241 static HcfResult GetDhPriKeyEncodedPem(const HcfPriKey *self, HcfParamsSpec *paramsSpec, const char *format,
242     char **returnString)
243 {
244     (void)self;
245     (void)paramsSpec;
246     (void)format;
247     (void)returnString;
248     return HCF_INVALID_PARAMS;
249 }
250 
GetDhPubKeyFormat(HcfKey * self)251 static const char *GetDhPubKeyFormat(HcfKey *self)
252 {
253     if (self == NULL) {
254         LOGE("Invalid input parameter.");
255         return NULL;
256     }
257     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhPubKeyClass())) {
258         LOGE("Class not match.");
259         return NULL;
260     }
261     return OPENSSL_DH_PUBKEY_FORMAT;
262 }
263 
GetDhPriKeyFormat(HcfKey * self)264 static const char *GetDhPriKeyFormat(HcfKey *self)
265 {
266     if (self == NULL) {
267         LOGE("Invalid input parameter.");
268         return NULL;
269     }
270     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) {
271         LOGE("Class not match.");
272         return NULL;
273     }
274     return OPENSSL_DH_PRIKEY_FORMAT;
275 }
276 
GetBigIntegerSpec(const HcfPubKey * pubSelf,const HcfPriKey * priSelf,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)277 static HcfResult GetBigIntegerSpec(const HcfPubKey *pubSelf, const HcfPriKey *priSelf, const AsyKeySpecItem item,
278     HcfBigInteger *returnBigInteger)
279 {
280     DH *dh = NULL;
281     if (pubSelf != NULL) {
282         if (item == DH_SK_BN) {
283             LOGE("Invalid item.");
284             return HCF_INVALID_PARAMS;
285         }
286         HcfOpensslDhPubKey *impl = (HcfOpensslDhPubKey *)pubSelf;
287         dh = impl->pk;
288     } else {
289         if (item == DH_PK_BN) {
290             LOGE("Invalid item.");
291             return HCF_INVALID_PARAMS;
292         }
293         HcfOpensslDhPriKey *impl = (HcfOpensslDhPriKey *)priSelf;
294         dh = impl->sk;
295     }
296     if (dh == NULL) {
297         LOGE("Dh is null.");
298         return HCF_INVALID_PARAMS;
299     }
300     HcfResult ret = HCF_SUCCESS;
301     switch (item) {
302         case DH_P_BN:
303             ret = BigNumToBigInteger(OpensslDhGet0P(dh), returnBigInteger);
304             break;
305         case DH_G_BN:
306             ret = BigNumToBigInteger(OpensslDhGet0G(dh), returnBigInteger);
307             break;
308         case DH_PK_BN:
309             ret = BigNumToBigInteger(OpensslDhGet0PubKey(dh), returnBigInteger);
310             break;
311         case DH_SK_BN:
312             ret = BigNumToBigInteger(OpensslDhGet0PrivKey(dh), returnBigInteger);
313             break;
314         default:
315             LOGE("Input item [%{public}d] is invalid", item);
316             ret = HCF_INVALID_PARAMS;
317             break;
318     }
319     return ret;
320 }
321 
GetBigIntegerSpecFromDhPubKey(const HcfPubKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)322 static HcfResult GetBigIntegerSpecFromDhPubKey(const HcfPubKey *self, const AsyKeySpecItem item,
323     HcfBigInteger *returnBigInteger)
324 {
325     if (self ==  NULL || returnBigInteger == NULL) {
326         LOGE("Invalid input parameter.");
327         return HCF_INVALID_PARAMS;
328     }
329     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhPubKeyClass())) {
330         LOGE("Invalid class of self.");
331         return HCF_INVALID_PARAMS;
332     }
333     HcfResult ret = GetBigIntegerSpec(self, NULL, item, returnBigInteger);
334     if (ret != HCF_SUCCESS) {
335         LOGE("Get big integer failed.");
336     }
337     return ret;
338 }
339 
GetBigIntegerSpecFromDhPriKey(const HcfPriKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)340 static HcfResult GetBigIntegerSpecFromDhPriKey(const HcfPriKey *self, const AsyKeySpecItem item,
341     HcfBigInteger *returnBigInteger)
342 {
343     if (self ==  NULL || returnBigInteger == NULL) {
344         LOGE("Invalid input parameter.");
345         return HCF_INVALID_PARAMS;
346     }
347     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) {
348         LOGE("Invalid class of self.");
349         return HCF_INVALID_PARAMS;
350     }
351     HcfResult ret = GetBigIntegerSpec(NULL, self, item, returnBigInteger);
352     if (ret != HCF_SUCCESS) {
353         LOGE("Get big integer failed.");
354     }
355     return ret;
356 }
357 
GetIntSpecFromDhPubKey(const HcfPubKey * self,const AsyKeySpecItem item,int * returnInt)358 static HcfResult GetIntSpecFromDhPubKey(const HcfPubKey *self, const AsyKeySpecItem item, int *returnInt)
359 {
360     (void)self;
361     (void)returnInt;
362     return HCF_NOT_SUPPORT;
363 }
364 
GetIntSpecFromDhPriKey(const HcfPriKey * self,const AsyKeySpecItem item,int * returnInt)365 static HcfResult GetIntSpecFromDhPriKey(const HcfPriKey *self, const AsyKeySpecItem item, int *returnInt)
366 {
367     if (self ==  NULL || returnInt == NULL) {
368         LOGE("Invalid input parameter.");
369         return HCF_INVALID_PARAMS;
370     }
371     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) {
372         LOGE("Invalid class of self.");
373         return HCF_INVALID_PARAMS;
374     }
375     if (item != DH_L_NUM) {
376         LOGE("Invalid input item.");
377         return HCF_INVALID_PARAMS;
378     }
379     HcfOpensslDhPriKey *impl = (HcfOpensslDhPriKey *)self;
380     DH *dh = impl->sk;
381     if (dh == NULL) {
382         LOGE("Dh is null.");
383         return HCF_INVALID_PARAMS;
384     }
385 
386     *returnInt = (int)OpensslDhGetLength(dh);
387     return HCF_SUCCESS;
388 }
389 
GetStrSpecFromDhPubKey(const HcfPubKey * self,const AsyKeySpecItem item,char ** returnString)390 static HcfResult GetStrSpecFromDhPubKey(const HcfPubKey *self, const AsyKeySpecItem item, char **returnString)
391 {
392     (void)self;
393     (void)returnString;
394     return HCF_NOT_SUPPORT;
395 }
396 
GetStrSpecFromDhPriKey(const HcfPriKey * self,const AsyKeySpecItem item,char ** returnString)397 static HcfResult GetStrSpecFromDhPriKey(const HcfPriKey *self, const AsyKeySpecItem item, char **returnString)
398 {
399     (void)self;
400     (void)returnString;
401     return HCF_NOT_SUPPORT;
402 }
403 
GetDhPriKeyEncodedDer(const HcfPriKey * self,const char * format,HcfBlob * returnBlob)404 static HcfResult GetDhPriKeyEncodedDer(const HcfPriKey *self, const char *format, HcfBlob *returnBlob)
405 {
406     (void)self;
407     (void)format;
408     (void)returnBlob;
409     return HCF_INVALID_PARAMS;
410 }
411 
ClearDhPriKeyMem(HcfPriKey * self)412 static void ClearDhPriKeyMem(HcfPriKey *self)
413 {
414     if (self == NULL) {
415         LOGE("Class is null.");
416         return;
417     }
418     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhPriKeyClass())) {
419         LOGE("Class not match.");
420         return;
421     }
422     HcfOpensslDhPriKey *impl = (HcfOpensslDhPriKey *)self;
423     OpensslDhFree(impl->sk);
424     impl->sk = NULL;
425 }
426 
ConstructDhOsslParamsAndGenPkey(int32_t dhId,EVP_PKEY_CTX * paramsCtx)427 static EVP_PKEY *ConstructDhOsslParamsAndGenPkey(int32_t dhId, EVP_PKEY_CTX *paramsCtx)
428 {
429     EVP_PKEY *paramsPkey = NULL;
430     OSSL_PARAM params[PARAMS_NUM_TWO];
431     char *nidName = GetNidNameByDhId(dhId);
432     if (nidName == NULL) {
433         LOGE("Get nid name failed.");
434         return NULL;
435     }
436     params[0] = OpensslOsslParamConstructUtf8String("group", nidName, 0);
437     params[1] = OpensslOsslParamConstructEnd();
438     if (OpensslEvpPkeyKeyGenInit(paramsCtx) != HCF_OPENSSL_SUCCESS) {
439         LOGD("[error] ParamsCtx generate init failed.");
440         return NULL;
441     }
442     if (OpensslEvpPkeyCtxSetParams(paramsCtx, params) != HCF_OPENSSL_SUCCESS) {
443         LOGD("[error] ParamsCtx set failed.");
444         return NULL;
445     }
446     if (OpensslEvpPkeyGenerate(paramsCtx, &paramsPkey) != HCF_OPENSSL_SUCCESS) {
447         LOGD("[error] Create generate failed.");
448         return NULL;
449     }
450     return paramsPkey;
451 }
452 
GenerateDhEvpKey(int32_t dhId,EVP_PKEY ** ppkey)453 static HcfResult GenerateDhEvpKey(int32_t dhId, EVP_PKEY **ppkey)
454 {
455     HcfResult ret = HCF_SUCCESS;
456     EVP_PKEY *paramsPkey = NULL;
457     EVP_PKEY_CTX *pkeyCtx = NULL;
458     EVP_PKEY_CTX *paramsCtx = NULL;
459 
460     do {
461         paramsCtx = OpensslEvpPkeyCtxNewFromName(NULL, "DH", NULL);
462         if (paramsCtx == NULL) {
463             LOGD("[error] New paramsCtx from name failed.");
464             ret = HCF_ERR_CRYPTO_OPERATION;
465             break;
466         }
467         paramsPkey = ConstructDhOsslParamsAndGenPkey(dhId, paramsCtx);
468         if (paramsPkey == NULL) {
469             LOGD("[error] Construct dh params and generate pkey failed.");
470             ret = HCF_ERR_CRYPTO_OPERATION;
471             break;
472         }
473         pkeyCtx = OpensslEvpPkeyCtxNew(paramsPkey, NULL);
474         if (pkeyCtx == NULL) {
475             LOGD("[error] Create pkey ctx failed.");
476             ret = HCF_ERR_CRYPTO_OPERATION;
477             break;
478         }
479         if (OpensslEvpPkeyKeyGenInit(pkeyCtx) != HCF_OPENSSL_SUCCESS) {
480             LOGD("[error] Key ctx generate init failed.");
481             ret = HCF_ERR_CRYPTO_OPERATION;
482             break;
483         }
484         if (OpensslEvpPkeyKeyGen(pkeyCtx, ppkey) != HCF_OPENSSL_SUCCESS) {
485             LOGD("[error] Generate pkey failed.");
486             ret = HCF_ERR_CRYPTO_OPERATION;
487             break;
488         }
489         if (OpensslEvpPkeyCheck(pkeyCtx) != HCF_OPENSSL_SUCCESS) {
490             LOGD("[error] Check pkey fail.");
491             OpensslEvpPkeyFree(*ppkey);
492             *ppkey = NULL;
493             ret = HCF_ERR_CRYPTO_OPERATION;
494             break;
495         }
496     } while (0);
497     FreeCtx(paramsCtx, paramsPkey, pkeyCtx);
498     return ret;
499 }
500 
GetDhPubKeyEncodedDer(const HcfPubKey * self,const char * format,HcfBlob * returnBlob)501 static HcfResult GetDhPubKeyEncodedDer(const HcfPubKey *self, const char *format, HcfBlob *returnBlob)
502 {
503     (void)self;
504     (void)format;
505     (void)returnBlob;
506     return HCF_INVALID_PARAMS;
507 }
508 
FillOpensslDhPubKeyFunc(HcfOpensslDhPubKey * pk)509 static void FillOpensslDhPubKeyFunc(HcfOpensslDhPubKey *pk)
510 {
511     pk->base.base.base.destroy = DestroyDhPubKey;
512     pk->base.base.base.getClass = GetDhPubKeyClass;
513     pk->base.base.getAlgorithm = GetDhPubKeyAlgorithm;
514     pk->base.base.getEncoded = GetDhPubKeyEncoded;
515     pk->base.base.getEncodedPem = GetDhPubKeyEncodedPem;
516     pk->base.base.getFormat = GetDhPubKeyFormat;
517     pk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromDhPubKey;
518     pk->base.getAsyKeySpecInt = GetIntSpecFromDhPubKey;
519     pk->base.getAsyKeySpecString = GetStrSpecFromDhPubKey;
520     pk->base.getEncodedDer = GetDhPubKeyEncodedDer;
521 }
522 
FillOpensslDhPriKeyFunc(HcfOpensslDhPriKey * sk)523 static void FillOpensslDhPriKeyFunc(HcfOpensslDhPriKey *sk)
524 {
525     sk->base.base.base.destroy = DestroyDhPriKey;
526     sk->base.base.base.getClass = GetDhPriKeyClass;
527     sk->base.base.getAlgorithm = GetDhPriKeyAlgorithm;
528     sk->base.base.getEncoded = GetDhPriKeyEncoded;
529     sk->base.getEncodedPem = GetDhPriKeyEncodedPem;
530     sk->base.base.getFormat = GetDhPriKeyFormat;
531     sk->base.getAsyKeySpecBigInteger = GetBigIntegerSpecFromDhPriKey;
532     sk->base.getAsyKeySpecInt = GetIntSpecFromDhPriKey;
533     sk->base.getAsyKeySpecString = GetStrSpecFromDhPriKey;
534     sk->base.getEncodedDer = GetDhPriKeyEncodedDer;
535     sk->base.clearMem = ClearDhPriKeyMem;
536 }
537 
CreateDhPubKey(DH * pk,HcfOpensslDhPubKey ** returnPubKey)538 static HcfResult CreateDhPubKey(DH *pk, HcfOpensslDhPubKey **returnPubKey)
539 {
540     HcfOpensslDhPubKey *dhPubKey = (HcfOpensslDhPubKey *)HcfMalloc(sizeof(HcfOpensslDhPubKey), 0);
541     if (dhPubKey == NULL) {
542         LOGE("Failed to allocate DH public key memory.");
543         return HCF_ERR_MALLOC;
544     }
545     FillOpensslDhPubKeyFunc(dhPubKey);
546     dhPubKey->pk = pk;
547 
548     *returnPubKey = dhPubKey;
549     return HCF_SUCCESS;
550 }
551 
CreateDhPriKey(DH * sk,HcfOpensslDhPriKey ** returnPriKey)552 static HcfResult CreateDhPriKey(DH *sk, HcfOpensslDhPriKey **returnPriKey)
553 {
554     HcfOpensslDhPriKey *dhPriKey = (HcfOpensslDhPriKey *)HcfMalloc(sizeof(HcfOpensslDhPriKey), 0);
555     if (dhPriKey == NULL) {
556         LOGE("Failed to allocate Dh private key memory.");
557         return HCF_ERR_MALLOC;
558     }
559     FillOpensslDhPriKeyFunc(dhPriKey);
560     dhPriKey->sk = sk;
561 
562     *returnPriKey = dhPriKey;
563     return HCF_SUCCESS;
564 }
565 
CreateDhKeyPair(const HcfOpensslDhPubKey * pubKey,const HcfOpensslDhPriKey * priKey,HcfKeyPair ** returnKeyPair)566 static HcfResult CreateDhKeyPair(const HcfOpensslDhPubKey *pubKey, const HcfOpensslDhPriKey *priKey,
567     HcfKeyPair **returnKeyPair)
568 {
569     HcfOpensslDhKeyPair *keyPair = (HcfOpensslDhKeyPair *)HcfMalloc(sizeof(HcfOpensslDhKeyPair), 0);
570     if (keyPair == NULL) {
571         LOGE("Failed to allocate keyPair memory.");
572         return HCF_ERR_MALLOC;
573     }
574     keyPair->base.base.getClass = GetDhKeyPairClass;
575     keyPair->base.base.destroy = DestroyDhKeyPair;
576     keyPair->base.pubKey = (HcfPubKey *)pubKey;
577     keyPair->base.priKey = (HcfPriKey *)priKey;
578 
579     *returnKeyPair = (HcfKeyPair *)keyPair;
580     return HCF_SUCCESS;
581 }
582 
GeneratePubKeyByPkey(EVP_PKEY * pkey,HcfOpensslDhPubKey ** returnPubKey)583 static HcfResult GeneratePubKeyByPkey(EVP_PKEY *pkey, HcfOpensslDhPubKey **returnPubKey)
584 {
585     DH *pk = OpensslEvpPkeyGet1Dh(pkey);
586     if (pk == NULL) {
587         LOGD("[error] Get dh public key from pkey failed");
588         HcfPrintOpensslError();
589         return HCF_ERR_CRYPTO_OPERATION;
590     }
591     HcfResult ret = CreateDhPubKey(pk, returnPubKey);
592     if (ret != HCF_SUCCESS) {
593         LOGD("[error] Create DH public key failed");
594         OpensslDhFree(pk);
595     }
596     return ret;
597 }
598 
GeneratePriKeyByPkey(EVP_PKEY * pkey,HcfOpensslDhPriKey ** returnPriKey)599 static HcfResult GeneratePriKeyByPkey(EVP_PKEY *pkey, HcfOpensslDhPriKey **returnPriKey)
600 {
601     DH *sk = OpensslEvpPkeyGet1Dh(pkey);
602     if (sk == NULL) {
603         LOGD("[error] Get DH private key from pkey failed");
604         HcfPrintOpensslError();
605         return HCF_ERR_CRYPTO_OPERATION;
606     }
607     HcfResult ret = CreateDhPriKey(sk, returnPriKey);
608     if (ret != HCF_SUCCESS) {
609         LOGD("[error] Create DH private key failed");
610         OpensslDhFree(sk);
611     }
612     return ret;
613 }
614 
GenerateDhPubAndPriKey(int32_t dhId,HcfOpensslDhPubKey ** returnPubKey,HcfOpensslDhPriKey ** returnPriKey)615 static HcfResult GenerateDhPubAndPriKey(int32_t dhId, HcfOpensslDhPubKey **returnPubKey,
616     HcfOpensslDhPriKey **returnPriKey)
617 {
618     EVP_PKEY *pkey = NULL;
619     HcfResult ret = GenerateDhEvpKey(dhId, &pkey);
620     if (ret != HCF_SUCCESS) {
621         LOGE("Generate DH EVP_PKEY failed.");
622         return ret;
623     }
624 
625     ret = GeneratePubKeyByPkey(pkey, returnPubKey);
626     if (ret != HCF_SUCCESS) {
627         OpensslEvpPkeyFree(pkey);
628         LOGE("Generate public key failed.");
629         return ret;
630     }
631 
632     ret = GeneratePriKeyByPkey(pkey, returnPriKey);
633     if (ret != HCF_SUCCESS) {
634         HcfObjDestroy(*returnPubKey);
635         *returnPubKey = NULL;
636         OpensslEvpPkeyFree(pkey);
637         LOGE("Generate private key failed.");
638         return ret;
639     }
640 
641     OpensslEvpPkeyFree(pkey);
642     return ret;
643 }
644 
ConvertCommSpec2Bn(const HcfDhCommParamsSpec * paramsSpec,BIGNUM ** p,BIGNUM ** g)645 static HcfResult ConvertCommSpec2Bn(const HcfDhCommParamsSpec *paramsSpec, BIGNUM **p, BIGNUM **g)
646 {
647     if (BigIntegerToBigNum(&(paramsSpec->p), p) != HCF_SUCCESS) {
648         LOGD("[error] Get openssl BN p failed");
649         return HCF_ERR_CRYPTO_OPERATION;
650     }
651     if (BigIntegerToBigNum(&(paramsSpec->g), g) != HCF_SUCCESS) {
652         LOGD("[error] Get openssl BN g failed");
653         OpensslBnFree(*p);
654         *p = NULL;
655         return HCF_ERR_CRYPTO_OPERATION;
656     }
657     return HCF_SUCCESS;
658 }
659 
CreateOpensslDhKey(const HcfDhCommParamsSpec * paramsSpec,BIGNUM * pk,BIGNUM * sk,DH ** returnDh)660 static HcfResult CreateOpensslDhKey(const HcfDhCommParamsSpec *paramsSpec, BIGNUM *pk, BIGNUM *sk, DH **returnDh)
661 {
662     BIGNUM *p = NULL;
663     BIGNUM *g = NULL;
664     if (ConvertCommSpec2Bn(paramsSpec, &p, &g)!= HCF_SUCCESS) {
665         LOGD("[error] Get openssl BN p q failed");
666         return HCF_ERR_CRYPTO_OPERATION;
667     }
668     DH *dh = OpensslDhNew();
669     if (dh == NULL) {
670         FreeCommSpecBn(p, g);
671         LOGD("[error] Openssl dh new failed");
672         HcfPrintOpensslError();
673         return HCF_ERR_CRYPTO_OPERATION;
674     }
675     if (OpensslDhSet0Pqg(dh, p, NULL, g) != HCF_OPENSSL_SUCCESS) {
676         LOGD("[error] Openssl dh set pqg failed");
677         HcfPrintOpensslError();
678         FreeCommSpecBn(p, g);
679         OpensslDhFree(dh);
680         return HCF_ERR_CRYPTO_OPERATION;
681     }
682     if (paramsSpec->length > 0) {
683         if (OpensslDhSetLength(dh, paramsSpec->length) != HCF_OPENSSL_SUCCESS) {
684             LOGD("[error] Openssl dh set length failed");
685             HcfPrintOpensslError();
686             OpensslDhFree(dh);
687             return HCF_ERR_CRYPTO_OPERATION;
688         }
689     }
690     if ((pk == NULL) && (sk == NULL)) {
691         *returnDh = dh;
692         return HCF_SUCCESS;
693     }
694     if (OpensslDhSet0Key(dh, pk, sk) != HCF_OPENSSL_SUCCESS) {
695         LOGD("[error] Openssl DH set key failed");
696         HcfPrintOpensslError();
697         OpensslDhFree(dh);
698         return HCF_ERR_CRYPTO_OPERATION;
699     }
700     *returnDh = dh;
701     return HCF_SUCCESS;
702 }
703 
GenerateOpensslDhKeyByCommSpec(const HcfDhCommParamsSpec * paramsSpec,DH ** returnDh)704 static HcfResult GenerateOpensslDhKeyByCommSpec(const HcfDhCommParamsSpec *paramsSpec, DH **returnDh)
705 {
706     if (CreateOpensslDhKey(paramsSpec, NULL, NULL, returnDh) != HCF_SUCCESS) {
707         LOGD("[error] Create openssl dh key failed");
708         return HCF_ERR_CRYPTO_OPERATION;
709     }
710 
711     if (OpensslDhGenerateKey(*returnDh) != HCF_OPENSSL_SUCCESS) {
712         LOGD("[error] Openssl DH generate key failed");
713         HcfPrintOpensslError();
714         OpensslDhFree(*returnDh);
715         *returnDh = NULL;
716         return HCF_ERR_CRYPTO_OPERATION;
717     }
718     return HCF_SUCCESS;
719 }
720 
GenerateOpensslDhKeyByPubKeySpec(const HcfDhPubKeyParamsSpec * paramsSpec,DH ** returnDh)721 static HcfResult GenerateOpensslDhKeyByPubKeySpec(const HcfDhPubKeyParamsSpec *paramsSpec, DH **returnDh)
722 {
723     BIGNUM *pubKey = NULL;
724     if (BigIntegerToBigNum(&(paramsSpec->pk), &pubKey) != HCF_SUCCESS) {
725         LOGD("[error] Get openssl BN pk failed");
726         return HCF_ERR_CRYPTO_OPERATION;
727     }
728 
729     if (CreateOpensslDhKey(&(paramsSpec->base), pubKey, NULL, returnDh) != HCF_SUCCESS) {
730         LOGD("[error] Create dh key failed.");
731         OpensslBnFree(pubKey);
732         return HCF_ERR_CRYPTO_OPERATION;
733     }
734     return HCF_SUCCESS;
735 }
736 
GenerateOpensslDhKeyByPriKeySpec(const HcfDhPriKeyParamsSpec * paramsSpec,DH ** returnDh)737 static HcfResult GenerateOpensslDhKeyByPriKeySpec(const HcfDhPriKeyParamsSpec *paramsSpec, DH **returnDh)
738 {
739     BIGNUM *priKey = NULL;
740     if (BigIntegerToBigNum(&(paramsSpec->sk), &priKey) != HCF_SUCCESS) {
741         LOGD("[error] Get openssl BN pk failed");
742         return HCF_ERR_CRYPTO_OPERATION;
743     }
744 
745     if (CreateOpensslDhKey(&(paramsSpec->base), NULL, priKey, returnDh) != HCF_SUCCESS) {
746         LOGD("[error] Create dh key failed.");
747         OpensslBnFree(priKey);
748         return HCF_ERR_CRYPTO_OPERATION;
749     }
750     return HCF_SUCCESS;
751 }
752 
GenerateOpensslDhKeyByKeyPairSpec(const HcfDhKeyPairParamsSpec * paramsSpec,DH ** returnDh)753 static HcfResult GenerateOpensslDhKeyByKeyPairSpec(const HcfDhKeyPairParamsSpec *paramsSpec, DH **returnDh)
754 {
755     BIGNUM *pubKey = NULL;
756     BIGNUM *priKey = NULL;
757     if (BigIntegerToBigNum(&(paramsSpec->pk), &pubKey) != HCF_SUCCESS) {
758         LOGD("[error] Get openssl BN pk failed");
759         return HCF_ERR_CRYPTO_OPERATION;
760     }
761     if (BigIntegerToBigNum(&(paramsSpec->sk), &priKey) != HCF_SUCCESS) {
762         LOGD("[error] Get openssl BN sk failed");
763         OpensslBnFree(pubKey);
764         return HCF_ERR_CRYPTO_OPERATION;
765     }
766     if (CreateOpensslDhKey(&(paramsSpec->base), pubKey, priKey, returnDh) != HCF_SUCCESS) {
767         LOGD("[error] Create dh key failed.");
768         OpensslBnFree(pubKey);
769         OpensslBnFree(priKey);
770         return HCF_ERR_CRYPTO_OPERATION;
771     }
772     return HCF_SUCCESS;
773 }
774 
CreateDhKeyPairByCommSpec(const HcfDhCommParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)775 static HcfResult CreateDhKeyPairByCommSpec(const HcfDhCommParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
776 {
777     DH *dh = NULL;
778     if (GenerateOpensslDhKeyByCommSpec(paramsSpec, &dh) != HCF_SUCCESS) {
779         LOGD("[error] Generate openssl dh key by commSpec failed.");
780         return HCF_ERR_CRYPTO_OPERATION;
781     }
782     HcfOpensslDhPubKey *pubKey = NULL;
783     if (CreateDhPubKey(dh, &pubKey) != HCF_SUCCESS) {
784         LOGE("Create dh pubKey failed.");
785         OpensslDhFree(dh);
786         return HCF_ERR_MALLOC;
787     }
788 
789     if (OpensslDhUpRef(dh) != HCF_OPENSSL_SUCCESS) {
790         LOGD("[error] DH_up_ref failed.");
791         HcfPrintOpensslError();
792         HcfObjDestroy(pubKey);
793         pubKey = NULL;
794         return HCF_ERR_CRYPTO_OPERATION;
795     }
796 
797     HcfOpensslDhPriKey *priKey = NULL;
798     if (CreateDhPriKey(dh, &priKey) != HCF_SUCCESS) {
799         LOGE("Create dh priKey failed.");
800         OpensslDhFree(dh);
801         HcfObjDestroy(pubKey);
802         pubKey = NULL;
803         return HCF_ERR_MALLOC;
804     }
805 
806     if (CreateDhKeyPair(pubKey, priKey, returnKeyPair) != HCF_SUCCESS) {
807         LOGE("Create dh keyPair failed.");
808         HcfObjDestroy(pubKey);
809         pubKey = NULL;
810         HcfObjDestroy(priKey);
811         priKey = NULL;
812         return HCF_ERR_MALLOC;
813     }
814     return HCF_SUCCESS;
815 }
816 
CreateDhPubKeyByKeyPairSpec(const HcfDhKeyPairParamsSpec * paramsSpec,HcfOpensslDhPubKey ** returnPubKey)817 static HcfResult CreateDhPubKeyByKeyPairSpec(const HcfDhKeyPairParamsSpec *paramsSpec,
818     HcfOpensslDhPubKey **returnPubKey)
819 {
820     DH *dh = NULL;
821     if (GenerateOpensslDhKeyByKeyPairSpec(paramsSpec, &dh) != HCF_SUCCESS) {
822         LOGD("[error] Generate openssl dh key by keyPairSpec failed.");
823         return HCF_ERR_CRYPTO_OPERATION;
824     }
825     if (CreateDhPubKey(dh, returnPubKey) != HCF_SUCCESS) {
826         LOGE("Create dh pubKey failed.");
827         OpensslDhFree(dh);
828         return HCF_ERR_MALLOC;
829     }
830     return HCF_SUCCESS;
831 }
832 
CreateDhPriKeyByKeyPairSpec(const HcfDhKeyPairParamsSpec * paramsSpec,HcfOpensslDhPriKey ** returnPriKey)833 static HcfResult CreateDhPriKeyByKeyPairSpec(const HcfDhKeyPairParamsSpec *paramsSpec,
834     HcfOpensslDhPriKey **returnPriKey)
835 {
836     DH *dh = NULL;
837     if (GenerateOpensslDhKeyByKeyPairSpec(paramsSpec, &dh) != HCF_SUCCESS) {
838         LOGD("[error] Generate openssl dh key by keyPairSpec failed.");
839         return HCF_ERR_CRYPTO_OPERATION;
840     }
841     if (CreateDhPriKey(dh, returnPriKey) != HCF_SUCCESS) {
842         LOGE("Create dh priKey failed.");
843         OpensslDhFree(dh);
844         return HCF_ERR_MALLOC;
845     }
846     return HCF_SUCCESS;
847 }
848 
CreateDhKeyPairByKeyPairSpec(const HcfDhKeyPairParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)849 static HcfResult CreateDhKeyPairByKeyPairSpec(const HcfDhKeyPairParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
850 {
851     HcfOpensslDhPubKey *pubKey = NULL;
852     HcfResult ret = CreateDhPubKeyByKeyPairSpec(paramsSpec, &pubKey);
853     if (ret != HCF_SUCCESS) {
854         LOGD("[error] Create dh pubKey by keyPairSpec failed.");
855         return ret;
856     }
857 
858     HcfOpensslDhPriKey *priKey = NULL;
859     ret = CreateDhPriKeyByKeyPairSpec(paramsSpec, &priKey);
860     if (ret != HCF_SUCCESS) {
861         LOGD("[error] Create dh priKey by keyPairSpec failed.");
862         HcfObjDestroy(pubKey);
863         pubKey = NULL;
864         return ret;
865     }
866     ret = CreateDhKeyPair(pubKey, priKey, returnKeyPair);
867     if (ret != HCF_SUCCESS) {
868         LOGD("[error] Create dh keyPair failed.");
869         HcfObjDestroy(pubKey);
870         pubKey = NULL;
871         HcfObjDestroy(priKey);
872         priKey = NULL;
873         return ret;
874     }
875     return HCF_SUCCESS;
876 }
877 
CreateDhKeyPairBySpec(const HcfAsyKeyParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)878 static HcfResult CreateDhKeyPairBySpec(const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
879 {
880     if (paramsSpec->specType == HCF_COMMON_PARAMS_SPEC) {
881         return CreateDhKeyPairByCommSpec((const HcfDhCommParamsSpec *)paramsSpec, returnKeyPair);
882     } else {
883         return CreateDhKeyPairByKeyPairSpec((const HcfDhKeyPairParamsSpec*)paramsSpec, returnKeyPair);
884     }
885 }
886 
CreateDhPubKeyBySpec(const HcfDhPubKeyParamsSpec * paramsSpec,HcfPubKey ** returnPubKey)887 static HcfResult CreateDhPubKeyBySpec(const HcfDhPubKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey)
888 {
889     DH *dh = NULL;
890     if (GenerateOpensslDhKeyByPubKeySpec(paramsSpec, &dh) != HCF_SUCCESS) {
891         LOGD("[error] Generate openssl dh key by pubKeySpec failed.");
892         return HCF_ERR_CRYPTO_OPERATION;
893     }
894 
895     HcfOpensslDhPubKey *pubKey = NULL;
896     if (CreateDhPubKey(dh, &pubKey) != HCF_SUCCESS) {
897         LOGE("Create dh pubKey failed.");
898         OpensslDhFree(dh);
899         return HCF_ERR_MALLOC;
900     }
901     *returnPubKey = (HcfPubKey *)pubKey;
902     return HCF_SUCCESS;
903 }
904 
CreateDhPriKeyBySpec(const HcfDhPriKeyParamsSpec * paramsSpec,HcfPriKey ** returnPriKey)905 static HcfResult CreateDhPriKeyBySpec(const HcfDhPriKeyParamsSpec *paramsSpec, HcfPriKey **returnPriKey)
906 {
907     DH *dh = NULL;
908     if (GenerateOpensslDhKeyByPriKeySpec(paramsSpec, &dh) != HCF_SUCCESS) {
909         LOGD("[error] Generate openssl dh key by priKeySpec failed.");
910         return HCF_ERR_CRYPTO_OPERATION;
911     }
912 
913     HcfOpensslDhPriKey *priKey = NULL;
914     if (CreateDhPriKey(dh, &priKey) != HCF_SUCCESS) {
915         LOGE("Create dh priKey failed.");
916         OpensslDhFree(dh);
917         return HCF_ERR_MALLOC;
918     }
919     *returnPriKey = (HcfPriKey *)priKey;
920     return HCF_SUCCESS;
921 }
922 
ConvertDhPubKey(const HcfBlob * pubKeyBlob,HcfOpensslDhPubKey ** returnPubKey)923 static HcfResult ConvertDhPubKey(const HcfBlob *pubKeyBlob, HcfOpensslDhPubKey **returnPubKey)
924 {
925     const unsigned char *temp = (const unsigned char *)pubKeyBlob->data;
926     EVP_PKEY *pKey = OpensslD2iPubKey(NULL, &temp, pubKeyBlob->len);
927     if (pKey == NULL) {
928         LOGD("[error] Call d2i_PUBKEY failed.");
929         HcfPrintOpensslError();
930         return HCF_ERR_CRYPTO_OPERATION;
931     }
932     DH *dh = OpensslEvpPkeyGet1Dh(pKey);
933     if (dh == NULL) {
934         LOGD("[error] EVP_PKEY_get1_DH failed");
935         HcfPrintOpensslError();
936         OpensslEvpPkeyFree(pKey);
937         return HCF_ERR_CRYPTO_OPERATION;
938     }
939     OpensslEvpPkeyFree(pKey);
940     HcfResult ret = CreateDhPubKey(dh, returnPubKey);
941     if (ret != HCF_SUCCESS) {
942         LOGE("Create dh public key failed");
943         OpensslDhFree(dh);
944     }
945     return ret;
946 }
947 
ConvertDhPriKey(const HcfBlob * priKeyBlob,HcfOpensslDhPriKey ** returnPriKey)948 static HcfResult ConvertDhPriKey(const HcfBlob *priKeyBlob, HcfOpensslDhPriKey **returnPriKey)
949 {
950     const unsigned char *temp = (const unsigned char *)priKeyBlob->data;
951     EVP_PKEY *pKey = OpensslD2iPrivateKey(EVP_PKEY_DH, NULL, &temp, priKeyBlob->len);
952     if (pKey == NULL) {
953         LOGD("[error] Call d2i_PrivateKey failed.");
954         HcfPrintOpensslError();
955         return HCF_ERR_CRYPTO_OPERATION;
956     }
957     DH *dh = OpensslEvpPkeyGet1Dh(pKey);
958     if (dh == NULL) {
959         LOGD("[error] EVP_PKEY_get1_DH failed");
960         HcfPrintOpensslError();
961         OpensslEvpPkeyFree(pKey);
962         return HCF_ERR_CRYPTO_OPERATION;
963     }
964     OpensslEvpPkeyFree(pKey);
965     HcfResult ret = CreateDhPriKey(dh, returnPriKey);
966     if (ret != HCF_SUCCESS) {
967         LOGE("Create DH private key failed");
968         OpensslDhFree(dh);
969     }
970     return ret;
971 }
972 
ConvertDhPubAndPriKey(const HcfBlob * pubKeyBlob,const HcfBlob * priKeyBlob,HcfOpensslDhPubKey ** returnPubKey,HcfOpensslDhPriKey ** returnPriKey)973 static HcfResult ConvertDhPubAndPriKey(const HcfBlob *pubKeyBlob, const HcfBlob *priKeyBlob,
974     HcfOpensslDhPubKey **returnPubKey, HcfOpensslDhPriKey **returnPriKey)
975 {
976     if (pubKeyBlob != NULL) {
977         if (ConvertDhPubKey(pubKeyBlob, returnPubKey) != HCF_SUCCESS) {
978             LOGD("[error] Convert DH public key failed.");
979             return HCF_ERR_CRYPTO_OPERATION;
980         }
981     }
982     if (priKeyBlob != NULL) {
983         if (ConvertDhPriKey(priKeyBlob, returnPriKey) != HCF_SUCCESS) {
984             LOGD("[error] Convert DH private key failed.");
985             HcfObjDestroy(*returnPubKey);
986             *returnPubKey = NULL;
987             return HCF_ERR_CRYPTO_OPERATION;
988         }
989     }
990     return HCF_SUCCESS;
991 }
992 
EngineGenerateDhKeyPair(HcfAsyKeyGeneratorSpi * self,HcfKeyPair ** returnKeyPair)993 static HcfResult EngineGenerateDhKeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **returnKeyPair)
994 {
995     if (self == NULL || returnKeyPair == NULL) {
996         LOGE("Invalid params.");
997         return HCF_INVALID_PARAMS;
998     }
999     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhKeyGeneratorSpiClass())) {
1000         LOGE("Class not match.");
1001         return HCF_INVALID_PARAMS;
1002     }
1003     HcfAsyKeyGeneratorSpiDhOpensslImpl *impl = (HcfAsyKeyGeneratorSpiDhOpensslImpl *)self;
1004 
1005     HcfOpensslDhPubKey *pubKey = NULL;
1006     HcfOpensslDhPriKey *priKey = NULL;
1007     HcfResult ret = GenerateDhPubAndPriKey(impl->pBits, &pubKey, &priKey);
1008     if (ret != HCF_SUCCESS) {
1009         LOGE("Generate DH pk and sk by openssl failed.");
1010         return ret;
1011     }
1012     ret = CreateDhKeyPair(pubKey, priKey, returnKeyPair);
1013     if (ret != HCF_SUCCESS) {
1014         LOGE("Create dh keyPair failed.");
1015         HcfObjDestroy(pubKey);
1016         pubKey = NULL;
1017         HcfObjDestroy(priKey);
1018         priKey = NULL;
1019         return ret;
1020     }
1021     return HCF_SUCCESS;
1022 }
1023 
EngineConvertDhKey(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,HcfBlob * pubKeyBlob,HcfBlob * priKeyBlob,HcfKeyPair ** returnKeyPair)1024 static HcfResult EngineConvertDhKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
1025     HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
1026 {
1027     (void)params;
1028     if ((self == NULL) || (returnKeyPair == NULL)) {
1029         LOGE("Invalid input parameter.");
1030         return HCF_INVALID_PARAMS;
1031     }
1032     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhKeyGeneratorSpiClass())) {
1033         LOGE("Class not match.");
1034         return HCF_INVALID_PARAMS;
1035     }
1036     bool pubKeyValid = HcfIsBlobValid(pubKeyBlob);
1037     bool priKeyValid = HcfIsBlobValid(priKeyBlob);
1038     if ((!pubKeyValid) && (!priKeyValid)) {
1039         LOGE("The private key and public key cannot both be NULL.");
1040         return HCF_INVALID_PARAMS;
1041     }
1042 
1043     HcfOpensslDhPubKey *pubKey = NULL;
1044     HcfOpensslDhPriKey *priKey = NULL;
1045     HcfBlob *inputPk = pubKeyValid ? pubKeyBlob : NULL;
1046     HcfBlob *inputSk = priKeyValid ? priKeyBlob : NULL;
1047     HcfResult ret = ConvertDhPubAndPriKey(inputPk, inputSk, &pubKey, &priKey);
1048     if (ret != HCF_SUCCESS) {
1049         LOGE("Convert dh pubKey and priKey failed.");
1050         return ret;
1051     }
1052     ret = CreateDhKeyPair(pubKey, priKey, returnKeyPair);
1053     if (ret != HCF_SUCCESS) {
1054         LOGE("Create dh keyPair failed.");
1055         HcfObjDestroy(pubKey);
1056         pubKey = NULL;
1057         HcfObjDestroy(priKey);
1058         priKey = NULL;
1059     }
1060     return ret;
1061 }
1062 
ConvertDhPemPubKey(const char * pubKeyStr,HcfOpensslDhPubKey ** returnPubKey)1063 static HcfResult ConvertDhPemPubKey(const char *pubKeyStr, HcfOpensslDhPubKey **returnPubKey)
1064 {
1065     EVP_PKEY *pkey = NULL;
1066     const char *keyType = "DH";
1067     HcfResult ret = ConvertPubPemStrToKey(&pkey, keyType, EVP_PKEY_PUBLIC_KEY, pubKeyStr);
1068     if (ret != HCF_SUCCESS) {
1069         LOGE("Convert dh pem public key failed.");
1070         return ret;
1071     }
1072 
1073     DH *dh = OpensslEvpPkeyGet1Dh(pkey);
1074     OpensslEvpPkeyFree(pkey);
1075     if (dh == NULL) {
1076         LOGE("Pkey to dh key failed.");
1077         HcfPrintOpensslError();
1078         return HCF_ERR_CRYPTO_OPERATION;
1079     }
1080 
1081     ret = CreateDhPubKey(dh, returnPubKey);
1082     if (ret != HCF_SUCCESS) {
1083         LOGE("Create dh public key failed.");
1084         OpensslDhFree(dh);
1085     }
1086 
1087     return ret;
1088 }
1089 
ConvertDhPemPriKey(const char * priKeyStr,HcfOpensslDhPriKey ** returnPriKey)1090 static HcfResult ConvertDhPemPriKey(const char *priKeyStr, HcfOpensslDhPriKey **returnPriKey)
1091 {
1092     EVP_PKEY *pkey = NULL;
1093     const char *keyType = "DH";
1094     HcfResult ret = ConvertPriPemStrToKey(priKeyStr, &pkey, keyType);
1095     if (ret != HCF_SUCCESS) {
1096         LOGE("Convert dh pem private key failed.");
1097         return ret;
1098     }
1099 
1100     DH *dh = OpensslEvpPkeyGet1Dh(pkey);
1101     OpensslEvpPkeyFree(pkey);
1102     if (dh == NULL) {
1103         LOGE("Pkey to dh key failed.");
1104         HcfPrintOpensslError();
1105         return HCF_ERR_CRYPTO_OPERATION;
1106     }
1107 
1108     ret = CreateDhPriKey(dh, returnPriKey);
1109     if (ret != HCF_SUCCESS) {
1110         LOGE("Create DH private key failed.");
1111         OpensslDhFree(dh);
1112     }
1113 
1114     return ret;
1115 }
1116 
ConvertDhPemPubAndPriKey(const char * pubKeyStr,const char * priKeyStr,HcfOpensslDhPubKey ** returnPubKey,HcfOpensslDhPriKey ** returnPriKey)1117 static HcfResult ConvertDhPemPubAndPriKey(const char *pubKeyStr, const char *priKeyStr,
1118     HcfOpensslDhPubKey **returnPubKey, HcfOpensslDhPriKey **returnPriKey)
1119 {
1120     if (pubKeyStr != NULL && strlen(pubKeyStr) != 0) {
1121         if (ConvertDhPemPubKey(pubKeyStr, returnPubKey) != HCF_SUCCESS) {
1122             LOGE("Convert dh pem public key failed.");
1123             return HCF_ERR_CRYPTO_OPERATION;
1124         }
1125     }
1126 
1127     if (priKeyStr != NULL && strlen(priKeyStr) != 0) {
1128         if (ConvertDhPemPriKey(priKeyStr, returnPriKey) != HCF_SUCCESS) {
1129             LOGE("Convert dh pem private key failed.");
1130             HcfObjDestroy(*returnPubKey);
1131             *returnPubKey = NULL;
1132             return HCF_ERR_CRYPTO_OPERATION;
1133         }
1134     }
1135 
1136     return HCF_SUCCESS;
1137 }
1138 
EngineConvertDhPemKey(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,const char * pubKeyStr,const char * priKeyStr,HcfKeyPair ** returnKeyPair)1139 static HcfResult EngineConvertDhPemKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, const char *pubKeyStr,
1140     const char *priKeyStr, HcfKeyPair **returnKeyPair)
1141 {
1142     (void)params;
1143     if ((self == NULL) || (returnKeyPair == NULL) || ((pubKeyStr == NULL) && (priKeyStr == NULL))) {
1144         LOGE("Invalid input parameter.");
1145         return HCF_INVALID_PARAMS;
1146     }
1147     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhKeyGeneratorSpiClass())) {
1148         LOGE("Class not match.");
1149         return HCF_INVALID_PARAMS;
1150     }
1151 
1152     HcfOpensslDhPubKey *pubKey = NULL;
1153     HcfOpensslDhPriKey *priKey = NULL;
1154 
1155     HcfResult ret = ConvertDhPemPubAndPriKey(pubKeyStr, priKeyStr, &pubKey, &priKey);
1156     if (ret != HCF_SUCCESS) {
1157         LOGE("Convert dh pem pubKey and priKey failed.");
1158         return ret;
1159     }
1160 
1161     ret = CreateDhKeyPair(pubKey, priKey, returnKeyPair);
1162     if (ret != HCF_SUCCESS) {
1163         LOGE("Create dh keyPair failed.");
1164         HcfObjDestroy(pubKey);
1165         pubKey = NULL;
1166         HcfObjDestroy(priKey);
1167         priKey = NULL;
1168     }
1169 
1170     return ret;
1171 }
1172 
EngineGenerateDhKeyPairBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)1173 static HcfResult EngineGenerateDhKeyPairBySpec(const HcfAsyKeyGeneratorSpi *self,
1174     const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
1175 {
1176     if ((self == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL) || (returnKeyPair == NULL)) {
1177         LOGE("Invalid input parameter.");
1178         return HCF_INVALID_PARAMS;
1179     }
1180 
1181     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhKeyGeneratorSpiClass())) {
1182         LOGE("Class not match.");
1183         return HCF_INVALID_PARAMS;
1184     }
1185 
1186     if ((strcmp(paramsSpec->algName, ALGORITHM_NAME_DH) != 0) ||
1187         ((paramsSpec->specType != HCF_COMMON_PARAMS_SPEC) && (paramsSpec->specType != HCF_KEY_PAIR_SPEC))) {
1188         LOGE("Invalid params spec.");
1189         return HCF_INVALID_PARAMS;
1190     }
1191     HcfResult ret = CreateDhKeyPairBySpec(paramsSpec, returnKeyPair);
1192     if (ret != HCF_SUCCESS) {
1193         LOGE("Create DH key pair by spec failed.");
1194     }
1195     return ret;
1196 }
1197 
EngineGenerateDhPubKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPubKey ** returnPubKey)1198 static HcfResult EngineGenerateDhPubKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
1199     const HcfAsyKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey)
1200 {
1201     if ((self == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL) || (returnPubKey == NULL)) {
1202         LOGE("Invalid input parameter.");
1203         return HCF_INVALID_PARAMS;
1204     }
1205 
1206     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhKeyGeneratorSpiClass())) {
1207         LOGE("Class not match.");
1208         return HCF_INVALID_PARAMS;
1209     }
1210 
1211     if ((strcmp(paramsSpec->algName, ALGORITHM_NAME_DH) != 0) ||
1212         ((paramsSpec->specType != HCF_PUBLIC_KEY_SPEC) && (paramsSpec->specType != HCF_KEY_PAIR_SPEC))) {
1213         LOGE("Invalid params spec.");
1214         return HCF_INVALID_PARAMS;
1215     }
1216 
1217     HcfResult ret = CreateDhPubKeyBySpec((const HcfDhPubKeyParamsSpec *)paramsSpec, returnPubKey);
1218     if (ret != HCF_SUCCESS) {
1219         LOGE("Create DH public key by spec failed.");
1220     }
1221     return ret;
1222 }
1223 
EngineGenerateDhPriKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPriKey ** returnPriKey)1224 static HcfResult EngineGenerateDhPriKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
1225     const HcfAsyKeyParamsSpec *paramsSpec, HcfPriKey **returnPriKey)
1226 {
1227     if ((self == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL) || (returnPriKey == NULL)) {
1228         LOGE("Invalid input parameter.");
1229         return HCF_INVALID_PARAMS;
1230     }
1231     if (!HcfIsClassMatch((HcfObjectBase *)self, GetDhKeyGeneratorSpiClass())) {
1232         LOGE("Class not match.");
1233         return HCF_INVALID_PARAMS;
1234     }
1235     if ((strcmp(paramsSpec->algName, ALGORITHM_NAME_DH) != 0) ||
1236         ((paramsSpec->specType != HCF_PRIVATE_KEY_SPEC) && (paramsSpec->specType != HCF_KEY_PAIR_SPEC))) {
1237         LOGE("Invalid params spec.");
1238         return HCF_INVALID_PARAMS;
1239     }
1240 
1241     HcfResult ret = CreateDhPriKeyBySpec((const HcfDhPriKeyParamsSpec *)paramsSpec, returnPriKey);
1242     if (ret != HCF_SUCCESS) {
1243         LOGE("Create DH private key by spec failed.");
1244     }
1245     return ret;
1246 }
1247 
HcfAsyKeyGeneratorSpiDhCreate(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** generator)1248 HcfResult HcfAsyKeyGeneratorSpiDhCreate(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **generator)
1249 {
1250     if (params == NULL || generator == NULL) {
1251         LOGE("Invalid input parameter.");
1252         return HCF_INVALID_PARAMS;
1253     }
1254     HcfAsyKeyGeneratorSpiDhOpensslImpl *impl = (HcfAsyKeyGeneratorSpiDhOpensslImpl *)HcfMalloc(
1255         sizeof(HcfAsyKeyGeneratorSpiDhOpensslImpl), 0);
1256     if (impl == NULL) {
1257         LOGE("Failed to allocate generator impl memroy.");
1258         return HCF_ERR_MALLOC;
1259     }
1260     impl->pBits = params->bits;
1261     impl->base.base.getClass = GetDhKeyGeneratorSpiClass;
1262     impl->base.base.destroy = DestroyDhKeyGeneratorSpiImpl;
1263     impl->base.engineGenerateKeyPair = EngineGenerateDhKeyPair;
1264     impl->base.engineConvertKey = EngineConvertDhKey;
1265     impl->base.engineConvertPemKey = EngineConvertDhPemKey;
1266     impl->base.engineGenerateKeyPairBySpec = EngineGenerateDhKeyPairBySpec;
1267     impl->base.engineGeneratePubKeyBySpec = EngineGenerateDhPubKeyBySpec;
1268     impl->base.engineGeneratePriKeyBySpec = EngineGenerateDhPriKeyBySpec;
1269 
1270     *generator = (HcfAsyKeyGeneratorSpi *)impl;
1271     return HCF_SUCCESS;
1272 }