Ruby  2.1.4p265(2014-10-27revision48166)
ossl_asn1.c
Go to the documentation of this file.
1 /*
2  * $Id: ossl_asn1.c 46214 2014-05-28 14:44:07Z nagachika $
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 
22 static VALUE join_der(VALUE enumerable);
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
32 asn1time_to_time(ASN1_TIME *time)
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
721 ossl_asn1data_initialize(VALUE self, VALUE value, VALUE tag, VALUE tag_class)
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
737 {
739  StringValue(i);
740  rb_str_append(str, i);
741  return Qnil;
742 }
743 
744 static VALUE
745 join_der(VALUE enumerable)
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  volatile 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  int_ossl_decode_sanity_check(len, read, offset);
1041  return Qnil;
1042 }
1043 
1044 /*
1045  * call-seq:
1046  * OpenSSL::ASN1.decode(der) -> ASN1Data
1047  *
1048  * Decodes a BER- or DER-encoded value and creates an ASN1Data instance. +der+
1049  * may be a +String+ or any object that features a +#to_der+ method transforming
1050  * it into a BER-/DER-encoded +String+.
1051  *
1052  * == Example
1053  * der = File.binread('asn1data')
1054  * asn1 = OpenSSL::ASN1.decode(der)
1055  */
1056 static VALUE
1058 {
1059  VALUE ret;
1060  unsigned char *p;
1061  volatile VALUE tmp;
1062  long len, read = 0, offset = 0;
1063 
1064  obj = ossl_to_der_if_possible(obj);
1065  tmp = rb_str_new4(StringValue(obj));
1066  p = (unsigned char *)RSTRING_PTR(tmp);
1067  len = RSTRING_LEN(tmp);
1068  ret = ossl_asn1_decode0(&p, len, &offset, 0, 0, &read);
1069  int_ossl_decode_sanity_check(len, read, offset);
1070  return ret;
1071 }
1072 
1073 /*
1074  * call-seq:
1075  * OpenSSL::ASN1.decode_all(der) -> Array of ASN1Data
1076  *
1077  * Similar to +decode+ with the difference that +decode+ expects one
1078  * distinct value represented in +der+. +decode_all+ on the contrary
1079  * decodes a sequence of sequential BER/DER values lined up in +der+
1080  * and returns them as an array.
1081  *
1082  * == Example
1083  * ders = File.binread('asn1data_seq')
1084  * asn1_ary = OpenSSL::ASN1.decode_all(ders)
1085  */
1086 static VALUE
1088 {
1089  VALUE ary, val;
1090  unsigned char *p;
1091  long len, tmp_len = 0, read = 0, offset = 0;
1092  volatile VALUE tmp;
1093 
1094  obj = ossl_to_der_if_possible(obj);
1095  tmp = rb_str_new4(StringValue(obj));
1096  p = (unsigned char *)RSTRING_PTR(tmp);
1097  len = RSTRING_LEN(tmp);
1098  tmp_len = len;
1099  ary = rb_ary_new();
1100  while (tmp_len > 0) {
1101  long tmp_read = 0;
1102  val = ossl_asn1_decode0(&p, tmp_len, &offset, 0, 0, &tmp_read);
1103  rb_ary_push(ary, val);
1104  read += tmp_read;
1105  tmp_len -= tmp_read;
1106  }
1107  int_ossl_decode_sanity_check(len, read, offset);
1108  return ary;
1109 }
1110 
1111 /*
1112  * call-seq:
1113  * OpenSSL::ASN1::Primitive.new( value [, tag, tagging, tag_class ]) => Primitive
1114  *
1115  * +value+: is mandatory.
1116  *
1117  * +tag+: optional, may be specified for tagged values. If no +tag+ is
1118  * specified, the UNIVERSAL tag corresponding to the Primitive sub-class
1119  * is used by default.
1120  *
1121  * +tagging+: may be used as an encoding hint to encode a value either
1122  * explicitly or implicitly, see ASN1 for possible values.
1123  *
1124  * +tag_class+: if +tag+ and +tagging+ are +nil+ then this is set to
1125  * +:UNIVERSAL+ by default. If either +tag+ or +tagging+ are set then
1126  * +:CONTEXT_SPECIFIC+ is used as the default. For possible values please
1127  * cf. ASN1.
1128  *
1129  * == Example
1130  * int = OpenSSL::ASN1::Integer.new(42)
1131  * zero_tagged_int = OpenSSL::ASN1::Integer.new(42, 0, :IMPLICIT)
1132  * private_explicit_zero_tagged_int = OpenSSL::ASN1::Integer.new(42, 0, :EXPLICIT, :PRIVATE)
1133  */
1134 static VALUE
1136 {
1137  VALUE value, tag, tagging, tag_class;
1138 
1139  rb_scan_args(argc, argv, "13", &value, &tag, &tagging, &tag_class);
1140  if(argc > 1){
1141  if(NIL_P(tag))
1142  ossl_raise(eASN1Error, "must specify tag number");
1143  if(!NIL_P(tagging) && !SYMBOL_P(tagging))
1144  ossl_raise(eASN1Error, "invalid tagging method");
1145  if(NIL_P(tag_class)) {
1146  if (NIL_P(tagging))
1147  tag_class = ID2SYM(sUNIVERSAL);
1148  else
1149  tag_class = ID2SYM(sCONTEXT_SPECIFIC);
1150  }
1151  if(!SYMBOL_P(tag_class))
1152  ossl_raise(eASN1Error, "invalid tag class");
1153  if(!NIL_P(tagging) && SYM2ID(tagging) == sIMPLICIT && NUM2INT(tag) > 31)
1154  ossl_raise(eASN1Error, "tag number for Universal too large");
1155  }
1156  else{
1157  tag = INT2NUM(ossl_asn1_default_tag(self));
1158  tagging = Qnil;
1159  tag_class = ID2SYM(sUNIVERSAL);
1160  }
1161  ossl_asn1_set_tag(self, tag);
1162  ossl_asn1_set_value(self, value);
1163  ossl_asn1_set_tagging(self, tagging);
1164  ossl_asn1_set_tag_class(self, tag_class);
1166 
1167  return self;
1168 }
1169 
1170 static VALUE
1172  VALUE tag, tagging, tag_class, value;
1173  tag = INT2NUM(ossl_asn1_default_tag(self));
1174  tagging = Qnil;
1175  tag_class = ID2SYM(sUNIVERSAL);
1176  value = rb_str_new("", 0);
1177  ossl_asn1_set_tag(self, tag);
1178  ossl_asn1_set_value(self, value);
1179  ossl_asn1_set_tagging(self, tagging);
1180  ossl_asn1_set_tag_class(self, tag_class);
1182  return self;
1183 }
1184 
1185 static int
1186 ossl_i2d_ASN1_TYPE(ASN1_TYPE *a, unsigned char **pp)
1187 {
1188 #if OPENSSL_VERSION_NUMBER < 0x00907000L
1189  if(!a) return 0;
1190  if(a->type == V_ASN1_BOOLEAN)
1191  return i2d_ASN1_BOOLEAN(a->value.boolean, pp);
1192 #endif
1193  return i2d_ASN1_TYPE(a, pp);
1194 }
1195 
1196 static void
1197 ossl_ASN1_TYPE_free(ASN1_TYPE *a)
1198 {
1199 #if OPENSSL_VERSION_NUMBER < 0x00907000L
1200  if(!a) return;
1201  if(a->type == V_ASN1_BOOLEAN){
1202  OPENSSL_free(a);
1203  return;
1204  }
1205 #endif
1206  ASN1_TYPE_free(a);
1207 }
1208 
1209 /*
1210  * call-seq:
1211  * asn1.to_der => DER-encoded String
1212  *
1213  * See ASN1Data#to_der for details. *
1214  */
1215 static VALUE
1217 {
1218  ASN1_TYPE *asn1;
1219  int tn, tc, explicit;
1220  long len, reallen;
1221  unsigned char *buf, *p;
1222  VALUE str;
1223 
1224  tn = NUM2INT(ossl_asn1_get_tag(self));
1225  tc = ossl_asn1_tag_class(self);
1226  explicit = ossl_asn1_is_explicit(self);
1227  asn1 = ossl_asn1_get_asn1type(self);
1228 
1229  len = ossl_asn1_object_size(1, ossl_i2d_ASN1_TYPE(asn1, NULL), tn);
1230  if(!(buf = OPENSSL_malloc(len))){
1231  ossl_ASN1_TYPE_free(asn1);
1232  ossl_raise(eASN1Error, "cannot alloc buffer");
1233  }
1234  p = buf;
1235  if (tc == V_ASN1_UNIVERSAL) {
1236  ossl_i2d_ASN1_TYPE(asn1, &p);
1237  } else if (explicit) {
1238  ossl_asn1_put_object(&p, 1, ossl_i2d_ASN1_TYPE(asn1, NULL), tn, tc);
1239  ossl_i2d_ASN1_TYPE(asn1, &p);
1240  } else {
1241  ossl_i2d_ASN1_TYPE(asn1, &p);
1242  *buf = tc | tn | (*buf & V_ASN1_CONSTRUCTED);
1243  }
1244  ossl_ASN1_TYPE_free(asn1);
1245  reallen = p - buf;
1246  assert(reallen <= len);
1247  str = ossl_buf2str((char *)buf, rb_long2int(reallen)); /* buf will be free in ossl_buf2str */
1248 
1249  return str;
1250 }
1251 
1252 /*
1253  * call-seq:
1254  * asn1.to_der => DER-encoded String
1255  *
1256  * See ASN1Data#to_der for details.
1257  */
1258 static VALUE
1260 {
1261  int tag, tn, tc, explicit, constructed = 1;
1262  int found_prim = 0, seq_len;
1263  long length;
1264  unsigned char *p;
1265  VALUE value, str, inf_length;
1266 
1267  tn = NUM2INT(ossl_asn1_get_tag(self));
1268  tc = ossl_asn1_tag_class(self);
1269  inf_length = ossl_asn1_get_infinite_length(self);
1270  if (inf_length == Qtrue) {
1271  VALUE ary, example;
1272  constructed = 2;
1273  if (CLASS_OF(self) == cASN1Sequence ||
1274  CLASS_OF(self) == cASN1Set) {
1275  tag = ossl_asn1_default_tag(self);
1276  }
1277  else { /* must be a constructive encoding of a primitive value */
1278  ary = ossl_asn1_get_value(self);
1279  if (!rb_obj_is_kind_of(ary, rb_cArray))
1280  ossl_raise(eASN1Error, "Constructive value must be an Array");
1281  /* Recursively descend until a primitive value is found.
1282  The overall value of the entire constructed encoding
1283  is of the type of the first primitive encoding to be
1284  found. */
1285  while (!found_prim){
1286  example = rb_ary_entry(ary, 0);
1287  if (rb_obj_is_kind_of(example, cASN1Primitive)){
1288  found_prim = 1;
1289  }
1290  else {
1291  /* example is another ASN1Constructive */
1292  if (!rb_obj_is_kind_of(example, cASN1Constructive)){
1293  ossl_raise(eASN1Error, "invalid constructed encoding");
1294  return Qnil; /* dummy */
1295  }
1296  ary = ossl_asn1_get_value(example);
1297  }
1298  }
1299  tag = ossl_asn1_default_tag(example);
1300  }
1301  }
1302  else {
1303  if (CLASS_OF(self) == cASN1Constructive)
1304  ossl_raise(eASN1Error, "Constructive shall only be used with infinite length");
1305  tag = ossl_asn1_default_tag(self);
1306  }
1307  explicit = ossl_asn1_is_explicit(self);
1308  value = join_der(ossl_asn1_get_value(self));
1309 
1310  seq_len = ossl_asn1_object_size(constructed, RSTRING_LENINT(value), tag);
1311  length = ossl_asn1_object_size(constructed, seq_len, tn);
1312  str = rb_str_new(0, length);
1313  p = (unsigned char *)RSTRING_PTR(str);
1314  if(tc == V_ASN1_UNIVERSAL)
1315  ossl_asn1_put_object(&p, constructed, RSTRING_LENINT(value), tn, tc);
1316  else{
1317  if(explicit){
1318  ossl_asn1_put_object(&p, constructed, seq_len, tn, tc);
1319  ossl_asn1_put_object(&p, constructed, RSTRING_LENINT(value), tag, V_ASN1_UNIVERSAL);
1320  }
1321  else{
1322  ossl_asn1_put_object(&p, constructed, RSTRING_LENINT(value), tn, tc);
1323  }
1324  }
1325  memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value));
1326  p += RSTRING_LEN(value);
1327 
1328  /* In this case we need an additional EOC (one for the explicit part and
1329  * one for the Constructive itself. The EOC for the Constructive is
1330  * supplied by the user, but that for the "explicit wrapper" must be
1331  * added here.
1332  */
1333  if (explicit && inf_length == Qtrue) {
1334  ASN1_put_eoc(&p);
1335  }
1336  ossl_str_adjust(str, p);
1337 
1338  return str;
1339 }
1340 
1341 /*
1342  * call-seq:
1343  * asn1_ary.each { |asn1| block } => asn1_ary
1344  *
1345  * Calls <i>block</i> once for each element in +self+, passing that element
1346  * as parameter +asn1+. If no block is given, an enumerator is returned
1347  * instead.
1348  *
1349  * == Example
1350  * asn1_ary.each do |asn1|
1351  * puts asn1
1352  * end
1353  */
1354 static VALUE
1356 {
1358  return self;
1359 }
1360 
1361 /*
1362  * call-seq:
1363  * ObjectId.register(object_id, short_name, long_name)
1364  *
1365  * This adds a new ObjectId to the internal tables. Where +object_id+ is the
1366  * numerical form, +short_name+ is the short name, and +long_name+ is the long
1367  * name.
1368  *
1369  * Returns +true+ if successful. Raises an ASN1Error otherwise.
1370  *
1371  */
1372 static VALUE
1374 {
1375  StringValue(oid);
1376  StringValue(sn);
1377  StringValue(ln);
1378 
1379  if(!OBJ_create(RSTRING_PTR(oid), RSTRING_PTR(sn), RSTRING_PTR(ln)))
1380  ossl_raise(eASN1Error, NULL);
1381 
1382  return Qtrue;
1383 }
1384 
1385 /* Document-method: OpenSSL::ASN1::ObjectId#sn
1386  *
1387  * The short name of the ObjectId, as defined in +openssl/objects.h+.
1388  */
1389 /* Document-method: OpenSSL::ASN1::ObjectId#short_name
1390  *
1391  * #short_name is an alias to #sn
1392  */
1393 static VALUE
1395 {
1396  VALUE val, ret = Qnil;
1397  int nid;
1398 
1399  val = ossl_asn1_get_value(self);
1400  if ((nid = OBJ_txt2nid(StringValuePtr(val))) != NID_undef)
1401  ret = rb_str_new2(OBJ_nid2sn(nid));
1402 
1403  return ret;
1404 }
1405 
1406 /* Document-method: OpenSSL::ASN1::ObjectId#ln
1407  *
1408  * The long name of the ObjectId, as defined in +openssl/objects.h+.
1409  */
1410 /* Document-method: OpenSSL::ASN1::ObjectId.long_name
1411  *
1412  * #long_name is an alias to #ln
1413  */
1414 static VALUE
1416 {
1417  VALUE val, ret = Qnil;
1418  int nid;
1419 
1420  val = ossl_asn1_get_value(self);
1421  if ((nid = OBJ_txt2nid(StringValuePtr(val))) != NID_undef)
1422  ret = rb_str_new2(OBJ_nid2ln(nid));
1423 
1424  return ret;
1425 }
1426 
1427 /* Document-method: OpenSSL::ASN1::ObjectId#oid
1428  *
1429  * The object identifier as a String.
1430  */
1431 static VALUE
1433 {
1434  VALUE val;
1435  ASN1_OBJECT *a1obj;
1436  char buf[128];
1437 
1438  val = ossl_asn1_get_value(self);
1439  a1obj = obj_to_asn1obj(val);
1440  OBJ_obj2txt(buf, sizeof(buf), a1obj, 1);
1441  ASN1_OBJECT_free(a1obj);
1442 
1443  return rb_str_new2(buf);
1444 }
1445 
1446 #define OSSL_ASN1_IMPL_FACTORY_METHOD(klass) \
1447 static VALUE ossl_asn1_##klass(int argc, VALUE *argv, VALUE self)\
1448 { return rb_funcall3(cASN1##klass, rb_intern("new"), argc, argv); }
1449 
1454 OSSL_ASN1_IMPL_FACTORY_METHOD(OctetString)
1456 OSSL_ASN1_IMPL_FACTORY_METHOD(NumericString)
1457 OSSL_ASN1_IMPL_FACTORY_METHOD(PrintableString)
1459 OSSL_ASN1_IMPL_FACTORY_METHOD(VideotexString)
1461 OSSL_ASN1_IMPL_FACTORY_METHOD(GraphicString)
1462 OSSL_ASN1_IMPL_FACTORY_METHOD(ISO64String)
1463 OSSL_ASN1_IMPL_FACTORY_METHOD(GeneralString)
1464 OSSL_ASN1_IMPL_FACTORY_METHOD(UniversalString)
1469 OSSL_ASN1_IMPL_FACTORY_METHOD(GeneralizedTime)
1472 OSSL_ASN1_IMPL_FACTORY_METHOD(EndOfContent)
1473 
1474 void
1476 {
1477  VALUE ary;
1478  int i;
1479 
1480 #if 0
1481  mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
1482 #endif
1483 
1484  sUNIVERSAL = rb_intern("UNIVERSAL");
1485  sCONTEXT_SPECIFIC = rb_intern("CONTEXT_SPECIFIC");
1486  sAPPLICATION = rb_intern("APPLICATION");
1487  sPRIVATE = rb_intern("PRIVATE");
1488  sEXPLICIT = rb_intern("EXPLICIT");
1489  sIMPLICIT = rb_intern("IMPLICIT");
1490 
1491  sivVALUE = rb_intern("@value");
1492  sivTAG = rb_intern("@tag");
1493  sivTAGGING = rb_intern("@tagging");
1494  sivTAG_CLASS = rb_intern("@tag_class");
1495  sivINFINITE_LENGTH = rb_intern("@infinite_length");
1496  sivUNUSED_BITS = rb_intern("@unused_bits");
1497 
1498  /*
1499  * Document-module: OpenSSL::ASN1
1500  *
1501  * Abstract Syntax Notation One (or ASN.1) is a notation syntax to
1502  * describe data structures and is defined in ITU-T X.680. ASN.1 itself
1503  * does not mandate any encoding or parsing rules, but usually ASN.1 data
1504  * structures are encoded using the Distinguished Encoding Rules (DER) or
1505  * less often the Basic Encoding Rules (BER) described in ITU-T X.690. DER
1506  * and BER encodings are binary Tag-Length-Value (TLV) encodings that are
1507  * quite concise compared to other popular data description formats such
1508  * as XML, JSON etc.
1509  * ASN.1 data structures are very common in cryptographic applications,
1510  * e.g. X.509 public key certificates or certificate revocation lists
1511  * (CRLs) are all defined in ASN.1 and DER-encoded. ASN.1, DER and BER are
1512  * the building blocks of applied cryptography.
1513  * The ASN1 module provides the necessary classes that allow generation
1514  * of ASN.1 data structures and the methods to encode them using a DER
1515  * encoding. The decode method allows parsing arbitrary BER-/DER-encoded
1516  * data to a Ruby object that can then be modified and re-encoded at will.
1517  *
1518  * == ASN.1 class hierarchy
1519  *
1520  * The base class representing ASN.1 structures is ASN1Data. ASN1Data offers
1521  * attributes to read and set the +tag+, the +tag_class+ and finally the
1522  * +value+ of a particular ASN.1 item. Upon parsing, any tagged values
1523  * (implicit or explicit) will be represented by ASN1Data instances because
1524  * their "real type" can only be determined using out-of-band information
1525  * from the ASN.1 type declaration. Since this information is normally
1526  * known when encoding a type, all sub-classes of ASN1Data offer an
1527  * additional attribute +tagging+ that allows to encode a value implicitly
1528  * (+:IMPLICIT+) or explicitly (+:EXPLICIT+).
1529  *
1530  * === Constructive
1531  *
1532  * Constructive is, as its name implies, the base class for all
1533  * constructed encodings, i.e. those that consist of several values,
1534  * opposed to "primitive" encodings with just one single value.
1535  * Primitive values that are encoded with "infinite length" are typically
1536  * constructed (their values come in multiple chunks) and are therefore
1537  * represented by instances of Constructive. The value of an Constructive
1538  * is always an Array.
1539  *
1540  * ==== ASN1::Set and ASN1::Sequence
1541  *
1542  * The most common constructive encodings are SETs and SEQUENCEs, which is
1543  * why there are two sub-classes of Constructive representing each of
1544  * them.
1545  *
1546  * === Primitive
1547  *
1548  * This is the super class of all primitive values. Primitive
1549  * itself is not used when parsing ASN.1 data, all values are either
1550  * instances of a corresponding sub-class of Primitive or they are
1551  * instances of ASN1Data if the value was tagged implicitly or explicitly.
1552  * Please cf. Primitive documentation for details on sub-classes and
1553  * their respective mappings of ASN.1 data types to Ruby objects.
1554  *
1555  * == Possible values for +tagging+
1556  *
1557  * When constructing an ASN1Data object the ASN.1 type definition may
1558  * require certain elements to be either implicitly or explicitly tagged.
1559  * This can be achieved by setting the +tagging+ attribute manually for
1560  * sub-classes of ASN1Data. Use the symbol +:IMPLICIT+ for implicit
1561  * tagging and +:EXPLICIT+ if the element requires explicit tagging.
1562  *
1563  * == Possible values for +tag_class+
1564  *
1565  * It is possible to create arbitrary ASN1Data objects that also support
1566  * a PRIVATE or APPLICATION tag class. Possible values for the +tag_class+
1567  * attribute are:
1568  * * +:UNIVERSAL+ (the default for untagged values)
1569  * * +:CONTEXT_SPECIFIC+ (the default for tagged values)
1570  * * +:APPLICATION+
1571  * * +:PRIVATE+
1572  *
1573  * == Tag constants
1574  *
1575  * There is a constant defined for each universal tag:
1576  * * OpenSSL::ASN1::EOC (0)
1577  * * OpenSSL::ASN1::BOOLEAN (1)
1578  * * OpenSSL::ASN1::INTEGER (2)
1579  * * OpenSSL::ASN1::BIT_STRING (3)
1580  * * OpenSSL::ASN1::OCTET_STRING (4)
1581  * * OpenSSL::ASN1::NULL (5)
1582  * * OpenSSL::ASN1::OBJECT (6)
1583  * * OpenSSL::ASN1::ENUMERATED (10)
1584  * * OpenSSL::ASN1::UTF8STRING (12)
1585  * * OpenSSL::ASN1::SEQUENCE (16)
1586  * * OpenSSL::ASN1::SET (17)
1587  * * OpenSSL::ASN1::NUMERICSTRING (18)
1588  * * OpenSSL::ASN1::PRINTABLESTRING (19)
1589  * * OpenSSL::ASN1::T61STRING (20)
1590  * * OpenSSL::ASN1::VIDEOTEXSTRING (21)
1591  * * OpenSSL::ASN1::IA5STRING (22)
1592  * * OpenSSL::ASN1::UTCTIME (23)
1593  * * OpenSSL::ASN1::GENERALIZEDTIME (24)
1594  * * OpenSSL::ASN1::GRAPHICSTRING (25)
1595  * * OpenSSL::ASN1::ISO64STRING (26)
1596  * * OpenSSL::ASN1::GENERALSTRING (27)
1597  * * OpenSSL::ASN1::UNIVERSALSTRING (28)
1598  * * OpenSSL::ASN1::BMPSTRING (30)
1599  *
1600  * == UNIVERSAL_TAG_NAME constant
1601  *
1602  * An Array that stores the name of a given tag number. These names are
1603  * the same as the name of the tag constant that is additionally defined,
1604  * e.g. UNIVERSAL_TAG_NAME[2] = "INTEGER" and OpenSSL::ASN1::INTEGER = 2.
1605  *
1606  * == Example usage
1607  *
1608  * === Decoding and viewing a DER-encoded file
1609  * require 'openssl'
1610  * require 'pp'
1611  * der = File.binread('data.der')
1612  * asn1 = OpenSSL::ASN1.decode(der)
1613  * pp der
1614  *
1615  * === Creating an ASN.1 structure and DER-encoding it
1616  * require 'openssl'
1617  * version = OpenSSL::ASN1::Integer.new(1)
1618  * # Explicitly 0-tagged implies context-specific tag class
1619  * serial = OpenSSL::ASN1::Integer.new(12345, 0, :EXPLICIT, :CONTEXT_SPECIFIC)
1620  * name = OpenSSL::ASN1::PrintableString.new('Data 1')
1621  * sequence = OpenSSL::ASN1::Sequence.new( [ version, serial, name ] )
1622  * der = sequence.to_der
1623  */
1624  mASN1 = rb_define_module_under(mOSSL, "ASN1");
1625 
1626  /* Document-class: OpenSSL::ASN1::ASN1Error
1627  *
1628  * Generic error class for all errors raised in ASN1 and any of the
1629  * classes defined in it.
1630  */
1631  eASN1Error = rb_define_class_under(mASN1, "ASN1Error", eOSSLError);
1632  rb_define_module_function(mASN1, "traverse", ossl_asn1_traverse, 1);
1633  rb_define_module_function(mASN1, "decode", ossl_asn1_decode, 1);
1634  rb_define_module_function(mASN1, "decode_all", ossl_asn1_decode_all, 1);
1635  ary = rb_ary_new();
1636 
1637  /*
1638  * Array storing tag names at the tag's index.
1639  */
1640  rb_define_const(mASN1, "UNIVERSAL_TAG_NAME", ary);
1641  for(i = 0; i < ossl_asn1_info_size; i++){
1642  if(ossl_asn1_info[i].name[0] == '[') continue;
1643  rb_define_const(mASN1, ossl_asn1_info[i].name, INT2NUM(i));
1644  rb_ary_store(ary, i, rb_str_new2(ossl_asn1_info[i].name));
1645  }
1646 
1647  /* Document-class: OpenSSL::ASN1::ASN1Data
1648  *
1649  * The top-level class representing any ASN.1 object. When parsed by
1650  * ASN1.decode, tagged values are always represented by an instance
1651  * of ASN1Data.
1652  *
1653  * == The role of ASN1Data for parsing tagged values
1654  *
1655  * When encoding an ASN.1 type it is inherently clear what original
1656  * type (e.g. INTEGER, OCTET STRING etc.) this value has, regardless
1657  * of its tagging.
1658  * But opposed to the time an ASN.1 type is to be encoded, when parsing
1659  * them it is not possible to deduce the "real type" of tagged
1660  * values. This is why tagged values are generally parsed into ASN1Data
1661  * instances, but with a different outcome for implicit and explicit
1662  * tagging.
1663  *
1664  * === Example of a parsed implicitly tagged value
1665  *
1666  * An implicitly 1-tagged INTEGER value will be parsed as an
1667  * ASN1Data with
1668  * * +tag+ equal to 1
1669  * * +tag_class+ equal to +:CONTEXT_SPECIFIC+
1670  * * +value+ equal to a +String+ that carries the raw encoding
1671  * of the INTEGER.
1672  * This implies that a subsequent decoding step is required to
1673  * completely decode implicitly tagged values.
1674  *
1675  * === Example of a parsed explicitly tagged value
1676  *
1677  * An explicitly 1-tagged INTEGER value will be parsed as an
1678  * ASN1Data with
1679  * * +tag+ equal to 1
1680  * * +tag_class+ equal to +:CONTEXT_SPECIFIC+
1681  * * +value+ equal to an +Array+ with one single element, an
1682  * instance of OpenSSL::ASN1::Integer, i.e. the inner element
1683  * is the non-tagged primitive value, and the tagging is represented
1684  * in the outer ASN1Data
1685  *
1686  * == Example - Decoding an implicitly tagged INTEGER
1687  * int = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT) # implicit 0-tagged
1688  * seq = OpenSSL::ASN1::Sequence.new( [int] )
1689  * der = seq.to_der
1690  * asn1 = OpenSSL::ASN1.decode(der)
1691  * # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
1692  * # @infinite_length=false,
1693  * # @tag=16,
1694  * # @tag_class=:UNIVERSAL,
1695  * # @tagging=nil,
1696  * # @value=
1697  * # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
1698  * # @infinite_length=false,
1699  * # @tag=0,
1700  * # @tag_class=:CONTEXT_SPECIFIC,
1701  * # @value="\x01">]>
1702  * raw_int = asn1.value[0]
1703  * # manually rewrite tag and tag class to make it an UNIVERSAL value
1704  * raw_int.tag = OpenSSL::ASN1::INTEGER
1705  * raw_int.tag_class = :UNIVERSAL
1706  * int2 = OpenSSL::ASN1.decode(raw_int)
1707  * puts int2.value # => 1
1708  *
1709  * == Example - Decoding an explicitly tagged INTEGER
1710  * int = OpenSSL::ASN1::Integer.new(1, 0, :EXPLICIT) # explicit 0-tagged
1711  * seq = OpenSSL::ASN1::Sequence.new( [int] )
1712  * der = seq.to_der
1713  * asn1 = OpenSSL::ASN1.decode(der)
1714  * # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
1715  * # @infinite_length=false,
1716  * # @tag=16,
1717  * # @tag_class=:UNIVERSAL,
1718  * # @tagging=nil,
1719  * # @value=
1720  * # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
1721  * # @infinite_length=false,
1722  * # @tag=0,
1723  * # @tag_class=:CONTEXT_SPECIFIC,
1724  * # @value=
1725  * # [#<OpenSSL::ASN1::Integer:0x85bf308
1726  * # @infinite_length=false,
1727  * # @tag=2,
1728  * # @tag_class=:UNIVERSAL
1729  * # @tagging=nil,
1730  * # @value=1>]>]>
1731  * int2 = asn1.value[0].value[0]
1732  * puts int2.value # => 1
1733  */
1734  cASN1Data = rb_define_class_under(mASN1, "ASN1Data", rb_cObject);
1735  /*
1736  * Carries the value of a ASN.1 type.
1737  * Please confer Constructive and Primitive for the mappings between
1738  * ASN.1 data types and Ruby classes.
1739  */
1740  rb_attr(cASN1Data, rb_intern("value"), 1, 1, 0);
1741  /*
1742  * A +Number+ representing the tag number of this ASN1Data. Never +nil+.
1743  */
1744  rb_attr(cASN1Data, rb_intern("tag"), 1, 1, 0);
1745  /*
1746  * A +Symbol+ representing the tag class of this ASN1Data. Never +nil+.
1747  * See ASN1Data for possible values.
1748  */
1749  rb_attr(cASN1Data, rb_intern("tag_class"), 1, 1, 0);
1750  /*
1751  * Never +nil+. A +Boolean+ indicating whether the encoding was infinite
1752  * length (in the case of parsing) or whether an infinite length encoding
1753  * shall be used (in the encoding case).
1754  * In DER, every value has a finite length associated with it. But in
1755  * scenarios where large amounts of data need to be transferred it
1756  * might be desirable to have some kind of streaming support available.
1757  * For example, huge OCTET STRINGs are preferably sent in smaller-sized
1758  * chunks, each at a time.
1759  * This is possible in BER by setting the length bytes of an encoding
1760  * to zero and by this indicating that the following value will be
1761  * sent in chunks. Infinite length encodings are always constructed.
1762  * The end of such a stream of chunks is indicated by sending a EOC
1763  * (End of Content) tag. SETs and SEQUENCEs may use an infinite length
1764  * encoding, but also primitive types such as e.g. OCTET STRINGS or
1765  * BIT STRINGS may leverage this functionality (cf. ITU-T X.690).
1766  */
1767  rb_attr(cASN1Data, rb_intern("infinite_length"), 1, 1, 0);
1768  rb_define_method(cASN1Data, "initialize", ossl_asn1data_initialize, 3);
1769  rb_define_method(cASN1Data, "to_der", ossl_asn1data_to_der, 0);
1770 
1771  /* Document-class: OpenSSL::ASN1::Primitive
1772  *
1773  * The parent class for all primitive encodings. Attributes are the same as
1774  * for ASN1Data, with the addition of +tagging+.
1775  * Primitive values can never be infinite length encodings, thus it is not
1776  * possible to set the +infinite_length+ attribute for Primitive and its
1777  * sub-classes.
1778  *
1779  * == Primitive sub-classes and their mapping to Ruby classes
1780  * * OpenSSL::ASN1::EndOfContent <=> +value+ is always +nil+
1781  * * OpenSSL::ASN1::Boolean <=> +value+ is a +Boolean+
1782  * * OpenSSL::ASN1::Integer <=> +value+ is a +Number+
1783  * * OpenSSL::ASN1::BitString <=> +value+ is a +String+
1784  * * OpenSSL::ASN1::OctetString <=> +value+ is a +String+
1785  * * OpenSSL::ASN1::Null <=> +value+ is always +nil+
1786  * * OpenSSL::ASN1::Object <=> +value+ is a +String+
1787  * * OpenSSL::ASN1::Enumerated <=> +value+ is a +Number+
1788  * * OpenSSL::ASN1::UTF8String <=> +value+ is a +String+
1789  * * OpenSSL::ASN1::NumericString <=> +value+ is a +String+
1790  * * OpenSSL::ASN1::PrintableString <=> +value+ is a +String+
1791  * * OpenSSL::ASN1::T61String <=> +value+ is a +String+
1792  * * OpenSSL::ASN1::VideotexString <=> +value+ is a +String+
1793  * * OpenSSL::ASN1::IA5String <=> +value+ is a +String+
1794  * * OpenSSL::ASN1::UTCTime <=> +value+ is a +Time+
1795  * * OpenSSL::ASN1::GeneralizedTime <=> +value+ is a +Time+
1796  * * OpenSSL::ASN1::GraphicString <=> +value+ is a +String+
1797  * * OpenSSL::ASN1::ISO64String <=> +value+ is a +String+
1798  * * OpenSSL::ASN1::GeneralString <=> +value+ is a +String+
1799  * * OpenSSL::ASN1::UniversalString <=> +value+ is a +String+
1800  * * OpenSSL::ASN1::BMPString <=> +value+ is a +String+
1801  *
1802  * == OpenSSL::ASN1::BitString
1803  *
1804  * === Additional attributes
1805  * +unused_bits+: if the underlying BIT STRING's
1806  * length is a multiple of 8 then +unused_bits+ is 0. Otherwise
1807  * +unused_bits+ indicates the number of bits that are to be ignored in
1808  * the final octet of the +BitString+'s +value+.
1809  *
1810  * == OpenSSL::ASN1::ObjectId
1811  *
1812  * While OpenSSL::ASN1::ObjectId.new will allocate a new ObjectId, it is
1813  * not typically allocated this way, but rather that are received from
1814  * parsed ASN1 encodings.
1815  *
1816  * === Additional attributes
1817  * * +sn+: the short name as defined in <openssl/objects.h>.
1818  * * +ln+: the long name as defined in <openssl/objects.h>.
1819  * * +oid+: the object identifier as a +String+, e.g. "1.2.3.4.5"
1820  * * +short_name+: alias for +sn+.
1821  * * +long_name+: alias for +ln+.
1822  *
1823  * == Examples
1824  * With the Exception of OpenSSL::ASN1::EndOfContent, each Primitive class
1825  * constructor takes at least one parameter, the +value+.
1826  *
1827  * === Creating EndOfContent
1828  * eoc = OpenSSL::ASN1::EndOfContent.new
1829  *
1830  * === Creating any other Primitive
1831  * prim = <class>.new(value) # <class> being one of the sub-classes except EndOfContent
1832  * prim_zero_tagged_implicit = <class>.new(value, 0, :IMPLICIT)
1833  * prim_zero_tagged_explicit = <class>.new(value, 0, :EXPLICIT)
1834  */
1835  cASN1Primitive = rb_define_class_under(mASN1, "Primitive", cASN1Data);
1836  /*
1837  * May be used as a hint for encoding a value either implicitly or
1838  * explicitly by setting it either to +:IMPLICIT+ or to +:EXPLICIT+.
1839  * +tagging+ is not set when a ASN.1 structure is parsed using
1840  * OpenSSL::ASN1.decode.
1841  */
1842  rb_attr(cASN1Primitive, rb_intern("tagging"), 1, 1, Qtrue);
1843  rb_undef_method(cASN1Primitive, "infinite_length=");
1844  rb_define_method(cASN1Primitive, "initialize", ossl_asn1_initialize, -1);
1845  rb_define_method(cASN1Primitive, "to_der", ossl_asn1prim_to_der, 0);
1846 
1847  /* Document-class: OpenSSL::ASN1::Constructive
1848  *
1849  * The parent class for all constructed encodings. The +value+ attribute
1850  * of a Constructive is always an +Array+. Attributes are the same as
1851  * for ASN1Data, with the addition of +tagging+.
1852  *
1853  * == SET and SEQUENCE
1854  *
1855  * Most constructed encodings come in the form of a SET or a SEQUENCE.
1856  * These encodings are represented by one of the two sub-classes of
1857  * Constructive:
1858  * * OpenSSL::ASN1::Set
1859  * * OpenSSL::ASN1::Sequence
1860  * Please note that tagged sequences and sets are still parsed as
1861  * instances of ASN1Data. Find further details on tagged values
1862  * there.
1863  *
1864  * === Example - constructing a SEQUENCE
1865  * int = OpenSSL::ASN1::Integer.new(1)
1866  * str = OpenSSL::ASN1::PrintableString.new('abc')
1867  * sequence = OpenSSL::ASN1::Sequence.new( [ int, str ] )
1868  *
1869  * === Example - constructing a SET
1870  * int = OpenSSL::ASN1::Integer.new(1)
1871  * str = OpenSSL::ASN1::PrintableString.new('abc')
1872  * set = OpenSSL::ASN1::Set.new( [ int, str ] )
1873  *
1874  * == Infinite length primitive values
1875  *
1876  * The only case where Constructive is used directly is for infinite
1877  * length encodings of primitive values. These encodings are always
1878  * constructed, with the contents of the +value+ +Array+ being either
1879  * UNIVERSAL non-infinite length partial encodings of the actual value
1880  * or again constructive encodings with infinite length (i.e. infinite
1881  * length primitive encodings may be constructed recursively with another
1882  * infinite length value within an already infinite length value). Each
1883  * partial encoding must be of the same UNIVERSAL type as the overall
1884  * encoding. The value of the overall encoding consists of the
1885  * concatenation of each partial encoding taken in sequence. The +value+
1886  * array of the outer infinite length value must end with a
1887  * OpenSSL::ASN1::EndOfContent instance.
1888  *
1889  * Please note that it is not possible to encode Constructive without
1890  * the +infinite_length+ attribute being set to +true+, use
1891  * OpenSSL::ASN1::Sequence or OpenSSL::ASN1::Set in these cases instead.
1892  *
1893  * === Example - Infinite length OCTET STRING
1894  * partial1 = OpenSSL::ASN1::OctetString.new("\x01")
1895  * partial2 = OpenSSL::ASN1::OctetString.new("\x02")
1896  * inf_octets = OpenSSL::ASN1::Constructive.new( [ partial1,
1897  * partial2,
1898  * OpenSSL::ASN1::EndOfContent.new ],
1899  * OpenSSL::ASN1::OCTET_STRING,
1900  * nil,
1901  * :UNIVERSAL )
1902  * # The real value of inf_octets is "\x01\x02", i.e. the concatenation
1903  * # of partial1 and partial2
1904  * inf_octets.infinite_length = true
1905  * der = inf_octets.to_der
1906  * asn1 = OpenSSL::ASN1.decode(der)
1907  * puts asn1.infinite_length # => true
1908  */
1909  cASN1Constructive = rb_define_class_under(mASN1,"Constructive", cASN1Data);
1910  rb_include_module(cASN1Constructive, rb_mEnumerable);
1911  /*
1912  * May be used as a hint for encoding a value either implicitly or
1913  * explicitly by setting it either to +:IMPLICIT+ or to +:EXPLICIT+.
1914  * +tagging+ is not set when a ASN.1 structure is parsed using
1915  * OpenSSL::ASN1.decode.
1916  */
1917  rb_attr(cASN1Constructive, rb_intern("tagging"), 1, 1, Qtrue);
1918  rb_define_method(cASN1Constructive, "initialize", ossl_asn1_initialize, -1);
1919  rb_define_method(cASN1Constructive, "to_der", ossl_asn1cons_to_der, 0);
1920  rb_define_method(cASN1Constructive, "each", ossl_asn1cons_each, 0);
1921 
1922 #define OSSL_ASN1_DEFINE_CLASS(name, super) \
1923 do{\
1924  cASN1##name = rb_define_class_under(mASN1, #name, cASN1##super);\
1925  rb_define_module_function(mASN1, #name, ossl_asn1_##name, -1);\
1926 }while(0)
1927 
1928  OSSL_ASN1_DEFINE_CLASS(Boolean, Primitive);
1929  OSSL_ASN1_DEFINE_CLASS(Integer, Primitive);
1930  OSSL_ASN1_DEFINE_CLASS(Enumerated, Primitive);
1931  OSSL_ASN1_DEFINE_CLASS(BitString, Primitive);
1932  OSSL_ASN1_DEFINE_CLASS(OctetString, Primitive);
1933  OSSL_ASN1_DEFINE_CLASS(UTF8String, Primitive);
1934  OSSL_ASN1_DEFINE_CLASS(NumericString, Primitive);
1935  OSSL_ASN1_DEFINE_CLASS(PrintableString, Primitive);
1936  OSSL_ASN1_DEFINE_CLASS(T61String, Primitive);
1937  OSSL_ASN1_DEFINE_CLASS(VideotexString, Primitive);
1938  OSSL_ASN1_DEFINE_CLASS(IA5String, Primitive);
1939  OSSL_ASN1_DEFINE_CLASS(GraphicString, Primitive);
1940  OSSL_ASN1_DEFINE_CLASS(ISO64String, Primitive);
1941  OSSL_ASN1_DEFINE_CLASS(GeneralString, Primitive);
1942  OSSL_ASN1_DEFINE_CLASS(UniversalString, Primitive);
1943  OSSL_ASN1_DEFINE_CLASS(BMPString, Primitive);
1944  OSSL_ASN1_DEFINE_CLASS(Null, Primitive);
1945  OSSL_ASN1_DEFINE_CLASS(ObjectId, Primitive);
1946  OSSL_ASN1_DEFINE_CLASS(UTCTime, Primitive);
1947  OSSL_ASN1_DEFINE_CLASS(GeneralizedTime, Primitive);
1948 
1949  OSSL_ASN1_DEFINE_CLASS(Sequence, Constructive);
1950  OSSL_ASN1_DEFINE_CLASS(Set, Constructive);
1951 
1952  OSSL_ASN1_DEFINE_CLASS(EndOfContent, Data);
1953 
1954 
1955  /* Document-class: OpenSSL::ASN1::ObjectId
1956  *
1957  * Represents the primitive object id for OpenSSL::ASN1
1958  */
1959 #if 0
1960  cASN1ObjectId = rb_define_class_under(mASN1, "ObjectId", cASN1Primitive); /* let rdoc know */
1961 #endif
1962  rb_define_singleton_method(cASN1ObjectId, "register", ossl_asn1obj_s_register, 3);
1963  rb_define_method(cASN1ObjectId, "sn", ossl_asn1obj_get_sn, 0);
1964  rb_define_method(cASN1ObjectId, "ln", ossl_asn1obj_get_ln, 0);
1965  rb_define_method(cASN1ObjectId, "oid", ossl_asn1obj_get_oid, 0);
1966  rb_define_alias(cASN1ObjectId, "short_name", "sn");
1967  rb_define_alias(cASN1ObjectId, "long_name", "ln");
1968  rb_attr(cASN1BitString, rb_intern("unused_bits"), 1, 1, 0);
1969 
1970  rb_define_method(cASN1EndOfContent, "initialize", ossl_asn1eoc_initialize, 0);
1971 
1972  class_tag_map = rb_hash_new();
1973  rb_hash_aset(class_tag_map, cASN1EndOfContent, INT2NUM(V_ASN1_EOC));
1974  rb_hash_aset(class_tag_map, cASN1Boolean, INT2NUM(V_ASN1_BOOLEAN));
1975  rb_hash_aset(class_tag_map, cASN1Integer, INT2NUM(V_ASN1_INTEGER));
1976  rb_hash_aset(class_tag_map, cASN1BitString, INT2NUM(V_ASN1_BIT_STRING));
1977  rb_hash_aset(class_tag_map, cASN1OctetString, INT2NUM(V_ASN1_OCTET_STRING));
1978  rb_hash_aset(class_tag_map, cASN1Null, INT2NUM(V_ASN1_NULL));
1979  rb_hash_aset(class_tag_map, cASN1ObjectId, INT2NUM(V_ASN1_OBJECT));
1980  rb_hash_aset(class_tag_map, cASN1Enumerated, INT2NUM(V_ASN1_ENUMERATED));
1981  rb_hash_aset(class_tag_map, cASN1UTF8String, INT2NUM(V_ASN1_UTF8STRING));
1982  rb_hash_aset(class_tag_map, cASN1Sequence, INT2NUM(V_ASN1_SEQUENCE));
1983  rb_hash_aset(class_tag_map, cASN1Set, INT2NUM(V_ASN1_SET));
1984  rb_hash_aset(class_tag_map, cASN1NumericString, INT2NUM(V_ASN1_NUMERICSTRING));
1985  rb_hash_aset(class_tag_map, cASN1PrintableString, INT2NUM(V_ASN1_PRINTABLESTRING));
1986  rb_hash_aset(class_tag_map, cASN1T61String, INT2NUM(V_ASN1_T61STRING));
1987  rb_hash_aset(class_tag_map, cASN1VideotexString, INT2NUM(V_ASN1_VIDEOTEXSTRING));
1988  rb_hash_aset(class_tag_map, cASN1IA5String, INT2NUM(V_ASN1_IA5STRING));
1989  rb_hash_aset(class_tag_map, cASN1UTCTime, INT2NUM(V_ASN1_UTCTIME));
1990  rb_hash_aset(class_tag_map, cASN1GeneralizedTime, INT2NUM(V_ASN1_GENERALIZEDTIME));
1991  rb_hash_aset(class_tag_map, cASN1GraphicString, INT2NUM(V_ASN1_GRAPHICSTRING));
1992  rb_hash_aset(class_tag_map, cASN1ISO64String, INT2NUM(V_ASN1_ISO64STRING));
1993  rb_hash_aset(class_tag_map, cASN1GeneralString, INT2NUM(V_ASN1_GENERALSTRING));
1994  rb_hash_aset(class_tag_map, cASN1UniversalString, INT2NUM(V_ASN1_UNIVERSALSTRING));
1995  rb_hash_aset(class_tag_map, cASN1BMPString, INT2NUM(V_ASN1_BMPSTRING));
1996  rb_global_variable(&class_tag_map);
1997 }
VALUE mOSSL
Definition: ossl.c:259
static int ossl_i2d_ASN1_TYPE(ASN1_TYPE *a, unsigned char **pp)
Definition: ossl_asn1.c:1186
VALUE cASN1Data
Definition: ossl_asn1.c:192
static VALUE decode_enum(unsigned char *der, long length)
Definition: ossl_asn1.c:402
#define rb_str_new4
Definition: intern.h:842
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1171
#define ossl_asn1_get_tagging(o)
Definition: ossl_asn1.c:179
#define rb_hash_lookup
Definition: tcltklib.c:269
#define INT2NUM(x)
Definition: ruby.h:1288
VALUE cASN1ISO64String
Definition: ossl_asn1.c:204
VALUE rb_String(VALUE)
Definition: object.c:2986
static ASN1_NULL * obj_to_asn1null(VALUE obj)
Definition: ossl_asn1.c:280
static ID sIMPLICIT
Definition: ossl_asn1.c:211
#define NUM2INT(x)
Definition: ruby.h:630
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:1655
static ID sAPPLICATION
Definition: ossl_asn1.c:212
#define CLASS_OF(v)
Definition: ruby.h:440
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
#define Qtrue
Definition: ruby.h:426
static VALUE ossl_asn1obj_get_oid(VALUE self)
Definition: ossl_asn1.c:1432
#define ossl_str_adjust(str, p)
Definition: ossl.h:138
VALUE rb_ary_each(VALUE array)
Definition: array.c:1778
VALUE cASN1Constructive
Definition: ossl_asn1.c:194
VALUE cASN1PrintableString
Definition: ossl_asn1.c:201
long tv_sec
Definition: ossl_asn1.c:17
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
static VALUE ossl_asn1obj_s_register(VALUE self, VALUE oid, VALUE sn, VALUE ln)
Definition: ossl_asn1.c:1373
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:896
#define rb_long2int(n)
Definition: ruby.h:317
#define SYM2ID(x)
Definition: ruby.h:356
static VALUE ossl_asn1data_to_der(VALUE self)
Definition: ossl_asn1.c:762
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:676
#define ossl_asn1_set_value(o, v)
Definition: ossl_asn1.c:183
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Definition: object.c:652
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:827
VALUE rb_block_call(VALUE, ID, int, const VALUE *, rb_block_call_func_t, VALUE)
VALUE asn1time_to_time(ASN1_TIME *time)
Definition: ossl_asn1.c:32
#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
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:1415
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1506
#define ossl_asn1_set_tagging(o, v)
Definition: ossl_asn1.c:185
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
void rb_global_variable(VALUE *var)
Definition: gc.c:4962
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Definition: ruby.h:1511
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
VALUE ossl_to_der_if_possible(VALUE obj)
Definition: ossl.c:283
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1393
static ID sivVALUE
Definition: ossl_asn1.c:213
#define val
VALUE cASN1UniversalString
Definition: ossl_asn1.c:205
long tv_usec
Definition: ossl_asn1.c:18
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1553
void Init_ossl_asn1()
Definition: ossl_asn1.c:1475
void rb_attr(VALUE, ID, int, int, int)
Definition: vm_method.c:860
#define OSSL_ASN1_DEFINE_CLASS(name, super)
VALUE cASN1Boolean
Definition: ossl_asn1.c:197
VALUE rb_ary_new(void)
Definition: array.c:495
static ID sivTAGGING
Definition: ossl_asn1.c:213
VALUE cASN1ObjectId
Definition: ossl_asn1.c:207
#define NIL_P(v)
Definition: ruby.h:438
static VALUE ossl_asn1_traverse(VALUE self, VALUE obj)
Definition: ossl_asn1.c:1029
#define OSSL_ASN1_IMPL_FACTORY_METHOD(klass)
Definition: ossl_asn1.c:1446
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:2225
void rb_ary_store(VALUE ary, long idx, VALUE val)
Definition: array.c:790
VALUE eOSSLError
Definition: ossl.c:264
int argc
Definition: ruby.c:131
#define Qfalse
Definition: ruby.h:425
VALUE rb_Integer(VALUE)
Definition: object.c:2736
static ID sivINFINITE_LENGTH
Definition: ossl_asn1.c:213
static VALUE ossl_asn1_decode(VALUE self, VALUE obj)
Definition: ossl_asn1.c:1057
static int ossl_asn1_tag(VALUE obj)
Definition: ossl_asn1.c:634
#define rb_str_new2
Definition: intern.h:840
VALUE rb_obj_alloc(VALUE)
Definition: object.c:1801
VALUE rb_class_superclass(VALUE)
Definition: object.c:1885
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 ossl_asn1_info_size
Definition: ossl_asn1.c:532
VALUE cASN1Set
Definition: ossl_asn1.c:209
static VALUE decode_obj(unsigned char *der, long length)
Definition: ossl_asn1.c:435
void rb_define_alias(VALUE klass, const char *name1, const char *name2)
Defines an alias of a method.
Definition: class.c:1697
#define RSTRING_LEN(str)
Definition: ruby.h:841
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
Definition: class.c:1670
#define ossl_asn1_set_tag(o, v)
Definition: ossl_asn1.c:184
VALUE rb_yield(VALUE)
Definition: vm_eval.c:942
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:1355
static ID sCONTEXT_SPECIFIC
Definition: ossl_asn1.c:212
static VALUE decode_int(unsigned char *der, long length)
Definition: ossl_asn1.c:362
VALUE rb_hash_new(void)
Definition: hash.c:298
static VALUE class_tag_map
Definition: ossl_asn1.c:534
static void ossl_ASN1_TYPE_free(ASN1_TYPE *a)
Definition: ossl_asn1.c:1197
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1728
VALUE cASN1UTF8String
Definition: ossl_asn1.c:200
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1133
static VALUE ossl_asn1_decode0(unsigned char **pp, long length, long *offset, int depth, int yield, long *num_read)
Definition: ossl_asn1.c:935
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
#define PRIsVALUE
Definition: ruby.h:137
unsigned long ID
Definition: ruby.h:89
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:1135
#define Qnil
Definition: ruby.h:427
VALUE cASN1GraphicString
Definition: ossl_asn1.c:203
unsigned long VALUE
Definition: ruby.h:88
#define rb_funcall2
Definition: ruby.h:1456
static VALUE join_der(VALUE enumerable)
Definition: ossl_asn1.c:745
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
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
#define LONG2NUM(x)
Definition: ruby.h:1309
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:766
VALUE cASN1Primitive
Definition: ossl_asn1.c:193
#define RSTRING_PTR(str)
Definition: ruby.h:845
VALUE cASN1T61String
Definition: ossl_asn1.c:202
static VALUE ossl_asn1prim_to_der(VALUE self)
Definition: ossl_asn1.c:1216
#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
static VALUE decode_time(unsigned char *der, long length)
Definition: ossl_asn1.c:464
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
static VALUE join_der_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, str))
Definition: ossl_asn1.c:736
static VALUE ossl_asn1cons_to_der(VALUE self)
Definition: ossl_asn1.c:1259
static VALUE ossl_asn1_decode_all(VALUE self, VALUE obj)
Definition: ossl_asn1.c:1087
static VALUE decode_eoc(unsigned char *der, long length)
Definition: ossl_asn1.c:483
#define RTEST(v)
Definition: ruby.h:437
static ID sivUNUSED_BITS
Definition: ossl_asn1.c:213
VALUE ossl_buf2str(char *buf, int len)
Definition: ossl.c:134
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
VALUE cASN1Sequence
Definition: ossl_asn1.c:209
const char * name
Definition: ossl_asn1.c:494
#define assert(condition)
Definition: ossl.h:45
const char * name
Definition: nkf.c:208
#define ID2SYM(x)
Definition: ruby.h:355
static VALUE decode_bool(unsigned char *der, long length)
Definition: ossl_asn1.c:349
#define ossl_asn1_get_tag(o)
Definition: ossl_asn1.c:178
VALUE ossl_bn_new(const BIGNUM *bn)
Definition: ossl_bn.c:43
VALUE cASN1Enumerated
Definition: ossl_asn1.c:198
#define StringValuePtr(v)
Definition: ruby.h:540
#define ossl_asn1_set_infinite_length(o, v)
Definition: ossl_asn1.c:187
VALUE cASN1NumericString
Definition: ossl_asn1.c:201
VALUE cASN1VideotexString
Definition: ossl_asn1.c:202
void rb_warning(const char *fmt,...)
Definition: error.c:236
#define RSTRING_LENINT(str)
Definition: ruby.h:853
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:1394
VALUE rb_define_module(const char *name)
Definition: class.c:746
VALUE rb_cstr_to_inum(const char *str, int base, int badcheck)
Definition: bignum.c:3961
#define rb_intern(str)
#define ossl_asn1_object_size(cons, len, tag)
Definition: ossl_asn1.c:221
#define SYMBOL_P(x)
Definition: ruby.h:354
VALUE cASN1GeneralString
Definition: ossl_asn1.c:204
static ASN1_BOOLEAN obj_to_asn1bool(VALUE obj)
Definition: ossl_asn1.c:232
#define NULL
Definition: _sdbm.c:103
static VALUE ossl_asn1eoc_initialize(VALUE self)
Definition: ossl_asn1.c:1171
RUBY_EXTERN VALUE rb_cTime
Definition: ruby.h:1587
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:1488
VALUE cASN1GeneralizedTime
Definition: ossl_asn1.c:208
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2298
static VALUE decode_null(unsigned char *der, long length)
Definition: ossl_asn1.c:421
#define NUM2LONG(x)
Definition: ruby.h:600
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_attr_get(VALUE, ID)
Definition: variable.c:1127
char ** argv
Definition: ruby.c:132
#define StringValue(v)
Definition: ruby.h:539
struct timeval rb_time_timeval(VALUE)
Definition: time.c:2417
VALUE rb_obj_class(VALUE)
Definition: object.c:226
VALUE rb_str_new(const char *, long)
Definition: string.c:534
ASN1_INTEGER * num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)
Definition: ossl_asn1.c:157