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
11 GENERIC: ssl-method ( symbol -- method )
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 ;
18 TUPLE: openssl-context < secure-context aliens sessions ;
20 : set-session-cache ( ctx -- )
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 ]
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
33 : password-callback ( -- alien )
34 "int" { "void*" "int" "bool" "void*" } "cdecl"
35 [| buf size rwflag password! |
36 password [ B{ 0 } password! ] unless
38 [let | len [ password strlen ] |
39 buf password len 1+ size min memcpy
44 : default-pasword ( ctx -- alien )
45 [ config>> password>> latin1 malloc-string ] [ aliens>> ] bi
46 [ push ] [ drop ] 2bi ;
48 : set-default-password ( ctx -- )
49 dup config>> password>> [
50 [ handle>> password-callback SSL_CTX_set_default_passwd_cb ]
52 [ handle>> ] [ default-pasword ] bi
53 SSL_CTX_set_default_passwd_cb_userdata
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
64 : load-verify-locations ( ctx -- )
65 dup config>> [ ca-file>> ] [ ca-path>> ] bi or [
69 [ ca-file>> dup [ (normalize-path) ] when ]
70 [ ca-path>> dup [ (normalize-path) ] when ] bi
72 SSL_CTX_load_verify_locations
73 ] [ handle>> SSL_CTX_set_default_verify_paths ] if ssl-error ;
75 : set-verify-depth ( ctx -- )
76 dup config>> verify-depth>> [
77 [ handle>> ] [ config>> verify-depth>> ] bi
78 SSL_CTX_set_verify_depth
81 TUPLE: bio handle disposed ;
83 : <bio> ( handle -- bio ) f bio boa ;
85 M: bio dispose* handle>> BIO_free ssl-error ;
87 : <file-bio> ( path -- bio )
88 normalize-path "r" BIO_new_file dup ssl-error <bio> ;
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
97 TUPLE: rsa handle disposed ;
99 : <rsa> ( handle -- rsa ) f rsa boa ;
101 M: rsa dispose* handle>> RSA_free ;
103 : generate-eph-rsa-key ( ctx -- )
106 config>> ephemeral-key-bits>> RSA_F4 f f RSA_generate_key
107 dup ssl-error <rsa> &dispose handle>>
109 SSL_CTX_set_tmp_rsa ssl-error ;
111 : <openssl-context> ( config ctx -- context )
116 H{ } clone >>sessions ;
118 M: openssl <secure-context> ( config -- context )
121 dup method>> ssl-method SSL_CTX_new
122 dup ssl-error <openssl-context> |dispose
124 [ set-session-cache ]
125 [ load-certificate-chain ]
126 [ set-default-password ]
127 [ use-private-key-file ]
128 [ load-verify-locations ]
131 [ generate-eph-rsa-key ]
136 M: openssl-context dispose*
137 [ aliens>> [ free ] each ]
138 [ sessions>> values [ SSL_SESSION_free ] each ]
139 [ handle>> SSL_CTX_free ]
142 TUPLE: ssl-handle file handle connected disposed ;
144 SYMBOL: default-secure-context
146 : context-expired? ( context -- ? )
147 dup [ handle>> expired? ] [ drop t ] if ;
149 : current-secure-context ( -- ctx )
151 default-secure-context get dup context-expired? [
153 <secure-config> <secure-context> default-secure-context set-global
154 current-secure-context
158 : <ssl-handle> ( fd -- ssl )
159 current-secure-context handle>> SSL_new dup ssl-error
162 M: ssl-handle dispose*
163 [ handle>> SSL_free ] [ file>> dispose ] bi ;
165 : check-verify-result ( ssl-handle -- )
166 SSL_get_verify_result dup X509_V_OK =
167 [ drop ] [ verify-message certificate-verify-error ] if ;
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 ;
175 : common-names-match? ( expected actual -- ? )
176 [ >lower ] bi@ "*." ?head [ tail? ] [ = ] if ;
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 ;
183 M: openssl check-certificate ( host ssl -- )
184 current-secure-context config>> verify>> [
186 [ nip check-verify-result ]
187 [ check-common-name ]
191 : get-session ( addrspec -- session/f )
192 current-secure-context sessions>> at
193 dup expired? [ drop f ] when ;
195 : save-session ( session addrspec -- )
196 current-secure-context sessions>> set-at ;
198 openssl secure-socket-backend set-global