Class: OpenSSL::Config

Inherits:
Object show all
Includes:
Enumerable
Defined in:
ext/openssl/ossl_config.c,
ext/openssl/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.

See also www.openssl.org/docs/apps/config.html

Constant Summary collapse

DEFAULT_CONFIG_FILE =
path_str

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#to_set

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.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'ext/openssl/ossl_config.c', line 132

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 */
    }
    return self;
}

Class Method Details

.parse(string) ⇒ OpenSSL::Config

Parses a given string as a blob that contains configuration for OpenSSL.

Returns:



81
82
83
84
85
86
87
88
89
90
91
# File 'ext/openssl/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 */
    return obj;
}

.parse_config(io) ⇒ Hash

Parses the configuration data read from io and returns the whole content as a Hash.

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'ext/openssl/ossl_config.c', line 103

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"}

Returns:



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'ext/openssl/ossl_config.c', line 223

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;
}

#eachObject

#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"

Returns:



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'ext/openssl/ossl_config.c', line 184

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



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'ext/openssl/ossl_config.c', line 150

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 */
    return self;
}

#inspectString

String representation of this configuration object, including the class name and its sections.

Returns:



394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'ext/openssl/ossl_config.c', line 394

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;
}

#sectionsObject



93
# File 'ext/openssl/ossl_config.c', line 93

static VALUE config_get_sections(VALUE self);

#to_sObject