Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

ossl.h
Go to the documentation of this file.
1 /*
2 * $Id: ossl.h 36355 2012-07-10 13:57:11Z nobu $
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");
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 */
86 extern VALUE mOSSL;
87
88 /*
89 * Common Error Class
90 */
91 extern 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 (%s)! (Expected kind of %s)",\
99 rb_obj_classname(obj), rb_class2name(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 (%s)! (Expected instance of %s)",\
106 rb_obj_classname(obj), rb_class2name(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 */
126 int string2hex(const unsigned char *, int, char **, int *);
127
128 /*
129 * Data Conversion
130 */
131 STACK_OF(X509) *ossl_x509_ary2sk0(VALUE);
132 STACK_OF(X509) *ossl_x509_ary2sk(VALUE);
133 STACK_OF(X509) *ossl_protect_x509_ary2sk(VALUE,int*);
134 VALUE ossl_x509_sk2ary(STACK_OF(X509) *certs);
135 VALUE ossl_x509crl_sk2ary(STACK_OF(X509_CRL) *crl);
136 VALUE ossl_x509name_sk2ary(STACK_OF(X509_NAME) *names);
137 VALUE ossl_buf2str(char *buf, int len);
138 #define ossl_str_adjust(str, p) \
139 do{\
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 */
149 int 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())
162 NORETURN(void ossl_raise(VALUE, const char *, ...));
163 VALUE ossl_exc_new(VALUE, const char *, ...);
164
165 /*
166 * Verify callback
167 */
168 extern int ossl_verify_cb_idx;
169
170 struct ossl_verify_cb_args {
171 VALUE proc;
172 VALUE preverify_ok;
173 VALUE store_ctx;
174 };
175
176 VALUE ossl_call_verify_cb_proc(struct ossl_verify_cb_args *);
177 int ossl_verify_cb(int, X509_STORE_CTX *);
178
179 /*
180 * String to DER String
181 */
182 extern ID ossl_s_to_der;
183 VALUE ossl_to_der(VALUE);
184 VALUE ossl_to_der_if_possible(VALUE);
185
186 /*
187 * Debug
188 */
189 extern 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
210 void 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
240 void Init_openssl(void);
241
242 #if defined(__cplusplus)
243 }
244 #endif
245
246 #endif /* _OSSL_H_ */
247
mOSSL
VALUE mOSSL
Definition: ossl.c:259
dOSSL
VALUE dOSSL
Definition: ossl.c:374
ossl_s_to_der
ID ossl_s_to_der
Definition: ossl.c:269
ruby.h
names
st_table * names
Definition: encoding.c:53
ossl_x509name_sk2ary
VALUE ossl_x509name_sk2ary(STACK_OF(X509_NAME)*names)
openssl_missing.h
ossl_x509_sk2ary
int *VALUE ossl_x509_sk2ary(STACK_OF(X509)*certs)
ossl_verify_cb_idx
int ossl_verify_cb_idx
Definition: ossl.c:201
io.h
ossl_debug
void ossl_debug(const char *fmt,...)
Definition: ossl.c:378
ossl_asn1.h
ossl_exc_new
VALUE ossl_exc_new(VALUE exc, const char *fmt,...)
Definition: ossl.c:339
Init_openssl
void Init_openssl()
Definition: ossl.c:943
NORETURN
#define NORETURN(x)
Definition: ruby.h:31
STACK_OF
STACK_OF(X509)*ossl_x509_ary2sk0(VALUE)
ossl_pkcs12.h
thread.h
ossl_to_der_if_possible
VALUE ossl_to_der_if_possible(VALUE obj)
Definition: ossl.c:283
ossl_verify_cb_args::store_ctx
VALUE store_ctx
Definition: ossl.h:173
ossl_verify_cb_args::preverify_ok
VALUE preverify_ok
Definition: ossl.h:172
ossl_version.h
ID
unsigned long ID
Definition: ripper.y:105
ossl_bio.h
eOSSLError
VALUE eOSSLError
Definition: ossl.c:264
ossl_verify_cb_args
Definition: ossl.h:170
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
ossl_x509.h
ossl_bn.h
ossl_engine.h
ossl_cipher.h
ossl_verify_cb_args::proc
VALUE proc
Definition: ossl.h:171
ruby_missing.h
mX509
VALUE mX509
Definition: ossl_x509.c:13
ossl_x509crl_sk2ary
VALUE ossl_x509crl_sk2ary(STACK_OF(X509_CRL)*crl)
rb_define_module_under
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:637
ossl_config.h
ossl_buf2str
VALUE ossl_buf2str(char *buf, int len)
Definition: ossl.c:134
ossl_raise
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:328
ossl_pkcs5.h
ossl_ssl.h
ossl_ocsp.h
VALUE
unsigned long VALUE
Definition: ripper.y:104
ossl_pem_passwd_cb
int ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd)
Definition: ossl.c:162
ossl_digest.h
ossl_call_verify_cb_proc
VALUE ossl_call_verify_cb_proc(struct ossl_verify_cb_args *args)
Definition: ossl.c:204
rb_define_module
VALUE rb_define_module(const char *name)
Definition: class.c:617
ossl_ns_spki.h
string2hex
int string2hex(const unsigned char *buf, int buf_len, char **hexbuf, int *hexbuf_len)
Definition: ossl.c:18
ossl_hmac.h
ossl_verify_cb
int ossl_verify_cb(int ok, X509_STORE_CTX *ctx)
Definition: ossl.c:211
ossl_to_der
VALUE ossl_to_der(VALUE obj)
Definition: ossl.c:272
ossl_pkcs7.h
ossl_rand.h
ossl_pkey.h
len
size_t len
Definition: tcltklib.c:3568