2008-05-11 18:44:14 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-09-04 04:57:57 -04:00
|
|
|
USING: accessors kernel namespaces continuations destructors io
|
|
|
|
debugger io.sockets io.sockets.private sequences summary
|
|
|
|
calendar delegate system vocabs.loader combinators present ;
|
2008-05-11 18:44:14 -04:00
|
|
|
IN: io.sockets.secure
|
|
|
|
|
2008-05-21 02:36:30 -04:00
|
|
|
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
|
|
|
|
|
|
|
SINGLETONS: SSLv2 SSLv23 SSLv3 TLSv1 ;
|
|
|
|
|
2008-05-17 18:45:56 -04:00
|
|
|
TUPLE: secure-config
|
|
|
|
method
|
|
|
|
key-file password
|
2008-05-22 01:29:19 -04:00
|
|
|
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
|
|
|
|
SSLv23 >>method
|
2008-05-22 01:29:19 -04:00
|
|
|
1024 >>ephemeral-key-bits
|
2009-02-15 20:53:21 -05:00
|
|
|
"vocab:openssl/cacert.pem" >>ca-file
|
2008-05-22 01:29:19 -04:00
|
|
|
t >>verify ;
|
2008-05-11 18:44:14 -04:00
|
|
|
|
2009-08-24 03:26:13 -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
|
2008-05-12 19:53:22 -04:00
|
|
|
|
2008-05-17 18:45:56 -04:00
|
|
|
TUPLE: secure addrspec ;
|
2008-05-12 19:53:22 -04:00
|
|
|
|
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 ;
|
|
|
|
|
2008-06-17 01:04:18 -04:00
|
|
|
CONSULT: inet secure addrspec>> ;
|
|
|
|
|
|
|
|
M: secure resolve-host ( secure -- seq )
|
|
|
|
addrspec>> resolve-host [ <secure> ] map ;
|
2008-05-17 18:45:56 -04:00
|
|
|
|
|
|
|
HOOK: check-certificate secure-socket-backend ( host handle -- )
|
2008-05-12 19:53:22 -04:00
|
|
|
|
2008-05-17 18:45:56 -04:00
|
|
|
PREDICATE: secure-inet < secure addrspec>> inet? ;
|
2008-05-12 19:53:22 -04:00
|
|
|
|
2008-11-30 14:46:39 -05:00
|
|
|
<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 ;
|
2008-05-12 19:53:22 -04:00
|
|
|
|
|
|
|
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: common-name-verify-error expected got ;
|
|
|
|
|
|
|
|
M: common-name-verify-error summary
|
|
|
|
drop "Common name verification failed" ;
|
2008-07-02 22:52:28 -04:00
|
|
|
|
2008-11-30 14:46:39 -05:00
|
|
|
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: send-secure-handshake secure-socket-backend ( -- )
|
|
|
|
|
|
|
|
HOOK: accept-secure-handshake secure-socket-backend ( -- )
|
|
|
|
|
2008-07-02 22:52:28 -04:00
|
|
|
{
|
2008-12-14 21:03:00 -05:00
|
|
|
{ [ os unix? ] [ "io.sockets.secure.unix" require ] }
|
2008-07-03 20:53:40 -04:00
|
|
|
{ [ os windows? ] [ "openssl" require ] }
|
2008-07-02 22:52:28 -04:00
|
|
|
} cond
|