• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 #include <unistd.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <arpa/inet.h>
8 
9 #include "securec.h"
10 
11 #include "bsl_sal.h"
12 #include "bsl_err.h"
13 #include "crypt_eal_init.h"
14 #include "crypt_algid.h"
15 #include "crypt_eal_rand.h"
16 #include "hitls_error.h"
17 #include "hitls_config.h"
18 #include "hitls.h"
19 #include "hitls_cert_init.h"
20 #include "hitls_cert.h"
21 #include "hitls_crypt_init.h"
22 #include "hitls_pki_cert.h"
23 #include "crypt_errno.h"
24 
25 #define CERTS_PATH      "../../../testcode/testdata/tls/certificate/der/ecdsa_sha256/"
26 #define HTTP_BUF_MAXLEN (18 * 1024) /* 18KB */
27 
main(int32_t argc,char * argv[])28 int main(int32_t argc, char *argv[])
29 {
30     int32_t exitValue = -1;
31     int32_t ret = 0;
32     HITLS_Config *config = NULL;
33     HITLS_Ctx *ctx = NULL;
34     BSL_UIO *uio = NULL;
35     int fd = 0;
36     HITLS_X509_Cert *rootCA = NULL;
37     HITLS_X509_Cert *subCA = NULL;
38 
39     /* 注册BSL内存能力、仅供参考 */
40     BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_MALLOC, malloc);
41     BSL_SAL_CallBack_Ctrl(BSL_SAL_MEM_FREE, free);
42     BSL_ERR_Init();
43 
44     ret = CRYPT_EAL_Init(CRYPT_EAL_INIT_CPU | CRYPT_EAL_INIT_PROVIDER);
45     if (ret != CRYPT_SUCCESS) {
46         printf("CRYPT_EAL_Init: error code is %x\n", ret);
47         return ret;
48     }
49 
50     ret = CRYPT_EAL_ProviderRandInitCtx(NULL, CRYPT_RAND_SHA256, "provider=default", NULL, 0, NULL);
51     if (ret != CRYPT_SUCCESS) {
52         printf("Init rand failed.\n");
53         goto EXIT;
54     }
55     HITLS_CertMethodInit();
56     HITLS_CryptMethodInit();
57 
58     fd = socket(AF_INET, SOCK_STREAM, 0);
59     if (fd == -1) {
60         printf("Create socket failed.\n");
61         goto EXIT;
62     }
63     int option = 1;
64     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)) < 0) {
65         close(fd);
66         printf("setsockopt SO_REUSEADDR failed.\n");
67         goto EXIT;
68     }
69 
70     // Set the protocol and port number
71     struct sockaddr_in serverAddr;
72     (void)memset_s(&serverAddr, sizeof(serverAddr), 0, sizeof(serverAddr));
73     serverAddr.sin_family = AF_INET;
74     serverAddr.sin_port = htons(12345);
75     serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
76 
77     if (connect(fd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0) {
78         printf("connect failed.\n");
79         goto EXIT;
80     }
81 
82     config = HITLS_CFG_NewTLS12Config();
83     if (config == NULL) {
84         printf("HITLS_CFG_NewTLS12Config failed.\n");
85         goto EXIT;
86     }
87     ret = HITLS_CFG_SetCheckKeyUsage(config, false); // disable cert keyusage check
88     if (ret != HITLS_SUCCESS) {
89         printf("Disable check KeyUsage failed.\n");
90         goto EXIT;
91     }
92 
93     /* 加载证书:需要用户实现 */
94     ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, CERTS_PATH "ca.der", &rootCA);
95     if (ret != HITLS_SUCCESS) {
96         printf("Parse ca failed.\n");
97         goto EXIT;
98     }
99     ret = HITLS_X509_CertParseFile(BSL_FORMAT_ASN1, CERTS_PATH "inter.der", &subCA);
100     if (ret != HITLS_SUCCESS) {
101         printf("Parse subca failed.\n");
102         goto EXIT;
103     }
104     HITLS_CFG_AddCertToStore(config, rootCA, TLS_CERT_STORE_TYPE_DEFAULT, true);
105     HITLS_CFG_AddCertToStore(config, subCA, TLS_CERT_STORE_TYPE_DEFAULT, true);
106 
107     /* 新建openHiTLS上下文 */
108     ctx = HITLS_New(config);
109     if (ctx == NULL) {
110         printf("HITLS_New failed.\n");
111         goto EXIT;
112     }
113 
114     uio = BSL_UIO_New(BSL_UIO_TcpMethod());
115     if (uio == NULL) {
116         printf("BSL_UIO_New failed.\n");
117         goto EXIT;
118     }
119 
120     ret = BSL_UIO_Ctrl(uio, BSL_UIO_SET_FD, (int32_t)sizeof(fd), &fd);
121     if (ret != HITLS_SUCCESS) {
122         BSL_UIO_Free(uio);
123         printf("BSL_UIO_SET_FD failed, fd = %u.\n", fd);
124         goto EXIT;
125     }
126 
127     ret = HITLS_SetUio(ctx, uio);
128     if (ret != HITLS_SUCCESS) {
129         BSL_UIO_Free(uio);
130         printf("HITLS_SetUio failed. ret = 0x%x.\n", ret);
131         goto EXIT;
132     }
133 
134     /* 进行TLS连接、用户需按实际场景考虑返回值 */
135     ret = HITLS_Connect(ctx);
136     if (ret != HITLS_SUCCESS) {
137         printf("HITLS_Connect failed, ret = 0x%x.\n", ret);
138         goto EXIT;
139     }
140 
141     /* 向对端发送报文、用户需按实际场景考虑返回值 */
142     const uint8_t sndBuf[] = "Hi, this is client\n";
143     uint32_t writeLen = 0;
144     ret = HITLS_Write(ctx, sndBuf, sizeof(sndBuf), &writeLen);
145     if (ret != HITLS_SUCCESS) {
146         printf("HITLS_Write error:error code:%d\n", ret);
147         goto EXIT;
148     }
149 
150     /* 读取对端报文、用户需按实际场景考虑返回值 */
151     uint8_t readBuf[HTTP_BUF_MAXLEN + 1] = {0};
152     uint32_t readLen = 0;
153     ret = HITLS_Read(ctx, readBuf, HTTP_BUF_MAXLEN, &readLen);
154     if (ret != HITLS_SUCCESS) {
155         printf("HITLS_Read failed, ret = 0x%x.\n", ret);
156         goto EXIT;
157     }
158 
159     printf("get from server size:%u :%s\n", readLen, readBuf);
160 
161     exitValue = 0;
162 EXIT:
163     HITLS_Close(ctx);
164     HITLS_Free(ctx);
165     HITLS_CFG_FreeConfig(config);
166     close(fd);
167     HITLS_X509_CertFree(rootCA);
168     HITLS_X509_CertFree(subCA);
169     BSL_UIO_Free(uio);
170     return exitValue;
171 }