]>
Commit | Line | Data |
---|---|---|
db137867 AC |
1 | /* |
2 | * libratbox: a library used by ircd-ratbox and other things | |
3 | * openssl.c: openssl related code | |
4 | * | |
5 | * Copyright (C) 2007-2008 ircd-ratbox development team | |
6 | * Copyright (C) 2007-2008 Aaron Sethman <androsyn@ratbox.org> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
21 | * USA | |
22 | * | |
23 | * $Id: commio.c 24808 2008-01-02 08:17:05Z androsyn $ | |
24 | */ | |
25 | ||
26 | #include <libratbox_config.h> | |
27 | #include <ratbox_lib.h> | |
28 | ||
29 | #ifdef HAVE_OPENSSL | |
30 | ||
31 | #include <commio-int.h> | |
32 | #include <commio-ssl.h> | |
33 | #include <openssl/ssl.h> | |
34 | #include <openssl/dh.h> | |
35 | #include <openssl/err.h> | |
36 | #include <openssl/rand.h> | |
37 | ||
38 | static SSL_CTX *ssl_server_ctx; | |
39 | static SSL_CTX *ssl_client_ctx; | |
c2ac22cc | 40 | static int libratbox_index = -1; |
db137867 | 41 | |
3202e249 VY |
42 | static unsigned long |
43 | get_last_err(void) | |
db137867 AC |
44 | { |
45 | unsigned long t_err, err = 0; | |
46 | err = ERR_get_error(); | |
47 | if(err == 0) | |
48 | return 0; | |
3202e249 | 49 | |
db137867 AC |
50 | while((t_err = ERR_get_error()) > 0) |
51 | err = t_err; | |
52 | ||
53 | return err; | |
54 | } | |
55 | ||
56 | void | |
3202e249 | 57 | rb_ssl_shutdown(rb_fde_t *F) |
db137867 AC |
58 | { |
59 | int i; | |
60 | if(F == NULL || F->ssl == NULL) | |
61 | return; | |
62 | SSL_set_shutdown((SSL *) F->ssl, SSL_RECEIVED_SHUTDOWN); | |
63 | ||
3202e249 | 64 | for(i = 0; i < 4; i++) |
db137867 AC |
65 | { |
66 | if(SSL_shutdown((SSL *) F->ssl)) | |
67 | break; | |
68 | } | |
69 | get_last_err(); | |
70 | SSL_free((SSL *) F->ssl); | |
71 | } | |
72 | ||
c2ac22cc VY |
73 | unsigned int |
74 | rb_ssl_handshake_count(rb_fde_t *F) | |
75 | { | |
76 | return F->handshake_count; | |
77 | } | |
78 | ||
79 | void | |
80 | rb_ssl_clear_handshake_count(rb_fde_t *F) | |
81 | { | |
82 | F->handshake_count = 0; | |
83 | } | |
84 | ||
db137867 | 85 | static void |
3202e249 | 86 | rb_ssl_timeout(rb_fde_t *F, void *notused) |
db137867 | 87 | { |
73d6283c VY |
88 | lrb_assert(F->accept != NULL); |
89 | F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data); | |
db137867 AC |
90 | } |
91 | ||
92 | ||
3202e249 VY |
93 | static void |
94 | rb_ssl_info_callback(SSL * ssl, int where, int ret) | |
c2ac22cc VY |
95 | { |
96 | if(where & SSL_CB_HANDSHAKE_START) | |
97 | { | |
98 | rb_fde_t *F = SSL_get_ex_data(ssl, libratbox_index); | |
99 | if(F == NULL) | |
100 | return; | |
101 | F->handshake_count++; | |
3202e249 | 102 | } |
c2ac22cc VY |
103 | } |
104 | ||
105 | static void | |
106 | rb_setup_ssl_cb(rb_fde_t *F) | |
107 | { | |
108 | SSL_set_ex_data(F->ssl, libratbox_index, (char *)F); | |
3202e249 | 109 | SSL_set_info_callback((SSL *) F->ssl, (void (*)(const SSL *,int,int))rb_ssl_info_callback); |
c2ac22cc VY |
110 | } |
111 | ||
db137867 | 112 | static void |
3202e249 | 113 | rb_ssl_tryaccept(rb_fde_t *F, void *data) |
db137867 AC |
114 | { |
115 | int ssl_err; | |
116 | lrb_assert(F->accept != NULL); | |
73d6283c | 117 | int flags; |
2142f691 | 118 | struct acceptdata *ad; |
db137867 AC |
119 | |
120 | if(!SSL_is_init_finished((SSL *) F->ssl)) | |
121 | { | |
122 | if((ssl_err = SSL_accept((SSL *) F->ssl)) <= 0) | |
123 | { | |
124 | switch (ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err)) | |
125 | { | |
db137867 AC |
126 | case SSL_ERROR_WANT_READ: |
127 | case SSL_ERROR_WANT_WRITE: | |
73d6283c VY |
128 | if(ssl_err == SSL_ERROR_WANT_WRITE) |
129 | flags = RB_SELECT_WRITE; | |
130 | else | |
131 | flags = RB_SELECT_READ; | |
132 | F->ssl_errno = get_last_err(); | |
133 | rb_setselect(F, flags, rb_ssl_tryaccept, NULL); | |
134 | break; | |
135 | case SSL_ERROR_SYSCALL: | |
136 | F->accept->callback(F, RB_ERROR, NULL, 0, F->accept->data); | |
137 | break; | |
db137867 AC |
138 | default: |
139 | F->ssl_errno = get_last_err(); | |
140 | F->accept->callback(F, RB_ERROR_SSL, NULL, 0, F->accept->data); | |
141 | break; | |
142 | } | |
143 | return; | |
144 | } | |
145 | } | |
146 | rb_settimeout(F, 0, NULL, NULL); | |
147 | rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL); | |
3202e249 | 148 | |
2142f691 | 149 | ad = F->accept; |
db137867 | 150 | F->accept = NULL; |
3202e249 | 151 | ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data); |
2142f691 | 152 | rb_free(ad); |
db137867 AC |
153 | |
154 | } | |
155 | ||
c2ac22cc VY |
156 | |
157 | static void | |
158 | rb_ssl_accept_common(rb_fde_t *new_F) | |
db137867 AC |
159 | { |
160 | int ssl_err; | |
db137867 AC |
161 | if((ssl_err = SSL_accept((SSL *) new_F->ssl)) <= 0) |
162 | { | |
163 | switch (ssl_err = SSL_get_error((SSL *) new_F->ssl, ssl_err)) | |
164 | { | |
165 | case SSL_ERROR_SYSCALL: | |
166 | if(rb_ignore_errno(errno)) | |
167 | case SSL_ERROR_WANT_READ: | |
168 | case SSL_ERROR_WANT_WRITE: | |
169 | { | |
170 | new_F->ssl_errno = get_last_err(); | |
171 | rb_setselect(new_F, RB_SELECT_READ | RB_SELECT_WRITE, | |
172 | rb_ssl_tryaccept, NULL); | |
173 | return; | |
174 | } | |
175 | default: | |
176 | new_F->ssl_errno = get_last_err(); | |
177 | new_F->accept->callback(new_F, RB_ERROR_SSL, NULL, 0, new_F->accept->data); | |
178 | return; | |
179 | } | |
180 | } | |
181 | else | |
182 | { | |
183 | rb_ssl_tryaccept(new_F, NULL); | |
184 | } | |
185 | } | |
186 | ||
c2ac22cc | 187 | void |
3202e249 | 188 | rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout) |
c2ac22cc VY |
189 | { |
190 | new_F->type |= RB_FD_SSL; | |
191 | new_F->ssl = SSL_new(ssl_server_ctx); | |
192 | new_F->accept = rb_malloc(sizeof(struct acceptdata)); | |
193 | ||
194 | new_F->accept->callback = cb; | |
195 | new_F->accept->data = data; | |
196 | rb_settimeout(new_F, timeout, rb_ssl_timeout, NULL); | |
197 | ||
198 | new_F->accept->addrlen = 0; | |
199 | SSL_set_fd((SSL *) new_F->ssl, rb_get_fd(new_F)); | |
200 | rb_setup_ssl_cb(new_F); | |
201 | rb_ssl_accept_common(new_F); | |
202 | } | |
203 | ||
db137867 AC |
204 | |
205 | ||
206 | ||
207 | void | |
3202e249 | 208 | rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, int addrlen) |
db137867 | 209 | { |
db137867 AC |
210 | new_F->type |= RB_FD_SSL; |
211 | new_F->ssl = SSL_new(ssl_server_ctx); | |
212 | new_F->accept = rb_malloc(sizeof(struct acceptdata)); | |
213 | ||
214 | new_F->accept->callback = F->accept->callback; | |
215 | new_F->accept->data = F->accept->data; | |
216 | rb_settimeout(new_F, 10, rb_ssl_timeout, NULL); | |
217 | memcpy(&new_F->accept->S, st, addrlen); | |
218 | new_F->accept->addrlen = addrlen; | |
219 | ||
a9fb3ed0 | 220 | SSL_set_fd((SSL *) new_F->ssl, rb_get_fd(new_F)); |
c2ac22cc VY |
221 | rb_setup_ssl_cb(new_F); |
222 | rb_ssl_accept_common(new_F); | |
db137867 AC |
223 | } |
224 | ||
225 | static ssize_t | |
3202e249 | 226 | rb_ssl_read_or_write(int r_or_w, rb_fde_t *F, void *rbuf, const void *wbuf, size_t count) |
db137867 AC |
227 | { |
228 | ssize_t ret; | |
229 | unsigned long err; | |
230 | SSL *ssl = F->ssl; | |
231 | ||
232 | if(r_or_w == 0) | |
3202e249 | 233 | ret = (ssize_t) SSL_read(ssl, rbuf, (int)count); |
db137867 | 234 | else |
3202e249 | 235 | ret = (ssize_t) SSL_write(ssl, wbuf, (int)count); |
db137867 AC |
236 | |
237 | if(ret < 0) | |
238 | { | |
239 | switch (SSL_get_error(ssl, ret)) | |
240 | { | |
241 | case SSL_ERROR_WANT_READ: | |
242 | errno = EAGAIN; | |
243 | return RB_RW_SSL_NEED_READ; | |
244 | case SSL_ERROR_WANT_WRITE: | |
245 | errno = EAGAIN; | |
246 | return RB_RW_SSL_NEED_WRITE; | |
247 | case SSL_ERROR_ZERO_RETURN: | |
248 | return 0; | |
249 | case SSL_ERROR_SYSCALL: | |
250 | err = get_last_err(); | |
251 | if(err == 0) | |
252 | { | |
253 | F->ssl_errno = 0; | |
254 | return RB_RW_IO_ERROR; | |
255 | } | |
256 | break; | |
257 | default: | |
258 | err = get_last_err(); | |
259 | break; | |
260 | } | |
261 | F->ssl_errno = err; | |
262 | if(err > 0) | |
263 | { | |
264 | errno = EIO; /* not great but... */ | |
265 | return RB_RW_SSL_ERROR; | |
266 | } | |
267 | return RB_RW_IO_ERROR; | |
268 | } | |
269 | return ret; | |
270 | } | |
271 | ||
272 | ssize_t | |
3202e249 | 273 | rb_ssl_read(rb_fde_t *F, void *buf, size_t count) |
db137867 AC |
274 | { |
275 | return rb_ssl_read_or_write(0, F, buf, NULL, count); | |
276 | } | |
277 | ||
278 | ssize_t | |
3202e249 | 279 | rb_ssl_write(rb_fde_t *F, const void *buf, size_t count) |
db137867 AC |
280 | { |
281 | return rb_ssl_read_or_write(1, F, NULL, buf, count); | |
282 | } | |
283 | ||
7247337a JT |
284 | static int |
285 | verify_accept_all_cb(int preverify_ok, X509_STORE_CTX *x509_ctx) | |
286 | { | |
287 | return 1; | |
288 | } | |
289 | ||
918d73d5 JT |
290 | static const char * |
291 | get_ssl_error(unsigned long err) | |
292 | { | |
293 | static char buf[512]; | |
294 | ||
295 | ERR_error_string_n(err, buf, sizeof buf); | |
296 | return buf; | |
297 | } | |
298 | ||
db137867 AC |
299 | int |
300 | rb_init_ssl(void) | |
301 | { | |
302 | int ret = 1; | |
c2ac22cc | 303 | char libratbox_data[] = "libratbox data"; |
db137867 AC |
304 | SSL_load_error_strings(); |
305 | SSL_library_init(); | |
c2ac22cc | 306 | libratbox_index = SSL_get_ex_new_index(0, libratbox_data, NULL, NULL, NULL); |
db137867 AC |
307 | ssl_server_ctx = SSL_CTX_new(SSLv23_server_method()); |
308 | if(ssl_server_ctx == NULL) | |
309 | { | |
310 | rb_lib_log("rb_init_openssl: Unable to initialize OpenSSL server context: %s", | |
918d73d5 | 311 | get_ssl_error(ERR_get_error())); |
db137867 AC |
312 | ret = 0; |
313 | } | |
314 | /* Disable SSLv2, make the client use our settings */ | |
315 | SSL_CTX_set_options(ssl_server_ctx, SSL_OP_NO_SSLv2 | SSL_OP_CIPHER_SERVER_PREFERENCE); | |
7247337a | 316 | SSL_CTX_set_verify(ssl_server_ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, verify_accept_all_cb); |
3202e249 | 317 | |
db137867 AC |
318 | ssl_client_ctx = SSL_CTX_new(TLSv1_client_method()); |
319 | ||
320 | if(ssl_client_ctx == NULL) | |
321 | { | |
322 | rb_lib_log("rb_init_openssl: Unable to initialize OpenSSL client context: %s", | |
918d73d5 | 323 | get_ssl_error(ERR_get_error())); |
db137867 AC |
324 | ret = 0; |
325 | } | |
326 | return ret; | |
327 | } | |
328 | ||
329 | ||
330 | int | |
331 | rb_setup_ssl_server(const char *cert, const char *keyfile, const char *dhfile) | |
332 | { | |
db137867 AC |
333 | DH *dh; |
334 | unsigned long err; | |
335 | if(cert == NULL) | |
336 | { | |
337 | rb_lib_log("rb_setup_ssl_server: No certificate file"); | |
338 | return 0; | |
339 | } | |
07e14084 | 340 | if(!SSL_CTX_use_certificate_chain_file(ssl_server_ctx, cert) || !SSL_CTX_use_certificate_chain_file(ssl_client_ctx, cert)) |
db137867 AC |
341 | { |
342 | err = ERR_get_error(); | |
343 | rb_lib_log("rb_setup_ssl_server: Error loading certificate file [%s]: %s", cert, | |
918d73d5 | 344 | get_ssl_error(err)); |
db137867 AC |
345 | return 0; |
346 | } | |
347 | ||
348 | if(keyfile == NULL) | |
349 | { | |
350 | rb_lib_log("rb_setup_ssl_server: No key file"); | |
351 | return 0; | |
352 | } | |
353 | ||
354 | ||
07e14084 | 355 | if(!SSL_CTX_use_PrivateKey_file(ssl_server_ctx, keyfile, SSL_FILETYPE_PEM) || !SSL_CTX_use_PrivateKey_file(ssl_client_ctx, keyfile, SSL_FILETYPE_PEM)) |
db137867 AC |
356 | { |
357 | err = ERR_get_error(); | |
358 | rb_lib_log("rb_setup_ssl_server: Error loading keyfile [%s]: %s", keyfile, | |
918d73d5 | 359 | get_ssl_error(err)); |
db137867 AC |
360 | return 0; |
361 | } | |
362 | ||
363 | if(dhfile != NULL) | |
364 | { | |
365 | /* DH parameters aren't necessary, but they are nice..if they didn't pass one..that is their problem */ | |
3202e249 VY |
366 | BIO *bio = BIO_new_file(dhfile, "r"); |
367 | if(bio != NULL) | |
db137867 | 368 | { |
3202e249 | 369 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
db137867 AC |
370 | if(dh == NULL) |
371 | { | |
372 | err = ERR_get_error(); | |
373 | rb_lib_log | |
374 | ("rb_setup_ssl_server: Error loading DH params file [%s]: %s", | |
918d73d5 | 375 | dhfile, get_ssl_error(err)); |
3202e249 | 376 | BIO_free(bio); |
db137867 AC |
377 | return 0; |
378 | } | |
3202e249 | 379 | BIO_free(bio); |
db137867 | 380 | SSL_CTX_set_tmp_dh(ssl_server_ctx, dh); |
3202e249 VY |
381 | } |
382 | else | |
383 | { | |
384 | err = ERR_get_error(); | |
385 | rb_lib_log("rb_setup_ssl_server: Error loading DH params file [%s]: %s", | |
918d73d5 | 386 | dhfile, get_ssl_error(err)); |
db137867 AC |
387 | } |
388 | } | |
389 | return 1; | |
390 | } | |
391 | ||
392 | int | |
3202e249 | 393 | rb_ssl_listen(rb_fde_t *F, int backlog) |
db137867 AC |
394 | { |
395 | F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL; | |
396 | return listen(F->fd, backlog); | |
397 | } | |
398 | ||
399 | struct ssl_connect | |
400 | { | |
401 | CNCB *callback; | |
402 | void *data; | |
403 | int timeout; | |
404 | }; | |
405 | ||
406 | static void | |
3202e249 | 407 | rb_ssl_connect_realcb(rb_fde_t *F, int status, struct ssl_connect *sconn) |
db137867 AC |
408 | { |
409 | F->connect->callback = sconn->callback; | |
410 | F->connect->data = sconn->data; | |
411 | rb_free(sconn); | |
412 | rb_connect_callback(F, status); | |
413 | } | |
414 | ||
415 | static void | |
3202e249 | 416 | rb_ssl_tryconn_timeout_cb(rb_fde_t *F, void *data) |
db137867 AC |
417 | { |
418 | rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data); | |
419 | } | |
420 | ||
421 | static void | |
3202e249 | 422 | rb_ssl_tryconn_cb(rb_fde_t *F, void *data) |
db137867 AC |
423 | { |
424 | struct ssl_connect *sconn = data; | |
425 | int ssl_err; | |
426 | if(!SSL_is_init_finished((SSL *) F->ssl)) | |
427 | { | |
428 | if((ssl_err = SSL_connect((SSL *) F->ssl)) <= 0) | |
429 | { | |
430 | switch (ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err)) | |
431 | { | |
432 | case SSL_ERROR_SYSCALL: | |
433 | if(rb_ignore_errno(errno)) | |
434 | case SSL_ERROR_WANT_READ: | |
435 | case SSL_ERROR_WANT_WRITE: | |
436 | { | |
437 | F->ssl_errno = get_last_err(); | |
438 | rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, | |
439 | rb_ssl_tryconn_cb, sconn); | |
440 | return; | |
441 | } | |
442 | default: | |
443 | F->ssl_errno = get_last_err(); | |
444 | rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn); | |
445 | return; | |
446 | } | |
447 | } | |
448 | else | |
449 | { | |
450 | rb_ssl_connect_realcb(F, RB_OK, sconn); | |
451 | } | |
452 | } | |
453 | } | |
454 | ||
455 | static void | |
3202e249 | 456 | rb_ssl_tryconn(rb_fde_t *F, int status, void *data) |
db137867 AC |
457 | { |
458 | struct ssl_connect *sconn = data; | |
459 | int ssl_err; | |
460 | if(status != RB_OK) | |
461 | { | |
462 | rb_ssl_connect_realcb(F, status, sconn); | |
463 | return; | |
464 | } | |
465 | ||
466 | F->type |= RB_FD_SSL; | |
467 | F->ssl = SSL_new(ssl_client_ctx); | |
468 | SSL_set_fd((SSL *) F->ssl, F->fd); | |
c2ac22cc | 469 | rb_setup_ssl_cb(F); |
db137867 AC |
470 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); |
471 | if((ssl_err = SSL_connect((SSL *) F->ssl)) <= 0) | |
472 | { | |
473 | switch (ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err)) | |
474 | { | |
475 | case SSL_ERROR_SYSCALL: | |
476 | if(rb_ignore_errno(errno)) | |
477 | case SSL_ERROR_WANT_READ: | |
478 | case SSL_ERROR_WANT_WRITE: | |
479 | { | |
480 | F->ssl_errno = get_last_err(); | |
481 | rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, | |
482 | rb_ssl_tryconn_cb, sconn); | |
483 | return; | |
484 | } | |
485 | default: | |
486 | F->ssl_errno = get_last_err(); | |
487 | rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn); | |
488 | return; | |
489 | } | |
490 | } | |
491 | else | |
492 | { | |
493 | rb_ssl_connect_realcb(F, RB_OK, sconn); | |
494 | } | |
495 | } | |
496 | ||
497 | void | |
3202e249 | 498 | rb_connect_tcp_ssl(rb_fde_t *F, struct sockaddr *dest, |
db137867 AC |
499 | struct sockaddr *clocal, int socklen, CNCB * callback, void *data, int timeout) |
500 | { | |
501 | struct ssl_connect *sconn; | |
502 | if(F == NULL) | |
503 | return; | |
504 | ||
505 | sconn = rb_malloc(sizeof(struct ssl_connect)); | |
506 | sconn->data = data; | |
507 | sconn->callback = callback; | |
508 | sconn->timeout = timeout; | |
509 | rb_connect_tcp(F, dest, clocal, socklen, rb_ssl_tryconn, sconn, timeout); | |
510 | ||
511 | } | |
512 | ||
513 | void | |
3202e249 | 514 | rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout) |
db137867 AC |
515 | { |
516 | struct ssl_connect *sconn; | |
517 | int ssl_err; | |
518 | if(F == NULL) | |
519 | return; | |
520 | ||
521 | sconn = rb_malloc(sizeof(struct ssl_connect)); | |
522 | sconn->data = data; | |
523 | sconn->callback = callback; | |
524 | sconn->timeout = timeout; | |
525 | F->connect = rb_malloc(sizeof(struct conndata)); | |
526 | F->connect->callback = callback; | |
527 | F->connect->data = data; | |
528 | F->type |= RB_FD_SSL; | |
529 | F->ssl = SSL_new(ssl_client_ctx); | |
3202e249 | 530 | |
db137867 | 531 | SSL_set_fd((SSL *) F->ssl, F->fd); |
c2ac22cc | 532 | rb_setup_ssl_cb(F); |
db137867 AC |
533 | rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn); |
534 | if((ssl_err = SSL_connect((SSL *) F->ssl)) <= 0) | |
535 | { | |
536 | switch (ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err)) | |
537 | { | |
538 | case SSL_ERROR_SYSCALL: | |
539 | if(rb_ignore_errno(errno)) | |
540 | case SSL_ERROR_WANT_READ: | |
541 | case SSL_ERROR_WANT_WRITE: | |
542 | { | |
543 | F->ssl_errno = get_last_err(); | |
544 | rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, | |
545 | rb_ssl_tryconn_cb, sconn); | |
546 | return; | |
547 | } | |
548 | default: | |
549 | F->ssl_errno = get_last_err(); | |
550 | rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn); | |
551 | return; | |
552 | } | |
553 | } | |
554 | else | |
555 | { | |
556 | rb_ssl_connect_realcb(F, RB_OK, sconn); | |
557 | } | |
558 | } | |
559 | ||
560 | int | |
561 | rb_init_prng(const char *path, prng_seed_t seed_type) | |
562 | { | |
563 | if(seed_type == RB_PRNG_DEFAULT) | |
564 | { | |
3202e249 | 565 | #ifdef _WIN32 |
db137867 AC |
566 | RAND_screen(); |
567 | #endif | |
568 | return RAND_status(); | |
569 | } | |
570 | if(path == NULL) | |
571 | return RAND_status(); | |
572 | ||
573 | switch (seed_type) | |
574 | { | |
575 | case RB_PRNG_EGD: | |
576 | if(RAND_egd(path) == -1) | |
577 | return -1; | |
578 | break; | |
579 | case RB_PRNG_FILE: | |
580 | if(RAND_load_file(path, -1) == -1) | |
581 | return -1; | |
582 | break; | |
3202e249 | 583 | #ifdef _WIN32 |
db137867 AC |
584 | case RB_PRNGWIN32: |
585 | RAND_screen(); | |
586 | break; | |
587 | #endif | |
588 | default: | |
589 | return -1; | |
590 | } | |
591 | ||
592 | return RAND_status(); | |
593 | } | |
594 | ||
595 | int | |
596 | rb_get_random(void *buf, size_t length) | |
597 | { | |
a9fb3ed0 | 598 | int ret; |
3202e249 | 599 | |
a9fb3ed0 | 600 | if((ret = RAND_bytes(buf, length)) == 0) |
db137867 | 601 | { |
a9fb3ed0 | 602 | /* remove the error from the queue */ |
3202e249 | 603 | ERR_get_error(); |
db137867 | 604 | } |
a9fb3ed0 | 605 | return ret; |
db137867 AC |
606 | } |
607 | ||
a9fb3ed0 VY |
608 | int |
609 | rb_get_pseudo_random(void *buf, size_t length) | |
610 | { | |
611 | int ret; | |
612 | ret = RAND_pseudo_bytes(buf, length); | |
613 | if(ret < 0) | |
614 | return 0; | |
615 | return 1; | |
616 | } | |
db137867 AC |
617 | |
618 | const char * | |
3202e249 | 619 | rb_get_ssl_strerror(rb_fde_t *F) |
db137867 | 620 | { |
918d73d5 | 621 | return get_ssl_error(F->ssl_errno); |
db137867 AC |
622 | } |
623 | ||
7247337a JT |
624 | int |
625 | rb_get_ssl_certfp(rb_fde_t *F, uint8_t certfp[RB_SSL_CERTFP_LEN]) | |
626 | { | |
627 | X509 *cert; | |
628 | int res; | |
629 | ||
630 | if (F->ssl == NULL) | |
631 | return 0; | |
632 | ||
633 | cert = SSL_get_peer_certificate((SSL *) F->ssl); | |
634 | if(cert != NULL) | |
635 | { | |
636 | res = SSL_get_verify_result((SSL *) F->ssl); | |
637 | if(res == X509_V_OK || | |
638 | res == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN || | |
639 | res == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE || | |
640 | res == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) | |
641 | { | |
642 | memcpy(certfp, cert->sha1_hash, RB_SSL_CERTFP_LEN); | |
643 | return 1; | |
644 | } | |
b2d64e51 | 645 | X509_free(cert); |
7247337a JT |
646 | } |
647 | ||
648 | return 0; | |
649 | } | |
650 | ||
db137867 AC |
651 | int |
652 | rb_supports_ssl(void) | |
653 | { | |
654 | return 1; | |
655 | } | |
656 | ||
030272f3 VY |
657 | void |
658 | rb_get_ssl_info(char *buf, size_t len) | |
659 | { | |
660 | rb_snprintf(buf, len, "Using SSL: %s compiled: 0x%lx, library 0x%lx", | |
661 | SSLeay_version(SSLEAY_VERSION), OPENSSL_VERSION_NUMBER, SSLeay()); | |
662 | } | |
663 | ||
664 | ||
db137867 | 665 | #endif /* HAVE_OPESSL */ |