2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2000-2002 GOTOU Yuuzou <gotoyuzo@notwork.org>
|
|
|
|
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
|
2007-04-02 18:10:12 -04:00
|
|
|
* Copyright (C) 2001-2007 Technorama Ltd. <oss-ruby@technorama.net>
|
2003-07-23 12:12:24 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This program is licenced under the same licence as Ruby.
|
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
#include "ossl.h"
|
|
|
|
|
|
|
|
#if defined(HAVE_UNISTD_H)
|
|
|
|
# include <unistd.h> /* for read(), and write() */
|
|
|
|
#endif
|
|
|
|
|
2011-03-09 13:01:11 -05:00
|
|
|
#define numberof(ary) (int)(sizeof(ary)/sizeof((ary)[0]))
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2003-07-26 08:27:04 -04:00
|
|
|
#ifdef _WIN32
|
2003-07-25 12:40:26 -04:00
|
|
|
# define TO_SOCKET(s) _get_osfhandle(s)
|
|
|
|
#else
|
2011-03-09 13:01:11 -05:00
|
|
|
# define TO_SOCKET(s) (s)
|
2003-07-25 12:40:26 -04:00
|
|
|
#endif
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
VALUE mSSL;
|
|
|
|
VALUE eSSLError;
|
|
|
|
VALUE cSSLContext;
|
|
|
|
VALUE cSSLSocket;
|
|
|
|
|
|
|
|
#define ossl_sslctx_set_cert(o,v) rb_iv_set((o),"@cert",(v))
|
|
|
|
#define ossl_sslctx_set_key(o,v) rb_iv_set((o),"@key",(v))
|
|
|
|
#define ossl_sslctx_set_client_ca(o,v) rb_iv_set((o),"@client_ca",(v))
|
|
|
|
#define ossl_sslctx_set_ca_file(o,v) rb_iv_set((o),"@ca_file",(v))
|
|
|
|
#define ossl_sslctx_set_ca_path(o,v) rb_iv_set((o),"@ca_path",(v))
|
|
|
|
#define ossl_sslctx_set_timeout(o,v) rb_iv_set((o),"@timeout",(v))
|
|
|
|
#define ossl_sslctx_set_verify_mode(o,v) rb_iv_set((o),"@verify_mode",(v))
|
|
|
|
#define ossl_sslctx_set_verify_dep(o,v) rb_iv_set((o),"@verify_depth",(v))
|
|
|
|
#define ossl_sslctx_set_verify_cb(o,v) rb_iv_set((o),"@verify_callback",(v))
|
|
|
|
#define ossl_sslctx_set_options(o,v) rb_iv_set((o),"@options",(v))
|
|
|
|
#define ossl_sslctx_set_cert_store(o,v) rb_iv_set((o),"@cert_store",(v))
|
2003-10-29 12:27:59 -05:00
|
|
|
#define ossl_sslctx_set_extra_cert(o,v) rb_iv_set((o),"@extra_chain_cert",(v))
|
2005-03-09 05:45:42 -05:00
|
|
|
#define ossl_sslctx_set_client_cert_cb(o,v) rb_iv_set((o),"@client_cert_cb",(v))
|
|
|
|
#define ossl_sslctx_set_tmp_dh_cb(o,v) rb_iv_set((o),"@tmp_dh_callback",(v))
|
2005-05-22 06:18:41 -04:00
|
|
|
#define ossl_sslctx_set_sess_id_ctx(o, v) rb_iv_get((o),"@session_id_context"(v))
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
#define ossl_sslctx_get_cert(o) rb_iv_get((o),"@cert")
|
|
|
|
#define ossl_sslctx_get_key(o) rb_iv_get((o),"@key")
|
|
|
|
#define ossl_sslctx_get_client_ca(o) rb_iv_get((o),"@client_ca")
|
|
|
|
#define ossl_sslctx_get_ca_file(o) rb_iv_get((o),"@ca_file")
|
|
|
|
#define ossl_sslctx_get_ca_path(o) rb_iv_get((o),"@ca_path")
|
|
|
|
#define ossl_sslctx_get_timeout(o) rb_iv_get((o),"@timeout")
|
|
|
|
#define ossl_sslctx_get_verify_mode(o) rb_iv_get((o),"@verify_mode")
|
|
|
|
#define ossl_sslctx_get_verify_dep(o) rb_iv_get((o),"@verify_depth")
|
|
|
|
#define ossl_sslctx_get_verify_cb(o) rb_iv_get((o),"@verify_callback")
|
|
|
|
#define ossl_sslctx_get_options(o) rb_iv_get((o),"@options")
|
|
|
|
#define ossl_sslctx_get_cert_store(o) rb_iv_get((o),"@cert_store")
|
2003-10-29 12:27:59 -05:00
|
|
|
#define ossl_sslctx_get_extra_cert(o) rb_iv_get((o),"@extra_chain_cert")
|
2005-03-09 05:45:42 -05:00
|
|
|
#define ossl_sslctx_get_client_cert_cb(o) rb_iv_get((o),"@client_cert_cb")
|
|
|
|
#define ossl_sslctx_get_tmp_dh_cb(o) rb_iv_get((o),"@tmp_dh_callback")
|
2005-05-22 06:18:41 -04:00
|
|
|
#define ossl_sslctx_get_sess_id_ctx(o) rb_iv_get((o),"@session_id_context")
|
2003-07-23 12:12:24 -04:00
|
|
|
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-09 17:20:17 -04:00
|
|
|
static const char *ossl_sslctx_attrs[] = {
|
2003-07-23 12:12:24 -04:00
|
|
|
"cert", "key", "client_ca", "ca_file", "ca_path",
|
|
|
|
"timeout", "verify_mode", "verify_depth",
|
2005-03-09 05:45:42 -05:00
|
|
|
"verify_callback", "options", "cert_store", "extra_chain_cert",
|
2005-05-22 06:18:41 -04:00
|
|
|
"client_cert_cb", "tmp_dh_callback", "session_id_context",
|
2007-04-02 18:10:12 -04:00
|
|
|
"session_get_cb", "session_new_cb", "session_remove_cb",
|
2009-01-24 16:45:42 -05:00
|
|
|
#ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME
|
|
|
|
"servername_cb",
|
|
|
|
#endif
|
2003-07-23 12:12:24 -04:00
|
|
|
};
|
|
|
|
|
2005-03-09 05:45:42 -05:00
|
|
|
#define ossl_ssl_get_io(o) rb_iv_get((o),"@io")
|
|
|
|
#define ossl_ssl_get_ctx(o) rb_iv_get((o),"@context")
|
|
|
|
#define ossl_ssl_get_sync_close(o) rb_iv_get((o),"@sync_close")
|
|
|
|
#define ossl_ssl_get_x509(o) rb_iv_get((o),"@x509")
|
|
|
|
#define ossl_ssl_get_key(o) rb_iv_get((o),"@key")
|
|
|
|
#define ossl_ssl_get_tmp_dh(o) rb_iv_get((o),"@tmp_dh")
|
|
|
|
|
|
|
|
#define ossl_ssl_set_io(o,v) rb_iv_set((o),"@io",(v))
|
|
|
|
#define ossl_ssl_set_ctx(o,v) rb_iv_set((o),"@context",(v))
|
|
|
|
#define ossl_ssl_set_sync_close(o,v) rb_iv_set((o),"@sync_close",(v))
|
|
|
|
#define ossl_ssl_set_x509(o,v) rb_iv_set((o),"@x509",(v))
|
|
|
|
#define ossl_ssl_set_key(o,v) rb_iv_set((o),"@key",(v))
|
|
|
|
#define ossl_ssl_set_tmp_dh(o,v) rb_iv_set((o),"@tmp_dh",(v))
|
|
|
|
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-09 17:20:17 -04:00
|
|
|
static const char *ossl_ssl_attr_readers[] = { "io", "context", };
|
2009-01-24 16:45:42 -05:00
|
|
|
static const char *ossl_ssl_attrs[] = {
|
|
|
|
#ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME
|
|
|
|
"hostname",
|
|
|
|
#endif
|
2010-04-22 04:04:13 -04:00
|
|
|
"sync_close",
|
2009-01-24 16:45:42 -05:00
|
|
|
};
|
2005-03-09 05:45:42 -05:00
|
|
|
|
2007-04-02 18:10:12 -04:00
|
|
|
ID ID_callback_state;
|
|
|
|
|
2005-03-09 05:45:42 -05:00
|
|
|
/*
|
|
|
|
* SSLContext class
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
struct {
|
|
|
|
const char *name;
|
|
|
|
SSL_METHOD *(*func)(void);
|
|
|
|
} ossl_ssl_method_tab[] = {
|
2010-05-21 05:10:23 -04:00
|
|
|
#define OSSL_SSL_METHOD_ENTRY(name) { #name, (SSL_METHOD *(*)(void))name##_method }
|
2003-07-23 12:12:24 -04:00
|
|
|
OSSL_SSL_METHOD_ENTRY(TLSv1),
|
|
|
|
OSSL_SSL_METHOD_ENTRY(TLSv1_server),
|
|
|
|
OSSL_SSL_METHOD_ENTRY(TLSv1_client),
|
2011-05-12 00:22:16 -04:00
|
|
|
#if defined(HAVE_SSLV2_METHOD) && defined(HAVE_SSLV2_SERVER_METHOD) && \
|
2011-05-15 07:55:52 -04:00
|
|
|
defined(HAVE_SSLV2_CLIENT_METHOD)
|
2003-07-23 12:12:24 -04:00
|
|
|
OSSL_SSL_METHOD_ENTRY(SSLv2),
|
|
|
|
OSSL_SSL_METHOD_ENTRY(SSLv2_server),
|
|
|
|
OSSL_SSL_METHOD_ENTRY(SSLv2_client),
|
2011-05-12 00:22:16 -04:00
|
|
|
#endif
|
2003-07-23 12:12:24 -04:00
|
|
|
OSSL_SSL_METHOD_ENTRY(SSLv3),
|
|
|
|
OSSL_SSL_METHOD_ENTRY(SSLv3_server),
|
|
|
|
OSSL_SSL_METHOD_ENTRY(SSLv3_client),
|
|
|
|
OSSL_SSL_METHOD_ENTRY(SSLv23),
|
|
|
|
OSSL_SSL_METHOD_ENTRY(SSLv23_server),
|
|
|
|
OSSL_SSL_METHOD_ENTRY(SSLv23_client),
|
|
|
|
#undef OSSL_SSL_METHOD_ENTRY
|
|
|
|
};
|
|
|
|
|
|
|
|
int ossl_ssl_ex_vcb_idx;
|
|
|
|
int ossl_ssl_ex_store_p;
|
2005-03-09 05:45:42 -05:00
|
|
|
int ossl_ssl_ex_ptr_idx;
|
|
|
|
int ossl_ssl_ex_client_cert_cb_idx;
|
|
|
|
int ossl_ssl_ex_tmp_dh_callback_idx;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
ossl_sslctx_free(SSL_CTX *ctx)
|
|
|
|
{
|
|
|
|
if(ctx && SSL_CTX_get_ex_data(ctx, ossl_ssl_ex_store_p)== (void*)1)
|
|
|
|
ctx->cert_store = NULL;
|
|
|
|
SSL_CTX_free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_s_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
2011-06-22 02:04:27 -04:00
|
|
|
long mode = SSL_MODE_ENABLE_PARTIAL_WRITE;
|
|
|
|
|
|
|
|
#ifdef SSL_MODE_RELEASE_BUFFERS
|
|
|
|
mode |= SSL_MODE_RELEASE_BUFFERS;
|
|
|
|
#endif
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
ctx = SSL_CTX_new(SSLv23_method());
|
|
|
|
if (!ctx) {
|
2011-11-23 18:15:09 -05:00
|
|
|
ossl_raise(eSSLError, "SSL_CTX_new");
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
2011-06-22 02:04:27 -04:00
|
|
|
SSL_CTX_set_mode(ctx, mode);
|
2003-07-23 12:12:24 -04:00
|
|
|
return Data_Wrap_Struct(klass, 0, ossl_sslctx_free, ctx);
|
|
|
|
}
|
|
|
|
|
2010-11-29 15:23:59 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ctx.ssl_version = :TLSv1
|
|
|
|
* ctx.ssl_version = "SSLv23_client"
|
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* You can get a list of valid versions with OpenSSL::SSL::SSLContext::METHODS
|
2010-11-29 15:23:59 -05:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
2007-12-17 07:18:28 -05:00
|
|
|
ossl_sslctx_set_ssl_version(VALUE self, VALUE ssl_method)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
|
|
|
SSL_METHOD *method = NULL;
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-09 17:20:17 -04:00
|
|
|
const char *s;
|
2007-12-17 07:18:28 -05:00
|
|
|
int i;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2007-12-17 07:18:28 -05:00
|
|
|
SSL_CTX *ctx;
|
2003-07-23 12:12:24 -04:00
|
|
|
if(TYPE(ssl_method) == T_SYMBOL)
|
|
|
|
s = rb_id2name(SYM2ID(ssl_method));
|
|
|
|
else
|
|
|
|
s = StringValuePtr(ssl_method);
|
|
|
|
for (i = 0; i < numberof(ossl_ssl_method_tab); i++) {
|
|
|
|
if (strcmp(ossl_ssl_method_tab[i].name, s) == 0) {
|
|
|
|
method = ossl_ssl_method_tab[i].func();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!method) {
|
|
|
|
ossl_raise(rb_eArgError, "unknown SSL method `%s'.", s);
|
|
|
|
}
|
2004-12-15 01:35:55 -05:00
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (SSL_CTX_set_ssl_version(ctx, method) != 1) {
|
2011-11-23 18:15:09 -05:00
|
|
|
ossl_raise(eSSLError, "SSL_CTX_set_ssl_version");
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2007-12-17 07:18:28 -05:00
|
|
|
return ssl_method;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* SSLContext.new => ctx
|
|
|
|
* SSLContext.new(:TLSv1) => ctx
|
|
|
|
* SSLContext.new("SSLv23_client") => ctx
|
|
|
|
*
|
|
|
|
* You can get a list of valid methods with OpenSSL::SSL::SSLContext::METHODS
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
VALUE ssl_method;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < numberof(ossl_sslctx_attrs); i++){
|
|
|
|
char buf[32];
|
|
|
|
snprintf(buf, sizeof(buf), "@%s", ossl_sslctx_attrs[i]);
|
|
|
|
rb_iv_set(self, buf, Qnil);
|
|
|
|
}
|
|
|
|
if (rb_scan_args(argc, argv, "01", &ssl_method) == 0){
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
ossl_sslctx_set_ssl_version(self, ssl_method);
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2005-03-09 05:45:42 -05:00
|
|
|
static VALUE
|
|
|
|
ossl_call_client_cert_cb(VALUE obj)
|
|
|
|
{
|
|
|
|
VALUE cb, ary, cert, key;
|
|
|
|
SSL *ssl;
|
|
|
|
|
|
|
|
Data_Get_Struct(obj, SSL, ssl);
|
|
|
|
cb = (VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_client_cert_cb_idx);
|
|
|
|
if (NIL_P(cb)) return Qfalse;
|
|
|
|
ary = rb_funcall(cb, rb_intern("call"), 1, obj);
|
|
|
|
Check_Type(ary, T_ARRAY);
|
|
|
|
GetX509CertPtr(cert = rb_ary_entry(ary, 0));
|
|
|
|
GetPKeyPtr(key = rb_ary_entry(ary, 1));
|
|
|
|
ossl_ssl_set_x509(obj, cert);
|
|
|
|
ossl_ssl_set_key(obj, key);
|
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ossl_client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
|
|
|
|
{
|
2011-03-24 03:29:21 -04:00
|
|
|
VALUE obj, success;
|
2005-03-09 05:45:42 -05:00
|
|
|
|
|
|
|
obj = (VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx);
|
|
|
|
success = rb_protect((VALUE(*)_((VALUE)))ossl_call_client_cert_cb,
|
2011-03-24 03:29:21 -04:00
|
|
|
obj, NULL);
|
|
|
|
if (!RTEST(success)) return 0;
|
2005-03-09 05:45:42 -05:00
|
|
|
*x509 = DupX509CertPtr(ossl_ssl_get_x509(obj));
|
|
|
|
*pkey = DupPKeyPtr(ossl_ssl_get_key(obj));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(OPENSSL_NO_DH)
|
|
|
|
static VALUE
|
|
|
|
ossl_call_tmp_dh_callback(VALUE *args)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
VALUE cb, dh;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
|
|
|
|
Data_Get_Struct(args[0], SSL, ssl);
|
|
|
|
cb = (VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_tmp_dh_callback_idx);
|
|
|
|
if (NIL_P(cb)) return Qfalse;
|
|
|
|
dh = rb_funcall(cb, rb_intern("call"), 3, args[0], args[1], args[2]);
|
|
|
|
pkey = GetPKeyPtr(dh);
|
|
|
|
if (EVP_PKEY_type(pkey->type) != EVP_PKEY_DH) return Qfalse;
|
2005-03-12 12:15:29 -05:00
|
|
|
ossl_ssl_set_tmp_dh(args[0], dh);
|
2005-03-09 05:45:42 -05:00
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DH*
|
|
|
|
ossl_tmp_dh_callback(SSL *ssl, int is_export, int keylength)
|
|
|
|
{
|
2011-03-24 03:29:21 -04:00
|
|
|
VALUE args[3], success;
|
2005-03-09 05:45:42 -05:00
|
|
|
|
|
|
|
args[0] = (VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx);
|
|
|
|
args[1] = INT2FIX(is_export);
|
|
|
|
args[2] = INT2FIX(keylength);
|
|
|
|
success = rb_protect((VALUE(*)_((VALUE)))ossl_call_tmp_dh_callback,
|
2011-03-24 03:29:21 -04:00
|
|
|
(VALUE)args, NULL);
|
|
|
|
if (!RTEST(success)) return NULL;
|
2005-03-09 05:45:42 -05:00
|
|
|
|
2005-03-12 12:38:37 -05:00
|
|
|
return GetPKeyPtr(ossl_ssl_get_tmp_dh(args[0]))->pkey.dh;
|
2005-03-09 05:45:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static DH*
|
|
|
|
ossl_default_tmp_dh_callback(SSL *ssl, int is_export, int keylength)
|
|
|
|
{
|
2007-12-25 06:31:51 -05:00
|
|
|
rb_warning("using default DH parameters.");
|
|
|
|
|
2005-03-09 05:45:42 -05:00
|
|
|
switch(keylength){
|
|
|
|
case 512:
|
|
|
|
return OSSL_DEFAULT_DH_512;
|
|
|
|
case 1024:
|
|
|
|
return OSSL_DEFAULT_DH_1024;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif /* OPENSSL_NO_DH */
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
static int
|
|
|
|
ossl_ssl_verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
|
|
|
|
{
|
|
|
|
VALUE cb;
|
|
|
|
SSL *ssl;
|
|
|
|
|
|
|
|
ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
|
|
|
|
cb = (VALUE)SSL_get_ex_data(ssl, ossl_ssl_ex_vcb_idx);
|
|
|
|
X509_STORE_CTX_set_ex_data(ctx, ossl_verify_cb_idx, (void*)cb);
|
|
|
|
return ossl_verify_cb(preverify_ok, ctx);
|
|
|
|
}
|
|
|
|
|
2007-04-02 18:10:12 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_call_session_get_cb(VALUE ary)
|
|
|
|
{
|
2008-07-22 11:34:23 -04:00
|
|
|
VALUE ssl_obj, sslctx_obj, cb;
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2007-04-02 18:10:12 -04:00
|
|
|
Check_Type(ary, T_ARRAY);
|
|
|
|
ssl_obj = rb_ary_entry(ary, 0);
|
|
|
|
|
|
|
|
sslctx_obj = rb_iv_get(ssl_obj, "@context");
|
|
|
|
if (NIL_P(sslctx_obj)) return Qnil;
|
|
|
|
cb = rb_iv_get(sslctx_obj, "@session_get_cb");
|
|
|
|
if (NIL_P(cb)) return Qnil;
|
|
|
|
|
|
|
|
return rb_funcall(cb, rb_intern("call"), 1, ary);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this method is currently only called for servers (in OpenSSL <= 0.9.8e) */
|
|
|
|
static SSL_SESSION *
|
|
|
|
ossl_sslctx_session_get_cb(SSL *ssl, unsigned char *buf, int len, int *copy)
|
|
|
|
{
|
|
|
|
VALUE ary, ssl_obj, ret_obj;
|
|
|
|
SSL_SESSION *sess;
|
|
|
|
void *ptr;
|
|
|
|
int state = 0;
|
|
|
|
|
|
|
|
OSSL_Debug("SSL SESSION get callback entered");
|
|
|
|
if ((ptr = SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
ssl_obj = (VALUE)ptr;
|
|
|
|
ary = rb_ary_new2(2);
|
|
|
|
rb_ary_push(ary, ssl_obj);
|
2008-07-22 11:34:23 -04:00
|
|
|
rb_ary_push(ary, rb_str_new((const char *)buf, len));
|
2007-04-02 18:10:12 -04:00
|
|
|
|
|
|
|
ret_obj = rb_protect((VALUE(*)_((VALUE)))ossl_call_session_get_cb, ary, &state);
|
|
|
|
if (state) {
|
|
|
|
rb_ivar_set(ssl_obj, ID_callback_state, INT2NUM(state));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (!rb_obj_is_instance_of(ret_obj, cSSLSession))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
SafeGetSSLSession(ret_obj, sess);
|
|
|
|
*copy = 1;
|
|
|
|
|
|
|
|
return sess;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_call_session_new_cb(VALUE ary)
|
|
|
|
{
|
2008-07-22 11:34:23 -04:00
|
|
|
VALUE ssl_obj, sslctx_obj, cb;
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2007-04-02 18:10:12 -04:00
|
|
|
Check_Type(ary, T_ARRAY);
|
|
|
|
ssl_obj = rb_ary_entry(ary, 0);
|
|
|
|
|
|
|
|
sslctx_obj = rb_iv_get(ssl_obj, "@context");
|
|
|
|
if (NIL_P(sslctx_obj)) return Qnil;
|
|
|
|
cb = rb_iv_get(sslctx_obj, "@session_new_cb");
|
|
|
|
if (NIL_P(cb)) return Qnil;
|
|
|
|
|
|
|
|
return rb_funcall(cb, rb_intern("call"), 1, ary);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return 1 normal. return 0 removes the session */
|
|
|
|
static int
|
|
|
|
ossl_sslctx_session_new_cb(SSL *ssl, SSL_SESSION *sess)
|
|
|
|
{
|
2011-10-18 21:15:35 -04:00
|
|
|
VALUE ary, ssl_obj, sess_obj;
|
2007-04-02 18:10:12 -04:00
|
|
|
void *ptr;
|
|
|
|
int state = 0;
|
|
|
|
|
|
|
|
OSSL_Debug("SSL SESSION new callback entered");
|
|
|
|
|
|
|
|
if ((ptr = SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx)) == NULL)
|
|
|
|
return 1;
|
|
|
|
ssl_obj = (VALUE)ptr;
|
|
|
|
sess_obj = rb_obj_alloc(cSSLSession);
|
|
|
|
CRYPTO_add(&sess->references, 1, CRYPTO_LOCK_SSL_SESSION);
|
|
|
|
DATA_PTR(sess_obj) = sess;
|
|
|
|
|
|
|
|
ary = rb_ary_new2(2);
|
|
|
|
rb_ary_push(ary, ssl_obj);
|
|
|
|
rb_ary_push(ary, sess_obj);
|
|
|
|
|
2011-10-18 21:15:35 -04:00
|
|
|
rb_protect((VALUE(*)_((VALUE)))ossl_call_session_new_cb, ary, &state);
|
2007-04-02 18:10:12 -04:00
|
|
|
if (state) {
|
|
|
|
rb_ivar_set(ssl_obj, ID_callback_state, INT2NUM(state));
|
|
|
|
}
|
|
|
|
|
2011-06-22 09:39:56 -04:00
|
|
|
/*
|
|
|
|
* return 0 which means to OpenSSL that the the session is still
|
|
|
|
* valid (since we created Ruby Session object) and was not freed by us
|
|
|
|
* with SSL_SESSION_free(). Call SSLContext#remove_session(sess) in
|
|
|
|
* session_get_cb block if you don't want OpenSSL to cache the session
|
|
|
|
* internally.
|
|
|
|
*/
|
|
|
|
return 0;
|
2007-04-02 18:10:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_call_session_remove_cb(VALUE ary)
|
|
|
|
{
|
2008-07-22 11:34:23 -04:00
|
|
|
VALUE sslctx_obj, cb;
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2007-04-02 18:10:12 -04:00
|
|
|
Check_Type(ary, T_ARRAY);
|
|
|
|
sslctx_obj = rb_ary_entry(ary, 0);
|
|
|
|
|
|
|
|
cb = rb_iv_get(sslctx_obj, "@session_remove_cb");
|
|
|
|
if (NIL_P(cb)) return Qnil;
|
|
|
|
|
|
|
|
return rb_funcall(cb, rb_intern("call"), 1, ary);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ossl_sslctx_session_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
|
|
|
|
{
|
2011-10-18 21:15:35 -04:00
|
|
|
VALUE ary, sslctx_obj, sess_obj;
|
2007-04-02 18:10:12 -04:00
|
|
|
void *ptr;
|
|
|
|
int state = 0;
|
|
|
|
|
|
|
|
OSSL_Debug("SSL SESSION remove callback entered");
|
|
|
|
|
|
|
|
if ((ptr = SSL_CTX_get_ex_data(ctx, ossl_ssl_ex_ptr_idx)) == NULL)
|
|
|
|
return;
|
|
|
|
sslctx_obj = (VALUE)ptr;
|
|
|
|
sess_obj = rb_obj_alloc(cSSLSession);
|
|
|
|
CRYPTO_add(&sess->references, 1, CRYPTO_LOCK_SSL_SESSION);
|
|
|
|
DATA_PTR(sess_obj) = sess;
|
|
|
|
|
|
|
|
ary = rb_ary_new2(2);
|
|
|
|
rb_ary_push(ary, sslctx_obj);
|
|
|
|
rb_ary_push(ary, sess_obj);
|
|
|
|
|
2011-10-18 21:15:35 -04:00
|
|
|
rb_protect((VALUE(*)_((VALUE)))ossl_call_session_remove_cb, ary, &state);
|
2007-04-02 18:10:12 -04:00
|
|
|
if (state) {
|
|
|
|
/*
|
|
|
|
the SSL_CTX is frozen, nowhere to save state.
|
|
|
|
there is no common accessor method to check it either.
|
|
|
|
rb_ivar_set(sslctx_obj, ID_callback_state, INT2NUM(state));
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-29 12:27:59 -05:00
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_add_extra_chain_cert_i(VALUE i, VALUE arg)
|
|
|
|
{
|
|
|
|
X509 *x509;
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
|
|
|
|
Data_Get_Struct(arg, SSL_CTX, ctx);
|
|
|
|
x509 = DupX509CertPtr(i);
|
|
|
|
if(!SSL_CTX_add_extra_chain_cert(ctx, x509)){
|
|
|
|
ossl_raise(eSSLError, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2009-01-24 16:45:42 -05:00
|
|
|
static VALUE ossl_sslctx_setup(VALUE self);
|
|
|
|
|
|
|
|
#ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME
|
|
|
|
static VALUE
|
|
|
|
ossl_call_servername_cb(VALUE ary)
|
|
|
|
{
|
|
|
|
VALUE ssl_obj, sslctx_obj, cb, ret_obj;
|
|
|
|
|
|
|
|
Check_Type(ary, T_ARRAY);
|
|
|
|
ssl_obj = rb_ary_entry(ary, 0);
|
|
|
|
|
|
|
|
sslctx_obj = rb_iv_get(ssl_obj, "@context");
|
|
|
|
if (NIL_P(sslctx_obj)) return Qnil;
|
|
|
|
cb = rb_iv_get(sslctx_obj, "@servername_cb");
|
|
|
|
if (NIL_P(cb)) return Qnil;
|
|
|
|
|
|
|
|
ret_obj = rb_funcall(cb, rb_intern("call"), 1, ary);
|
|
|
|
if (rb_obj_is_kind_of(ret_obj, cSSLContext)) {
|
|
|
|
SSL *ssl;
|
|
|
|
SSL_CTX *ctx2;
|
|
|
|
|
|
|
|
ossl_sslctx_setup(ret_obj);
|
|
|
|
Data_Get_Struct(ssl_obj, SSL, ssl);
|
|
|
|
Data_Get_Struct(ret_obj, SSL_CTX, ctx2);
|
|
|
|
SSL_set_SSL_CTX(ssl, ctx2);
|
|
|
|
} else if (!NIL_P(ret_obj)) {
|
2011-06-22 04:41:08 -04:00
|
|
|
ossl_raise(rb_eArgError, "servername_cb must return an OpenSSL::SSL::SSLContext object or nil");
|
2009-01-24 16:45:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ssl_servername_cb(SSL *ssl, int *ad, void *arg)
|
|
|
|
{
|
2011-10-18 21:15:35 -04:00
|
|
|
VALUE ary, ssl_obj;
|
2009-01-24 16:45:42 -05:00
|
|
|
void *ptr;
|
|
|
|
int state = 0;
|
|
|
|
const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
|
|
|
|
|
|
|
|
if (!servername)
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
|
|
|
|
if ((ptr = SSL_get_ex_data(ssl, ossl_ssl_ex_ptr_idx)) == NULL)
|
|
|
|
return SSL_TLSEXT_ERR_ALERT_FATAL;
|
|
|
|
ssl_obj = (VALUE)ptr;
|
|
|
|
ary = rb_ary_new2(2);
|
|
|
|
rb_ary_push(ary, ssl_obj);
|
|
|
|
rb_ary_push(ary, rb_str_new2(servername));
|
|
|
|
|
2011-10-18 21:15:35 -04:00
|
|
|
rb_protect((VALUE(*)_((VALUE)))ossl_call_servername_cb, ary, &state);
|
2009-01-24 16:45:42 -05:00
|
|
|
if (state) {
|
|
|
|
rb_ivar_set(ssl_obj, ID_callback_state, INT2NUM(state));
|
|
|
|
return SSL_TLSEXT_ERR_ALERT_FATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SSL_TLSEXT_ERR_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-04-20 18:32:06 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ctx.setup => Qtrue # first time
|
|
|
|
* ctx.setup => nil # thereafter
|
|
|
|
*
|
|
|
|
* This method is called automatically when a new SSLSocket is created.
|
2010-12-05 19:54:44 -05:00
|
|
|
* Normally you do not need to call this method (unless you are writing an
|
|
|
|
* extension in C).
|
2008-04-20 18:32:06 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_setup(VALUE self)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
X509 *cert = NULL, *client_ca = NULL;
|
|
|
|
X509_STORE *store;
|
|
|
|
EVP_PKEY *key = NULL;
|
|
|
|
char *ca_path = NULL, *ca_file = NULL;
|
|
|
|
int i, verify_mode;
|
|
|
|
VALUE val;
|
|
|
|
|
|
|
|
if(OBJ_FROZEN(self)) return Qnil;
|
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
|
|
|
|
2005-03-09 05:45:42 -05:00
|
|
|
#if !defined(OPENSSL_NO_DH)
|
|
|
|
if (RTEST(ossl_sslctx_get_tmp_dh_cb(self))){
|
|
|
|
SSL_CTX_set_tmp_dh_callback(ctx, ossl_tmp_dh_callback);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
SSL_CTX_set_tmp_dh_callback(ctx, ossl_default_tmp_dh_callback);
|
|
|
|
}
|
|
|
|
#endif
|
2007-04-02 18:10:12 -04:00
|
|
|
SSL_CTX_set_ex_data(ctx, ossl_ssl_ex_ptr_idx, (void*)self);
|
2005-03-09 05:45:42 -05:00
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
val = ossl_sslctx_get_cert_store(self);
|
|
|
|
if(!NIL_P(val)){
|
|
|
|
/*
|
|
|
|
* WORKAROUND:
|
|
|
|
* X509_STORE can count references, but
|
|
|
|
* X509_STORE_free() doesn't care it.
|
|
|
|
* So we won't increment it but mark it by ex_data.
|
|
|
|
*/
|
|
|
|
store = GetX509StorePtr(val); /* NO NEED TO DUP */
|
|
|
|
SSL_CTX_set_cert_store(ctx, store);
|
|
|
|
SSL_CTX_set_ex_data(ctx, ossl_ssl_ex_store_p, (void*)1);
|
|
|
|
}
|
|
|
|
|
2003-10-29 12:27:59 -05:00
|
|
|
val = ossl_sslctx_get_extra_cert(self);
|
|
|
|
if(!NIL_P(val)){
|
2006-02-03 04:15:42 -05:00
|
|
|
rb_block_call(val, rb_intern("each"), 0, 0, ossl_sslctx_add_extra_chain_cert_i, self);
|
2003-10-29 12:27:59 -05:00
|
|
|
}
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
/* private key may be bundled in certificate file. */
|
|
|
|
val = ossl_sslctx_get_cert(self);
|
|
|
|
cert = NIL_P(val) ? NULL : GetX509CertPtr(val); /* NO DUP NEEDED */
|
|
|
|
val = ossl_sslctx_get_key(self);
|
|
|
|
key = NIL_P(val) ? NULL : GetPKeyPtr(val); /* NO DUP NEEDED */
|
|
|
|
if (cert && key) {
|
|
|
|
if (!SSL_CTX_use_certificate(ctx, cert)) {
|
|
|
|
/* Adds a ref => Safe to FREE */
|
2011-11-23 18:15:09 -05:00
|
|
|
ossl_raise(eSSLError, "SSL_CTX_use_certificate");
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
if (!SSL_CTX_use_PrivateKey(ctx, key)) {
|
|
|
|
/* Adds a ref => Safe to FREE */
|
2011-11-23 18:15:09 -05:00
|
|
|
ossl_raise(eSSLError, "SSL_CTX_use_PrivateKey");
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
if (!SSL_CTX_check_private_key(ctx)) {
|
2011-11-23 18:15:09 -05:00
|
|
|
ossl_raise(eSSLError, "SSL_CTX_check_private_key");
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val = ossl_sslctx_get_client_ca(self);
|
|
|
|
if(!NIL_P(val)){
|
|
|
|
if(TYPE(val) == T_ARRAY){
|
2006-09-02 10:42:08 -04:00
|
|
|
for(i = 0; i < RARRAY_LEN(val); i++){
|
|
|
|
client_ca = GetX509CertPtr(RARRAY_PTR(val)[i]);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (!SSL_CTX_add_client_CA(ctx, client_ca)){
|
|
|
|
/* Copies X509_NAME => FREE it. */
|
|
|
|
ossl_raise(eSSLError, "SSL_CTX_add_client_CA");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
client_ca = GetX509CertPtr(val); /* NO DUP NEEDED. */
|
|
|
|
if (!SSL_CTX_add_client_CA(ctx, client_ca)){
|
|
|
|
/* Copies X509_NAME => FREE it. */
|
|
|
|
ossl_raise(eSSLError, "SSL_CTX_add_client_CA");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val = ossl_sslctx_get_ca_file(self);
|
|
|
|
ca_file = NIL_P(val) ? NULL : StringValuePtr(val);
|
|
|
|
val = ossl_sslctx_get_ca_path(self);
|
|
|
|
ca_path = NIL_P(val) ? NULL : StringValuePtr(val);
|
|
|
|
if(ca_file || ca_path){
|
|
|
|
if (!SSL_CTX_load_verify_locations(ctx, ca_file, ca_path))
|
|
|
|
rb_warning("can't set verify locations");
|
|
|
|
}
|
|
|
|
|
|
|
|
val = ossl_sslctx_get_verify_mode(self);
|
|
|
|
verify_mode = NIL_P(val) ? SSL_VERIFY_NONE : NUM2INT(val);
|
|
|
|
SSL_CTX_set_verify(ctx, verify_mode, ossl_ssl_verify_callback);
|
2005-03-09 05:45:42 -05:00
|
|
|
if (RTEST(ossl_sslctx_get_client_cert_cb(self)))
|
|
|
|
SSL_CTX_set_client_cert_cb(ctx, ossl_client_cert_cb);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
val = ossl_sslctx_get_timeout(self);
|
|
|
|
if(!NIL_P(val)) SSL_CTX_set_timeout(ctx, NUM2LONG(val));
|
|
|
|
|
|
|
|
val = ossl_sslctx_get_verify_dep(self);
|
2011-03-24 00:49:18 -04:00
|
|
|
if(!NIL_P(val)) SSL_CTX_set_verify_depth(ctx, NUM2INT(val));
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
val = ossl_sslctx_get_options(self);
|
2012-02-08 00:27:14 -05:00
|
|
|
if(!NIL_P(val)) {
|
|
|
|
SSL_CTX_set_options(ctx, NUM2LONG(val));
|
|
|
|
} else {
|
|
|
|
SSL_CTX_set_options(ctx, SSL_OP_ALL);
|
|
|
|
}
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_obj_freeze(self);
|
|
|
|
|
2005-05-22 06:18:41 -04:00
|
|
|
val = ossl_sslctx_get_sess_id_ctx(self);
|
|
|
|
if (!NIL_P(val)){
|
|
|
|
StringValue(val);
|
2008-07-22 11:34:23 -04:00
|
|
|
if (!SSL_CTX_set_session_id_context(ctx, (unsigned char *)RSTRING_PTR(val),
|
2011-03-24 03:29:21 -04:00
|
|
|
RSTRING_LENINT(val))){
|
2011-11-23 18:15:09 -05:00
|
|
|
ossl_raise(eSSLError, "SSL_CTX_set_session_id_context");
|
2005-05-22 06:18:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-02 18:10:12 -04:00
|
|
|
if (RTEST(rb_iv_get(self, "@session_get_cb"))) {
|
|
|
|
SSL_CTX_sess_set_get_cb(ctx, ossl_sslctx_session_get_cb);
|
|
|
|
OSSL_Debug("SSL SESSION get callback added");
|
|
|
|
}
|
|
|
|
if (RTEST(rb_iv_get(self, "@session_new_cb"))) {
|
|
|
|
SSL_CTX_sess_set_new_cb(ctx, ossl_sslctx_session_new_cb);
|
|
|
|
OSSL_Debug("SSL SESSION new callback added");
|
|
|
|
}
|
|
|
|
if (RTEST(rb_iv_get(self, "@session_remove_cb"))) {
|
|
|
|
SSL_CTX_sess_set_remove_cb(ctx, ossl_sslctx_session_remove_cb);
|
|
|
|
OSSL_Debug("SSL SESSION remove callback added");
|
|
|
|
}
|
2009-01-24 16:45:42 -05:00
|
|
|
|
|
|
|
#ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME
|
|
|
|
val = rb_iv_get(self, "@servername_cb");
|
|
|
|
if (!NIL_P(val)) {
|
|
|
|
SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
|
|
|
|
OSSL_Debug("SSL TLSEXT servername callback added");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_cipher_to_ary(SSL_CIPHER *cipher)
|
|
|
|
{
|
|
|
|
VALUE ary;
|
|
|
|
int bits, alg_bits;
|
|
|
|
|
|
|
|
ary = rb_ary_new2(4);
|
|
|
|
rb_ary_push(ary, rb_str_new2(SSL_CIPHER_get_name(cipher)));
|
|
|
|
rb_ary_push(ary, rb_str_new2(SSL_CIPHER_get_version(cipher)));
|
|
|
|
bits = SSL_CIPHER_get_bits(cipher, &alg_bits);
|
|
|
|
rb_ary_push(ary, INT2FIX(bits));
|
|
|
|
rb_ary_push(ary, INT2FIX(alg_bits));
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ctx.ciphers => [[name, version, bits, alg_bits], ...]
|
2010-12-05 19:54:44 -05:00
|
|
|
*
|
|
|
|
* The list of ciphers configured for this context.
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_get_ciphers(VALUE self)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
STACK_OF(SSL_CIPHER) *ciphers;
|
|
|
|
SSL_CIPHER *cipher;
|
|
|
|
VALUE ary;
|
|
|
|
int i, num;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
|
|
|
if(!ctx){
|
|
|
|
rb_warning("SSL_CTX is not initialized.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
ciphers = ctx->cipher_list;
|
|
|
|
|
|
|
|
if (!ciphers)
|
|
|
|
return rb_ary_new();
|
|
|
|
|
2010-06-11 10:06:23 -04:00
|
|
|
num = sk_SSL_CIPHER_num(ciphers);
|
2003-07-23 12:12:24 -04:00
|
|
|
ary = rb_ary_new2(num);
|
|
|
|
for(i = 0; i < num; i++){
|
2010-06-11 10:06:23 -04:00
|
|
|
cipher = sk_SSL_CIPHER_value(ciphers, i);
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_ary_push(ary, ossl_ssl_cipher_to_ary(cipher));
|
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ctx.ciphers = "cipher1:cipher2:..."
|
|
|
|
* ctx.ciphers = [name, ...]
|
|
|
|
* ctx.ciphers = [[name, version, bits, alg_bits], ...]
|
2010-11-29 15:23:59 -05:00
|
|
|
*
|
|
|
|
* Sets the list of available ciphers for this context. Note in a server
|
|
|
|
* context some ciphers require the appropriate certificates. For example, an
|
|
|
|
* RSA cipher can only be chosen when an RSA certificate is available.
|
|
|
|
*
|
|
|
|
* See also OpenSSL::Cipher and OpenSSL::Cipher::ciphers
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_set_ciphers(VALUE self, VALUE v)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
VALUE str, elem;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
rb_check_frozen(self);
|
2005-03-09 05:45:42 -05:00
|
|
|
if (NIL_P(v))
|
|
|
|
return v;
|
|
|
|
else if (TYPE(v) == T_ARRAY) {
|
2006-03-15 02:42:00 -05:00
|
|
|
str = rb_str_new(0, 0);
|
2006-09-02 10:42:08 -04:00
|
|
|
for (i = 0; i < RARRAY_LEN(v); i++) {
|
2003-07-23 12:12:24 -04:00
|
|
|
elem = rb_ary_entry(v, i);
|
|
|
|
if (TYPE(elem) == T_ARRAY) elem = rb_ary_entry(elem, 0);
|
|
|
|
elem = rb_String(elem);
|
|
|
|
rb_str_append(str, elem);
|
2006-09-02 10:42:08 -04:00
|
|
|
if (i < RARRAY_LEN(v)-1) rb_str_cat2(str, ":");
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
str = v;
|
|
|
|
StringValue(str);
|
|
|
|
}
|
|
|
|
|
2004-12-15 01:35:55 -05:00
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
|
|
|
if(!ctx){
|
|
|
|
ossl_raise(eSSLError, "SSL_CTX is not initialized.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
2006-08-31 06:30:33 -04:00
|
|
|
if (!SSL_CTX_set_cipher_list(ctx, RSTRING_PTR(str))) {
|
2011-11-23 18:15:09 -05:00
|
|
|
ossl_raise(eSSLError, "SSL_CTX_set_cipher_list");
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
2005-03-09 05:45:42 -05:00
|
|
|
|
|
|
|
return v;
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2007-04-02 18:10:12 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ctx.session_add(session) -> true | false
|
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Adds +session+ to the session cache
|
2007-04-02 18:10:12 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_session_add(VALUE self, VALUE arg)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
SSL_SESSION *sess;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
|
|
|
SafeGetSSLSession(arg, sess);
|
|
|
|
|
|
|
|
return SSL_CTX_add_session(ctx, sess) == 1 ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ctx.session_remove(session) -> true | false
|
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Removes +session+ from the session cache
|
2007-04-02 18:10:12 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_session_remove(VALUE self, VALUE arg)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
SSL_SESSION *sess;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
|
|
|
SafeGetSSLSession(arg, sess);
|
|
|
|
|
|
|
|
return SSL_CTX_remove_session(ctx, sess) == 1 ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-12-05 19:54:44 -05:00
|
|
|
* ctx.session_cache_mode -> Integer
|
2007-04-02 18:10:12 -04:00
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* The current session cache mode.
|
2007-04-02 18:10:12 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_get_session_cache_mode(VALUE self)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
|
|
|
|
|
|
|
return LONG2NUM(SSL_CTX_get_session_cache_mode(ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-12-05 19:54:44 -05:00
|
|
|
* ctx.session_cache_mode=(integer) -> Integer
|
2007-04-02 18:10:12 -04:00
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Sets the SSL session cache mode. Bitwise-or together the desired
|
|
|
|
* SESSION_CACHE_* constants to set. See SSL_CTX_set_session_cache_mode(3) for
|
|
|
|
* details.
|
2007-04-02 18:10:12 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_set_session_cache_mode(VALUE self, VALUE arg)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
|
|
|
|
|
|
|
SSL_CTX_set_session_cache_mode(ctx, NUM2LONG(arg));
|
|
|
|
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-12-05 19:54:44 -05:00
|
|
|
* ctx.session_cache_size -> Integer
|
2007-04-02 18:10:12 -04:00
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Returns the current session cache size. Zero is used to represent an
|
|
|
|
* unlimited cache size.
|
2007-04-02 18:10:12 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_get_session_cache_size(VALUE self)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
|
|
|
|
|
|
|
return LONG2NUM(SSL_CTX_sess_get_cache_size(ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-12-05 19:54:44 -05:00
|
|
|
* ctx.session_cache_size=(integer) -> Integer
|
2007-04-02 18:10:12 -04:00
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Sets the session cache size. Returns the previously valid session cache
|
|
|
|
* size. Zero is used to represent an unlimited session cache size.
|
2007-04-02 18:10:12 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_set_session_cache_size(VALUE self, VALUE arg)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
|
|
|
|
|
|
|
SSL_CTX_sess_set_cache_size(ctx, NUM2LONG(arg));
|
|
|
|
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ctx.session_cache_stats -> Hash
|
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Returns a Hash containing the following keys:
|
|
|
|
*
|
|
|
|
* :accept:: Number of started SSL/TLS handshakes in server mode
|
|
|
|
* :accept_good:: Number of established SSL/TLS sessions in server mode
|
|
|
|
* :accept_renegotiate:: Number of start renegotiations in server mode
|
|
|
|
* :cache_full:: Number of sessions that were removed due to cache overflow
|
|
|
|
* :cache_hits:: Number of successfully reused connections
|
|
|
|
* :cache_misses:: Number of sessions proposed by clients that were not found
|
|
|
|
* in the cache
|
|
|
|
* :cache_num:: Number of sessions in the internal session cache
|
|
|
|
* :cb_hits:: Number of sessions retrieved from the external cache in server
|
|
|
|
* mode
|
|
|
|
* :connect:: Number of started SSL/TLS handshakes in client mode
|
|
|
|
* :connect_good:: Number of established SSL/TLS sessions in client mode
|
|
|
|
* :connect_renegotiate:: Number of start renegotiations in client mode
|
|
|
|
* :timeouts:: Number of sessions proposed by clients that were found in the
|
|
|
|
* cache but had expired due to timeouts
|
2007-04-02 18:10:12 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_get_session_cache_stats(VALUE self)
|
|
|
|
{
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
VALUE hash;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
|
|
|
|
|
|
|
hash = rb_hash_new();
|
2008-04-20 18:32:06 -04:00
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("cache_num")), LONG2NUM(SSL_CTX_sess_number(ctx)));
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("connect")), LONG2NUM(SSL_CTX_sess_connect(ctx)));
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("connect_good")), LONG2NUM(SSL_CTX_sess_connect_good(ctx)));
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("connect_renegotiate")), LONG2NUM(SSL_CTX_sess_connect_renegotiate(ctx)));
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("accept")), LONG2NUM(SSL_CTX_sess_accept(ctx)));
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("accept_good")), LONG2NUM(SSL_CTX_sess_accept_good(ctx)));
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("accept_renegotiate")), LONG2NUM(SSL_CTX_sess_accept_renegotiate(ctx)));
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("cache_hits")), LONG2NUM(SSL_CTX_sess_hits(ctx)));
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("cb_hits")), LONG2NUM(SSL_CTX_sess_cb_hits(ctx)));
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("cache_misses")), LONG2NUM(SSL_CTX_sess_misses(ctx)));
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("cache_full")), LONG2NUM(SSL_CTX_sess_cache_full(ctx)));
|
|
|
|
rb_hash_aset(hash, ID2SYM(rb_intern("timeouts")), LONG2NUM(SSL_CTX_sess_timeouts(ctx)));
|
2007-04-02 18:10:12 -04:00
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ctx.flush_sessions(time | nil) -> self
|
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Removes sessions in the internal cache that have expired at +time+.
|
2007-04-02 18:10:12 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_sslctx_flush_sessions(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
VALUE arg1;
|
|
|
|
SSL_CTX *ctx;
|
|
|
|
time_t tm = 0;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &arg1);
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL_CTX, ctx);
|
|
|
|
|
|
|
|
if (NIL_P(arg1)) {
|
|
|
|
tm = time(0);
|
|
|
|
} else if (rb_obj_is_instance_of(arg1, rb_cTime)) {
|
|
|
|
tm = NUM2LONG(rb_funcall(arg1, rb_intern("to_i"), 0));
|
|
|
|
} else {
|
2011-06-22 04:41:08 -04:00
|
|
|
ossl_raise(rb_eArgError, "arg must be Time or nil");
|
2007-04-02 18:10:12 -04:00
|
|
|
}
|
|
|
|
|
2009-03-13 03:45:35 -04:00
|
|
|
SSL_CTX_flush_sessions(ctx, (long)tm);
|
2007-04-02 18:10:12 -04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
/*
|
|
|
|
* SSLSocket class
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ossl_ssl_shutdown(SSL *ssl)
|
|
|
|
{
|
2011-06-24 03:01:52 -04:00
|
|
|
int i, rc;
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
if (ssl) {
|
2011-06-24 03:01:52 -04:00
|
|
|
/* 4 is from SSL_smart_shutdown() of mod_ssl.c (v2.2.19) */
|
|
|
|
/* It says max 2x pending + 2x data = 4 */
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
/*
|
|
|
|
* Ignore the case SSL_shutdown returns -1. Empty handshake_func
|
|
|
|
* must not happen.
|
|
|
|
*/
|
|
|
|
if (rc = SSL_shutdown(ssl))
|
|
|
|
break;
|
|
|
|
}
|
2011-07-25 00:30:02 -04:00
|
|
|
ERR_clear_error();
|
|
|
|
SSL_clear(ssl);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ossl_ssl_free(SSL *ssl)
|
|
|
|
{
|
|
|
|
ossl_ssl_shutdown(ssl);
|
|
|
|
SSL_free(ssl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_s_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
return Data_Wrap_Struct(klass, 0, ossl_ssl_free, NULL);
|
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* SSLSocket.new(io) => aSSLSocket
|
|
|
|
* SSLSocket.new(io, ctx) => aSSLSocket
|
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Creates a new SSL socket from +io+ which must be a real ruby object (not an
|
|
|
|
* IO-like object that responds to read/write.
|
|
|
|
*
|
|
|
|
* If +ctx+ is provided the SSL Sockets initial params will be taken from
|
|
|
|
* the context.
|
2007-04-03 14:29:30 -04:00
|
|
|
*
|
|
|
|
* The OpenSSL::Buffering module provides additional IO methods.
|
|
|
|
*
|
|
|
|
* This method will freeze the SSLContext if one is provided;
|
|
|
|
* however, session management is still allowed in the frozen SSLContext.
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_ssl_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
VALUE io, ctx;
|
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "11", &io, &ctx) == 1) {
|
|
|
|
ctx = rb_funcall(cSSLContext, rb_intern("new"), 0);
|
|
|
|
}
|
|
|
|
OSSL_Check_Kind(ctx, cSSLContext);
|
|
|
|
Check_Type(io, T_FILE);
|
|
|
|
ossl_ssl_set_io(self, io);
|
|
|
|
ossl_ssl_set_ctx(self, ctx);
|
2003-08-18 18:49:48 -04:00
|
|
|
ossl_ssl_set_sync_close(self, Qfalse);
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_sslctx_setup(ctx);
|
2009-12-02 09:54:50 -05:00
|
|
|
|
|
|
|
rb_iv_set(self, "@hostname", Qnil);
|
|
|
|
|
2003-10-29 12:27:59 -05:00
|
|
|
rb_call_super(0, 0);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_setup(VALUE self)
|
|
|
|
{
|
2005-03-09 05:45:42 -05:00
|
|
|
VALUE io, v_ctx, cb;
|
2003-07-23 12:12:24 -04:00
|
|
|
SSL_CTX *ctx;
|
|
|
|
SSL *ssl;
|
* file.c, gc.c, io.c, ruby.h, rubyio.h, win32/win32.h (rb_io_t):
renamed from OpenFile.
* ext/dl/cptr.c, ext/io/wait/wait.c, ext/openssl/ossl.h,
ext/openssl/ossl_bio.c, ext/openssl/ossl_ssl.c, ext/pty/pty.c,
ext/readline/readline.c, ext/socket/socket.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-24 05:30:50 -05:00
|
|
|
rb_io_t *fptr;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
if(!ssl){
|
2009-01-24 16:45:42 -05:00
|
|
|
#ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME
|
|
|
|
VALUE hostname = rb_iv_get(self, "@hostname");
|
|
|
|
#endif
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
v_ctx = ossl_ssl_get_ctx(self);
|
|
|
|
Data_Get_Struct(v_ctx, SSL_CTX, ctx);
|
|
|
|
|
|
|
|
ssl = SSL_new(ctx);
|
|
|
|
if (!ssl) {
|
2011-11-23 18:15:09 -05:00
|
|
|
ossl_raise(eSSLError, "SSL_new");
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
DATA_PTR(self) = ssl;
|
|
|
|
|
2009-01-24 16:45:42 -05:00
|
|
|
#ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME
|
|
|
|
if (!NIL_P(hostname)) {
|
|
|
|
if (SSL_set_tlsext_host_name(ssl, StringValuePtr(hostname)) != 1)
|
2011-11-23 18:15:09 -05:00
|
|
|
ossl_raise(eSSLError, "SSL_set_tlsext_host_name");
|
2009-01-24 16:45:42 -05:00
|
|
|
}
|
|
|
|
#endif
|
2003-07-23 12:12:24 -04:00
|
|
|
io = ossl_ssl_get_io(self);
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
|
|
|
rb_io_check_writable(fptr);
|
2007-04-02 15:00:23 -04:00
|
|
|
SSL_set_fd(ssl, TO_SOCKET(FPTR_TO_FD(fptr)));
|
2005-03-09 05:45:42 -05:00
|
|
|
SSL_set_ex_data(ssl, ossl_ssl_ex_ptr_idx, (void*)self);
|
|
|
|
cb = ossl_sslctx_get_verify_cb(v_ctx);
|
|
|
|
SSL_set_ex_data(ssl, ossl_ssl_ex_vcb_idx, (void*)cb);
|
|
|
|
cb = ossl_sslctx_get_client_cert_cb(v_ctx);
|
|
|
|
SSL_set_ex_data(ssl, ossl_ssl_ex_client_cert_cb_idx, (void*)cb);
|
|
|
|
cb = ossl_sslctx_get_tmp_dh_cb(v_ctx);
|
|
|
|
SSL_set_ex_data(ssl, ossl_ssl_ex_tmp_dh_callback_idx, (void*)cb);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2005-03-07 02:47:40 -05:00
|
|
|
#ifdef _WIN32
|
2011-03-09 13:01:11 -05:00
|
|
|
#define ssl_get_error(ssl, ret) (errno = rb_w32_map_errno(WSAGetLastError()), SSL_get_error((ssl), (ret)))
|
2005-03-07 02:47:40 -05:00
|
|
|
#else
|
2011-03-09 13:01:11 -05:00
|
|
|
#define ssl_get_error(ssl, ret) SSL_get_error((ssl), (ret))
|
2005-03-07 02:47:40 -05:00
|
|
|
#endif
|
|
|
|
|
2009-03-21 02:56:33 -04:00
|
|
|
static void
|
|
|
|
write_would_block(int nonblock)
|
|
|
|
{
|
|
|
|
if (nonblock) {
|
|
|
|
VALUE exc = ossl_exc_new(eSSLError, "write would block");
|
|
|
|
rb_extend_object(exc, rb_mWaitWritable);
|
|
|
|
rb_exc_raise(exc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
read_would_block(int nonblock)
|
|
|
|
{
|
|
|
|
if (nonblock) {
|
|
|
|
VALUE exc = ossl_exc_new(eSSLError, "read would block");
|
|
|
|
rb_extend_object(exc, rb_mWaitReadable);
|
|
|
|
rb_exc_raise(exc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-05 16:36:11 -05:00
|
|
|
static VALUE
|
2009-03-21 02:56:33 -04:00
|
|
|
ossl_start_ssl(VALUE self, int (*func)(), const char *funcname, int nonblock)
|
2003-12-06 08:59:31 -05:00
|
|
|
{
|
2005-03-05 16:36:11 -05:00
|
|
|
SSL *ssl;
|
* file.c, gc.c, io.c, ruby.h, rubyio.h, win32/win32.h (rb_io_t):
renamed from OpenFile.
* ext/dl/cptr.c, ext/io/wait/wait.c, ext/openssl/ossl.h,
ext/openssl/ossl_bio.c, ext/openssl/ossl_ssl.c, ext/pty/pty.c,
ext/readline/readline.c, ext/socket/socket.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-24 05:30:50 -05:00
|
|
|
rb_io_t *fptr;
|
2007-04-02 18:10:12 -04:00
|
|
|
int ret, ret2;
|
|
|
|
VALUE cb_state;
|
|
|
|
|
|
|
|
rb_ivar_set(self, ID_callback_state, Qnil);
|
2003-12-06 08:59:31 -05:00
|
|
|
|
2005-03-05 16:36:11 -05:00
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
GetOpenFile(ossl_ssl_get_io(self), fptr);
|
2003-12-06 08:59:31 -05:00
|
|
|
for(;;){
|
2009-01-24 16:45:42 -05:00
|
|
|
ret = func(ssl);
|
|
|
|
|
|
|
|
cb_state = rb_ivar_get(self, ID_callback_state);
|
|
|
|
if (!NIL_P(cb_state))
|
|
|
|
rb_jump_tag(NUM2INT(cb_state));
|
|
|
|
|
|
|
|
if (ret > 0)
|
|
|
|
break;
|
|
|
|
|
2007-04-02 18:10:12 -04:00
|
|
|
switch((ret2 = ssl_get_error(ssl, ret))){
|
2003-12-06 08:59:31 -05:00
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2009-03-21 02:56:33 -04:00
|
|
|
write_would_block(nonblock);
|
2007-04-02 15:00:23 -04:00
|
|
|
rb_io_wait_writable(FPTR_TO_FD(fptr));
|
2005-03-05 16:36:11 -05:00
|
|
|
continue;
|
2003-12-06 08:59:31 -05:00
|
|
|
case SSL_ERROR_WANT_READ:
|
2009-03-21 02:56:33 -04:00
|
|
|
read_would_block(nonblock);
|
2007-04-02 15:00:23 -04:00
|
|
|
rb_io_wait_readable(FPTR_TO_FD(fptr));
|
2005-03-05 16:36:11 -05:00
|
|
|
continue;
|
|
|
|
case SSL_ERROR_SYSCALL:
|
2007-04-02 18:10:12 -04:00
|
|
|
if (errno) rb_sys_fail(funcname);
|
|
|
|
ossl_raise(eSSLError, "%s SYSCALL returned=%d errno=%d state=%s", funcname, ret2, errno, SSL_state_string_long(ssl));
|
2003-12-06 08:59:31 -05:00
|
|
|
default:
|
2007-04-02 18:10:12 -04:00
|
|
|
ossl_raise(eSSLError, "%s returned=%d errno=%d state=%s", funcname, ret2, errno, SSL_state_string_long(ssl));
|
2003-12-06 08:59:31 -05:00
|
|
|
}
|
|
|
|
}
|
2005-03-05 16:36:11 -05:00
|
|
|
|
|
|
|
return self;
|
2003-12-06 08:59:31 -05:00
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.connect => self
|
2010-12-05 19:54:44 -05:00
|
|
|
*
|
|
|
|
* Initiates an SSL/TLS handshake with a server. The handshake may be started
|
|
|
|
* after unencrypted data has been sent over the socket.
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_ssl_connect(VALUE self)
|
|
|
|
{
|
|
|
|
ossl_ssl_setup(self);
|
2009-03-21 02:56:33 -04:00
|
|
|
return ossl_start_ssl(self, SSL_connect, "SSL_connect", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.connect_nonblock => self
|
2009-03-21 04:08:52 -04:00
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Initiates the SSL/TLS handshake as a client in non-blocking manner.
|
2009-03-21 04:08:52 -04:00
|
|
|
*
|
|
|
|
* # emulates blocking connect
|
|
|
|
* begin
|
|
|
|
* ssl.connect_nonblock
|
|
|
|
* rescue IO::WaitReadable
|
|
|
|
* IO.select([s2])
|
|
|
|
* retry
|
|
|
|
* rescue IO::WaitWritable
|
|
|
|
* IO.select(nil, [s2])
|
|
|
|
* retry
|
|
|
|
* end
|
|
|
|
*
|
2009-03-21 02:56:33 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_connect_nonblock(VALUE self)
|
|
|
|
{
|
|
|
|
ossl_ssl_setup(self);
|
|
|
|
return ossl_start_ssl(self, SSL_connect, "SSL_connect", 1);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.accept => self
|
2010-12-05 19:54:44 -05:00
|
|
|
*
|
|
|
|
* Waits for a SSL/TLS client to initiate a handshake. The handshake may be
|
|
|
|
* started after unencrypted data has been sent over the socket.
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_ssl_accept(VALUE self)
|
|
|
|
{
|
|
|
|
ossl_ssl_setup(self);
|
2009-03-21 02:56:33 -04:00
|
|
|
return ossl_start_ssl(self, SSL_accept, "SSL_accept", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.accept_nonblock => self
|
2009-03-21 04:08:52 -04:00
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Initiates the SSL/TLS handshake as a server in non-blocking manner.
|
2009-03-21 04:08:52 -04:00
|
|
|
*
|
|
|
|
* # emulates blocking accept
|
|
|
|
* begin
|
|
|
|
* ssl.accept_nonblock
|
|
|
|
* rescue IO::WaitReadable
|
|
|
|
* IO.select([s2])
|
|
|
|
* retry
|
|
|
|
* rescue IO::WaitWritable
|
|
|
|
* IO.select(nil, [s2])
|
|
|
|
* retry
|
|
|
|
* end
|
|
|
|
*
|
2009-03-21 02:56:33 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_accept_nonblock(VALUE self)
|
|
|
|
{
|
|
|
|
ossl_ssl_setup(self);
|
|
|
|
return ossl_start_ssl(self, SSL_accept, "SSL_accept", 1);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2008-12-30 23:52:18 -05:00
|
|
|
ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
int ilen, nread = 0;
|
2004-05-26 14:11:29 -04:00
|
|
|
VALUE len, str;
|
* file.c, gc.c, io.c, ruby.h, rubyio.h, win32/win32.h (rb_io_t):
renamed from OpenFile.
* ext/dl/cptr.c, ext/io/wait/wait.c, ext/openssl/ossl.h,
ext/openssl/ossl_bio.c, ext/openssl/ossl_ssl.c, ext/pty/pty.c,
ext/readline/readline.c, ext/socket/socket.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-24 05:30:50 -05:00
|
|
|
rb_io_t *fptr;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2004-05-26 14:11:29 -04:00
|
|
|
rb_scan_args(argc, argv, "11", &len, &str);
|
2003-07-23 12:12:24 -04:00
|
|
|
ilen = NUM2INT(len);
|
2004-05-26 14:11:29 -04:00
|
|
|
if(NIL_P(str)) str = rb_str_new(0, ilen);
|
|
|
|
else{
|
|
|
|
StringValue(str);
|
|
|
|
rb_str_modify(str);
|
|
|
|
rb_str_resize(str, ilen);
|
|
|
|
}
|
2004-08-22 03:28:45 -04:00
|
|
|
if(ilen == 0) return str;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2004-12-15 01:35:55 -05:00
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
GetOpenFile(ossl_ssl_get_io(self), fptr);
|
2003-07-23 12:12:24 -04:00
|
|
|
if (ssl) {
|
2008-12-30 23:52:18 -05:00
|
|
|
if(!nonblock && SSL_pending(ssl) <= 0)
|
|
|
|
rb_thread_wait_fd(FPTR_TO_FD(fptr));
|
2003-12-24 02:29:40 -05:00
|
|
|
for (;;){
|
2011-03-24 03:29:21 -04:00
|
|
|
nread = SSL_read(ssl, RSTRING_PTR(str), RSTRING_LENINT(str));
|
2008-12-30 23:52:18 -05:00
|
|
|
switch(ssl_get_error(ssl, nread)){
|
2003-12-24 02:29:40 -05:00
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
goto end;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
2004-08-22 03:28:45 -04:00
|
|
|
rb_eof_error();
|
2003-12-24 02:29:40 -05:00
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2009-03-21 02:56:33 -04:00
|
|
|
write_would_block(nonblock);
|
2007-04-02 15:00:23 -04:00
|
|
|
rb_io_wait_writable(FPTR_TO_FD(fptr));
|
2005-03-05 16:36:11 -05:00
|
|
|
continue;
|
2003-12-24 02:29:40 -05:00
|
|
|
case SSL_ERROR_WANT_READ:
|
2009-03-21 02:56:33 -04:00
|
|
|
read_would_block(nonblock);
|
2008-12-30 23:52:18 -05:00
|
|
|
rb_io_wait_readable(FPTR_TO_FD(fptr));
|
2003-12-24 02:29:40 -05:00
|
|
|
continue;
|
2004-08-22 03:28:45 -04:00
|
|
|
case SSL_ERROR_SYSCALL:
|
|
|
|
if(ERR_peek_error() == 0 && nread == 0) rb_eof_error();
|
2005-02-15 12:50:47 -05:00
|
|
|
rb_sys_fail(0);
|
2003-12-24 02:29:40 -05:00
|
|
|
default:
|
2011-11-23 18:15:09 -05:00
|
|
|
ossl_raise(eSSLError, "SSL_read");
|
2003-12-24 02:29:40 -05:00
|
|
|
}
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2008-12-30 23:52:18 -05:00
|
|
|
ID meth = nonblock ? rb_intern("read_nonblock") : rb_intern("sysread");
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_warning("SSL session is not started yet.");
|
2008-12-30 23:52:18 -05:00
|
|
|
return rb_funcall(ossl_ssl_get_io(self), meth, 2, len, str);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2003-12-24 02:29:40 -05:00
|
|
|
end:
|
2006-08-31 06:30:33 -04:00
|
|
|
rb_str_set_len(str, nread);
|
2003-07-23 12:12:24 -04:00
|
|
|
OBJ_TAINT(str);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2008-12-30 23:52:18 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.sysread(length) => string
|
|
|
|
* ssl.sysread(length, buffer) => buffer
|
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Reads +length+ bytes from the SSL connection. If a pre-allocated +buffer+
|
|
|
|
* is provided the data will be written into it.
|
2008-12-30 23:52:18 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_read(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
return ossl_ssl_read_internal(argc, argv, self, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.sysread_nonblock(length) => string
|
|
|
|
* ssl.sysread_nonblock(length, buffer) => buffer
|
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* A non-blocking version of #sysread. Raises an SSLError if reading would
|
|
|
|
* block.
|
|
|
|
*
|
|
|
|
* Reads +length+ bytes from the SSL connection. If a pre-allocated +buffer+
|
|
|
|
* is provided the data will be written into it.
|
2008-12-30 23:52:18 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_read_nonblock(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
return ossl_ssl_read_internal(argc, argv, self, 1);
|
|
|
|
}
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
2009-03-20 13:39:44 -04:00
|
|
|
ossl_ssl_write_internal(VALUE self, VALUE str, int nonblock)
|
2003-07-23 12:12:24 -04:00
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
int nwrite = 0;
|
* file.c, gc.c, io.c, ruby.h, rubyio.h, win32/win32.h (rb_io_t):
renamed from OpenFile.
* ext/dl/cptr.c, ext/io/wait/wait.c, ext/openssl/ossl.h,
ext/openssl/ossl_bio.c, ext/openssl/ossl_ssl.c, ext/pty/pty.c,
ext/readline/readline.c, ext/socket/socket.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11869 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-24 05:30:50 -05:00
|
|
|
rb_io_t *fptr;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
StringValue(str);
|
2004-12-15 01:35:55 -05:00
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
2005-03-05 16:36:11 -05:00
|
|
|
GetOpenFile(ossl_ssl_get_io(self), fptr);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
if (ssl) {
|
2003-12-24 02:29:40 -05:00
|
|
|
for (;;){
|
2011-03-24 03:29:21 -04:00
|
|
|
nwrite = SSL_write(ssl, RSTRING_PTR(str), RSTRING_LENINT(str));
|
2005-03-07 02:47:40 -05:00
|
|
|
switch(ssl_get_error(ssl, nwrite)){
|
2003-12-24 02:29:40 -05:00
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
goto end;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2009-03-21 02:56:33 -04:00
|
|
|
write_would_block(nonblock);
|
2007-04-02 15:00:23 -04:00
|
|
|
rb_io_wait_writable(FPTR_TO_FD(fptr));
|
2005-03-05 16:36:11 -05:00
|
|
|
continue;
|
2003-12-24 02:29:40 -05:00
|
|
|
case SSL_ERROR_WANT_READ:
|
2009-03-21 02:56:33 -04:00
|
|
|
read_would_block(nonblock);
|
2007-04-02 15:00:23 -04:00
|
|
|
rb_io_wait_readable(FPTR_TO_FD(fptr));
|
2005-03-05 16:36:11 -05:00
|
|
|
continue;
|
2005-02-15 12:50:47 -05:00
|
|
|
case SSL_ERROR_SYSCALL:
|
2005-03-09 05:45:42 -05:00
|
|
|
if (errno) rb_sys_fail(0);
|
2003-12-24 02:29:40 -05:00
|
|
|
default:
|
2011-11-23 18:15:09 -05:00
|
|
|
ossl_raise(eSSLError, "SSL_write");
|
2003-12-24 02:29:40 -05:00
|
|
|
}
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2004-08-22 03:28:45 -04:00
|
|
|
ID id_syswrite = rb_intern("syswrite");
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_warning("SSL session is not started yet.");
|
2004-08-22 03:28:45 -04:00
|
|
|
return rb_funcall(ossl_ssl_get_io(self), id_syswrite, 1, str);
|
2003-07-23 12:12:24 -04:00
|
|
|
}
|
|
|
|
|
2003-12-24 02:29:40 -05:00
|
|
|
end:
|
2003-07-23 12:12:24 -04:00
|
|
|
return INT2NUM(nwrite);
|
|
|
|
}
|
|
|
|
|
2009-03-20 13:39:44 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-12-05 19:54:44 -05:00
|
|
|
* ssl.syswrite(string) => Integer
|
|
|
|
*
|
|
|
|
* Writes +string+ to the SSL connection.
|
2009-03-20 13:39:44 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_write(VALUE self, VALUE str)
|
|
|
|
{
|
|
|
|
return ossl_ssl_write_internal(self, str, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-12-05 19:54:44 -05:00
|
|
|
* ssl.syswrite_nonblock(string) => Integer
|
|
|
|
*
|
|
|
|
* Writes +string+ to the SSL connection in a non-blocking manner. Raises an
|
|
|
|
* SSLError if writing would block.
|
2009-03-20 13:39:44 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_write_nonblock(VALUE self, VALUE str)
|
|
|
|
{
|
|
|
|
return ossl_ssl_write_internal(self, str, 1);
|
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.sysclose => nil
|
2010-12-05 19:54:44 -05:00
|
|
|
*
|
|
|
|
* Shuts down the SSL connection and prepares it for another connection.
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_ssl_close(VALUE self)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
ossl_ssl_shutdown(ssl);
|
2003-08-18 18:49:48 -04:00
|
|
|
if (RTEST(ossl_ssl_get_sync_close(self)))
|
|
|
|
rb_funcall(ossl_ssl_get_io(self), rb_intern("close"), 0);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.cert => cert or nil
|
2010-12-05 19:54:44 -05:00
|
|
|
*
|
|
|
|
* The X509 certificate for this socket endpoint.
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_ssl_get_cert(VALUE self)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
X509 *cert = NULL;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
2010-11-14 16:46:08 -05:00
|
|
|
if (!ssl) {
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_warning("SSL session is not started yet.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is this OpenSSL bug? Should add a ref?
|
|
|
|
* TODO: Ask for.
|
|
|
|
*/
|
|
|
|
cert = SSL_get_certificate(ssl); /* NO DUPs => DON'T FREE. */
|
|
|
|
|
|
|
|
if (!cert) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
return ossl_x509_new(cert);
|
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.peer_cert => cert or nil
|
2010-12-05 19:54:44 -05:00
|
|
|
*
|
|
|
|
* The X509 certificate for this socket's peer.
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_ssl_get_peer_cert(VALUE self)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
X509 *cert = NULL;
|
|
|
|
VALUE obj;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
|
|
|
|
if (!ssl){
|
|
|
|
rb_warning("SSL session is not started yet.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
cert = SSL_get_peer_certificate(ssl); /* Adds a ref => Safe to FREE. */
|
|
|
|
|
|
|
|
if (!cert) {
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
obj = ossl_x509_new(cert);
|
|
|
|
X509_free(cert);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.peer_cert_chain => [cert, ...] or nil
|
2010-12-05 19:54:44 -05:00
|
|
|
*
|
|
|
|
* The X509 certificate chain for this socket's peer.
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2003-11-01 04:24:55 -05:00
|
|
|
static VALUE
|
|
|
|
ossl_ssl_get_peer_cert_chain(VALUE self)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
STACK_OF(X509) *chain;
|
|
|
|
X509 *cert;
|
|
|
|
VALUE ary;
|
|
|
|
int i, num;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
if(!ssl){
|
|
|
|
rb_warning("SSL session is not started yet.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
chain = SSL_get_peer_cert_chain(ssl);
|
2003-11-04 18:48:13 -05:00
|
|
|
if(!chain) return Qnil;
|
2010-02-27 21:56:26 -05:00
|
|
|
num = sk_X509_num(chain);
|
2003-11-01 04:24:55 -05:00
|
|
|
ary = rb_ary_new2(num);
|
|
|
|
for (i = 0; i < num; i++){
|
2010-02-27 21:56:26 -05:00
|
|
|
cert = sk_X509_value(chain, i);
|
2003-11-01 04:24:55 -05:00
|
|
|
rb_ary_push(ary, ossl_x509_new(cert));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.cipher => [name, version, bits, alg_bits]
|
2010-12-05 19:54:44 -05:00
|
|
|
*
|
|
|
|
* The cipher being used for the current connection
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_ssl_get_cipher(VALUE self)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
SSL_CIPHER *cipher;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
if (!ssl) {
|
|
|
|
rb_warning("SSL session is not started yet.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-05-21 05:10:23 -04:00
|
|
|
cipher = (SSL_CIPHER *)SSL_get_current_cipher(ssl);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
return ossl_ssl_cipher_to_ary(cipher);
|
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.state => string
|
2010-12-05 19:54:44 -05:00
|
|
|
*
|
|
|
|
* A description of the current connection state.
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_ssl_get_state(VALUE self)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
VALUE ret;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
if (!ssl) {
|
|
|
|
rb_warning("SSL session is not started yet.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
ret = rb_str_new2(SSL_state_string(ssl));
|
|
|
|
if (ruby_verbose) {
|
|
|
|
rb_str_cat2(ret, ": ");
|
|
|
|
rb_str_cat2(ret, SSL_state_string_long(ssl));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-12-05 19:54:44 -05:00
|
|
|
* ssl.pending => Integer
|
|
|
|
*
|
|
|
|
* The number of bytes that are immediately available for reading
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2004-08-22 03:28:45 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_ssl_pending(VALUE self)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
if (!ssl) {
|
|
|
|
rb_warning("SSL session is not started yet.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return INT2NUM(SSL_pending(ssl));
|
|
|
|
}
|
|
|
|
|
2007-04-02 18:10:12 -04:00
|
|
|
/*
|
2010-12-05 19:54:44 -05:00
|
|
|
* call-seq:
|
|
|
|
* ssl.session_reused? -> true | false
|
2007-04-02 18:10:12 -04:00
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Returns true if a reused session was negotiated during the handshake.
|
2007-04-02 18:10:12 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_session_reused(VALUE self)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
if (!ssl) {
|
|
|
|
rb_warning("SSL session is not started yet.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(SSL_session_reused(ssl)) {
|
|
|
|
case 1: return Qtrue;
|
|
|
|
case 0: return Qfalse;
|
|
|
|
default: ossl_raise(eSSLError, "SSL_session_reused");
|
|
|
|
}
|
2012-04-13 19:45:37 -04:00
|
|
|
|
|
|
|
UNREACHABLE;
|
2007-04-02 18:10:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-12-05 19:54:44 -05:00
|
|
|
* call-seq:
|
|
|
|
* ssl.session = session -> session
|
2007-04-02 18:10:12 -04:00
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* Sets the Session to be used when the connection is established.
|
2007-04-02 18:10:12 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_set_session(VALUE self, VALUE arg1)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
SSL_SESSION *sess;
|
|
|
|
|
|
|
|
/* why is ossl_ssl_setup delayed? */
|
|
|
|
ossl_ssl_setup(self);
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
if (!ssl) {
|
|
|
|
rb_warning("SSL session is not started yet.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
SafeGetSSLSession(arg1, sess);
|
|
|
|
|
|
|
|
if (SSL_set_session(ssl, sess) != 1)
|
|
|
|
ossl_raise(eSSLError, "SSL_set_session");
|
|
|
|
|
|
|
|
return arg1;
|
|
|
|
}
|
|
|
|
|
2010-12-05 19:54:44 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.verify_result => Integer
|
|
|
|
*
|
|
|
|
* Returns the result of the peer certificates verification. See verify(1)
|
|
|
|
* for error values and descriptions.
|
|
|
|
*
|
|
|
|
* If no peer certificate was presented X509_V_OK is returned.
|
|
|
|
*/
|
2007-12-17 07:18:28 -05:00
|
|
|
static VALUE
|
|
|
|
ossl_ssl_get_verify_result(VALUE self)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
if (!ssl) {
|
|
|
|
rb_warning("SSL session is not started yet.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return INT2FIX(SSL_get_verify_result(ssl));
|
|
|
|
}
|
2007-04-02 18:10:12 -04:00
|
|
|
|
2011-06-30 10:48:52 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* ssl.client_ca => [x509name, ...]
|
|
|
|
*
|
2011-06-30 16:20:32 -04:00
|
|
|
* Returns the list of client CAs. Please note that in contrast to
|
2011-06-30 10:48:52 -04:00
|
|
|
* SSLContext#client_ca= no array of X509::Certificate is returned but
|
|
|
|
* X509::Name instances of the CA's subject distinguished name.
|
|
|
|
*
|
|
|
|
* In server mode, returns the list set by SSLContext#client_ca=.
|
|
|
|
* In client mode, returns the list of client CAs sent from the server.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
ossl_ssl_get_client_ca_list(VALUE self)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
STACK_OF(X509_NAME) *ca;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, SSL, ssl);
|
|
|
|
if (!ssl) {
|
|
|
|
rb_warning("SSL session is not started yet.");
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
ca = SSL_get_client_CA_list(ssl);
|
|
|
|
return ossl_x509name_sk2ary(ca);
|
|
|
|
}
|
|
|
|
|
2003-07-23 12:12:24 -04:00
|
|
|
void
|
|
|
|
Init_ossl_ssl()
|
|
|
|
{
|
|
|
|
int i;
|
2007-04-03 14:29:30 -04:00
|
|
|
VALUE ary;
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2010-12-05 19:54:44 -05:00
|
|
|
#if 0
|
|
|
|
mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
|
2007-03-11 22:01:19 -04:00
|
|
|
#endif
|
|
|
|
|
2007-04-02 18:10:12 -04:00
|
|
|
ID_callback_state = rb_intern("@callback_state");
|
|
|
|
|
2008-07-22 11:34:23 -04:00
|
|
|
ossl_ssl_ex_vcb_idx = SSL_get_ex_new_index(0,(void *)"ossl_ssl_ex_vcb_idx",0,0,0);
|
|
|
|
ossl_ssl_ex_store_p = SSL_get_ex_new_index(0,(void *)"ossl_ssl_ex_store_p",0,0,0);
|
|
|
|
ossl_ssl_ex_ptr_idx = SSL_get_ex_new_index(0,(void *)"ossl_ssl_ex_ptr_idx",0,0,0);
|
2005-03-09 05:45:42 -05:00
|
|
|
ossl_ssl_ex_client_cert_cb_idx =
|
2008-07-22 11:34:23 -04:00
|
|
|
SSL_get_ex_new_index(0,(void *)"ossl_ssl_ex_client_cert_cb_idx",0,0,0);
|
2005-03-09 05:45:42 -05:00
|
|
|
ossl_ssl_ex_tmp_dh_callback_idx =
|
2008-07-22 11:34:23 -04:00
|
|
|
SSL_get_ex_new_index(0,(void *)"ossl_ssl_ex_tmp_dh_callback_idx",0,0,0);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
mSSL = rb_define_module_under(mOSSL, "SSL");
|
|
|
|
eSSLError = rb_define_class_under(mSSL, "SSLError", eOSSLError);
|
|
|
|
|
2008-04-20 18:32:06 -04:00
|
|
|
Init_ossl_ssl_session();
|
|
|
|
|
2010-12-05 19:54:44 -05:00
|
|
|
/* Document-class: OpenSSL::SSL::SSLContext
|
2007-04-03 14:29:30 -04:00
|
|
|
*
|
2010-12-05 19:54:44 -05:00
|
|
|
* An SSLContext is used to set various options regarding certificates,
|
|
|
|
* algorithms, verification, session caching, etc. The SSLContext is
|
|
|
|
* used to create an SSLSocket.
|
|
|
|
*
|
|
|
|
* All attributes must be set before creating an SSLSocket as the
|
|
|
|
* SSLContext will be frozen afterward.
|
|
|
|
*
|
|
|
|
* The following attributes are available but don't show up in rdoc:
|
2007-12-17 07:18:28 -05:00
|
|
|
* * ssl_version, cert, key, client_ca, ca_file, ca_path, timeout,
|
|
|
|
* * verify_mode, verify_depth client_cert_cb, tmp_dh_callback,
|
|
|
|
* * session_id_context, session_add_cb, session_new_cb, session_remove_cb
|
2007-04-03 14:29:30 -04:00
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
cSSLContext = rb_define_class_under(mSSL, "SSLContext", rb_cObject);
|
|
|
|
rb_define_alloc_func(cSSLContext, ossl_sslctx_s_alloc);
|
2010-12-05 19:54:44 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Context certificate
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("cert"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Context private key
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("key"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A certificate or Array of certificates that will be sent to the client.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("client_ca"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The path to a file containing a PEM-format CA certificate
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("ca_file"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The path to a directory containing CA certificates in PEM format.
|
|
|
|
*
|
|
|
|
* Files are looked up by subject's X509 name's hash value.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("ca_path"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Maximum session lifetime.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("timeout"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Session verification mode.
|
|
|
|
*
|
|
|
|
* Valid modes are VERIFY_NONE, VERIFY_PEER, VERIFY_CLIENT_ONCE,
|
|
|
|
* VERIFY_FAIL_IF_NO_PEER_CERT and defined on OpenSSL::SSL
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("verify_mode"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Number of CA certificates to walk when verifying a certificate chain.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("verify_depth"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A callback for additional certificate verification. The callback is
|
|
|
|
* invoked for each certificate in the chain.
|
|
|
|
*
|
|
|
|
* The callback is invoked with two values. +preverify_ok+ indicates
|
|
|
|
* indicates if the verification was passed (true) or not (false).
|
|
|
|
* +store_context+ is an OpenSSL::X509::StoreContext containing the
|
|
|
|
* context used for certificate verification.
|
|
|
|
*
|
|
|
|
* If the callback returns false verification is stopped.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("verify_callback"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets various OpenSSL options.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("options"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An OpenSSL::X509::Store used for certificate verification
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("cert_store"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An Array of extra X509 certificates to be added to the certificate
|
|
|
|
* chain.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("extra_chain_cert"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A callback invoked when a client certificate is requested by a server
|
|
|
|
* and no certificate has been set.
|
|
|
|
*
|
|
|
|
* The callback is invoked with a Session and must return an Array
|
|
|
|
* containing an OpenSSL::X509::Certificate and an OpenSSL::PKey. If any
|
|
|
|
* other value is returned the handshake is suspended.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("client_cert_cb"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A callback invoked when DH parameters are required.
|
|
|
|
*
|
|
|
|
* The callback is invoked with the Session for the key exchange, an
|
|
|
|
* flag indicating the use of an export cipher and the keylength
|
|
|
|
* required.
|
|
|
|
*
|
|
|
|
* The callback must return an OpenSSL::PKey::DH instance of the correct
|
|
|
|
* key length.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("tmp_dh_callback"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets the context in which a session can be reused. This allows
|
|
|
|
* sessions for multiple applications to be distinguished, for exapmle, by
|
|
|
|
* name.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("session_id_context"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A callback invoked on a server when a session is proposed by the client
|
|
|
|
* but the session could not be found in the server's internal cache.
|
|
|
|
*
|
|
|
|
* The callback is invoked with the SSLSocket and session id. The
|
|
|
|
* callback may return a Session from an external cache.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("session_get_cb"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A callback invoked when a new session was negotiatied.
|
|
|
|
*
|
|
|
|
* The callback is invoked with an SSLSocket. If false is returned the
|
|
|
|
* session will be removed from the internal cache.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("session_new_cb"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A callback invoked when a session is removed from the internal cache.
|
|
|
|
*
|
|
|
|
* The callback is invoked with an SSLContext and a Session.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("session_remove_cb"), 1, 1, Qfalse);
|
|
|
|
|
|
|
|
#ifdef HAVE_SSL_SET_TLSEXT_HOST_NAME
|
|
|
|
/*
|
|
|
|
* A callback invoked at connect time to distinguish between multiple
|
|
|
|
* server names.
|
|
|
|
*
|
|
|
|
* The callback is invoked with an SSLSocket and a server name. The
|
|
|
|
* callback must return an SSLContext for the server name or nil.
|
|
|
|
*/
|
|
|
|
rb_attr(cSSLContext, rb_intern("servername_cb"), 1, 1, Qfalse);
|
|
|
|
#endif
|
|
|
|
|
2007-12-17 07:18:28 -05:00
|
|
|
rb_define_alias(cSSLContext, "ssl_timeout", "timeout");
|
2010-04-10 00:16:19 -04:00
|
|
|
rb_define_alias(cSSLContext, "ssl_timeout=", "timeout=");
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cSSLContext, "initialize", ossl_sslctx_initialize, -1);
|
2007-12-17 07:18:28 -05:00
|
|
|
rb_define_method(cSSLContext, "ssl_version=", ossl_sslctx_set_ssl_version, 1);
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cSSLContext, "ciphers", ossl_sslctx_get_ciphers, 0);
|
|
|
|
rb_define_method(cSSLContext, "ciphers=", ossl_sslctx_set_ciphers, 1);
|
|
|
|
|
2008-04-20 18:32:06 -04:00
|
|
|
rb_define_method(cSSLContext, "setup", ossl_sslctx_setup, 0);
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
|
2010-12-05 19:54:44 -05:00
|
|
|
/*
|
|
|
|
* No session caching for client or server
|
|
|
|
*/
|
2007-04-02 18:10:12 -04:00
|
|
|
rb_define_const(cSSLContext, "SESSION_CACHE_OFF", LONG2FIX(SSL_SESS_CACHE_OFF));
|
2010-12-05 19:54:44 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Client sessions are added to the session cache
|
|
|
|
*/
|
2007-04-02 18:10:12 -04:00
|
|
|
rb_define_const(cSSLContext, "SESSION_CACHE_CLIENT", LONG2FIX(SSL_SESS_CACHE_CLIENT)); /* doesn't actually do anything in 0.9.8e */
|
2010-12-05 19:54:44 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Server sessions are added to the session cache
|
|
|
|
*/
|
2007-04-02 18:10:12 -04:00
|
|
|
rb_define_const(cSSLContext, "SESSION_CACHE_SERVER", LONG2FIX(SSL_SESS_CACHE_SERVER));
|
2010-12-05 19:54:44 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Both client and server sessions are added to the session cache
|
|
|
|
*/
|
2007-04-02 18:10:12 -04:00
|
|
|
rb_define_const(cSSLContext, "SESSION_CACHE_BOTH", LONG2FIX(SSL_SESS_CACHE_BOTH)); /* no different than CACHE_SERVER in 0.9.8e */
|
2010-12-05 19:54:44 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Normally the sesison cache is checked for expired sessions every 255
|
|
|
|
* connections. Since this may lead to a delay that cannot be controlled,
|
|
|
|
* the automatic flushing may be disabled and #flush_sessions can be
|
|
|
|
* called explicitly.
|
|
|
|
*/
|
2007-04-02 18:10:12 -04:00
|
|
|
rb_define_const(cSSLContext, "SESSION_CACHE_NO_AUTO_CLEAR", LONG2FIX(SSL_SESS_CACHE_NO_AUTO_CLEAR));
|
2010-12-05 19:54:44 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Always perform external lookups of sessions even if they are in the
|
|
|
|
* internal cache.
|
|
|
|
*
|
|
|
|
* This flag has no effect on clients
|
|
|
|
*/
|
2007-04-02 18:10:12 -04:00
|
|
|
rb_define_const(cSSLContext, "SESSION_CACHE_NO_INTERNAL_LOOKUP", LONG2FIX(SSL_SESS_CACHE_NO_INTERNAL_LOOKUP));
|
2010-12-05 19:54:44 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Never automatically store sessions in the internal store.
|
|
|
|
*/
|
2007-04-02 18:10:12 -04:00
|
|
|
rb_define_const(cSSLContext, "SESSION_CACHE_NO_INTERNAL_STORE", LONG2FIX(SSL_SESS_CACHE_NO_INTERNAL_STORE));
|
2010-12-05 19:54:44 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Enables both SESSION_CACHE_NO_INTERNAL_LOOKUP and
|
|
|
|
* SESSION_CACHE_NO_INTERNAL_STORE.
|
|
|
|
*/
|
2007-04-02 18:10:12 -04:00
|
|
|
rb_define_const(cSSLContext, "SESSION_CACHE_NO_INTERNAL", LONG2FIX(SSL_SESS_CACHE_NO_INTERNAL));
|
2010-12-05 19:54:44 -05:00
|
|
|
|
2007-04-02 18:10:12 -04:00
|
|
|
rb_define_method(cSSLContext, "session_add", ossl_sslctx_session_add, 1);
|
|
|
|
rb_define_method(cSSLContext, "session_remove", ossl_sslctx_session_remove, 1);
|
|
|
|
rb_define_method(cSSLContext, "session_cache_mode", ossl_sslctx_get_session_cache_mode, 0);
|
|
|
|
rb_define_method(cSSLContext, "session_cache_mode=", ossl_sslctx_set_session_cache_mode, 1);
|
|
|
|
rb_define_method(cSSLContext, "session_cache_size", ossl_sslctx_get_session_cache_size, 0);
|
|
|
|
rb_define_method(cSSLContext, "session_cache_size=", ossl_sslctx_set_session_cache_size, 1);
|
|
|
|
rb_define_method(cSSLContext, "session_cache_stats", ossl_sslctx_get_session_cache_stats, 0);
|
|
|
|
rb_define_method(cSSLContext, "flush_sessions", ossl_sslctx_flush_sessions, -1);
|
|
|
|
|
2007-04-03 14:29:30 -04:00
|
|
|
ary = rb_ary_new2(numberof(ossl_ssl_method_tab));
|
|
|
|
for (i = 0; i < numberof(ossl_ssl_method_tab); i++) {
|
|
|
|
rb_ary_push(ary, ID2SYM(rb_intern(ossl_ssl_method_tab[i].name)));
|
|
|
|
}
|
|
|
|
rb_obj_freeze(ary);
|
2010-12-05 19:54:44 -05:00
|
|
|
/* The list of available SSL/TLS methods */
|
2007-04-03 14:29:30 -04:00
|
|
|
rb_define_const(cSSLContext, "METHODS", ary);
|
|
|
|
|
2010-12-05 19:54:44 -05:00
|
|
|
/*
|
|
|
|
* Document-class: OpenSSL::SSL::SSLSocket
|
2007-04-03 14:29:30 -04:00
|
|
|
*
|
|
|
|
* The following attributes are available but don't show up in rdoc.
|
|
|
|
* * io, context, sync_close
|
|
|
|
*
|
|
|
|
*/
|
2003-07-23 12:12:24 -04:00
|
|
|
cSSLSocket = rb_define_class_under(mSSL, "SSLSocket", rb_cObject);
|
|
|
|
rb_define_alloc_func(cSSLSocket, ossl_ssl_s_alloc);
|
2003-08-18 18:49:48 -04:00
|
|
|
for(i = 0; i < numberof(ossl_ssl_attr_readers); i++)
|
|
|
|
rb_attr(cSSLSocket, rb_intern(ossl_ssl_attr_readers[i]), 1, 0, Qfalse);
|
2003-07-23 12:12:24 -04:00
|
|
|
for(i = 0; i < numberof(ossl_ssl_attrs); i++)
|
2003-08-18 18:49:48 -04:00
|
|
|
rb_attr(cSSLSocket, rb_intern(ossl_ssl_attrs[i]), 1, 1, Qfalse);
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_alias(cSSLSocket, "to_io", "io");
|
|
|
|
rb_define_method(cSSLSocket, "initialize", ossl_ssl_initialize, -1);
|
|
|
|
rb_define_method(cSSLSocket, "connect", ossl_ssl_connect, 0);
|
2009-03-21 02:56:33 -04:00
|
|
|
rb_define_method(cSSLSocket, "connect_nonblock", ossl_ssl_connect_nonblock, 0);
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cSSLSocket, "accept", ossl_ssl_accept, 0);
|
2009-03-21 02:56:33 -04:00
|
|
|
rb_define_method(cSSLSocket, "accept_nonblock", ossl_ssl_accept_nonblock, 0);
|
2004-05-26 14:11:29 -04:00
|
|
|
rb_define_method(cSSLSocket, "sysread", ossl_ssl_read, -1);
|
2008-12-30 23:52:18 -05:00
|
|
|
rb_define_private_method(cSSLSocket, "sysread_nonblock", ossl_ssl_read_nonblock, -1);
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cSSLSocket, "syswrite", ossl_ssl_write, 1);
|
2009-03-20 13:39:44 -04:00
|
|
|
rb_define_private_method(cSSLSocket, "syswrite_nonblock", ossl_ssl_write_nonblock, 1);
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cSSLSocket, "sysclose", ossl_ssl_close, 0);
|
|
|
|
rb_define_method(cSSLSocket, "cert", ossl_ssl_get_cert, 0);
|
|
|
|
rb_define_method(cSSLSocket, "peer_cert", ossl_ssl_get_peer_cert, 0);
|
2003-11-01 04:24:55 -05:00
|
|
|
rb_define_method(cSSLSocket, "peer_cert_chain", ossl_ssl_get_peer_cert_chain, 0);
|
2003-07-23 12:12:24 -04:00
|
|
|
rb_define_method(cSSLSocket, "cipher", ossl_ssl_get_cipher, 0);
|
|
|
|
rb_define_method(cSSLSocket, "state", ossl_ssl_get_state, 0);
|
2004-08-22 03:28:45 -04:00
|
|
|
rb_define_method(cSSLSocket, "pending", ossl_ssl_pending, 0);
|
2007-04-02 18:10:12 -04:00
|
|
|
rb_define_method(cSSLSocket, "session_reused?", ossl_ssl_session_reused, 0);
|
2011-11-25 20:25:07 -05:00
|
|
|
/* implementation of OpenSSL::SSL::SSLSocket#session is in lib/openssl/ssl.rb */
|
2007-04-02 18:10:12 -04:00
|
|
|
rb_define_method(cSSLSocket, "session=", ossl_ssl_set_session, 1);
|
2007-12-17 07:18:28 -05:00
|
|
|
rb_define_method(cSSLSocket, "verify_result", ossl_ssl_get_verify_result, 0);
|
2011-06-30 10:48:52 -04:00
|
|
|
rb_define_method(cSSLSocket, "client_ca", ossl_ssl_get_client_ca_list, 0);
|
2003-07-23 12:12:24 -04:00
|
|
|
|
2009-03-12 09:16:19 -04:00
|
|
|
#define ossl_ssl_def_const(x) rb_define_const(mSSL, #x, INT2NUM(SSL_##x))
|
2003-07-23 12:12:24 -04:00
|
|
|
|
|
|
|
ossl_ssl_def_const(VERIFY_NONE);
|
|
|
|
ossl_ssl_def_const(VERIFY_PEER);
|
|
|
|
ossl_ssl_def_const(VERIFY_FAIL_IF_NO_PEER_CERT);
|
|
|
|
ossl_ssl_def_const(VERIFY_CLIENT_ONCE);
|
2012-02-08 00:27:14 -05:00
|
|
|
/* Introduce constants included in OP_ALL. These constants are mostly for
|
|
|
|
* unset some bits in OP_ALL such as;
|
|
|
|
* ctx.options = OP_ALL & ~OP_DONT_INSERT_EMPTY_FRAGMENTS
|
2003-07-23 12:12:24 -04:00
|
|
|
*/
|
2012-02-08 00:27:14 -05:00
|
|
|
ossl_ssl_def_const(OP_MICROSOFT_SESS_ID_BUG);
|
|
|
|
ossl_ssl_def_const(OP_NETSCAPE_CHALLENGE_BUG);
|
|
|
|
ossl_ssl_def_const(OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG);
|
|
|
|
ossl_ssl_def_const(OP_SSLREF2_REUSE_CERT_TYPE_BUG);
|
|
|
|
ossl_ssl_def_const(OP_MICROSOFT_BIG_SSLV3_BUFFER);
|
|
|
|
ossl_ssl_def_const(OP_MSIE_SSLV2_RSA_PADDING);
|
|
|
|
ossl_ssl_def_const(OP_SSLEAY_080_CLIENT_DH_BUG);
|
|
|
|
ossl_ssl_def_const(OP_TLS_D5_BUG);
|
|
|
|
ossl_ssl_def_const(OP_TLS_BLOCK_PADDING_BUG);
|
|
|
|
ossl_ssl_def_const(OP_DONT_INSERT_EMPTY_FRAGMENTS);
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_ssl_def_const(OP_ALL);
|
|
|
|
#if defined(SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)
|
|
|
|
ossl_ssl_def_const(OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
|
|
|
|
#endif
|
|
|
|
#if defined(SSL_OP_SINGLE_ECDH_USE)
|
|
|
|
ossl_ssl_def_const(OP_SINGLE_ECDH_USE);
|
|
|
|
#endif
|
|
|
|
ossl_ssl_def_const(OP_SINGLE_DH_USE);
|
|
|
|
ossl_ssl_def_const(OP_EPHEMERAL_RSA);
|
|
|
|
#if defined(SSL_OP_CIPHER_SERVER_PREFERENCE)
|
|
|
|
ossl_ssl_def_const(OP_CIPHER_SERVER_PREFERENCE);
|
|
|
|
#endif
|
|
|
|
ossl_ssl_def_const(OP_TLS_ROLLBACK_BUG);
|
|
|
|
ossl_ssl_def_const(OP_NO_SSLv2);
|
|
|
|
ossl_ssl_def_const(OP_NO_SSLv3);
|
|
|
|
ossl_ssl_def_const(OP_NO_TLSv1);
|
2008-06-05 07:36:55 -04:00
|
|
|
#if defined(SSL_OP_NO_TICKET)
|
|
|
|
ossl_ssl_def_const(OP_NO_TICKET);
|
2011-08-14 21:10:26 -04:00
|
|
|
#endif
|
|
|
|
#if defined(SSL_OP_NO_COMPRESSION)
|
|
|
|
ossl_ssl_def_const(OP_NO_COMPRESSION);
|
2008-06-05 07:36:55 -04:00
|
|
|
#endif
|
2003-07-23 12:12:24 -04:00
|
|
|
ossl_ssl_def_const(OP_PKCS1_CHECK_1);
|
|
|
|
ossl_ssl_def_const(OP_PKCS1_CHECK_2);
|
|
|
|
ossl_ssl_def_const(OP_NETSCAPE_CA_DN_BUG);
|
|
|
|
ossl_ssl_def_const(OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG);
|
|
|
|
}
|