factor/basis/io/sockets/secure/secure.factor

132 lines
3.3 KiB
Factor
Raw Normal View History

! Copyright (C) 2008, 2010 Slava Pestov.
2008-05-11 18:44:14 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.libraries calendar combinators delegate
destructors io io.sockets io.sockets.private kernel memoize
namespaces present sequences summary system vocabs ;
2008-05-11 18:44:14 -04:00
IN: io.sockets.secure
SYMBOL: secure-socket-timeout
1 minutes secure-socket-timeout set-global
2008-05-17 18:45:56 -04:00
SYMBOL: secure-socket-backend
2008-05-11 18:44:14 -04:00
HOOK: ssl-supported? secure-socket-backend ( -- ? )
HOOK: ssl-certificate-verification-supported? secure-socket-backend ( -- ? )
M: object ssl-supported? f ;
M: object ssl-certificate-verification-supported? f ;
SINGLETONS: TLSv1 TLSv1.1 TLSv1.2 ;
ERROR: no-tls-supported ;
MEMO: best-tls-method ( -- class )
{
{ [ "TLSv1_2_method" "libssl" dlsym? ] [ TLSv1.2 ] }
{ [ "TLSv1_1_method" "libssl" dlsym? ] [ TLSv1.1 ] }
{ [ "TLSv1_method" "libssl" dlsym? ] [ TLSv1 ] }
[ no-tls-supported ]
} cond ;
2008-05-11 18:44:14 -04:00
2008-05-17 18:45:56 -04:00
TUPLE: secure-config
method
key-file password
verify
verify-depth
2008-05-17 18:45:56 -04:00
ca-file ca-path
dh-file
ephemeral-key-bits ;
2008-05-11 18:44:14 -04:00
2008-05-17 18:45:56 -04:00
: <secure-config> ( -- config )
secure-config new
best-tls-method >>method
1024 >>ephemeral-key-bits
ssl-certificate-verification-supported? >>verify ;
2008-05-11 18:44:14 -04:00
TUPLE: secure-context < disposable config handle ;
2008-05-11 18:44:14 -04:00
2008-05-17 18:45:56 -04:00
HOOK: <secure-context> secure-socket-backend ( config -- context )
2008-05-11 18:44:14 -04:00
2008-05-17 18:45:56 -04:00
: with-secure-context ( config quot -- )
2008-05-11 18:44:14 -04:00
[
2008-05-17 18:45:56 -04:00
[ <secure-context> ] [ [ secure-context set ] prepose ] bi*
2008-05-11 18:44:14 -04:00
with-disposal
] with-scope ; inline
2016-03-02 18:29:59 -05:00
TUPLE: secure
{ addrspec read-only }
{ hostname read-only } ;
2008-05-17 18:45:56 -04:00
C: <secure> secure
2008-09-22 17:09:10 -04:00
M: secure present addrspec>> present " (secure)" append ;
M: secure (server) addrspec>> (server) ;
2008-06-17 01:04:18 -04:00
CONSULT: inet secure addrspec>> ;
M: secure resolve-host ( secure -- seq )
2016-03-02 18:29:59 -05:00
[ addrspec>> resolve-host ] [ hostname>> ] bi
[ <secure> ] curry map ;
2008-05-17 18:45:56 -04:00
HOOK: check-certificate secure-socket-backend ( host handle -- )
2008-05-17 18:45:56 -04:00
PREDICATE: secure-inet < secure addrspec>> inet? ;
<PRIVATE
2008-05-17 18:45:56 -04:00
M: secure-inet (client)
[
2008-06-17 01:04:18 -04:00
[ resolve-host (client) [ |dispose ] dip ] keep
addrspec>> host>> pick handle>> check-certificate
2008-05-17 18:45:56 -04:00
] with-destructors ;
PRIVATE>
2008-05-17 18:45:56 -04:00
ERROR: premature-close ;
M: premature-close summary
drop "Connection closed prematurely - potential truncation attack" ;
ERROR: certificate-verify-error result ;
M: certificate-verify-error summary
drop "Certificate verification failed" ;
ERROR: subject-name-verify-error expected got ;
2008-05-17 18:45:56 -04:00
M: subject-name-verify-error summary
drop "Subject name verification failed" ;
2008-07-02 22:52:28 -04:00
ERROR: certificate-missing-error ;
M: certificate-missing-error summary
drop "Host did not present any certificate" ;
ERROR: upgrade-on-non-socket ;
M: upgrade-on-non-socket summary
drop
"send-secure-handshake can only be used if input-stream and" print
"output-stream are a socket" ;
ERROR: upgrade-buffers-full ;
M: upgrade-buffers-full summary
drop
"send-secure-handshake can only be used if buffers are empty" ;
HOOK: non-ssl-socket? os ( obj -- ? )
HOOK: socket-handle os ( obj -- ? )
HOOK: send-secure-handshake secure-socket-backend ( -- )
HOOK: accept-secure-handshake secure-socket-backend ( -- )
2008-07-02 22:52:28 -04:00
{
{ [ os unix? ] [ "io.sockets.secure.unix" require ] }
{ [ os windows? ] [ "io.sockets.secure.windows" require ] }
2008-07-02 22:52:28 -04:00
} cond