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