2003-10-02 04:47:11 -04:00
|
|
|
/*
|
|
|
|
* 'OpenSSL for Ruby' project
|
|
|
|
* Copyright (C) 2003 GOTOU Yuuzou <gotoyuzo@notwork.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
/*
|
2015-04-19 23:55:09 -04:00
|
|
|
* This program is licensed under the same licence as Ruby.
|
2003-10-02 04:47:11 -04:00
|
|
|
* (See the file 'LICENCE'.)
|
|
|
|
*/
|
|
|
|
#include "ossl.h"
|
|
|
|
|
2016-05-25 04:50:03 -04:00
|
|
|
#if !defined(OPENSSL_NO_ENGINE)
|
2003-10-02 04:47:11 -04:00
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
#define NewEngine(klass) \
|
|
|
|
TypedData_Wrap_Struct((klass), &ossl_engine_type, 0)
|
|
|
|
#define SetEngine(obj, engine) do { \
|
2011-02-23 08:05:32 -05:00
|
|
|
if (!(engine)) { \
|
2003-10-02 04:47:11 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "ENGINE wasn't initialized."); \
|
|
|
|
} \
|
2015-05-29 01:55:02 -04:00
|
|
|
RTYPEDDATA_DATA(obj) = (engine); \
|
2003-10-02 04:47:11 -04:00
|
|
|
} while(0)
|
|
|
|
#define GetEngine(obj, engine) do { \
|
2014-12-12 16:57:49 -05:00
|
|
|
TypedData_Get_Struct((obj), ENGINE, &ossl_engine_type, (engine)); \
|
2011-02-23 08:05:32 -05:00
|
|
|
if (!(engine)) { \
|
2003-10-02 04:47:11 -04:00
|
|
|
ossl_raise(rb_eRuntimeError, "ENGINE wasn't initialized."); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
#define SafeGetEngine(obj, engine) do { \
|
2011-02-23 08:05:32 -05:00
|
|
|
OSSL_Check_Kind((obj), cEngine); \
|
|
|
|
GetPKCS7((obj), (engine)); \
|
2003-10-02 04:47:11 -04:00
|
|
|
} while (0)
|
|
|
|
|
2010-04-22 04:04:13 -04:00
|
|
|
/*
|
2003-10-02 04:47:11 -04:00
|
|
|
* Classes
|
|
|
|
*/
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-class: OpenSSL::Engine
|
|
|
|
*
|
|
|
|
* This class is the access to openssl's ENGINE cryptographic module
|
|
|
|
* implementation.
|
|
|
|
*
|
|
|
|
* See also, https://www.openssl.org/docs/crypto/engine.html
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
VALUE cEngine;
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-class: OpenSSL::Engine::EngineError
|
|
|
|
*
|
|
|
|
* This is the generic exception for OpenSSL::Engine related errors
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
VALUE eEngineError;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private
|
|
|
|
*/
|
|
|
|
#define OSSL_ENGINE_LOAD_IF_MATCH(x) \
|
|
|
|
do{\
|
2006-08-31 06:30:33 -04:00
|
|
|
if(!strcmp(#x, RSTRING_PTR(name))){\
|
2003-10-02 04:47:11 -04:00
|
|
|
ENGINE_load_##x();\
|
|
|
|
return Qtrue;\
|
|
|
|
}\
|
|
|
|
}while(0)
|
|
|
|
|
2014-12-12 16:57:49 -05:00
|
|
|
static void
|
|
|
|
ossl_engine_free(void *engine)
|
|
|
|
{
|
|
|
|
ENGINE_free(engine);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const rb_data_type_t ossl_engine_type = {
|
|
|
|
"OpenSSL/Engine",
|
|
|
|
{
|
|
|
|
0, ossl_engine_free,
|
|
|
|
},
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
|
|
|
|
};
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine.load
|
|
|
|
*
|
|
|
|
* call-seq:
|
|
|
|
* load(enginename = nil)
|
|
|
|
*
|
|
|
|
* This method loads engines. If +name+ is nil, then all builtin engines are
|
|
|
|
* loaded. Otherwise, the given +name+, as a string, is loaded if available to
|
|
|
|
* your runtime, and returns true. If +name+ is not found, then nil is
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_s_load(int argc, VALUE *argv, VALUE klass)
|
|
|
|
{
|
2003-10-05 09:58:02 -04:00
|
|
|
#if !defined(HAVE_ENGINE_LOAD_BUILTIN_ENGINES)
|
|
|
|
return Qnil;
|
|
|
|
#else
|
2003-10-02 04:47:11 -04:00
|
|
|
VALUE name;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "01", &name);
|
2005-03-31 00:09:24 -05:00
|
|
|
if(NIL_P(name)){
|
|
|
|
ENGINE_load_builtin_engines();
|
|
|
|
return Qtrue;
|
|
|
|
}
|
2016-05-23 07:40:07 -04:00
|
|
|
StringValueCStr(name);
|
2005-07-23 03:50:17 -04:00
|
|
|
#ifndef OPENSSL_NO_STATIC_ENGINE
|
2010-04-23 04:37:55 -04:00
|
|
|
#if HAVE_ENGINE_LOAD_DYNAMIC
|
2003-10-02 04:47:11 -04:00
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(dynamic);
|
2010-04-23 04:37:55 -04:00
|
|
|
#endif
|
2011-11-03 19:45:23 -04:00
|
|
|
#if HAVE_ENGINE_LOAD_4758CCA
|
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(4758cca);
|
2010-04-23 04:37:55 -04:00
|
|
|
#endif
|
2011-11-03 19:45:23 -04:00
|
|
|
#if HAVE_ENGINE_LOAD_AEP
|
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(aep);
|
2010-04-23 04:37:55 -04:00
|
|
|
#endif
|
|
|
|
#if HAVE_ENGINE_LOAD_ATALLA
|
2003-10-02 04:47:11 -04:00
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(atalla);
|
2010-04-23 04:37:55 -04:00
|
|
|
#endif
|
2011-11-03 19:45:23 -04:00
|
|
|
#if HAVE_ENGINE_LOAD_CHIL
|
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(chil);
|
|
|
|
#endif
|
|
|
|
#if HAVE_ENGINE_LOAD_CSWIFT
|
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(cswift);
|
|
|
|
#endif
|
2010-04-23 04:37:55 -04:00
|
|
|
#if HAVE_ENGINE_LOAD_NURON
|
2003-10-02 04:47:11 -04:00
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(nuron);
|
2010-04-23 04:37:55 -04:00
|
|
|
#endif
|
2011-11-04 03:19:23 -04:00
|
|
|
#if HAVE_ENGINE_LOAD_SUREWARE
|
2011-11-03 19:45:23 -04:00
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(sureware);
|
|
|
|
#endif
|
2011-11-04 03:19:23 -04:00
|
|
|
#if HAVE_ENGINE_LOAD_UBSEC
|
2003-10-02 04:47:11 -04:00
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(ubsec);
|
2010-04-23 04:37:55 -04:00
|
|
|
#endif
|
2011-11-03 19:45:23 -04:00
|
|
|
#if HAVE_ENGINE_LOAD_PADLOCK
|
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(padlock);
|
2010-04-23 04:37:55 -04:00
|
|
|
#endif
|
2011-11-03 19:45:23 -04:00
|
|
|
#if HAVE_ENGINE_LOAD_CAPI
|
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(capi);
|
2010-04-23 04:37:55 -04:00
|
|
|
#endif
|
2011-11-03 19:45:23 -04:00
|
|
|
#if HAVE_ENGINE_LOAD_GMP
|
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(gmp);
|
|
|
|
#endif
|
|
|
|
#if HAVE_ENGINE_LOAD_GOST
|
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(gost);
|
|
|
|
#endif
|
|
|
|
#if HAVE_ENGINE_LOAD_CRYPTODEV
|
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(cryptodev);
|
|
|
|
#endif
|
|
|
|
#if HAVE_ENGINE_LOAD_AESNI
|
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(aesni);
|
2005-07-23 03:50:17 -04:00
|
|
|
#endif
|
2010-04-23 04:37:55 -04:00
|
|
|
#endif
|
2003-10-02 04:47:11 -04:00
|
|
|
#ifdef HAVE_ENGINE_LOAD_OPENBSD_DEV_CRYPTO
|
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(openbsd_dev_crypto);
|
|
|
|
#endif
|
2005-07-23 03:50:17 -04:00
|
|
|
OSSL_ENGINE_LOAD_IF_MATCH(openssl);
|
2016-05-23 07:40:07 -04:00
|
|
|
rb_warning("no such builtin loader for `%"PRIsVALUE"'", name);
|
2005-03-31 00:09:24 -05:00
|
|
|
return Qnil;
|
2003-10-05 09:58:02 -04:00
|
|
|
#endif /* HAVE_ENGINE_LOAD_BUILTIN_ENGINES */
|
2003-10-02 04:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine.cleanup
|
|
|
|
* call-seq:
|
|
|
|
* OpenSSL::Engine.cleanup
|
|
|
|
*
|
|
|
|
* It is only necessary to run cleanup when engines are loaded via
|
|
|
|
* OpenSSL::Engine.load. However, running cleanup before exit is recommended.
|
|
|
|
*
|
2016-06-07 08:20:46 -04:00
|
|
|
* Note that this is needed and works only in OpenSSL < 1.1.0.
|
2013-11-20 23:37:14 -05:00
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_s_cleanup(VALUE self)
|
|
|
|
{
|
|
|
|
ENGINE_cleanup();
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine.engines
|
|
|
|
*
|
|
|
|
* Returns an array of currently loaded engines.
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_s_engines(VALUE klass)
|
|
|
|
{
|
|
|
|
ENGINE *e;
|
|
|
|
VALUE ary, obj;
|
|
|
|
|
|
|
|
ary = rb_ary_new();
|
|
|
|
for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)){
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewEngine(klass);
|
2011-07-21 20:13:07 -04:00
|
|
|
/* Need a ref count of two here because of ENGINE_free being
|
|
|
|
* called internally by OpenSSL when moving to the next ENGINE
|
|
|
|
* and by us when releasing the ENGINE reference */
|
2011-11-04 03:19:23 -04:00
|
|
|
ENGINE_up_ref(e);
|
2015-05-29 01:55:02 -04:00
|
|
|
SetEngine(obj, e);
|
2003-10-02 04:47:11 -04:00
|
|
|
rb_ary_push(ary, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine.by_id
|
|
|
|
*
|
|
|
|
* call-seq:
|
|
|
|
* by_id(name) -> engine
|
|
|
|
*
|
|
|
|
* Fetch the engine as specified by the +id+ String
|
|
|
|
*
|
|
|
|
* OpenSSL::Engine.by_id("openssl")
|
|
|
|
* => #<OpenSSL::Engine id="openssl" name="Software engine support">
|
|
|
|
*
|
|
|
|
* See OpenSSL::Engine.engines for the currently loaded engines
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_s_by_id(VALUE klass, VALUE id)
|
|
|
|
{
|
|
|
|
ENGINE *e;
|
|
|
|
VALUE obj;
|
|
|
|
|
2016-05-23 07:40:07 -04:00
|
|
|
StringValueCStr(id);
|
2003-10-02 04:47:11 -04:00
|
|
|
ossl_engine_s_load(1, &id, klass);
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewEngine(klass);
|
2006-08-31 06:30:33 -04:00
|
|
|
if(!(e = ENGINE_by_id(RSTRING_PTR(id))))
|
2003-10-02 04:47:11 -04:00
|
|
|
ossl_raise(eEngineError, NULL);
|
2015-05-29 01:55:02 -04:00
|
|
|
SetEngine(obj, e);
|
2005-09-07 04:29:47 -04:00
|
|
|
if(rb_block_given_p()) rb_yield(obj);
|
2003-10-02 04:47:11 -04:00
|
|
|
if(!ENGINE_init(e))
|
|
|
|
ossl_raise(eEngineError, NULL);
|
|
|
|
ENGINE_ctrl(e, ENGINE_CTRL_SET_PASSWORD_CALLBACK,
|
2009-03-13 03:45:35 -04:00
|
|
|
0, NULL, (void(*)(void))ossl_pem_passwd_cb);
|
2016-05-18 00:07:45 -04:00
|
|
|
ossl_clear_error();
|
2003-10-02 04:47:11 -04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ossl_engine_s_alloc(VALUE klass)
|
|
|
|
{
|
|
|
|
ENGINE *e;
|
|
|
|
VALUE obj;
|
|
|
|
|
2015-05-29 01:55:02 -04:00
|
|
|
obj = NewEngine(klass);
|
2003-10-02 04:47:11 -04:00
|
|
|
if (!(e = ENGINE_new())) {
|
|
|
|
ossl_raise(eEngineError, NULL);
|
|
|
|
}
|
2015-05-29 01:55:02 -04:00
|
|
|
SetEngine(obj, e);
|
2003-10-02 04:47:11 -04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine#id
|
|
|
|
*
|
|
|
|
* Get the id for this engine
|
|
|
|
*
|
|
|
|
* OpenSSL::Engine.load
|
|
|
|
* OpenSSL::Engine.engines #=> [#<OpenSSL::Engine#>, ...]
|
|
|
|
* OpenSSL::Engine.engines.first.id
|
|
|
|
* #=> "rsax"
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_get_id(VALUE self)
|
|
|
|
{
|
|
|
|
ENGINE *e;
|
|
|
|
GetEngine(self, e);
|
|
|
|
return rb_str_new2(ENGINE_get_id(e));
|
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine#name
|
|
|
|
*
|
|
|
|
* Get the descriptive name for this engine
|
|
|
|
*
|
|
|
|
* OpenSSL::Engine.load
|
|
|
|
* OpenSSL::Engine.engines #=> [#<OpenSSL::Engine#>, ...]
|
|
|
|
* OpenSSL::Engine.engines.first.name
|
|
|
|
* #=> "RSAX engine support"
|
|
|
|
*
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_get_name(VALUE self)
|
|
|
|
{
|
|
|
|
ENGINE *e;
|
|
|
|
GetEngine(self, e);
|
|
|
|
return rb_str_new2(ENGINE_get_name(e));
|
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine#finish
|
|
|
|
*
|
|
|
|
* Releases all internal structural references for this engine.
|
|
|
|
*
|
|
|
|
* May raise an EngineError if the engine is unavailable
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_finish(VALUE self)
|
|
|
|
{
|
|
|
|
ENGINE *e;
|
|
|
|
|
|
|
|
GetEngine(self, e);
|
|
|
|
if(!ENGINE_finish(e)) ossl_raise(eEngineError, NULL);
|
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine#cipher
|
|
|
|
*
|
|
|
|
* call-seq:
|
|
|
|
* engine.cipher(name) -> OpenSSL::Cipher
|
|
|
|
*
|
|
|
|
* This returns an OpenSSL::Cipher by +name+, if it is available in this
|
|
|
|
* engine.
|
|
|
|
*
|
|
|
|
* A EngineError will be raised if the cipher is unavailable.
|
|
|
|
*
|
|
|
|
* e = OpenSSL::Engine.by_id("openssl")
|
|
|
|
* => #<OpenSSL::Engine id="openssl" name="Software engine support">
|
|
|
|
* e.cipher("RC4")
|
|
|
|
* => #<OpenSSL::Cipher:0x007fc5cacc3048>
|
|
|
|
*
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_get_cipher(VALUE self, VALUE name)
|
|
|
|
{
|
|
|
|
ENGINE *e;
|
|
|
|
const EVP_CIPHER *ciph, *tmp;
|
|
|
|
int nid;
|
|
|
|
|
2016-05-23 07:40:07 -04:00
|
|
|
tmp = EVP_get_cipherbyname(StringValueCStr(name));
|
|
|
|
if(!tmp) ossl_raise(eEngineError, "no such cipher `%"PRIsVALUE"'", name);
|
2003-10-02 04:47:11 -04:00
|
|
|
nid = EVP_CIPHER_nid(tmp);
|
|
|
|
GetEngine(self, e);
|
|
|
|
ciph = ENGINE_get_cipher(e, nid);
|
|
|
|
if(!ciph) ossl_raise(eEngineError, NULL);
|
|
|
|
|
|
|
|
return ossl_cipher_new(ciph);
|
2009-04-18 10:10:06 -04:00
|
|
|
}
|
2003-10-02 04:47:11 -04:00
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine#digest
|
|
|
|
*
|
|
|
|
* call-seq:
|
|
|
|
* engine.digest(name) -> OpenSSL::Digest
|
|
|
|
*
|
|
|
|
* This returns an OpenSSL::Digest by +name+.
|
|
|
|
*
|
|
|
|
* Will raise an EngineError if the digest is unavailable.
|
|
|
|
*
|
|
|
|
* e = OpenSSL::Engine.by_id("openssl")
|
|
|
|
* #=> #<OpenSSL::Engine id="openssl" name="Software engine support">
|
|
|
|
* e.digest("SHA1")
|
|
|
|
* #=> #<OpenSSL::Digest: da39a3ee5e6b4b0d3255bfef95601890afd80709>
|
|
|
|
* e.digest("zomg")
|
|
|
|
* #=> OpenSSL::Engine::EngineError: no such digest `zomg'
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_get_digest(VALUE self, VALUE name)
|
|
|
|
{
|
|
|
|
ENGINE *e;
|
|
|
|
const EVP_MD *md, *tmp;
|
|
|
|
int nid;
|
|
|
|
|
2016-05-23 07:40:07 -04:00
|
|
|
tmp = EVP_get_digestbyname(StringValueCStr(name));
|
|
|
|
if(!tmp) ossl_raise(eEngineError, "no such digest `%"PRIsVALUE"'", name);
|
2003-10-02 04:47:11 -04:00
|
|
|
nid = EVP_MD_nid(tmp);
|
|
|
|
GetEngine(self, e);
|
|
|
|
md = ENGINE_get_digest(e, nid);
|
|
|
|
if(!md) ossl_raise(eEngineError, NULL);
|
|
|
|
|
|
|
|
return ossl_digest_new(md);
|
2009-04-18 10:10:06 -04:00
|
|
|
}
|
2003-10-02 04:47:11 -04:00
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine#load_private_key
|
|
|
|
*
|
|
|
|
* call-seq:
|
|
|
|
* engine.load_private_key(id = nil, data = nil) -> OpenSSL::PKey
|
|
|
|
*
|
|
|
|
* Loads the given private key by +id+ and +data+.
|
|
|
|
*
|
|
|
|
* An EngineError is raised of the OpenSSL::PKey is unavailable.
|
|
|
|
*
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_load_privkey(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
ENGINE *e;
|
|
|
|
EVP_PKEY *pkey;
|
2005-09-18 18:56:11 -04:00
|
|
|
VALUE id, data, obj;
|
2003-10-02 04:47:11 -04:00
|
|
|
char *sid, *sdata;
|
|
|
|
|
2005-09-07 04:29:47 -04:00
|
|
|
rb_scan_args(argc, argv, "02", &id, &data);
|
2016-05-23 07:40:07 -04:00
|
|
|
sid = NIL_P(id) ? NULL : StringValueCStr(id);
|
|
|
|
sdata = NIL_P(data) ? NULL : StringValueCStr(data);
|
2003-10-02 04:47:11 -04:00
|
|
|
GetEngine(self, e);
|
|
|
|
pkey = ENGINE_load_private_key(e, sid, NULL, sdata);
|
|
|
|
if (!pkey) ossl_raise(eEngineError, NULL);
|
2005-09-18 18:56:11 -04:00
|
|
|
obj = ossl_pkey_new(pkey);
|
|
|
|
OSSL_PKEY_SET_PRIVATE(obj);
|
2003-10-02 04:47:11 -04:00
|
|
|
|
2005-09-18 18:56:11 -04:00
|
|
|
return obj;
|
2003-10-02 04:47:11 -04:00
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine#load_public_key
|
|
|
|
*
|
|
|
|
* call-seq:
|
|
|
|
* engine.load_public_key(id = nil, data = nil) -> OpenSSL::PKey
|
|
|
|
*
|
|
|
|
* Loads the given private key by +id+ and +data+.
|
|
|
|
*
|
|
|
|
* An EngineError is raised of the OpenSSL::PKey is unavailable.
|
|
|
|
*
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_load_pubkey(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
ENGINE *e;
|
|
|
|
EVP_PKEY *pkey;
|
|
|
|
VALUE id, data;
|
|
|
|
char *sid, *sdata;
|
|
|
|
|
2005-09-18 18:56:11 -04:00
|
|
|
rb_scan_args(argc, argv, "02", &id, &data);
|
2016-05-23 07:40:07 -04:00
|
|
|
sid = NIL_P(id) ? NULL : StringValueCStr(id);
|
|
|
|
sdata = NIL_P(data) ? NULL : StringValueCStr(data);
|
2003-10-02 04:47:11 -04:00
|
|
|
GetEngine(self, e);
|
|
|
|
pkey = ENGINE_load_public_key(e, sid, NULL, sdata);
|
|
|
|
if (!pkey) ossl_raise(eEngineError, NULL);
|
|
|
|
|
|
|
|
return ossl_pkey_new(pkey);
|
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine#set_default
|
|
|
|
*
|
|
|
|
* call-seq:
|
|
|
|
* engine.set_default(flag)
|
|
|
|
*
|
|
|
|
* Set the defaults for this engine with the given +flag+.
|
|
|
|
*
|
|
|
|
* These flags are used to control combinations of algorithm methods.
|
|
|
|
*
|
|
|
|
* +flag+ can be one of the following, other flags are available depending on
|
|
|
|
* your OS.
|
|
|
|
*
|
|
|
|
* [All flags] 0xFFFF
|
|
|
|
* [No flags] 0x0000
|
|
|
|
*
|
|
|
|
* See also <openssl/engine.h>
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_set_default(VALUE self, VALUE flag)
|
|
|
|
{
|
|
|
|
ENGINE *e;
|
2004-12-15 01:35:55 -05:00
|
|
|
int f = NUM2INT(flag);
|
2003-10-02 04:47:11 -04:00
|
|
|
|
|
|
|
GetEngine(self, e);
|
2004-12-15 01:35:55 -05:00
|
|
|
ENGINE_set_default(e, f);
|
2003-10-02 04:47:11 -04:00
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine#ctrl_cmd
|
|
|
|
*
|
|
|
|
* call-seq:
|
|
|
|
* engine.ctrl_cmd(command, value = nil) -> engine
|
|
|
|
*
|
|
|
|
* Send the given +command+ to this engine.
|
|
|
|
*
|
|
|
|
* Raises an EngineError if the +command+ fails.
|
|
|
|
*/
|
2005-09-07 04:29:47 -04:00
|
|
|
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);
|
2016-05-23 07:40:07 -04:00
|
|
|
ret = ENGINE_ctrl_cmd_string(e, StringValueCStr(cmd),
|
|
|
|
NIL_P(val) ? NULL : StringValueCStr(val), 0);
|
2005-09-07 04:29:47 -04:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine#cmds
|
|
|
|
*
|
|
|
|
* Returns an array of command definitions for the current engine
|
|
|
|
*/
|
2005-09-07 04:29:47 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-20 23:37:14 -05:00
|
|
|
/* Document-method: OpenSSL::Engine#inspect
|
|
|
|
*
|
|
|
|
* Pretty print this engine
|
|
|
|
*/
|
2003-10-02 04:47:11 -04:00
|
|
|
static VALUE
|
|
|
|
ossl_engine_inspect(VALUE self)
|
|
|
|
{
|
2014-01-12 19:57:41 -05:00
|
|
|
ENGINE *e;
|
|
|
|
|
|
|
|
GetEngine(self, e);
|
|
|
|
return rb_sprintf("#<%"PRIsVALUE" id=\"%s\" name=\"%s\">",
|
|
|
|
rb_obj_class(self), ENGINE_get_id(e), ENGINE_get_name(e));
|
2003-10-02 04:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DefEngineConst(x) rb_define_const(cEngine, #x, INT2NUM(ENGINE_##x))
|
|
|
|
|
|
|
|
void
|
2014-09-30 01:25:32 -04:00
|
|
|
Init_ossl_engine(void)
|
2003-10-02 04:47:11 -04:00
|
|
|
{
|
2016-08-29 01:47:09 -04:00
|
|
|
#if 0
|
|
|
|
mOSSL = rb_define_module("OpenSSL");
|
|
|
|
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
|
|
|
|
#endif
|
|
|
|
|
2003-10-02 04:47:11 -04:00
|
|
|
cEngine = rb_define_class_under(mOSSL, "Engine", rb_cObject);
|
|
|
|
eEngineError = rb_define_class_under(cEngine, "EngineError", eOSSLError);
|
|
|
|
|
|
|
|
rb_define_alloc_func(cEngine, ossl_engine_s_alloc);
|
|
|
|
rb_define_singleton_method(cEngine, "load", ossl_engine_s_load, -1);
|
|
|
|
rb_define_singleton_method(cEngine, "cleanup", ossl_engine_s_cleanup, 0);
|
|
|
|
rb_define_singleton_method(cEngine, "engines", ossl_engine_s_engines, 0);
|
|
|
|
rb_define_singleton_method(cEngine, "by_id", ossl_engine_s_by_id, 1);
|
|
|
|
rb_undef_method(CLASS_OF(cEngine), "new");
|
2016-08-29 01:47:09 -04:00
|
|
|
rb_undef_method(cEngine, "initialize_copy");
|
2003-10-02 04:47:11 -04:00
|
|
|
|
|
|
|
rb_define_method(cEngine, "id", ossl_engine_get_id, 0);
|
|
|
|
rb_define_method(cEngine, "name", ossl_engine_get_name, 0);
|
|
|
|
rb_define_method(cEngine, "finish", ossl_engine_finish, 0);
|
|
|
|
rb_define_method(cEngine, "cipher", ossl_engine_get_cipher, 1);
|
|
|
|
rb_define_method(cEngine, "digest", ossl_engine_get_digest, 1);
|
|
|
|
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);
|
2005-09-07 04:29:47 -04:00
|
|
|
rb_define_method(cEngine, "ctrl_cmd", ossl_engine_ctrl_cmd, -1);
|
|
|
|
rb_define_method(cEngine, "cmds", ossl_engine_get_cmds, 0);
|
2003-10-02 04:47:11 -04:00
|
|
|
rb_define_method(cEngine, "inspect", ossl_engine_inspect, 0);
|
|
|
|
|
|
|
|
DefEngineConst(METHOD_RSA);
|
|
|
|
DefEngineConst(METHOD_DSA);
|
|
|
|
DefEngineConst(METHOD_DH);
|
|
|
|
DefEngineConst(METHOD_RAND);
|
2003-10-05 09:58:02 -04:00
|
|
|
#ifdef ENGINE_METHOD_BN_MOD_EXP
|
|
|
|
DefEngineConst(METHOD_BN_MOD_EXP);
|
|
|
|
#endif
|
|
|
|
#ifdef ENGINE_METHOD_BN_MOD_EXP_CRT
|
|
|
|
DefEngineConst(METHOD_BN_MOD_EXP_CRT);
|
|
|
|
#endif
|
2003-10-02 04:47:11 -04:00
|
|
|
DefEngineConst(METHOD_CIPHERS);
|
|
|
|
DefEngineConst(METHOD_DIGESTS);
|
|
|
|
DefEngineConst(METHOD_ALL);
|
|
|
|
DefEngineConst(METHOD_NONE);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void
|
2014-09-30 01:25:32 -04:00
|
|
|
Init_ossl_engine(void)
|
2003-10-02 04:47:11 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|