]> gitweb.factorcode.org Git - factor.git/blob - basis/io/unix/sockets/secure/secure.factor
Create basis vocab root
[factor.git] / basis / io / unix / sockets / secure / secure.factor
1 ! Copyright (C) 2007, 2008, Slava Pestov, Elie CHAFTARI.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors unix byte-arrays kernel debugger sequences namespaces math
4 math.order combinators init alien alien.c-types alien.strings libc
5 continuations destructors
6 openssl openssl.libcrypto openssl.libssl
7 io.files io.ports io.unix.backend io.unix.sockets
8 io.encodings.ascii io.buffers io.sockets io.sockets.secure
9 io.timeouts system summary ;
10 IN: io.unix.sockets.secure
11
12 M: ssl-handle handle-fd file>> handle-fd ;
13
14 : syscall-error ( r -- * )
15     ERR_get_error dup zero? [
16         drop
17         {
18             { -1 [ err_no ECONNRESET = [ premature-close ] [ (io-error) ] if ] }
19             { 0 [ premature-close ] }
20         } case
21     ] [
22         nip (ssl-error)
23     ] if ;
24
25 : check-accept-response ( handle r -- event )
26     over handle>> over SSL_get_error
27     {
28         { SSL_ERROR_NONE [ 2drop f ] }
29         { SSL_ERROR_WANT_READ [ 2drop +input+ ] }
30         { SSL_ERROR_WANT_WRITE [ 2drop +output+ ] }
31         { SSL_ERROR_SYSCALL [ syscall-error ] }
32         { SSL_ERROR_SSL [ (ssl-error) ] }
33     } case ;
34
35 : do-ssl-accept ( ssl-handle -- )
36     dup dup handle>> SSL_accept check-accept-response dup
37     [ >r dup file>> r> wait-for-fd do-ssl-accept ] [ 2drop ] if ;
38
39 : maybe-handshake ( ssl-handle -- )
40     dup connected>> [ drop ] [
41         t >>connected
42         [ do-ssl-accept ] with-timeout
43     ] if ;
44
45 : check-response ( port r -- port r n )
46     over handle>> handle>> over SSL_get_error ; inline
47
48 ! Input ports
49 : check-read-response ( port r -- event )
50     check-response
51     {
52         { SSL_ERROR_NONE [ swap buffer>> n>buffer f ] }
53         { SSL_ERROR_ZERO_RETURN [ 2drop f ] }
54         { SSL_ERROR_WANT_READ [ 2drop +input+ ] }
55         { SSL_ERROR_WANT_WRITE [ 2drop +output+ ] }
56         { SSL_ERROR_SYSCALL [ syscall-error ] }
57         { SSL_ERROR_SSL [ (ssl-error) ] }
58     } case ;
59
60 M: ssl-handle refill
61     dup maybe-handshake
62     handle>> ! ssl
63     over buffer>>
64     [ buffer-end ] ! buf
65     [ buffer-capacity ] bi ! len
66     SSL_read
67     check-read-response ;
68
69 ! Output ports
70 : check-write-response ( port r -- event )
71     check-response
72     {
73         { SSL_ERROR_NONE [ swap buffer>> buffer-consume f ] }
74         { SSL_ERROR_WANT_READ [ 2drop +input+ ] }
75         { SSL_ERROR_WANT_WRITE [ 2drop +output+ ] }
76         { SSL_ERROR_SYSCALL [ syscall-error ] }
77         { SSL_ERROR_SSL [ (ssl-error) ] }
78     } case ;
79
80 M: ssl-handle drain
81     dup maybe-handshake
82     handle>> ! ssl
83     over buffer>>
84     [ buffer@ ] ! buf
85     [ buffer-length ] bi ! len
86     SSL_write
87     check-write-response ;
88
89 M: ssl-handle cancel-operation
90     file>> cancel-operation ;
91
92 M: ssl-handle timeout
93     drop secure-socket-timeout get ;
94
95 ! Client sockets
96 : <ssl-socket> ( fd -- ssl )
97     [ fd>> BIO_NOCLOSE BIO_new_socket dup ssl-error ] keep <ssl-handle>
98     [ handle>> swap dup SSL_set_bio ] keep ;
99
100 M: secure ((client)) ( addrspec -- handle )
101     addrspec>> ((client)) <ssl-socket> ;
102
103 M: secure parse-sockaddr addrspec>> parse-sockaddr <secure> ;
104
105 M: secure (get-local-address) addrspec>> (get-local-address) ;
106
107 : check-connect-response ( ssl-handle r -- event )
108     over handle>> over SSL_get_error
109     {
110         { SSL_ERROR_NONE [ 2drop f ] }
111         { SSL_ERROR_WANT_READ [ 2drop +input+ ] }
112         { SSL_ERROR_WANT_WRITE [ 2drop +output+ ] }
113         { SSL_ERROR_SYSCALL [ syscall-error ] }
114         { SSL_ERROR_SSL [ (ssl-error) ] }
115     } case ;
116
117 : do-ssl-connect ( ssl-handle -- )
118     dup dup handle>> SSL_connect check-connect-response dup
119     [ dupd wait-for-fd do-ssl-connect ] [ 2drop ] if ;
120
121 : resume-session ( ssl-handle ssl-session -- )
122     [ [ handle>> ] dip SSL_set_session ssl-error ]
123     [ drop do-ssl-connect ]
124     2bi ;
125
126 : begin-session ( ssl-handle addrspec -- )
127     [ drop do-ssl-connect ]
128     [ [ handle>> SSL_get1_session ] dip save-session ]
129     2bi ;
130
131 : secure-connection ( ssl-handle addrspec -- )
132     dup get-session [ resume-session ] [ begin-session ] ?if ;
133
134 M: secure establish-connection ( client-out remote -- )
135     addrspec>>
136     [ establish-connection ]
137     [
138         [ handle>> ] dip
139         [ [ secure-connection ] curry with-timeout ]
140         [ drop t >>connected drop ]
141         2bi
142     ] 2bi ;
143
144 M: secure (server) addrspec>> (server) ;
145
146 M: secure (accept)
147     [
148         addrspec>> (accept) >r |dispose <ssl-socket> r>
149     ] with-destructors ;
150
151 : check-shutdown-response ( handle r -- event )
152     #! We don't do two-step shutdown here because I couldn't
153     #! figure out how to do it with non-blocking BIOs. Also, it
154     #! seems that SSL_shutdown always returns 0 -- this sounds
155     #! like a bug
156     over handle>> over SSL_get_error
157     {
158         { SSL_ERROR_NONE [ 2drop f ] }
159         { SSL_ERROR_WANT_READ [ 2drop +input+ ] }
160         { SSL_ERROR_WANT_WRITE [ 2drop +output+ ] }
161         { SSL_ERROR_SYSCALL [ dup zero? [ 2drop f ] [ syscall-error ] if ] }
162         { SSL_ERROR_SSL [ (ssl-error) ] }
163     } case ;
164
165 : (shutdown) ( handle -- )
166     dup dup handle>> SSL_shutdown check-shutdown-response
167     dup [ dupd wait-for-fd (shutdown) ] [ 2drop ] if ;
168
169 M: ssl-handle shutdown
170     dup connected>> [
171         f >>connected [ (shutdown) ] with-timeout
172     ] [ drop ] if ;