factor/basis/openssl/openssl.factor

230 lines
6.4 KiB
Factor
Raw Normal View History

2008-05-11 18:41:54 -04:00
! Copyright (C) 2007, 2008, Slava Pestov, Elie CHAFTARI.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors byte-arrays kernel debugger sequences namespaces math
math.order combinators init alien alien.c-types alien.strings libc
2008-07-02 01:20:01 -04:00
continuations destructors debugger summary splitting assocs
2008-06-15 23:49:54 -04:00
random math.parser locals unicode.case
2008-05-11 18:41:54 -04:00
openssl.libcrypto openssl.libssl
io.backend io.ports io.files io.encodings.8-bit io.sockets.secure
io.timeouts ;
2008-05-11 18:41:54 -04:00
IN: openssl
! This code is based on http://www.rtfm.com/openssl-examples/
SINGLETON: openssl
GENERIC: ssl-method ( symbol -- method )
M: SSLv2 ssl-method drop SSLv2_client_method ;
M: SSLv23 ssl-method drop SSLv23_method ;
M: SSLv3 ssl-method drop SSLv3_method ;
M: TLSv1 ssl-method drop TLSv1_method ;
: (ssl-error-string) ( n -- string )
ERR_clear_error f ERR_error_string ;
: ssl-error-string ( -- string )
ERR_get_error ERR_clear_error f ERR_error_string ;
2008-05-11 18:41:54 -04:00
2008-05-13 19:24:46 -04:00
: (ssl-error) ( -- * )
ssl-error-string throw ;
2008-05-11 18:41:54 -04:00
: ssl-error ( obj -- )
2008-05-13 19:24:46 -04:00
{ f 0 } member? [ (ssl-error) ] when ;
2008-05-11 18:41:54 -04:00
: init-ssl ( -- )
SSL_library_init ssl-error
SSL_load_error_strings
OpenSSL_add_all_digests
OpenSSL_add_all_ciphers ;
2008-05-31 01:07:40 -04:00
SYMBOL: ssl-initialized?
2008-05-11 18:41:54 -04:00
: maybe-init-ssl ( -- )
2008-05-31 01:07:40 -04:00
ssl-initialized? get-global [
2008-05-11 18:41:54 -04:00
init-ssl
2008-05-31 01:07:40 -04:00
t ssl-initialized? set-global
2008-05-11 18:41:54 -04:00
] unless ;
2008-05-31 01:07:40 -04:00
[ f ssl-initialized? set-global ] "openssl" add-init-hook
2008-05-11 18:41:54 -04:00
2008-06-15 23:49:54 -04:00
TUPLE: openssl-context < secure-context aliens sessions ;
: set-session-cache ( ctx -- )
handle>>
[ SSL_SESS_CACHE_BOTH SSL_CTX_set_session_cache_mode ssl-error ]
[ 32 random-bits >hex dup length SSL_CTX_set_session_id_context ssl-error ]
bi ;
2008-05-11 18:41:54 -04:00
: load-certificate-chain ( ctx -- )
dup config>> key-file>> [
[ handle>> ] [ config>> key-file>> (normalize-path) ] bi
SSL_CTX_use_certificate_chain_file
ssl-error
] [ drop ] if ;
: password-callback ( -- alien )
"int" { "void*" "int" "bool" "void*" } "cdecl"
[| buf size rwflag password! |
password [ B{ 0 } password! ] unless
[let | len [ password strlen ] |
buf password len 1+ size min memcpy
len
]
] alien-callback ;
: default-pasword ( ctx -- alien )
2008-05-18 23:24:55 -04:00
[ config>> password>> latin1 malloc-string ] [ aliens>> ] bi
2008-05-11 18:41:54 -04:00
[ push ] [ drop ] 2bi ;
: set-default-password ( ctx -- )
[ handle>> password-callback SSL_CTX_set_default_passwd_cb ]
[
[ handle>> ] [ default-pasword ] bi
SSL_CTX_set_default_passwd_cb_userdata
] bi ;
: use-private-key-file ( ctx -- )
dup config>> key-file>> [
[ handle>> ] [ config>> key-file>> (normalize-path) ] bi
SSL_FILETYPE_PEM SSL_CTX_use_PrivateKey_file
ssl-error
] [ drop ] if ;
: load-verify-locations ( ctx -- )
dup config>> [ ca-file>> ] [ ca-path>> ] bi or [
[ handle>> ]
[
config>>
[ ca-file>> dup [ (normalize-path) ] when ]
[ ca-path>> dup [ (normalize-path) ] when ] bi
] bi
SSL_CTX_load_verify_locations
] [ handle>> SSL_CTX_set_default_verify_paths ] if ssl-error ;
2008-05-11 18:41:54 -04:00
: set-verify-depth ( ctx -- )
dup config>> verify-depth>> [
[ handle>> ] [ config>> verify-depth>> ] bi
SSL_CTX_set_verify_depth
] [ drop ] if ;
2008-05-11 18:41:54 -04:00
2008-05-17 18:45:56 -04:00
TUPLE: bio handle disposed ;
2008-06-08 16:32:55 -04:00
: <bio> ( handle -- bio ) f bio boa ;
2008-05-17 18:45:56 -04:00
M: bio dispose* handle>> BIO_free ssl-error ;
: <file-bio> ( path -- bio )
normalize-path "r" BIO_new_file dup ssl-error <bio> ;
: load-dh-params ( ctx -- )
dup config>> dh-file>> [
[ handle>> ] [ config>> dh-file>> ] bi <file-bio> &dispose
handle>> f f f PEM_read_bio_DHparams dup ssl-error
SSL_CTX_set_tmp_dh ssl-error
] [ drop ] if ;
TUPLE: rsa handle disposed ;
2008-06-08 16:32:55 -04:00
: <rsa> ( handle -- rsa ) f rsa boa ;
2008-05-17 18:45:56 -04:00
M: rsa dispose* handle>> RSA_free ;
: generate-eph-rsa-key ( ctx -- )
[ handle>> ]
[
config>> ephemeral-key-bits>> RSA_F4 f f RSA_generate_key
dup ssl-error <rsa> &dispose handle>>
] bi
SSL_CTX_set_tmp_rsa ssl-error ;
2008-06-15 23:49:54 -04:00
: <openssl-context> ( config ctx -- context )
openssl-context new
swap >>handle
swap >>config
V{ } clone >>aliens
H{ } clone >>sessions ;
2008-05-17 18:45:56 -04:00
M: openssl <secure-context> ( config -- context )
2008-05-11 18:41:54 -04:00
maybe-init-ssl
[
dup method>> ssl-method SSL_CTX_new
2008-06-15 23:49:54 -04:00
dup ssl-error <openssl-context> |dispose
2008-05-11 18:41:54 -04:00
{
2008-06-15 23:49:54 -04:00
[ set-session-cache ]
2008-05-11 18:41:54 -04:00
[ load-certificate-chain ]
[ set-default-password ]
[ use-private-key-file ]
[ load-verify-locations ]
[ set-verify-depth ]
2008-05-17 18:45:56 -04:00
[ load-dh-params ]
[ generate-eph-rsa-key ]
2008-05-11 18:41:54 -04:00
[ ]
} cleave
] with-destructors ;
2008-05-17 18:45:56 -04:00
M: openssl-context dispose*
[ aliens>> [ free ] each ]
2008-06-15 23:49:54 -04:00
[ sessions>> values [ SSL_SESSION_free ] each ]
2008-05-17 18:45:56 -04:00
[ handle>> SSL_CTX_free ]
2008-06-15 23:49:54 -04:00
tri ;
2008-05-11 18:41:54 -04:00
2008-05-14 04:55:33 -04:00
TUPLE: ssl-handle file handle connected disposed ;
2008-05-12 20:23:32 -04:00
2008-05-21 16:54:27 -04:00
SYMBOL: default-secure-context
: context-expired? ( context -- ? )
dup [ handle>> expired? ] [ drop t ] if ;
: current-secure-context ( -- ctx )
secure-context get [
default-secure-context get dup context-expired? [
drop
<secure-config> <secure-context> default-secure-context set-global
current-secure-context
] when
] unless* ;
: <ssl-handle> ( fd -- ssl )
2008-05-21 16:54:27 -04:00
current-secure-context handle>> SSL_new dup ssl-error
2008-05-14 04:55:33 -04:00
f f ssl-handle boa ;
2008-05-11 18:41:54 -04:00
2008-05-15 00:23:12 -04:00
M: ssl-handle dispose*
[ handle>> SSL_free ] [ file>> dispose ] bi ;
2008-05-11 18:41:54 -04:00
: check-verify-result ( ssl-handle -- )
SSL_get_verify_result dup X509_V_OK =
2008-05-17 18:45:56 -04:00
[ drop ] [ verify-message certificate-verify-error ] if ;
2008-05-11 18:41:54 -04:00
: common-name ( certificate -- host )
X509_get_subject_name
NID_commonName 256 <byte-array>
[ 256 X509_NAME_get_text_by_NID ] keep
2008-05-18 23:24:55 -04:00
swap -1 = [ drop f ] [ latin1 alien>string ] if ;
2008-05-11 18:41:54 -04:00
2008-06-04 20:14:20 -04:00
: common-names-match? ( expected actual -- ? )
[ >lower ] bi@ "*." ?head [ tail? ] [ = ] if ;
2008-05-11 18:41:54 -04:00
: check-common-name ( host ssl-handle -- )
2008-06-04 20:14:20 -04:00
SSL_get_peer_certificate common-name
2dup common-names-match?
2008-05-11 18:41:54 -04:00
[ 2drop ] [ common-name-verify-error ] if ;
2008-05-17 18:45:56 -04:00
M: openssl check-certificate ( host ssl -- )
current-secure-context config>> verify>> [
handle>>
[ nip check-verify-result ]
[ check-common-name ]
2bi
] [ 2drop ] if ;
2008-05-11 18:41:54 -04:00
2008-06-15 23:49:54 -04:00
: get-session ( addrspec -- session/f )
current-secure-context sessions>> at
dup expired? [ drop f ] when ;
: save-session ( session addrspec -- )
current-secure-context sessions>> set-at ;
2008-05-17 18:45:56 -04:00
openssl secure-socket-backend set-global