18 string2hex(
const unsigned char *
buf,
int buf_len,
char **hexbuf,
int *hexbuf_len)
20 static const char hex[]=
"0123456789abcdef";
21 int i,
len = 2 * buf_len;
23 if (buf_len < 0 || len < buf_len) {
32 if (!(*hexbuf = OPENSSL_malloc(len + 1))) {
35 for (i = 0; i < buf_len; i++) {
36 (*hexbuf)[2 *
i] = hex[((
unsigned char)buf[i]) >> 4];
37 (*hexbuf)[2 * i + 1] = hex[buf[
i] & 0x0f];
39 (*hexbuf)[2 *
i] =
'\0';
50 #define OSSL_IMPL_ARY2SK(name, type, expected_class, dup) \
52 ossl_##name##_ary2sk0(VALUE ary) \
59 Check_Type(ary, T_ARRAY); \
60 sk = sk_##type##_new_null(); \
61 if (!sk) ossl_raise(eOSSLError, NULL); \
63 for (i = 0; i < RARRAY_LEN(ary); i++) { \
64 val = rb_ary_entry(ary, i); \
65 if (!rb_obj_is_kind_of(val, expected_class)) { \
66 sk_##type##_pop_free(sk, type##_free); \
67 ossl_raise(eOSSLError, "object in array not" \
68 " of class ##type##"); \
71 sk_##type##_push(sk, x); \
77 ossl_protect_##name##_ary2sk(VALUE ary, int *status) \
79 return (STACK_OF(type)*)rb_protect( \
80 (VALUE(*)_((VALUE)))ossl_##name##_ary2sk0, \
86 ossl_##name##_ary2sk(VALUE ary) \
91 sk = ossl_protect_##name##_ary2sk(ary, &status); \
92 if (status) rb_jump_tag(status); \
98 #define OSSL_IMPL_SK2ARY(name, type) \
100 ossl_##name##_sk2ary(STACK_OF(type) *sk) \
107 OSSL_Debug("empty sk!"); \
110 num = sk_##type##_num(sk); \
112 OSSL_Debug("items in sk < -1???"); \
113 return rb_ary_new(); \
115 ary = rb_ary_new2(num); \
117 for (i=0; i<num; i++) { \
118 t = sk_##type##_value(sk, i); \
119 rb_ary_push(ary, ossl_##name##_new(t)); \
185 rb_warning(
"password must be longer than 4 bytes");
189 rb_warning(
"password must be shorter then %d bytes", max_len-1);
217 proc = (
VALUE)X509_STORE_CTX_get_ex_data(ctx, ossl_verify_cb_idx);
218 if ((
void*)proc == 0)
220 if ((
void*)proc == 0)
228 rb_warn(
"StoreContext initialization failure");
237 rb_warn(
"exception in verify_callback is ignored");
242 X509_STORE_CTX_set_error(ctx, X509_V_OK);
246 if (X509_STORE_CTX_get_error(ctx) == X509_V_OK) {
247 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
300 #ifdef HAVE_ERR_PEEK_LAST_ERROR
301 e = ERR_peek_last_error();
303 e = ERR_peek_error();
310 msg = ERR_error_string(e,
NULL);
312 msg = ERR_reason_error_string(e);
322 while ((e = ERR_get_error()) != 0){
323 rb_warn(
"error on stack: %s", ERR_error_string(e,
NULL));
369 while ((e = ERR_get_error()) != 0){
381 #if !defined(HAVE_VA_ARGS_MACRO)
387 if (dOSSL ==
Qtrue) {
388 fprintf(stderr,
"OSSL_DEBUG: ");
390 vfprintf(stderr, fmt, args);
392 fprintf(stderr,
" [CONTEXT N/A]\n");
421 if (dOSSL ==
Qtrue) {
422 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
423 fprintf(stderr,
"OSSL_DEBUG: IS NOW ON!\n");
424 }
else if (old ==
Qtrue) {
425 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_OFF);
426 fprintf(stderr,
"OSSL_DEBUG: IS NOW OFF!\n");
449 #ifdef HAVE_OPENSSL_FIPS
450 if (
RTEST(enabled)) {
451 int mode = FIPS_mode();
452 if(!mode && !FIPS_mode_set(1))
453 ossl_raise(eOSSLError,
"Turning on FIPS mode failed");
455 if(!FIPS_mode_set(0))
456 ossl_raise(eOSSLError,
"Turning off FIPS mode failed");
461 ossl_raise(eOSSLError,
"This version of OpenSSL does not support FIPS mode");
469 #include "../../thread_native.h"
475 if (mode & CRYPTO_LOCK) {
513 #ifdef HAVE_CRYPTO_THREADID_PTR
514 static void ossl_threadid_func(CRYPTO_THREADID *
id)
530 int num_locks = CRYPTO_num_locks();
532 if ((
unsigned)num_locks >= INT_MAX / (
int)
sizeof(
VALUE)) {
539 for (i = 0; i < num_locks; i++) {
543 #ifdef HAVE_CRYPTO_THREADID_PTR
544 CRYPTO_THREADID_set_callback(ossl_threadid_func);
1050 OpenSSL_add_ssl_algorithms();
1051 OpenSSL_add_all_algorithms();
1052 ERR_load_crypto_strings();
1053 SSL_load_error_strings();
1060 CONF_modules_unload(1);
1061 destroy_ui_method();
1064 CRYPTO_cleanup_all_ex_data();
1065 ERR_remove_state(0);
1099 #ifdef HAVE_OPENSSL_FIPS
1116 if ((ossl_verify_cb_idx = X509_STORE_CTX_get_ex_new_index(0, (
void *)
"ossl_verify_cb_idx", 0, 0, 0)) < 0)
1117 ossl_raise(eOSSLError,
"X509_STORE_CTX_get_ex_new_index");
1157 #if defined(OSSL_DEBUG)
RUBY_SYMBOL_EXPORT_BEGIN rb_nativethread_id_t rb_nativethread_self()
static VALUE ossl_str_new(int size)
void rb_nativethread_lock_unlock(rb_nativethread_lock_t *lock)
static void ossl_lock_callback(int mode, int type, const char *file, int line)
rb_funcall(memo->yielder, id_lshift, 1, rb_assoc_new(memo->prev_value, memo->prev_elts))
static void ossl_lock_unlock(int mode, rb_nativethread_lock_t *lock)
VALUE rb_str_new_cstr(const char *)
rb_nativethread_lock_t lock
VALUE rb_ary_push(VALUE ary, VALUE item)
#define OSSL_IMPL_SK2ARY(name, type)
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_raise(VALUE exc, const char *fmt,...)
static void Init_ossl_locks(void)
void rb_nativethread_lock_initialize(rb_nativethread_lock_t *lock)
pthread_mutex_t rb_nativethread_lock_t
void * X509_STORE_get_ex_data(X509_STORE *str, int idx)
#define SafeStringValue(v)
static VALUE ossl_make_error(VALUE exc, const char *fmt, va_list args)
static rb_nativethread_lock_t * ossl_locks
Stores locks needed for OpenSSL thread safety.
void ossl_debug(const char *fmt,...)
VALUE ossl_exc_new(VALUE exc, const char *fmt,...)
void rb_exc_raise(VALUE mesg)
static VALUE ossl_pem_passwd_cb0(VALUE flag)
static VALUE ossl_fips_mode_set(VALUE self, VALUE enabled)
VALUE ossl_x509stctx_clear_ptr(VALUE)
VALUE ossl_to_der_if_possible(VALUE obj)
int rb_block_given_p(void)
static void ossl_dyn_destroy_callback(struct CRYPTO_dynlock_value *l, const char *file, int line)
VALUE ossl_x509stctx_new(X509_STORE_CTX *)
void rb_define_const(VALUE, const char *, VALUE)
VALUE rb_str_cat2(VALUE, const char *)
unsigned char buf[MIME_BUF_SIZE]
static struct CRYPTO_dynlock_value * ossl_dyn_create_callback(const char *file, int line)
int PEM_def_callback(char *buf, int num, int w, void *key)
static VALUE ossl_debug_get(VALUE self)
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
static void ossl_dyn_lock_callback(int mode, struct CRYPTO_dynlock_value *l, const char *file, int line)
#define OSSL_IMPL_ARY2SK(name, type, expected_class, dup)
void rb_jump_tag(int tag)
static VALUE ossl_debug_set(VALUE self, VALUE val)
VALUE rb_vsprintf(const char *, va_list)
static unsigned long ossl_thread_id(void)
void rb_set_errinfo(VALUE err)
VALUE ossl_buf2str(char *buf, int len)
int rb_respond_to(VALUE, ID)
void ossl_raise(VALUE exc, const char *fmt,...)
void rb_nativethread_lock_lock(rb_nativethread_lock_t *lock)
#define RSTRING_LENINT(str)
X509 * DupX509CertPtr(VALUE)
VALUE rb_str_new(const char *, long)
int main(int argc, char **argv)
void Init_ossl_cipher(void)
void rb_global_variable(VALUE *)
int ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd)
void rb_warning(const char *fmt,...)
VALUE ossl_call_verify_cb_proc(struct ossl_verify_cb_args *args)
VALUE rb_define_module(const char *name)
void rb_warn(const char *fmt,...)
int string2hex(const unsigned char *buf, int buf_len, char **hexbuf, int *hexbuf_len)
int ossl_verify_cb(int ok, X509_STORE_CTX *ctx)
VALUE ossl_to_der(VALUE obj)
void rb_nativethread_lock_destroy(rb_nativethread_lock_t *lock)