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 "securec.h"
17 #include "string.h"
18
19 #include "openssl_adapter.h"
20 #include "openssl_class.h"
21 #include "openssl_common.h"
22 #include "openssl/pem.h"
23 #include "openssl/x509.h"
24
25 #include "algorithm_parameter.h"
26 #include "asy_key_generator_spi.h"
27 #include "detailed_rsa_key_params.h"
28 #include "log.h"
29 #include "memory.h"
30 #include "rsa_openssl_common.h"
31 #include "utils.h"
32 #include "asy_key_generator.h"
33
34 #include "rsa_asy_key_generator_openssl.h"
35
36 #define OPENSSL_BITS_PER_BYTE 8
37 #define OPENSSL_RSA_KEYPAIR_CNT 3
38 #define OPENSSL_RSA_KEYGEN_DEFAULT_PRIMES 2
39 #define MAX_KEY_SIZE 8192
40 #define MIN_KEY_SIZE 512
41 #define PRIMES_2 2
42 #define PRIMES_3 3
43 #define PRIMES_4 4
44 #define PRIMES_5 5
45
46 enum OpensslRsaKeySize {
47 OPENSSL_RSA_KEY_SIZE_BY_SPEC = 0,
48 OPENSSL_RSA_KEY_SIZE_512 = 512,
49 OPENSSL_RSA_KEY_SIZE_768 = 768,
50 OPENSSL_RSA_KEY_SIZE_1024 = 1024,
51 OPENSSL_RSA_KEY_SIZE_2048 = 2048,
52 OPENSSL_RSA_KEY_SIZE_3072 = 3072,
53 OPENSSL_RSA_KEY_SIZE_4096 = 4096,
54 OPENSSL_RSA_KEY_SIZE_8192 = 8192,
55 };
56
57 enum OpensslRsaPrimesSize {
58 OPENSSL_RSA_PRIMES_SIZE_2 = 2,
59 OPENSSL_RSA_PRIMES_SIZE_3 = 3,
60 OPENSSL_RSA_PRIMES_SIZE_4 = 4,
61 OPENSSL_RSA_PRIMES_SIZE_5 = 5,
62 };
63
64 typedef struct {
65 int32_t bits;
66 int32_t primes;
67 BIGNUM *pubExp;
68 } HcfAsyKeyGenSpiRsaParams;
69
70 typedef struct {
71 HcfAsyKeyGeneratorSpi base;
72
73 HcfAsyKeyGenSpiRsaParams *params;
74 } HcfAsyKeyGeneratorSpiRsaOpensslImpl;
75
76 #define CIPHER_LIST_SIZE 4
77
78 static const char *g_supportedCiphers[] = {
79 "DES-EDE3-CBC",
80 "AES-128-CBC",
81 "AES-192-CBC",
82 "AES-256-CBC"
83 };
84
IsCipherSupported(const char * cipher)85 static bool IsCipherSupported(const char *cipher)
86 {
87 if (cipher == NULL) {
88 return false;
89 }
90 for (size_t i = 0; i < CIPHER_LIST_SIZE; i++) {
91 if (strcmp(cipher, g_supportedCiphers[i]) == 0) {
92 return true;
93 }
94 }
95 return false;
96 }
97
CheckRsaKeyGenParams(HcfAsyKeyGenSpiRsaParams * params)98 static HcfResult CheckRsaKeyGenParams(HcfAsyKeyGenSpiRsaParams *params)
99 {
100 switch (params->bits) {
101 case OPENSSL_RSA_KEY_SIZE_BY_SPEC:
102 break;
103 case OPENSSL_RSA_KEY_SIZE_512:
104 case OPENSSL_RSA_KEY_SIZE_768:
105 if (params->primes != OPENSSL_RSA_PRIMES_SIZE_2) {
106 LOGE("Set invalid primes %d to Keygen bits %d.", params->primes, params->bits);
107 return HCF_INVALID_PARAMS;
108 }
109 break;
110 case OPENSSL_RSA_KEY_SIZE_1024:
111 case OPENSSL_RSA_KEY_SIZE_2048:
112 case OPENSSL_RSA_KEY_SIZE_3072:
113 if (params->primes > OPENSSL_RSA_PRIMES_SIZE_3 || params->primes < OPENSSL_RSA_PRIMES_SIZE_2) {
114 LOGE("Set invalid primes %d to Keygen bits %d.", params->primes, params->bits);
115 return HCF_INVALID_PARAMS;
116 }
117 break;
118 case OPENSSL_RSA_KEY_SIZE_4096:
119 if (params->primes > OPENSSL_RSA_PRIMES_SIZE_4 || params->primes < OPENSSL_RSA_PRIMES_SIZE_2) {
120 LOGE("Set invalid primes %d to Keygen bits %d.", params->primes, params->bits);
121 return HCF_INVALID_PARAMS;
122 }
123 break;
124 case OPENSSL_RSA_KEY_SIZE_8192: // This keySize can use primes from 2 to 5.
125 break;
126 default:
127 LOGE("The current bits %d is invalid.", params->bits);
128 return HCF_INVALID_PARAMS;
129 }
130 return HCF_SUCCESS;
131 }
132
GetOpensslPubkeyClass(void)133 static const char *GetOpensslPubkeyClass(void)
134 {
135 return OPENSSL_RSA_PUBKEY_CLASS;
136 }
137
GetOpensslPrikeyClass(void)138 static const char *GetOpensslPrikeyClass(void)
139 {
140 return OPENSSL_RSA_PRIKEY_CLASS;
141 }
142
GetOpensslKeyPairClass(void)143 static const char *GetOpensslKeyPairClass(void)
144 {
145 return OPENSSL_RSA_KEYPAIR_CLASS;
146 }
147
GetRsaPubKeySpecString(const HcfPubKey * self,const AsyKeySpecItem item,char ** returnString)148 static HcfResult GetRsaPubKeySpecString(const HcfPubKey *self, const AsyKeySpecItem item,
149 char **returnString)
150 {
151 (void)self;
152 (void)returnString;
153 LOGE("Rsa has no string attribute");
154 return HCF_NOT_SUPPORT;
155 }
156
GetRsaPubKeySpecInt(const HcfPubKey * self,const AsyKeySpecItem item,int * returnInt)157 static HcfResult GetRsaPubKeySpecInt(const HcfPubKey *self, const AsyKeySpecItem item,
158 int *returnInt)
159 {
160 (void)self;
161 (void)returnInt;
162 LOGE("Rsa has no integer attribute");
163 return HCF_NOT_SUPPORT;
164 }
165
GetRsaPriKeySpecString(const HcfPriKey * self,const AsyKeySpecItem item,char ** returnString)166 static HcfResult GetRsaPriKeySpecString(const HcfPriKey *self, const AsyKeySpecItem item,
167 char **returnString)
168 {
169 (void)self;
170 (void)returnString;
171 LOGE("Rsa has no string attribute");
172 return HCF_NOT_SUPPORT;
173 }
174
GetRsaPriKeySpecInt(const HcfPriKey * self,const AsyKeySpecItem item,int * returnInt)175 static HcfResult GetRsaPriKeySpecInt(const HcfPriKey *self, const AsyKeySpecItem item,
176 int *returnInt)
177 {
178 (void)self;
179 (void)returnInt;
180 LOGE("Rsa has no integer attribute");
181 return HCF_NOT_SUPPORT;
182 }
GetRsaPriKeyEncodedDer(const HcfPriKey * self,const char * format,HcfBlob * returnBlob)183 static HcfResult GetRsaPriKeyEncodedDer(const HcfPriKey *self, const char *format, HcfBlob *returnBlob)
184 {
185 (void)self;
186 (void)format;
187 (void)returnBlob;
188 return HCF_INVALID_PARAMS;
189 }
190
GetRsaPriKeySpecBigInteger(const HcfPriKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)191 static HcfResult GetRsaPriKeySpecBigInteger(const HcfPriKey *self, const AsyKeySpecItem item,
192 HcfBigInteger *returnBigInteger)
193 {
194 if (self == NULL || returnBigInteger == NULL) {
195 LOGE("Input params is invalid.");
196 return HCF_INVALID_PARAMS;
197 }
198 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) {
199 LOGE("Class not match");
200 return HCF_INVALID_PARAMS;
201 }
202 HcfOpensslRsaPriKey *impl = (HcfOpensslRsaPriKey *)self;
203 if (impl->sk == NULL) {
204 LOGE("Cannot use priKey after free");
205 return HCF_INVALID_PARAMS;
206 }
207 HcfResult ret = HCF_INVALID_PARAMS;
208 if (item == RSA_N_BN) {
209 const BIGNUM *n = OpensslRsaGet0N(impl->sk);
210 if (n == NULL) {
211 LOGD("[error] fail to get n");
212 return HCF_ERR_CRYPTO_OPERATION;
213 }
214 ret = BigNumToBigInteger(n, returnBigInteger);
215 if (ret != HCF_SUCCESS) {
216 LOGD("[error] fail get RSA Big Integer n");
217 return ret;
218 }
219 } else if (item == RSA_SK_BN) {
220 const BIGNUM *d = OpensslRsaGet0D(impl->sk);
221 if (d == NULL) {
222 LOGD("[error] fail to get sk");
223 return HCF_ERR_CRYPTO_OPERATION;
224 }
225 ret = BigNumToBigInteger(d, returnBigInteger);
226 if (ret != HCF_SUCCESS) {
227 LOGE("fail get RSA Big Integer d");
228 return ret;
229 }
230 } else {
231 LOGE("Invalid RSA pri key spec");
232 return HCF_INVALID_PARAMS;
233 }
234 return ret;
235 }
236
GetRsaPubKeySpecBigInteger(const HcfPubKey * self,const AsyKeySpecItem item,HcfBigInteger * returnBigInteger)237 static HcfResult GetRsaPubKeySpecBigInteger(const HcfPubKey *self, const AsyKeySpecItem item,
238 HcfBigInteger *returnBigInteger)
239 {
240 if (self == NULL || returnBigInteger == NULL) {
241 LOGE("Input params is invalid.");
242 return HCF_INVALID_PARAMS;
243 }
244 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PUBKEY_CLASS)) {
245 LOGE("Class not match");
246 return HCF_INVALID_PARAMS;
247 }
248 HcfOpensslRsaPubKey *impl = (HcfOpensslRsaPubKey *)self;
249 HcfResult ret = HCF_INVALID_PARAMS;
250 if (item == RSA_N_BN) {
251 const BIGNUM *n = OpensslRsaGet0N(impl->pk);
252 if (n == NULL) {
253 LOGD("[error] fail to get n");
254 return HCF_ERR_CRYPTO_OPERATION;
255 }
256 ret = BigNumToBigInteger(n, returnBigInteger);
257 if (ret != HCF_SUCCESS) {
258 LOGE("fail get RSA Big Integer n");
259 return ret;
260 }
261 } else if (item == RSA_PK_BN) {
262 const BIGNUM *e = OpensslRsaGet0E(impl->pk);
263 if (e == NULL) {
264 LOGD("[error] fail to get pk");
265 return HCF_ERR_CRYPTO_OPERATION;
266 }
267 ret = BigNumToBigInteger(e, returnBigInteger);
268 if (ret != HCF_SUCCESS) {
269 LOGE("fail get RSA Big Integer e");
270 return ret;
271 }
272 } else {
273 LOGE("Invalid RSA pub key spec");
274 return HCF_INVALID_PARAMS;
275 }
276 return ret;
277 }
278
DestroyPubKey(HcfObjectBase * self)279 static void DestroyPubKey(HcfObjectBase *self)
280 {
281 if (self == NULL) {
282 LOGE("PubKey is NULL.");
283 return;
284 }
285 if (!HcfIsClassMatch(self, OPENSSL_RSA_PUBKEY_CLASS)) {
286 LOGE("Class not match");
287 return;
288 }
289 HcfOpensslRsaPubKey *impl = (HcfOpensslRsaPubKey *)self;
290 OpensslRsaFree(impl->pk);
291 impl->pk = NULL;
292 HcfFree(self);
293 }
294
DestroyPriKey(HcfObjectBase * self)295 static void DestroyPriKey(HcfObjectBase *self)
296 {
297 if (self == NULL) {
298 LOGE("PubKey is NULL.");
299 return;
300 }
301 if (!HcfIsClassMatch(self, OPENSSL_RSA_PRIKEY_CLASS)) {
302 LOGE("Class not match");
303 return;
304 }
305 HcfOpensslRsaPriKey *impl = (HcfOpensslRsaPriKey*)self;
306 // RSA_free func will clear private information
307 OpensslRsaFree(impl->sk);
308 impl->sk = NULL;
309 HcfFree(self);
310 }
311
DestroyKeyPair(HcfObjectBase * self)312 static void DestroyKeyPair(HcfObjectBase *self)
313 {
314 if (self == NULL) {
315 LOGE("PubKey is NULL.");
316 return;
317 }
318 if (!HcfIsClassMatch(self, OPENSSL_RSA_KEYPAIR_CLASS)) {
319 LOGE("Class not match");
320 return;
321 }
322 HcfOpensslRsaKeyPair *impl = (HcfOpensslRsaKeyPair*)self;
323 if (impl->base.pubKey != NULL) {
324 DestroyPubKey((HcfObjectBase *)impl->base.pubKey);
325 impl->base.pubKey = NULL;
326 }
327 if (impl->base.priKey != NULL) {
328 DestroyPriKey((HcfObjectBase *)impl->base.priKey);
329 impl->base.priKey = NULL;
330 }
331 HcfFree(self);
332 }
333
CopyMemFromBIO(BIO * bio,HcfBlob * outBlob)334 static HcfResult CopyMemFromBIO(BIO *bio, HcfBlob *outBlob)
335 {
336 if (bio == NULL || outBlob == NULL) {
337 LOGE("Invalid input.");
338 return HCF_INVALID_PARAMS;
339 }
340 int len = BIO_pending(bio);
341 if (len < 0) {
342 LOGE("Bio len less than 0.");
343 return HCF_INVALID_PARAMS;
344 }
345 HcfBlob blob;
346 blob.len = len;
347 blob.data = (uint8_t *)HcfMalloc(sizeof(uint8_t) * len, 0);
348 if (blob.data == NULL) {
349 LOGE("Malloc mem for blob fail.");
350 return HCF_ERR_MALLOC;
351 }
352 if (OpensslBioRead(bio, blob.data, blob.len) <= 0) {
353 LOGD("[error] Bio read fail");
354 HcfPrintOpensslError();
355 HcfFree(blob.data);
356 return HCF_ERR_CRYPTO_OPERATION;
357 }
358 outBlob->len = blob.len;
359 outBlob->data = blob.data;
360 return HCF_SUCCESS;
361 }
362
CopyStrFromBIO(BIO * bio,char ** returnString)363 static HcfResult CopyStrFromBIO(BIO *bio, char **returnString)
364 {
365 if (bio == NULL || returnString == NULL) {
366 LOGE("Invalid input.");
367 return HCF_INVALID_PARAMS;
368 }
369 int len = BIO_pending(bio);
370 if (len < 0) {
371 LOGE("Bio len less than 0.");
372 return HCF_INVALID_PARAMS;
373 }
374 *returnString = (char *)HcfMalloc(len + 1, 0);
375 if (*returnString == NULL) {
376 LOGE("Malloc mem for blob fail.");
377 return HCF_ERR_MALLOC;
378 }
379 if (OpensslBioRead(bio, *returnString, len) <= 0) {
380 LOGE("Bio read fail");
381 HcfPrintOpensslError();
382 HcfFree(*returnString);
383 *returnString = NULL;
384 return HCF_ERR_CRYPTO_OPERATION;
385 }
386 return HCF_SUCCESS;
387 }
388
ConvertPubKeyFromX509(HcfBlob * x509Blob,RSA ** rsa)389 static HcfResult ConvertPubKeyFromX509(HcfBlob *x509Blob, RSA **rsa)
390 {
391 uint8_t *temp = x509Blob->data;
392 RSA *tempRsa = OpensslD2iRsaPubKey(NULL, (const unsigned char **)&temp, x509Blob->len);
393 if (tempRsa == NULL) {
394 LOGD("[error] d2i_RSA_PUBKEY fail.");
395 return HCF_ERR_CRYPTO_OPERATION;
396 }
397 *rsa = tempRsa;
398 return HCF_SUCCESS;
399 }
400
ConvertPriKeyFromPKCS8(HcfBlob * pkcs8Blob,RSA ** rsa)401 static HcfResult ConvertPriKeyFromPKCS8(HcfBlob *pkcs8Blob, RSA **rsa)
402 {
403 const unsigned char *temp = (const unsigned char *)pkcs8Blob->data;
404 EVP_PKEY *pKey = OpensslD2iAutoPrivateKey(NULL, &temp, pkcs8Blob->len);
405 if (pKey == NULL) {
406 LOGD("[error] d2i_AutoPrivateKey fail.");
407 HcfPrintOpensslError();
408 return HCF_ERR_CRYPTO_OPERATION;
409 }
410 RSA *tmpRsa = OpensslEvpPkeyGet1Rsa(pKey);
411 if (tmpRsa == NULL) {
412 LOGD("[error] EVP_PKEY_get1_RSA fail");
413 HcfPrintOpensslError();
414 OpensslEvpPkeyFree(pKey);
415 return HCF_ERR_CRYPTO_OPERATION;
416 }
417 *rsa = tmpRsa;
418 OpensslEvpPkeyFree(pKey);
419 return HCF_SUCCESS;
420 }
421
EncodePubKeyToX509(RSA * rsa,HcfBlob * returnBlob)422 static HcfResult EncodePubKeyToX509(RSA *rsa, HcfBlob *returnBlob)
423 {
424 unsigned char *tempData = NULL;
425 int len = OpensslI2dRsaPubKey(rsa, &tempData);
426 if (len <= 0) {
427 LOGD("[error] i2d_RSA_PUBKEY fail");
428 HcfPrintOpensslError();
429 return HCF_ERR_CRYPTO_OPERATION;
430 }
431 returnBlob->data = tempData;
432 returnBlob->len = len;
433 return HCF_SUCCESS;
434 }
435
EncodePriKeyToPKCS8(RSA * rsa,HcfBlob * returnBlob)436 static HcfResult EncodePriKeyToPKCS8(RSA *rsa, HcfBlob *returnBlob)
437 {
438 EVP_PKEY *pKey = NewEvpPkeyByRsa(rsa, true);
439 if (pKey == NULL) {
440 LOGD("[error] NewEvpPkeyByRsa fail.");
441 return HCF_ERR_CRYPTO_OPERATION;
442 }
443 HcfResult ret = HCF_SUCCESS;
444 BIO *bio = OpensslBioNew(OpensslBioSMem());
445 if (bio == NULL) {
446 LOGD("[error] BIO new fail.");
447 HcfPrintOpensslError();
448 ret = HCF_ERR_CRYPTO_OPERATION;
449 goto ERR2;
450 }
451 if (i2d_PKCS8PrivateKey_bio(bio, pKey, NULL, NULL, 0, NULL, NULL) != HCF_OPENSSL_SUCCESS) {
452 LOGD("[error] i2b_PrivateKey_bio fail.");
453 HcfPrintOpensslError();
454 ret = HCF_ERR_CRYPTO_OPERATION;
455 goto ERR1;
456 }
457 if (CopyMemFromBIO(bio, returnBlob) != HCF_SUCCESS) {
458 LOGD("[error] CopyMemFromBIO fail.");
459 ret = HCF_ERR_CRYPTO_OPERATION;
460 goto ERR1;
461 }
462 ERR1:
463 OpensslBioFreeAll(bio);
464 ERR2:
465 OpensslEvpPkeyFree(pKey);
466 return ret;
467 }
468
GetPubKeyEncoded(HcfKey * self,HcfBlob * returnBlob)469 static HcfResult GetPubKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
470 {
471 if (self == NULL || returnBlob == NULL) {
472 LOGE("Input params is invalid.");
473 return HCF_INVALID_PARAMS;
474 }
475 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PUBKEY_CLASS)) {
476 LOGE("Class not match.");
477 return HCF_INVALID_PARAMS;
478 }
479 HcfOpensslRsaPubKey *impl = (HcfOpensslRsaPubKey *)self;
480 return EncodePubKeyToX509(impl->pk, returnBlob);
481 }
482
GetPubKeyPkcs1Pem(RSA * pk,char ** returnString)483 static HcfResult GetPubKeyPkcs1Pem(RSA *pk, char **returnString)
484 {
485 BIO *bio = OpensslBioNew(OpensslBioSMem());
486 if (bio == NULL) {
487 LOGE("BIO new fail.");
488 HcfPrintOpensslError();
489 return HCF_ERR_CRYPTO_OPERATION;
490 }
491 int ret = OpensslPemWriteBioRsaPublicKey(bio, pk);
492 if (ret != HCF_OPENSSL_SUCCESS) {
493 LOGE("OpensslPemWriteBioRsaPublicKey fail.");
494 HcfPrintOpensslError();
495 OpensslBioFreeAll(bio);
496 return HCF_ERR_CRYPTO_OPERATION;
497 }
498 if (CopyStrFromBIO(bio, returnString) != HCF_SUCCESS) {
499 LOGE("CopyMemFromBIO fail.");
500 OpensslBioFreeAll(bio);
501 return HCF_ERR_CRYPTO_OPERATION;
502 }
503 OpensslBioFreeAll(bio);
504 return HCF_SUCCESS;
505 }
506
GetPubKeyX509Pem(RSA * pk,char ** returnString)507 static HcfResult GetPubKeyX509Pem(RSA *pk, char **returnString)
508 {
509 BIO *bio = OpensslBioNew(OpensslBioSMem());
510 if (bio == NULL) {
511 LOGE("BIO new fail.");
512 HcfPrintOpensslError();
513 return HCF_ERR_CRYPTO_OPERATION;
514 }
515 int ret = OpensslPemWriteBioRsaPubKey(bio, pk);
516 if (ret != HCF_OPENSSL_SUCCESS) {
517 LOGE("OpensslPemWriteBioRsaPubKey fail.");
518 HcfPrintOpensslError();
519 OpensslBioFreeAll(bio);
520 return HCF_ERR_CRYPTO_OPERATION;
521 }
522 if (CopyStrFromBIO(bio, returnString) != HCF_SUCCESS) {
523 LOGE("CopyMemFromBIO fail.");
524 OpensslBioFreeAll(bio);
525 return HCF_ERR_CRYPTO_OPERATION;
526 }
527 OpensslBioFreeAll(bio);
528 return HCF_SUCCESS;
529 }
530
GetPubKeyPem(const char * format,RSA * pk,char ** returnString)531 static HcfResult GetPubKeyPem(const char *format, RSA *pk, char **returnString)
532 {
533 HcfResult result;
534 if (strcmp(format, "PKCS1") == 0) {
535 result = GetPubKeyPkcs1Pem(pk, returnString);
536 if (result != HCF_SUCCESS) {
537 return result;
538 }
539 }
540 if (strcmp(format, "X509") == 0) {
541 result = GetPubKeyX509Pem(pk, returnString);
542 if (result != HCF_SUCCESS) {
543 return result;
544 }
545 }
546 return HCF_SUCCESS;
547 }
548
549
GetPubKeyEncodedPem(HcfKey * self,const char * format,char ** returnString)550 static HcfResult GetPubKeyEncodedPem(HcfKey *self, const char *format, char **returnString)
551 {
552 if (self == NULL || format == NULL|| returnString == NULL) {
553 LOGE("param is null.");
554 return HCF_INVALID_PARAMS;
555 }
556 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PUBKEY_CLASS)) {
557 LOGE("Class not match.");
558 return HCF_INVALID_PARAMS;
559 }
560 const char *outPutStruct = NULL;
561 if (strcmp(format, "PKCS1") == 0) {
562 outPutStruct = "pkcs1";
563 } else if (strcmp(format, "X509") == 0) {
564 outPutStruct = "subjectPublicKeyInfo";
565 } else {
566 LOGE("format is invalid.");
567 return HCF_INVALID_PARAMS;
568 }
569 HcfOpensslRsaPubKey *impl = (HcfOpensslRsaPubKey *)self;
570 EVP_PKEY *pkey = NewEvpPkeyByRsa(impl->pk, true);
571 if (pkey == NULL) {
572 LOGE("NewEvpPkeyByRsa failed.");
573 return HCF_ERR_CRYPTO_OPERATION;
574 }
575 HcfResult result = GetKeyEncodedPem(pkey, outPutStruct, EVP_PKEY_PUBLIC_KEY, returnString);
576 OpensslEvpPkeyFree(pkey);
577 if (result != HCF_SUCCESS) {
578 if (GetPubKeyPem(format, impl->pk, returnString) != HCF_SUCCESS) {
579 LOGE("GetPubKeyPem failed.");
580 return HCF_ERR_CRYPTO_OPERATION;
581 }
582 }
583 return HCF_SUCCESS;
584 }
585
GetPriKeyEncoded(HcfKey * self,HcfBlob * returnBlob)586 static HcfResult GetPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
587 {
588 if (self == NULL || returnBlob == NULL) {
589 LOGE("Key is null.");
590 return HCF_INVALID_PARAMS;
591 }
592
593 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) {
594 LOGE("Class not match.");
595 return HCF_INVALID_PARAMS;
596 }
597 HcfOpensslRsaPriKey *impl = (HcfOpensslRsaPriKey *)self;
598 const BIGNUM *p = NULL;
599 const BIGNUM *q = NULL;
600 OpensslRsaGet0Factors(impl->sk, &p, &q);
601 if (p == NULL || q == NULL) {
602 LOGD("[error] RSA private key missing p, q, not support to get encoded PK");
603 return HCF_NOT_SUPPORT;
604 }
605 return EncodePriKeyToPKCS8(impl->sk, returnBlob);
606 }
607
GetPrikeyPkcs8Pem(EVP_PKEY * pkey,const EVP_CIPHER * cipher,const char * passWord,char ** returnString)608 static HcfResult GetPrikeyPkcs8Pem(EVP_PKEY *pkey, const EVP_CIPHER *cipher, const char *passWord, char **returnString)
609 {
610 BIO *bio = OpensslBioNew(OpensslBioSMem());
611 if (bio == NULL) {
612 LOGE("BIO new fail.");
613 HcfPrintOpensslError();
614 return HCF_ERR_CRYPTO_OPERATION;
615 }
616
617 size_t passLen = 0;
618 if (passWord != NULL) {
619 passLen = strlen(passWord);
620 }
621
622 int ret = PEM_write_bio_PKCS8PrivateKey(bio, pkey, cipher, passWord, passLen, NULL, NULL);
623 if (ret != HCF_OPENSSL_SUCCESS) {
624 LOGE("OpensslPemWriteBioPkcs8PrivateKey fail.");
625 HcfPrintOpensslError();
626 OpensslBioFreeAll(bio);
627 return HCF_ERR_CRYPTO_OPERATION;
628 }
629 if (CopyStrFromBIO(bio, returnString) != HCF_SUCCESS) {
630 LOGE("CopyMemFromBIO fail.");
631 OpensslBioFreeAll(bio);
632 return HCF_ERR_CRYPTO_OPERATION;
633 }
634 OpensslBioFreeAll(bio);
635 return HCF_SUCCESS;
636 }
637
GetPrikeyPkcs1Pem(EVP_PKEY * pkey,const EVP_CIPHER * cipher,const char * passWord,char ** returnString)638 static HcfResult GetPrikeyPkcs1Pem(EVP_PKEY *pkey, const EVP_CIPHER *cipher, const char *passWord, char **returnString)
639 {
640 BIO *bio = OpensslBioNew(OpensslBioSMem());
641 if (bio == NULL) {
642 LOGE("BIO new fail.");
643 HcfPrintOpensslError();
644 return HCF_ERR_CRYPTO_OPERATION;
645 }
646
647 size_t passLen = 0;
648 if (passWord != NULL) {
649 passLen = strlen(passWord);
650 }
651
652 int ret = PEM_write_bio_PrivateKey_traditional(bio, pkey, cipher, (unsigned char *)passWord, passLen, NULL, NULL);
653 if (ret != HCF_OPENSSL_SUCCESS) {
654 LOGE("OpensslPemWriteBioRsaPrivateKey fail.");
655 HcfPrintOpensslError();
656 OpensslBioFreeAll(bio);
657 return HCF_ERR_CRYPTO_OPERATION;
658 }
659 if (CopyStrFromBIO(bio, returnString) != HCF_SUCCESS) {
660 LOGE("CopyStrFromBIO fail.");
661 OpensslBioFreeAll(bio);
662 return HCF_ERR_CRYPTO_OPERATION;
663 }
664 OpensslBioFreeAll(bio);
665 return HCF_SUCCESS;
666 }
667
GetPriKeyPem(const char * format,EVP_PKEY * pkey,const EVP_CIPHER * cipher,const char * passWord,char ** returnString)668 static HcfResult GetPriKeyPem(const char *format, EVP_PKEY *pkey, const EVP_CIPHER *cipher,
669 const char *passWord, char **returnString)
670 {
671 HcfResult result;
672 if (strcmp(format, "PKCS8") == 0) {
673 result = GetPrikeyPkcs8Pem(pkey, cipher, passWord, returnString);
674 if (result != HCF_SUCCESS) {
675 return result;
676 }
677 } else if (strcmp(format, "PKCS1") == 0) {
678 result = GetPrikeyPkcs1Pem(pkey, cipher, passWord, returnString);
679 if (result != HCF_SUCCESS) {
680 return result;
681 }
682 } else {
683 LOGE("format is invalid.");
684 return HCF_INVALID_PARAMS;
685 }
686 return HCF_SUCCESS;
687 }
688
ValidateInputParams(const HcfPriKey * self,const char * format,char ** returnString)689 static HcfResult ValidateInputParams(const HcfPriKey *self, const char *format, char **returnString)
690 {
691 if (self == NULL || format == NULL || returnString == NULL) {
692 LOGE("param is null.");
693 return HCF_INVALID_PARAMS;
694 }
695 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) {
696 LOGE("Class not match.");
697 return HCF_INVALID_PARAMS;
698 }
699 return HCF_SUCCESS;
700 }
701
GetPriKeyEncodedPem(const HcfPriKey * self,HcfParamsSpec * params,const char * format,char ** returnString)702 static HcfResult GetPriKeyEncodedPem(const HcfPriKey *self, HcfParamsSpec *params, const char *format,
703 char **returnString)
704 {
705 HcfResult result = ValidateInputParams(self, format, returnString);
706 if (result != HCF_SUCCESS) {
707 return result;
708 }
709
710 HcfOpensslRsaPriKey *impl = (HcfOpensslRsaPriKey *)self;
711 EVP_PKEY *pkey = NewEvpPkeyByRsa(impl->sk, true);
712 if (pkey == NULL) {
713 LOGE("NewEvpPkeyByRsa failed.");
714 return HCF_ERR_CRYPTO_OPERATION;
715 }
716
717 if (params != NULL) {
718 const EVP_CIPHER *cipher = NULL;
719 const char *passWord = NULL;
720 HcfKeyEncodingParamsSpec *spec = (HcfKeyEncodingParamsSpec *)params;
721 const char *cipherStr = (const char *)spec->cipher;
722 if (!IsCipherSupported(cipherStr)) {
723 LOGE("Cipher algorithm %s not supported", cipherStr);
724 OpensslEvpPkeyFree(pkey);
725 return HCF_NOT_SUPPORT;
726 }
727 cipher = EVP_CIPHER_fetch(NULL, cipherStr, NULL);
728 passWord = (const char *)spec->password;
729 result = GetPriKeyPem(format, pkey, cipher, passWord, returnString);
730 EVP_CIPHER_free((EVP_CIPHER *)cipher);
731 } else {
732 result = GetPriKeyPem(format, pkey, NULL, NULL, returnString);
733 }
734
735 if (result != HCF_SUCCESS) {
736 LOGE("GetPriKeyPem failed.");
737 OpensslEvpPkeyFree(pkey);
738 return result;
739 }
740 OpensslEvpPkeyFree(pkey);
741 return HCF_SUCCESS;
742 }
743
GetPubKeyFormat(HcfKey * self)744 static const char *GetPubKeyFormat(HcfKey *self)
745 {
746 if (self == NULL) {
747 LOGE("Invalid input parameter.");
748 return NULL;
749 }
750 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PUBKEY_CLASS)) {
751 return NULL;
752 }
753 return OPENSSL_RSA_PUBKEY_FORMAT;
754 }
755
GetPriKeyFormat(HcfKey * self)756 static const char *GetPriKeyFormat(HcfKey *self)
757 {
758 if (self == NULL) {
759 LOGE("Invalid input parameter.");
760 return NULL;
761 }
762 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) {
763 return NULL;
764 }
765 return OPENSSL_RSA_PRIKEY_FORMAT;
766 }
767
GetPriKeyAlgorithm(HcfKey * self)768 static const char *GetPriKeyAlgorithm(HcfKey *self)
769 {
770 if (self == NULL) {
771 LOGE("Invalid input parameter.");
772 return NULL;
773 }
774 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) {
775 return NULL;
776 }
777 return OPENSSL_RSA_ALGORITHM;
778 }
779
GetPubKeyAlgorithm(HcfKey * self)780 static const char *GetPubKeyAlgorithm(HcfKey *self)
781 {
782 if (self == NULL) {
783 LOGE("Invalid input parameter.");
784 return NULL;
785 }
786 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PUBKEY_CLASS)) {
787 return NULL;
788 }
789 return OPENSSL_RSA_ALGORITHM;
790 }
791
ClearPriKeyMem(HcfPriKey * self)792 static void ClearPriKeyMem(HcfPriKey *self)
793 {
794 if (self == NULL) {
795 LOGE("PriKey is NULL.");
796 return;
797 }
798 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) {
799 LOGE("Class not match");
800 return;
801 }
802 HcfOpensslRsaPriKey *impl = (HcfOpensslRsaPriKey *)self;
803 OpensslRsaFree(impl->sk);
804 impl->sk = NULL;
805 }
806
GetRsaPubKeyEncodedDer(const HcfPubKey * self,const char * format,HcfBlob * returnBlob)807 static HcfResult GetRsaPubKeyEncodedDer(const HcfPubKey *self, const char *format, HcfBlob *returnBlob)
808 {
809 (void)self;
810 (void)format;
811 (void)returnBlob;
812 return HCF_INVALID_PARAMS;
813 }
814
PackPubKey(RSA * rsaPubKey,HcfOpensslRsaPubKey ** retPubKey)815 static HcfResult PackPubKey(RSA *rsaPubKey, HcfOpensslRsaPubKey **retPubKey)
816 {
817 if (retPubKey == NULL || rsaPubKey == NULL) {
818 LOGE("Invalid params");
819 return HCF_INVALID_PARAMS;
820 }
821 *retPubKey = (HcfOpensslRsaPubKey *)HcfMalloc(sizeof(HcfOpensslRsaPubKey), 0);
822 if (*retPubKey == NULL) {
823 LOGE("Malloc retPubKey fail");
824 return HCF_ERR_MALLOC;
825 }
826 (*retPubKey)->pk = rsaPubKey;
827 (*retPubKey)->bits = (uint32_t)OpensslRsaBits(rsaPubKey);
828 (*retPubKey)->base.base.getAlgorithm = GetPubKeyAlgorithm;
829 (*retPubKey)->base.base.getEncoded = GetPubKeyEncoded;
830 (*retPubKey)->base.base.getEncodedPem = GetPubKeyEncodedPem;
831 (*retPubKey)->base.base.getFormat = GetPubKeyFormat;
832 (*retPubKey)->base.base.base.getClass = GetOpensslPubkeyClass;
833 (*retPubKey)->base.base.base.destroy = DestroyPubKey;
834 (*retPubKey)->base.getAsyKeySpecBigInteger = GetRsaPubKeySpecBigInteger;
835 (*retPubKey)->base.getAsyKeySpecString = GetRsaPubKeySpecString;
836 (*retPubKey)->base.getAsyKeySpecInt = GetRsaPubKeySpecInt;
837 (*retPubKey)->base.getEncodedDer = GetRsaPubKeyEncodedDer;
838 return HCF_SUCCESS;
839 }
840
841 // spec中,prikey只有n,e,d,没有p, q
PackPriKey(RSA * rsaPriKey,HcfOpensslRsaPriKey ** retPriKey)842 static HcfResult PackPriKey(RSA *rsaPriKey, HcfOpensslRsaPriKey **retPriKey)
843 {
844 if (retPriKey == NULL || rsaPriKey == NULL) {
845 LOGE("Invalid params");
846 return HCF_INVALID_PARAMS;
847 }
848 *retPriKey = (HcfOpensslRsaPriKey *)HcfMalloc(sizeof(HcfOpensslRsaPriKey), 0);
849 if (*retPriKey == NULL) {
850 LOGE("Malloc retPriKey fail");
851 return HCF_ERR_MALLOC;
852 }
853 (*retPriKey)->sk = rsaPriKey;
854 (*retPriKey)->bits = (uint32_t)OpensslRsaBits(rsaPriKey);
855 (*retPriKey)->base.clearMem = ClearPriKeyMem;
856 (*retPriKey)->base.base.getAlgorithm = GetPriKeyAlgorithm;
857 (*retPriKey)->base.base.getEncoded = GetPriKeyEncoded;
858 (*retPriKey)->base.getEncodedPem = GetPriKeyEncodedPem;
859 (*retPriKey)->base.base.getFormat = GetPriKeyFormat;
860 (*retPriKey)->base.base.base.getClass = GetOpensslPrikeyClass;
861 (*retPriKey)->base.base.base.destroy = DestroyPriKey;
862 (*retPriKey)->base.getAsyKeySpecBigInteger = GetRsaPriKeySpecBigInteger;
863 (*retPriKey)->base.getAsyKeySpecString = GetRsaPriKeySpecString;
864 (*retPriKey)->base.getAsyKeySpecInt = GetRsaPriKeySpecInt;
865 (*retPriKey)->base.getEncodedDer = GetRsaPriKeyEncodedDer;
866 return HCF_SUCCESS;
867 }
868
DuplicatePkAndSkFromRSA(RSA * rsa,RSA ** pubKey,RSA ** priKey)869 static HcfResult DuplicatePkAndSkFromRSA(RSA *rsa, RSA **pubKey, RSA **priKey)
870 {
871 if (rsa == NULL) {
872 LOGE("Rsa is NULL.");
873 return HCF_INVALID_PARAMS;
874 }
875 if (DuplicateRsa(rsa, false, pubKey) != HCF_SUCCESS) {
876 LOGD("[error] Duplicate pubkey rsa fail");
877 return HCF_ERR_CRYPTO_OPERATION;
878 }
879 if (DuplicateRsa(rsa, true, priKey) != HCF_SUCCESS) {
880 LOGD("[error] Duplicate prikey rsa fail");
881 OpensslRsaFree(*pubKey);
882 *pubKey = NULL;
883 return HCF_ERR_CRYPTO_OPERATION;
884 }
885 return HCF_SUCCESS;
886 }
887
PackKeyPair(RSA * rsa,uint32_t realBits,HcfOpensslRsaKeyPair ** retKeyPair)888 static HcfResult PackKeyPair(RSA *rsa, uint32_t realBits, HcfOpensslRsaKeyPair **retKeyPair)
889 {
890 if (retKeyPair == NULL || rsa == NULL) {
891 LOGE("Invalid params");
892 return HCF_INVALID_PARAMS;
893 }
894 RSA *pubKey = NULL;
895 RSA *priKey = NULL;
896 if (DuplicatePkAndSkFromRSA(rsa, &pubKey, &priKey) != HCF_SUCCESS) {
897 LOGD("[error] DuplicatePkAndSkFromRSA fail");
898 return HCF_ERR_CRYPTO_OPERATION;
899 }
900 HcfResult ret = HCF_SUCCESS;
901 *retKeyPair = (HcfOpensslRsaKeyPair *)HcfMalloc(sizeof(HcfOpensslRsaKeyPair), 0);
902 if (*retKeyPair == NULL) {
903 LOGE("Malloc keypair fail");
904 OpensslRsaFree(pubKey);
905 OpensslRsaFree(priKey);
906 return HCF_ERR_MALLOC;
907 }
908 HcfOpensslRsaPriKey *priKeyImpl = NULL;
909 HcfOpensslRsaPubKey *pubKeyImpl = NULL;
910 ret = PackPubKey(pubKey, &pubKeyImpl);
911 if (ret != HCF_SUCCESS) {
912 LOGE("Pack pubKey fail.");
913 goto ERR2;
914 }
915 ret = PackPriKey(priKey, &priKeyImpl);
916 if (ret != HCF_SUCCESS) {
917 LOGE("Pack priKey fail.");
918 goto ERR1;
919 }
920 (*retKeyPair)->base.priKey = (HcfPriKey *)priKeyImpl;
921 (*retKeyPair)->base.pubKey = (HcfPubKey *)pubKeyImpl;
922 (*retKeyPair)->base.base.getClass = GetOpensslKeyPairClass;
923 (*retKeyPair)->base.base.destroy = DestroyKeyPair;
924 return HCF_SUCCESS;
925 ERR1:
926 HcfFree(pubKeyImpl);
927 ERR2:
928 OpensslRsaFree(pubKey);
929 OpensslRsaFree(priKey);
930 HcfFree(*retKeyPair);
931 *retKeyPair = NULL;
932 return ret;
933 }
934
GetRealPrimes(int32_t primesFlag)935 static int32_t GetRealPrimes(int32_t primesFlag)
936 {
937 switch (primesFlag) {
938 case OPENSSL_RSA_PRIMES_SIZE_2:
939 return PRIMES_2;
940 case OPENSSL_RSA_PRIMES_SIZE_3:
941 return PRIMES_3;
942 case OPENSSL_RSA_PRIMES_SIZE_4:
943 return PRIMES_4;
944 case OPENSSL_RSA_PRIMES_SIZE_5:
945 return PRIMES_5;
946 default:
947 LOGD("set default primes 2");
948 return PRIMES_2;
949 }
950 }
951
GenerateKeyPair(HcfAsyKeyGenSpiRsaParams * params,HcfKeyPair ** keyPair)952 static HcfResult GenerateKeyPair(HcfAsyKeyGenSpiRsaParams *params, HcfKeyPair **keyPair)
953 {
954 // check input params is valid
955 HcfResult res = CheckRsaKeyGenParams(params);
956 if (res != HCF_SUCCESS) {
957 LOGE("Rsa CheckRsaKeyGenParams fail.");
958 return HCF_INVALID_PARAMS;
959 }
960 // Generate keyPair RSA
961 RSA *rsa = OpensslRsaNew();
962 if (rsa == NULL) {
963 LOGE("new RSA fail.");
964 return HCF_ERR_MALLOC;
965 }
966 LOGD("keygen bits is %d, primes is %d", params->bits, GetRealPrimes(params->primes));
967 if (GetRealPrimes(params->primes) != OPENSSL_RSA_KEYGEN_DEFAULT_PRIMES) {
968 if (RSA_generate_multi_prime_key(rsa, params->bits, GetRealPrimes(params->primes), params->pubExp, NULL)
969 != HCF_OPENSSL_SUCCESS) {
970 LOGD("[error] Generate multi-primes rsa key fail");
971 HcfPrintOpensslError();
972 OpensslRsaFree(rsa);
973 return HCF_ERR_CRYPTO_OPERATION;
974 }
975 } else {
976 if (RSA_generate_key_ex(rsa, params->bits, params->pubExp, NULL) != HCF_OPENSSL_SUCCESS) {
977 LOGD("[error] Generate rsa key fail");
978 HcfPrintOpensslError();
979 OpensslRsaFree(rsa);
980 return HCF_ERR_CRYPTO_OPERATION;
981 }
982 }
983
984 // devided to pk and sk;
985 HcfOpensslRsaKeyPair *keyPairImpl = NULL;
986 res = PackKeyPair(rsa, params->bits, &keyPairImpl);
987 if (res != HCF_SUCCESS) {
988 LOGE("Generate keyPair fail.");
989 OpensslRsaFree(rsa);
990 return res;
991 }
992 *keyPair = (HcfKeyPair *)keyPairImpl;
993 OpensslRsaFree(rsa);
994 LOGD("Generate keypair success.");
995 return res;
996 }
997
EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi * self,HcfKeyPair ** keyPair)998 static HcfResult EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **keyPair)
999 {
1000 if (self == NULL || keyPair == NULL) {
1001 LOGE("Invalid params.");
1002 return HCF_INVALID_PARAMS;
1003 }
1004 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1005 LOGE("Class not match.");
1006 return HCF_INVALID_PARAMS;
1007 }
1008 HcfAsyKeyGeneratorSpiRsaOpensslImpl *impl = (HcfAsyKeyGeneratorSpiRsaOpensslImpl *)self;
1009 return GenerateKeyPair(impl->params, keyPair);
1010 }
1011
GetKeyGeneratorClass(void)1012 static const char *GetKeyGeneratorClass(void)
1013 {
1014 return OPENSSL_RSA_GENERATOR_CLASS;
1015 }
1016
DestroyKeyGeneratorSpiImpl(HcfObjectBase * self)1017 static void DestroyKeyGeneratorSpiImpl(HcfObjectBase *self)
1018 {
1019 if (self == NULL) {
1020 LOGE("DestroyKeyGeneratorSpiImpl is null");
1021 return;
1022 }
1023 if (!HcfIsClassMatch(self, OPENSSL_RSA_GENERATOR_CLASS)) {
1024 LOGE("Class not match.");
1025 return;
1026 }
1027 // destroy pubExp first.
1028 HcfAsyKeyGeneratorSpiRsaOpensslImpl *impl = (HcfAsyKeyGeneratorSpiRsaOpensslImpl *)self;
1029 if (impl->params != NULL && impl->params->pubExp != NULL) {
1030 OpensslBnFree(impl->params->pubExp);
1031 }
1032 HcfFree(impl->params);
1033 impl->params = NULL;
1034 HcfFree(self);
1035 }
1036
ConvertPubKey(HcfBlob * pubKeyBlob,HcfOpensslRsaPubKey ** pubkeyRet)1037 static HcfResult ConvertPubKey(HcfBlob *pubKeyBlob, HcfOpensslRsaPubKey **pubkeyRet)
1038 {
1039 RSA *rsaPk = NULL;
1040 if (ConvertPubKeyFromX509(pubKeyBlob, &rsaPk) != HCF_SUCCESS) {
1041 LOGD("[error] Convert pubKey from X509 fail.");
1042 return HCF_ERR_CRYPTO_OPERATION;
1043 }
1044 HcfOpensslRsaPubKey *pubKey = NULL;
1045 HcfResult ret = PackPubKey(rsaPk, &pubKey);
1046 if (ret != HCF_SUCCESS) {
1047 LOGD("[error] PackPubKey fail");
1048 goto ERR;
1049 }
1050 *pubkeyRet = pubKey;
1051 return ret;
1052 ERR:
1053 OpensslRsaFree(rsaPk);
1054 return ret;
1055 }
1056
ConvertPemKeyToKey(const char * keyStr,HcfParamsSpec * params,int selection,RSA ** rsa)1057 static HcfResult ConvertPemKeyToKey(const char *keyStr, HcfParamsSpec *params, int selection, RSA **rsa)
1058 {
1059 EVP_PKEY *pkey = NULL;
1060 OSSL_DECODER_CTX *ctx = OpensslOsslDecoderCtxNewForPkey(&pkey, "PEM", NULL, "RSA", selection, NULL, NULL);
1061 if (ctx == NULL) {
1062 LOGE("OpensslOsslDecoderCtxNewForPkey fail.");
1063 HcfPrintOpensslError();
1064 return HCF_ERR_CRYPTO_OPERATION;
1065 }
1066 if (params != NULL) {
1067 HcfKeyDecodingParamsSpec *spec = (HcfKeyDecodingParamsSpec *)params;
1068 const unsigned char *passWd = (const unsigned char *)spec->password;
1069 if (OpensslOsslDecoderCtxSetPassPhrase(ctx, passWd, strlen(spec->password)) != HCF_OPENSSL_SUCCESS) {
1070 HcfPrintOpensslError();
1071 OpensslOsslDecoderCtxFree(ctx);
1072 OpensslEvpPkeyFree(pkey);
1073 return HCF_ERR_CRYPTO_OPERATION;
1074 }
1075 }
1076 size_t pdataLen = strlen(keyStr);
1077 const unsigned char *pdata = (const unsigned char *)keyStr;
1078 int ret = OpensslOsslDecoderFromData(ctx, &pdata, &pdataLen);
1079 OpensslOsslDecoderCtxFree(ctx);
1080 if (ret != HCF_OPENSSL_SUCCESS) {
1081 LOGE("OpensslOsslDecoderFromData failed.");
1082 HcfPrintOpensslError();
1083 OpensslEvpPkeyFree(pkey);
1084 return HCF_ERR_CRYPTO_OPERATION;
1085 }
1086 *rsa = OpensslEvpPkeyGet1Rsa(pkey);
1087 OpensslEvpPkeyFree(pkey);
1088 if (*rsa == NULL) {
1089 LOGE("OpensslEvpPkeyGet1Rsa fail.");
1090 HcfPrintOpensslError();
1091 return HCF_ERR_CRYPTO_OPERATION;
1092 }
1093 return HCF_SUCCESS;
1094 }
1095
ConvertPemPubKey(const char * pubKeyStr,int selection,HcfOpensslRsaPubKey ** pubKeyRet)1096 static HcfResult ConvertPemPubKey(const char *pubKeyStr, int selection, HcfOpensslRsaPubKey **pubKeyRet)
1097 {
1098 RSA *rsaPk = NULL;
1099 HcfResult ret;
1100 ret = ConvertPemKeyToKey(pubKeyStr, NULL, selection, &rsaPk);
1101 if (ret != HCF_SUCCESS) {
1102 LOGE("ConvertPemKeyToKey failed.");
1103 return ret;
1104 }
1105
1106 HcfOpensslRsaPubKey *pubKey = NULL;
1107 HcfResult result = PackPubKey(rsaPk, &pubKey);
1108 if (result != HCF_SUCCESS) {
1109 LOGE("PackPubKey fail.");
1110 OpensslRsaFree(rsaPk);
1111 return result;
1112 }
1113 *pubKeyRet = pubKey;
1114 return HCF_SUCCESS;
1115 }
1116
ConvertPriKey(HcfBlob * priKeyBlob,HcfOpensslRsaPriKey ** priKeyRet)1117 static HcfResult ConvertPriKey(HcfBlob *priKeyBlob, HcfOpensslRsaPriKey **priKeyRet)
1118 {
1119 RSA *rsaSk = NULL;
1120 if (ConvertPriKeyFromPKCS8(priKeyBlob, &rsaSk) != HCF_SUCCESS) {
1121 LOGE("ConvertPriKeyFromPKCS8 fail.");
1122 return HCF_ERR_MALLOC;
1123 }
1124 HcfOpensslRsaPriKey *priKey = NULL;
1125 HcfResult ret = PackPriKey(rsaSk, &priKey);
1126 if (ret != HCF_SUCCESS) {
1127 LOGD("[error] PackPriKey fail");
1128 goto ERR;
1129 }
1130 *priKeyRet = priKey;
1131 return ret;
1132 ERR:
1133 OpensslRsaFree(rsaSk);
1134 return ret;
1135 }
1136
ConvertPemPriKey(const char * priKeyStr,HcfParamsSpec * params,int selection,HcfOpensslRsaPriKey ** priKeyRet)1137 static HcfResult ConvertPemPriKey(const char *priKeyStr, HcfParamsSpec *params, int selection,
1138 HcfOpensslRsaPriKey **priKeyRet)
1139 {
1140 RSA *rsaSk = NULL;
1141 HcfResult ret;
1142 ret = ConvertPemKeyToKey(priKeyStr, params, selection, &rsaSk);
1143 if (ret != HCF_SUCCESS) {
1144 LOGE("ConvertPemKeyToKey failed.");
1145 return ret;
1146 }
1147 HcfOpensslRsaPriKey *priKey = NULL;
1148 HcfResult result = PackPriKey(rsaSk, &priKey);
1149 if (result != HCF_SUCCESS) {
1150 LOGE("PackPriKey fail.");
1151 OpensslRsaFree(rsaSk);
1152 return result;
1153 }
1154 *priKeyRet = priKey;
1155 return HCF_SUCCESS;
1156 }
1157
EngineConvertKey(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,HcfBlob * pubKeyBlob,HcfBlob * priKeyBlob,HcfKeyPair ** returnKeyPair)1158 static HcfResult EngineConvertKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
1159 HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
1160 {
1161 (void)params;
1162 if ((self == NULL) || (returnKeyPair == NULL) || ((pubKeyBlob == NULL) && (priKeyBlob == NULL))) {
1163 LOGE("ConvertKeyParams is invalid.");
1164 return HCF_INVALID_PARAMS;
1165 }
1166 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1167 LOGE("Class not match.");
1168 return HCF_INVALID_PARAMS;
1169 }
1170
1171 HcfOpensslRsaPubKey *pubKey = NULL;
1172 if ((pubKeyBlob != NULL) && (pubKeyBlob->len != 0) && (pubKeyBlob->data != NULL)) {
1173 if (ConvertPubKey(pubKeyBlob, &pubKey) != HCF_SUCCESS) {
1174 LOGE("convert pubkey fail.");
1175 return HCF_INVALID_PARAMS;
1176 }
1177 }
1178
1179 HcfOpensslRsaPriKey *priKey = NULL;
1180 if ((priKeyBlob != NULL) && (priKeyBlob->len != 0) && (priKeyBlob->data != NULL)) {
1181 if (ConvertPriKey(priKeyBlob, &priKey) != HCF_SUCCESS) {
1182 LOGE("convert prikey fail.");
1183 HcfObjDestroy((HcfObjectBase *)pubKey);
1184 return HCF_INVALID_PARAMS;
1185 }
1186 }
1187
1188 if (pubKey == NULL && priKey == NULL) {
1189 LOGE("Convert key failed with invalid blob");
1190 return HCF_INVALID_PARAMS;
1191 }
1192
1193 HcfOpensslRsaKeyPair *keyPair = (HcfOpensslRsaKeyPair *)HcfMalloc(sizeof(HcfOpensslRsaKeyPair), 0);
1194 if (keyPair == NULL) {
1195 LOGE("Malloc keyPair fail.");
1196 HcfObjDestroy((HcfObjectBase *)pubKey);
1197 HcfObjDestroy((HcfObjectBase *)priKey);
1198 return HCF_ERR_MALLOC;
1199 }
1200
1201 keyPair->base.priKey = (HcfPriKey *)priKey;
1202 keyPair->base.pubKey = (HcfPubKey *)pubKey;
1203 keyPair->base.base.getClass = GetOpensslKeyPairClass;
1204 keyPair->base.base.destroy = DestroyKeyPair;
1205 *returnKeyPair = (HcfKeyPair *)keyPair;
1206 return HCF_SUCCESS;
1207 }
1208
EngineConvertPemKey(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,const char * pubKeyStr,const char * priKeyStr,HcfKeyPair ** returnKeyPair)1209 static HcfResult EngineConvertPemKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, const char *pubKeyStr,
1210 const char *priKeyStr, HcfKeyPair **returnKeyPair)
1211 {
1212 if ((self == NULL) || (returnKeyPair == NULL) || ((pubKeyStr == NULL) && (priKeyStr == NULL))) {
1213 LOGE("ConvertPemKeyParams is invalid.");
1214 return HCF_INVALID_PARAMS;
1215 }
1216 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1217 LOGE("Class not match.");
1218 return HCF_INVALID_PARAMS;
1219 }
1220 HcfOpensslRsaPubKey *pubKey = NULL;
1221 if (pubKeyStr != NULL && strlen(pubKeyStr) != 0) {
1222 if (ConvertPemPubKey(pubKeyStr, EVP_PKEY_PUBLIC_KEY, &pubKey) != HCF_SUCCESS) {
1223 LOGE("convert pubkey fail.");
1224 return HCF_ERR_CRYPTO_OPERATION;
1225 }
1226 }
1227 HcfOpensslRsaPriKey *priKey = NULL;
1228 if (priKeyStr != NULL && strlen(priKeyStr) != 0) {
1229 if (ConvertPemPriKey(priKeyStr, params, EVP_PKEY_KEYPAIR, &priKey) != HCF_SUCCESS) {
1230 LOGE("convert prikey fail.");
1231 HcfObjDestroy((HcfObjectBase *)pubKey);
1232 return HCF_ERR_CRYPTO_OPERATION;
1233 }
1234 }
1235
1236 HcfOpensslRsaKeyPair *keyPair = (HcfOpensslRsaKeyPair *)HcfMalloc(sizeof(HcfOpensslRsaKeyPair), 0);
1237 if (keyPair == NULL) {
1238 LOGE("Malloc keyPair fail.");
1239 HcfObjDestroy((HcfObjectBase *)pubKey);
1240 HcfObjDestroy((HcfObjectBase *)priKey);
1241 return HCF_ERR_MALLOC;
1242 }
1243 keyPair->base.priKey = (HcfPriKey *)priKey;
1244 keyPair->base.pubKey = (HcfPubKey *)pubKey;
1245 keyPair->base.base.getClass = GetOpensslKeyPairClass;
1246 keyPair->base.base.destroy = DestroyKeyPair;
1247 *returnKeyPair = (HcfKeyPair *)keyPair;
1248 return HCF_SUCCESS;
1249 }
1250
ParseRsaBnFromBin(const HcfAsyKeyParamsSpec * paramsSpec,BIGNUM ** n,BIGNUM ** e,BIGNUM ** d)1251 static HcfResult ParseRsaBnFromBin(const HcfAsyKeyParamsSpec *paramsSpec, BIGNUM **n,
1252 BIGNUM **e, BIGNUM **d)
1253 {
1254 // when meeting the fail situation, the BIGNUM will be NULL and other BIGNUM will be freeed in InitRsaStructByBin();
1255 if (BigIntegerToBigNum(&((HcfRsaCommParamsSpec *)paramsSpec)->n, n) != HCF_SUCCESS) {
1256 LOGD("[error] Rsa new BN n fail.");
1257 return HCF_ERR_CRYPTO_OPERATION;
1258 }
1259 if (paramsSpec->specType == HCF_KEY_PAIR_SPEC) {
1260 if (BigIntegerToBigNum(&((HcfRsaKeyPairParamsSpec *)paramsSpec)->pk, e) != HCF_SUCCESS) {
1261 LOGD("[error] Rsa new BN e fail.");
1262 OpensslBnFree(*n);
1263 *n = NULL;
1264 return HCF_ERR_CRYPTO_OPERATION;
1265 }
1266 if (BigIntegerToBigNum(&((HcfRsaKeyPairParamsSpec *)paramsSpec)->sk, d) != HCF_SUCCESS) {
1267 LOGD("[error] Rsa new BN d fail.");
1268 OpensslBnFree(*n);
1269 *n = NULL;
1270 OpensslBnFree(*e);
1271 *e = NULL;
1272 return HCF_ERR_CRYPTO_OPERATION;
1273 }
1274 }
1275 if (paramsSpec->specType == HCF_PUBLIC_KEY_SPEC) {
1276 if (BigIntegerToBigNum(&((HcfRsaPubKeyParamsSpec *)paramsSpec)->pk, e) != HCF_SUCCESS) {
1277 LOGD("[error] Rsa new BN e fail.");
1278 OpensslBnFree(*n);
1279 *n = NULL;
1280 return HCF_ERR_CRYPTO_OPERATION;
1281 }
1282 }
1283 return HCF_SUCCESS;
1284 }
1285
InitRsaStructByBin(const HcfAsyKeyParamsSpec * paramsSpec)1286 static RSA *InitRsaStructByBin(const HcfAsyKeyParamsSpec *paramsSpec)
1287 {
1288 BIGNUM *n = NULL;
1289 BIGNUM *e = NULL;
1290 BIGNUM *d = NULL;
1291 RSA *rsa = NULL;
1292
1293 if (ParseRsaBnFromBin(paramsSpec, &n, &e, &d) != HCF_SUCCESS) {
1294 LOGD("[error] ParseRsaBnFromBin fail");
1295 return rsa;
1296 }
1297 rsa = OpensslRsaNew();
1298 if (rsa == NULL) {
1299 OpensslBnFree(n);
1300 OpensslBnFree(e);
1301 OpensslBnClearFree(d);
1302 LOGD("[error] new RSA fail");
1303 return rsa;
1304 }
1305 // if set0 success, RSA object will take the owner of n, e, d and will free them.
1306 // as a new RSA object, in RSA_set0_key(), n and e cannot be NULL.
1307 if (OpensslRsaSet0Key(rsa, n, e, d) != HCF_OPENSSL_SUCCESS) {
1308 LOGD("[error] set RSA fail");
1309 HcfPrintOpensslError();
1310 OpensslBnFree(n);
1311 OpensslBnFree(e);
1312 OpensslBnClearFree(d);
1313 OpensslRsaFree(rsa);
1314 rsa = NULL;
1315 return rsa;
1316 }
1317 return rsa;
1318 }
1319
GenerateKeyPairBySpec(const HcfAsyKeyParamsSpec * paramsSpec,HcfKeyPair ** keyPair)1320 static HcfResult GenerateKeyPairBySpec(const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **keyPair)
1321 {
1322 // Generate keyPair RSA by spec
1323 RSA *rsa = InitRsaStructByBin(paramsSpec);
1324 if (rsa == NULL) {
1325 LOGD("[error] Generate RSA fail.");
1326 return HCF_ERR_CRYPTO_OPERATION;
1327 }
1328 HcfOpensslRsaKeyPair *keyPairImpl = (HcfOpensslRsaKeyPair *)HcfMalloc(sizeof(HcfOpensslRsaKeyPair), 0);
1329 if (keyPairImpl == NULL) {
1330 LOGE("Malloc keyPair fail.");
1331 OpensslRsaFree(rsa);
1332 return HCF_ERR_MALLOC;
1333 }
1334 // devided to pk and sk;
1335 HcfOpensslRsaPubKey *pubKeyImpl = NULL;
1336 HcfOpensslRsaPriKey *priKeyImpl = NULL;
1337
1338 RSA *pubKeyRsa = NULL;
1339 if (DuplicateRsa(rsa, false, &pubKeyRsa) != HCF_SUCCESS) {
1340 LOGD("[error] Duplicate pubKey rsa fail");
1341 OpensslRsaFree(rsa);
1342 HcfFree(keyPairImpl);
1343 return HCF_ERR_CRYPTO_OPERATION;
1344 }
1345
1346 HcfResult res = PackPubKey(pubKeyRsa, &pubKeyImpl);
1347 if (res != HCF_SUCCESS) {
1348 LOGE("pack pup key fail.");
1349 OpensslRsaFree(rsa);
1350 OpensslRsaFree(pubKeyRsa);
1351 HcfFree(keyPairImpl);
1352 return res;
1353 }
1354
1355 res = PackPriKey(rsa, &priKeyImpl);
1356 if (res != HCF_SUCCESS) {
1357 LOGE("pack pri key fail.");
1358 OpensslRsaFree(rsa);
1359 OpensslRsaFree(pubKeyRsa);
1360 HcfFree(keyPairImpl);
1361 HcfFree(pubKeyImpl);
1362 return res;
1363 }
1364 keyPairImpl->base.priKey = (HcfPriKey *)priKeyImpl;
1365 keyPairImpl->base.pubKey = (HcfPubKey *)pubKeyImpl;
1366 keyPairImpl->base.base.getClass = GetOpensslKeyPairClass;
1367 keyPairImpl->base.base.destroy = DestroyKeyPair;
1368 *keyPair = (HcfKeyPair *)keyPairImpl;
1369 LOGD("Generate keypair success.");
1370 return res;
1371 }
1372
GeneratePubKeyBySpec(const HcfAsyKeyParamsSpec * paramsSpec,HcfPubKey ** pubKey)1373 static HcfResult GeneratePubKeyBySpec(const HcfAsyKeyParamsSpec *paramsSpec, HcfPubKey **pubKey)
1374 {
1375 RSA *rsa = InitRsaStructByBin(paramsSpec);
1376 if (rsa == NULL) {
1377 LOGD("[error] Generate RSA fail.");
1378 return HCF_ERR_CRYPTO_OPERATION;
1379 }
1380 RSA *pubKeyRsa = NULL;
1381 if (DuplicateRsa(rsa, false, &pubKeyRsa) != HCF_SUCCESS) {
1382 LOGD("[error] Duplicate pubKey rsa fail");
1383 OpensslRsaFree(rsa);
1384 return HCF_ERR_CRYPTO_OPERATION;
1385 }
1386 HcfOpensslRsaPubKey *pubKeyImpl = NULL;
1387 HcfResult res = PackPubKey(pubKeyRsa, &pubKeyImpl);
1388 if (res != HCF_SUCCESS) {
1389 LOGD("[error] pack pup key fail.");
1390 OpensslRsaFree(rsa);
1391 OpensslRsaFree(pubKeyRsa);
1392 return res;
1393 }
1394 *pubKey = (HcfPubKey *)pubKeyImpl;
1395 OpensslRsaFree(rsa);
1396 LOGD("Generate pub key success.");
1397 return res;
1398 }
1399
GeneratePriKeyBySpec(const HcfAsyKeyParamsSpec * paramsSpec,HcfPriKey ** priKey)1400 static HcfResult GeneratePriKeyBySpec(const HcfAsyKeyParamsSpec *paramsSpec, HcfPriKey **priKey)
1401 {
1402 RSA *rsa = InitRsaStructByBin(paramsSpec);
1403 if (rsa == NULL) {
1404 LOGD("[error] Generate RSA fail.");
1405 return HCF_ERR_CRYPTO_OPERATION;
1406 }
1407 HcfOpensslRsaPriKey *priKeyImpl = NULL;
1408 HcfResult res = PackPriKey(rsa, &priKeyImpl);
1409 if (res != HCF_SUCCESS) {
1410 LOGD("[error] pack pri key fail.");
1411 OpensslRsaFree(rsa);
1412 return res;
1413 }
1414 *priKey = (HcfPriKey *)priKeyImpl;
1415 LOGD("Generate pri key success.");
1416 return res;
1417 }
1418
EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)1419 static HcfResult EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi *self,
1420 const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
1421 {
1422 if ((self == NULL) || (returnKeyPair == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL)) {
1423 LOGE("GenerateKeyPairBySpec Params is invalid.");
1424 return HCF_INVALID_PARAMS;
1425 }
1426 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1427 LOGE("Class not match.");
1428 return HCF_INVALID_PARAMS;
1429 }
1430 if (strcmp(paramsSpec->algName, RSA_ALG_NAME) != 0) {
1431 LOGE("Spec alg not match.");
1432 return HCF_INVALID_PARAMS;
1433 }
1434 if (paramsSpec->specType != HCF_KEY_PAIR_SPEC) {
1435 LOGE("Spec type not match.");
1436 return HCF_INVALID_PARAMS;
1437 }
1438 return GenerateKeyPairBySpec(paramsSpec, returnKeyPair);
1439 }
1440
EngineGeneratePubKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPubKey ** returnPubKey)1441 static HcfResult EngineGeneratePubKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
1442 const HcfAsyKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey)
1443 {
1444 if ((self == NULL) || (returnPubKey == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL)) {
1445 LOGE("GeneratePubKeyBySpec Params is invalid.");
1446 return HCF_INVALID_PARAMS;
1447 }
1448 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1449 LOGE("Class not match.");
1450 return HCF_INVALID_PARAMS;
1451 }
1452 if (strcmp(paramsSpec->algName, RSA_ALG_NAME) != 0) {
1453 LOGE("Spec alg not match.");
1454 return HCF_INVALID_PARAMS;
1455 }
1456 if (paramsSpec->specType != HCF_PUBLIC_KEY_SPEC && paramsSpec->specType != HCF_KEY_PAIR_SPEC) {
1457 LOGE("Spec not match.");
1458 return HCF_INVALID_PARAMS;
1459 }
1460 return GeneratePubKeyBySpec(paramsSpec, returnPubKey);
1461 }
1462
EngineGeneratePriKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPriKey ** returnPriKey)1463 static HcfResult EngineGeneratePriKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
1464 const HcfAsyKeyParamsSpec *paramsSpec, HcfPriKey **returnPriKey)
1465 {
1466 if ((self == NULL) || (returnPriKey == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL)) {
1467 LOGE("GeneratePriKeyBySpec Params is invalid.");
1468 return HCF_INVALID_PARAMS;
1469 }
1470 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1471 LOGE("Class not match.");
1472 return HCF_INVALID_PARAMS;
1473 }
1474 if (strcmp(paramsSpec->algName, RSA_ALG_NAME) != 0) {
1475 LOGE("Spec alg not match.");
1476 return HCF_INVALID_PARAMS;
1477 }
1478 if (paramsSpec->specType != HCF_KEY_PAIR_SPEC) {
1479 LOGE("Spec not match.");
1480 return HCF_INVALID_PARAMS;
1481 }
1482 return GeneratePriKeyBySpec(paramsSpec, returnPriKey);
1483 }
1484
SetDefaultValue(HcfAsyKeyGenSpiRsaParams * params)1485 static HcfResult SetDefaultValue(HcfAsyKeyGenSpiRsaParams *params)
1486 {
1487 if (params->primes == 0) {
1488 LOGD("set default primes 2");
1489 params->primes = OPENSSL_RSA_PRIMES_SIZE_2;
1490 }
1491 if (params->pubExp != NULL) {
1492 LOGE("RSA has pubKey default unexpectedly.");
1493 return HCF_SUCCESS;
1494 }
1495 BIGNUM *e = OpensslBnNew();
1496 if (e == NULL) {
1497 LOGD("[error] RSA new BN fail.");
1498 return HCF_ERR_CRYPTO_OPERATION;
1499 }
1500 if (OpensslBnSetWord(e, RSA_F4) != HCF_OPENSSL_SUCCESS) {
1501 LOGD("[error] RSA keygen Bn_set_word fail.");
1502 OpensslBnFree(e);
1503 return HCF_ERR_CRYPTO_OPERATION;
1504 }
1505 params->pubExp = e;
1506 return HCF_SUCCESS;
1507 }
1508
DecodeParams(HcfAsyKeyGenParams * from,HcfAsyKeyGenSpiRsaParams ** to)1509 static HcfResult DecodeParams(HcfAsyKeyGenParams *from, HcfAsyKeyGenSpiRsaParams **to)
1510 {
1511 *to = (HcfAsyKeyGenSpiRsaParams *)HcfMalloc(sizeof(HcfAsyKeyGenSpiRsaParams), 0);
1512 if (*to == NULL) {
1513 LOGE("Malloc HcfAsyKeyGenSpiRsaParams fail");
1514 return HCF_ERR_MALLOC;
1515 }
1516
1517 (*to)->bits = from->bits;
1518 (*to)->primes = from->primes;
1519
1520 // set 2 as default primes, RSA_F4 as default pubExp
1521 if (SetDefaultValue(*to) != HCF_SUCCESS) {
1522 LOGE("Set default value fail.");
1523 HcfFree(*to);
1524 *to = NULL;
1525 return HCF_INVALID_PARAMS;
1526 }
1527 if (CheckRsaKeyGenParams(*to) != HCF_SUCCESS) {
1528 LOGE("Invalid keyGen params");
1529 OpensslBnFree((*to)->pubExp);
1530 HcfFree(*to);
1531 *to = NULL;
1532 return HCF_INVALID_PARAMS;
1533 }
1534 return HCF_SUCCESS;
1535 }
1536
HcfAsyKeyGeneratorSpiRsaCreate(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** generator)1537 HcfResult HcfAsyKeyGeneratorSpiRsaCreate(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **generator)
1538 {
1539 if (params == NULL || generator == NULL) {
1540 LOGE("Invalid input, params is invalid or generator is null.");
1541 return HCF_INVALID_PARAMS;
1542 }
1543 HcfAsyKeyGeneratorSpiRsaOpensslImpl *impl = (HcfAsyKeyGeneratorSpiRsaOpensslImpl *)
1544 HcfMalloc(sizeof(HcfAsyKeyGeneratorSpiRsaOpensslImpl), 0);
1545 if (impl == NULL) {
1546 LOGE("Failed to allocate returnImpl memroy!");
1547 return HCF_ERR_MALLOC;
1548 }
1549 if (DecodeParams(params, &impl->params) != HCF_SUCCESS) {
1550 LOGE("Keygen params is invalid.");
1551 HcfFree(impl);
1552 return HCF_INVALID_PARAMS;
1553 }
1554 impl->base.base.getClass = GetKeyGeneratorClass;
1555 impl->base.base.destroy = DestroyKeyGeneratorSpiImpl;
1556 impl->base.engineGenerateKeyPair = EngineGenerateKeyPair;
1557 impl->base.engineConvertKey = EngineConvertKey;
1558 impl->base.engineConvertPemKey = EngineConvertPemKey;
1559 impl->base.engineGenerateKeyPairBySpec = EngineGenerateKeyPairBySpec;
1560 impl->base.engineGeneratePubKeyBySpec = EngineGeneratePubKeyBySpec;
1561 impl->base.engineGeneratePriKeyBySpec = EngineGeneratePriKeyBySpec;
1562 *generator = (HcfAsyKeyGeneratorSpi *)impl;
1563 return HCF_SUCCESS;
1564 }
1565