class OpenSSL::Config
openssl 库的配置。
许多系统上 openssl 库的安装将取决于您的系统配置。请参阅 OpenSSL::Config::DEFAULT_CONFIG_FILE 的值,以获取您主机上的文件位置。
常量
- DEFAULT_CONFIG_FILE
OpenSSL的默认系统配置文件。
公共类方法
new(filename) → OpenSSL::Config 点击以切换源代码
从 filename 指定的文件内容创建一个 OpenSSL::Config 实例。
这可以在诸如 OpenSSL::X509::ExtensionFactory.config= 等上下文中使用。
这可能会根据文件的访问权限或可用性引发 IO 异常。 根据正在配置的数据的有效性,可能会引发 ConfigError 异常。
static VALUE
config_initialize(int argc, VALUE *argv, VALUE self)
{
CONF *conf = GetConfig(self);
VALUE filename;
/* 0-arguments call has no use-case, but is kept for compatibility */
rb_scan_args(argc, argv, "01", &filename);
rb_check_frozen(self);
if (!NIL_P(filename)) {
BIO *bio = BIO_new_file(StringValueCStr(filename), "rb");
if (!bio)
ossl_raise(eConfigError, "BIO_new_file");
config_load_bio(conf, bio); /* Consumes BIO */
}
rb_obj_freeze(self);
return self;
}
parse(string) → OpenSSL::Config 点击以切换源代码
解析给定的 string 作为包含 OpenSSL 配置的 blob。
static VALUE
config_s_parse(VALUE klass, VALUE str)
{
VALUE obj = config_s_alloc(klass);
CONF *conf = GetConfig(obj);
BIO *bio;
bio = ossl_obj2bio(&str);
config_load_bio(conf, bio); /* Consumes BIO */
rb_obj_freeze(obj);
return obj;
}
parse_config(io) → hash 点击以切换源代码
解析从 io 读取的配置数据,并将整个内容作为 Hash 返回。
static VALUE
config_s_parse_config(VALUE klass, VALUE io)
{
VALUE obj, sections, ret;
long i;
obj = config_s_parse(klass, io);
sections = config_get_sections(obj);
ret = rb_hash_new();
for (i = 0; i < RARRAY_LEN(sections); i++) {
VALUE section = rb_ary_entry(sections, i);
rb_hash_aset(ret, section, config_get_section(obj, section));
}
return ret;
}
公共实例方法
config[section] → hash 点击以切换源代码
从当前配置中获取特定 section 中的所有键值对。
假设加载了以下配置文件
config = OpenSSL::Config.load('foo.cnf') #=> #<OpenSSL::Config sections=["default"]> puts config.to_s #=> [ default ] # foo=bar
您可以像这样获取特定 section 的哈希值
config['default'] #=> {"foo"=>"bar"}
static VALUE
config_get_section(VALUE self, VALUE section)
{
CONF *conf = GetConfig(self);
STACK_OF(CONF_VALUE) *sk;
int i, entries;
VALUE hash;
hash = rb_hash_new();
StringValueCStr(section);
if (!(sk = NCONF_get_section(conf, RSTRING_PTR(section)))) {
ossl_clear_error();
return hash;
}
entries = sk_CONF_VALUE_num(sk);
for (i = 0; i < entries; i++) {
CONF_VALUE *entry = sk_CONF_VALUE_value(sk, i);
rb_hash_aset(hash, rb_str_new_cstr(entry->name),
rb_str_new_cstr(entry->value));
}
return hash;
}
each { |section, key, value| } 点击以切换源代码
检索当前配置的 section 及其键值对。
config.each do |section, key, value| # ... end
static VALUE
config_each(VALUE self)
{
CONF *conf = GetConfig(self);
RETURN_ENUMERATOR(self, 0, 0);
lh_doall_arg((_LHASH *)conf->data, LHASH_DOALL_ARG_FN(each_conf_value),
NULL);
return self;
}
get_value(section, key) → string 点击以切换源代码
从给定的 section 获取 key 的值。
假设加载了以下配置文件
config = OpenSSL::Config.load('foo.cnf') #=> #<OpenSSL::Config sections=["default"]> puts config.to_s #=> [ default ] # foo=bar
如果您知道 section 和 key,则可以像这样从配置中获取特定值
config.get_value('default','foo') #=> "bar"
static VALUE
config_get_value(VALUE self, VALUE section, VALUE key)
{
CONF *conf = GetConfig(self);
const char *str, *sectionp;
StringValueCStr(section);
StringValueCStr(key);
/* For compatibility; NULL means "default". */
sectionp = RSTRING_LEN(section) ? RSTRING_PTR(section) : NULL;
str = NCONF_get_string(conf, sectionp, RSTRING_PTR(key));
if (!str) {
ossl_clear_error();
return Qnil;
}
return rb_str_new_cstr(str);
}
initialize_copy(p1) 点击以切换源代码
static VALUE
config_initialize_copy(VALUE self, VALUE other)
{
CONF *conf = GetConfig(self);
VALUE str;
BIO *bio;
str = rb_funcall(other, rb_intern("to_s"), 0);
rb_check_frozen(self);
bio = ossl_obj2bio(&str);
config_load_bio(conf, bio); /* Consumes BIO */
rb_obj_freeze(self);
return self;
}
inspect → string 点击以切换源代码
此配置对象的字符串表示形式,包括类名及其 section。
static VALUE
config_inspect(VALUE self)
{
VALUE str, ary = config_get_sections(self);
const char *cname = rb_class2name(rb_obj_class(self));
str = rb_str_new_cstr("#<");
rb_str_cat_cstr(str, cname);
rb_str_cat_cstr(str, " sections=");
rb_str_append(str, rb_inspect(ary));
rb_str_cat_cstr(str, ">");
return str;
}
sections → string 数组 点击以切换源代码
获取当前配置中所有 section 的名称。
static VALUE
config_get_sections(VALUE self)
{
CONF *conf = GetConfig(self);
VALUE ary;
ary = rb_ary_new();
lh_doall_arg((_LHASH *)conf->data, LHASH_DOALL_ARG_FN(get_conf_section),
&ary);
return ary;
}
to_s → string 点击以切换源代码
获取当前配置的可解析形式。
假设加载了以下配置文件
config = OpenSSL::Config.load('baz.cnf') #=> #<OpenSSL::Config sections=["default"]> puts config.to_s #=> [ default ] # foo=bar # baz=buz
您可以使用 to_s 获取序列化的配置,然后在以后对其进行解析
serialized_config = config.to_s # much later... new_config = OpenSSL::Config.parse(serialized_config) #=> #<OpenSSL::Config sections=["default"]> puts new_config #=> [ default ] foo=bar baz=buz
static VALUE
config_to_s(VALUE self)
{
CONF *conf = GetConfig(self);
VALUE str;
str = rb_str_new(NULL, 0);
lh_doall_arg((_LHASH *)conf->data, LHASH_DOALL_ARG_FN(dump_conf_value),
&str);
return str;
}