1 ! Copyright (C) 2007, 2008, Slava Pestov, Elie CHAFTARI.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.enums
4 alien.strings assocs byte-arrays classes.struct combinators
5 combinators.short-circuit destructors endian io io.backend
6 io.buffers io.encodings.latin1 io.encodings.string
7 io.encodings.utf8 io.files io.pathnames io.ports io.sockets
8 io.sockets.secure io.timeouts kernel libc math math.functions
9 math.order math.parser namespaces openssl openssl.libcrypto
10 openssl.libssl random sequences sets splitting unicode ;
11 SLOT: alpn-supported-protocols
12 IN: io.sockets.secure.openssl
14 GENERIC: ssl-method ( symbol -- method )
15 M: TLSv1 ssl-method drop TLSv1_method ;
16 M: TLSv1.1 ssl-method drop TLSv1_1_method ;
17 M: TLSv1.2 ssl-method drop TLSv1_2_method ;
19 CONSTANT: weak-ciphers-for-compatibility
21 ! Weak 12/28/2021, included for compatibility for now
22 "ECDHE-ECDSA-AES256-SHA384"
23 "ECDHE-ECDSA-AES128-SHA256"
24 "ECDHE-RSA-AES256-GCM-SHA384"
25 "ECDHE-RSA-AES256-SHA384"
26 "ECDHE-RSA-AES128-SHA256"
27 "ECDHE-RSA-CAMELLIA256-SHA384"
28 "ECDHE-RSA-CAMELLIA128-SHA256"
29 "ECDHE-ECDSA-CAMELLIA256-SHA384"
30 "ECDHE-ECDSA-CAMELLIA128-SHA256"
40 MEMO: make-cipher-list ( -- string )
42 ! https://ciphersuite.info/cs/?security=recommended&software=openssl&singlepage=true
43 ! Recommended 12/28/2021
44 "ECDHE-ECDSA-AES256-GCM-SHA384"
45 "ECDHE-ECDSA-AES128-GCM-SHA256"
46 "ECDHE-ECDSA-CHACHA20-POLY1305"
47 "ECDHE-PSK-CHACHA20-POLY1305"
48 "DHE-DSS-AES256-GCM-SHA384"
49 "DHE-DSS-AES128-GCM-SHA256"
50 "DHE-PSK-AES256-GCM-SHA384"
51 "DHE-PSK-AES128-GCM-SHA256"
52 "DHE-PSK-CHACHA20-POLY1305"
55 "ECDHE-RSA-AES128-GCM-SHA256"
56 "ECDHE-ECDSA-AES256-CCM8"
57 "ECDHE-ECDSA-AES256-CCM"
58 "ECDHE-ECDSA-AES128-CCM8"
59 "ECDHE-ECDSA-AES128-CCM"
60 "ECDHE-RSA-CHACHA20-POLY1305"
63 weak-ciphers-for-compatibility append
66 TUPLE: openssl-context < secure-context aliens sessions ;
70 : bn-bytes-needed ( num -- bytes-required )
71 log2 1 + 8 / ceiling ;
75 : number>bn ( num -- bn )
76 dup bn-bytes-needed >be
80 : disable-old-tls ( ctx -- )
82 SSL_OP_NO_TLSv1 SSL_OP_NO_TLSv1_1 bitor
83 SSL_CTX_set_options ssl-error ;
85 : set-session-cache ( ctx -- )
87 [ SSL_SESS_CACHE_BOTH SSL_CTX_set_session_cache_mode ssl-error ]
88 [ 32 random-bits >hex dup length SSL_CTX_set_session_id_context ssl-error ]
91 ERROR: file-expected path ;
93 : ensure-exists ( path -- path )
94 dup file-exists? [ file-expected ] unless ; inline
96 : ssl-file-path ( path -- path' )
97 absolute-path ensure-exists ;
99 : load-certificate-chain ( ctx -- )
100 dup config>> key-file>> [
101 [ handle>> ] [ config>> key-file>> ssl-file-path ] bi
102 SSL_CTX_use_certificate_chain_file
106 : password-callback ( -- alien )
107 int { void* int bool void* } cdecl
108 [| buf size rwflag password! |
109 password [ B{ 0 } password! ] unless
111 password strlen :> len
112 buf password len 1 + size min memcpy
116 : default-pasword ( ctx -- alien )
117 [ config>> password>> latin1 malloc-string ] [ aliens>> ] bi
118 [ push ] [ drop ] 2bi ;
120 : set-default-password ( ctx -- )
121 dup config>> password>> [
122 [ handle>> password-callback SSL_CTX_set_default_passwd_cb ]
124 [ handle>> ] [ default-pasword ] bi
125 SSL_CTX_set_default_passwd_cb_userdata
129 : use-private-key-file ( ctx -- )
130 dup config>> key-file>> [
132 [ config>> key-file>> ssl-file-path ] bi
133 SSL_FILETYPE_PEM SSL_CTX_use_PrivateKey_file
137 : load-verify-locations ( ctx -- )
138 dup config>> [ ca-file>> ] [ ca-path>> ] bi or [
142 [ ca-file>> dup [ ssl-file-path ] when ]
143 [ ca-path>> dup [ ssl-file-path ] when ] bi
145 SSL_CTX_load_verify_locations
146 ] [ handle>> SSL_CTX_set_default_verify_paths ] if ssl-error ;
148 : set-verify-depth ( ctx -- )
149 dup config>> verify-depth>> [
150 [ handle>> ] [ config>> verify-depth>> ] bi
151 SSL_CTX_set_verify_depth
154 TUPLE: bio < disposable handle ;
156 : <bio> ( handle -- bio ) bio new-disposable swap >>handle ;
158 M: bio dispose* handle>> BIO_free ssl-error ;
160 : <file-bio> ( path -- bio )
161 normalize-path "r" BIO_new_file dup ssl-error <bio> ;
163 : load-dh-params ( ctx -- )
164 dup config>> dh-file>> [
165 [ handle>> ] [ config>> dh-file>> ] bi <file-bio> &dispose
166 handle>> f f f PEM_read_bio_DHparams dup ssl-error
167 SSL_CTX_set_tmp_dh ssl-error
170 ! Attempt to set ecdh. If it fails, ignore...?
171 : set-ecdh-params ( ctx -- )
172 handle>> SSL_CTRL_SET_ECDH_AUTO 1 f SSL_CTX_ctrl drop ;
174 : <openssl-context> ( config ctx -- context )
175 openssl-context new-disposable
179 H{ } clone >>sessions ;
181 M: openssl <secure-context>
184 dup method>> ssl-method SSL_CTX_new
185 dup ssl-error <openssl-context> |dispose
187 [ set-session-cache ]
188 [ load-certificate-chain ]
189 [ set-default-password ]
190 [ use-private-key-file ]
191 [ load-verify-locations ]
199 M: openssl-context dispose*
201 [ aliens>> [ &free drop ] each ]
202 [ sessions>> values [ SSL_SESSION_free ] each ]
203 [ handle>> SSL_CTX_free ]
207 TUPLE: ssl-handle < disposable file handle connected terminated ;
209 SYMBOL: default-secure-context
211 : current-secure-context ( -- ctx )
213 default-secure-context [
214 <secure-config> <secure-context>
218 : get-session ( addrspec -- session/f )
219 current-secure-context sessions>> at ;
221 : save-session ( session addrspec -- )
222 current-secure-context sessions>> set-at ;
224 : set-secure-cipher-list-only ( ssl -- ssl )
225 dup handle>> make-cipher-list SSL_set_cipher_list ssl-error ;
227 : <ssl-handle> ( fd -- ssl )
229 ssl-handle new-disposable |dispose
230 current-secure-context handle>> SSL_new
231 dup ssl-error >>handle
233 set-secure-cipher-list-only
238 : alpn_select_cb_func ( -- alien )
239 [| ssl out outlen in inlen arg |
240 ! if alpn-protocols is empty return err noack
242 ! current-secure-context relies on secure-context
243 ! variable being set. if this is not set in a callback,
244 ! we need some other way of accessing it (probably
245 ! passing it as arg to SSL_CTX_set_alpn_select_cb, but
246 ! need to make sure that stays defined as long as the
247 ! callback can be called)
248 current-secure-context config>> alpn-supported-protocols>>
249 [ SSL_TLSEXT_ERR_NOACK ]
251 ! convert alpn-protocols from list of strings to
252 ! c-string in wire format and length.
253 ! see https://www.openssl.org/docs/manmaster/man3/SSL_set_alpn_protos.html
254 [ utf8 encode dup length prefix ] map
256 in inlen SSL_select_next_proto
257 ! the function returns OPENSSL_NPN_NO_OVERLAP when no
258 ! match is found, otherwise OPENSSL_NPN_NEGOTIATED
259 OPENSSL_NPN_NEGOTIATED =
260 [ ! DOUBLECHECK: The value in out is already copied
261 ! from the original, so we can just leave it and
262 ! return... otherwise this detail needs to be ironed
263 ! out, probably by finding the entry in in that out
264 ! is identical to. (out needs to point directly into
265 ! in, or a buffer that will outlive the tls
268 [ SSL_TLSEXT_ERR_ALERT_FATAL ] if
270 ] SSL_CTX_alpn_select_cb_func ;
272 : get_alpn_selected_wrapper ( ssl* -- alpn_string/f )
273 { c-string int } [ SSL_get0_alpn_selected ] with-out-parameters
274 drop ! how do we unbox the c-string?
275 ! also, the string is not null-terminated, is that problematic?
280 :: <ssl-socket> ( winsock hostname -- ssl )
281 winsock socket-handle BIO_NOCLOSE BIO_new_socket dup ssl-error :> bio
282 winsock <ssl-handle> :> handle
283 handle handle>> :> native-handle
284 current-secure-context config>> alpn-supported-protocols>>
285 [ drop native-handle ctx>> alpn_select_cb_func f SSL_CTX_set_alpn_select_cb ]
289 native-handle swap SSL_set_tlsext_host_name ssl-error
291 native-handle bio bio SSL_set_bio
294 : ssl-error-syscall ( ssl-handle -- event/f )
300 [ premature-close-error ] [ throw-errno ] if f
302 ! https://stackoverflow.com/questions/13686398/ssl-read-failing-with-ssl-error-syscall-error
307 : check-ssl-error ( ssl-handle ret -- event/f )
308 [ drop ] [ [ handle>> ] dip SSL_get_error ] 2bi
310 { SSL_ERROR_NONE [ drop f ] }
311 { SSL_ERROR_WANT_READ [ drop +input+ ] }
312 { SSL_ERROR_WANT_WRITE [ drop +output+ ] }
313 { SSL_ERROR_SYSCALL [ ssl-error-syscall ] }
314 { SSL_ERROR_SSL [ drop throw-ssl-error ] }
315 ! https://stackoverflow.com/questions/50223224/ssl-read-returns-ssl-error-zero-return-but-err-get-error-is-0
316 ! there are no more bytes to read
317 { SSL_ERROR_ZERO_RETURN [ drop f ] }
318 { SSL_ERROR_WANT_ACCEPT [ drop +input+ ] }
322 : do-ssl-accept-once ( ssl-handle -- event/f )
323 dup handle>> SSL_accept check-ssl-error ;
325 : do-ssl-accept ( ssl-handle -- )
326 dup do-ssl-accept-once
327 [ [ dup file>> ] dip wait-for-fd do-ssl-accept ] [ drop ] if* ;
329 : maybe-handshake ( ssl-handle -- )
330 dup connected>> [ drop ] [
334 [ [ do-ssl-accept ] with-timeout ]
335 [ t swap connected<< ] bi
340 : do-ssl-read ( buffer ssl-handle -- event/f )
341 2dup handle>> swap [ buffer-end ] [ buffer-capacity ] bi SSL_read
342 [ check-ssl-error ] keep swap [ 2nip ] [
343 dup 0 > [ swap buffer+ ] [ 2drop ] if f
346 : throw-if-terminated ( ssl-handle -- ssl-handle )
347 dup terminated>> [ premature-close-error ] when ;
351 dup maybe-handshake [ buffer>> ] dip do-ssl-read ;
354 : do-ssl-write ( buffer ssl-handle -- event/f )
355 2dup handle>> swap [ buffer@ ] [ buffer-length ] bi SSL_write
356 [ check-ssl-error ] keep swap [ 2nip ] [
357 dup 0 > [ swap buffer-consume ] [ 2drop ] if f
362 dup maybe-handshake [ buffer>> ] dip do-ssl-write ;
365 : do-ssl-connect-once ( ssl-handle -- event/f )
366 dup handle>> SSL_connect check-ssl-error ;
368 : do-ssl-connect ( ssl-handle -- )
369 dup do-ssl-connect-once
370 [ dupd wait-for-fd do-ssl-connect ] [ drop ] if* ;
372 : resume-session ( ssl-handle ssl-session -- )
373 [ [ handle>> ] dip SSL_set_session ssl-error ]
374 [ drop do-ssl-connect ]
377 : begin-session ( ssl-handle addrspec -- )
378 [ drop do-ssl-connect ]
379 [ [ handle>> SSL_get1_session ] dip save-session ]
382 : secure-connection ( client-out addrspec -- )
387 [ resume-session ] [ begin-session ] ?if
389 ] [ drop t >>connected drop ] 2bi ;
391 M: ssl-handle timeout
392 drop secure-socket-timeout get ;
394 M: ssl-handle cancel-operation
395 file>> cancel-operation ;
397 M: ssl-handle dispose*
399 ! Free file>> after SSL_free
400 [ file>> &dispose drop ]
401 [ handle>> SSL_free ] bi
404 : check-verify-result ( ssl-handle -- )
405 SSL_get_verify_result X509_V_ERROR number>enum dup X509_V_ERR_OK =
406 [ drop ] [ certificate-verify-error ] if ;
408 : x509name>string ( x509name -- string )
409 NID_commonName 256 <byte-array>
410 [ 256 X509_NAME_get_text_by_NID ] keep
411 swap -1 = [ drop f ] [ latin1 alien>string ] if ;
413 : subject-name ( certificate -- host )
414 X509_get_subject_name x509name>string ;
416 : issuer-name ( certificate -- issuer )
417 X509_get_issuer_name x509name>string ;
419 : sk-value ( stack v -- obj )
420 ssl-new-api? get-global [ OPENSSL_sk_value ] [ sk_value ] if ;
422 : sk-num ( stack -- num )
423 ssl-new-api? get-global [ OPENSSL_sk_num ] [ sk_num ] if ;
425 : name-stack>sequence ( name-stack -- seq )
427 sk-value GENERAL_NAME_st memory>struct
430 : alternative-dns-names ( certificate -- dns-names )
431 NID_subject_alt_name f f X509_get_ext_d2i
432 [ name-stack>sequence ] [ f ] if*
433 [ type>> GEN_DNS = ] filter
434 [ d>> dNSName>> data>> utf8 alien>string ] map ;
436 ! *.foo.com matches: foo.com, www.foo.com, a.foo.com
437 ! *.bar.foo.com matches: bar.foo.com, www.bar.foo.com, b.bar.foo.com
438 : subject-names-match? ( name pattern -- ? )
443 [ [ [ CHAR: . = ] count ] bi@ - 1 <= ]
449 : check-subject-name ( host ssl-handle -- )
450 SSL_get_peer_certificate [
451 [ alternative-dns-names ]
452 [ subject-name ] bi suffix members
453 2dup [ subject-names-match? ] with any?
454 [ 2drop ] [ subject-name-verify-error ] if
455 ] [ certificate-missing-error ] if* ;
457 M: openssl check-certificate
458 current-secure-context config>> verify>> [
460 [ nip check-verify-result ]
461 [ check-subject-name ]
465 : check-buffer ( port -- port )
466 dup buffer>> buffer-empty? [ upgrade-buffers-full ] unless ;
468 : input/output-ports ( -- input output )
469 input-stream output-stream
470 [ get underlying-port check-buffer ] bi@
471 2dup [ handle>> ] bi@ eq? [ upgrade-on-non-socket ] unless ;
473 : make-input/output-secure ( input output -- )
474 dup handle>> non-ssl-socket? [ upgrade-on-non-socket ] unless
475 [ f <ssl-socket> ] change-handle
476 handle>> >>handle drop ;
478 : (send-secure-handshake) ( output -- )
479 remote-address get [ upgrade-on-non-socket ] unless*
482 M: openssl send-secure-handshake
484 [ make-input/output-secure ] keep
485 [ (send-secure-handshake) ] keep
486 remote-address get dup inet? [
487 host>> swap handle>> check-certificate
490 M: openssl accept-secure-handshake
492 make-input/output-secure ;
495 openssl secure-socket-backend set-global