• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "rsa_openssl_common.h"
16 #include "log.h"
17 #include "openssl_common.h"
18 
DuplicateRsa(RSA * rsa,bool needPrivate,RSA ** dupRsa)19 HcfResult DuplicateRsa(RSA *rsa, bool needPrivate, RSA **dupRsa)
20 {
21     RSA *retRSA = NULL;
22     if (rsa == NULL || dupRsa == NULL) {
23         LOGE("Rsa or dupRsa is NULL.");
24         return HCF_INVALID_PARAMS;
25     }
26     if (needPrivate) {
27         retRSA = RSAPrivateKey_dup(rsa);
28     } else {
29         retRSA = RSAPublicKey_dup(rsa);
30     }
31     if (retRSA == NULL) {
32         LOGE("Duplicate RSA fail.");
33         HcfPrintOpensslError();
34         return HCF_ERR_CRYPTO_OPERATION;
35     }
36     *dupRsa = retRSA;
37     return HCF_SUCCESS;
38 }
39 
NewEvpPkeyByRsa(RSA * rsa,bool withDuplicate)40 EVP_PKEY *NewEvpPkeyByRsa(RSA *rsa, bool withDuplicate)
41 {
42     if (rsa == NULL) {
43         LOGE("RSA is NULL");
44         return NULL;
45     }
46     EVP_PKEY *pKey = EVP_PKEY_new();
47     if (pKey == NULL) {
48         LOGE("EVP_PKEY_new fail");
49         HcfPrintOpensslError();
50         return NULL;
51     }
52     if (withDuplicate) {
53         if (EVP_PKEY_set1_RSA(pKey, rsa) != HCF_OPENSSL_SUCCESS) {
54             LOGE("EVP_PKEY_set1_RSA fail");
55             HcfPrintOpensslError();
56             EVP_PKEY_free(pKey);
57             return NULL;
58         }
59     } else {
60         if (EVP_PKEY_assign_RSA(pKey, rsa) != HCF_OPENSSL_SUCCESS) {
61             LOGE("EVP_PKEY_assign_RSA fail");
62             HcfPrintOpensslError();
63             EVP_PKEY_free(pKey);
64             return NULL;
65         }
66     }
67     return pKey;
68 }
69