-
Notifications
You must be signed in to change notification settings - Fork 203
/
polarsslbio.cpp
144 lines (117 loc) · 4.03 KB
/
polarsslbio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "config.h"
#include <string.h>
#include "mytime.h"
#include "polarsslbio.h"
#if OPENSSL ==0
#if ISMBEDTLS
int ssl_init_info(int *server_fd,ssl_info *sslinfo)
{
int ret;
const char *pers = "ssl";
mbedtls_x509_crt_init(&sslinfo->cacert );
mbedtls_ctr_drbg_init(&sslinfo->ctr_drbg);
mbedtls_entropy_init(&sslinfo->entropy );
if( mbedtls_ctr_drbg_seed( &sslinfo->ctr_drbg, mbedtls_entropy_func, &sslinfo->entropy,
(const unsigned char *) pers, strlen( pers ) ) != 0 )
{
return -1;
}
mbedtls_ssl_init( &sslinfo->ssl );
mbedtls_ssl_config_init( &sslinfo->conf );
if( ( ret = mbedtls_ssl_config_defaults( &sslinfo->conf,
MBEDTLS_SSL_IS_CLIENT,
MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
{
echo( " failed\n ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
return -1;
}
mbedtls_ssl_conf_endpoint( &sslinfo->conf, MBEDTLS_SSL_IS_CLIENT );
mbedtls_ssl_conf_authmode( &sslinfo->conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
mbedtls_ssl_conf_ca_chain( &sslinfo->conf, &sslinfo->cacert,NULL);
mbedtls_ssl_conf_rng( &sslinfo->conf, mbedtls_ctr_drbg_random, &sslinfo->ctr_drbg );
if( ( ret = mbedtls_ssl_setup( &sslinfo->ssl, &sslinfo->conf ) ) != 0 )
{
echo( " failed\n ! mbedtls_ssl_setup returned %d\n\n", ret );
return -1;
}
mbedtls_ssl_set_bio( &sslinfo->ssl, server_fd,mbedtls_net_send, mbedtls_net_recv,NULL );
mbedtls_ssl_set_session(&sslinfo->ssl, &ssn);
while((ret = mbedtls_ssl_handshake(&sslinfo->ssl))!=0)
{
if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
{
echo( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
return -1;
}
//CPU sleep
sleeps(1);
}
if((ret = mbedtls_ssl_get_verify_result( &sslinfo->ssl ) ) != 0 )
{
// echo( "Verifying peer X.509 certificate...failed \r\n" );
}
else
{
echo( " ok\n" );
}
//保存session
if( ( ret = mbedtls_ssl_get_session( &sslinfo->ssl, &ssn ) ) != 0 )
{
//失败初始化
memset(&ssn, 0, sizeof(mbedtls_ssl_session));
}
return 0;
}
#else
int ssl_init_info(int *server_fd, ssl_info *sslinfo)
{
int ret;
const char *pers = "ssl";
x509_crt_init(&sslinfo->cacert );
entropy_init(&sslinfo->entropy );
if( ( ret = ctr_drbg_init( &sslinfo->ctr_drbg, entropy_func, &sslinfo->entropy,
(const unsigned char *) pers,
strlen( pers ) ) ) != 0 )
{
return -1;
}
if( ( ret = ssl_init( &sslinfo->ssl ) ) != 0 )
{
echo( " failed\n ! ssl_init returned %d\n\n", ret );
return -1;
}
ssl_set_endpoint( &sslinfo->ssl, SSL_IS_CLIENT );
ssl_set_authmode( &sslinfo->ssl, SSL_VERIFY_OPTIONAL );
ssl_set_ca_chain( &sslinfo->ssl, &sslinfo->cacert, NULL, "" );
ssl_set_rng( &sslinfo->ssl, ctr_drbg_random, &sslinfo->ctr_drbg );
ssl_set_bio( &sslinfo->ssl, net_recv, server_fd,net_send, server_fd );
ssl_set_session(&sslinfo->ssl, &ssn);
while((ret = ssl_handshake(&sslinfo->ssl))!=0)
{
if( ret != POLARSSL_ERR_NET_WANT_READ && ret != POLARSSL_ERR_NET_WANT_WRITE )
{
echo( " failed\n ! ssl_handshake returned -0x%x\n\n", -ret );
return -1;
}
//CPU sleep
sleeps(1);
}
if((ret = ssl_get_verify_result( &sslinfo->ssl ) ) != 0 )
{
// echo( "Verifying peer X.509 certificate...failed \r\n" );
}
else
{
echo( " ok\n" );
}
//保存session加快握手速度
if( ( ret = ssl_get_session( &sslinfo->ssl, &ssn ) ) != 0 )
{
//失败初始化
memset(&ssn, 0, sizeof(ssl_session));
}
return 0;
}
#endif // ISMBEDTLS
#endif//openssl