• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the openHiTLS project.
3  *
4  * openHiTLS is licensed under the Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *
8  *     http://license.coscl.org.cn/MulanPSL2
9  *
10  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13  * See the Mulan PSL v2 for more details.
14  */
15 #include "hitls_build.h"
16 #include "securec.h"
17 #include "bsl_sal.h"
18 #include "tls_binlog_id.h"
19 #include "hitls_cert_type.h"
20 #include "cert_method.h"
21 #include "cert_mgr.h"
22 #include "cert_mgr_ctx.h"
23 
SAL_CERT_PairGetX509(CERT_Pair * certPair)24 HITLS_CERT_X509 *SAL_CERT_PairGetX509(CERT_Pair *certPair)
25 {
26     if (certPair == NULL) {
27         return NULL;
28     }
29     return certPair->cert;
30 }
31 
32 #ifdef HITLS_TLS_PROTO_TLCP11
SAL_CERT_GetTlcpEncCert(CERT_Pair * certPair)33 HITLS_CERT_X509 *SAL_CERT_GetTlcpEncCert(CERT_Pair *certPair)
34 {
35     if (certPair == NULL) {
36         return NULL;
37     }
38     return certPair->encCert;
39 }
40 #endif
41 #if defined(HITLS_TLS_CONNECTION_INFO_NEGOTIATION)
SAL_CERT_PairGetChain(CERT_Pair * certPair)42 HITLS_CERT_Chain *SAL_CERT_PairGetChain(CERT_Pair *certPair)
43 {
44     if (certPair == NULL) {
45         return NULL;
46     }
47     return certPair->chain;
48 }
49 #endif /* HITLS_TLS_CONNECTION_INFO_NEGOTIATION */
50 
51 #ifdef HITLS_TLS_PROTO_TLCP11
TlcpCertPairDup(CERT_MgrCtx * mgrCtx,CERT_Pair * srcCertPair,CERT_Pair * destCertPair)52 static int32_t TlcpCertPairDup(CERT_MgrCtx *mgrCtx, CERT_Pair *srcCertPair, CERT_Pair *destCertPair)
53 {
54     if (srcCertPair->encCert != NULL) {
55         destCertPair->encCert = SAL_CERT_X509Dup(mgrCtx, srcCertPair->encCert);
56         if (destCertPair->encCert == NULL) {
57             BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17341, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
58                 "enc X509Dup fail", 0, 0, 0, 0);
59             return HITLS_CERT_ERR_X509_DUP;
60         }
61     }
62 
63     if (srcCertPair->encPrivateKey != NULL) {
64         destCertPair->encPrivateKey = SAL_CERT_KeyDup(mgrCtx, srcCertPair->encPrivateKey);
65         if (destCertPair->encPrivateKey == NULL) {
66             BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17342, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
67                 "enc KeyDup fail", 0, 0, 0, 0);
68             return HITLS_CERT_ERR_X509_DUP;
69         }
70     }
71     return  HITLS_SUCCESS;
72 }
73 #endif
74 
SAL_CERT_PairDup(CERT_MgrCtx * mgrCtx,CERT_Pair * srcCertPair)75 CERT_Pair *SAL_CERT_PairDup(CERT_MgrCtx *mgrCtx, CERT_Pair *srcCertPair)
76 {
77     CERT_Pair *destCertPair = BSL_SAL_Calloc(1, sizeof(CERT_MgrCtx));
78     if (destCertPair == NULL) {
79         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16299, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN, "Calloc fail", 0, 0, 0, 0);
80         return NULL;
81     }
82 
83     do {
84 #ifdef HITLS_TLS_PROTO_TLCP11
85         if (TlcpCertPairDup(mgrCtx, srcCertPair, destCertPair) != HITLS_SUCCESS) {
86             break;
87         }
88 #endif
89 
90         if (srcCertPair->cert != NULL) {
91             destCertPair->cert = SAL_CERT_X509Dup(mgrCtx, srcCertPair->cert);
92             if (destCertPair->cert == NULL) {
93                 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16300, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
94                     "X509Dup fail", 0, 0, 0, 0);
95                 break;
96             }
97         }
98 
99         if (srcCertPair->privateKey != NULL) {
100             destCertPair->privateKey = SAL_CERT_KeyDup(mgrCtx, srcCertPair->privateKey);
101             if (destCertPair->privateKey == NULL) {
102                 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16301, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
103                     "KeyDup fail", 0, 0, 0, 0);
104                 break;
105             }
106         }
107 
108         if (srcCertPair->chain != NULL) {
109             destCertPair->chain = SAL_CERT_ChainDup(mgrCtx, srcCertPair->chain);
110             if (destCertPair->chain == NULL) {
111                 BSL_LOG_BINLOG_FIXLEN(BINLOG_ID16302, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
112                     "ChainDup fail", 0, 0, 0, 0);
113                 break;
114             }
115         }
116         return destCertPair;
117     } while (false);
118     SAL_CERT_PairFree(mgrCtx, destCertPair);
119     return NULL;
120 }
121 
SAL_CERT_PairClear(CERT_MgrCtx * mgrCtx,CERT_Pair * certPair)122 void SAL_CERT_PairClear(CERT_MgrCtx *mgrCtx, CERT_Pair *certPair)
123 {
124     if (mgrCtx == NULL || certPair == NULL) {
125         return;
126     }
127 
128     if (certPair->cert != NULL) {
129         SAL_CERT_X509Free(certPair->cert);
130     }
131 #ifdef HITLS_TLS_PROTO_TLCP11
132     if (certPair->encCert != NULL) {
133         SAL_CERT_X509Free(certPair->encCert);
134     }
135     if (certPair->encPrivateKey != NULL) {
136         SAL_CERT_KeyFree(mgrCtx, certPair->encPrivateKey);
137     }
138 #endif
139     if (certPair->privateKey != NULL) {
140         SAL_CERT_KeyFree(mgrCtx, certPair->privateKey);
141     }
142 
143     if (certPair->chain != NULL) {
144         SAL_CERT_ChainFree(certPair->chain);
145     }
146 
147     (void)memset_s(certPair, sizeof(CERT_Pair), 0, sizeof(CERT_Pair));
148     return;
149 }
150 
SAL_CERT_PairFree(CERT_MgrCtx * mgrCtx,CERT_Pair * certPair)151 void SAL_CERT_PairFree(CERT_MgrCtx *mgrCtx, CERT_Pair *certPair)
152 {
153     SAL_CERT_PairClear(mgrCtx, certPair);
154     BSL_SAL_FREE(certPair);
155     return;
156 }
157 
SAL_CERT_HashDup(CERT_MgrCtx * destMgrCtx,CERT_MgrCtx * srcMgrCtx)158 int32_t SAL_CERT_HashDup(CERT_MgrCtx *destMgrCtx, CERT_MgrCtx *srcMgrCtx)
159 {
160     destMgrCtx->certPairs = BSL_HASH_Create(CERT_DEFAULT_HASH_BKT_SIZE, NULL, NULL, NULL, NULL);
161     if (destMgrCtx->certPairs == NULL) {
162         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID17347, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
163             "BSL_HASH_Create fail", 0, 0, 0, 0);
164         return HITLS_MEMALLOC_FAIL;
165     }
166 
167     BSL_HASH_Hash *certPairs = srcMgrCtx->certPairs;
168     BSL_HASH_Iterator iter = BSL_HASH_IterBegin(certPairs);
169     while (iter != BSL_HASH_IterEnd(certPairs)) {
170         uint32_t keyType = (uint32_t)BSL_HASH_HashIterKey(certPairs, iter);
171         CERT_Pair *certPair = (CERT_Pair *)BSL_HASH_IterValue(certPairs, iter);
172         if (certPair != NULL) {
173             CERT_Pair *newCertPair = SAL_CERT_PairDup(srcMgrCtx, certPair);
174             if (newCertPair == NULL) {
175                 return RETURN_ERROR_NUMBER_PROCESS(HITLS_CERT_ERR_X509_DUP, BINLOG_ID17348, "x509dup fail");
176             }
177             int32_t ret = BSL_HASH_Insert(destMgrCtx->certPairs, keyType, 0, (uintptr_t)newCertPair, sizeof(CERT_Pair));
178             if (ret != HITLS_SUCCESS) {
179                 SAL_CERT_PairFree(destMgrCtx, newCertPair);
180                 return RETURN_ERROR_NUMBER_PROCESS(ret, BINLOG_ID17349, "insert fail");
181             }
182         }
183         iter = BSL_HASH_IterNext(certPairs, iter);
184     }
185     return HITLS_SUCCESS;
186 }