1/* 2 * $Id: ossl.h 44659 2014-01-19 16:28:53Z nagachika $ 3 * 'OpenSSL for Ruby' project 4 * Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz> 5 * All rights reserved. 6 */ 7/* 8 * This program is licenced under the same licence as Ruby. 9 * (See the file 'LICENCE'.) 10 */ 11#if !defined(_OSSL_H_) 12#define _OSSL_H_ 13 14#include RUBY_EXTCONF_H 15 16#if defined(__cplusplus) 17extern "C" { 18#endif 19 20#if 0 21 mOSSL = rb_define_module("OpenSSL"); 22 mX509 = rb_define_module_under(mOSSL, "X509"); 23#endif 24 25/* 26* OpenSSL has defined RFILE and Ruby has defined RFILE - so undef it! 27*/ 28#if defined(RFILE) /*&& !defined(OSSL_DEBUG)*/ 29# undef RFILE 30#endif 31#include <ruby.h> 32#include <ruby/io.h> 33#include <ruby/thread.h> 34 35/* 36 * Check the OpenSSL version 37 * The only supported are: 38 * OpenSSL >= 0.9.7 39 */ 40#include <openssl/opensslv.h> 41 42#ifdef HAVE_ASSERT_H 43# include <assert.h> 44#else 45# define assert(condition) 46#endif 47 48#if defined(_WIN32) 49# include <openssl/e_os2.h> 50# define OSSL_NO_CONF_API 1 51# if !defined(OPENSSL_SYS_WIN32) 52# define OPENSSL_SYS_WIN32 1 53# endif 54# include <winsock2.h> 55#endif 56#include <errno.h> 57#include <openssl/err.h> 58#include <openssl/asn1_mac.h> 59#include <openssl/x509v3.h> 60#include <openssl/ssl.h> 61#include <openssl/pkcs12.h> 62#include <openssl/pkcs7.h> 63#include <openssl/hmac.h> 64#include <openssl/rand.h> 65#include <openssl/conf.h> 66#include <openssl/conf_api.h> 67#undef X509_NAME 68#undef PKCS7_SIGNER_INFO 69#if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ST_ENGINE) 70# define OSSL_ENGINE_ENABLED 71# include <openssl/engine.h> 72#endif 73#if defined(HAVE_OPENSSL_OCSP_H) 74# define OSSL_OCSP_ENABLED 75# include <openssl/ocsp.h> 76#endif 77 78/* OpenSSL requires passwords for PEM-encoded files to be at least four 79 * characters long 80 */ 81#define OSSL_MIN_PWD_LEN 4 82 83/* 84 * Common Module 85 */ 86extern VALUE mOSSL; 87 88/* 89 * Common Error Class 90 */ 91extern VALUE eOSSLError; 92 93/* 94 * CheckTypes 95 */ 96#define OSSL_Check_Kind(obj, klass) do {\ 97 if (!rb_obj_is_kind_of((obj), (klass))) {\ 98 ossl_raise(rb_eTypeError, "wrong argument (%"PRIsVALUE")! (Expected kind of %"PRIsVALUE")",\ 99 rb_obj_class(obj), (klass));\ 100 }\ 101} while (0) 102 103#define OSSL_Check_Instance(obj, klass) do {\ 104 if (!rb_obj_is_instance_of((obj), (klass))) {\ 105 ossl_raise(rb_eTypeError, "wrong argument (%"PRIsVALUE")! (Expected instance of %"PRIsVALUE")",\ 106 rb_obj_class(obj), (klass));\ 107 }\ 108} while (0) 109 110#define OSSL_Check_Same_Class(obj1, obj2) do {\ 111 if (!rb_obj_is_instance_of((obj1), rb_obj_class(obj2))) {\ 112 ossl_raise(rb_eTypeError, "wrong argument type");\ 113 }\ 114} while (0) 115 116/* 117 * Compatibility 118 */ 119#if OPENSSL_VERSION_NUMBER >= 0x10000000L 120#define STACK _STACK 121#endif 122 123/* 124 * String to HEXString conversion 125 */ 126int string2hex(const unsigned char *, int, char **, int *); 127 128/* 129 * Data Conversion 130 */ 131STACK_OF(X509) *ossl_x509_ary2sk0(VALUE); 132STACK_OF(X509) *ossl_x509_ary2sk(VALUE); 133STACK_OF(X509) *ossl_protect_x509_ary2sk(VALUE,int*); 134VALUE ossl_x509_sk2ary(STACK_OF(X509) *certs); 135VALUE ossl_x509crl_sk2ary(STACK_OF(X509_CRL) *crl); 136VALUE ossl_x509name_sk2ary(STACK_OF(X509_NAME) *names); 137VALUE ossl_buf2str(char *buf, int len); 138#define ossl_str_adjust(str, p) \ 139do{\ 140 int len = RSTRING_LENINT(str);\ 141 int newlen = rb_long2int((p) - (unsigned char*)RSTRING_PTR(str));\ 142 assert(newlen <= len);\ 143 rb_str_set_len((str), newlen);\ 144}while(0) 145 146/* 147 * our default PEM callback 148 */ 149int ossl_pem_passwd_cb(char *, int, int, void *); 150 151/* 152 * Clear BIO* with this in PEM/DER fallback scenarios to avoid decoding 153 * errors piling up in OpenSSL::Errors 154 */ 155#define OSSL_BIO_reset(bio) (void)BIO_reset((bio)); \ 156 ERR_clear_error(); 157 158/* 159 * ERRor messages 160 */ 161#define OSSL_ErrMsg() ERR_reason_error_string(ERR_get_error()) 162NORETURN(void ossl_raise(VALUE, const char *, ...)); 163VALUE ossl_exc_new(VALUE, const char *, ...); 164 165/* 166 * Verify callback 167 */ 168extern int ossl_verify_cb_idx; 169 170struct ossl_verify_cb_args { 171 VALUE proc; 172 VALUE preverify_ok; 173 VALUE store_ctx; 174}; 175 176VALUE ossl_call_verify_cb_proc(struct ossl_verify_cb_args *); 177int ossl_verify_cb(int, X509_STORE_CTX *); 178 179/* 180 * String to DER String 181 */ 182extern ID ossl_s_to_der; 183VALUE ossl_to_der(VALUE); 184VALUE ossl_to_der_if_possible(VALUE); 185 186/* 187 * Debug 188 */ 189extern VALUE dOSSL; 190 191#if defined(HAVE_VA_ARGS_MACRO) 192#define OSSL_Debug(...) do { \ 193 if (dOSSL == Qtrue) { \ 194 fprintf(stderr, "OSSL_DEBUG: "); \ 195 fprintf(stderr, __VA_ARGS__); \ 196 fprintf(stderr, " [%s:%d]\n", __FILE__, __LINE__); \ 197 } \ 198} while (0) 199 200#define OSSL_Warning(fmt, ...) do { \ 201 OSSL_Debug((fmt), ##__VA_ARGS__); \ 202 rb_warning((fmt), ##__VA_ARGS__); \ 203} while (0) 204 205#define OSSL_Warn(fmt, ...) do { \ 206 OSSL_Debug((fmt), ##__VA_ARGS__); \ 207 rb_warn((fmt), ##__VA_ARGS__); \ 208} while (0) 209#else 210void ossl_debug(const char *, ...); 211#define OSSL_Debug ossl_debug 212#define OSSL_Warning rb_warning 213#define OSSL_Warn rb_warn 214#endif 215 216/* 217 * Include all parts 218 */ 219#include "openssl_missing.h" 220#include "ruby_missing.h" 221#include "ossl_asn1.h" 222#include "ossl_bio.h" 223#include "ossl_bn.h" 224#include "ossl_cipher.h" 225#include "ossl_config.h" 226#include "ossl_digest.h" 227#include "ossl_hmac.h" 228#include "ossl_ns_spki.h" 229#include "ossl_ocsp.h" 230#include "ossl_pkcs12.h" 231#include "ossl_pkcs7.h" 232#include "ossl_pkcs5.h" 233#include "ossl_pkey.h" 234#include "ossl_rand.h" 235#include "ossl_ssl.h" 236#include "ossl_version.h" 237#include "ossl_x509.h" 238#include "ossl_engine.h" 239 240void Init_openssl(void); 241 242#if defined(__cplusplus) 243} 244#endif 245 246#endif /* _OSSL_H_ */ 247 248