14 int depth,
int yield,
long *num_read);
28 if (!time || !time->data)
return Qnil;
29 memset(&tm, 0,
sizeof(
struct tm));
33 count = sscanf((
const char *)time->data,
"%2d%2d%2d%2d%2d%2dZ",
34 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min,
39 }
else if (count != 6) {
43 if (tm.tm_year < 69) {
49 case V_ASN1_GENERALIZEDTIME:
50 count = sscanf((
const char *)time->data,
"%4d%2d%2d%2d%2d%2dZ",
51 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min,
56 else if (count != 6) {
75 #if defined(HAVE_ASN1_TIME_ADJ) 77 ossl_time_split(
VALUE time, time_t *sec,
int *days)
105 return rb_str_new((
const char *)str->data, str->length);
120 if (ai->type == V_ASN1_ENUMERATED)
122 bn = ASN1_ENUMERATED_to_BN((ASN1_ENUMERATED *)ai,
NULL);
124 bn = ASN1_INTEGER_to_BN(ai,
NULL);
144 if (!(ai = BN_to_ASN1_INTEGER(bn, ai)))
154 #define ossl_asn1_get_value(o) rb_attr_get((o),sivVALUE) 155 #define ossl_asn1_get_tag(o) rb_attr_get((o),sivTAG) 156 #define ossl_asn1_get_tagging(o) rb_attr_get((o),sivTAGGING) 157 #define ossl_asn1_get_tag_class(o) rb_attr_get((o),sivTAG_CLASS) 158 #define ossl_asn1_get_infinite_length(o) rb_attr_get((o),sivINFINITE_LENGTH) 160 #define ossl_asn1_set_value(o,v) rb_ivar_set((o),sivVALUE,(v)) 161 #define ossl_asn1_set_tag(o,v) rb_ivar_set((o),sivTAG,(v)) 162 #define ossl_asn1_set_tagging(o,v) rb_ivar_set((o),sivTAGGING,(v)) 163 #define ossl_asn1_set_tag_class(o,v) rb_ivar_set((o),sivTAG_CLASS,(v)) 164 #define ossl_asn1_set_infinite_length(o,v) rb_ivar_set((o),sivINFINITE_LENGTH,(v)) 202 return RTEST(obj) ? 0xff : 0x0;
211 static ASN1_BIT_STRING*
214 ASN1_BIT_STRING *bstr;
216 if(unused_bits < 0) unused_bits = 0;
218 if(!(bstr = ASN1_BIT_STRING_new()))
221 bstr->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
222 bstr->flags |= ASN1_STRING_FLAG_BITS_LEFT|(unused_bits&0x07);
233 if(!(str = ASN1_STRING_new()))
247 if(!(null = ASN1_NULL_new()))
260 if(!a1obj) a1obj = OBJ_txt2obj(
RSTRING_PTR(obj), 1);
266 static ASN1_UTCTIME *
272 #if defined(HAVE_ASN1_TIME_ADJ) 275 ossl_time_split(time, &sec, &off_days);
276 if (!(t = ASN1_UTCTIME_adj(
NULL, sec, off_days, 0)))
279 if (!(t = ASN1_UTCTIME_set(
NULL, sec)))
286 static ASN1_GENERALIZEDTIME *
290 ASN1_GENERALIZEDTIME *t;
292 #if defined(HAVE_ASN1_TIME_ADJ) 295 ossl_time_split(time, &sec, &off_days);
296 if (!(t = ASN1_GENERALIZEDTIME_adj(
NULL, sec, off_days, 0)))
299 if (!(t = ASN1_GENERALIZEDTIME_set(
NULL, sec)))
313 if(!(a1str = ASN1_STRING_new()))
326 const unsigned char *p = der;
330 if (p[0] != 1 || p[1] != 1)
340 const unsigned char *p;
345 if(!(ai = d2i_ASN1_INTEGER(
NULL, &p, length)))
349 ASN1_INTEGER_free(ai);
358 ASN1_BIT_STRING *bstr;
359 const unsigned char *p;
364 if(!(bstr = d2i_ASN1_BIT_STRING(
NULL, &p, length)))
368 if(bstr->flags & ASN1_STRING_FLAG_BITS_LEFT)
369 *unused_bits = bstr->flags & 0x07;
370 ret =
rb_str_new((
const char *)bstr->data, len);
371 ASN1_BIT_STRING_free(bstr);
380 const unsigned char *p;
385 if(!(ai = d2i_ASN1_ENUMERATED(
NULL, &p, length)))
389 ASN1_ENUMERATED_free(ai);
399 const unsigned char *p;
402 if(!(null = d2i_ASN1_NULL(
NULL, &p, length)))
404 ASN1_NULL_free(null);
413 const unsigned char *p;
419 if(!(obj = d2i_ASN1_OBJECT(
NULL, &p, length)))
421 if((nid = OBJ_obj2nid(obj)) != NID_undef){
422 ASN1_OBJECT_free(obj);
426 if(!(bio = BIO_new(BIO_s_mem()))){
427 ASN1_OBJECT_free(obj);
430 i2a_ASN1_OBJECT(bio, obj);
431 ASN1_OBJECT_free(obj);
442 const unsigned char *p;
447 if(!(time = d2i_ASN1_TIME(
NULL, &p, length)))
450 (
VALUE)time, &status);
451 ASN1_TIME_free(time);
460 if (length != 2 || !(der[0] == 0x00 && der[1] == 0x00))
481 {
"OBJECT_DESCRIPTOR",
NULL, },
482 {
"EXTERNAL",
NULL, },
485 {
"EMBEDDED_PDV",
NULL, },
487 {
"RELATIVE_OID",
NULL, },
488 {
"[UNIVERSAL 14]",
NULL, },
489 {
"[UNIVERSAL 15]",
NULL, },
503 {
"CHARACTER_STRING",
NULL, },
530 case V_ASN1_ENUMERATED:
532 free_func = ASN1_INTEGER_free;
534 case V_ASN1_BIT_STRING:
538 free_func = ASN1_BIT_STRING_free;
542 free_func = ASN1_NULL_free;
544 case V_ASN1_OCTET_STRING:
545 case V_ASN1_UTF8STRING:
546 case V_ASN1_NUMERICSTRING:
547 case V_ASN1_PRINTABLESTRING:
548 case V_ASN1_T61STRING:
549 case V_ASN1_VIDEOTEXSTRING:
550 case V_ASN1_IA5STRING:
551 case V_ASN1_GRAPHICSTRING:
552 case V_ASN1_ISO64STRING:
553 case V_ASN1_GENERALSTRING:
554 case V_ASN1_UNIVERSALSTRING:
555 case V_ASN1_BMPSTRING:
557 free_func = ASN1_STRING_free;
561 free_func = ASN1_OBJECT_free;
565 free_func = ASN1_TIME_free;
567 case V_ASN1_GENERALIZEDTIME:
569 free_func = ASN1_TIME_free;
572 case V_ASN1_SEQUENCE:
574 free_func = ASN1_STRING_free;
579 if(!(ret = OPENSSL_malloc(
sizeof(ASN1_TYPE)))){
580 if(free_func) free_func(ptr);
583 memset(ret, 0,
sizeof(ASN1_TYPE));
584 ASN1_TYPE_set(ret, tag, ptr);
592 VALUE tmp_class, tag;
595 while (!
NIL_P(tmp_class)) {
638 return V_ASN1_UNIVERSAL;
640 return V_ASN1_APPLICATION;
642 return V_ASN1_CONTEXT_SPECIFIC;
644 return V_ASN1_PRIVATE;
652 if((tc & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
654 else if((tc & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
656 else if((tc & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
722 VALUE value, der, inf_length;
723 int tag, tag_class, is_cons = 0;
737 if (inf_length ==
Qtrue) {
740 if((length = ASN1_object_size(is_cons,
RSTRING_LENINT(value), tag)) <= 0)
744 ASN1_put_object(&p, is_cons,
RSTRING_LENINT(value), tag, tag_class);
754 VALUE tc,
long *num_read)
756 VALUE value, asn1data;
773 case V_ASN1_BIT_STRING:
779 case V_ASN1_ENUMERATED:
786 case V_ASN1_GENERALIZEDTIME:
801 *pp += hlen + length;
802 *num_read = hlen + length;
814 if(tag == V_ASN1_BIT_STRING){
828 long *offset,
int depth,
int yield,
int j,
829 int tag,
VALUE tc,
long *num_read)
831 VALUE value, asn1data, ary;
833 long available_len, off = *offset;
835 infinite = (j == 0x21);
838 available_len = infinite ? max_len : length;
839 while (available_len > 0) {
841 value =
ossl_asn1_decode0(pp, available_len, &off, depth + 1, yield, &inner_read);
842 *num_read += inner_read;
843 available_len -= inner_read;
855 int not_sequence_or_set;
857 not_sequence_or_set = tag != V_ASN1_SEQUENCE && tag != V_ASN1_SET;
859 if (not_sequence_or_set) {
894 int yield,
long *num_read)
896 unsigned char *start, *p;
897 const unsigned char *p0;
898 long len = 0, inner_read = 0, off = *offset, hlen;
900 VALUE asn1data, tag_class;
905 j = ASN1_get_object(&p0, &len, &tag, &tc, length);
906 p = (
unsigned char *)p0;
909 if((tc & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
911 else if((tc & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
913 else if((tc & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
932 if(j & V_ASN1_CONSTRUCTED) {
944 *num_read = inner_read;
945 if (len != 0 && inner_read != hlen + len) {
947 "Type mismatch. Bytes read: %ld Bytes available: %ld",
948 inner_read, hlen + len);
958 if (len != 0 && (read != len || offset != len)) {
960 "Type mismatch. Total bytes read: %ld Bytes available: %ld Offset: %ld",
991 long len, read = 0, offset = 0;
1021 long len, read = 0, offset = 0;
1051 long len, tmp_len = 0, read = 0, offset = 0;
1060 while (tmp_len > 0) {
1065 tmp_len -= tmp_read;
1098 VALUE value, tag, tagging, tag_class;
1100 rb_scan_args(argc, argv,
"13", &value, &tag, &tagging, &tag_class);
1106 if(
NIL_P(tag_class)) {
1133 VALUE tag, tagging, tag_class, value;
1156 int tn, tc,
explicit;
1158 unsigned char *
buf, *p;
1166 len = ASN1_object_size(1, i2d_ASN1_TYPE(asn1,
NULL), tn);
1167 if(!(buf = OPENSSL_malloc(len))){
1168 ASN1_TYPE_free(asn1);
1172 if (tc == V_ASN1_UNIVERSAL) {
1173 i2d_ASN1_TYPE(asn1, &p);
1174 }
else if (
explicit) {
1175 ASN1_put_object(&p, 1, i2d_ASN1_TYPE(asn1,
NULL), tn, tc);
1176 i2d_ASN1_TYPE(asn1, &p);
1178 i2d_ASN1_TYPE(asn1, &p);
1179 *buf = tc | tn | (*buf & V_ASN1_CONSTRUCTED);
1181 ASN1_TYPE_free(asn1);
1198 int tag, tn, tc,
explicit, constructed = 1;
1199 int found_prim = 0, seq_len;
1202 VALUE value, str, inf_length;
1207 if (inf_length ==
Qtrue) {
1222 while (!found_prim){
1247 seq_len = ASN1_object_size(constructed,
RSTRING_LENINT(value), tag);
1248 length = ASN1_object_size(constructed, seq_len, tn);
1251 if(tc == V_ASN1_UNIVERSAL)
1255 ASN1_put_object(&p, constructed, seq_len, tn, tc);
1256 ASN1_put_object(&p, constructed,
RSTRING_LENINT(value), tag, V_ASN1_UNIVERSAL);
1270 if (
explicit && inf_length ==
Qtrue) {
1378 OBJ_obj2txt(buf,
sizeof(buf), a1obj, 1);
1379 ASN1_OBJECT_free(a1obj);
1384 #define OSSL_ASN1_IMPL_FACTORY_METHOD(klass) \ 1385 static VALUE ossl_asn1_##klass(int argc, VALUE *argv, VALUE self)\ 1386 { return rb_funcall3(cASN1##klass, rb_intern("new"), argc, argv); } 1581 if(ossl_asn1_info[i].
name[0] ==
'[')
continue;
1861 #define OSSL_ASN1_DEFINE_CLASS(name, super) \ 1863 cASN1##name = rb_define_class_under(mASN1, #name, cASN1##super);\ 1864 rb_define_module_function(mASN1, #name, ossl_asn1_##name, -1);\
static VALUE decode_enum(unsigned char *der, long length)
VALUE rb_ary_entry(VALUE ary, long offset)
#define ossl_asn1_get_tagging(o)
static ASN1_NULL * obj_to_asn1null(VALUE obj)
static VALUE int_ossl_asn1_decode0_cons(unsigned char **pp, long max_len, long length, long *offset, int depth, int yield, int j, int tag, VALUE tc, long *num_read)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
static VALUE ossl_asn1obj_get_oid(VALUE self)
#define ossl_str_adjust(str, p)
VALUE cASN1PrintableString
static ASN1_UTCTIME * obj_to_asn1utime(VALUE time)
static VALUE ossl_asn1obj_s_register(VALUE self, VALUE oid, VALUE sn, VALUE ln)
VALUE rb_ary_push(VALUE ary, VALUE item)
static VALUE ossl_asn1data_to_der(VALUE self)
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
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.
#define ossl_asn1_set_value(o, v)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
void Init_ossl_asn1(void)
static VALUE ossl_asn1data_initialize(VALUE self, VALUE value, VALUE tag, VALUE tag_class)
void rb_include_module(VALUE klass, VALUE module)
VALUE rb_block_call(VALUE, ID, int, const VALUE *, rb_block_call_func_t, VALUE)
VALUE rb_hash_lookup(VALUE hash, VALUE key)
VALUE asn1integer_to_num(const ASN1_INTEGER *ai)
#define ossl_asn1_get_tag_class(o)
static VALUE sym_APPLICATION
VALUE ossl_membio2str(BIO *bio)
static ASN1_INTEGER * obj_to_asn1int(VALUE obj)
static VALUE decode_bstr(unsigned char *der, long length, long *unused_bits)
static VALUE ossl_asn1obj_get_ln(VALUE self)
static const ossl_asn1_info_t ossl_asn1_info[]
void rb_undef_method(VALUE klass, const char *name)
#define ossl_asn1_set_tagging(o, v)
time_t time_to_time_t(VALUE time)
void rb_global_variable(VALUE *var)
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
static void int_ossl_decode_sanity_check(long len, long read, long offset)
VALUE ossl_to_der_if_possible(VALUE obj)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
VALUE cASN1UniversalString
RUBY_EXTERN VALUE rb_cObject
void rb_attr(VALUE, ID, int, int, int)
static VALUE sym_CONTEXT_SPECIFIC
#define OSSL_ASN1_DEFINE_CLASS(name, super)
static VALUE ossl_asn1_traverse(VALUE self, VALUE obj)
#define OSSL_ASN1_IMPL_FACTORY_METHOD(klass)
void rb_define_const(VALUE, const char *, VALUE)
void rb_ary_store(VALUE ary, long idx, VALUE val)
static ID sivINFINITE_LENGTH
static VALUE ossl_asn1_decode(VALUE self, VALUE obj)
static int ossl_asn1_tag(VALUE obj)
VALUE rb_obj_alloc(VALUE)
VALUE rb_class_superclass(VALUE)
static ASN1_STRING * obj_to_asn1str(VALUE obj)
ASN1_TYPE * ossl_asn1_get_asn1type(VALUE obj)
static VALUE decode_obj(unsigned char *der, long length)
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
#define ossl_asn1_set_tag(o, v)
static VALUE ossl_asn1cons_each(VALUE self)
static VALUE decode_int(unsigned char *der, long length)
static VALUE class_tag_map
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
VALUE rb_ivar_set(VALUE, ID, VALUE)
static VALUE ossl_asn1_decode0(unsigned char **pp, long length, long *offset, int depth, int yield, long *num_read)
unsigned char buf[MIME_BUF_SIZE]
static ASN1_OBJECT * obj_to_asn1obj(VALUE obj)
static VALUE ossl_asn1_initialize(int argc, VALUE *argv, VALUE self)
static VALUE join_der(VALUE enumerable)
#define ossl_asn1_set_tag_class(o, v)
static ASN1_STRING * obj_to_asn1derstr(VALUE obj)
static int ossl_asn1_default_tag(VALUE obj)
void rb_jump_tag(int tag)
static int ossl_asn1_tag_class(VALUE obj)
register unsigned int len
VALUE rb_define_module_under(VALUE outer, const char *name)
#define StringValueCStr(v)
static VALUE sym_UNIVERSAL
static VALUE ossl_asn1prim_to_der(VALUE self)
#define ossl_asn1_get_infinite_length(o)
VALUE asn1str_to_str(const ASN1_STRING *str)
static VALUE decode_time(unsigned char *der, long length)
static VALUE int_ossl_asn1_decode0_prim(unsigned char **pp, long length, long hlen, int tag, VALUE tc, long *num_read)
static VALUE join_der_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, str))
static VALUE ossl_asn1cons_to_der(VALUE self)
static VALUE ossl_asn1_decode_all(VALUE self, VALUE obj)
VALUE asn1time_to_time(const ASN1_TIME *time)
static VALUE decode_eoc(unsigned char *der, long length)
VALUE ossl_buf2str(char *buf, int len)
#define ossl_asn1_get_value(o)
void ossl_raise(VALUE exc, const char *fmt,...)
static VALUE ossl_asn1_class2sym(int tc)
static VALUE decode_bool(unsigned char *der, long length)
#define ossl_asn1_get_tag(o)
VALUE ossl_bn_new(const BIGNUM *bn)
#define ossl_asn1_set_infinite_length(o, v)
VALUE cASN1VideotexString
static VALUE sym_EXPLICIT
void rb_warning(const char *fmt,...)
#define RSTRING_LENINT(str)
static ASN1_GENERALIZEDTIME * obj_to_asn1gtime(VALUE time)
#define rb_intern_const(str)
static VALUE ossl_asn1obj_get_sn(VALUE self)
VALUE rb_define_module(const char *name)
static ASN1_BOOLEAN obj_to_asn1bool(VALUE obj)
static VALUE ossl_asn1eoc_initialize(VALUE self)
RUBY_EXTERN VALUE rb_cTime
static VALUE sym_IMPLICIT
static int ossl_asn1_is_explicit(VALUE obj)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE cASN1GeneralizedTime
VALUE rb_str_append(VALUE, VALUE)
static VALUE decode_null(unsigned char *der, long length)
VALUE ossl_to_der(VALUE obj)
static ASN1_BIT_STRING * obj_to_asn1bstr(VALUE obj, long unused_bits)
VALUE rb_attr_get(VALUE, ID)
VALUE rb_obj_class(VALUE)
VALUE rb_str_new(const char *, long)
ASN1_INTEGER * num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)