class OpenSSL::Config - Documentation for Ruby 4.0
Description: class OpenSSL::Config: Configuration for the openssl library. Many system's installation of openssl library will depend on your system configuration.
Keywords: ruby,class,OpenSSL::Config

🔍

No recent searches

# class OpenSSL::Config

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 docs.openssl.org/master/man5/config/

### Constants

DEFAULT_CONFIG_FILE

  The default system configuration file for `OpenSSL`.

### Public Class Methods

new(filename) → OpenSSL::Config
Source

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

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.

parse(string) → OpenSSL::Config
Source

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

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

parse_config(io) → hash
Source

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

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

### Public Instance Methods

config[section] → hash
Source

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

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

each { |section, key, value| }
Source

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

Retrieves the section and its pairs for the current configuration.

    config.each do |section, key, value|
      # ...
    end

get_value(section, key) → string
Source

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

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"

initialize_copy (p1)
Source

    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
Source

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

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

sections → array of string
Source

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

Get the names of all sections in the current configuration.

to_s → string
Source

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

Gets the parsable form of the current configuration.

Given the following configuration file being loaded:

    config = OpenSSL::Config.load('baz.cnf')
      #=> #<OpenSSL::Config sections=["default"]>
    puts config.to_s
      #=> [ default ]
      #   foo=bar
      #   baz=buz

You can get the serialized configuration using `to_s` and then parse it later:

    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