-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtls.c
191 lines (164 loc) · 3.77 KB
/
tls.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "tls.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void
uvv_ssl__init() {
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ERR_load_crypto_strings();
ERR_load_BIO_strings();
}
void
uvv_ssl__unit() {
EVP_cleanup();
ERR_free_strings();
}
uvv_tls_ctx *
uvv_tls_ctx__create(void) {
SSL_CTX *ctx;
ctx = SSL_CTX_new(SSLv23_client_method());
return ctx;
}
void
uvv_tls_ctx__destroy(uvv_tls_ctx *ctx) {
SSL_CTX_free(ctx);
}
struct uvv_tls *
uvv_tls__create(uvv_tls_ctx *ctx) {
struct uvv_tls *tls;
tls = (struct uvv_tls *)malloc(sizeof *tls);
memset(tls, 0, sizeof *tls);
tls->ctx = ctx;
tls->ssl = SSL_new(tls->ctx);
tls->bio_in = BIO_new(BIO_s_mem());
tls->bio_out = BIO_new(BIO_s_mem());
tls->connected = -1;
SSL_set_mode(tls->ssl, SSL_MODE_AUTO_RETRY);
SSL_set_connect_state(tls->ssl);
SSL_set_bio(tls->ssl, tls->bio_in, tls->bio_out);
return tls;
}
void
uvv_tls__destroy(struct uvv_tls *tls) {
if (tls->ssl) {
SSL_free(tls->ssl);
}
if (tls->b.buf) {
free(tls->b.buf);
}
free(tls);
}
void
uvv_tls__shutdown(struct uvv_tls *tls) {
SSL_shutdown(tls->ssl);
}
int
uvv_tls__connect(struct uvv_tls *tls) {
int rv;
int er;
rv = SSL_do_handshake(tls->ssl);
if (rv == 1) {
return -1;
}
if (!SSL_is_init_finished(tls->ssl))
er = SSL_connect(tls->ssl);
else
return -1;
if (er < 0 && SSL_get_error(tls->ssl, er) == SSL_ERROR_WANT_READ)
return 0;
else
return -1;
}
static int
_bio_error(struct uvv_tls *tls, int err) {
int rv, retry;
retry = BIO_should_retry(tls->bio_out);
if (BIO_should_write(tls->bio_out))
rv = -retry;
else if (BIO_should_read(tls->bio_out))
rv = -retry;
else {
char ssl_error_buf[512];
ERR_error_string_n(err, ssl_error_buf, sizeof(ssl_error_buf));
fprintf(stderr, "[%p] BIO: read failed: (%d) %s\n", tls->ssl, err, ssl_error_buf);
return err;
}
return rv;
}
static int
_ssl_error(struct uvv_tls *tls, int err) {
int ret, rv;
rv = SSL_get_error(tls->ssl, err);
switch (rv) {
case SSL_ERROR_WANT_READ:
ret = 1;
break;
default:
ret = -2;
break;
}
return ret;
}
int
uvv_tls__bio_read(struct uvv_tls *tls, int len) {
int ret;
if (len == 0) {
len = sizeof(tls->buf);
}
memset(tls->buf, 0, len);
ret = BIO_read(tls->bio_out, tls->buf, len);
if (ret >= 0) {
return ret;
} else {
return _bio_error(tls, ret);
}
}
int
uvv_tls__bio_write(struct uvv_tls *tls, const char *data, int len) {
int ret;
ret = BIO_write(tls->bio_in, data, len);
if (ret >= 0)
return ret;
else
return _bio_error(tls, ret);
}
int
uvv_tls__read(struct uvv_tls *tls) {
int err, ret, read, done;
done = SSL_is_init_finished(tls->ssl);
if (!done) {
err = SSL_connect(tls->ssl);
if (err <= 0) {
return _ssl_error(tls, err);
}
}
if (tls->b.buf) {
free(tls->b.buf);
tls->b.buf = 0;
tls->b.len = 0;
}
ret = -1;
do {
read = SSL_read(tls->ssl, tls->buf, SSL_CHUNK_SIZE);
if (read > 0) {
ret = 0;
tls->b.buf = realloc(tls->b.buf, tls->b.len + read);
memcpy(tls->b.buf + tls->b.len, tls->buf, read);
tls->b.len += read;
} else {
_ssl_error(tls, read);
}
} while (read > 0);
if (tls->connected == -1) {
tls->connected = 1;
} else {
ret = 0;
}
return ret;
}
int
uvv_tls__write(struct uvv_tls *tls, const char *data, int len) {
return SSL_write(tls->ssl, data, len);
}