• 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 <stdint.h>
16 #include "securec.h"
17 #include "bsl_uio.h"
18 #include "bsl_errno.h"
19 #include "bsl_err_internal.h"
20 
21 #define BSL_ASN1_PRINT_LEN 1024
22 #define X509_PRINT_MAX_LAYER 10
23 #define X509_PRINT_EACH_LAYER_INDENT 4
24 #define X509_PRINT_MAX_INDENT ((X509_PRINT_EACH_LAYER_INDENT) * (X509_PRINT_MAX_LAYER))
25 
BSL_ASN1_PrintfBuff(uint32_t layer,BSL_UIO * uio,const void * buff,uint32_t buffLen)26 int32_t BSL_ASN1_PrintfBuff(uint32_t layer, BSL_UIO *uio, const void *buff, uint32_t buffLen)
27 {
28     int32_t ret;
29     uint32_t writeLen = 0;
30     char *indent[X509_PRINT_MAX_INDENT + 1] = {};
31     (void)memset_s(indent, X509_PRINT_MAX_INDENT, ' ', X509_PRINT_MAX_INDENT);
32     if (layer > 0) {
33         ret = BSL_UIO_Write(uio, indent, layer * X509_PRINT_EACH_LAYER_INDENT, &writeLen);
34         if (ret != BSL_SUCCESS) {
35             BSL_ERR_PUSH_ERROR(ret);
36             return ret;
37         }
38         if (writeLen != (layer * X509_PRINT_EACH_LAYER_INDENT)) {
39             BSL_ERR_PUSH_ERROR(BSL_ASN1_ERR_PRINTF_IO_ERR);
40             return BSL_ASN1_ERR_PRINTF_IO_ERR;
41         }
42     }
43     if (buffLen == 0) {
44         return BSL_SUCCESS;
45     }
46     writeLen = 0;
47     ret = BSL_UIO_Write(uio, buff, buffLen, &writeLen);
48     if (ret != BSL_SUCCESS) {
49         BSL_ERR_PUSH_ERROR(ret);
50         return ret;
51     }
52     if (writeLen != buffLen) {
53         BSL_ERR_PUSH_ERROR(BSL_ASN1_ERR_PRINTF_IO_ERR);
54         return BSL_ASN1_ERR_PRINTF_IO_ERR;
55     }
56 
57     return BSL_SUCCESS;
58 }
59 
BSL_ASN1_Printf(uint32_t layer,BSL_UIO * uio,const char * fmt,...)60 int32_t BSL_ASN1_Printf(uint32_t layer, BSL_UIO *uio, const char *fmt, ...)
61 {
62     if (layer > X509_PRINT_MAX_LAYER || uio == NULL || fmt == NULL) {
63         BSL_ERR_PUSH_ERROR(BSL_INVALID_ARG);
64         return BSL_INVALID_ARG;
65     }
66     va_list args;
67     va_start(args, fmt);
68     char buff[BSL_ASN1_PRINT_LEN + 1] = {0};
69     if (vsprintf_s(buff, BSL_ASN1_PRINT_LEN + 1, fmt, args) == -1) {
70         va_end(args);
71         BSL_ERR_PUSH_ERROR(BSL_ASN1_ERR_PRINTF);
72         return BSL_ASN1_ERR_PRINTF;
73     }
74     int32_t ret = BSL_ASN1_PrintfBuff(layer, uio, buff, (uint32_t)strlen(buff));
75     va_end(args);
76 
77     return ret;
78 }
79