forked from ibawt/chezuv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.c
215 lines (177 loc) · 3.82 KB
/
lib.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdio.h>
#include <string.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include "uv.h"
#define ERROR -1
#define DRAIN_OUTPUT_BUFFER -2
#define FILL_INPUT_BUFFER -3
typedef struct {
SSL *ssl;
BIO *reader;
BIO *writer;
} ssl_client;
static int translate_ssl_error(SSL* ssl, int n)
{
int e = SSL_get_error(ssl, n);
switch(e) {
case SSL_ERROR_NONE:
return 0;
case SSL_ERROR_WANT_READ:
return FILL_INPUT_BUFFER;
case SSL_ERROR_WANT_WRITE:
return DRAIN_OUTPUT_BUFFER;
}
return -1;
}
ssl_client* new_ssl_client(SSL_CTX* ctx, int client)
{
ssl_client *sc = malloc(sizeof(ssl_client));
if(!sc) {
return sc;
}
memset(sc, 0, sizeof(ssl_client));
sc->reader = BIO_new(BIO_s_mem());
sc->writer = BIO_new(BIO_s_mem());
sc->ssl = SSL_new(ctx);
if(client) {
SSL_set_connect_state(sc->ssl);
} else {
SSL_set_accept_state(sc->ssl);
}
SSL_set_bio(sc->ssl, sc->reader, sc->writer);
return sc;
}
static int clamp(int x)
{
return x < 0 ? 0 : x;
}
int fill_input_buffer(ssl_client *c, char *b, int len)
{
int n;
int bytes_read = 0;
do {
n = BIO_write(c->reader, b + bytes_read, clamp(len - bytes_read));
if(n > 0) {
bytes_read += n;
}
} while( n > 0);
return bytes_read;
}
int drain_output_buffer(ssl_client *c, char *b, int len)
{
int n;
int bytes_written = 0;
do {
n = BIO_read(c->writer, b + bytes_written, clamp(len - bytes_written));
if(n > 0) {
bytes_written += n;
} else if (!BIO_should_retry(c->writer)) {
return -1;
}
} while( n > 0 );
return bytes_written;
}
int ssl_connect(ssl_client *c)
{
int n;
if(!SSL_in_connect_init(c->ssl)) {
return 0;
}
n = SSL_connect(c->ssl);
return (n < 0 ) ? translate_ssl_error(c->ssl, n) : n;
}
int ssl_accept(ssl_client *c)
{
int n;
if(SSL_is_init_finished(c->ssl)) {
return 1;
}
n = SSL_accept(c->ssl);
return (n <= 0 ) ? translate_ssl_error(c->ssl, n) : n;
}
int ssl_read(ssl_client *c, char *b, int len)
{
int bytes_read = 0;
int n;
do {
n = SSL_read(c->ssl, b + bytes_read, clamp(len - bytes_read));
if( n > 0 ) {
bytes_read += n;
}
} while( n > 0);
if( n < 0 && bytes_read == 0 ) {
int ssl_err = translate_ssl_error(c->ssl, n);
if(ssl_err < 0 ) {
return ssl_err;
}
}
return bytes_read;
}
int ssl_write(ssl_client *c, char *b, int len)
{
int bytes_written = 0;
int n;
do {
n = SSL_write(c->ssl, b + bytes_written, clamp(len - bytes_written));
if (n > 0 ) {
bytes_written += n;
}
} while( n > 0);
if( n < 0 && bytes_written == 0) {
int ssl_err = translate_ssl_error(c->ssl, n);
if(ssl_err < 0 ) {
return ssl_err;
}
}
return bytes_written;
}
void free_ssl_client(ssl_client *c)
{
SSL_free(c->ssl);
free(c);
}
SSL_CTX* new_ssl_context(const char* cert, const char* key, int client)
{
SSL_CTX *ctx = SSL_CTX_new( client ? SSLv23_method() : SSLv23_server_method());
if (!ctx) {
return NULL;
}
if( cert && key ) {
int err = SSL_CTX_use_certificate_file(ctx, cert, SSL_FILETYPE_PEM);
if (err != 1) {
goto CLEAN_UP;
}
err = SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM);
if( err != 1 ) {
goto CLEAN_UP;
}
if( SSL_CTX_check_private_key(ctx) != 1) {
goto CLEAN_UP;
}
}
SSL_CTX_set_options(ctx, SSL_OP_ALL|SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3);
return ctx;
CLEAN_UP:
SSL_CTX_free(ctx);
return NULL;
}
int ssl_shutdown(ssl_client *c)
{
int n = SSL_shutdown(c->ssl);
return (n < 0 ) ? translate_ssl_error(c->ssl, n) : n;
}
void free_ssl_context(SSL_CTX* ssl)
{
SSL_CTX_free(ssl);
}
void init_ssl(void)
{
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ERR_load_BIO_strings();
ERR_load_crypto_strings();
}