Ruby  1.9.3p429(2013-05-15revision40747)
ossl.h
Go to the documentation of this file.
1 /*
2  * $Id: ossl.h 32452 2011-07-08 06:03:17Z usa $
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)
17 extern "C" {
18 #endif
19 
20 #if 0
21  mOSSL = rb_define_module("OpenSSL");
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 
34 /*
35  * Check the OpenSSL version
36  * The only supported are:
37  * OpenSSL >= 0.9.7
38  */
39 #include <openssl/opensslv.h>
40 
41 #ifdef HAVE_ASSERT_H
42 # include <assert.h>
43 #else
44 # define assert(condition)
45 #endif
46 
47 #if defined(_WIN32)
48 # include <openssl/e_os2.h>
49 # define OSSL_NO_CONF_API 1
50 # if !defined(OPENSSL_SYS_WIN32)
51 # define OPENSSL_SYS_WIN32 1
52 # endif
53 # include <winsock2.h>
54 #endif
55 #include <errno.h>
56 #include <openssl/err.h>
57 #include <openssl/asn1_mac.h>
58 #include <openssl/x509v3.h>
59 #include <openssl/ssl.h>
60 #include <openssl/pkcs12.h>
61 #include <openssl/pkcs7.h>
62 #include <openssl/hmac.h>
63 #include <openssl/rand.h>
64 #include <openssl/conf.h>
65 #include <openssl/conf_api.h>
66 #undef X509_NAME
67 #undef PKCS7_SIGNER_INFO
68 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ST_ENGINE)
69 # define OSSL_ENGINE_ENABLED
70 # include <openssl/engine.h>
71 #endif
72 #if defined(HAVE_OPENSSL_OCSP_H)
73 # define OSSL_OCSP_ENABLED
74 # include <openssl/ocsp.h>
75 #endif
76 
77 /*
78  * Common Module
79  */
80 extern VALUE mOSSL;
81 
82 /*
83  * Common Error Class
84  */
85 extern VALUE eOSSLError;
86 
87 /*
88  * CheckTypes
89  */
90 #define OSSL_Check_Kind(obj, klass) do {\
91  if (!rb_obj_is_kind_of((obj), (klass))) {\
92  ossl_raise(rb_eTypeError, "wrong argument (%s)! (Expected kind of %s)",\
93  rb_obj_classname(obj), rb_class2name(klass));\
94  }\
95 } while (0)
96 
97 #define OSSL_Check_Instance(obj, klass) do {\
98  if (!rb_obj_is_instance_of((obj), (klass))) {\
99  ossl_raise(rb_eTypeError, "wrong argument (%s)! (Expected instance of %s)",\
100  rb_obj_classname(obj), rb_class2name(klass));\
101  }\
102 } while (0)
103 
104 #define OSSL_Check_Same_Class(obj1, obj2) do {\
105  if (!rb_obj_is_instance_of((obj1), rb_obj_class(obj2))) {\
106  ossl_raise(rb_eTypeError, "wrong argument type");\
107  }\
108 } while (0)
109 
110 /*
111  * Compatibility
112  */
113 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
114 #define STACK _STACK
115 #endif
116 
117 /*
118  * String to HEXString conversion
119  */
120 int string2hex(const unsigned char *, int, char **, int *);
121 
122 /*
123  * Data Conversion
124  */
125 STACK_OF(X509) *ossl_x509_ary2sk0(VALUE);
126 STACK_OF(X509) *ossl_x509_ary2sk(VALUE);
127 STACK_OF(X509) *ossl_protect_x509_ary2sk(VALUE,int*);
128 VALUE ossl_x509_sk2ary(STACK_OF(X509) *certs);
129 VALUE ossl_x509crl_sk2ary(STACK_OF(X509_CRL) *crl);
131 VALUE ossl_buf2str(char *buf, int len);
132 #define ossl_str_adjust(str, p) \
133 do{\
134  int len = RSTRING_LENINT(str);\
135  int newlen = rb_long2int((p) - (unsigned char*)RSTRING_PTR(str));\
136  assert(newlen <= len);\
137  rb_str_set_len((str), newlen);\
138 }while(0)
139 
140 /*
141  * our default PEM callback
142  */
143 int ossl_pem_passwd_cb(char *, int, int, void *);
144 
145 /*
146  * Clear BIO* with this in PEM/DER fallback scenarios to avoid decoding
147  * errors piling up in OpenSSL::Errors
148  */
149 #define OSSL_BIO_reset(bio) (void)BIO_reset((bio)); \
150  ERR_clear_error();
151 
152 /*
153  * ERRor messages
154  */
155 #define OSSL_ErrMsg() ERR_reason_error_string(ERR_get_error())
156 NORETURN(void ossl_raise(VALUE, const char *, ...));
157 VALUE ossl_exc_new(VALUE, const char *, ...);
158 
159 /*
160  * Verify callback
161  */
162 extern int ossl_verify_cb_idx;
163 
168 };
169 
171 int ossl_verify_cb(int, X509_STORE_CTX *);
172 
173 /*
174  * String to DER String
175  */
176 extern ID ossl_s_to_der;
179 
180 /*
181  * Debug
182  */
183 extern VALUE dOSSL;
184 
185 #if defined(HAVE_VA_ARGS_MACRO)
186 #define OSSL_Debug(...) do { \
187  if (dOSSL == Qtrue) { \
188  fprintf(stderr, "OSSL_DEBUG: "); \
189  fprintf(stderr, __VA_ARGS__); \
190  fprintf(stderr, " [%s:%d]\n", __FILE__, __LINE__); \
191  } \
192 } while (0)
193 
194 #define OSSL_Warning(fmt, ...) do { \
195  OSSL_Debug((fmt), ##__VA_ARGS__); \
196  rb_warning((fmt), ##__VA_ARGS__); \
197 } while (0)
198 
199 #define OSSL_Warn(fmt, ...) do { \
200  OSSL_Debug((fmt), ##__VA_ARGS__); \
201  rb_warn((fmt), ##__VA_ARGS__); \
202 } while (0)
203 #else
204 void ossl_debug(const char *, ...);
205 #define OSSL_Debug ossl_debug
206 #define OSSL_Warning rb_warning
207 #define OSSL_Warn rb_warn
208 #endif
209 
210 /*
211  * Include all parts
212  */
213 #include "openssl_missing.h"
214 #include "ruby_missing.h"
215 #include "ossl_asn1.h"
216 #include "ossl_bio.h"
217 #include "ossl_bn.h"
218 #include "ossl_cipher.h"
219 #include "ossl_config.h"
220 #include "ossl_digest.h"
221 #include "ossl_hmac.h"
222 #include "ossl_ns_spki.h"
223 #include "ossl_ocsp.h"
224 #include "ossl_pkcs12.h"
225 #include "ossl_pkcs7.h"
226 #include "ossl_pkcs5.h"
227 #include "ossl_pkey.h"
228 #include "ossl_rand.h"
229 #include "ossl_ssl.h"
230 #include "ossl_version.h"
231 #include "ossl_x509.h"
232 #include "ossl_engine.h"
233 
234 void Init_openssl(void);
235 
236 #if defined(__cplusplus)
237 }
238 #endif
239 
240 #endif /* _OSSL_H_ */
241 
242