2011-01-31 23:45:45 -05:00
|
|
|
! Copyright (C) 2007, 2011, Slava Pestov, Elie CHAFTARI.
|
2008-05-12 19:53:22 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-09-04 04:57:57 -04:00
|
|
|
USING: accessors unix byte-arrays kernel sequences namespaces
|
|
|
|
math math.order combinators init alien alien.c-types
|
2008-11-23 02:00:29 -05:00
|
|
|
alien.strings libc continuations destructors openssl
|
2008-11-30 14:46:39 -05:00
|
|
|
openssl.libcrypto openssl.libssl io io.files io.ports
|
2008-12-14 21:03:00 -05:00
|
|
|
io.backend.unix io.sockets.unix io.encodings.ascii io.buffers
|
2009-09-04 04:57:57 -04:00
|
|
|
io.sockets io.sockets.private io.sockets.secure
|
2010-01-20 23:42:07 -05:00
|
|
|
io.sockets.secure.openssl io.timeouts system summary fry
|
|
|
|
unix.ffi ;
|
2009-05-14 17:54:16 -04:00
|
|
|
FROM: io.ports => shutdown ;
|
2008-12-14 21:03:00 -05:00
|
|
|
IN: io.sockets.secure.unix
|
2008-05-12 19:53:22 -04:00
|
|
|
|
2010-10-24 18:54:19 -04:00
|
|
|
M: openssl ssl-supported? t ;
|
2013-10-14 08:45:33 -04:00
|
|
|
M: openssl ssl-certificate-verification-supported? t ;
|
2010-10-24 18:54:19 -04:00
|
|
|
|
2008-05-14 04:55:33 -04:00
|
|
|
M: ssl-handle handle-fd file>> handle-fd ;
|
2008-05-12 19:53:22 -04:00
|
|
|
|
|
|
|
! Client sockets
|
2008-05-14 04:55:33 -04:00
|
|
|
: <ssl-socket> ( fd -- ssl )
|
|
|
|
[ fd>> BIO_NOCLOSE BIO_new_socket dup ssl-error ] keep <ssl-handle>
|
|
|
|
[ handle>> swap dup SSL_set_bio ] keep ;
|
|
|
|
|
2008-05-17 18:45:56 -04:00
|
|
|
M: secure ((client)) ( addrspec -- handle )
|
2008-05-14 04:55:33 -04:00
|
|
|
addrspec>> ((client)) <ssl-socket> ;
|
2008-05-12 19:53:22 -04:00
|
|
|
|
2008-05-17 18:45:56 -04:00
|
|
|
M: secure parse-sockaddr addrspec>> parse-sockaddr <secure> ;
|
2008-05-14 00:36:15 -04:00
|
|
|
|
2008-05-17 18:45:56 -04:00
|
|
|
M: secure (get-local-address) addrspec>> (get-local-address) ;
|
2008-05-15 06:19:59 -04:00
|
|
|
|
2008-05-17 18:45:56 -04:00
|
|
|
M: secure establish-connection ( client-out remote -- )
|
2008-11-30 14:46:39 -05:00
|
|
|
addrspec>> [ establish-connection ] [ secure-connection ] 2bi ;
|
2008-05-14 04:55:33 -04:00
|
|
|
|
2008-05-17 18:45:56 -04:00
|
|
|
M: secure (server) addrspec>> (server) ;
|
2008-05-14 04:55:33 -04:00
|
|
|
|
2008-05-17 18:45:56 -04:00
|
|
|
M: secure (accept)
|
2008-05-14 04:55:33 -04:00
|
|
|
[
|
2008-11-30 14:46:39 -05:00
|
|
|
addrspec>> (accept) [ |dispose <ssl-socket> ] dip
|
2008-05-14 04:55:33 -04:00
|
|
|
] with-destructors ;
|
|
|
|
|
|
|
|
: check-shutdown-response ( handle r -- event )
|
2008-05-22 01:29:19 -04:00
|
|
|
#! We don't do two-step shutdown here because I couldn't
|
|
|
|
#! figure out how to do it with non-blocking BIOs. Also, it
|
|
|
|
#! seems that SSL_shutdown always returns 0 -- this sounds
|
|
|
|
#! like a bug
|
|
|
|
over handle>> over SSL_get_error
|
2008-05-14 04:55:33 -04:00
|
|
|
{
|
2008-05-22 01:29:19 -04:00
|
|
|
{ SSL_ERROR_NONE [ 2drop f ] }
|
|
|
|
{ SSL_ERROR_WANT_READ [ 2drop +input+ ] }
|
|
|
|
{ SSL_ERROR_WANT_WRITE [ 2drop +output+ ] }
|
2014-04-18 12:32:59 -04:00
|
|
|
{ SSL_ERROR_SYSCALL [ [ drop f ] [ nip syscall-error ] if-zero ] }
|
2008-05-22 01:29:19 -04:00
|
|
|
{ SSL_ERROR_SSL [ (ssl-error) ] }
|
2008-05-14 04:55:33 -04:00
|
|
|
} case ;
|
|
|
|
|
2008-05-21 02:36:30 -04:00
|
|
|
: (shutdown) ( handle -- )
|
|
|
|
dup dup handle>> SSL_shutdown check-shutdown-response
|
|
|
|
dup [ dupd wait-for-fd (shutdown) ] [ 2drop ] if ;
|
|
|
|
|
2008-05-21 16:54:27 -04:00
|
|
|
M: ssl-handle shutdown
|
|
|
|
dup connected>> [
|
|
|
|
f >>connected [ (shutdown) ] with-timeout
|
|
|
|
] [ drop ] if ;
|
2008-11-30 14:46:39 -05:00
|
|
|
|
|
|
|
: check-buffer ( port -- port )
|
|
|
|
dup buffer>> buffer-empty? [ upgrade-buffers-full ] unless ;
|
|
|
|
|
|
|
|
: input/output-ports ( -- input output )
|
|
|
|
input-stream output-stream
|
|
|
|
[ get underlying-port check-buffer ] bi@
|
|
|
|
2dup [ handle>> ] bi@ eq? [ upgrade-on-non-socket ] unless ;
|
|
|
|
|
|
|
|
: make-input/output-secure ( input output -- )
|
|
|
|
dup handle>> fd? [ upgrade-on-non-socket ] unless
|
|
|
|
[ <ssl-socket> ] change-handle
|
|
|
|
handle>> >>handle drop ;
|
|
|
|
|
|
|
|
: (send-secure-handshake) ( output -- )
|
|
|
|
remote-address get [ upgrade-on-non-socket ] unless*
|
|
|
|
secure-connection ;
|
|
|
|
|
|
|
|
M: openssl send-secure-handshake
|
|
|
|
input/output-ports
|
|
|
|
[ make-input/output-secure ] keep
|
|
|
|
[ (send-secure-handshake) ] keep
|
|
|
|
remote-address get dup inet? [
|
|
|
|
host>> swap handle>> check-certificate
|
|
|
|
] [ 2drop ] if ;
|
|
|
|
|
|
|
|
M: openssl accept-secure-handshake
|
|
|
|
input/output-ports
|
|
|
|
make-input/output-secure ;
|