12 #define NewX509Store(klass) \ 13 TypedData_Wrap_Struct((klass), &ossl_x509store_type, 0) 14 #define SetX509Store(obj, st) do { \ 16 ossl_raise(rb_eRuntimeError, "STORE wasn't initialized!"); \ 18 RTYPEDDATA_DATA(obj) = (st); \ 20 #define GetX509Store(obj, st) do { \ 21 TypedData_Get_Struct((obj), X509_STORE, &ossl_x509store_type, (st)); \ 23 ossl_raise(rb_eRuntimeError, "STORE wasn't initialized!"); \ 27 #define NewX509StCtx(klass) \ 28 TypedData_Wrap_Struct((klass), &ossl_x509stctx_type, 0) 29 #define SetX509StCtx(obj, ctx) do { \ 31 ossl_raise(rb_eRuntimeError, "STORE_CTX wasn't initialized!"); \ 33 RTYPEDDATA_DATA(obj) = (ctx); \ 35 #define GetX509StCtx(obj, ctx) do { \ 36 TypedData_Get_Struct((obj), X509_STORE_CTX, &ossl_x509stctx_type, (ctx)); \ 38 ossl_raise(rb_eRuntimeError, "STORE_CTX is out of scope!"); \ 45 static int stctx_ex_verify_cb_idx, store_ex_verify_cb_idx;
46 static VALUE ossl_x509stctx_new(X509_STORE_CTX *);
75 rb_warn(
"StoreContext initialization failure");
84 rb_warn(
"exception in verify_callback is ignored");
89 X509_STORE_CTX_set_error(ctx, X509_V_OK);
93 if (X509_STORE_CTX_get_error(ctx) == X509_V_OK)
94 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REJECTED);
109 ossl_x509store_free(
void *ptr)
111 X509_STORE_free(ptr);
115 "OpenSSL/X509/STORE",
117 0, ossl_x509store_free,
139 x509store_verify_cb(
int ok, X509_STORE_CTX *ctx)
143 proc = (
VALUE)X509_STORE_CTX_get_ex_data(ctx, stctx_ex_verify_cb_idx);
146 store_ex_verify_cb_idx);
154 ossl_x509store_alloc(
VALUE klass)
160 if((store = X509_STORE_new()) ==
NULL){
172 ossl_x509store_set_vfy_cb(
VALUE self,
VALUE cb)
197 #if !defined(HAVE_OPAQUE_OPENSSL) 199 store->ex_data.sk =
NULL;
201 X509_STORE_set_verify_cb(store, x509store_verify_cb);
202 ossl_x509store_set_vfy_cb(
self,
Qnil);
221 ossl_x509store_set_flags(
VALUE self,
VALUE flags)
227 X509_STORE_set_flags(store, f);
251 ossl_x509store_set_purpose(
VALUE self,
VALUE purpose)
257 X509_STORE_set_purpose(store, p);
267 ossl_x509store_set_trust(
VALUE self,
VALUE trust)
273 X509_STORE_set_trust(store, t);
285 ossl_x509store_set_time(
VALUE self,
VALUE time)
300 ossl_x509store_add_file(
VALUE self,
VALUE file)
311 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
313 if(X509_LOOKUP_load_file(lookup, path, X509_FILETYPE_PEM) != 1){
316 #if OPENSSL_VERSION_NUMBER < 0x10101000 || defined(LIBRESSL_VERSION_NUMBER) 336 ossl_x509store_add_path(
VALUE self,
VALUE dir)
347 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
349 if(X509_LOOKUP_add_dir(lookup, path, X509_FILETYPE_PEM) != 1){
368 ossl_x509store_set_default_paths(
VALUE self)
373 if (X509_STORE_set_default_paths(store) != 1){
387 ossl_x509store_add_cert(
VALUE self,
VALUE arg)
394 if (X509_STORE_add_cert(store, cert) != 1){
415 if (X509_STORE_add_crl(store, crl) != 1){
423 static VALUE ossl_x509stctx_get_err_string(
VALUE);
451 rb_iv_set(ctx,
"@verify_callback", proc);
454 rb_iv_set(
self,
"@error", ossl_x509stctx_get_err(ctx));
455 rb_iv_set(
self,
"@error_string", ossl_x509stctx_get_err_string(ctx));
456 rb_iv_set(
self,
"@chain", ossl_x509stctx_get_chain(ctx));
464 static void ossl_x509stctx_free(
void*);
468 "OpenSSL/X509/STORE_CTX",
470 0, ossl_x509stctx_free,
479 ossl_x509stctx_free(
void *ptr)
481 X509_STORE_CTX *ctx = ptr;
486 X509_STORE_CTX_free(ctx);
490 ossl_x509stctx_alloc(
VALUE klass)
496 if((ctx = X509_STORE_CTX_new()) ==
NULL){
505 ossl_x509stctx_new(X509_STORE_CTX *ctx)
527 VALUE store, cert, chain, t;
537 if(!
NIL_P(chain)) x509s = ossl_x509_ary2sk(chain);
538 if(X509_STORE_CTX_init(ctx, x509st, x509, x509s) != 1){
539 sk_X509_pop_free(x509s, X509_free);
543 ossl_x509stctx_set_time(
self, t);
555 ossl_x509stctx_verify(
VALUE self)
560 X509_STORE_CTX_set_ex_data(ctx, stctx_ex_verify_cb_idx,
561 (
void *)
rb_iv_get(
self,
"@verify_callback"));
563 switch (X509_verify_cert(ctx)) {
579 ossl_x509stctx_get_chain(
VALUE self)
591 if((num = sk_X509_num(chain)) < 0){
596 for(i = 0; i < num; i++) {
597 x509 = sk_X509_value(chain, i);
609 ossl_x509stctx_get_err(
VALUE self)
615 return INT2NUM(X509_STORE_CTX_get_error(ctx));
628 X509_STORE_CTX_set_error(ctx,
NUM2INT(err));
640 ossl_x509stctx_get_err_string(
VALUE self)
646 err = X509_STORE_CTX_get_error(ctx);
648 return rb_str_new2(X509_verify_cert_error_string(err));
656 ossl_x509stctx_get_err_depth(
VALUE self)
662 return INT2NUM(X509_STORE_CTX_get_error_depth(ctx));
670 ossl_x509stctx_get_curr_cert(
VALUE self)
684 ossl_x509stctx_get_curr_crl(
VALUE self)
690 crl = X509_STORE_CTX_get0_current_crl(ctx);
704 ossl_x509stctx_set_flags(
VALUE self,
VALUE flags)
706 X509_STORE_CTX *store;
710 X509_STORE_CTX_set_flags(store, f);
722 ossl_x509stctx_set_purpose(
VALUE self,
VALUE purpose)
724 X509_STORE_CTX *store;
728 X509_STORE_CTX_set_purpose(store, p);
738 ossl_x509stctx_set_trust(
VALUE self,
VALUE trust)
740 X509_STORE_CTX *store;
744 X509_STORE_CTX_set_trust(store, t);
756 ossl_x509stctx_set_time(
VALUE self,
VALUE time)
758 X509_STORE_CTX *store;
763 X509_STORE_CTX_set_time(store, 0, t);
781 stctx_ex_verify_cb_idx = X509_STORE_CTX_get_ex_new_index(0, (
void *)
"stctx_ex_verify_cb_idx", 0, 0, 0);
782 if (stctx_ex_verify_cb_idx < 0)
785 if (store_ex_verify_cb_idx < 0)
VALUE ossl_x509crl_new(X509_CRL *)
#define X509_STORE_CTX_get0_untrusted(x)
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,...)
#define X509_STORE_get_ex_data(x, idx)
#define RUBY_TYPED_FREE_IMMEDIATELY
int rb_block_given_p(void)
Determines if the current method is given a block.
VALUE rb_ary_push(VALUE ary, VALUE item)
#define GetX509Store(obj, st)
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
VALUE rb_iv_set(VALUE, const char *, VALUE)
VALUE rb_iv_get(VALUE, const char *)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_Integer(VALUE)
Equivalent to Kernel#Integer in Ruby.
void rb_undef_method(VALUE klass, const char *name)
#define X509_STORE_get_ex_new_index(l, p, newf, dupf, freef)
#define X509_STORE_CTX_get0_chain(ctx)
X509 * GetX509CertPtr(VALUE)
void ossl_clear_error(void)
#define GetX509StCtx(obj, ctx)
RUBY_EXTERN VALUE rb_cObject
STACK_OF(X509) *ossl_x509_ary2sk(VALUE)
void rb_attr(VALUE, ID, int, int, int)
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
#define SetX509Store(obj, st)
int ossl_verify_cb_call(VALUE proc, int ok, X509_STORE_CTX *ctx)
VALUE ossl_x509_new(X509 *)
#define NewX509StCtx(klass)
#define X509_STORE_set_ex_data(x, idx, data)
X509_STORE * GetX509StorePtr(VALUE obj)
#define X509_STORE_CTX_get0_store(x)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
#define NewX509Store(klass)
void Init_ossl_x509store(void)
VALUE rb_define_module_under(VALUE outer, const char *name)
#define StringValueCStr(v)
#define SetX509StCtx(obj, ctx)
VALUE rb_block_proc(void)
void rb_check_safe_obj(VALUE)
void ossl_raise(VALUE exc, const char *fmt,...)
X509 * DupX509CertPtr(VALUE)
#define RTYPEDDATA_DATA(v)
VALUE rb_define_module(const char *name)
#define X509_STORE_CTX_get0_cert(x)
X509_CRL * GetX509CRLPtr(VALUE)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)