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