Ruby 1.9.3p327(2012-11-10revision37606)
ext/openssl/ossl_config.c
Go to the documentation of this file.
00001 /*
00002  * $Id: ossl_config.c 29359 2010-09-29 03:37:44Z nobu $
00003  * 'OpenSSL for Ruby' project
00004  * Copyright (C) 2001-2002  Michal Rokos <m.rokos@sh.cvut.cz>
00005  * All rights reserved.
00006  */
00007 /*
00008  * This program is licenced under the same licence as Ruby.
00009  * (See the file 'LICENCE'.)
00010  */
00011 #include "ossl.h"
00012 
00013 
00014 /*
00015  * Classes
00016  */
00017 VALUE cConfig;
00018 VALUE eConfigError;
00019 
00020 /*
00021  * Public
00022  */
00023 
00024 /*
00025  * GetConfigPtr is a public C-level function for getting OpenSSL CONF struct
00026  * from an OpenSSL::Config(eConfig) instance.  We decided to implement
00027  * OpenSSL::Config in Ruby level but we need to pass native CONF struct for
00028  * some OpenSSL features such as X509V3_EXT_*.
00029  */
00030 CONF *
00031 GetConfigPtr(VALUE obj)
00032 {
00033     CONF *conf;
00034     VALUE str;
00035     BIO *bio;
00036     long eline = -1;
00037 
00038     OSSL_Check_Kind(obj, cConfig);
00039     str = rb_funcall(obj, rb_intern("to_s"), 0);
00040     bio = ossl_obj2bio(str);
00041     conf = NCONF_new(NULL);
00042     if(!conf){
00043         BIO_free(bio);
00044         ossl_raise(eConfigError, NULL);
00045     }
00046     if(!NCONF_load_bio(conf, bio, &eline)){
00047         BIO_free(bio);
00048         NCONF_free(conf);
00049         if (eline <= 0) ossl_raise(eConfigError, "wrong config format");
00050         else ossl_raise(eConfigError, "error in line %d", eline);
00051         ossl_raise(eConfigError, NULL);
00052     }
00053     BIO_free(bio);
00054 
00055     return conf;
00056 }
00057 
00058 
00059 /*
00060  * INIT
00061  */
00062 void
00063 Init_ossl_config()
00064 {
00065     char *default_config_file;
00066     eConfigError = rb_define_class_under(mOSSL, "ConfigError", eOSSLError);
00067     cConfig = rb_define_class_under(mOSSL, "Config", rb_cObject);
00068 
00069     default_config_file = CONF_get1_default_config_file();
00070     rb_define_const(cConfig, "DEFAULT_CONFIG_FILE",
00071                     rb_str_new2(default_config_file));
00072     OPENSSL_free(default_config_file);
00073     /* methods are defined by openssl/config.rb */
00074 }
00075