]> gitweb.factorcode.org Git - factor.git/blob - basis/io/sockets/secure/openssl/openssl.factor
Change a throw to rethrow so that we don't lose the original stack trace
[factor.git] / basis / io / sockets / secure / openssl / openssl.factor
1 ! Copyright (C) 2007, 2008, Slava Pestov, Elie CHAFTARI.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors byte-arrays kernel sequences namespaces math
4 math.order combinators init alien alien.c-types alien.strings
5 libc continuations destructors summary splitting assocs random
6 math.parser locals unicode.case openssl openssl.libcrypto
7 openssl.libssl io.backend io.ports io.pathnames
8 io.encodings.8-bit io.timeouts io.sockets.secure ;
9 IN: io.sockets.secure.openssl
10
11 GENERIC: ssl-method ( symbol -- method )
12
13 M: SSLv2  ssl-method drop SSLv2_client_method ;
14 M: SSLv23 ssl-method drop SSLv23_method ;
15 M: SSLv3  ssl-method drop SSLv3_method ;
16 M: TLSv1  ssl-method drop TLSv1_method ;
17
18 TUPLE: openssl-context < secure-context aliens sessions ;
19
20 : set-session-cache ( ctx -- )
21     handle>>
22     [ SSL_SESS_CACHE_BOTH SSL_CTX_set_session_cache_mode ssl-error ]
23     [ 32 random-bits >hex dup length SSL_CTX_set_session_id_context ssl-error ]
24     bi ;
25
26 : load-certificate-chain ( ctx -- )
27     dup config>> key-file>> [
28         [ handle>> ] [ config>> key-file>> (normalize-path) ] bi
29         SSL_CTX_use_certificate_chain_file
30         ssl-error
31     ] [ drop ] if ;
32
33 : password-callback ( -- alien )
34     "int" { "void*" "int" "bool" "void*" } "cdecl"
35     [| buf size rwflag password! |
36         password [ B{ 0 } password! ] unless
37
38         [let | len [ password strlen ] |
39             buf password len 1+ size min memcpy
40             len
41         ]
42     ] alien-callback ;
43
44 : default-pasword ( ctx -- alien )
45     [ config>> password>> latin1 malloc-string ] [ aliens>> ] bi
46     [ push ] [ drop ] 2bi ;
47
48 : set-default-password ( ctx -- )
49     dup config>> password>> [
50         [ handle>> password-callback SSL_CTX_set_default_passwd_cb ]
51         [
52             [ handle>> ] [ default-pasword ] bi
53             SSL_CTX_set_default_passwd_cb_userdata
54         ] bi
55     ] [ drop ] if ;
56
57 : use-private-key-file ( ctx -- )
58     dup config>> key-file>> [
59         [ handle>> ] [ config>> key-file>> (normalize-path) ] bi
60         SSL_FILETYPE_PEM SSL_CTX_use_PrivateKey_file
61         ssl-error
62     ] [ drop ] if ;
63
64 : load-verify-locations ( ctx -- )
65     dup config>> [ ca-file>> ] [ ca-path>> ] bi or [
66         [ handle>> ]
67         [
68             config>>
69             [ ca-file>> dup [ (normalize-path) ] when ]
70             [ ca-path>> dup [ (normalize-path) ] when ] bi
71         ] bi
72         SSL_CTX_load_verify_locations
73     ] [ handle>> SSL_CTX_set_default_verify_paths ] if ssl-error ;
74
75 : set-verify-depth ( ctx -- )
76     dup config>> verify-depth>> [
77         [ handle>> ] [ config>> verify-depth>> ] bi
78         SSL_CTX_set_verify_depth
79     ] [ drop ] if ;
80
81 TUPLE: bio handle disposed ;
82
83 : <bio> ( handle -- bio ) f bio boa ;
84
85 M: bio dispose* handle>> BIO_free ssl-error ;
86
87 : <file-bio> ( path -- bio )
88     normalize-path "r" BIO_new_file dup ssl-error <bio> ;
89
90 : load-dh-params ( ctx -- )
91     dup config>> dh-file>> [
92         [ handle>> ] [ config>> dh-file>> ] bi <file-bio> &dispose
93         handle>> f f f PEM_read_bio_DHparams dup ssl-error
94         SSL_CTX_set_tmp_dh ssl-error
95     ] [ drop ] if ;
96
97 TUPLE: rsa handle disposed ;
98
99 : <rsa> ( handle -- rsa ) f rsa boa ;
100
101 M: rsa dispose* handle>> RSA_free ;
102
103 : generate-eph-rsa-key ( ctx -- )
104     [ handle>> ]
105     [
106         config>> ephemeral-key-bits>> RSA_F4 f f RSA_generate_key
107         dup ssl-error <rsa> &dispose handle>>
108     ] bi
109     SSL_CTX_set_tmp_rsa ssl-error ;
110
111 : <openssl-context> ( config ctx -- context )
112     openssl-context new
113         swap >>handle
114         swap >>config
115         V{ } clone >>aliens
116         H{ } clone >>sessions ;
117
118 M: openssl <secure-context> ( config -- context )
119     maybe-init-ssl
120     [
121         dup method>> ssl-method SSL_CTX_new
122         dup ssl-error <openssl-context> |dispose
123         {
124             [ set-session-cache ]
125             [ load-certificate-chain ]
126             [ set-default-password ]
127             [ use-private-key-file ]
128             [ load-verify-locations ]
129             [ set-verify-depth ]
130             [ load-dh-params ]
131             [ generate-eph-rsa-key ]
132             [ ]
133         } cleave
134     ] with-destructors ;
135
136 M: openssl-context dispose*
137     [ aliens>> [ free ] each ]
138     [ sessions>> values [ SSL_SESSION_free ] each ]
139     [ handle>> SSL_CTX_free ]
140     tri ;
141
142 TUPLE: ssl-handle file handle connected disposed ;
143
144 SYMBOL: default-secure-context
145
146 : context-expired? ( context -- ? )
147     dup [ handle>> expired? ] [ drop t ] if ;
148
149 : current-secure-context ( -- ctx )
150     secure-context get [
151         default-secure-context get dup context-expired? [
152             drop
153             <secure-config> <secure-context> default-secure-context set-global
154             current-secure-context
155         ] when
156     ] unless* ;
157
158 : <ssl-handle> ( fd -- ssl )
159     current-secure-context handle>> SSL_new dup ssl-error
160     f f ssl-handle boa ;
161
162 M: ssl-handle dispose*
163     [ handle>> SSL_free ] [ file>> dispose ] bi ;
164
165 : check-verify-result ( ssl-handle -- )
166     SSL_get_verify_result dup X509_V_OK =
167     [ drop ] [ verify-message certificate-verify-error ] if ;
168
169 : common-name ( certificate -- host )
170     X509_get_subject_name
171     NID_commonName 256 <byte-array>
172     [ 256 X509_NAME_get_text_by_NID ] keep
173     swap -1 = [ drop f ] [ latin1 alien>string ] if ;
174
175 : common-names-match? ( expected actual -- ? )
176     [ >lower ] bi@ "*." ?head [ tail? ] [ = ] if ;
177
178 : check-common-name ( host ssl-handle -- )
179     SSL_get_peer_certificate common-name
180     2dup common-names-match?
181     [ 2drop ] [ common-name-verify-error ] if ;
182
183 M: openssl check-certificate ( host ssl -- )
184     current-secure-context config>> verify>> [
185         handle>>
186         [ nip check-verify-result ]
187         [ check-common-name ]
188         2bi
189     ] [ 2drop ] if ;
190
191 : get-session ( addrspec -- session/f )
192     current-secure-context sessions>> at
193     dup expired? [ drop f ] when ;
194
195 : save-session ( session addrspec -- )
196     current-secure-context sessions>> set-at ;
197
198 openssl secure-socket-backend set-global