Ruby  2.0.0p247(2013-06-27revision41674)
ext/openssl/ossl.h
Go to the documentation of this file.
00001 /*
00002  * $Id: ossl.h 36355 2012-07-10 13:57:11Z 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 #if !defined(_OSSL_H_)
00012 #define _OSSL_H_
00013 
00014 #include RUBY_EXTCONF_H
00015 
00016 #if defined(__cplusplus)
00017 extern "C" {
00018 #endif
00019 
00020 #if 0
00021   mOSSL = rb_define_module("OpenSSL");
00022   mX509 = rb_define_module_under(mOSSL, "X509");
00023 #endif
00024 
00025 /*
00026 * OpenSSL has defined RFILE and Ruby has defined RFILE - so undef it!
00027 */
00028 #if defined(RFILE) /*&& !defined(OSSL_DEBUG)*/
00029 #  undef RFILE
00030 #endif
00031 #include <ruby.h>
00032 #include <ruby/io.h>
00033 #include <ruby/thread.h>
00034 
00035 /*
00036  * Check the OpenSSL version
00037  * The only supported are:
00038  *      OpenSSL >= 0.9.7
00039  */
00040 #include <openssl/opensslv.h>
00041 
00042 #ifdef HAVE_ASSERT_H
00043 #  include <assert.h>
00044 #else
00045 #  define assert(condition)
00046 #endif
00047 
00048 #if defined(_WIN32)
00049 #  include <openssl/e_os2.h>
00050 #  define OSSL_NO_CONF_API 1
00051 #  if !defined(OPENSSL_SYS_WIN32)
00052 #    define OPENSSL_SYS_WIN32 1
00053 #  endif
00054 #  include <winsock2.h>
00055 #endif
00056 #include <errno.h>
00057 #include <openssl/err.h>
00058 #include <openssl/asn1_mac.h>
00059 #include <openssl/x509v3.h>
00060 #include <openssl/ssl.h>
00061 #include <openssl/pkcs12.h>
00062 #include <openssl/pkcs7.h>
00063 #include <openssl/hmac.h>
00064 #include <openssl/rand.h>
00065 #include <openssl/conf.h>
00066 #include <openssl/conf_api.h>
00067 #undef X509_NAME
00068 #undef PKCS7_SIGNER_INFO
00069 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ST_ENGINE)
00070 #  define OSSL_ENGINE_ENABLED
00071 #  include <openssl/engine.h>
00072 #endif
00073 #if defined(HAVE_OPENSSL_OCSP_H)
00074 #  define OSSL_OCSP_ENABLED
00075 #  include <openssl/ocsp.h>
00076 #endif
00077 
00078 /* OpenSSL requires passwords for PEM-encoded files to be at least four
00079  * characters long
00080  */
00081 #define OSSL_MIN_PWD_LEN 4
00082 
00083 /*
00084  * Common Module
00085  */
00086 extern VALUE mOSSL;
00087 
00088 /*
00089  * Common Error Class
00090  */
00091 extern VALUE eOSSLError;
00092 
00093 /*
00094  * CheckTypes
00095  */
00096 #define OSSL_Check_Kind(obj, klass) do {\
00097   if (!rb_obj_is_kind_of((obj), (klass))) {\
00098     ossl_raise(rb_eTypeError, "wrong argument (%s)! (Expected kind of %s)",\
00099                rb_obj_classname(obj), rb_class2name(klass));\
00100   }\
00101 } while (0)
00102 
00103 #define OSSL_Check_Instance(obj, klass) do {\
00104   if (!rb_obj_is_instance_of((obj), (klass))) {\
00105     ossl_raise(rb_eTypeError, "wrong argument (%s)! (Expected instance of %s)",\
00106                rb_obj_classname(obj), rb_class2name(klass));\
00107   }\
00108 } while (0)
00109 
00110 #define OSSL_Check_Same_Class(obj1, obj2) do {\
00111   if (!rb_obj_is_instance_of((obj1), rb_obj_class(obj2))) {\
00112     ossl_raise(rb_eTypeError, "wrong argument type");\
00113   }\
00114 } while (0)
00115 
00116 /*
00117  * Compatibility
00118  */
00119 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
00120 #define STACK _STACK
00121 #endif
00122 
00123 /*
00124  * String to HEXString conversion
00125  */
00126 int string2hex(const unsigned char *, int, char **, int *);
00127 
00128 /*
00129  * Data Conversion
00130  */
00131 STACK_OF(X509) *ossl_x509_ary2sk0(VALUE);
00132 STACK_OF(X509) *ossl_x509_ary2sk(VALUE);
00133 STACK_OF(X509) *ossl_protect_x509_ary2sk(VALUE,int*);
00134 VALUE ossl_x509_sk2ary(STACK_OF(X509) *certs);
00135 VALUE ossl_x509crl_sk2ary(STACK_OF(X509_CRL) *crl);
00136 VALUE ossl_x509name_sk2ary(STACK_OF(X509_NAME) *names);
00137 VALUE ossl_buf2str(char *buf, int len);
00138 #define ossl_str_adjust(str, p) \
00139 do{\
00140     int len = RSTRING_LENINT(str);\
00141     int newlen = rb_long2int((p) - (unsigned char*)RSTRING_PTR(str));\
00142     assert(newlen <= len);\
00143     rb_str_set_len((str), newlen);\
00144 }while(0)
00145 
00146 /*
00147  * our default PEM callback
00148  */
00149 int ossl_pem_passwd_cb(char *, int, int, void *);
00150 
00151 /*
00152  * Clear BIO* with this in PEM/DER fallback scenarios to avoid decoding
00153  * errors piling up in OpenSSL::Errors
00154  */
00155 #define OSSL_BIO_reset(bio)     (void)BIO_reset((bio)); \
00156                                 ERR_clear_error();
00157 
00158 /*
00159  * ERRor messages
00160  */
00161 #define OSSL_ErrMsg() ERR_reason_error_string(ERR_get_error())
00162 NORETURN(void ossl_raise(VALUE, const char *, ...));
00163 VALUE ossl_exc_new(VALUE, const char *, ...);
00164 
00165 /*
00166  * Verify callback
00167  */
00168 extern int ossl_verify_cb_idx;
00169 
00170 struct ossl_verify_cb_args {
00171     VALUE proc;
00172     VALUE preverify_ok;
00173     VALUE store_ctx;
00174 };
00175 
00176 VALUE ossl_call_verify_cb_proc(struct ossl_verify_cb_args *);
00177 int ossl_verify_cb(int, X509_STORE_CTX *);
00178 
00179 /*
00180  * String to DER String
00181  */
00182 extern ID ossl_s_to_der;
00183 VALUE ossl_to_der(VALUE);
00184 VALUE ossl_to_der_if_possible(VALUE);
00185 
00186 /*
00187  * Debug
00188  */
00189 extern VALUE dOSSL;
00190 
00191 #if defined(HAVE_VA_ARGS_MACRO)
00192 #define OSSL_Debug(...) do { \
00193   if (dOSSL == Qtrue) { \
00194     fprintf(stderr, "OSSL_DEBUG: "); \
00195     fprintf(stderr, __VA_ARGS__); \
00196     fprintf(stderr, " [%s:%d]\n", __FILE__, __LINE__); \
00197   } \
00198 } while (0)
00199 
00200 #define OSSL_Warning(fmt, ...) do { \
00201   OSSL_Debug((fmt), ##__VA_ARGS__); \
00202   rb_warning((fmt), ##__VA_ARGS__); \
00203 } while (0)
00204 
00205 #define OSSL_Warn(fmt, ...) do { \
00206   OSSL_Debug((fmt), ##__VA_ARGS__); \
00207   rb_warn((fmt), ##__VA_ARGS__); \
00208 } while (0)
00209 #else
00210 void ossl_debug(const char *, ...);
00211 #define OSSL_Debug ossl_debug
00212 #define OSSL_Warning rb_warning
00213 #define OSSL_Warn rb_warn
00214 #endif
00215 
00216 /*
00217  * Include all parts
00218  */
00219 #include "openssl_missing.h"
00220 #include "ruby_missing.h"
00221 #include "ossl_asn1.h"
00222 #include "ossl_bio.h"
00223 #include "ossl_bn.h"
00224 #include "ossl_cipher.h"
00225 #include "ossl_config.h"
00226 #include "ossl_digest.h"
00227 #include "ossl_hmac.h"
00228 #include "ossl_ns_spki.h"
00229 #include "ossl_ocsp.h"
00230 #include "ossl_pkcs12.h"
00231 #include "ossl_pkcs7.h"
00232 #include "ossl_pkcs5.h"
00233 #include "ossl_pkey.h"
00234 #include "ossl_rand.h"
00235 #include "ossl_ssl.h"
00236 #include "ossl_version.h"
00237 #include "ossl_x509.h"
00238 #include "ossl_engine.h"
00239 
00240 void Init_openssl(void);
00241 
00242 #if defined(__cplusplus)
00243 }
00244 #endif
00245 
00246 #endif /* _OSSL_H_ */
00247 
00248