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 %{public}d to Keygen bits %{public}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 %{public}d to Keygen bits %{public}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 %{public}d to Keygen bits %{public}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 %{public}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 blob.data = NULL;
357 return HCF_ERR_CRYPTO_OPERATION;
358 }
359 outBlob->len = blob.len;
360 outBlob->data = blob.data;
361 return HCF_SUCCESS;
362 }
363
CopyStrFromBIO(BIO * bio,char ** returnString)364 static HcfResult CopyStrFromBIO(BIO *bio, char **returnString)
365 {
366 if (bio == NULL || returnString == NULL) {
367 LOGE("Invalid input.");
368 return HCF_INVALID_PARAMS;
369 }
370 int len = BIO_pending(bio);
371 if (len < 0) {
372 LOGE("Bio len less than 0.");
373 return HCF_INVALID_PARAMS;
374 }
375 *returnString = (char *)HcfMalloc(len + 1, 0);
376 if (*returnString == NULL) {
377 LOGE("Malloc mem for blob fail.");
378 return HCF_ERR_MALLOC;
379 }
380 if (OpensslBioRead(bio, *returnString, len) <= 0) {
381 LOGE("Bio read fail");
382 HcfPrintOpensslError();
383 HcfFree(*returnString);
384 *returnString = NULL;
385 return HCF_ERR_CRYPTO_OPERATION;
386 }
387 return HCF_SUCCESS;
388 }
389
ConvertPubKeyFromX509(HcfBlob * x509Blob,RSA ** rsa)390 static HcfResult ConvertPubKeyFromX509(HcfBlob *x509Blob, RSA **rsa)
391 {
392 uint8_t *temp = x509Blob->data;
393 RSA *tempRsa = OpensslD2iRsaPubKey(NULL, (const unsigned char **)&temp, x509Blob->len);
394 if (tempRsa == NULL) {
395 LOGD("[error] d2i_RSA_PUBKEY fail.");
396 return HCF_ERR_CRYPTO_OPERATION;
397 }
398 *rsa = tempRsa;
399 return HCF_SUCCESS;
400 }
401
ConvertPriKeyFromPKCS8(HcfBlob * pkcs8Blob,RSA ** rsa)402 static HcfResult ConvertPriKeyFromPKCS8(HcfBlob *pkcs8Blob, RSA **rsa)
403 {
404 const unsigned char *temp = (const unsigned char *)pkcs8Blob->data;
405 EVP_PKEY *pKey = OpensslD2iAutoPrivateKey(NULL, &temp, pkcs8Blob->len);
406 if (pKey == NULL) {
407 LOGD("[error] d2i_AutoPrivateKey fail.");
408 HcfPrintOpensslError();
409 return HCF_ERR_CRYPTO_OPERATION;
410 }
411 RSA *tmpRsa = OpensslEvpPkeyGet1Rsa(pKey);
412 if (tmpRsa == NULL) {
413 LOGD("[error] EVP_PKEY_get1_RSA fail");
414 HcfPrintOpensslError();
415 OpensslEvpPkeyFree(pKey);
416 return HCF_ERR_CRYPTO_OPERATION;
417 }
418 *rsa = tmpRsa;
419 OpensslEvpPkeyFree(pKey);
420 return HCF_SUCCESS;
421 }
422
EncodePubKeyToX509(RSA * rsa,HcfBlob * returnBlob)423 static HcfResult EncodePubKeyToX509(RSA *rsa, HcfBlob *returnBlob)
424 {
425 unsigned char *tempData = NULL;
426 int len = OpensslI2dRsaPubKey(rsa, &tempData);
427 if (len <= 0) {
428 LOGD("[error] i2d_RSA_PUBKEY fail");
429 HcfPrintOpensslError();
430 return HCF_ERR_CRYPTO_OPERATION;
431 }
432 returnBlob->data = tempData;
433 returnBlob->len = len;
434 return HCF_SUCCESS;
435 }
436
EncodePriKeyToPKCS8(RSA * rsa,HcfBlob * returnBlob)437 static HcfResult EncodePriKeyToPKCS8(RSA *rsa, HcfBlob *returnBlob)
438 {
439 EVP_PKEY *pKey = NewEvpPkeyByRsa(rsa, true);
440 if (pKey == NULL) {
441 LOGD("[error] NewEvpPkeyByRsa fail.");
442 return HCF_ERR_CRYPTO_OPERATION;
443 }
444 HcfResult ret = HCF_SUCCESS;
445 BIO *bio = OpensslBioNew(OpensslBioSMem());
446 if (bio == NULL) {
447 LOGD("[error] BIO new fail.");
448 HcfPrintOpensslError();
449 ret = HCF_ERR_CRYPTO_OPERATION;
450 goto ERR2;
451 }
452 if (i2d_PKCS8PrivateKey_bio(bio, pKey, NULL, NULL, 0, NULL, NULL) != HCF_OPENSSL_SUCCESS) {
453 LOGD("[error] i2b_PrivateKey_bio fail.");
454 HcfPrintOpensslError();
455 ret = HCF_ERR_CRYPTO_OPERATION;
456 goto ERR1;
457 }
458 if (CopyMemFromBIO(bio, returnBlob) != HCF_SUCCESS) {
459 LOGD("[error] CopyMemFromBIO fail.");
460 ret = HCF_ERR_CRYPTO_OPERATION;
461 goto ERR1;
462 }
463 ERR1:
464 OpensslBioFreeAll(bio);
465 ERR2:
466 OpensslEvpPkeyFree(pKey);
467 return ret;
468 }
469
GetPubKeyEncoded(HcfKey * self,HcfBlob * returnBlob)470 static HcfResult GetPubKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
471 {
472 if (self == NULL || returnBlob == NULL) {
473 LOGE("Input params is invalid.");
474 return HCF_INVALID_PARAMS;
475 }
476 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PUBKEY_CLASS)) {
477 LOGE("Class not match.");
478 return HCF_INVALID_PARAMS;
479 }
480 HcfOpensslRsaPubKey *impl = (HcfOpensslRsaPubKey *)self;
481 return EncodePubKeyToX509(impl->pk, returnBlob);
482 }
483
GetPubKeyPkcs1Pem(RSA * pk,char ** returnString)484 static HcfResult GetPubKeyPkcs1Pem(RSA *pk, char **returnString)
485 {
486 BIO *bio = OpensslBioNew(OpensslBioSMem());
487 if (bio == NULL) {
488 LOGE("BIO new fail.");
489 HcfPrintOpensslError();
490 return HCF_ERR_CRYPTO_OPERATION;
491 }
492 int ret = OpensslPemWriteBioRsaPublicKey(bio, pk);
493 if (ret != HCF_OPENSSL_SUCCESS) {
494 LOGE("OpensslPemWriteBioRsaPublicKey fail.");
495 HcfPrintOpensslError();
496 OpensslBioFreeAll(bio);
497 return HCF_ERR_CRYPTO_OPERATION;
498 }
499 if (CopyStrFromBIO(bio, returnString) != HCF_SUCCESS) {
500 LOGE("CopyMemFromBIO fail.");
501 OpensslBioFreeAll(bio);
502 return HCF_ERR_CRYPTO_OPERATION;
503 }
504 OpensslBioFreeAll(bio);
505 return HCF_SUCCESS;
506 }
507
GetPubKeyX509Pem(RSA * pk,char ** returnString)508 static HcfResult GetPubKeyX509Pem(RSA *pk, char **returnString)
509 {
510 BIO *bio = OpensslBioNew(OpensslBioSMem());
511 if (bio == NULL) {
512 LOGE("BIO new fail.");
513 HcfPrintOpensslError();
514 return HCF_ERR_CRYPTO_OPERATION;
515 }
516 int ret = OpensslPemWriteBioRsaPubKey(bio, pk);
517 if (ret != HCF_OPENSSL_SUCCESS) {
518 LOGE("OpensslPemWriteBioRsaPubKey fail.");
519 HcfPrintOpensslError();
520 OpensslBioFreeAll(bio);
521 return HCF_ERR_CRYPTO_OPERATION;
522 }
523 if (CopyStrFromBIO(bio, returnString) != HCF_SUCCESS) {
524 LOGE("CopyMemFromBIO fail.");
525 OpensslBioFreeAll(bio);
526 return HCF_ERR_CRYPTO_OPERATION;
527 }
528 OpensslBioFreeAll(bio);
529 return HCF_SUCCESS;
530 }
531
GetPubKeyPem(const char * format,RSA * pk,char ** returnString)532 static HcfResult GetPubKeyPem(const char *format, RSA *pk, char **returnString)
533 {
534 HcfResult result;
535 if (strcmp(format, "PKCS1") == 0) {
536 result = GetPubKeyPkcs1Pem(pk, returnString);
537 if (result != HCF_SUCCESS) {
538 return result;
539 }
540 }
541 if (strcmp(format, "X509") == 0) {
542 result = GetPubKeyX509Pem(pk, returnString);
543 if (result != HCF_SUCCESS) {
544 return result;
545 }
546 }
547 return HCF_SUCCESS;
548 }
549
550
GetPubKeyEncodedPem(HcfKey * self,const char * format,char ** returnString)551 static HcfResult GetPubKeyEncodedPem(HcfKey *self, const char *format, char **returnString)
552 {
553 if (self == NULL || format == NULL|| returnString == NULL) {
554 LOGE("param is null.");
555 return HCF_INVALID_PARAMS;
556 }
557 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PUBKEY_CLASS)) {
558 LOGE("Class not match.");
559 return HCF_INVALID_PARAMS;
560 }
561 const char *outPutStruct = NULL;
562 if (strcmp(format, "PKCS1") == 0) {
563 outPutStruct = "pkcs1";
564 } else if (strcmp(format, "X509") == 0) {
565 outPutStruct = "subjectPublicKeyInfo";
566 } else {
567 LOGE("format is invalid.");
568 return HCF_INVALID_PARAMS;
569 }
570 HcfOpensslRsaPubKey *impl = (HcfOpensslRsaPubKey *)self;
571 EVP_PKEY *pkey = NewEvpPkeyByRsa(impl->pk, true);
572 if (pkey == NULL) {
573 LOGE("NewEvpPkeyByRsa failed.");
574 return HCF_ERR_CRYPTO_OPERATION;
575 }
576 HcfResult result = GetKeyEncodedPem(pkey, outPutStruct, EVP_PKEY_PUBLIC_KEY, returnString);
577 OpensslEvpPkeyFree(pkey);
578 if (result != HCF_SUCCESS) {
579 if (GetPubKeyPem(format, impl->pk, returnString) != HCF_SUCCESS) {
580 LOGE("GetPubKeyPem failed.");
581 return HCF_ERR_CRYPTO_OPERATION;
582 }
583 }
584 return HCF_SUCCESS;
585 }
586
GetPriKeyEncoded(HcfKey * self,HcfBlob * returnBlob)587 static HcfResult GetPriKeyEncoded(HcfKey *self, HcfBlob *returnBlob)
588 {
589 if (self == NULL || returnBlob == NULL) {
590 LOGE("Key is null.");
591 return HCF_INVALID_PARAMS;
592 }
593
594 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) {
595 LOGE("Class not match.");
596 return HCF_INVALID_PARAMS;
597 }
598 HcfOpensslRsaPriKey *impl = (HcfOpensslRsaPriKey *)self;
599 const BIGNUM *p = NULL;
600 const BIGNUM *q = NULL;
601 OpensslRsaGet0Factors(impl->sk, &p, &q);
602 if (p == NULL || q == NULL) {
603 LOGD("[error] RSA private key missing p, q, not support to get encoded PK");
604 return HCF_NOT_SUPPORT;
605 }
606 return EncodePriKeyToPKCS8(impl->sk, returnBlob);
607 }
608
GetPrikeyPkcs8Pem(EVP_PKEY * pkey,const EVP_CIPHER * cipher,const char * passWord,char ** returnString)609 static HcfResult GetPrikeyPkcs8Pem(EVP_PKEY *pkey, const EVP_CIPHER *cipher, const char *passWord, char **returnString)
610 {
611 BIO *bio = OpensslBioNew(OpensslBioSMem());
612 if (bio == NULL) {
613 LOGE("BIO new fail.");
614 HcfPrintOpensslError();
615 return HCF_ERR_CRYPTO_OPERATION;
616 }
617
618 size_t passLen = 0;
619 if (passWord != NULL) {
620 passLen = strlen(passWord);
621 }
622
623 int ret = PEM_write_bio_PKCS8PrivateKey(bio, pkey, cipher, passWord, passLen, NULL, NULL);
624 if (ret != HCF_OPENSSL_SUCCESS) {
625 LOGE("OpensslPemWriteBioPkcs8PrivateKey fail.");
626 HcfPrintOpensslError();
627 OpensslBioFreeAll(bio);
628 return HCF_ERR_CRYPTO_OPERATION;
629 }
630 if (CopyStrFromBIO(bio, returnString) != HCF_SUCCESS) {
631 LOGE("CopyMemFromBIO fail.");
632 OpensslBioFreeAll(bio);
633 return HCF_ERR_CRYPTO_OPERATION;
634 }
635 OpensslBioFreeAll(bio);
636 return HCF_SUCCESS;
637 }
638
GetPrikeyPkcs1Pem(EVP_PKEY * pkey,const EVP_CIPHER * cipher,const char * passWord,char ** returnString)639 static HcfResult GetPrikeyPkcs1Pem(EVP_PKEY *pkey, const EVP_CIPHER *cipher, const char *passWord, char **returnString)
640 {
641 BIO *bio = OpensslBioNew(OpensslBioSMem());
642 if (bio == NULL) {
643 LOGE("BIO new fail.");
644 HcfPrintOpensslError();
645 return HCF_ERR_CRYPTO_OPERATION;
646 }
647
648 size_t passLen = 0;
649 if (passWord != NULL) {
650 passLen = strlen(passWord);
651 }
652
653 int ret = PEM_write_bio_PrivateKey_traditional(bio, pkey, cipher, (unsigned char *)passWord, passLen, NULL, NULL);
654 if (ret != HCF_OPENSSL_SUCCESS) {
655 LOGE("OpensslPemWriteBioRsaPrivateKey fail.");
656 HcfPrintOpensslError();
657 OpensslBioFreeAll(bio);
658 return HCF_ERR_CRYPTO_OPERATION;
659 }
660 if (CopyStrFromBIO(bio, returnString) != HCF_SUCCESS) {
661 LOGE("CopyStrFromBIO fail.");
662 OpensslBioFreeAll(bio);
663 return HCF_ERR_CRYPTO_OPERATION;
664 }
665 OpensslBioFreeAll(bio);
666 return HCF_SUCCESS;
667 }
668
GetPriKeyPem(const char * format,EVP_PKEY * pkey,const EVP_CIPHER * cipher,const char * passWord,char ** returnString)669 static HcfResult GetPriKeyPem(const char *format, EVP_PKEY *pkey, const EVP_CIPHER *cipher,
670 const char *passWord, char **returnString)
671 {
672 HcfResult result;
673 if (strcmp(format, "PKCS8") == 0) {
674 result = GetPrikeyPkcs8Pem(pkey, cipher, passWord, returnString);
675 if (result != HCF_SUCCESS) {
676 return result;
677 }
678 } else if (strcmp(format, "PKCS1") == 0) {
679 result = GetPrikeyPkcs1Pem(pkey, cipher, passWord, returnString);
680 if (result != HCF_SUCCESS) {
681 return result;
682 }
683 } else {
684 LOGE("format is invalid.");
685 return HCF_INVALID_PARAMS;
686 }
687 return HCF_SUCCESS;
688 }
689
ValidateInputParams(const HcfPriKey * self,const char * format,char ** returnString)690 static HcfResult ValidateInputParams(const HcfPriKey *self, const char *format, char **returnString)
691 {
692 if (self == NULL || format == NULL || returnString == NULL) {
693 LOGE("param is null.");
694 return HCF_INVALID_PARAMS;
695 }
696 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) {
697 LOGE("Class not match.");
698 return HCF_INVALID_PARAMS;
699 }
700 return HCF_SUCCESS;
701 }
702
GetPriKeyEncodedPem(const HcfPriKey * self,HcfParamsSpec * params,const char * format,char ** returnString)703 static HcfResult GetPriKeyEncodedPem(const HcfPriKey *self, HcfParamsSpec *params, const char *format,
704 char **returnString)
705 {
706 HcfResult result = ValidateInputParams(self, format, returnString);
707 if (result != HCF_SUCCESS) {
708 return result;
709 }
710
711 HcfOpensslRsaPriKey *impl = (HcfOpensslRsaPriKey *)self;
712 EVP_PKEY *pkey = NewEvpPkeyByRsa(impl->sk, true);
713 if (pkey == NULL) {
714 LOGE("NewEvpPkeyByRsa failed.");
715 return HCF_ERR_CRYPTO_OPERATION;
716 }
717
718 if (params != NULL) {
719 const EVP_CIPHER *cipher = NULL;
720 const char *passWord = NULL;
721 HcfKeyEncodingParamsSpec *spec = (HcfKeyEncodingParamsSpec *)params;
722 const char *cipherStr = (const char *)spec->cipher;
723 if (!IsCipherSupported(cipherStr)) {
724 LOGE("Cipher algorithm %{public}s not supported", cipherStr);
725 OpensslEvpPkeyFree(pkey);
726 return HCF_NOT_SUPPORT;
727 }
728 passWord = (const char *)spec->password;
729 if (passWord == NULL) {
730 LOGE("passWord is NULL.");
731 OpensslEvpPkeyFree(pkey);
732 return HCF_ERR_PARAMETER_CHECK_FAILED;
733 }
734 cipher = EVP_CIPHER_fetch(NULL, cipherStr, NULL);
735 result = GetPriKeyPem(format, pkey, cipher, passWord, returnString);
736 EVP_CIPHER_free((EVP_CIPHER *)cipher);
737 } else {
738 result = GetPriKeyPem(format, pkey, NULL, NULL, returnString);
739 }
740
741 if (result != HCF_SUCCESS) {
742 LOGE("GetPriKeyPem failed.");
743 OpensslEvpPkeyFree(pkey);
744 return result;
745 }
746 OpensslEvpPkeyFree(pkey);
747 return HCF_SUCCESS;
748 }
749
GetPubKeyFormat(HcfKey * self)750 static const char *GetPubKeyFormat(HcfKey *self)
751 {
752 if (self == NULL) {
753 LOGE("Invalid input parameter.");
754 return NULL;
755 }
756 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PUBKEY_CLASS)) {
757 return NULL;
758 }
759 return OPENSSL_RSA_PUBKEY_FORMAT;
760 }
761
GetPriKeyFormat(HcfKey * self)762 static const char *GetPriKeyFormat(HcfKey *self)
763 {
764 if (self == NULL) {
765 LOGE("Invalid input parameter.");
766 return NULL;
767 }
768 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) {
769 return NULL;
770 }
771 return OPENSSL_RSA_PRIKEY_FORMAT;
772 }
773
GetPriKeyAlgorithm(HcfKey * self)774 static const char *GetPriKeyAlgorithm(HcfKey *self)
775 {
776 if (self == NULL) {
777 LOGE("Invalid input parameter.");
778 return NULL;
779 }
780 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) {
781 return NULL;
782 }
783 return OPENSSL_RSA_ALGORITHM;
784 }
785
GetPubKeyAlgorithm(HcfKey * self)786 static const char *GetPubKeyAlgorithm(HcfKey *self)
787 {
788 if (self == NULL) {
789 LOGE("Invalid input parameter.");
790 return NULL;
791 }
792 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PUBKEY_CLASS)) {
793 return NULL;
794 }
795 return OPENSSL_RSA_ALGORITHM;
796 }
797
ClearPriKeyMem(HcfPriKey * self)798 static void ClearPriKeyMem(HcfPriKey *self)
799 {
800 if (self == NULL) {
801 LOGE("PriKey is NULL.");
802 return;
803 }
804 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_PRIKEY_CLASS)) {
805 LOGE("Class not match");
806 return;
807 }
808 HcfOpensslRsaPriKey *impl = (HcfOpensslRsaPriKey *)self;
809 OpensslRsaFree(impl->sk);
810 impl->sk = NULL;
811 }
812
GetRsaPubKeyEncodedDer(const HcfPubKey * self,const char * format,HcfBlob * returnBlob)813 static HcfResult GetRsaPubKeyEncodedDer(const HcfPubKey *self, const char *format, HcfBlob *returnBlob)
814 {
815 (void)self;
816 (void)format;
817 (void)returnBlob;
818 return HCF_INVALID_PARAMS;
819 }
820
PackPubKey(RSA * rsaPubKey,HcfOpensslRsaPubKey ** retPubKey)821 static HcfResult PackPubKey(RSA *rsaPubKey, HcfOpensslRsaPubKey **retPubKey)
822 {
823 if (retPubKey == NULL || rsaPubKey == NULL) {
824 LOGE("Invalid params");
825 return HCF_INVALID_PARAMS;
826 }
827 *retPubKey = (HcfOpensslRsaPubKey *)HcfMalloc(sizeof(HcfOpensslRsaPubKey), 0);
828 if (*retPubKey == NULL) {
829 LOGE("Malloc retPubKey fail");
830 return HCF_ERR_MALLOC;
831 }
832 (*retPubKey)->pk = rsaPubKey;
833 (*retPubKey)->bits = (uint32_t)OpensslRsaBits(rsaPubKey);
834 (*retPubKey)->base.base.getAlgorithm = GetPubKeyAlgorithm;
835 (*retPubKey)->base.base.getEncoded = GetPubKeyEncoded;
836 (*retPubKey)->base.base.getEncodedPem = GetPubKeyEncodedPem;
837 (*retPubKey)->base.base.getFormat = GetPubKeyFormat;
838 (*retPubKey)->base.base.base.getClass = GetOpensslPubkeyClass;
839 (*retPubKey)->base.base.base.destroy = DestroyPubKey;
840 (*retPubKey)->base.getAsyKeySpecBigInteger = GetRsaPubKeySpecBigInteger;
841 (*retPubKey)->base.getAsyKeySpecString = GetRsaPubKeySpecString;
842 (*retPubKey)->base.getAsyKeySpecInt = GetRsaPubKeySpecInt;
843 (*retPubKey)->base.getEncodedDer = GetRsaPubKeyEncodedDer;
844 return HCF_SUCCESS;
845 }
846
847 // spec中,prikey只有n,e,d,没有p, q
PackPriKey(RSA * rsaPriKey,HcfOpensslRsaPriKey ** retPriKey)848 static HcfResult PackPriKey(RSA *rsaPriKey, HcfOpensslRsaPriKey **retPriKey)
849 {
850 if (retPriKey == NULL || rsaPriKey == NULL) {
851 LOGE("Invalid params");
852 return HCF_INVALID_PARAMS;
853 }
854 *retPriKey = (HcfOpensslRsaPriKey *)HcfMalloc(sizeof(HcfOpensslRsaPriKey), 0);
855 if (*retPriKey == NULL) {
856 LOGE("Malloc retPriKey fail");
857 return HCF_ERR_MALLOC;
858 }
859 (*retPriKey)->sk = rsaPriKey;
860 (*retPriKey)->bits = (uint32_t)OpensslRsaBits(rsaPriKey);
861 (*retPriKey)->base.clearMem = ClearPriKeyMem;
862 (*retPriKey)->base.base.getAlgorithm = GetPriKeyAlgorithm;
863 (*retPriKey)->base.base.getEncoded = GetPriKeyEncoded;
864 (*retPriKey)->base.getEncodedPem = GetPriKeyEncodedPem;
865 (*retPriKey)->base.base.getFormat = GetPriKeyFormat;
866 (*retPriKey)->base.base.base.getClass = GetOpensslPrikeyClass;
867 (*retPriKey)->base.base.base.destroy = DestroyPriKey;
868 (*retPriKey)->base.getAsyKeySpecBigInteger = GetRsaPriKeySpecBigInteger;
869 (*retPriKey)->base.getAsyKeySpecString = GetRsaPriKeySpecString;
870 (*retPriKey)->base.getAsyKeySpecInt = GetRsaPriKeySpecInt;
871 (*retPriKey)->base.getEncodedDer = GetRsaPriKeyEncodedDer;
872 return HCF_SUCCESS;
873 }
874
DuplicatePkAndSkFromRSA(RSA * rsa,RSA ** pubKey,RSA ** priKey)875 static HcfResult DuplicatePkAndSkFromRSA(RSA *rsa, RSA **pubKey, RSA **priKey)
876 {
877 if (rsa == NULL) {
878 LOGE("Rsa is NULL.");
879 return HCF_INVALID_PARAMS;
880 }
881 if (DuplicateRsa(rsa, false, pubKey) != HCF_SUCCESS) {
882 LOGD("[error] Duplicate pubkey rsa fail");
883 return HCF_ERR_CRYPTO_OPERATION;
884 }
885 if (DuplicateRsa(rsa, true, priKey) != HCF_SUCCESS) {
886 LOGD("[error] Duplicate prikey rsa fail");
887 OpensslRsaFree(*pubKey);
888 *pubKey = NULL;
889 return HCF_ERR_CRYPTO_OPERATION;
890 }
891 return HCF_SUCCESS;
892 }
893
PackKeyPair(RSA * rsa,uint32_t realBits,HcfOpensslRsaKeyPair ** retKeyPair)894 static HcfResult PackKeyPair(RSA *rsa, uint32_t realBits, HcfOpensslRsaKeyPair **retKeyPair)
895 {
896 if (retKeyPair == NULL || rsa == NULL) {
897 LOGE("Invalid params");
898 return HCF_INVALID_PARAMS;
899 }
900 RSA *pubKey = NULL;
901 RSA *priKey = NULL;
902 if (DuplicatePkAndSkFromRSA(rsa, &pubKey, &priKey) != HCF_SUCCESS) {
903 LOGD("[error] DuplicatePkAndSkFromRSA fail");
904 return HCF_ERR_CRYPTO_OPERATION;
905 }
906 HcfResult ret = HCF_SUCCESS;
907 *retKeyPair = (HcfOpensslRsaKeyPair *)HcfMalloc(sizeof(HcfOpensslRsaKeyPair), 0);
908 if (*retKeyPair == NULL) {
909 LOGE("Malloc keypair fail");
910 OpensslRsaFree(pubKey);
911 OpensslRsaFree(priKey);
912 return HCF_ERR_MALLOC;
913 }
914 HcfOpensslRsaPriKey *priKeyImpl = NULL;
915 HcfOpensslRsaPubKey *pubKeyImpl = NULL;
916 ret = PackPubKey(pubKey, &pubKeyImpl);
917 if (ret != HCF_SUCCESS) {
918 LOGE("Pack pubKey fail.");
919 goto ERR2;
920 }
921 ret = PackPriKey(priKey, &priKeyImpl);
922 if (ret != HCF_SUCCESS) {
923 LOGE("Pack priKey fail.");
924 goto ERR1;
925 }
926 (*retKeyPair)->base.priKey = (HcfPriKey *)priKeyImpl;
927 (*retKeyPair)->base.pubKey = (HcfPubKey *)pubKeyImpl;
928 (*retKeyPair)->base.base.getClass = GetOpensslKeyPairClass;
929 (*retKeyPair)->base.base.destroy = DestroyKeyPair;
930 return HCF_SUCCESS;
931 ERR1:
932 HcfFree(pubKeyImpl);
933 pubKeyImpl = NULL;
934 ERR2:
935 OpensslRsaFree(pubKey);
936 OpensslRsaFree(priKey);
937 HcfFree(*retKeyPair);
938 *retKeyPair = NULL;
939 return ret;
940 }
941
GetRealPrimes(int32_t primesFlag)942 static int32_t GetRealPrimes(int32_t primesFlag)
943 {
944 switch (primesFlag) {
945 case OPENSSL_RSA_PRIMES_SIZE_2:
946 return PRIMES_2;
947 case OPENSSL_RSA_PRIMES_SIZE_3:
948 return PRIMES_3;
949 case OPENSSL_RSA_PRIMES_SIZE_4:
950 return PRIMES_4;
951 case OPENSSL_RSA_PRIMES_SIZE_5:
952 return PRIMES_5;
953 default:
954 LOGD("set default primes 2");
955 return PRIMES_2;
956 }
957 }
958
GenerateKeyPair(HcfAsyKeyGenSpiRsaParams * params,HcfKeyPair ** keyPair)959 static HcfResult GenerateKeyPair(HcfAsyKeyGenSpiRsaParams *params, HcfKeyPair **keyPair)
960 {
961 // check input params is valid
962 HcfResult res = CheckRsaKeyGenParams(params);
963 if (res != HCF_SUCCESS) {
964 LOGE("Rsa CheckRsaKeyGenParams fail.");
965 return HCF_INVALID_PARAMS;
966 }
967 // Generate keyPair RSA
968 RSA *rsa = OpensslRsaNew();
969 if (rsa == NULL) {
970 LOGE("new RSA fail.");
971 return HCF_ERR_MALLOC;
972 }
973 LOGD("keygen bits is %d, primes is %d", params->bits, GetRealPrimes(params->primes));
974 if (GetRealPrimes(params->primes) != OPENSSL_RSA_KEYGEN_DEFAULT_PRIMES) {
975 if (RSA_generate_multi_prime_key(rsa, params->bits, GetRealPrimes(params->primes), params->pubExp, NULL)
976 != HCF_OPENSSL_SUCCESS) {
977 LOGD("[error] Generate multi-primes rsa key fail");
978 HcfPrintOpensslError();
979 OpensslRsaFree(rsa);
980 return HCF_ERR_CRYPTO_OPERATION;
981 }
982 } else {
983 if (RSA_generate_key_ex(rsa, params->bits, params->pubExp, NULL) != HCF_OPENSSL_SUCCESS) {
984 LOGD("[error] Generate rsa key fail");
985 HcfPrintOpensslError();
986 OpensslRsaFree(rsa);
987 return HCF_ERR_CRYPTO_OPERATION;
988 }
989 }
990
991 // devided to pk and sk;
992 HcfOpensslRsaKeyPair *keyPairImpl = NULL;
993 res = PackKeyPair(rsa, params->bits, &keyPairImpl);
994 if (res != HCF_SUCCESS) {
995 LOGE("Generate keyPair fail.");
996 OpensslRsaFree(rsa);
997 return res;
998 }
999 *keyPair = (HcfKeyPair *)keyPairImpl;
1000 OpensslRsaFree(rsa);
1001 LOGD("Generate keypair success.");
1002 return res;
1003 }
1004
EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi * self,HcfKeyPair ** keyPair)1005 static HcfResult EngineGenerateKeyPair(HcfAsyKeyGeneratorSpi *self, HcfKeyPair **keyPair)
1006 {
1007 if (self == NULL || keyPair == NULL) {
1008 LOGE("Invalid params.");
1009 return HCF_INVALID_PARAMS;
1010 }
1011 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1012 LOGE("Class not match.");
1013 return HCF_INVALID_PARAMS;
1014 }
1015 HcfAsyKeyGeneratorSpiRsaOpensslImpl *impl = (HcfAsyKeyGeneratorSpiRsaOpensslImpl *)self;
1016 return GenerateKeyPair(impl->params, keyPair);
1017 }
1018
GetKeyGeneratorClass(void)1019 static const char *GetKeyGeneratorClass(void)
1020 {
1021 return OPENSSL_RSA_GENERATOR_CLASS;
1022 }
1023
DestroyKeyGeneratorSpiImpl(HcfObjectBase * self)1024 static void DestroyKeyGeneratorSpiImpl(HcfObjectBase *self)
1025 {
1026 if (self == NULL) {
1027 LOGE("DestroyKeyGeneratorSpiImpl is null");
1028 return;
1029 }
1030 if (!HcfIsClassMatch(self, OPENSSL_RSA_GENERATOR_CLASS)) {
1031 LOGE("Class not match.");
1032 return;
1033 }
1034 // destroy pubExp first.
1035 HcfAsyKeyGeneratorSpiRsaOpensslImpl *impl = (HcfAsyKeyGeneratorSpiRsaOpensslImpl *)self;
1036 if (impl->params != NULL && impl->params->pubExp != NULL) {
1037 OpensslBnFree(impl->params->pubExp);
1038 }
1039 HcfFree(impl->params);
1040 impl->params = NULL;
1041 HcfFree(self);
1042 }
1043
ConvertPubKey(HcfBlob * pubKeyBlob,HcfOpensslRsaPubKey ** pubkeyRet)1044 static HcfResult ConvertPubKey(HcfBlob *pubKeyBlob, HcfOpensslRsaPubKey **pubkeyRet)
1045 {
1046 RSA *rsaPk = NULL;
1047 if (ConvertPubKeyFromX509(pubKeyBlob, &rsaPk) != HCF_SUCCESS) {
1048 LOGD("[error] Convert pubKey from X509 fail.");
1049 return HCF_ERR_CRYPTO_OPERATION;
1050 }
1051 HcfOpensslRsaPubKey *pubKey = NULL;
1052 HcfResult ret = PackPubKey(rsaPk, &pubKey);
1053 if (ret != HCF_SUCCESS) {
1054 LOGD("[error] PackPubKey fail");
1055 goto ERR;
1056 }
1057 *pubkeyRet = pubKey;
1058 return ret;
1059 ERR:
1060 OpensslRsaFree(rsaPk);
1061 return ret;
1062 }
1063
ConvertPemKeyToKey(const char * keyStr,HcfParamsSpec * params,int selection,RSA ** rsa)1064 static HcfResult ConvertPemKeyToKey(const char *keyStr, HcfParamsSpec *params, int selection, RSA **rsa)
1065 {
1066 EVP_PKEY *pkey = NULL;
1067 OSSL_DECODER_CTX *ctx = OpensslOsslDecoderCtxNewForPkey(&pkey, "PEM", NULL, "RSA", selection, NULL, NULL);
1068 if (ctx == NULL) {
1069 LOGE("OpensslOsslDecoderCtxNewForPkey fail.");
1070 HcfPrintOpensslError();
1071 return HCF_ERR_CRYPTO_OPERATION;
1072 }
1073 if (params != NULL) {
1074 HcfKeyDecodingParamsSpec *spec = (HcfKeyDecodingParamsSpec *)params;
1075 const unsigned char *passWd = (const unsigned char *)spec->password;
1076 if (OpensslOsslDecoderCtxSetPassPhrase(ctx, passWd, strlen(spec->password)) != HCF_OPENSSL_SUCCESS) {
1077 HcfPrintOpensslError();
1078 OpensslOsslDecoderCtxFree(ctx);
1079 OpensslEvpPkeyFree(pkey);
1080 return HCF_ERR_CRYPTO_OPERATION;
1081 }
1082 }
1083 size_t pdataLen = strlen(keyStr);
1084 const unsigned char *pdata = (const unsigned char *)keyStr;
1085 int ret = OpensslOsslDecoderFromData(ctx, &pdata, &pdataLen);
1086 OpensslOsslDecoderCtxFree(ctx);
1087 if (ret != HCF_OPENSSL_SUCCESS) {
1088 LOGE("OpensslOsslDecoderFromData failed.");
1089 HcfPrintOpensslError();
1090 OpensslEvpPkeyFree(pkey);
1091 return HCF_ERR_CRYPTO_OPERATION;
1092 }
1093 *rsa = OpensslEvpPkeyGet1Rsa(pkey);
1094 OpensslEvpPkeyFree(pkey);
1095 if (*rsa == NULL) {
1096 LOGE("OpensslEvpPkeyGet1Rsa fail.");
1097 HcfPrintOpensslError();
1098 return HCF_ERR_CRYPTO_OPERATION;
1099 }
1100 return HCF_SUCCESS;
1101 }
1102
ConvertPemPubKey(const char * pubKeyStr,int selection,HcfOpensslRsaPubKey ** pubKeyRet)1103 static HcfResult ConvertPemPubKey(const char *pubKeyStr, int selection, HcfOpensslRsaPubKey **pubKeyRet)
1104 {
1105 RSA *rsaPk = NULL;
1106 HcfResult ret;
1107 ret = ConvertPemKeyToKey(pubKeyStr, NULL, selection, &rsaPk);
1108 if (ret != HCF_SUCCESS) {
1109 LOGE("ConvertPemKeyToKey failed.");
1110 return ret;
1111 }
1112
1113 HcfOpensslRsaPubKey *pubKey = NULL;
1114 HcfResult result = PackPubKey(rsaPk, &pubKey);
1115 if (result != HCF_SUCCESS) {
1116 LOGE("PackPubKey fail.");
1117 OpensslRsaFree(rsaPk);
1118 return result;
1119 }
1120 *pubKeyRet = pubKey;
1121 return HCF_SUCCESS;
1122 }
1123
ConvertPriKey(HcfBlob * priKeyBlob,HcfOpensslRsaPriKey ** priKeyRet)1124 static HcfResult ConvertPriKey(HcfBlob *priKeyBlob, HcfOpensslRsaPriKey **priKeyRet)
1125 {
1126 RSA *rsaSk = NULL;
1127 if (ConvertPriKeyFromPKCS8(priKeyBlob, &rsaSk) != HCF_SUCCESS) {
1128 LOGE("ConvertPriKeyFromPKCS8 fail.");
1129 return HCF_ERR_MALLOC;
1130 }
1131 HcfOpensslRsaPriKey *priKey = NULL;
1132 HcfResult ret = PackPriKey(rsaSk, &priKey);
1133 if (ret != HCF_SUCCESS) {
1134 LOGD("[error] PackPriKey fail");
1135 goto ERR;
1136 }
1137 *priKeyRet = priKey;
1138 return ret;
1139 ERR:
1140 OpensslRsaFree(rsaSk);
1141 return ret;
1142 }
1143
ConvertPemPriKey(const char * priKeyStr,HcfParamsSpec * params,int selection,HcfOpensslRsaPriKey ** priKeyRet)1144 static HcfResult ConvertPemPriKey(const char *priKeyStr, HcfParamsSpec *params, int selection,
1145 HcfOpensslRsaPriKey **priKeyRet)
1146 {
1147 RSA *rsaSk = NULL;
1148 HcfResult ret;
1149 ret = ConvertPemKeyToKey(priKeyStr, params, selection, &rsaSk);
1150 if (ret != HCF_SUCCESS) {
1151 LOGE("ConvertPemKeyToKey failed.");
1152 return ret;
1153 }
1154 HcfOpensslRsaPriKey *priKey = NULL;
1155 HcfResult result = PackPriKey(rsaSk, &priKey);
1156 if (result != HCF_SUCCESS) {
1157 LOGE("PackPriKey fail.");
1158 OpensslRsaFree(rsaSk);
1159 return result;
1160 }
1161 *priKeyRet = priKey;
1162 return HCF_SUCCESS;
1163 }
1164
EngineConvertKey(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,HcfBlob * pubKeyBlob,HcfBlob * priKeyBlob,HcfKeyPair ** returnKeyPair)1165 static HcfResult EngineConvertKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, HcfBlob *pubKeyBlob,
1166 HcfBlob *priKeyBlob, HcfKeyPair **returnKeyPair)
1167 {
1168 (void)params;
1169 if ((self == NULL) || (returnKeyPair == NULL) || ((pubKeyBlob == NULL) && (priKeyBlob == NULL))) {
1170 LOGE("ConvertKeyParams is invalid.");
1171 return HCF_INVALID_PARAMS;
1172 }
1173 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1174 LOGE("Class not match.");
1175 return HCF_INVALID_PARAMS;
1176 }
1177
1178 HcfOpensslRsaPubKey *pubKey = NULL;
1179 if ((pubKeyBlob != NULL) && (pubKeyBlob->len != 0) && (pubKeyBlob->data != NULL)) {
1180 if (ConvertPubKey(pubKeyBlob, &pubKey) != HCF_SUCCESS) {
1181 LOGE("convert pubkey fail.");
1182 return HCF_INVALID_PARAMS;
1183 }
1184 }
1185
1186 HcfOpensslRsaPriKey *priKey = NULL;
1187 if ((priKeyBlob != NULL) && (priKeyBlob->len != 0) && (priKeyBlob->data != NULL)) {
1188 if (ConvertPriKey(priKeyBlob, &priKey) != HCF_SUCCESS) {
1189 LOGE("convert prikey fail.");
1190 HcfObjDestroy((HcfObjectBase *)pubKey);
1191 pubKey = NULL;
1192 return HCF_INVALID_PARAMS;
1193 }
1194 }
1195
1196 if (pubKey == NULL && priKey == NULL) {
1197 LOGE("Convert key failed with invalid blob");
1198 return HCF_INVALID_PARAMS;
1199 }
1200
1201 HcfOpensslRsaKeyPair *keyPair = (HcfOpensslRsaKeyPair *)HcfMalloc(sizeof(HcfOpensslRsaKeyPair), 0);
1202 if (keyPair == NULL) {
1203 LOGE("Malloc keyPair fail.");
1204 HcfObjDestroy((HcfObjectBase *)pubKey);
1205 pubKey = NULL;
1206 HcfObjDestroy((HcfObjectBase *)priKey);
1207 priKey = NULL;
1208 return HCF_ERR_MALLOC;
1209 }
1210
1211 keyPair->base.priKey = (HcfPriKey *)priKey;
1212 keyPair->base.pubKey = (HcfPubKey *)pubKey;
1213 keyPair->base.base.getClass = GetOpensslKeyPairClass;
1214 keyPair->base.base.destroy = DestroyKeyPair;
1215 *returnKeyPair = (HcfKeyPair *)keyPair;
1216 return HCF_SUCCESS;
1217 }
1218
EngineConvertPemKey(HcfAsyKeyGeneratorSpi * self,HcfParamsSpec * params,const char * pubKeyStr,const char * priKeyStr,HcfKeyPair ** returnKeyPair)1219 static HcfResult EngineConvertPemKey(HcfAsyKeyGeneratorSpi *self, HcfParamsSpec *params, const char *pubKeyStr,
1220 const char *priKeyStr, HcfKeyPair **returnKeyPair)
1221 {
1222 if ((self == NULL) || (returnKeyPair == NULL) || ((pubKeyStr == NULL) && (priKeyStr == NULL))) {
1223 LOGE("ConvertPemKeyParams is invalid.");
1224 return HCF_INVALID_PARAMS;
1225 }
1226 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1227 LOGE("Class not match.");
1228 return HCF_INVALID_PARAMS;
1229 }
1230 HcfOpensslRsaPubKey *pubKey = NULL;
1231 if (pubKeyStr != NULL && strlen(pubKeyStr) != 0) {
1232 if (ConvertPemPubKey(pubKeyStr, EVP_PKEY_PUBLIC_KEY, &pubKey) != HCF_SUCCESS) {
1233 LOGE("convert pubkey fail.");
1234 return HCF_ERR_CRYPTO_OPERATION;
1235 }
1236 }
1237 HcfOpensslRsaPriKey *priKey = NULL;
1238 if (priKeyStr != NULL && strlen(priKeyStr) != 0) {
1239 if (ConvertPemPriKey(priKeyStr, params, EVP_PKEY_KEYPAIR, &priKey) != HCF_SUCCESS) {
1240 LOGE("convert prikey fail.");
1241 HcfObjDestroy((HcfObjectBase *)pubKey);
1242 pubKey = NULL;
1243 return HCF_ERR_CRYPTO_OPERATION;
1244 }
1245 }
1246
1247 HcfOpensslRsaKeyPair *keyPair = (HcfOpensslRsaKeyPair *)HcfMalloc(sizeof(HcfOpensslRsaKeyPair), 0);
1248 if (keyPair == NULL) {
1249 LOGE("Malloc keyPair fail.");
1250 HcfObjDestroy((HcfObjectBase *)pubKey);
1251 pubKey = NULL;
1252 HcfObjDestroy((HcfObjectBase *)priKey);
1253 priKey = NULL;
1254 return HCF_ERR_MALLOC;
1255 }
1256 keyPair->base.priKey = (HcfPriKey *)priKey;
1257 keyPair->base.pubKey = (HcfPubKey *)pubKey;
1258 keyPair->base.base.getClass = GetOpensslKeyPairClass;
1259 keyPair->base.base.destroy = DestroyKeyPair;
1260 *returnKeyPair = (HcfKeyPair *)keyPair;
1261 return HCF_SUCCESS;
1262 }
1263
ParseRsaBnFromBin(const HcfAsyKeyParamsSpec * paramsSpec,BIGNUM ** n,BIGNUM ** e,BIGNUM ** d)1264 static HcfResult ParseRsaBnFromBin(const HcfAsyKeyParamsSpec *paramsSpec, BIGNUM **n,
1265 BIGNUM **e, BIGNUM **d)
1266 {
1267 // when meeting the fail situation, the BIGNUM will be NULL and other BIGNUM will be freeed in InitRsaStructByBin();
1268 if (BigIntegerToBigNum(&((HcfRsaCommParamsSpec *)paramsSpec)->n, n) != HCF_SUCCESS) {
1269 LOGD("[error] Rsa new BN n fail.");
1270 return HCF_ERR_CRYPTO_OPERATION;
1271 }
1272 if (paramsSpec->specType == HCF_KEY_PAIR_SPEC) {
1273 if (BigIntegerToBigNum(&((HcfRsaKeyPairParamsSpec *)paramsSpec)->pk, e) != HCF_SUCCESS) {
1274 LOGD("[error] Rsa new BN e fail.");
1275 OpensslBnFree(*n);
1276 *n = NULL;
1277 return HCF_ERR_CRYPTO_OPERATION;
1278 }
1279 if (BigIntegerToBigNum(&((HcfRsaKeyPairParamsSpec *)paramsSpec)->sk, d) != HCF_SUCCESS) {
1280 LOGD("[error] Rsa new BN d fail.");
1281 OpensslBnFree(*n);
1282 *n = NULL;
1283 OpensslBnFree(*e);
1284 *e = NULL;
1285 return HCF_ERR_CRYPTO_OPERATION;
1286 }
1287 }
1288 if (paramsSpec->specType == HCF_PUBLIC_KEY_SPEC) {
1289 if (BigIntegerToBigNum(&((HcfRsaPubKeyParamsSpec *)paramsSpec)->pk, e) != HCF_SUCCESS) {
1290 LOGD("[error] Rsa new BN e fail.");
1291 OpensslBnFree(*n);
1292 *n = NULL;
1293 return HCF_ERR_CRYPTO_OPERATION;
1294 }
1295 }
1296 return HCF_SUCCESS;
1297 }
1298
InitRsaStructByBin(const HcfAsyKeyParamsSpec * paramsSpec)1299 static RSA *InitRsaStructByBin(const HcfAsyKeyParamsSpec *paramsSpec)
1300 {
1301 BIGNUM *n = NULL;
1302 BIGNUM *e = NULL;
1303 BIGNUM *d = NULL;
1304 RSA *rsa = NULL;
1305
1306 if (ParseRsaBnFromBin(paramsSpec, &n, &e, &d) != HCF_SUCCESS) {
1307 LOGD("[error] ParseRsaBnFromBin fail");
1308 return rsa;
1309 }
1310 rsa = OpensslRsaNew();
1311 if (rsa == NULL) {
1312 OpensslBnFree(n);
1313 OpensslBnFree(e);
1314 OpensslBnClearFree(d);
1315 LOGD("[error] new RSA fail");
1316 return rsa;
1317 }
1318 // if set0 success, RSA object will take the owner of n, e, d and will free them.
1319 // as a new RSA object, in RSA_set0_key(), n and e cannot be NULL.
1320 if (OpensslRsaSet0Key(rsa, n, e, d) != HCF_OPENSSL_SUCCESS) {
1321 LOGD("[error] set RSA fail");
1322 HcfPrintOpensslError();
1323 OpensslBnFree(n);
1324 OpensslBnFree(e);
1325 OpensslBnClearFree(d);
1326 OpensslRsaFree(rsa);
1327 rsa = NULL;
1328 return rsa;
1329 }
1330 return rsa;
1331 }
1332
GenerateKeyPairBySpec(const HcfAsyKeyParamsSpec * paramsSpec,HcfKeyPair ** keyPair)1333 static HcfResult GenerateKeyPairBySpec(const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **keyPair)
1334 {
1335 // Generate keyPair RSA by spec
1336 RSA *rsa = InitRsaStructByBin(paramsSpec);
1337 if (rsa == NULL) {
1338 LOGD("[error] Generate RSA fail.");
1339 return HCF_ERR_CRYPTO_OPERATION;
1340 }
1341 HcfOpensslRsaKeyPair *keyPairImpl = (HcfOpensslRsaKeyPair *)HcfMalloc(sizeof(HcfOpensslRsaKeyPair), 0);
1342 if (keyPairImpl == NULL) {
1343 LOGE("Malloc keyPair fail.");
1344 OpensslRsaFree(rsa);
1345 return HCF_ERR_MALLOC;
1346 }
1347 // devided to pk and sk;
1348 HcfOpensslRsaPubKey *pubKeyImpl = NULL;
1349 HcfOpensslRsaPriKey *priKeyImpl = NULL;
1350
1351 RSA *pubKeyRsa = NULL;
1352 if (DuplicateRsa(rsa, false, &pubKeyRsa) != HCF_SUCCESS) {
1353 LOGD("[error] Duplicate pubKey rsa fail");
1354 OpensslRsaFree(rsa);
1355 HcfFree(keyPairImpl);
1356 keyPairImpl = NULL;
1357 return HCF_ERR_CRYPTO_OPERATION;
1358 }
1359
1360 HcfResult res = PackPubKey(pubKeyRsa, &pubKeyImpl);
1361 if (res != HCF_SUCCESS) {
1362 LOGE("pack pup key fail.");
1363 OpensslRsaFree(rsa);
1364 OpensslRsaFree(pubKeyRsa);
1365 HcfFree(keyPairImpl);
1366 keyPairImpl = NULL;
1367 return res;
1368 }
1369
1370 res = PackPriKey(rsa, &priKeyImpl);
1371 if (res != HCF_SUCCESS) {
1372 LOGE("pack pri key fail.");
1373 OpensslRsaFree(rsa);
1374 OpensslRsaFree(pubKeyRsa);
1375 HcfFree(keyPairImpl);
1376 keyPairImpl = NULL;
1377 HcfFree(pubKeyImpl);
1378 pubKeyImpl = NULL;
1379 return res;
1380 }
1381 keyPairImpl->base.priKey = (HcfPriKey *)priKeyImpl;
1382 keyPairImpl->base.pubKey = (HcfPubKey *)pubKeyImpl;
1383 keyPairImpl->base.base.getClass = GetOpensslKeyPairClass;
1384 keyPairImpl->base.base.destroy = DestroyKeyPair;
1385 *keyPair = (HcfKeyPair *)keyPairImpl;
1386 LOGD("Generate keypair success.");
1387 return res;
1388 }
1389
GeneratePubKeyBySpec(const HcfAsyKeyParamsSpec * paramsSpec,HcfPubKey ** pubKey)1390 static HcfResult GeneratePubKeyBySpec(const HcfAsyKeyParamsSpec *paramsSpec, HcfPubKey **pubKey)
1391 {
1392 RSA *rsa = InitRsaStructByBin(paramsSpec);
1393 if (rsa == NULL) {
1394 LOGD("[error] Generate RSA fail.");
1395 return HCF_ERR_CRYPTO_OPERATION;
1396 }
1397 RSA *pubKeyRsa = NULL;
1398 if (DuplicateRsa(rsa, false, &pubKeyRsa) != HCF_SUCCESS) {
1399 LOGD("[error] Duplicate pubKey rsa fail");
1400 OpensslRsaFree(rsa);
1401 return HCF_ERR_CRYPTO_OPERATION;
1402 }
1403 HcfOpensslRsaPubKey *pubKeyImpl = NULL;
1404 HcfResult res = PackPubKey(pubKeyRsa, &pubKeyImpl);
1405 if (res != HCF_SUCCESS) {
1406 LOGD("[error] pack pup key fail.");
1407 OpensslRsaFree(rsa);
1408 OpensslRsaFree(pubKeyRsa);
1409 return res;
1410 }
1411 *pubKey = (HcfPubKey *)pubKeyImpl;
1412 OpensslRsaFree(rsa);
1413 LOGD("Generate pub key success.");
1414 return res;
1415 }
1416
GeneratePriKeyBySpec(const HcfAsyKeyParamsSpec * paramsSpec,HcfPriKey ** priKey)1417 static HcfResult GeneratePriKeyBySpec(const HcfAsyKeyParamsSpec *paramsSpec, HcfPriKey **priKey)
1418 {
1419 RSA *rsa = InitRsaStructByBin(paramsSpec);
1420 if (rsa == NULL) {
1421 LOGD("[error] Generate RSA fail.");
1422 return HCF_ERR_CRYPTO_OPERATION;
1423 }
1424 HcfOpensslRsaPriKey *priKeyImpl = NULL;
1425 HcfResult res = PackPriKey(rsa, &priKeyImpl);
1426 if (res != HCF_SUCCESS) {
1427 LOGD("[error] pack pri key fail.");
1428 OpensslRsaFree(rsa);
1429 return res;
1430 }
1431 *priKey = (HcfPriKey *)priKeyImpl;
1432 LOGD("Generate pri key success.");
1433 return res;
1434 }
1435
EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfKeyPair ** returnKeyPair)1436 static HcfResult EngineGenerateKeyPairBySpec(const HcfAsyKeyGeneratorSpi *self,
1437 const HcfAsyKeyParamsSpec *paramsSpec, HcfKeyPair **returnKeyPair)
1438 {
1439 if ((self == NULL) || (returnKeyPair == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL)) {
1440 LOGE("GenerateKeyPairBySpec Params is invalid.");
1441 return HCF_INVALID_PARAMS;
1442 }
1443 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1444 LOGE("Class not match.");
1445 return HCF_INVALID_PARAMS;
1446 }
1447 if (strcmp(paramsSpec->algName, RSA_ALG_NAME) != 0) {
1448 LOGE("Spec alg not match.");
1449 return HCF_INVALID_PARAMS;
1450 }
1451 if (paramsSpec->specType != HCF_KEY_PAIR_SPEC) {
1452 LOGE("Spec type not match.");
1453 return HCF_INVALID_PARAMS;
1454 }
1455 return GenerateKeyPairBySpec(paramsSpec, returnKeyPair);
1456 }
1457
EngineGeneratePubKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPubKey ** returnPubKey)1458 static HcfResult EngineGeneratePubKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
1459 const HcfAsyKeyParamsSpec *paramsSpec, HcfPubKey **returnPubKey)
1460 {
1461 if ((self == NULL) || (returnPubKey == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL)) {
1462 LOGE("GeneratePubKeyBySpec Params is invalid.");
1463 return HCF_INVALID_PARAMS;
1464 }
1465 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1466 LOGE("Class not match.");
1467 return HCF_INVALID_PARAMS;
1468 }
1469 if (strcmp(paramsSpec->algName, RSA_ALG_NAME) != 0) {
1470 LOGE("Spec alg not match.");
1471 return HCF_INVALID_PARAMS;
1472 }
1473 if (paramsSpec->specType != HCF_PUBLIC_KEY_SPEC && paramsSpec->specType != HCF_KEY_PAIR_SPEC) {
1474 LOGE("Spec not match.");
1475 return HCF_INVALID_PARAMS;
1476 }
1477 return GeneratePubKeyBySpec(paramsSpec, returnPubKey);
1478 }
1479
EngineGeneratePriKeyBySpec(const HcfAsyKeyGeneratorSpi * self,const HcfAsyKeyParamsSpec * paramsSpec,HcfPriKey ** returnPriKey)1480 static HcfResult EngineGeneratePriKeyBySpec(const HcfAsyKeyGeneratorSpi *self,
1481 const HcfAsyKeyParamsSpec *paramsSpec, HcfPriKey **returnPriKey)
1482 {
1483 if ((self == NULL) || (returnPriKey == NULL) || (paramsSpec == NULL) || (paramsSpec->algName == NULL)) {
1484 LOGE("GeneratePriKeyBySpec Params is invalid.");
1485 return HCF_INVALID_PARAMS;
1486 }
1487 if (!HcfIsClassMatch((HcfObjectBase *)self, OPENSSL_RSA_GENERATOR_CLASS)) {
1488 LOGE("Class not match.");
1489 return HCF_INVALID_PARAMS;
1490 }
1491 if (strcmp(paramsSpec->algName, RSA_ALG_NAME) != 0) {
1492 LOGE("Spec alg not match.");
1493 return HCF_INVALID_PARAMS;
1494 }
1495 if (paramsSpec->specType != HCF_KEY_PAIR_SPEC) {
1496 LOGE("Spec not match.");
1497 return HCF_INVALID_PARAMS;
1498 }
1499 return GeneratePriKeyBySpec(paramsSpec, returnPriKey);
1500 }
1501
SetDefaultValue(HcfAsyKeyGenSpiRsaParams * params)1502 static HcfResult SetDefaultValue(HcfAsyKeyGenSpiRsaParams *params)
1503 {
1504 if (params->primes == 0) {
1505 LOGD("set default primes 2");
1506 params->primes = OPENSSL_RSA_PRIMES_SIZE_2;
1507 }
1508 if (params->pubExp != NULL) {
1509 LOGE("RSA has pubKey default unexpectedly.");
1510 return HCF_SUCCESS;
1511 }
1512 BIGNUM *e = OpensslBnNew();
1513 if (e == NULL) {
1514 LOGD("[error] RSA new BN fail.");
1515 return HCF_ERR_CRYPTO_OPERATION;
1516 }
1517 if (OpensslBnSetWord(e, RSA_F4) != HCF_OPENSSL_SUCCESS) {
1518 LOGD("[error] RSA keygen Bn_set_word fail.");
1519 OpensslBnFree(e);
1520 return HCF_ERR_CRYPTO_OPERATION;
1521 }
1522 params->pubExp = e;
1523 return HCF_SUCCESS;
1524 }
1525
DecodeParams(HcfAsyKeyGenParams * from,HcfAsyKeyGenSpiRsaParams ** to)1526 static HcfResult DecodeParams(HcfAsyKeyGenParams *from, HcfAsyKeyGenSpiRsaParams **to)
1527 {
1528 *to = (HcfAsyKeyGenSpiRsaParams *)HcfMalloc(sizeof(HcfAsyKeyGenSpiRsaParams), 0);
1529 if (*to == NULL) {
1530 LOGE("Malloc HcfAsyKeyGenSpiRsaParams fail");
1531 return HCF_ERR_MALLOC;
1532 }
1533
1534 (*to)->bits = from->bits;
1535 (*to)->primes = from->primes;
1536
1537 // set 2 as default primes, RSA_F4 as default pubExp
1538 if (SetDefaultValue(*to) != HCF_SUCCESS) {
1539 LOGE("Set default value fail.");
1540 HcfFree(*to);
1541 *to = NULL;
1542 return HCF_INVALID_PARAMS;
1543 }
1544 if (CheckRsaKeyGenParams(*to) != HCF_SUCCESS) {
1545 LOGE("Invalid keyGen params");
1546 OpensslBnFree((*to)->pubExp);
1547 HcfFree(*to);
1548 *to = NULL;
1549 return HCF_INVALID_PARAMS;
1550 }
1551 return HCF_SUCCESS;
1552 }
1553
HcfAsyKeyGeneratorSpiRsaCreate(HcfAsyKeyGenParams * params,HcfAsyKeyGeneratorSpi ** generator)1554 HcfResult HcfAsyKeyGeneratorSpiRsaCreate(HcfAsyKeyGenParams *params, HcfAsyKeyGeneratorSpi **generator)
1555 {
1556 if (params == NULL || generator == NULL) {
1557 LOGE("Invalid input, params is invalid or generator is null.");
1558 return HCF_INVALID_PARAMS;
1559 }
1560 HcfAsyKeyGeneratorSpiRsaOpensslImpl *impl = (HcfAsyKeyGeneratorSpiRsaOpensslImpl *)
1561 HcfMalloc(sizeof(HcfAsyKeyGeneratorSpiRsaOpensslImpl), 0);
1562 if (impl == NULL) {
1563 LOGE("Failed to allocate returnImpl memroy!");
1564 return HCF_ERR_MALLOC;
1565 }
1566 if (DecodeParams(params, &impl->params) != HCF_SUCCESS) {
1567 LOGE("Keygen params is invalid.");
1568 HcfFree(impl);
1569 impl = NULL;
1570 return HCF_INVALID_PARAMS;
1571 }
1572 impl->base.base.getClass = GetKeyGeneratorClass;
1573 impl->base.base.destroy = DestroyKeyGeneratorSpiImpl;
1574 impl->base.engineGenerateKeyPair = EngineGenerateKeyPair;
1575 impl->base.engineConvertKey = EngineConvertKey;
1576 impl->base.engineConvertPemKey = EngineConvertPemKey;
1577 impl->base.engineGenerateKeyPairBySpec = EngineGenerateKeyPairBySpec;
1578 impl->base.engineGeneratePubKeyBySpec = EngineGeneratePubKeyBySpec;
1579 impl->base.engineGeneratePriKeyBySpec = EngineGeneratePriKeyBySpec;
1580 *generator = (HcfAsyKeyGeneratorSpi *)impl;
1581 return HCF_SUCCESS;
1582 }
1583