Class: OpenSSL::Config
- Inherits:
-
Object
- Object
- OpenSSL::Config
- Includes:
- Enumerable
- Defined in:
- ossl_config.c,
ossl_config.c
Overview
Configuration for the openssl library.
Many system’s installation of openssl library will depend on your system configuration. See the value of OpenSSL::Config::DEFAULT_CONFIG_FILE for the location of the file for your host.
Constant Summary collapse
- DEFAULT_CONFIG_FILE =
path_str
Class Method Summary collapse
-
.parse(string) ⇒ OpenSSL::Config
Parses a given string as a blob that contains configuration for OpenSSL.
-
.parse_config(io) ⇒ Hash
Parses the configuration data read from io and returns the whole content as a Hash.
Instance Method Summary collapse
-
#[](section) ⇒ Hash
Gets all key-value pairs in a specific section from the current configuration.
- #each ⇒ Object
-
#get_value(section, key) ⇒ String
Gets the value of key from the given section.
-
#new(filename) ⇒ OpenSSL::Config
constructor
Creates an instance of OpenSSL::Config from the content of the file specified by filename.
- #initialize_copy(other) ⇒ Object
-
#inspect ⇒ String
String representation of this configuration object, including the class name and its sections.
- #sections ⇒ Object
- #to_s ⇒ Object
Constructor Details
#new(filename) ⇒ OpenSSL::Config
Creates an instance of OpenSSL::Config from the content of the file specified by filename.
This can be used in contexts like OpenSSL::X509::ExtensionFactory.config=
This can raise IO exceptions based on the access, or availability of the file. A ConfigError exception may be raised depending on the validity of the data being configured.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'ossl_config.c', line 133
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;
}
|
Class Method Details
.parse(string) ⇒ OpenSSL::Config
Parses a given string as a blob that contains configuration for OpenSSL.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'ossl_config.c', line 81
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
Parses the configuration data read from io and returns the whole content as a Hash.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'ossl_config.c', line 104
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;
}
|
Instance Method Details
#[](section) ⇒ Hash
Gets all key-value pairs in a specific section from the current configuration.
Given the following configurating file being loaded:
config = OpenSSL::Config.load('foo.cnf')
#=> #<OpenSSL::Config sections=["default"]>
puts config.to_s
#=> [ default ]
# foo=bar
You can get a hash of the specific section like so:
config['default']
#=> {"foo"=>"bar"}
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'ossl_config.c', line 226
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 ⇒ Object
#get_value(section, key) ⇒ String
Gets the value of key from the given section.
Given the following configurating file being loaded:
config = OpenSSL::Config.load('foo.cnf')
#=> #<OpenSSL::Config sections=["default"]>
puts config.to_s
#=> [ default ]
# foo=bar
You can get a specific value from the config if you know the section and key like so:
config.get_value('default','foo')
#=> "bar"
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'ossl_config.c', line 187
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(other) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'ossl_config.c', line 152
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
String representation of this configuration object, including the class name and its sections.
395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'ossl_config.c', line 395
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 ⇒ Object
94 |
# File 'ossl_config.c', line 94 static VALUE config_get_sections(VALUE self); |