From d6f7df8db336a3cddfb44d8f7e4e9455bfdb56de Mon Sep 17 00:00:00 2001 From: gotoyuzo Date: Sat, 10 Sep 2005 01:19:38 +0000 Subject: * ext/openssl/ossl_engine.c (ossl_engine_s_by_id): OpenSSL::Engine.by_id calls given block before calling ENGINE_init (block parameter is the return value of this method itself). this functionality is useful to load dynamic shared engines. the following code is a sample of loading a key using OpenSC PKCS #11 module. require "openssl" pkcs11 = OpenSSL::Engine.by_id("dynamic"){|e| e.ctrl_cmd("SO_PATH", "/usr/lib/opensc/engine_pkcs11.so") e.ctrl_cmd("LIST_ADD", "1") e.ctrl_cmd("LOAD") } pkcs11.ctrl_cmd("PIN", "secret") key = pkcs11.load_private_key * ext/openssl/ossl_engine.c (ossl_engine_ctrl_cmd): new method OpenSSL::Engine#ctrl_cmd. it wraps ENGINE_ctrl_cmd_string. * ext/openssl/ossl_engine.c (ossl_engine_get_cmds): new method OpenSSL::Engine#cmds. it returms engine command definitions. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@9116 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 24 +++++++++++++++++++ ext/openssl/ossl_engine.c | 61 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index cacdfcf5dd..993afcf558 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,27 @@ +Sat Sep 10 10:17:03 2005 GOTOU Yuuzou + + * ext/openssl/ossl_engine.c (ossl_engine_s_by_id): + OpenSSL::Engine.by_id calls given block before calling + ENGINE_init (block parameter is the return value of this method + itself). this functionality is useful to load dynamic shared + engines. the following code is a sample of loading a key using + OpenSC PKCS #11 module. + + require "openssl" + pkcs11 = OpenSSL::Engine.by_id("dynamic"){|e| + e.ctrl_cmd("SO_PATH", "/usr/lib/opensc/engine_pkcs11.so") + e.ctrl_cmd("LIST_ADD", "1") + e.ctrl_cmd("LOAD") + } + pkcs11.ctrl_cmd("PIN", "secret") + key = pkcs11.load_private_key + + * ext/openssl/ossl_engine.c (ossl_engine_ctrl_cmd): new method + OpenSSL::Engine#ctrl_cmd. it wraps ENGINE_ctrl_cmd_string. + + * ext/openssl/ossl_engine.c (ossl_engine_get_cmds): new method + OpenSSL::Engine#cmds. it returms engine command definitions. + Sat Sep 10 10:09:47 2005 GOTOU Yuuzou * ext/openssl/ossl_asn1.c (asn1str_to_str): new function. diff --git a/ext/openssl/ossl_engine.c b/ext/openssl/ossl_engine.c index d2633118c9..3d943b0098 100644 --- a/ext/openssl/ossl_engine.c +++ b/ext/openssl/ossl_engine.c @@ -114,12 +114,13 @@ ossl_engine_s_by_id(VALUE klass, VALUE id) ossl_engine_s_load(1, &id, klass); if(!(e = ENGINE_by_id(RSTRING(id)->ptr))) ossl_raise(eEngineError, NULL); + WrapEngine(klass, obj, e); + if(rb_block_given_p()) rb_yield(obj); if(!ENGINE_init(e)) ossl_raise(eEngineError, NULL); ENGINE_ctrl(e, ENGINE_CTRL_SET_PASSWORD_CALLBACK, 0, NULL, (void(*)())ossl_pem_passwd_cb); ERR_clear_error(); - WrapEngine(klass, obj, e); return obj; } @@ -219,8 +220,8 @@ ossl_engine_load_privkey(int argc, VALUE *argv, VALUE self) VALUE id, data; char *sid, *sdata; - rb_scan_args(argc, argv, "11", &id, &data); - sid = StringValuePtr(id); + rb_scan_args(argc, argv, "02", &id, &data); + sid = NIL_P(id) ? NULL : StringValuePtr(id); sdata = NIL_P(data) ? NULL : StringValuePtr(data); GetEngine(self, e); #if OPENSSL_VERSION_NUMBER < 0x00907000L @@ -267,6 +268,58 @@ ossl_engine_set_default(VALUE self, VALUE flag) return Qtrue; } +static VALUE +ossl_engine_ctrl_cmd(int argc, VALUE *argv, VALUE self) +{ + ENGINE *e; + VALUE cmd, val; + int ret; + + GetEngine(self, e); + rb_scan_args(argc, argv, "11", &cmd, &val); + StringValue(cmd); + if (!NIL_P(val)) StringValue(val); + ret = ENGINE_ctrl_cmd_string(e, RSTRING(cmd)->ptr, + NIL_P(val) ? NULL : RSTRING(val)->ptr, 0); + if (!ret) ossl_raise(eEngineError, NULL); + + return self; +} + +static VALUE +ossl_engine_cmd_flag_to_name(int flag) +{ + switch(flag){ + case ENGINE_CMD_FLAG_NUMERIC: return rb_str_new2("NUMERIC"); + case ENGINE_CMD_FLAG_STRING: return rb_str_new2("STRING"); + case ENGINE_CMD_FLAG_NO_INPUT: return rb_str_new2("NO_INPUT"); + case ENGINE_CMD_FLAG_INTERNAL: return rb_str_new2("INTERNAL"); + default: return rb_str_new2("UNKNOWN"); + } +} + +static VALUE +ossl_engine_get_cmds(VALUE self) +{ + ENGINE *e; + const ENGINE_CMD_DEFN *defn, *p; + VALUE ary, tmp; + + GetEngine(self, e); + ary = rb_ary_new(); + if ((defn = ENGINE_get_cmd_defns(e)) != NULL){ + for (p = defn; p->cmd_num > 0; p++){ + tmp = rb_ary_new(); + rb_ary_push(tmp, rb_str_new2(p->cmd_name)); + rb_ary_push(tmp, rb_str_new2(p->cmd_desc)); + rb_ary_push(tmp, ossl_engine_cmd_flag_to_name(p->cmd_flags)); + rb_ary_push(ary, tmp); + } + } + + return ary; +} + static VALUE ossl_engine_inspect(VALUE self) { @@ -307,6 +360,8 @@ Init_ossl_engine() rb_define_method(cEngine, "load_private_key", ossl_engine_load_privkey, -1); rb_define_method(cEngine, "load_public_key", ossl_engine_load_pubkey, -1); rb_define_method(cEngine, "set_default", ossl_engine_set_default, 1); + rb_define_method(cEngine, "ctrl_cmd", ossl_engine_ctrl_cmd, -1); + rb_define_method(cEngine, "cmds", ossl_engine_get_cmds, 0); rb_define_method(cEngine, "inspect", ossl_engine_inspect, 0); DefEngineConst(METHOD_RSA); -- cgit v1.2.3