17#define OSSL_IMPL_ARY2SK(name, type, expected_class, dup) \
19ossl_##name##_ary2sk0(VALUE ary) \
26 Check_Type(ary, T_ARRAY); \
27 sk = sk_##type##_new_null(); \
28 if (!sk) ossl_raise(eOSSLError, NULL); \
30 for (i = 0; i < RARRAY_LEN(ary); i++) { \
31 val = rb_ary_entry(ary, i); \
32 if (!rb_obj_is_kind_of(val, expected_class)) { \
33 sk_##type##_pop_free(sk, type##_free); \
34 ossl_raise(eOSSLError, "object in array not" \
35 " of class ##type##"); \
38 sk_##type##_push(sk, x); \
44ossl_protect_##name##_ary2sk(VALUE ary, int *status) \
46 return (STACK_OF(type)*)rb_protect( \
47 (VALUE (*)(VALUE))ossl_##name##_ary2sk0, \
53ossl_##name##_ary2sk(VALUE ary) \
58 sk = ossl_protect_##name##_ary2sk(ary, &status); \
59 if (status) rb_jump_tag(status); \
65#define OSSL_IMPL_SK2ARY(name, type) \
67ossl_##name##_sk2ary(const STACK_OF(type) *sk) \
74 OSSL_Debug("empty sk!"); \
77 num = sk_##type##_num(sk); \
79 OSSL_Debug("items in sk < -1???"); \
80 return rb_ary_new(); \
82 ary = rb_ary_new2(num); \
84 for (i=0; i<num; i++) { \
85 t = sk_##type##_value(sk, i); \
86 rb_ary_push(ary, ossl_##name##_new(t)); \
135 const char *hex =
"0123456789abcdef";
139 for (i = 0; i < inlen; i++) {
140 unsigned char p =
in[i];
142 out[i * 2 + 0] = hex[p >> 4];
143 out[i * 2 + 1] = hex[p & 0x0f];
167ossl_pem_passwd_cb0(
VALUE flag)
189 if (
len <= max_len) {
194 OSSL_Debug(
"passed data is not valid String???");
199 return PEM_def_callback(
buf, max_len, flag,
NULL);
209 pass =
rb_protect(ossl_pem_passwd_cb0, rflag, &status);
219 rb_warning(
"password must not be longer than %d bytes", max_len);
241static ID ossl_s_to_der;
266ossl_make_error(
VALUE exc,
const char *fmt, va_list args)
274 e = ERR_peek_last_error();
276 const char *msg = ERR_reason_error_string(e);
298 err = ossl_make_error(exc, fmt, args);
308 const char *
file, *data, *errstr;
311 while ((e = ERR_get_error_line_data(&
file, &line, &data, &flags))) {
312 errstr = ERR_error_string(e,
NULL);
316 if (flags & ERR_TXT_STRING) {
319 rb_warn(
"error on stack: %s (%s)", errstr, data);
322 rb_warn(
"error on stack: %s", errstr);
347 while ((e = ERR_get_error()) != 0){
359#if !defined(HAVE_VA_ARGS_MACRO)
361ossl_debug(
const char *fmt, ...)
366 fprintf(stderr,
"OSSL_DEBUG: ");
368 vfprintf(stderr, fmt, args);
370 fprintf(stderr,
" [CONTEXT N/A]\n");
380ossl_debug_get(
VALUE self)
405ossl_fips_mode_get(
VALUE self)
434 if (
RTEST(enabled)) {
435 int mode = FIPS_mode();
436 if(!
mode && !FIPS_mode_set(1))
439 if(!FIPS_mode_set(0))
450#if defined(OSSL_DEBUG)
451#if !defined(LIBRESSL_VERSION_NUMBER) && \
452 (OPENSSL_VERSION_NUMBER >= 0x10100000 && !defined(OPENSSL_NO_CRYPTO_MDEBUG) || \
453 defined(CRYPTO_malloc_debug_init))
465mem_check_start(
VALUE self)
467 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
494print_mem_leaks(
VALUE self)
496#if OPENSSL_VERSION_NUMBER >= 0x10100000
500#ifndef HAVE_RB_EXT_RACTOR_SAFE
506#if OPENSSL_VERSION_NUMBER >= 0x10100000
507 ret = CRYPTO_mem_leaks_fp(stderr);
512 CRYPTO_mem_leaks_fp(stderr);
519#if !defined(HAVE_OPENSSL_110_THREADING_API)
539 if (
mode & CRYPTO_LOCK) {
556ossl_dyn_create_callback(
const char *
file,
int line)
562 ossl_lock_init(dynlock);
569 ossl_lock_unlock(
mode, l);
579static void ossl_threadid_func(CRYPTO_THREADID *
id)
588ossl_lock_callback(
int mode,
int type,
const char *
file,
int line)
590 ossl_lock_unlock(
mode, &ossl_locks[
type]);
593static void Init_ossl_locks(
void)
596 int num_locks = CRYPTO_num_locks();
599 for (i = 0; i < num_locks; i++)
600 ossl_lock_init(&ossl_locks[i]);
602 CRYPTO_THREADID_set_callback(ossl_threadid_func);
603 CRYPTO_set_locking_callback(ossl_lock_callback);
604 CRYPTO_set_dynlock_create_callback(ossl_dyn_create_callback);
605 CRYPTO_set_dynlock_lock_callback(ossl_dyn_lock_callback);
606 CRYPTO_set_dynlock_destroy_callback(ossl_dyn_destroy_callback);
622ossl_crypto_fixed_length_secure_compare(
VALUE dummy,
VALUE str1,
VALUE str2)
624 const unsigned char *p1 = (
const unsigned char *)
StringValuePtr(str1);
625 const unsigned char *p2 = (
const unsigned char *)
StringValuePtr(str2);
633 switch (CRYPTO_memcmp(p1, p2, len1)) {
634 case 0:
return Qtrue;
1132#if HAVE_RB_EXT_RACTOR_SAFE
1147#if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000
1148 if (!OPENSSL_init_ssl(0,
NULL))
1151 OpenSSL_add_ssl_algorithms();
1152 OpenSSL_add_all_algorithms();
1153 ERR_load_crypto_strings();
1154 SSL_load_error_strings();
1172#if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000
1220#if !defined(HAVE_OPENSSL_110_THREADING_API)
1238#ifndef OPENSSL_NO_TS
1247#if defined(OSSL_DEBUG)
1251#if !defined(LIBRESSL_VERSION_NUMBER) && \
1252 (OPENSSL_VERSION_NUMBER >= 0x10100000 && !defined(OPENSSL_NO_CRYPTO_MDEBUG) || \
1253 defined(CRYPTO_malloc_debug_init))
1257#if defined(CRYPTO_malloc_debug_init)
1258 CRYPTO_malloc_debug_init();
1261#if defined(V_CRYPTO_MDEBUG_ALL)
1262 CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
1265#if OPENSSL_VERSION_NUMBER < 0x10100000
1273 for (i = 0; i <= 15; i++) {
1274 if (CRYPTO_get_ex_new_index(i, 0, (
void *)
"ossl-mdebug-dummy", 0, 0, 0) < 0)
1276 "class index %d failed", i);
VALUE rb_ary_push(VALUE ary, VALUE item)
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_module_function(klass, mid, func, arity)
Defines klass#mid and makes it a module function.
char str[HTML_ESCAPE_MAX_LEN+1]
#define RSTRING_LEN(string)
#define RSTRING_PTR(string)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_define_module(const char *name)
int rb_block_given_p(void)
Determines if the current method is given a block.
void rb_raise(VALUE exc, const char *fmt,...)
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
void rb_warn(const char *fmt,...)
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
void rb_warning(const char *fmt,...)
unsigned in(void *in_desc, z_const unsigned char **buf)
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
void rb_ext_ractor_safe(bool flag)
#define rb_str_new(str, len)
#define rb_str_new_cstr(str)
int rb_respond_to(VALUE, ID)
ID rb_intern(const char *)
void rb_define_const(VALUE, const char *, VALUE)
VALUE type(ANYARGS)
ANYARGS-ed function type.
void ossl_bin2hex(unsigned char *in, char *out, size_t inlen)
int ossl_pem_passwd_cb(char *buf, int max_len, int flag, void *pwd_)
VALUE ossl_get_errors(VALUE _)
#define OSSL_IMPL_ARY2SK(name, type, expected_class, dup)
VALUE ossl_buf2str(char *buf, int len)
VALUE ossl_str_new(const char *ptr, long len, int *pstate)
#define OSSL_IMPL_SK2ARY(name, type)
VALUE ossl_pem_passwd_value(VALUE pass)
VALUE ossl_to_der(VALUE obj)
VALUE ossl_to_der_if_possible(VALUE obj)
void ossl_raise(VALUE exc, const char *fmt,...)
void ossl_clear_error(void)
void Init_ossl_asn1(void)
void ossl_bn_ctx_free(void)
void Init_ossl_cipher(void)
void Init_ossl_config(void)
void Init_ossl_digest(void)
void Init_ossl_engine(void)
void Init_ossl_hmac(void)
void Init_ossl_ns_spki(void)
void Init_ossl_ocsp(void)
void Init_ossl_pkcs12(void)
void Init_ossl_pkcs7(void)
void Init_ossl_pkey(void)
void Init_ossl_rand(void)
void Init_ossl_x509(void)
X509 * DupX509CertPtr(VALUE)
#define StringValuePtr(v)
VALUE rb_vsprintf(const char *, va_list)
Stores locks needed for OpenSSL thread safety.
rb_nativethread_lock_t lock
rb_nativethread_id_t owner
rb_nativethread_id_t rb_nativethread_self()
void rb_nativethread_lock_lock(rb_nativethread_lock_t *lock)
void rb_nativethread_lock_unlock(rb_nativethread_lock_t *lock)
void rb_nativethread_lock_initialize(rb_nativethread_lock_t *lock)
void rb_nativethread_lock_destroy(rb_nativethread_lock_t *lock)