• 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 
16 #include <stdint.h>
17 #include "hitls_build.h"
18 #include "securec.h"
19 #include "tls_binlog_id.h"
20 #include "bsl_log_internal.h"
21 #include "bsl_log.h"
22 #include "bsl_err_internal.h"
23 #include "bsl_bytes.h"
24 #include "cert.h"
25 #include "hitls_error.h"
26 #include "tls.h"
27 #include "hs_ctx.h"
28 #include "hs_common.h"
29 #include "hs_extensions.h"
30 #include "pack_common.h"
31 
32 #if defined(HITLS_TLS_PROTO_TLS_BASIC) || defined(HITLS_TLS_PROTO_DTLS12)
PackCertificate(TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)33 int32_t PackCertificate(TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
34 {
35     int32_t ret = HITLS_SUCCESS;
36 
37     if (bufLen < CERT_LEN_TAG_SIZE) {
38         return PackBufLenError(BINLOG_ID15808, BINGLOG_STR("cert"));
39     }
40 
41     /* Certificate content */
42     ret = SAL_CERT_EncodeCertChain(ctx, &buf[CERT_LEN_TAG_SIZE], bufLen - CERT_LEN_TAG_SIZE, usedLen);
43     if (ret != HITLS_SUCCESS) {
44         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15809, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
45             "encode cert list fail.", 0, 0, 0, 0);
46         return ret;
47     }
48 
49     /* Certificate length */
50     BSL_Uint24ToByte(*usedLen, buf);
51     *usedLen += CERT_LEN_TAG_SIZE;
52     return HITLS_SUCCESS;
53 }
54 #endif /* HITLS_TLS_PROTO_TLS_BASIC || HITLS_TLS_PROTO_DTLS12 */
55 #ifdef HITLS_TLS_PROTO_TLS13
Tls13PackCertificate(TLS_Ctx * ctx,uint8_t * buf,uint32_t bufLen,uint32_t * usedLen)56 int32_t Tls13PackCertificate(TLS_Ctx *ctx, uint8_t *buf, uint32_t bufLen, uint32_t *usedLen)
57 {
58     int32_t ret = HITLS_SUCCESS;
59     uint32_t offset = 0;
60 
61     if (bufLen < (CERT_LEN_TAG_SIZE + ctx->certificateReqCtxSize + sizeof(uint16_t))) {
62         return PackBufLenError(BINLOG_ID15810, BINGLOG_STR("cert"));
63     }
64     /* Pack the length of certificate_request_context */
65     buf[offset] = (uint8_t)ctx->certificateReqCtxSize;
66     offset++;
67 
68     /* Pack the content of certificate_request_context */
69     if (ctx->certificateReqCtxSize > 0) {
70         (void)memcpy_s(&buf[offset], bufLen - offset, ctx->certificateReqCtx, ctx->certificateReqCtxSize);
71         offset += ctx->certificateReqCtxSize;
72     }
73 
74     uint32_t certLenFieldOffset = offset;
75     offset += CERT_LEN_TAG_SIZE;
76 
77     /* Certificate content */
78     ret = SAL_CERT_EncodeCertChain(ctx, &buf[offset], bufLen - offset, usedLen);
79     if (ret != HITLS_SUCCESS) {
80         BSL_LOG_BINLOG_FIXLEN(BINLOG_ID15811, BSL_LOG_LEVEL_ERR, BSL_LOG_BINLOG_TYPE_RUN,
81             "encode cert list fail when pack certificate msg.", 0, 0, 0, 0);
82         return ret;
83     }
84 
85     /* Certificate length */
86     BSL_Uint24ToByte(*usedLen, &buf[certLenFieldOffset]);
87     *usedLen += offset;
88     return HITLS_SUCCESS;
89 }
90 #endif