Ruby  2.4.2p198(2017-09-14revision59899)
ossl_asn1.c
Go to the documentation of this file.
1 /*
2  * 'OpenSSL for Ruby' team members
3  * Copyright (C) 2003
4  * All rights reserved.
5  */
6 /*
7  * This program is licensed under the same licence as Ruby.
8  * (See the file 'LICENCE'.)
9  */
10 #include "ossl.h"
11 
12 static VALUE join_der(VALUE enumerable);
13 static VALUE ossl_asn1_decode0(unsigned char **pp, long length, long *offset,
14  int depth, int yield, long *num_read);
15 static VALUE ossl_asn1_initialize(int argc, VALUE *argv, VALUE self);
17 
18 /*
19  * DATE conversion
20  */
21 VALUE
22 asn1time_to_time(const ASN1_TIME *time)
23 {
24  struct tm tm;
25  VALUE argv[6];
26  int count;
27 
28  if (!time || !time->data) return Qnil;
29  memset(&tm, 0, sizeof(struct tm));
30 
31  switch (time->type) {
32  case V_ASN1_UTCTIME:
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,
35  &tm.tm_sec);
36 
37  if (count == 5) {
38  tm.tm_sec = 0;
39  } else if (count != 6) {
40  ossl_raise(rb_eTypeError, "bad UTCTIME format: \"%s\"",
41  time->data);
42  }
43  if (tm.tm_year < 69) {
44  tm.tm_year += 2000;
45  } else {
46  tm.tm_year += 1900;
47  }
48  break;
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,
52  &tm.tm_sec);
53  if (count == 5) {
54  tm.tm_sec = 0;
55  }
56  else if (count != 6) {
57  ossl_raise(rb_eTypeError, "bad GENERALIZEDTIME format: \"%s\"",
58  time->data);
59  }
60  break;
61  default:
62  rb_warning("unknown time format");
63  return Qnil;
64  }
65  argv[0] = INT2NUM(tm.tm_year);
66  argv[1] = INT2NUM(tm.tm_mon);
67  argv[2] = INT2NUM(tm.tm_mday);
68  argv[3] = INT2NUM(tm.tm_hour);
69  argv[4] = INT2NUM(tm.tm_min);
70  argv[5] = INT2NUM(tm.tm_sec);
71 
72  return rb_funcall2(rb_cTime, rb_intern("utc"), 6, argv);
73 }
74 
75 #if defined(HAVE_ASN1_TIME_ADJ)
76 void
77 ossl_time_split(VALUE time, time_t *sec, int *days)
78 {
79  VALUE num = rb_Integer(time);
80 
81  if (FIXNUM_P(num)) {
82  time_t t = FIX2LONG(num);
83  *sec = t % 86400;
84  *days = rb_long2int(t / 86400);
85  }
86  else {
87  *days = NUM2INT(rb_funcall(num, rb_intern("/"), 1, INT2FIX(86400)));
88  *sec = NUM2TIMET(rb_funcall(num, rb_intern("%"), 1, INT2FIX(86400)));
89  }
90 }
91 #else
92 time_t
94 {
95  return (time_t)NUM2TIMET(rb_Integer(time));
96 }
97 #endif
98 
99 /*
100  * STRING conversion
101  */
102 VALUE
103 asn1str_to_str(const ASN1_STRING *str)
104 {
105  return rb_str_new((const char *)str->data, str->length);
106 }
107 
108 /*
109  * ASN1_INTEGER conversions
110  */
111 VALUE
112 asn1integer_to_num(const ASN1_INTEGER *ai)
113 {
114  BIGNUM *bn;
115  VALUE num;
116 
117  if (!ai) {
118  ossl_raise(rb_eTypeError, "ASN1_INTEGER is NULL!");
119  }
120  if (ai->type == V_ASN1_ENUMERATED)
121  /* const_cast: workaround for old OpenSSL */
122  bn = ASN1_ENUMERATED_to_BN((ASN1_ENUMERATED *)ai, NULL);
123  else
124  bn = ASN1_INTEGER_to_BN(ai, NULL);
125 
126  if (!bn)
128  num = ossl_bn_new(bn);
129  BN_free(bn);
130 
131  return num;
132 }
133 
134 ASN1_INTEGER *
135 num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)
136 {
137  BIGNUM *bn;
138 
139  if (NIL_P(obj))
140  ossl_raise(rb_eTypeError, "Can't convert nil into Integer");
141 
142  bn = GetBNPtr(obj);
143 
144  if (!(ai = BN_to_ASN1_INTEGER(bn, ai)))
146 
147  return ai;
148 }
149 
150 /********/
151 /*
152  * ASN1 module
153  */
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)
159 
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))
165 
168 
172 
174 VALUE cASN1Boolean; /* BOOLEAN */
176 VALUE cASN1BitString; /* BIT STRING */
183 VALUE cASN1Null; /* NULL */
184 VALUE cASN1ObjectId; /* OBJECT IDENTIFIER */
186 VALUE cASN1Sequence, cASN1Set; /* CONSTRUCTIVE */
187 
191 static ID id_each;
192 
193 /*
194  * Ruby to ASN1 converters
195  */
196 static ASN1_BOOLEAN
198 {
199  if (NIL_P(obj))
200  ossl_raise(rb_eTypeError, "Can't convert nil into Boolean");
201 
202  return RTEST(obj) ? 0xff : 0x0;
203 }
204 
205 static ASN1_INTEGER*
207 {
208  return num_to_asn1integer(obj, NULL);
209 }
210 
211 static ASN1_BIT_STRING*
212 obj_to_asn1bstr(VALUE obj, long unused_bits)
213 {
214  ASN1_BIT_STRING *bstr;
215 
216  if(unused_bits < 0) unused_bits = 0;
217  StringValue(obj);
218  if(!(bstr = ASN1_BIT_STRING_new()))
220  ASN1_BIT_STRING_set(bstr, (unsigned char *)RSTRING_PTR(obj), RSTRING_LENINT(obj));
221  bstr->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); /* clear */
222  bstr->flags |= ASN1_STRING_FLAG_BITS_LEFT|(unused_bits&0x07);
223 
224  return bstr;
225 }
226 
227 static ASN1_STRING*
229 {
230  ASN1_STRING *str;
231 
232  StringValue(obj);
233  if(!(str = ASN1_STRING_new()))
235  ASN1_STRING_set(str, RSTRING_PTR(obj), RSTRING_LENINT(obj));
236 
237  return str;
238 }
239 
240 static ASN1_NULL*
242 {
243  ASN1_NULL *null;
244 
245  if(!NIL_P(obj))
246  ossl_raise(eASN1Error, "nil expected");
247  if(!(null = ASN1_NULL_new()))
249 
250  return null;
251 }
252 
253 static ASN1_OBJECT*
255 {
256  ASN1_OBJECT *a1obj;
257 
258  StringValueCStr(obj);
259  a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 0);
260  if(!a1obj) a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 1);
261  if(!a1obj) ossl_raise(eASN1Error, "invalid OBJECT ID %"PRIsVALUE, obj);
262 
263  return a1obj;
264 }
265 
266 static ASN1_UTCTIME *
268 {
269  time_t sec;
270  ASN1_UTCTIME *t;
271 
272 #if defined(HAVE_ASN1_TIME_ADJ)
273  int off_days;
274 
275  ossl_time_split(time, &sec, &off_days);
276  if (!(t = ASN1_UTCTIME_adj(NULL, sec, off_days, 0)))
277 #else
278  sec = time_to_time_t(time);
279  if (!(t = ASN1_UTCTIME_set(NULL, sec)))
280 #endif
282 
283  return t;
284 }
285 
286 static ASN1_GENERALIZEDTIME *
288 {
289  time_t sec;
290  ASN1_GENERALIZEDTIME *t;
291 
292 #if defined(HAVE_ASN1_TIME_ADJ)
293  int off_days;
294 
295  ossl_time_split(time, &sec, &off_days);
296  if (!(t = ASN1_GENERALIZEDTIME_adj(NULL, sec, off_days, 0)))
297 #else
298  sec = time_to_time_t(time);
299  if (!(t = ASN1_GENERALIZEDTIME_set(NULL, sec)))
300 #endif
302 
303  return t;
304 }
305 
306 static ASN1_STRING*
308 {
309  ASN1_STRING *a1str;
310  VALUE str;
311 
312  str = ossl_to_der(obj);
313  if(!(a1str = ASN1_STRING_new()))
315  ASN1_STRING_set(a1str, RSTRING_PTR(str), RSTRING_LENINT(str));
316 
317  return a1str;
318 }
319 
320 /*
321  * DER to Ruby converters
322  */
323 static VALUE
324 decode_bool(unsigned char* der, long length)
325 {
326  const unsigned char *p = der;
327 
328  if (length != 3)
329  ossl_raise(eASN1Error, "invalid length for BOOLEAN");
330  if (p[0] != 1 || p[1] != 1)
331  ossl_raise(eASN1Error, "invalid BOOLEAN");
332 
333  return p[2] ? Qtrue : Qfalse;
334 }
335 
336 static VALUE
337 decode_int(unsigned char* der, long length)
338 {
339  ASN1_INTEGER *ai;
340  const unsigned char *p;
341  VALUE ret;
342  int status = 0;
343 
344  p = der;
345  if(!(ai = d2i_ASN1_INTEGER(NULL, &p, length)))
348  (VALUE)ai, &status);
349  ASN1_INTEGER_free(ai);
350  if(status) rb_jump_tag(status);
351 
352  return ret;
353 }
354 
355 static VALUE
356 decode_bstr(unsigned char* der, long length, long *unused_bits)
357 {
358  ASN1_BIT_STRING *bstr;
359  const unsigned char *p;
360  long len;
361  VALUE ret;
362 
363  p = der;
364  if(!(bstr = d2i_ASN1_BIT_STRING(NULL, &p, length)))
366  len = bstr->length;
367  *unused_bits = 0;
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);
372 
373  return ret;
374 }
375 
376 static VALUE
377 decode_enum(unsigned char* der, long length)
378 {
379  ASN1_ENUMERATED *ai;
380  const unsigned char *p;
381  VALUE ret;
382  int status = 0;
383 
384  p = der;
385  if(!(ai = d2i_ASN1_ENUMERATED(NULL, &p, length)))
388  (VALUE)ai, &status);
389  ASN1_ENUMERATED_free(ai);
390  if(status) rb_jump_tag(status);
391 
392  return ret;
393 }
394 
395 static VALUE
396 decode_null(unsigned char* der, long length)
397 {
398  ASN1_NULL *null;
399  const unsigned char *p;
400 
401  p = der;
402  if(!(null = d2i_ASN1_NULL(NULL, &p, length)))
404  ASN1_NULL_free(null);
405 
406  return Qnil;
407 }
408 
409 static VALUE
410 decode_obj(unsigned char* der, long length)
411 {
412  ASN1_OBJECT *obj;
413  const unsigned char *p;
414  VALUE ret;
415  int nid;
416  BIO *bio;
417 
418  p = der;
419  if(!(obj = d2i_ASN1_OBJECT(NULL, &p, length)))
421  if((nid = OBJ_obj2nid(obj)) != NID_undef){
422  ASN1_OBJECT_free(obj);
423  ret = rb_str_new2(OBJ_nid2sn(nid));
424  }
425  else{
426  if(!(bio = BIO_new(BIO_s_mem()))){
427  ASN1_OBJECT_free(obj);
429  }
430  i2a_ASN1_OBJECT(bio, obj);
431  ASN1_OBJECT_free(obj);
432  ret = ossl_membio2str(bio);
433  }
434 
435  return ret;
436 }
437 
438 static VALUE
439 decode_time(unsigned char* der, long length)
440 {
441  ASN1_TIME *time;
442  const unsigned char *p;
443  VALUE ret;
444  int status = 0;
445 
446  p = der;
447  if(!(time = d2i_ASN1_TIME(NULL, &p, length)))
450  (VALUE)time, &status);
451  ASN1_TIME_free(time);
452  if(status) rb_jump_tag(status);
453 
454  return ret;
455 }
456 
457 static VALUE
458 decode_eoc(unsigned char *der, long length)
459 {
460  if (length != 2 || !(der[0] == 0x00 && der[1] == 0x00))
462 
463  return rb_str_new("", 0);
464 }
465 
466 /********/
467 
468 typedef struct {
469  const char *name;
472 
474  { "EOC", &cASN1EndOfContent, }, /* 0 */
475  { "BOOLEAN", &cASN1Boolean, }, /* 1 */
476  { "INTEGER", &cASN1Integer, }, /* 2 */
477  { "BIT_STRING", &cASN1BitString, }, /* 3 */
478  { "OCTET_STRING", &cASN1OctetString, }, /* 4 */
479  { "NULL", &cASN1Null, }, /* 5 */
480  { "OBJECT", &cASN1ObjectId, }, /* 6 */
481  { "OBJECT_DESCRIPTOR", NULL, }, /* 7 */
482  { "EXTERNAL", NULL, }, /* 8 */
483  { "REAL", NULL, }, /* 9 */
484  { "ENUMERATED", &cASN1Enumerated, }, /* 10 */
485  { "EMBEDDED_PDV", NULL, }, /* 11 */
486  { "UTF8STRING", &cASN1UTF8String, }, /* 12 */
487  { "RELATIVE_OID", NULL, }, /* 13 */
488  { "[UNIVERSAL 14]", NULL, }, /* 14 */
489  { "[UNIVERSAL 15]", NULL, }, /* 15 */
490  { "SEQUENCE", &cASN1Sequence, }, /* 16 */
491  { "SET", &cASN1Set, }, /* 17 */
492  { "NUMERICSTRING", &cASN1NumericString, }, /* 18 */
493  { "PRINTABLESTRING", &cASN1PrintableString, }, /* 19 */
494  { "T61STRING", &cASN1T61String, }, /* 20 */
495  { "VIDEOTEXSTRING", &cASN1VideotexString, }, /* 21 */
496  { "IA5STRING", &cASN1IA5String, }, /* 22 */
497  { "UTCTIME", &cASN1UTCTime, }, /* 23 */
498  { "GENERALIZEDTIME", &cASN1GeneralizedTime, }, /* 24 */
499  { "GRAPHICSTRING", &cASN1GraphicString, }, /* 25 */
500  { "ISO64STRING", &cASN1ISO64String, }, /* 26 */
501  { "GENERALSTRING", &cASN1GeneralString, }, /* 27 */
502  { "UNIVERSALSTRING", &cASN1UniversalString, }, /* 28 */
503  { "CHARACTER_STRING", NULL, }, /* 29 */
504  { "BMPSTRING", &cASN1BMPString, }, /* 30 */
505 };
506 
507 enum {ossl_asn1_info_size = (sizeof(ossl_asn1_info)/sizeof(ossl_asn1_info[0]))};
508 
510 
511 static int ossl_asn1_default_tag(VALUE obj);
512 
513 ASN1_TYPE*
515 {
516  ASN1_TYPE *ret;
517  VALUE value, rflag;
518  void *ptr;
519  void (*free_func)();
520  int tag, flag;
521 
522  tag = ossl_asn1_default_tag(obj);
523  value = ossl_asn1_get_value(obj);
524  switch(tag){
525  case V_ASN1_BOOLEAN:
526  ptr = (void*)(VALUE)obj_to_asn1bool(value);
527  free_func = NULL;
528  break;
529  case V_ASN1_INTEGER: /* FALLTHROUGH */
530  case V_ASN1_ENUMERATED:
531  ptr = obj_to_asn1int(value);
532  free_func = ASN1_INTEGER_free;
533  break;
534  case V_ASN1_BIT_STRING:
535  rflag = rb_attr_get(obj, sivUNUSED_BITS);
536  flag = NIL_P(rflag) ? -1 : NUM2INT(rflag);
537  ptr = obj_to_asn1bstr(value, flag);
538  free_func = ASN1_BIT_STRING_free;
539  break;
540  case V_ASN1_NULL:
541  ptr = obj_to_asn1null(value);
542  free_func = ASN1_NULL_free;
543  break;
544  case V_ASN1_OCTET_STRING: /* FALLTHROUGH */
545  case V_ASN1_UTF8STRING: /* FALLTHROUGH */
546  case V_ASN1_NUMERICSTRING: /* FALLTHROUGH */
547  case V_ASN1_PRINTABLESTRING: /* FALLTHROUGH */
548  case V_ASN1_T61STRING: /* FALLTHROUGH */
549  case V_ASN1_VIDEOTEXSTRING: /* FALLTHROUGH */
550  case V_ASN1_IA5STRING: /* FALLTHROUGH */
551  case V_ASN1_GRAPHICSTRING: /* FALLTHROUGH */
552  case V_ASN1_ISO64STRING: /* FALLTHROUGH */
553  case V_ASN1_GENERALSTRING: /* FALLTHROUGH */
554  case V_ASN1_UNIVERSALSTRING: /* FALLTHROUGH */
555  case V_ASN1_BMPSTRING:
556  ptr = obj_to_asn1str(value);
557  free_func = ASN1_STRING_free;
558  break;
559  case V_ASN1_OBJECT:
560  ptr = obj_to_asn1obj(value);
561  free_func = ASN1_OBJECT_free;
562  break;
563  case V_ASN1_UTCTIME:
564  ptr = obj_to_asn1utime(value);
565  free_func = ASN1_TIME_free;
566  break;
567  case V_ASN1_GENERALIZEDTIME:
568  ptr = obj_to_asn1gtime(value);
569  free_func = ASN1_TIME_free;
570  break;
571  case V_ASN1_SET: /* FALLTHROUGH */
572  case V_ASN1_SEQUENCE:
573  ptr = obj_to_asn1derstr(obj);
574  free_func = ASN1_STRING_free;
575  break;
576  default:
577  ossl_raise(eASN1Error, "unsupported ASN.1 type");
578  }
579  if(!(ret = OPENSSL_malloc(sizeof(ASN1_TYPE)))){
580  if(free_func) free_func(ptr);
581  ossl_raise(eASN1Error, "ASN1_TYPE alloc failure");
582  }
583  memset(ret, 0, sizeof(ASN1_TYPE));
584  ASN1_TYPE_set(ret, tag, ptr);
585 
586  return ret;
587 }
588 
589 static int
591 {
592  VALUE tmp_class, tag;
593 
594  tmp_class = CLASS_OF(obj);
595  while (!NIL_P(tmp_class)) {
596  tag = rb_hash_lookup(class_tag_map, tmp_class);
597  if (tag != Qnil)
598  return NUM2INT(tag);
599  tmp_class = rb_class_superclass(tmp_class);
600  }
601  ossl_raise(eASN1Error, "universal tag for %"PRIsVALUE" not found",
602  rb_obj_class(obj));
603 }
604 
605 static int
607 {
608  VALUE tag;
609 
610  tag = ossl_asn1_get_tag(obj);
611  if(NIL_P(tag))
612  ossl_raise(eASN1Error, "tag number not specified");
613 
614  return NUM2INT(tag);
615 }
616 
617 static int
619 {
620  VALUE s;
621 
622  s = ossl_asn1_get_tagging(obj);
623  if (NIL_P(s) || s == sym_IMPLICIT)
624  return 0;
625  else if (s == sym_EXPLICIT)
626  return 1;
627  else
628  ossl_raise(eASN1Error, "invalid tag default");
629 }
630 
631 static int
633 {
634  VALUE s;
635 
636  s = ossl_asn1_get_tag_class(obj);
637  if (NIL_P(s) || s == sym_UNIVERSAL)
638  return V_ASN1_UNIVERSAL;
639  else if (s == sym_APPLICATION)
640  return V_ASN1_APPLICATION;
641  else if (s == sym_CONTEXT_SPECIFIC)
642  return V_ASN1_CONTEXT_SPECIFIC;
643  else if (s == sym_PRIVATE)
644  return V_ASN1_PRIVATE;
645  else
646  ossl_raise(eASN1Error, "invalid tag class");
647 }
648 
649 static VALUE
651 {
652  if((tc & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
653  return sym_PRIVATE;
654  else if((tc & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
655  return sym_CONTEXT_SPECIFIC;
656  else if((tc & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
657  return sym_APPLICATION;
658  else
659  return sym_UNIVERSAL;
660 }
661 
662 /*
663  * call-seq:
664  * OpenSSL::ASN1::ASN1Data.new(value, tag, tag_class) => ASN1Data
665  *
666  * +value+: Please have a look at Constructive and Primitive to see how Ruby
667  * types are mapped to ASN.1 types and vice versa.
668  *
669  * +tag+: A +Number+ indicating the tag number.
670  *
671  * +tag_class+: A +Symbol+ indicating the tag class. Please cf. ASN1 for
672  * possible values.
673  *
674  * == Example
675  * asn1_int = OpenSSL::ASN1Data.new(42, 2, :UNIVERSAL) # => Same as OpenSSL::ASN1::Integer.new(42)
676  * tagged_int = OpenSSL::ASN1Data.new(42, 0, :CONTEXT_SPECIFIC) # implicitly 0-tagged INTEGER
677  */
678 static VALUE
679 ossl_asn1data_initialize(VALUE self, VALUE value, VALUE tag, VALUE tag_class)
680 {
681  if(!SYMBOL_P(tag_class))
682  ossl_raise(eASN1Error, "invalid tag class");
683  if (tag_class == sym_UNIVERSAL && NUM2INT(tag) > 31)
684  ossl_raise(eASN1Error, "tag number for Universal too large");
685  ossl_asn1_set_tag(self, tag);
686  ossl_asn1_set_value(self, value);
687  ossl_asn1_set_tag_class(self, tag_class);
689 
690  return self;
691 }
692 
693 static VALUE
695 {
697  StringValue(i);
698  rb_str_append(str, i);
699  return Qnil;
700 }
701 
702 static VALUE
703 join_der(VALUE enumerable)
704 {
705  VALUE str = rb_str_new(0, 0);
706  rb_block_call(enumerable, id_each, 0, 0, join_der_i, str);
707  return str;
708 }
709 
710 /*
711  * call-seq:
712  * asn1.to_der => DER-encoded String
713  *
714  * Encodes this ASN1Data into a DER-encoded String value. The result is
715  * DER-encoded except for the possibility of infinite length encodings.
716  * Infinite length encodings are not allowed in strict DER, so strictly
717  * speaking the result of such an encoding would be a BER-encoding.
718  */
719 static VALUE
721 {
722  VALUE value, der, inf_length;
723  int tag, tag_class, is_cons = 0;
724  long length;
725  unsigned char *p;
726 
727  value = ossl_asn1_get_value(self);
728  if(rb_obj_is_kind_of(value, rb_cArray)){
729  is_cons = 1;
730  value = join_der(value);
731  }
732  StringValue(value);
733 
734  tag = ossl_asn1_tag(self);
735  tag_class = ossl_asn1_tag_class(self);
736  inf_length = ossl_asn1_get_infinite_length(self);
737  if (inf_length == Qtrue) {
738  is_cons = 2;
739  }
740  if((length = ASN1_object_size(is_cons, RSTRING_LENINT(value), tag)) <= 0)
742  der = rb_str_new(0, length);
743  p = (unsigned char *)RSTRING_PTR(der);
744  ASN1_put_object(&p, is_cons, RSTRING_LENINT(value), tag, tag_class);
745  memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value));
746  p += RSTRING_LEN(value);
747  ossl_str_adjust(der, p);
748 
749  return der;
750 }
751 
752 static VALUE
753 int_ossl_asn1_decode0_prim(unsigned char **pp, long length, long hlen, int tag,
754  VALUE tc, long *num_read)
755 {
756  VALUE value, asn1data;
757  unsigned char *p;
758  long flag = 0;
759 
760  p = *pp;
761 
762  if(tc == sym_UNIVERSAL && tag < ossl_asn1_info_size) {
763  switch(tag){
764  case V_ASN1_EOC:
765  value = decode_eoc(p, hlen+length);
766  break;
767  case V_ASN1_BOOLEAN:
768  value = decode_bool(p, hlen+length);
769  break;
770  case V_ASN1_INTEGER:
771  value = decode_int(p, hlen+length);
772  break;
773  case V_ASN1_BIT_STRING:
774  value = decode_bstr(p, hlen+length, &flag);
775  break;
776  case V_ASN1_NULL:
777  value = decode_null(p, hlen+length);
778  break;
779  case V_ASN1_ENUMERATED:
780  value = decode_enum(p, hlen+length);
781  break;
782  case V_ASN1_OBJECT:
783  value = decode_obj(p, hlen+length);
784  break;
785  case V_ASN1_UTCTIME: /* FALLTHROUGH */
786  case V_ASN1_GENERALIZEDTIME:
787  value = decode_time(p, hlen+length);
788  break;
789  default:
790  /* use original value */
791  p += hlen;
792  value = rb_str_new((const char *)p, length);
793  break;
794  }
795  }
796  else {
797  p += hlen;
798  value = rb_str_new((const char *)p, length);
799  }
800 
801  *pp += hlen + length;
802  *num_read = hlen + length;
803 
804  if (tc == sym_UNIVERSAL &&
805  tag < ossl_asn1_info_size && ossl_asn1_info[tag].klass) {
806  VALUE klass = *ossl_asn1_info[tag].klass;
807  VALUE args[4];
808  args[0] = value;
809  args[1] = INT2NUM(tag);
810  args[2] = Qnil;
811  args[3] = tc;
812  asn1data = rb_obj_alloc(klass);
813  ossl_asn1_initialize(4, args, asn1data);
814  if(tag == V_ASN1_BIT_STRING){
815  rb_ivar_set(asn1data, sivUNUSED_BITS, LONG2NUM(flag));
816  }
817  }
818  else {
819  asn1data = rb_obj_alloc(cASN1Data);
820  ossl_asn1data_initialize(asn1data, value, INT2NUM(tag), tc);
821  }
822 
823  return asn1data;
824 }
825 
826 static VALUE
827 int_ossl_asn1_decode0_cons(unsigned char **pp, long max_len, long length,
828  long *offset, int depth, int yield, int j,
829  int tag, VALUE tc, long *num_read)
830 {
831  VALUE value, asn1data, ary;
832  int infinite;
833  long available_len, off = *offset;
834 
835  infinite = (j == 0x21);
836  ary = rb_ary_new();
837 
838  available_len = infinite ? max_len : length;
839  while (available_len > 0) {
840  long inner_read = 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;
844  rb_ary_push(ary, value);
845 
846  if (infinite &&
847  NUM2INT(ossl_asn1_get_tag(value)) == V_ASN1_EOC &&
849  break;
850  }
851  }
852 
853  if (tc == sym_UNIVERSAL) {
854  VALUE args[4];
855  int not_sequence_or_set;
856 
857  not_sequence_or_set = tag != V_ASN1_SEQUENCE && tag != V_ASN1_SET;
858 
859  if (not_sequence_or_set) {
860  if (infinite) {
861  asn1data = rb_obj_alloc(cASN1Constructive);
862  }
863  else {
864  ossl_raise(eASN1Error, "invalid non-infinite tag");
865  return Qnil;
866  }
867  }
868  else {
869  VALUE klass = *ossl_asn1_info[tag].klass;
870  asn1data = rb_obj_alloc(klass);
871  }
872  args[0] = ary;
873  args[1] = INT2NUM(tag);
874  args[2] = Qnil;
875  args[3] = tc;
876  ossl_asn1_initialize(4, args, asn1data);
877  }
878  else {
879  asn1data = rb_obj_alloc(cASN1Data);
880  ossl_asn1data_initialize(asn1data, ary, INT2NUM(tag), tc);
881  }
882 
883  if (infinite)
885  else
887 
888  *offset = off;
889  return asn1data;
890 }
891 
892 static VALUE
893 ossl_asn1_decode0(unsigned char **pp, long length, long *offset, int depth,
894  int yield, long *num_read)
895 {
896  unsigned char *start, *p;
897  const unsigned char *p0;
898  long len = 0, inner_read = 0, off = *offset, hlen;
899  int tag, tc, j;
900  VALUE asn1data, tag_class;
901 
902  p = *pp;
903  start = p;
904  p0 = p;
905  j = ASN1_get_object(&p0, &len, &tag, &tc, length);
906  p = (unsigned char *)p0;
907  if(j & 0x80) ossl_raise(eASN1Error, NULL);
908  if(len > length) ossl_raise(eASN1Error, "value is too short");
909  if((tc & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
910  tag_class = sym_PRIVATE;
911  else if((tc & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
912  tag_class = sym_CONTEXT_SPECIFIC;
913  else if((tc & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
914  tag_class = sym_APPLICATION;
915  else
916  tag_class = sym_UNIVERSAL;
917 
918  hlen = p - start;
919 
920  if(yield) {
921  VALUE arg = rb_ary_new();
922  rb_ary_push(arg, LONG2NUM(depth));
923  rb_ary_push(arg, LONG2NUM(*offset));
924  rb_ary_push(arg, LONG2NUM(hlen));
925  rb_ary_push(arg, LONG2NUM(len));
926  rb_ary_push(arg, (j & V_ASN1_CONSTRUCTED) ? Qtrue : Qfalse);
928  rb_ary_push(arg, INT2NUM(tag));
929  rb_yield(arg);
930  }
931 
932  if(j & V_ASN1_CONSTRUCTED) {
933  *pp += hlen;
934  off += hlen;
935  asn1data = int_ossl_asn1_decode0_cons(pp, length - hlen, len, &off, depth, yield, j, tag, tag_class, &inner_read);
936  inner_read += hlen;
937  }
938  else {
939  if ((j & 0x01) && (len == 0)) ossl_raise(eASN1Error, "Infinite length for primitive value");
940  asn1data = int_ossl_asn1_decode0_prim(pp, len, hlen, tag, tag_class, &inner_read);
941  off += hlen + len;
942  }
943  if (num_read)
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);
949  }
950 
951  *offset = off;
952  return asn1data;
953 }
954 
955 static void
956 int_ossl_decode_sanity_check(long len, long read, long offset)
957 {
958  if (len != 0 && (read != len || offset != len)) {
960  "Type mismatch. Total bytes read: %ld Bytes available: %ld Offset: %ld",
961  read, len, offset);
962  }
963 }
964 
965 /*
966  * call-seq:
967  * OpenSSL::ASN1.traverse(asn1) -> nil
968  *
969  * If a block is given, it prints out each of the elements encountered.
970  * Block parameters are (in that order):
971  * * depth: The recursion depth, plus one with each constructed value being encountered (Number)
972  * * offset: Current byte offset (Number)
973  * * header length: Combined length in bytes of the Tag and Length headers. (Number)
974  * * length: The overall remaining length of the entire data (Number)
975  * * constructed: Whether this value is constructed or not (Boolean)
976  * * tag_class: Current tag class (Symbol)
977  * * tag: The current tag (Number)
978  *
979  * == Example
980  * der = File.binread('asn1data.der')
981  * OpenSSL::ASN1.traverse(der) do | depth, offset, header_len, length, constructed, tag_class, tag|
982  * puts "Depth: #{depth} Offset: #{offset} Length: #{length}"
983  * puts "Header length: #{header_len} Tag: #{tag} Tag class: #{tag_class} Constructed: #{constructed}"
984  * end
985  */
986 static VALUE
988 {
989  unsigned char *p;
990  VALUE tmp;
991  long len, read = 0, offset = 0;
992 
993  obj = ossl_to_der_if_possible(obj);
994  tmp = rb_str_new4(StringValue(obj));
995  p = (unsigned char *)RSTRING_PTR(tmp);
996  len = RSTRING_LEN(tmp);
997  ossl_asn1_decode0(&p, len, &offset, 0, 1, &read);
998  RB_GC_GUARD(tmp);
999  int_ossl_decode_sanity_check(len, read, offset);
1000  return Qnil;
1001 }
1002 
1003 /*
1004  * call-seq:
1005  * OpenSSL::ASN1.decode(der) -> ASN1Data
1006  *
1007  * Decodes a BER- or DER-encoded value and creates an ASN1Data instance. +der+
1008  * may be a +String+ or any object that features a +#to_der+ method transforming
1009  * it into a BER-/DER-encoded +String+.
1010  *
1011  * == Example
1012  * der = File.binread('asn1data')
1013  * asn1 = OpenSSL::ASN1.decode(der)
1014  */
1015 static VALUE
1017 {
1018  VALUE ret;
1019  unsigned char *p;
1020  VALUE tmp;
1021  long len, read = 0, offset = 0;
1022 
1023  obj = ossl_to_der_if_possible(obj);
1024  tmp = rb_str_new4(StringValue(obj));
1025  p = (unsigned char *)RSTRING_PTR(tmp);
1026  len = RSTRING_LEN(tmp);
1027  ret = ossl_asn1_decode0(&p, len, &offset, 0, 0, &read);
1028  RB_GC_GUARD(tmp);
1029  int_ossl_decode_sanity_check(len, read, offset);
1030  return ret;
1031 }
1032 
1033 /*
1034  * call-seq:
1035  * OpenSSL::ASN1.decode_all(der) -> Array of ASN1Data
1036  *
1037  * Similar to +decode+ with the difference that +decode+ expects one
1038  * distinct value represented in +der+. +decode_all+ on the contrary
1039  * decodes a sequence of sequential BER/DER values lined up in +der+
1040  * and returns them as an array.
1041  *
1042  * == Example
1043  * ders = File.binread('asn1data_seq')
1044  * asn1_ary = OpenSSL::ASN1.decode_all(ders)
1045  */
1046 static VALUE
1048 {
1049  VALUE ary, val;
1050  unsigned char *p;
1051  long len, tmp_len = 0, read = 0, offset = 0;
1052  VALUE tmp;
1053 
1054  obj = ossl_to_der_if_possible(obj);
1055  tmp = rb_str_new4(StringValue(obj));
1056  p = (unsigned char *)RSTRING_PTR(tmp);
1057  len = RSTRING_LEN(tmp);
1058  tmp_len = len;
1059  ary = rb_ary_new();
1060  while (tmp_len > 0) {
1061  long tmp_read = 0;
1062  val = ossl_asn1_decode0(&p, tmp_len, &offset, 0, 0, &tmp_read);
1063  rb_ary_push(ary, val);
1064  read += tmp_read;
1065  tmp_len -= tmp_read;
1066  }
1067  RB_GC_GUARD(tmp);
1068  int_ossl_decode_sanity_check(len, read, offset);
1069  return ary;
1070 }
1071 
1072 /*
1073  * call-seq:
1074  * OpenSSL::ASN1::Primitive.new( value [, tag, tagging, tag_class ]) => Primitive
1075  *
1076  * +value+: is mandatory.
1077  *
1078  * +tag+: optional, may be specified for tagged values. If no +tag+ is
1079  * specified, the UNIVERSAL tag corresponding to the Primitive sub-class
1080  * is used by default.
1081  *
1082  * +tagging+: may be used as an encoding hint to encode a value either
1083  * explicitly or implicitly, see ASN1 for possible values.
1084  *
1085  * +tag_class+: if +tag+ and +tagging+ are +nil+ then this is set to
1086  * +:UNIVERSAL+ by default. If either +tag+ or +tagging+ are set then
1087  * +:CONTEXT_SPECIFIC+ is used as the default. For possible values please
1088  * cf. ASN1.
1089  *
1090  * == Example
1091  * int = OpenSSL::ASN1::Integer.new(42)
1092  * zero_tagged_int = OpenSSL::ASN1::Integer.new(42, 0, :IMPLICIT)
1093  * private_explicit_zero_tagged_int = OpenSSL::ASN1::Integer.new(42, 0, :EXPLICIT, :PRIVATE)
1094  */
1095 static VALUE
1097 {
1098  VALUE value, tag, tagging, tag_class;
1099 
1100  rb_scan_args(argc, argv, "13", &value, &tag, &tagging, &tag_class);
1101  if(argc > 1){
1102  if(NIL_P(tag))
1103  ossl_raise(eASN1Error, "must specify tag number");
1104  if(!NIL_P(tagging) && !SYMBOL_P(tagging))
1105  ossl_raise(eASN1Error, "invalid tagging method");
1106  if(NIL_P(tag_class)) {
1107  if (NIL_P(tagging))
1108  tag_class = sym_UNIVERSAL;
1109  else
1110  tag_class = sym_CONTEXT_SPECIFIC;
1111  }
1112  if(!SYMBOL_P(tag_class))
1113  ossl_raise(eASN1Error, "invalid tag class");
1114  if (tagging == sym_IMPLICIT && NUM2INT(tag) > 31)
1115  ossl_raise(eASN1Error, "tag number for Universal too large");
1116  }
1117  else{
1118  tag = INT2NUM(ossl_asn1_default_tag(self));
1119  tagging = Qnil;
1120  tag_class = sym_UNIVERSAL;
1121  }
1122  ossl_asn1_set_tag(self, tag);
1123  ossl_asn1_set_value(self, value);
1124  ossl_asn1_set_tagging(self, tagging);
1125  ossl_asn1_set_tag_class(self, tag_class);
1127 
1128  return self;
1129 }
1130 
1131 static VALUE
1133  VALUE tag, tagging, tag_class, value;
1134  tag = INT2NUM(ossl_asn1_default_tag(self));
1135  tagging = Qnil;
1136  tag_class = sym_UNIVERSAL;
1137  value = rb_str_new("", 0);
1138  ossl_asn1_set_tag(self, tag);
1139  ossl_asn1_set_value(self, value);
1140  ossl_asn1_set_tagging(self, tagging);
1141  ossl_asn1_set_tag_class(self, tag_class);
1143  return self;
1144 }
1145 
1146 /*
1147  * call-seq:
1148  * asn1.to_der => DER-encoded String
1149  *
1150  * See ASN1Data#to_der for details. *
1151  */
1152 static VALUE
1154 {
1155  ASN1_TYPE *asn1;
1156  int tn, tc, explicit;
1157  long len, reallen;
1158  unsigned char *buf, *p;
1159  VALUE str;
1160 
1161  tn = NUM2INT(ossl_asn1_get_tag(self));
1162  tc = ossl_asn1_tag_class(self);
1163  explicit = ossl_asn1_is_explicit(self);
1164  asn1 = ossl_asn1_get_asn1type(self);
1165 
1166  len = ASN1_object_size(1, i2d_ASN1_TYPE(asn1, NULL), tn);
1167  if(!(buf = OPENSSL_malloc(len))){
1168  ASN1_TYPE_free(asn1);
1169  ossl_raise(eASN1Error, "cannot alloc buffer");
1170  }
1171  p = buf;
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);
1177  } else {
1178  i2d_ASN1_TYPE(asn1, &p);
1179  *buf = tc | tn | (*buf & V_ASN1_CONSTRUCTED);
1180  }
1181  ASN1_TYPE_free(asn1);
1182  reallen = p - buf;
1183  assert(reallen <= len);
1184  str = ossl_buf2str((char *)buf, rb_long2int(reallen)); /* buf will be free in ossl_buf2str */
1185 
1186  return str;
1187 }
1188 
1189 /*
1190  * call-seq:
1191  * asn1.to_der => DER-encoded String
1192  *
1193  * See ASN1Data#to_der for details.
1194  */
1195 static VALUE
1197 {
1198  int tag, tn, tc, explicit, constructed = 1;
1199  int found_prim = 0, seq_len;
1200  long length;
1201  unsigned char *p;
1202  VALUE value, str, inf_length;
1203 
1204  tn = NUM2INT(ossl_asn1_get_tag(self));
1205  tc = ossl_asn1_tag_class(self);
1206  inf_length = ossl_asn1_get_infinite_length(self);
1207  if (inf_length == Qtrue) {
1208  VALUE ary, example;
1209  constructed = 2;
1210  if (rb_obj_class(self) == cASN1Sequence ||
1211  rb_obj_class(self) == cASN1Set) {
1212  tag = ossl_asn1_default_tag(self);
1213  }
1214  else { /* must be a constructive encoding of a primitive value */
1215  ary = ossl_asn1_get_value(self);
1216  if (!rb_obj_is_kind_of(ary, rb_cArray))
1217  ossl_raise(eASN1Error, "Constructive value must be an Array");
1218  /* Recursively descend until a primitive value is found.
1219  The overall value of the entire constructed encoding
1220  is of the type of the first primitive encoding to be
1221  found. */
1222  while (!found_prim){
1223  example = rb_ary_entry(ary, 0);
1224  if (rb_obj_is_kind_of(example, cASN1Primitive)){
1225  found_prim = 1;
1226  }
1227  else {
1228  /* example is another ASN1Constructive */
1229  if (!rb_obj_is_kind_of(example, cASN1Constructive)){
1230  ossl_raise(eASN1Error, "invalid constructed encoding");
1231  return Qnil; /* dummy */
1232  }
1233  ary = ossl_asn1_get_value(example);
1234  }
1235  }
1236  tag = ossl_asn1_default_tag(example);
1237  }
1238  }
1239  else {
1240  if (rb_obj_class(self) == cASN1Constructive)
1241  ossl_raise(eASN1Error, "Constructive shall only be used with infinite length");
1242  tag = ossl_asn1_default_tag(self);
1243  }
1244  explicit = ossl_asn1_is_explicit(self);
1245  value = join_der(ossl_asn1_get_value(self));
1246 
1247  seq_len = ASN1_object_size(constructed, RSTRING_LENINT(value), tag);
1248  length = ASN1_object_size(constructed, seq_len, tn);
1249  str = rb_str_new(0, length);
1250  p = (unsigned char *)RSTRING_PTR(str);
1251  if(tc == V_ASN1_UNIVERSAL)
1252  ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tn, tc);
1253  else{
1254  if(explicit){
1255  ASN1_put_object(&p, constructed, seq_len, tn, tc);
1256  ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tag, V_ASN1_UNIVERSAL);
1257  }
1258  else{
1259  ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tn, tc);
1260  }
1261  }
1262  memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value));
1263  p += RSTRING_LEN(value);
1264 
1265  /* In this case we need an additional EOC (one for the explicit part and
1266  * one for the Constructive itself. The EOC for the Constructive is
1267  * supplied by the user, but that for the "explicit wrapper" must be
1268  * added here.
1269  */
1270  if (explicit && inf_length == Qtrue) {
1271  ASN1_put_eoc(&p);
1272  }
1273  ossl_str_adjust(str, p);
1274 
1275  return str;
1276 }
1277 
1278 /*
1279  * call-seq:
1280  * asn1_ary.each { |asn1| block } => asn1_ary
1281  *
1282  * Calls <i>block</i> once for each element in +self+, passing that element
1283  * as parameter +asn1+. If no block is given, an enumerator is returned
1284  * instead.
1285  *
1286  * == Example
1287  * asn1_ary.each do |asn1|
1288  * puts asn1
1289  * end
1290  */
1291 static VALUE
1293 {
1294  rb_block_call(ossl_asn1_get_value(self), id_each, 0, 0, 0, 0);
1295 
1296  return self;
1297 }
1298 
1299 /*
1300  * call-seq:
1301  * OpenSSL::ASN1::ObjectId.register(object_id, short_name, long_name)
1302  *
1303  * This adds a new ObjectId to the internal tables. Where +object_id+ is the
1304  * numerical form, +short_name+ is the short name, and +long_name+ is the long
1305  * name.
1306  *
1307  * Returns +true+ if successful. Raises an OpenSSL::ASN1::ASN1Error if it fails.
1308  *
1309  */
1310 static VALUE
1312 {
1313  StringValueCStr(oid);
1314  StringValueCStr(sn);
1315  StringValueCStr(ln);
1316 
1317  if(!OBJ_create(RSTRING_PTR(oid), RSTRING_PTR(sn), RSTRING_PTR(ln)))
1319 
1320  return Qtrue;
1321 }
1322 
1323 /* Document-method: OpenSSL::ASN1::ObjectId#sn
1324  *
1325  * The short name of the ObjectId, as defined in <openssl/objects.h>.
1326  */
1327 /* Document-method: OpenSSL::ASN1::ObjectId#short_name
1328  *
1329  * +short_name+ is an alias to +sn+
1330  */
1331 static VALUE
1333 {
1334  VALUE val, ret = Qnil;
1335  int nid;
1336 
1337  val = ossl_asn1_get_value(self);
1338  if ((nid = OBJ_txt2nid(StringValueCStr(val))) != NID_undef)
1339  ret = rb_str_new2(OBJ_nid2sn(nid));
1340 
1341  return ret;
1342 }
1343 
1344 /* Document-method: OpenSSL::ASN1::ObjectId#ln
1345  *
1346  * The long name of the ObjectId, as defined in <openssl/objects.h>.
1347  */
1348 /* Document-method: OpenSSL::ASN1::ObjectId#long_name
1349  *
1350  * +long_name+ is an alias to +ln+
1351  */
1352 static VALUE
1354 {
1355  VALUE val, ret = Qnil;
1356  int nid;
1357 
1358  val = ossl_asn1_get_value(self);
1359  if ((nid = OBJ_txt2nid(StringValueCStr(val))) != NID_undef)
1360  ret = rb_str_new2(OBJ_nid2ln(nid));
1361 
1362  return ret;
1363 }
1364 
1365 /* Document-method: OpenSSL::ASN1::ObjectId#oid
1366  *
1367  * The object identifier as a +String+, e.g. "1.2.3.4.5"
1368  */
1369 static VALUE
1371 {
1372  VALUE val;
1373  ASN1_OBJECT *a1obj;
1374  char buf[128];
1375 
1376  val = ossl_asn1_get_value(self);
1377  a1obj = obj_to_asn1obj(val);
1378  OBJ_obj2txt(buf, sizeof(buf), a1obj, 1);
1379  ASN1_OBJECT_free(a1obj);
1380 
1381  return rb_str_new2(buf);
1382 }
1383 
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); }
1387 
1392 OSSL_ASN1_IMPL_FACTORY_METHOD(OctetString)
1394 OSSL_ASN1_IMPL_FACTORY_METHOD(NumericString)
1395 OSSL_ASN1_IMPL_FACTORY_METHOD(PrintableString)
1397 OSSL_ASN1_IMPL_FACTORY_METHOD(VideotexString)
1399 OSSL_ASN1_IMPL_FACTORY_METHOD(GraphicString)
1400 OSSL_ASN1_IMPL_FACTORY_METHOD(ISO64String)
1401 OSSL_ASN1_IMPL_FACTORY_METHOD(GeneralString)
1402 OSSL_ASN1_IMPL_FACTORY_METHOD(UniversalString)
1407 OSSL_ASN1_IMPL_FACTORY_METHOD(GeneralizedTime)
1410 OSSL_ASN1_IMPL_FACTORY_METHOD(EndOfContent)
1411 
1412 void
1414 {
1415  VALUE ary;
1416  int i;
1417 
1418 #if 0
1419  mOSSL = rb_define_module("OpenSSL");
1421 #endif
1422 
1423  sym_UNIVERSAL = ID2SYM(rb_intern_const("UNIVERSAL"));
1424  sym_CONTEXT_SPECIFIC = ID2SYM(rb_intern_const("CONTEXT_SPECIFIC"));
1425  sym_APPLICATION = ID2SYM(rb_intern_const("APPLICATION"));
1426  sym_PRIVATE = ID2SYM(rb_intern_const("PRIVATE"));
1427  sym_EXPLICIT = ID2SYM(rb_intern_const("EXPLICIT"));
1428  sym_IMPLICIT = ID2SYM(rb_intern_const("IMPLICIT"));
1429 
1430  sivVALUE = rb_intern("@value");
1431  sivTAG = rb_intern("@tag");
1432  sivTAGGING = rb_intern("@tagging");
1433  sivTAG_CLASS = rb_intern("@tag_class");
1434  sivINFINITE_LENGTH = rb_intern("@infinite_length");
1435  sivUNUSED_BITS = rb_intern("@unused_bits");
1436 
1437  /*
1438  * Document-module: OpenSSL::ASN1
1439  *
1440  * Abstract Syntax Notation One (or ASN.1) is a notation syntax to
1441  * describe data structures and is defined in ITU-T X.680. ASN.1 itself
1442  * does not mandate any encoding or parsing rules, but usually ASN.1 data
1443  * structures are encoded using the Distinguished Encoding Rules (DER) or
1444  * less often the Basic Encoding Rules (BER) described in ITU-T X.690. DER
1445  * and BER encodings are binary Tag-Length-Value (TLV) encodings that are
1446  * quite concise compared to other popular data description formats such
1447  * as XML, JSON etc.
1448  * ASN.1 data structures are very common in cryptographic applications,
1449  * e.g. X.509 public key certificates or certificate revocation lists
1450  * (CRLs) are all defined in ASN.1 and DER-encoded. ASN.1, DER and BER are
1451  * the building blocks of applied cryptography.
1452  * The ASN1 module provides the necessary classes that allow generation
1453  * of ASN.1 data structures and the methods to encode them using a DER
1454  * encoding. The decode method allows parsing arbitrary BER-/DER-encoded
1455  * data to a Ruby object that can then be modified and re-encoded at will.
1456  *
1457  * == ASN.1 class hierarchy
1458  *
1459  * The base class representing ASN.1 structures is ASN1Data. ASN1Data offers
1460  * attributes to read and set the +tag+, the +tag_class+ and finally the
1461  * +value+ of a particular ASN.1 item. Upon parsing, any tagged values
1462  * (implicit or explicit) will be represented by ASN1Data instances because
1463  * their "real type" can only be determined using out-of-band information
1464  * from the ASN.1 type declaration. Since this information is normally
1465  * known when encoding a type, all sub-classes of ASN1Data offer an
1466  * additional attribute +tagging+ that allows to encode a value implicitly
1467  * (+:IMPLICIT+) or explicitly (+:EXPLICIT+).
1468  *
1469  * === Constructive
1470  *
1471  * Constructive is, as its name implies, the base class for all
1472  * constructed encodings, i.e. those that consist of several values,
1473  * opposed to "primitive" encodings with just one single value.
1474  * Primitive values that are encoded with "infinite length" are typically
1475  * constructed (their values come in multiple chunks) and are therefore
1476  * represented by instances of Constructive. The value of an Constructive
1477  * is always an Array.
1478  *
1479  * ==== ASN1::Set and ASN1::Sequence
1480  *
1481  * The most common constructive encodings are SETs and SEQUENCEs, which is
1482  * why there are two sub-classes of Constructive representing each of
1483  * them.
1484  *
1485  * === Primitive
1486  *
1487  * This is the super class of all primitive values. Primitive
1488  * itself is not used when parsing ASN.1 data, all values are either
1489  * instances of a corresponding sub-class of Primitive or they are
1490  * instances of ASN1Data if the value was tagged implicitly or explicitly.
1491  * Please cf. Primitive documentation for details on sub-classes and
1492  * their respective mappings of ASN.1 data types to Ruby objects.
1493  *
1494  * == Possible values for +tagging+
1495  *
1496  * When constructing an ASN1Data object the ASN.1 type definition may
1497  * require certain elements to be either implicitly or explicitly tagged.
1498  * This can be achieved by setting the +tagging+ attribute manually for
1499  * sub-classes of ASN1Data. Use the symbol +:IMPLICIT+ for implicit
1500  * tagging and +:EXPLICIT+ if the element requires explicit tagging.
1501  *
1502  * == Possible values for +tag_class+
1503  *
1504  * It is possible to create arbitrary ASN1Data objects that also support
1505  * a PRIVATE or APPLICATION tag class. Possible values for the +tag_class+
1506  * attribute are:
1507  * * +:UNIVERSAL+ (the default for untagged values)
1508  * * +:CONTEXT_SPECIFIC+ (the default for tagged values)
1509  * * +:APPLICATION+
1510  * * +:PRIVATE+
1511  *
1512  * == Tag constants
1513  *
1514  * There is a constant defined for each universal tag:
1515  * * OpenSSL::ASN1::EOC (0)
1516  * * OpenSSL::ASN1::BOOLEAN (1)
1517  * * OpenSSL::ASN1::INTEGER (2)
1518  * * OpenSSL::ASN1::BIT_STRING (3)
1519  * * OpenSSL::ASN1::OCTET_STRING (4)
1520  * * OpenSSL::ASN1::NULL (5)
1521  * * OpenSSL::ASN1::OBJECT (6)
1522  * * OpenSSL::ASN1::ENUMERATED (10)
1523  * * OpenSSL::ASN1::UTF8STRING (12)
1524  * * OpenSSL::ASN1::SEQUENCE (16)
1525  * * OpenSSL::ASN1::SET (17)
1526  * * OpenSSL::ASN1::NUMERICSTRING (18)
1527  * * OpenSSL::ASN1::PRINTABLESTRING (19)
1528  * * OpenSSL::ASN1::T61STRING (20)
1529  * * OpenSSL::ASN1::VIDEOTEXSTRING (21)
1530  * * OpenSSL::ASN1::IA5STRING (22)
1531  * * OpenSSL::ASN1::UTCTIME (23)
1532  * * OpenSSL::ASN1::GENERALIZEDTIME (24)
1533  * * OpenSSL::ASN1::GRAPHICSTRING (25)
1534  * * OpenSSL::ASN1::ISO64STRING (26)
1535  * * OpenSSL::ASN1::GENERALSTRING (27)
1536  * * OpenSSL::ASN1::UNIVERSALSTRING (28)
1537  * * OpenSSL::ASN1::BMPSTRING (30)
1538  *
1539  * == UNIVERSAL_TAG_NAME constant
1540  *
1541  * An Array that stores the name of a given tag number. These names are
1542  * the same as the name of the tag constant that is additionally defined,
1543  * e.g. UNIVERSAL_TAG_NAME[2] = "INTEGER" and OpenSSL::ASN1::INTEGER = 2.
1544  *
1545  * == Example usage
1546  *
1547  * === Decoding and viewing a DER-encoded file
1548  * require 'openssl'
1549  * require 'pp'
1550  * der = File.binread('data.der')
1551  * asn1 = OpenSSL::ASN1.decode(der)
1552  * pp der
1553  *
1554  * === Creating an ASN.1 structure and DER-encoding it
1555  * require 'openssl'
1556  * version = OpenSSL::ASN1::Integer.new(1)
1557  * # Explicitly 0-tagged implies context-specific tag class
1558  * serial = OpenSSL::ASN1::Integer.new(12345, 0, :EXPLICIT, :CONTEXT_SPECIFIC)
1559  * name = OpenSSL::ASN1::PrintableString.new('Data 1')
1560  * sequence = OpenSSL::ASN1::Sequence.new( [ version, serial, name ] )
1561  * der = sequence.to_der
1562  */
1563  mASN1 = rb_define_module_under(mOSSL, "ASN1");
1564 
1565  /* Document-class: OpenSSL::ASN1::ASN1Error
1566  *
1567  * Generic error class for all errors raised in ASN1 and any of the
1568  * classes defined in it.
1569  */
1574  ary = rb_ary_new();
1575 
1576  /*
1577  * Array storing tag names at the tag's index.
1578  */
1579  rb_define_const(mASN1, "UNIVERSAL_TAG_NAME", ary);
1580  for(i = 0; i < ossl_asn1_info_size; i++){
1581  if(ossl_asn1_info[i].name[0] == '[') continue;
1582  rb_define_const(mASN1, ossl_asn1_info[i].name, INT2NUM(i));
1583  rb_ary_store(ary, i, rb_str_new2(ossl_asn1_info[i].name));
1584  }
1585 
1586  /* Document-class: OpenSSL::ASN1::ASN1Data
1587  *
1588  * The top-level class representing any ASN.1 object. When parsed by
1589  * ASN1.decode, tagged values are always represented by an instance
1590  * of ASN1Data.
1591  *
1592  * == The role of ASN1Data for parsing tagged values
1593  *
1594  * When encoding an ASN.1 type it is inherently clear what original
1595  * type (e.g. INTEGER, OCTET STRING etc.) this value has, regardless
1596  * of its tagging.
1597  * But opposed to the time an ASN.1 type is to be encoded, when parsing
1598  * them it is not possible to deduce the "real type" of tagged
1599  * values. This is why tagged values are generally parsed into ASN1Data
1600  * instances, but with a different outcome for implicit and explicit
1601  * tagging.
1602  *
1603  * === Example of a parsed implicitly tagged value
1604  *
1605  * An implicitly 1-tagged INTEGER value will be parsed as an
1606  * ASN1Data with
1607  * * +tag+ equal to 1
1608  * * +tag_class+ equal to +:CONTEXT_SPECIFIC+
1609  * * +value+ equal to a +String+ that carries the raw encoding
1610  * of the INTEGER.
1611  * This implies that a subsequent decoding step is required to
1612  * completely decode implicitly tagged values.
1613  *
1614  * === Example of a parsed explicitly tagged value
1615  *
1616  * An explicitly 1-tagged INTEGER value will be parsed as an
1617  * ASN1Data with
1618  * * +tag+ equal to 1
1619  * * +tag_class+ equal to +:CONTEXT_SPECIFIC+
1620  * * +value+ equal to an +Array+ with one single element, an
1621  * instance of OpenSSL::ASN1::Integer, i.e. the inner element
1622  * is the non-tagged primitive value, and the tagging is represented
1623  * in the outer ASN1Data
1624  *
1625  * == Example - Decoding an implicitly tagged INTEGER
1626  * int = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT) # implicit 0-tagged
1627  * seq = OpenSSL::ASN1::Sequence.new( [int] )
1628  * der = seq.to_der
1629  * asn1 = OpenSSL::ASN1.decode(der)
1630  * # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
1631  * # @infinite_length=false,
1632  * # @tag=16,
1633  * # @tag_class=:UNIVERSAL,
1634  * # @tagging=nil,
1635  * # @value=
1636  * # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
1637  * # @infinite_length=false,
1638  * # @tag=0,
1639  * # @tag_class=:CONTEXT_SPECIFIC,
1640  * # @value="\x01">]>
1641  * raw_int = asn1.value[0]
1642  * # manually rewrite tag and tag class to make it an UNIVERSAL value
1643  * raw_int.tag = OpenSSL::ASN1::INTEGER
1644  * raw_int.tag_class = :UNIVERSAL
1645  * int2 = OpenSSL::ASN1.decode(raw_int)
1646  * puts int2.value # => 1
1647  *
1648  * == Example - Decoding an explicitly tagged INTEGER
1649  * int = OpenSSL::ASN1::Integer.new(1, 0, :EXPLICIT) # explicit 0-tagged
1650  * seq = OpenSSL::ASN1::Sequence.new( [int] )
1651  * der = seq.to_der
1652  * asn1 = OpenSSL::ASN1.decode(der)
1653  * # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
1654  * # @infinite_length=false,
1655  * # @tag=16,
1656  * # @tag_class=:UNIVERSAL,
1657  * # @tagging=nil,
1658  * # @value=
1659  * # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
1660  * # @infinite_length=false,
1661  * # @tag=0,
1662  * # @tag_class=:CONTEXT_SPECIFIC,
1663  * # @value=
1664  * # [#<OpenSSL::ASN1::Integer:0x85bf308
1665  * # @infinite_length=false,
1666  * # @tag=2,
1667  * # @tag_class=:UNIVERSAL
1668  * # @tagging=nil,
1669  * # @value=1>]>]>
1670  * int2 = asn1.value[0].value[0]
1671  * puts int2.value # => 1
1672  */
1674  /*
1675  * Carries the value of a ASN.1 type.
1676  * Please confer Constructive and Primitive for the mappings between
1677  * ASN.1 data types and Ruby classes.
1678  */
1679  rb_attr(cASN1Data, rb_intern("value"), 1, 1, 0);
1680  /*
1681  * A +Number+ representing the tag number of this ASN1Data. Never +nil+.
1682  */
1683  rb_attr(cASN1Data, rb_intern("tag"), 1, 1, 0);
1684  /*
1685  * A +Symbol+ representing the tag class of this ASN1Data. Never +nil+.
1686  * See ASN1Data for possible values.
1687  */
1688  rb_attr(cASN1Data, rb_intern("tag_class"), 1, 1, 0);
1689  /*
1690  * Never +nil+. A +Boolean+ indicating whether the encoding was infinite
1691  * length (in the case of parsing) or whether an infinite length encoding
1692  * shall be used (in the encoding case).
1693  * In DER, every value has a finite length associated with it. But in
1694  * scenarios where large amounts of data need to be transferred it
1695  * might be desirable to have some kind of streaming support available.
1696  * For example, huge OCTET STRINGs are preferably sent in smaller-sized
1697  * chunks, each at a time.
1698  * This is possible in BER by setting the length bytes of an encoding
1699  * to zero and by this indicating that the following value will be
1700  * sent in chunks. Infinite length encodings are always constructed.
1701  * The end of such a stream of chunks is indicated by sending a EOC
1702  * (End of Content) tag. SETs and SEQUENCEs may use an infinite length
1703  * encoding, but also primitive types such as e.g. OCTET STRINGS or
1704  * BIT STRINGS may leverage this functionality (cf. ITU-T X.690).
1705  */
1706  rb_attr(cASN1Data, rb_intern("infinite_length"), 1, 1, 0);
1709 
1710  /* Document-class: OpenSSL::ASN1::Primitive
1711  *
1712  * The parent class for all primitive encodings. Attributes are the same as
1713  * for ASN1Data, with the addition of +tagging+.
1714  * Primitive values can never be infinite length encodings, thus it is not
1715  * possible to set the +infinite_length+ attribute for Primitive and its
1716  * sub-classes.
1717  *
1718  * == Primitive sub-classes and their mapping to Ruby classes
1719  * * OpenSSL::ASN1::EndOfContent <=> +value+ is always +nil+
1720  * * OpenSSL::ASN1::Boolean <=> +value+ is a +Boolean+
1721  * * OpenSSL::ASN1::Integer <=> +value+ is a +Number+
1722  * * OpenSSL::ASN1::BitString <=> +value+ is a +String+
1723  * * OpenSSL::ASN1::OctetString <=> +value+ is a +String+
1724  * * OpenSSL::ASN1::Null <=> +value+ is always +nil+
1725  * * OpenSSL::ASN1::Object <=> +value+ is a +String+
1726  * * OpenSSL::ASN1::Enumerated <=> +value+ is a +Number+
1727  * * OpenSSL::ASN1::UTF8String <=> +value+ is a +String+
1728  * * OpenSSL::ASN1::NumericString <=> +value+ is a +String+
1729  * * OpenSSL::ASN1::PrintableString <=> +value+ is a +String+
1730  * * OpenSSL::ASN1::T61String <=> +value+ is a +String+
1731  * * OpenSSL::ASN1::VideotexString <=> +value+ is a +String+
1732  * * OpenSSL::ASN1::IA5String <=> +value+ is a +String+
1733  * * OpenSSL::ASN1::UTCTime <=> +value+ is a +Time+
1734  * * OpenSSL::ASN1::GeneralizedTime <=> +value+ is a +Time+
1735  * * OpenSSL::ASN1::GraphicString <=> +value+ is a +String+
1736  * * OpenSSL::ASN1::ISO64String <=> +value+ is a +String+
1737  * * OpenSSL::ASN1::GeneralString <=> +value+ is a +String+
1738  * * OpenSSL::ASN1::UniversalString <=> +value+ is a +String+
1739  * * OpenSSL::ASN1::BMPString <=> +value+ is a +String+
1740  *
1741  * == OpenSSL::ASN1::BitString
1742  *
1743  * === Additional attributes
1744  * +unused_bits+: if the underlying BIT STRING's
1745  * length is a multiple of 8 then +unused_bits+ is 0. Otherwise
1746  * +unused_bits+ indicates the number of bits that are to be ignored in
1747  * the final octet of the +BitString+'s +value+.
1748  *
1749  * == OpenSSL::ASN1::ObjectId
1750  *
1751  * NOTE: While OpenSSL::ASN1::ObjectId.new will allocate a new ObjectId,
1752  * it is not typically allocated this way, but rather that are received from
1753  * parsed ASN1 encodings.
1754  *
1755  * === Additional attributes
1756  * * +sn+: the short name as defined in <openssl/objects.h>.
1757  * * +ln+: the long name as defined in <openssl/objects.h>.
1758  * * +oid+: the object identifier as a +String+, e.g. "1.2.3.4.5"
1759  * * +short_name+: alias for +sn+.
1760  * * +long_name+: alias for +ln+.
1761  *
1762  * == Examples
1763  * With the Exception of OpenSSL::ASN1::EndOfContent, each Primitive class
1764  * constructor takes at least one parameter, the +value+.
1765  *
1766  * === Creating EndOfContent
1767  * eoc = OpenSSL::ASN1::EndOfContent.new
1768  *
1769  * === Creating any other Primitive
1770  * prim = <class>.new(value) # <class> being one of the sub-classes except EndOfContent
1771  * prim_zero_tagged_implicit = <class>.new(value, 0, :IMPLICIT)
1772  * prim_zero_tagged_explicit = <class>.new(value, 0, :EXPLICIT)
1773  */
1775  /*
1776  * May be used as a hint for encoding a value either implicitly or
1777  * explicitly by setting it either to +:IMPLICIT+ or to +:EXPLICIT+.
1778  * +tagging+ is not set when a ASN.1 structure is parsed using
1779  * OpenSSL::ASN1.decode.
1780  */
1781  rb_attr(cASN1Primitive, rb_intern("tagging"), 1, 1, Qtrue);
1782  rb_undef_method(cASN1Primitive, "infinite_length=");
1785 
1786  /* Document-class: OpenSSL::ASN1::Constructive
1787  *
1788  * The parent class for all constructed encodings. The +value+ attribute
1789  * of a Constructive is always an +Array+. Attributes are the same as
1790  * for ASN1Data, with the addition of +tagging+.
1791  *
1792  * == SET and SEQUENCE
1793  *
1794  * Most constructed encodings come in the form of a SET or a SEQUENCE.
1795  * These encodings are represented by one of the two sub-classes of
1796  * Constructive:
1797  * * OpenSSL::ASN1::Set
1798  * * OpenSSL::ASN1::Sequence
1799  * Please note that tagged sequences and sets are still parsed as
1800  * instances of ASN1Data. Find further details on tagged values
1801  * there.
1802  *
1803  * === Example - constructing a SEQUENCE
1804  * int = OpenSSL::ASN1::Integer.new(1)
1805  * str = OpenSSL::ASN1::PrintableString.new('abc')
1806  * sequence = OpenSSL::ASN1::Sequence.new( [ int, str ] )
1807  *
1808  * === Example - constructing a SET
1809  * int = OpenSSL::ASN1::Integer.new(1)
1810  * str = OpenSSL::ASN1::PrintableString.new('abc')
1811  * set = OpenSSL::ASN1::Set.new( [ int, str ] )
1812  *
1813  * == Infinite length primitive values
1814  *
1815  * The only case where Constructive is used directly is for infinite
1816  * length encodings of primitive values. These encodings are always
1817  * constructed, with the contents of the +value+ +Array+ being either
1818  * UNIVERSAL non-infinite length partial encodings of the actual value
1819  * or again constructive encodings with infinite length (i.e. infinite
1820  * length primitive encodings may be constructed recursively with another
1821  * infinite length value within an already infinite length value). Each
1822  * partial encoding must be of the same UNIVERSAL type as the overall
1823  * encoding. The value of the overall encoding consists of the
1824  * concatenation of each partial encoding taken in sequence. The +value+
1825  * array of the outer infinite length value must end with a
1826  * OpenSSL::ASN1::EndOfContent instance.
1827  *
1828  * Please note that it is not possible to encode Constructive without
1829  * the +infinite_length+ attribute being set to +true+, use
1830  * OpenSSL::ASN1::Sequence or OpenSSL::ASN1::Set in these cases instead.
1831  *
1832  * === Example - Infinite length OCTET STRING
1833  * partial1 = OpenSSL::ASN1::OctetString.new("\x01")
1834  * partial2 = OpenSSL::ASN1::OctetString.new("\x02")
1835  * inf_octets = OpenSSL::ASN1::Constructive.new( [ partial1,
1836  * partial2,
1837  * OpenSSL::ASN1::EndOfContent.new ],
1838  * OpenSSL::ASN1::OCTET_STRING,
1839  * nil,
1840  * :UNIVERSAL )
1841  * # The real value of inf_octets is "\x01\x02", i.e. the concatenation
1842  * # of partial1 and partial2
1843  * inf_octets.infinite_length = true
1844  * der = inf_octets.to_der
1845  * asn1 = OpenSSL::ASN1.decode(der)
1846  * puts asn1.infinite_length # => true
1847  */
1850  /*
1851  * May be used as a hint for encoding a value either implicitly or
1852  * explicitly by setting it either to +:IMPLICIT+ or to +:EXPLICIT+.
1853  * +tagging+ is not set when a ASN.1 structure is parsed using
1854  * OpenSSL::ASN1.decode.
1855  */
1856  rb_attr(cASN1Constructive, rb_intern("tagging"), 1, 1, Qtrue);
1860 
1861 #define OSSL_ASN1_DEFINE_CLASS(name, super) \
1862 do{\
1863  cASN1##name = rb_define_class_under(mASN1, #name, cASN1##super);\
1864  rb_define_module_function(mASN1, #name, ossl_asn1_##name, -1);\
1865 }while(0)
1866 
1867  OSSL_ASN1_DEFINE_CLASS(Boolean, Primitive);
1868  OSSL_ASN1_DEFINE_CLASS(Integer, Primitive);
1869  OSSL_ASN1_DEFINE_CLASS(Enumerated, Primitive);
1870  OSSL_ASN1_DEFINE_CLASS(BitString, Primitive);
1871  OSSL_ASN1_DEFINE_CLASS(OctetString, Primitive);
1872  OSSL_ASN1_DEFINE_CLASS(UTF8String, Primitive);
1873  OSSL_ASN1_DEFINE_CLASS(NumericString, Primitive);
1874  OSSL_ASN1_DEFINE_CLASS(PrintableString, Primitive);
1875  OSSL_ASN1_DEFINE_CLASS(T61String, Primitive);
1876  OSSL_ASN1_DEFINE_CLASS(VideotexString, Primitive);
1877  OSSL_ASN1_DEFINE_CLASS(IA5String, Primitive);
1878  OSSL_ASN1_DEFINE_CLASS(GraphicString, Primitive);
1879  OSSL_ASN1_DEFINE_CLASS(ISO64String, Primitive);
1880  OSSL_ASN1_DEFINE_CLASS(GeneralString, Primitive);
1881  OSSL_ASN1_DEFINE_CLASS(UniversalString, Primitive);
1882  OSSL_ASN1_DEFINE_CLASS(BMPString, Primitive);
1883  OSSL_ASN1_DEFINE_CLASS(Null, Primitive);
1884  OSSL_ASN1_DEFINE_CLASS(ObjectId, Primitive);
1885  OSSL_ASN1_DEFINE_CLASS(UTCTime, Primitive);
1886  OSSL_ASN1_DEFINE_CLASS(GeneralizedTime, Primitive);
1887 
1888  OSSL_ASN1_DEFINE_CLASS(Sequence, Constructive);
1889  OSSL_ASN1_DEFINE_CLASS(Set, Constructive);
1890 
1891  OSSL_ASN1_DEFINE_CLASS(EndOfContent, Data);
1892 
1893 
1894  /* Document-class: OpenSSL::ASN1::ObjectId
1895  *
1896  * Represents the primitive object id for OpenSSL::ASN1
1897  */
1898 #if 0
1899  cASN1ObjectId = rb_define_class_under(mASN1, "ObjectId", cASN1Primitive); /* let rdoc know */
1900 #endif
1905  rb_define_alias(cASN1ObjectId, "short_name", "sn");
1906  rb_define_alias(cASN1ObjectId, "long_name", "ln");
1907  rb_attr(cASN1BitString, rb_intern("unused_bits"), 1, 1, 0);
1908 
1910 
1913  rb_hash_aset(class_tag_map, cASN1Boolean, INT2NUM(V_ASN1_BOOLEAN));
1914  rb_hash_aset(class_tag_map, cASN1Integer, INT2NUM(V_ASN1_INTEGER));
1915  rb_hash_aset(class_tag_map, cASN1BitString, INT2NUM(V_ASN1_BIT_STRING));
1916  rb_hash_aset(class_tag_map, cASN1OctetString, INT2NUM(V_ASN1_OCTET_STRING));
1917  rb_hash_aset(class_tag_map, cASN1Null, INT2NUM(V_ASN1_NULL));
1918  rb_hash_aset(class_tag_map, cASN1ObjectId, INT2NUM(V_ASN1_OBJECT));
1919  rb_hash_aset(class_tag_map, cASN1Enumerated, INT2NUM(V_ASN1_ENUMERATED));
1920  rb_hash_aset(class_tag_map, cASN1UTF8String, INT2NUM(V_ASN1_UTF8STRING));
1921  rb_hash_aset(class_tag_map, cASN1Sequence, INT2NUM(V_ASN1_SEQUENCE));
1922  rb_hash_aset(class_tag_map, cASN1Set, INT2NUM(V_ASN1_SET));
1923  rb_hash_aset(class_tag_map, cASN1NumericString, INT2NUM(V_ASN1_NUMERICSTRING));
1924  rb_hash_aset(class_tag_map, cASN1PrintableString, INT2NUM(V_ASN1_PRINTABLESTRING));
1925  rb_hash_aset(class_tag_map, cASN1T61String, INT2NUM(V_ASN1_T61STRING));
1926  rb_hash_aset(class_tag_map, cASN1VideotexString, INT2NUM(V_ASN1_VIDEOTEXSTRING));
1927  rb_hash_aset(class_tag_map, cASN1IA5String, INT2NUM(V_ASN1_IA5STRING));
1928  rb_hash_aset(class_tag_map, cASN1UTCTime, INT2NUM(V_ASN1_UTCTIME));
1929  rb_hash_aset(class_tag_map, cASN1GeneralizedTime, INT2NUM(V_ASN1_GENERALIZEDTIME));
1930  rb_hash_aset(class_tag_map, cASN1GraphicString, INT2NUM(V_ASN1_GRAPHICSTRING));
1931  rb_hash_aset(class_tag_map, cASN1ISO64String, INT2NUM(V_ASN1_ISO64STRING));
1932  rb_hash_aset(class_tag_map, cASN1GeneralString, INT2NUM(V_ASN1_GENERALSTRING));
1933  rb_hash_aset(class_tag_map, cASN1UniversalString, INT2NUM(V_ASN1_UNIVERSALSTRING));
1934  rb_hash_aset(class_tag_map, cASN1BMPString, INT2NUM(V_ASN1_BMPSTRING));
1936 
1937  id_each = rb_intern_const("each");
1938 }
VALUE rb_eStandardError
Definition: error.c:760
VALUE mOSSL
Definition: ossl.c:213
VALUE cASN1Data
Definition: ossl_asn1.c:169
static VALUE decode_enum(unsigned char *der, long length)
Definition: ossl_asn1.c:377
#define rb_str_new4
Definition: intern.h:859
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1196
#define ossl_asn1_get_tagging(o)
Definition: ossl_asn1.c:156
#define INT2NUM(x)
Definition: ruby.h:1538
VALUE cASN1ISO64String
Definition: ossl_asn1.c:181
static ASN1_NULL * obj_to_asn1null(VALUE obj)
Definition: ossl_asn1.c:241
#define NUM2INT(x)
Definition: ruby.h:684
int count
Definition: encoding.c:56
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)
Definition: ossl_asn1.c:827
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
#define CLASS_OF(v)
Definition: ruby.h:453
VALUE cASN1IA5String
Definition: ossl_asn1.c:180
#define Qtrue
Definition: ruby.h:437
static VALUE ossl_asn1obj_get_oid(VALUE self)
Definition: ossl_asn1.c:1370
#define ossl_str_adjust(str, p)
Definition: ossl.h:82
VALUE cASN1Constructive
Definition: ossl_asn1.c:171
VALUE cASN1PrintableString
Definition: ossl_asn1.c:178
VALUE mASN1
Definition: ossl_asn1.c:166
VALUE rb_eTypeError
Definition: error.c:762
static ASN1_UTCTIME * obj_to_asn1utime(VALUE time)
Definition: ossl_asn1.c:267
static VALUE ossl_asn1obj_s_register(VALUE self, VALUE oid, VALUE sn, VALUE ln)
Definition: ossl_asn1.c:1311
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:905
#define rb_long2int(n)
Definition: ruby.h:319
static VALUE ossl_asn1data_to_der(VALUE self)
Definition: ossl_asn1.c:720
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:821
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:891
VALUE cASN1BMPString
Definition: ossl_asn1.c:182
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:693
#define ossl_asn1_set_value(o, v)
Definition: ossl_asn1.c:160
#define RB_GC_GUARD(v)
Definition: ruby.h:552
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:690
void Init_ossl_asn1(void)
Definition: ossl_asn1.c:1413
static VALUE ossl_asn1data_initialize(VALUE self, VALUE value, VALUE tag, VALUE tag_class)
Definition: ossl_asn1.c:679
void rb_include_module(VALUE klass, VALUE module)
Definition: class.c:864
VALUE rb_block_call(VALUE, ID, int, const VALUE *, rb_block_call_func_t, VALUE)
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Definition: hash.c:867
VALUE asn1integer_to_num(const ASN1_INTEGER *ai)
Definition: ossl_asn1.c:112
#define assert(x)
Definition: dlmalloc.c:1176
#define ossl_asn1_get_tag_class(o)
Definition: ossl_asn1.c:157
static VALUE sym_APPLICATION
Definition: ossl_asn1.c:189
VALUE ossl_membio2str(BIO *bio)
Definition: ossl_bio.c:47
static ASN1_INTEGER * obj_to_asn1int(VALUE obj)
Definition: ossl_asn1.c:206
#define FIXNUM_P(f)
Definition: ruby.h:365
static VALUE decode_bstr(unsigned char *der, long length, long *unused_bits)
Definition: ossl_asn1.c:356
static VALUE ossl_asn1obj_get_ln(VALUE self)
Definition: ossl_asn1.c:1353
static const ossl_asn1_info_t ossl_asn1_info[]
Definition: ossl_asn1.c:473
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1533
#define ossl_asn1_set_tagging(o, v)
Definition: ossl_asn1.c:162
VALUE cASN1Null
Definition: ossl_asn1.c:183
VALUE cASN1OctetString
Definition: ossl_asn1.c:177
time_t time_to_time_t(VALUE time)
Definition: ossl_asn1.c:93
void rb_global_variable(VALUE *var)
Definition: gc.c:6203
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Definition: ruby.h:1830
VALUE cASN1BitString
Definition: ossl_asn1.c:176
static void int_ossl_decode_sanity_check(long len, long read, long offset)
Definition: ossl_asn1.c:956
VALUE ossl_to_der_if_possible(VALUE obj)
Definition: ossl.c:237
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1576
static ID sivVALUE
Definition: ossl_asn1.c:190
#define val
VALUE cASN1UniversalString
Definition: ossl_asn1.c:182
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1872
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:1138
static VALUE sym_CONTEXT_SPECIFIC
Definition: ossl_asn1.c:189
#define OSSL_ASN1_DEFINE_CLASS(name, super)
VALUE cASN1Boolean
Definition: ossl_asn1.c:174
VALUE rb_ary_new(void)
Definition: array.c:493
static ID sivTAGGING
Definition: ossl_asn1.c:190
#define GetBNPtr(obj)
Definition: ossl_bn.h:18
VALUE cASN1ObjectId
Definition: ossl_asn1.c:184
#define NIL_P(v)
Definition: ruby.h:451
static VALUE ossl_asn1_traverse(VALUE self, VALUE obj)
Definition: ossl_asn1.c:987
#define OSSL_ASN1_IMPL_FACTORY_METHOD(klass)
Definition: ossl_asn1.c:1384
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2734
void rb_ary_store(VALUE ary, long idx, VALUE val)
Definition: array.c:799
VALUE eOSSLError
Definition: ossl.c:218
int argc
Definition: ruby.c:183
#define Qfalse
Definition: ruby.h:436
VALUE rb_Integer(VALUE)
Definition: object.c:2736
static ID sivINFINITE_LENGTH
Definition: ossl_asn1.c:190
static VALUE ossl_asn1_decode(VALUE self, VALUE obj)
Definition: ossl_asn1.c:1016
static int ossl_asn1_tag(VALUE obj)
Definition: ossl_asn1.c:606
#define rb_str_new2
Definition: intern.h:857
VALUE rb_obj_alloc(VALUE)
Definition: object.c:1845
VALUE rb_class_superclass(VALUE)
Definition: object.c:1921
static ASN1_STRING * obj_to_asn1str(VALUE obj)
Definition: ossl_asn1.c:228
ASN1_TYPE * ossl_asn1_get_asn1type(VALUE obj)
Definition: ossl_asn1.c:514
VALUE cASN1Set
Definition: ossl_asn1.c:186
static VALUE decode_obj(unsigned char *der, long length)
Definition: ossl_asn1.c:410
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1758
#define RSTRING_LEN(str)
Definition: ruby.h:978
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1731
#define ossl_asn1_set_tag(o, v)
Definition: ossl_asn1.c:161
VALUE rb_yield(VALUE)
Definition: vm_eval.c:1020
static ID sivTAG
Definition: ossl_asn1.c:190
VALUE rb_mEnumerable
Definition: enum.c:18
static VALUE ossl_asn1cons_each(VALUE self)
Definition: ossl_asn1.c:1292
static VALUE decode_int(unsigned char *der, long length)
Definition: ossl_asn1.c:337
VALUE rb_hash_new(void)
Definition: hash.c:441
static VALUE class_tag_map
Definition: ossl_asn1.c:509
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1919
VALUE cASN1UTF8String
Definition: ossl_asn1.c:177
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1364
static VALUE ossl_asn1_decode0(unsigned char **pp, long length, long *offset, int depth, int yield, long *num_read)
Definition: ossl_asn1.c:893
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4309
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
static ASN1_OBJECT * obj_to_asn1obj(VALUE obj)
Definition: ossl_asn1.c:254
static VALUE ossl_asn1_initialize(int argc, VALUE *argv, VALUE self)
Definition: ossl_asn1.c:1096
#define Qnil
Definition: ruby.h:438
VALUE cASN1GraphicString
Definition: ossl_asn1.c:180
unsigned long VALUE
Definition: ruby.h:85
#define rb_funcall2
Definition: ruby.h:1770
static VALUE join_der(VALUE enumerable)
Definition: ossl_asn1.c:703
#define ossl_asn1_set_tag_class(o, v)
Definition: ossl_asn1.c:163
static ASN1_STRING * obj_to_asn1derstr(VALUE obj)
Definition: ossl_asn1.c:307
static int ossl_asn1_default_tag(VALUE obj)
Definition: ossl_asn1.c:590
void rb_jump_tag(int tag)
Definition: eval.c:788
static int ossl_asn1_tag_class(VALUE obj)
Definition: ossl_asn1.c:632
VALUE cASN1Integer
Definition: ossl_asn1.c:175
#define LONG2NUM(x)
Definition: ruby.h:1573
register unsigned int len
Definition: zonetab.h:51
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:790
#define StringValueCStr(v)
Definition: ruby.h:571
VALUE cASN1Primitive
Definition: ossl_asn1.c:170
static ID id_each
Definition: ossl_asn1.c:191
#define RSTRING_PTR(str)
Definition: ruby.h:982
static VALUE sym_UNIVERSAL
Definition: ossl_asn1.c:189
VALUE cASN1T61String
Definition: ossl_asn1.c:179
static VALUE ossl_asn1prim_to_der(VALUE self)
Definition: ossl_asn1.c:1153
#define ossl_asn1_get_infinite_length(o)
Definition: ossl_asn1.c:158
VALUE cASN1UTCTime
Definition: ossl_asn1.c:185
VALUE cASN1EndOfContent
Definition: ossl_asn1.c:173
VALUE asn1str_to_str(const ASN1_STRING *str)
Definition: ossl_asn1.c:103
#define INT2FIX(i)
Definition: ruby.h:232
static VALUE decode_time(unsigned char *der, long length)
Definition: ossl_asn1.c:439
static VALUE int_ossl_asn1_decode0_prim(unsigned char **pp, long length, long hlen, int tag, VALUE tc, long *num_read)
Definition: ossl_asn1.c:753
static ID sivTAG_CLASS
Definition: ossl_asn1.c:190
static VALUE join_der_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, str))
Definition: ossl_asn1.c:694
static VALUE ossl_asn1cons_to_der(VALUE self)
Definition: ossl_asn1.c:1196
static VALUE ossl_asn1_decode_all(VALUE self, VALUE obj)
Definition: ossl_asn1.c:1047
VALUE asn1time_to_time(const ASN1_TIME *time)
Definition: ossl_asn1.c:22
static VALUE decode_eoc(unsigned char *der, long length)
Definition: ossl_asn1.c:458
#define RTEST(v)
Definition: ruby.h:450
static ID sivUNUSED_BITS
Definition: ossl_asn1.c:190
VALUE ossl_buf2str(char *buf, int len)
Definition: ossl.c:101
#define ossl_asn1_get_value(o)
Definition: ossl_asn1.c:154
void ossl_raise(VALUE exc, const char *fmt,...)
Definition: ossl.c:278
static VALUE ossl_asn1_class2sym(int tc)
Definition: ossl_asn1.c:650
VALUE rb_cArray
Definition: array.c:25
VALUE cASN1Sequence
Definition: ossl_asn1.c:186
const char * name
Definition: ossl_asn1.c:469
const char * name
Definition: nkf.c:208
#define ID2SYM(x)
Definition: ruby.h:383
static VALUE decode_bool(unsigned char *der, long length)
Definition: ossl_asn1.c:324
#define ossl_asn1_get_tag(o)
Definition: ossl_asn1.c:155
VALUE ossl_bn_new(const BIGNUM *bn)
Definition: ossl_bn.c:63
VALUE cASN1Enumerated
Definition: ossl_asn1.c:175
#define ossl_asn1_set_infinite_length(o, v)
Definition: ossl_asn1.c:164
int nid
VALUE cASN1NumericString
Definition: ossl_asn1.c:178
VALUE cASN1VideotexString
Definition: ossl_asn1.c:179
static VALUE sym_EXPLICIT
Definition: ossl_asn1.c:188
void rb_warning(const char *fmt,...)
Definition: error.c:250
#define RSTRING_LENINT(str)
Definition: ruby.h:990
static ASN1_GENERALIZEDTIME * obj_to_asn1gtime(VALUE time)
Definition: ossl_asn1.c:287
#define rb_intern_const(str)
Definition: ruby.h:1756
#define memcpy(d, s, n)
Definition: ffi_common.h:55
static VALUE ossl_asn1obj_get_sn(VALUE self)
Definition: ossl_asn1.c:1332
VALUE rb_define_module(const char *name)
Definition: class.c:768
#define rb_intern(str)
static VALUE sym_PRIVATE
Definition: ossl_asn1.c:189
#define SYMBOL_P(x)
Definition: ruby.h:382
VALUE cASN1GeneralString
Definition: ossl_asn1.c:181
static ASN1_BOOLEAN obj_to_asn1bool(VALUE obj)
Definition: ossl_asn1.c:197
#define NULL
Definition: _sdbm.c:102
static VALUE ossl_asn1eoc_initialize(VALUE self)
Definition: ossl_asn1.c:1132
#define FIX2LONG(x)
Definition: ruby.h:363
RUBY_EXTERN VALUE rb_cTime
Definition: ruby.h:1910
static VALUE sym_IMPLICIT
Definition: ossl_asn1.c:188
static int ossl_asn1_is_explicit(VALUE obj)
Definition: ossl_asn1.c:618
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
VALUE cASN1GeneralizedTime
Definition: ossl_asn1.c:185
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2818
static VALUE decode_null(unsigned char *der, long length)
Definition: ossl_asn1.c:396
VALUE ossl_to_der(VALUE obj)
Definition: ossl.c:226
VALUE eASN1Error
Definition: ossl_asn1.c:167
static ASN1_BIT_STRING * obj_to_asn1bstr(VALUE obj, long unused_bits)
Definition: ossl_asn1.c:212
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1273
char ** argv
Definition: ruby.c:184
#define StringValue(v)
Definition: ruby.h:569
VALUE rb_obj_class(VALUE)
Definition: object.c:229
VALUE rb_str_new(const char *, long)
Definition: string.c:736
ASN1_INTEGER * num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)
Definition: ossl_asn1.c:135