Ruby: ext/openssl/ossl_config.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

ossl_config.c
Go to the documentation of this file.
1/*
2 * 'OpenSSL for Ruby' project
3 * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
4 * All rights reserved.
5 */
6/*
7 * This program is licensed under the same licence as Ruby.
8 * (See the file 'LICENCE'.)
9 */
10#include "ossl.h"
11
12
13/*
14 * Classes
15 */
16 VALUE cConfig;
17/* Document-class: OpenSSL::ConfigError
18 *
19 * General error for openssl library configuration files. Including formatting,
20 * parsing errors, etc.
21 */
22 VALUE eConfigError;
23
24/*
25 * Public
26 */
27
28/*
29 * DupConfigPtr is a public C-level function for getting OpenSSL CONF struct
30 * from an OpenSSL::Config(eConfig) instance. We decided to implement
31 * OpenSSL::Config in Ruby level but we need to pass native CONF struct for
32 * some OpenSSL features such as X509V3_EXT_*.
33 */
34CONF *
35DupConfigPtr(VALUE obj)
36{
37 CONF *conf;
38 VALUE str;
39 BIO *bio;
40 long eline = -1;
41
42 OSSL_Check_Kind(obj, cConfig);
43 str = rb_funcall(obj, rb_intern("to_s"), 0);
44 bio = ossl_obj2bio(&str);
45 conf = NCONF_new(NULL);
46 if(!conf){
47 BIO_free(bio);
48 ossl_raise(eConfigError, NULL);
49 }
50 if(!NCONF_load_bio(conf, bio, &eline)){
51 BIO_free(bio);
52 NCONF_free(conf);
53 if (eline <= 0)
54 ossl_raise(eConfigError, "wrong config format");
55 else
56 ossl_raise(eConfigError, "error in line %d", eline);
57 }
58 BIO_free(bio);
59
60 return conf;
61}
62
63/* Document-const: DEFAULT_CONFIG_FILE
64 *
65 * The default system configuration file for openssl
66 */
67
68/*
69 * INIT
70 */
71void
72Init_ossl_config(void)
73{
74 char *default_config_file;
75
76#if 0
77 mOSSL = rb_define_module("OpenSSL");
78 eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
79#endif
80
81 eConfigError = rb_define_class_under(mOSSL, "ConfigError", eOSSLError);
82 cConfig = rb_define_class_under(mOSSL, "Config", rb_cObject);
83
84 default_config_file = CONF_get1_default_config_file();
85 rb_define_const(cConfig, "DEFAULT_CONFIG_FILE",
86 rb_str_new2(default_config_file));
87 OPENSSL_free(default_config_file);
88 /* methods are defined by openssl/config.rb */
89}
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
rb_define_class_under
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:797
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:871
rb_eStandardError
VALUE rb_eStandardError
Definition: error.c:1054
rb_cObject
VALUE rb_cObject
Object class.
Definition: object.c:49
rb_funcall
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:1077
rb_str_new2
#define rb_str_new2
Definition: string.h:276
rb_intern
ID rb_intern(const char *)
Definition: symbol.c:785
rb_define_const
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:3150
mOSSL
VALUE mOSSL
Definition: ossl.c:231
ossl_raise
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:293
eOSSLError
VALUE eOSSLError
Definition: ossl.c:236
ossl.h
OSSL_Check_Kind
#define OSSL_Check_Kind(obj, klass)
Definition: ossl.h:59
ossl_obj2bio
BIO * ossl_obj2bio(volatile VALUE *pobj)
Definition: ossl_bio.c:13
eConfigError
VALUE eConfigError
Definition: ossl_config.c:22
Init_ossl_config
void Init_ossl_config(void)
Definition: ossl_config.c:72
cConfig
VALUE cConfig
Definition: ossl_config.c:16
DupConfigPtr
CONF * DupConfigPtr(VALUE obj)
Definition: ossl_config.c:35
NULL
#define NULL
Definition: regenc.h:69
VALUE
unsigned long VALUE
Definition: value.h:38