openssl 2.0.9 → 2.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of openssl might be problematic. Click here for more details.

Files changed (60) hide show
  1. checksums.yaml +5 -5
  2. data/History.md +28 -69
  3. data/README.md +1 -1
  4. data/ext/openssl/deprecation.rb +0 -3
  5. data/ext/openssl/extconf.rb +8 -52
  6. data/ext/openssl/openssl_missing.c +0 -67
  7. data/ext/openssl/openssl_missing.h +3 -50
  8. data/ext/openssl/ossl.c +81 -74
  9. data/ext/openssl/ossl.h +14 -27
  10. data/ext/openssl/ossl_asn1.c +287 -374
  11. data/ext/openssl/ossl_asn1.h +0 -4
  12. data/ext/openssl/ossl_bio.c +5 -20
  13. data/ext/openssl/ossl_bio.h +0 -2
  14. data/ext/openssl/ossl_bn.c +70 -28
  15. data/ext/openssl/ossl_cipher.c +18 -42
  16. data/ext/openssl/ossl_cipher.h +1 -1
  17. data/ext/openssl/ossl_digest.c +8 -12
  18. data/ext/openssl/ossl_digest.h +1 -1
  19. data/ext/openssl/ossl_engine.c +47 -47
  20. data/ext/openssl/ossl_hmac.c +19 -22
  21. data/ext/openssl/ossl_kdf.c +221 -0
  22. data/ext/openssl/ossl_kdf.h +6 -0
  23. data/ext/openssl/ossl_ns_spki.c +17 -21
  24. data/ext/openssl/ossl_ocsp.c +85 -80
  25. data/ext/openssl/ossl_pkcs12.c +15 -21
  26. data/ext/openssl/ossl_pkcs7.c +8 -21
  27. data/ext/openssl/ossl_pkey.c +24 -48
  28. data/ext/openssl/ossl_pkey.h +1 -6
  29. data/ext/openssl/ossl_pkey_dh.c +11 -11
  30. data/ext/openssl/ossl_pkey_dsa.c +16 -22
  31. data/ext/openssl/ossl_pkey_ec.c +43 -56
  32. data/ext/openssl/ossl_pkey_rsa.c +19 -19
  33. data/ext/openssl/ossl_rand.c +12 -12
  34. data/ext/openssl/ossl_ssl.c +291 -243
  35. data/ext/openssl/ossl_ssl.h +0 -5
  36. data/ext/openssl/ossl_ssl_session.c +7 -9
  37. data/ext/openssl/ossl_version.h +1 -1
  38. data/ext/openssl/ossl_x509.c +0 -15
  39. data/ext/openssl/ossl_x509.h +0 -7
  40. data/ext/openssl/ossl_x509attr.c +3 -7
  41. data/ext/openssl/ossl_x509cert.c +17 -54
  42. data/ext/openssl/ossl_x509crl.c +15 -25
  43. data/ext/openssl/ossl_x509ext.c +9 -14
  44. data/ext/openssl/ossl_x509name.c +76 -41
  45. data/ext/openssl/ossl_x509req.c +10 -47
  46. data/ext/openssl/ossl_x509revoked.c +8 -8
  47. data/ext/openssl/ossl_x509store.c +15 -45
  48. data/ext/openssl/ruby_missing.h +2 -13
  49. data/lib/openssl.rb +1 -0
  50. data/lib/openssl/bn.rb +2 -1
  51. data/lib/openssl/buffering.rb +24 -23
  52. data/lib/openssl/config.rb +12 -11
  53. data/lib/openssl/digest.rb +3 -6
  54. data/lib/openssl/pkcs5.rb +22 -0
  55. data/lib/openssl/pkey.rb +0 -41
  56. data/lib/openssl/ssl.rb +118 -16
  57. data/lib/openssl/x509.rb +7 -1
  58. metadata +8 -7
  59. data/ext/openssl/ossl_pkcs5.c +0 -180
  60. data/ext/openssl/ossl_pkcs5.h +0 -6
@@ -56,29 +56,29 @@ extern VALUE eOSSLError;
56
56
  }\
57
57
  } while (0)
58
58
 
59
- #define OSSL_Check_Instance(obj, klass) do {\
60
- if (!rb_obj_is_instance_of((obj), (klass))) {\
61
- ossl_raise(rb_eTypeError, "wrong argument (%"PRIsVALUE")! (Expected instance of %"PRIsVALUE")",\
62
- rb_obj_class(obj), (klass));\
63
- }\
64
- } while (0)
65
-
66
- #define OSSL_Check_Same_Class(obj1, obj2) do {\
67
- if (!rb_obj_is_instance_of((obj1), rb_obj_class(obj2))) {\
68
- ossl_raise(rb_eTypeError, "wrong argument type");\
69
- }\
70
- } while (0)
59
+ /*
60
+ * Type conversions
61
+ */
62
+ #if !defined(NUM2UINT64T) /* in case Ruby starts to provide */
63
+ # if SIZEOF_LONG == 8
64
+ # define NUM2UINT64T(x) ((uint64_t)NUM2ULONG(x))
65
+ # elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
66
+ # define NUM2UINT64T(x) ((uint64_t)NUM2ULL(x))
67
+ # else
68
+ # error "unknown platform; no 64-bit width integer"
69
+ # endif
70
+ #endif
71
71
 
72
72
  /*
73
73
  * Data Conversion
74
74
  */
75
- STACK_OF(X509) *ossl_x509_ary2sk0(VALUE);
76
75
  STACK_OF(X509) *ossl_x509_ary2sk(VALUE);
77
76
  STACK_OF(X509) *ossl_protect_x509_ary2sk(VALUE,int*);
78
77
  VALUE ossl_x509_sk2ary(const STACK_OF(X509) *certs);
79
78
  VALUE ossl_x509crl_sk2ary(const STACK_OF(X509_CRL) *crl);
80
79
  VALUE ossl_x509name_sk2ary(const STACK_OF(X509_NAME) *names);
81
80
  VALUE ossl_buf2str(char *buf, int len);
81
+ VALUE ossl_str_new(const char *, long, int *);
82
82
  #define ossl_str_adjust(str, p) \
83
83
  do{\
84
84
  long len = RSTRING_LEN(str);\
@@ -115,7 +115,6 @@ int ossl_pem_passwd_cb(char *, int, int, void *);
115
115
  /*
116
116
  * ERRor messages
117
117
  */
118
- #define OSSL_ErrMsg() ERR_reason_error_string(ERR_get_error())
119
118
  NORETURN(void ossl_raise(VALUE, const char *, ...));
120
119
  /* Clear OpenSSL error queue. If dOSSL is set, rb_warn() them. */
121
120
  void ossl_clear_error(void);
@@ -123,7 +122,6 @@ void ossl_clear_error(void);
123
122
  /*
124
123
  * String to DER String
125
124
  */
126
- extern ID ossl_s_to_der;
127
125
  VALUE ossl_to_der(VALUE);
128
126
  VALUE ossl_to_der_if_possible(VALUE);
129
127
 
@@ -141,20 +139,9 @@ extern VALUE dOSSL;
141
139
  } \
142
140
  } while (0)
143
141
 
144
- #define OSSL_Warning(fmt, ...) do { \
145
- OSSL_Debug((fmt), ##__VA_ARGS__); \
146
- rb_warning((fmt), ##__VA_ARGS__); \
147
- } while (0)
148
-
149
- #define OSSL_Warn(fmt, ...) do { \
150
- OSSL_Debug((fmt), ##__VA_ARGS__); \
151
- rb_warn((fmt), ##__VA_ARGS__); \
152
- } while (0)
153
142
  #else
154
143
  void ossl_debug(const char *, ...);
155
144
  #define OSSL_Debug ossl_debug
156
- #define OSSL_Warning rb_warning
157
- #define OSSL_Warn rb_warn
158
145
  #endif
159
146
 
160
147
  /*
@@ -173,13 +160,13 @@ void ossl_debug(const char *, ...);
173
160
  #include "ossl_ocsp.h"
174
161
  #include "ossl_pkcs12.h"
175
162
  #include "ossl_pkcs7.h"
176
- #include "ossl_pkcs5.h"
177
163
  #include "ossl_pkey.h"
178
164
  #include "ossl_rand.h"
179
165
  #include "ossl_ssl.h"
180
166
  #include "ossl_version.h"
181
167
  #include "ossl_x509.h"
182
168
  #include "ossl_engine.h"
169
+ #include "ossl_kdf.h"
183
170
 
184
171
  void Init_openssl(void);
185
172
 
@@ -9,11 +9,9 @@
9
9
  */
10
10
  #include "ossl.h"
11
11
 
12
- static VALUE join_der(VALUE enumerable);
13
12
  static VALUE ossl_asn1_decode0(unsigned char **pp, long length, long *offset,
14
13
  int depth, int yield, long *num_read);
15
14
  static VALUE ossl_asn1_initialize(int argc, VALUE *argv, VALUE self);
16
- static VALUE ossl_asn1eoc_initialize(VALUE self);
17
15
 
18
16
  /*
19
17
  * DATE conversion
@@ -25,7 +23,6 @@ asn1time_to_time(const ASN1_TIME *time)
25
23
  VALUE argv[6];
26
24
  int count;
27
25
 
28
- if (!time || !time->data) return Qnil;
29
26
  memset(&tm, 0, sizeof(struct tm));
30
27
 
31
28
  switch (time->type) {
@@ -72,7 +69,6 @@ asn1time_to_time(const ASN1_TIME *time)
72
69
  return rb_funcall2(rb_cTime, rb_intern("utc"), 6, argv);
73
70
  }
74
71
 
75
- #if defined(HAVE_ASN1_TIME_ADJ)
76
72
  void
77
73
  ossl_time_split(VALUE time, time_t *sec, int *days)
78
74
  {
@@ -88,13 +84,6 @@ ossl_time_split(VALUE time, time_t *sec, int *days)
88
84
  *sec = NUM2TIMET(rb_funcall(num, rb_intern("%"), 1, INT2FIX(86400)));
89
85
  }
90
86
  }
91
- #else
92
- time_t
93
- time_to_time_t(VALUE time)
94
- {
95
- return (time_t)NUM2TIMET(rb_Integer(time));
96
- }
97
- #endif
98
87
 
99
88
  /*
100
89
  * STRING conversion
@@ -155,13 +144,13 @@ num_to_asn1integer(VALUE obj, ASN1_INTEGER *ai)
155
144
  #define ossl_asn1_get_tag(o) rb_attr_get((o),sivTAG)
156
145
  #define ossl_asn1_get_tagging(o) rb_attr_get((o),sivTAGGING)
157
146
  #define ossl_asn1_get_tag_class(o) rb_attr_get((o),sivTAG_CLASS)
158
- #define ossl_asn1_get_infinite_length(o) rb_attr_get((o),sivINFINITE_LENGTH)
147
+ #define ossl_asn1_get_indefinite_length(o) rb_attr_get((o),sivINDEFINITE_LENGTH)
159
148
 
160
149
  #define ossl_asn1_set_value(o,v) rb_ivar_set((o),sivVALUE,(v))
161
150
  #define ossl_asn1_set_tag(o,v) rb_ivar_set((o),sivTAG,(v))
162
151
  #define ossl_asn1_set_tagging(o,v) rb_ivar_set((o),sivTAGGING,(v))
163
152
  #define ossl_asn1_set_tag_class(o,v) rb_ivar_set((o),sivTAG_CLASS,(v))
164
- #define ossl_asn1_set_infinite_length(o,v) rb_ivar_set((o),sivINFINITE_LENGTH,(v))
153
+ #define ossl_asn1_set_indefinite_length(o,v) rb_ivar_set((o),sivINDEFINITE_LENGTH,(v))
165
154
 
166
155
  VALUE mASN1;
167
156
  VALUE eASN1Error;
@@ -187,7 +176,7 @@ VALUE cASN1Sequence, cASN1Set; /* CONSTRUCTIVE */
187
176
 
188
177
  static VALUE sym_IMPLICIT, sym_EXPLICIT;
189
178
  static VALUE sym_UNIVERSAL, sym_APPLICATION, sym_CONTEXT_SPECIFIC, sym_PRIVATE;
190
- static ID sivVALUE, sivTAG, sivTAG_CLASS, sivTAGGING, sivINFINITE_LENGTH, sivUNUSED_BITS;
179
+ static ID sivVALUE, sivTAG, sivTAG_CLASS, sivTAGGING, sivINDEFINITE_LENGTH, sivUNUSED_BITS;
191
180
  static ID id_each;
192
181
 
193
182
  /*
@@ -213,13 +202,15 @@ obj_to_asn1bstr(VALUE obj, long unused_bits)
213
202
  {
214
203
  ASN1_BIT_STRING *bstr;
215
204
 
216
- if(unused_bits < 0) unused_bits = 0;
205
+ if (unused_bits < 0 || unused_bits > 7)
206
+ ossl_raise(eASN1Error, "unused_bits for a bitstring value must be in "\
207
+ "the range 0 to 7");
217
208
  StringValue(obj);
218
209
  if(!(bstr = ASN1_BIT_STRING_new()))
219
210
  ossl_raise(eASN1Error, NULL);
220
211
  ASN1_BIT_STRING_set(bstr, (unsigned char *)RSTRING_PTR(obj), RSTRING_LENINT(obj));
221
212
  bstr->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); /* clear */
222
- bstr->flags |= ASN1_STRING_FLAG_BITS_LEFT|(unused_bits&0x07);
213
+ bstr->flags |= ASN1_STRING_FLAG_BITS_LEFT | unused_bits;
223
214
 
224
215
  return bstr;
225
216
  }
@@ -269,15 +260,10 @@ obj_to_asn1utime(VALUE time)
269
260
  time_t sec;
270
261
  ASN1_UTCTIME *t;
271
262
 
272
- #if defined(HAVE_ASN1_TIME_ADJ)
273
263
  int off_days;
274
264
 
275
265
  ossl_time_split(time, &sec, &off_days);
276
266
  if (!(t = ASN1_UTCTIME_adj(NULL, sec, off_days, 0)))
277
- #else
278
- sec = time_to_time_t(time);
279
- if (!(t = ASN1_UTCTIME_set(NULL, sec)))
280
- #endif
281
267
  ossl_raise(eASN1Error, NULL);
282
268
 
283
269
  return t;
@@ -289,15 +275,10 @@ obj_to_asn1gtime(VALUE time)
289
275
  time_t sec;
290
276
  ASN1_GENERALIZEDTIME *t;
291
277
 
292
- #if defined(HAVE_ASN1_TIME_ADJ)
293
278
  int off_days;
294
279
 
295
280
  ossl_time_split(time, &sec, &off_days);
296
281
  if (!(t = ASN1_GENERALIZEDTIME_adj(NULL, sec, off_days, 0)))
297
- #else
298
- sec = time_to_time_t(time);
299
- if (!(t = ASN1_GENERALIZEDTIME_set(NULL, sec)))
300
- #endif
301
282
  ossl_raise(eASN1Error, NULL);
302
283
 
303
284
  return t;
@@ -517,7 +498,7 @@ ossl_asn1_get_asn1type(VALUE obj)
517
498
  VALUE value, rflag;
518
499
  void *ptr;
519
500
  void (*free_func)();
520
- int tag, flag;
501
+ int tag;
521
502
 
522
503
  tag = ossl_asn1_default_tag(obj);
523
504
  value = ossl_asn1_get_value(obj);
@@ -533,8 +514,7 @@ ossl_asn1_get_asn1type(VALUE obj)
533
514
  break;
534
515
  case V_ASN1_BIT_STRING:
535
516
  rflag = rb_attr_get(obj, sivUNUSED_BITS);
536
- flag = NIL_P(rflag) ? -1 : NUM2INT(rflag);
537
- ptr = obj_to_asn1bstr(value, flag);
517
+ ptr = obj_to_asn1bstr(value, NUM2INT(rflag));
538
518
  free_func = ASN1_BIT_STRING_free;
539
519
  break;
540
520
  case V_ASN1_NULL:
@@ -598,8 +578,8 @@ ossl_asn1_default_tag(VALUE obj)
598
578
  return NUM2INT(tag);
599
579
  tmp_class = rb_class_superclass(tmp_class);
600
580
  }
601
- ossl_raise(eASN1Error, "universal tag for %"PRIsVALUE" not found",
602
- rb_obj_class(obj));
581
+
582
+ return -1;
603
583
  }
604
584
 
605
585
  static int
@@ -614,20 +594,6 @@ ossl_asn1_tag(VALUE obj)
614
594
  return NUM2INT(tag);
615
595
  }
616
596
 
617
- static int
618
- ossl_asn1_is_explicit(VALUE obj)
619
- {
620
- VALUE s;
621
-
622
- s = ossl_asn1_get_tagging(obj);
623
- if (NIL_P(s) || s == sym_IMPLICIT)
624
- return 0;
625
- else if (s == sym_EXPLICIT)
626
- return 1;
627
- else
628
- ossl_raise(eASN1Error, "invalid tag default");
629
- }
630
-
631
597
  static int
632
598
  ossl_asn1_tag_class(VALUE obj)
633
599
  {
@@ -663,12 +629,12 @@ ossl_asn1_class2sym(int tc)
663
629
  * call-seq:
664
630
  * OpenSSL::ASN1::ASN1Data.new(value, tag, tag_class) => ASN1Data
665
631
  *
666
- * +value+: Please have a look at Constructive and Primitive to see how Ruby
632
+ * _value_: Please have a look at Constructive and Primitive to see how Ruby
667
633
  * types are mapped to ASN.1 types and vice versa.
668
634
  *
669
- * +tag+: A +Number+ indicating the tag number.
635
+ * _tag_: An Integer indicating the tag number.
670
636
  *
671
- * +tag_class+: A +Symbol+ indicating the tag class. Please cf. ASN1 for
637
+ * _tag_class_: A Symbol indicating the tag class. Please cf. ASN1 for
672
638
  * possible values.
673
639
  *
674
640
  * == Example
@@ -680,73 +646,85 @@ ossl_asn1data_initialize(VALUE self, VALUE value, VALUE tag, VALUE tag_class)
680
646
  {
681
647
  if(!SYMBOL_P(tag_class))
682
648
  ossl_raise(eASN1Error, "invalid tag class");
683
- if (tag_class == sym_UNIVERSAL && NUM2INT(tag) > 31)
684
- ossl_raise(eASN1Error, "tag number for Universal too large");
685
649
  ossl_asn1_set_tag(self, tag);
686
650
  ossl_asn1_set_value(self, value);
687
651
  ossl_asn1_set_tag_class(self, tag_class);
688
- ossl_asn1_set_infinite_length(self, Qfalse);
652
+ ossl_asn1_set_indefinite_length(self, Qfalse);
689
653
 
690
654
  return self;
691
655
  }
692
656
 
693
657
  static VALUE
694
- join_der_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, str))
658
+ to_der_internal(VALUE self, int constructed, int indef_len, VALUE body)
695
659
  {
696
- i = ossl_to_der_if_possible(i);
697
- StringValue(i);
698
- rb_str_append(str, i);
699
- return Qnil;
700
- }
660
+ int encoding = constructed ? indef_len ? 2 : 1 : 0;
661
+ int tag_class = ossl_asn1_tag_class(self);
662
+ int tag_number = ossl_asn1_tag(self);
663
+ int default_tag_number = ossl_asn1_default_tag(self);
664
+ int body_length, total_length;
665
+ VALUE str;
666
+ unsigned char *p;
701
667
 
702
- static VALUE
703
- join_der(VALUE enumerable)
704
- {
705
- VALUE str = rb_str_new(0, 0);
706
- rb_block_call(enumerable, id_each, 0, 0, join_der_i, str);
668
+ body_length = RSTRING_LENINT(body);
669
+ if (ossl_asn1_get_tagging(self) == sym_EXPLICIT) {
670
+ int inner_length, e_encoding = indef_len ? 2 : 1;
671
+
672
+ if (default_tag_number == -1)
673
+ ossl_raise(eASN1Error, "explicit tagging of unknown tag");
674
+
675
+ inner_length = ASN1_object_size(encoding, body_length, default_tag_number);
676
+ total_length = ASN1_object_size(e_encoding, inner_length, tag_number);
677
+ str = rb_str_new(NULL, total_length);
678
+ p = (unsigned char *)RSTRING_PTR(str);
679
+ /* Put explicit tag */
680
+ ASN1_put_object(&p, e_encoding, inner_length, tag_number, tag_class);
681
+ /* Append inner object */
682
+ ASN1_put_object(&p, encoding, body_length, default_tag_number, V_ASN1_UNIVERSAL);
683
+ memcpy(p, RSTRING_PTR(body), body_length);
684
+ p += body_length;
685
+ if (indef_len) {
686
+ ASN1_put_eoc(&p); /* For inner object */
687
+ ASN1_put_eoc(&p); /* For wrapper object */
688
+ }
689
+ }
690
+ else {
691
+ total_length = ASN1_object_size(encoding, body_length, tag_number);
692
+ str = rb_str_new(NULL, total_length);
693
+ p = (unsigned char *)RSTRING_PTR(str);
694
+ ASN1_put_object(&p, encoding, body_length, tag_number, tag_class);
695
+ memcpy(p, RSTRING_PTR(body), body_length);
696
+ p += body_length;
697
+ if (indef_len)
698
+ ASN1_put_eoc(&p);
699
+ }
700
+ assert(p - (unsigned char *)RSTRING_PTR(str) == total_length);
707
701
  return str;
708
702
  }
709
703
 
704
+ static VALUE ossl_asn1prim_to_der(VALUE);
705
+ static VALUE ossl_asn1cons_to_der(VALUE);
710
706
  /*
711
707
  * call-seq:
712
708
  * asn1.to_der => DER-encoded String
713
709
  *
714
710
  * Encodes this ASN1Data into a DER-encoded String value. The result is
715
- * DER-encoded except for the possibility of infinite length encodings.
716
- * Infinite length encodings are not allowed in strict DER, so strictly
717
- * speaking the result of such an encoding would be a BER-encoding.
711
+ * DER-encoded except for the possibility of indefinite length forms.
712
+ * Indefinite length forms are not allowed in strict DER, so strictly speaking
713
+ * the result of such an encoding would be a BER-encoding.
718
714
  */
719
715
  static VALUE
720
716
  ossl_asn1data_to_der(VALUE self)
721
717
  {
722
- VALUE value, der, inf_length;
723
- int tag, tag_class, is_cons = 0;
724
- long length;
725
- unsigned char *p;
726
-
727
- value = ossl_asn1_get_value(self);
728
- if(rb_obj_is_kind_of(value, rb_cArray)){
729
- is_cons = 1;
730
- value = join_der(value);
731
- }
732
- StringValue(value);
718
+ VALUE value = ossl_asn1_get_value(self);
733
719
 
734
- tag = ossl_asn1_tag(self);
735
- tag_class = ossl_asn1_tag_class(self);
736
- inf_length = ossl_asn1_get_infinite_length(self);
737
- if (inf_length == Qtrue) {
738
- is_cons = 2;
720
+ if (rb_obj_is_kind_of(value, rb_cArray))
721
+ return ossl_asn1cons_to_der(self);
722
+ else {
723
+ if (RTEST(ossl_asn1_get_indefinite_length(self)))
724
+ ossl_raise(eASN1Error, "indefinite length form cannot be used " \
725
+ "with primitive encoding");
726
+ return ossl_asn1prim_to_der(self);
739
727
  }
740
- if((length = ASN1_object_size(is_cons, RSTRING_LENINT(value), tag)) <= 0)
741
- ossl_raise(eASN1Error, NULL);
742
- der = rb_str_new(0, length);
743
- p = (unsigned char *)RSTRING_PTR(der);
744
- ASN1_put_object(&p, is_cons, RSTRING_LENINT(value), tag, tag_class);
745
- memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value));
746
- p += RSTRING_LEN(value);
747
- ossl_str_adjust(der, p);
748
-
749
- return der;
750
728
  }
751
729
 
752
730
  static VALUE
@@ -829,46 +807,33 @@ int_ossl_asn1_decode0_cons(unsigned char **pp, long max_len, long length,
829
807
  int tag, VALUE tc, long *num_read)
830
808
  {
831
809
  VALUE value, asn1data, ary;
832
- int infinite;
810
+ int indefinite;
833
811
  long available_len, off = *offset;
834
812
 
835
- infinite = (j == 0x21);
813
+ indefinite = (j == 0x21);
836
814
  ary = rb_ary_new();
837
815
 
838
- available_len = infinite ? max_len : length;
816
+ available_len = indefinite ? max_len : length;
839
817
  while (available_len > 0) {
840
818
  long inner_read = 0;
841
819
  value = ossl_asn1_decode0(pp, available_len, &off, depth + 1, yield, &inner_read);
842
820
  *num_read += inner_read;
843
821
  available_len -= inner_read;
844
- rb_ary_push(ary, value);
845
822
 
846
- if (infinite &&
847
- NUM2INT(ossl_asn1_get_tag(value)) == V_ASN1_EOC &&
823
+ if (indefinite &&
824
+ ossl_asn1_tag(value) == V_ASN1_EOC &&
848
825
  ossl_asn1_get_tag_class(value) == sym_UNIVERSAL) {
849
826
  break;
850
827
  }
828
+ rb_ary_push(ary, value);
851
829
  }
852
830
 
853
831
  if (tc == sym_UNIVERSAL) {
854
832
  VALUE args[4];
855
- int not_sequence_or_set;
856
-
857
- not_sequence_or_set = tag != V_ASN1_SEQUENCE && tag != V_ASN1_SET;
858
-
859
- if (not_sequence_or_set) {
860
- if (infinite) {
861
- asn1data = rb_obj_alloc(cASN1Constructive);
862
- }
863
- else {
864
- ossl_raise(eASN1Error, "invalid non-infinite tag");
865
- return Qnil;
866
- }
867
- }
868
- else {
869
- VALUE klass = *ossl_asn1_info[tag].klass;
870
- asn1data = rb_obj_alloc(klass);
871
- }
833
+ if (tag == V_ASN1_SEQUENCE || tag == V_ASN1_SET)
834
+ asn1data = rb_obj_alloc(*ossl_asn1_info[tag].klass);
835
+ else
836
+ asn1data = rb_obj_alloc(cASN1Constructive);
872
837
  args[0] = ary;
873
838
  args[1] = INT2NUM(tag);
874
839
  args[2] = Qnil;
@@ -880,10 +845,10 @@ int_ossl_asn1_decode0_cons(unsigned char **pp, long max_len, long length,
880
845
  ossl_asn1data_initialize(asn1data, ary, INT2NUM(tag), tc);
881
846
  }
882
847
 
883
- if (infinite)
884
- ossl_asn1_set_infinite_length(asn1data, Qtrue);
848
+ if (indefinite)
849
+ ossl_asn1_set_indefinite_length(asn1data, Qtrue);
885
850
  else
886
- ossl_asn1_set_infinite_length(asn1data, Qfalse);
851
+ ossl_asn1_set_indefinite_length(asn1data, Qfalse);
887
852
 
888
853
  *offset = off;
889
854
  return asn1data;
@@ -936,7 +901,8 @@ ossl_asn1_decode0(unsigned char **pp, long length, long *offset, int depth,
936
901
  inner_read += hlen;
937
902
  }
938
903
  else {
939
- if ((j & 0x01) && (len == 0)) ossl_raise(eASN1Error, "Infinite length for primitive value");
904
+ if ((j & 0x01) && (len == 0))
905
+ ossl_raise(eASN1Error, "indefinite length for primitive value");
940
906
  asn1data = int_ossl_asn1_decode0_prim(pp, len, hlen, tag, tag_class, &inner_read);
941
907
  off += hlen + len;
942
908
  }
@@ -968,13 +934,13 @@ int_ossl_decode_sanity_check(long len, long read, long offset)
968
934
  *
969
935
  * If a block is given, it prints out each of the elements encountered.
970
936
  * Block parameters are (in that order):
971
- * * depth: The recursion depth, plus one with each constructed value being encountered (Number)
972
- * * offset: Current byte offset (Number)
973
- * * header length: Combined length in bytes of the Tag and Length headers. (Number)
974
- * * length: The overall remaining length of the entire data (Number)
937
+ * * depth: The recursion depth, plus one with each constructed value being encountered (Integer)
938
+ * * offset: Current byte offset (Integer)
939
+ * * header length: Combined length in bytes of the Tag and Length headers. (Integer)
940
+ * * length: The overall remaining length of the entire data (Integer)
975
941
  * * constructed: Whether this value is constructed or not (Boolean)
976
942
  * * tag_class: Current tag class (Symbol)
977
- * * tag: The current tag (Number)
943
+ * * tag: The current tag number (Integer)
978
944
  *
979
945
  * == Example
980
946
  * der = File.binread('asn1data.der')
@@ -1004,9 +970,9 @@ ossl_asn1_traverse(VALUE self, VALUE obj)
1004
970
  * call-seq:
1005
971
  * OpenSSL::ASN1.decode(der) -> ASN1Data
1006
972
  *
1007
- * Decodes a BER- or DER-encoded value and creates an ASN1Data instance. +der+
1008
- * may be a +String+ or any object that features a +#to_der+ method transforming
1009
- * it into a BER-/DER-encoded +String+.
973
+ * Decodes a BER- or DER-encoded value and creates an ASN1Data instance. _der_
974
+ * may be a String or any object that features a +.to_der+ method transforming
975
+ * it into a BER-/DER-encoded String+
1010
976
  *
1011
977
  * == Example
1012
978
  * der = File.binread('asn1data')
@@ -1034,9 +1000,9 @@ ossl_asn1_decode(VALUE self, VALUE obj)
1034
1000
  * call-seq:
1035
1001
  * OpenSSL::ASN1.decode_all(der) -> Array of ASN1Data
1036
1002
  *
1037
- * Similar to +decode+ with the difference that +decode+ expects one
1038
- * distinct value represented in +der+. +decode_all+ on the contrary
1039
- * decodes a sequence of sequential BER/DER values lined up in +der+
1003
+ * Similar to #decode with the difference that #decode expects one
1004
+ * distinct value represented in _der_. #decode_all on the contrary
1005
+ * decodes a sequence of sequential BER/DER values lined up in _der_
1040
1006
  * and returns them as an array.
1041
1007
  *
1042
1008
  * == Example
@@ -1071,19 +1037,19 @@ ossl_asn1_decode_all(VALUE self, VALUE obj)
1071
1037
 
1072
1038
  /*
1073
1039
  * call-seq:
1074
- * OpenSSL::ASN1::Primitive.new( value [, tag, tagging, tag_class ]) => Primitive
1040
+ * OpenSSL::ASN1::Primitive.new(value [, tag, tagging, tag_class ]) => Primitive
1075
1041
  *
1076
- * +value+: is mandatory.
1042
+ * _value_: is mandatory.
1077
1043
  *
1078
- * +tag+: optional, may be specified for tagged values. If no +tag+ is
1044
+ * _tag_: optional, may be specified for tagged values. If no _tag_ is
1079
1045
  * specified, the UNIVERSAL tag corresponding to the Primitive sub-class
1080
1046
  * is used by default.
1081
1047
  *
1082
- * +tagging+: may be used as an encoding hint to encode a value either
1048
+ * _tagging_: may be used as an encoding hint to encode a value either
1083
1049
  * explicitly or implicitly, see ASN1 for possible values.
1084
1050
  *
1085
- * +tag_class+: if +tag+ and +tagging+ are +nil+ then this is set to
1086
- * +:UNIVERSAL+ by default. If either +tag+ or +tagging+ are set then
1051
+ * _tag_class_: if _tag_ and _tagging_ are +nil+ then this is set to
1052
+ * +:UNIVERSAL+ by default. If either _tag_ or _tagging_ are set then
1087
1053
  * +:CONTEXT_SPECIFIC+ is used as the default. For possible values please
1088
1054
  * cf. ASN1.
1089
1055
  *
@@ -1096,9 +1062,12 @@ static VALUE
1096
1062
  ossl_asn1_initialize(int argc, VALUE *argv, VALUE self)
1097
1063
  {
1098
1064
  VALUE value, tag, tagging, tag_class;
1065
+ int default_tag;
1099
1066
 
1100
1067
  rb_scan_args(argc, argv, "13", &value, &tag, &tagging, &tag_class);
1101
- if(argc > 1){
1068
+ default_tag = ossl_asn1_default_tag(self);
1069
+
1070
+ if (default_tag == -1 || argc > 1) {
1102
1071
  if(NIL_P(tag))
1103
1072
  ossl_raise(eASN1Error, "must specify tag number");
1104
1073
  if(!NIL_P(tagging) && !SYMBOL_P(tagging))
@@ -1111,11 +1080,9 @@ ossl_asn1_initialize(int argc, VALUE *argv, VALUE self)
1111
1080
  }
1112
1081
  if(!SYMBOL_P(tag_class))
1113
1082
  ossl_raise(eASN1Error, "invalid tag class");
1114
- if (tagging == sym_IMPLICIT && NUM2INT(tag) > 31)
1115
- ossl_raise(eASN1Error, "tag number for Universal too large");
1116
1083
  }
1117
1084
  else{
1118
- tag = INT2NUM(ossl_asn1_default_tag(self));
1085
+ tag = INT2NUM(default_tag);
1119
1086
  tagging = Qnil;
1120
1087
  tag_class = sym_UNIVERSAL;
1121
1088
  }
@@ -1123,7 +1090,9 @@ ossl_asn1_initialize(int argc, VALUE *argv, VALUE self)
1123
1090
  ossl_asn1_set_value(self, value);
1124
1091
  ossl_asn1_set_tagging(self, tagging);
1125
1092
  ossl_asn1_set_tag_class(self, tag_class);
1126
- ossl_asn1_set_infinite_length(self, Qfalse);
1093
+ ossl_asn1_set_indefinite_length(self, Qfalse);
1094
+ if (default_tag == V_ASN1_BIT_STRING)
1095
+ rb_ivar_set(self, sivUNUSED_BITS, INT2FIX(0));
1127
1096
 
1128
1097
  return self;
1129
1098
  }
@@ -1131,7 +1100,7 @@ ossl_asn1_initialize(int argc, VALUE *argv, VALUE self)
1131
1100
  static VALUE
1132
1101
  ossl_asn1eoc_initialize(VALUE self) {
1133
1102
  VALUE tag, tagging, tag_class, value;
1134
- tag = INT2NUM(ossl_asn1_default_tag(self));
1103
+ tag = INT2FIX(0);
1135
1104
  tagging = Qnil;
1136
1105
  tag_class = sym_UNIVERSAL;
1137
1106
  value = rb_str_new("", 0);
@@ -1139,51 +1108,58 @@ ossl_asn1eoc_initialize(VALUE self) {
1139
1108
  ossl_asn1_set_value(self, value);
1140
1109
  ossl_asn1_set_tagging(self, tagging);
1141
1110
  ossl_asn1_set_tag_class(self, tag_class);
1142
- ossl_asn1_set_infinite_length(self, Qfalse);
1111
+ ossl_asn1_set_indefinite_length(self, Qfalse);
1143
1112
  return self;
1144
1113
  }
1145
1114
 
1115
+ static VALUE
1116
+ ossl_asn1eoc_to_der(VALUE self)
1117
+ {
1118
+ return rb_str_new("\0\0", 2);
1119
+ }
1120
+
1146
1121
  /*
1147
1122
  * call-seq:
1148
1123
  * asn1.to_der => DER-encoded String
1149
1124
  *
1150
- * See ASN1Data#to_der for details. *
1125
+ * See ASN1Data#to_der for details.
1151
1126
  */
1152
1127
  static VALUE
1153
1128
  ossl_asn1prim_to_der(VALUE self)
1154
1129
  {
1155
1130
  ASN1_TYPE *asn1;
1156
- int tn, tc, explicit;
1157
- long len, reallen;
1158
- unsigned char *buf, *p;
1131
+ long alllen, bodylen;
1132
+ unsigned char *p0, *p1;
1133
+ int j, tag, tc, state;
1159
1134
  VALUE str;
1160
1135
 
1161
- tn = NUM2INT(ossl_asn1_get_tag(self));
1162
- tc = ossl_asn1_tag_class(self);
1163
- explicit = ossl_asn1_is_explicit(self);
1164
- asn1 = ossl_asn1_get_asn1type(self);
1136
+ if (ossl_asn1_default_tag(self) == -1) {
1137
+ str = ossl_asn1_get_value(self);
1138
+ return to_der_internal(self, 0, 0, StringValue(str));
1139
+ }
1165
1140
 
1166
- len = ASN1_object_size(1, i2d_ASN1_TYPE(asn1, NULL), tn);
1167
- if(!(buf = OPENSSL_malloc(len))){
1141
+ asn1 = ossl_asn1_get_asn1type(self);
1142
+ alllen = i2d_ASN1_TYPE(asn1, NULL);
1143
+ if (alllen < 0) {
1168
1144
  ASN1_TYPE_free(asn1);
1169
- ossl_raise(eASN1Error, "cannot alloc buffer");
1145
+ ossl_raise(eASN1Error, "i2d_ASN1_TYPE");
1170
1146
  }
1171
- p = buf;
1172
- if (tc == V_ASN1_UNIVERSAL) {
1173
- i2d_ASN1_TYPE(asn1, &p);
1174
- } else if (explicit) {
1175
- ASN1_put_object(&p, 1, i2d_ASN1_TYPE(asn1, NULL), tn, tc);
1176
- i2d_ASN1_TYPE(asn1, &p);
1177
- } else {
1178
- i2d_ASN1_TYPE(asn1, &p);
1179
- *buf = tc | tn | (*buf & V_ASN1_CONSTRUCTED);
1147
+ str = ossl_str_new(NULL, alllen, &state);
1148
+ if (state) {
1149
+ ASN1_TYPE_free(asn1);
1150
+ rb_jump_tag(state);
1180
1151
  }
1152
+ p0 = p1 = (unsigned char *)RSTRING_PTR(str);
1153
+ i2d_ASN1_TYPE(asn1, &p0);
1181
1154
  ASN1_TYPE_free(asn1);
1182
- reallen = p - buf;
1183
- assert(reallen <= len);
1184
- str = ossl_buf2str((char *)buf, rb_long2int(reallen)); /* buf will be free in ossl_buf2str */
1155
+ assert(p0 - p1 == alllen);
1185
1156
 
1186
- return str;
1157
+ /* Strip header since to_der_internal() wants only the payload */
1158
+ j = ASN1_get_object((const unsigned char **)&p1, &bodylen, &tag, &tc, alllen);
1159
+ if (j & 0x80)
1160
+ ossl_raise(eASN1Error, "ASN1_get_object"); /* should not happen */
1161
+
1162
+ return to_der_internal(self, 0, 0, rb_str_drop_bytes(str, alllen - bodylen));
1187
1163
  }
1188
1164
 
1189
1165
  /*
@@ -1195,92 +1171,41 @@ ossl_asn1prim_to_der(VALUE self)
1195
1171
  static VALUE
1196
1172
  ossl_asn1cons_to_der(VALUE self)
1197
1173
  {
1198
- int tag, tn, tc, explicit, constructed = 1;
1199
- int found_prim = 0, seq_len;
1200
- long length;
1201
- unsigned char *p;
1202
- VALUE value, str, inf_length;
1203
-
1204
- tn = NUM2INT(ossl_asn1_get_tag(self));
1205
- tc = ossl_asn1_tag_class(self);
1206
- inf_length = ossl_asn1_get_infinite_length(self);
1207
- if (inf_length == Qtrue) {
1208
- VALUE ary, example;
1209
- constructed = 2;
1210
- if (rb_obj_class(self) == cASN1Sequence ||
1211
- rb_obj_class(self) == cASN1Set) {
1212
- tag = ossl_asn1_default_tag(self);
1213
- }
1214
- else { /* must be a constructive encoding of a primitive value */
1215
- ary = ossl_asn1_get_value(self);
1216
- if (!rb_obj_is_kind_of(ary, rb_cArray))
1217
- ossl_raise(eASN1Error, "Constructive value must be an Array");
1218
- /* Recursively descend until a primitive value is found.
1219
- The overall value of the entire constructed encoding
1220
- is of the type of the first primitive encoding to be
1221
- found. */
1222
- while (!found_prim){
1223
- example = rb_ary_entry(ary, 0);
1224
- if (rb_obj_is_kind_of(example, cASN1Primitive)){
1225
- found_prim = 1;
1226
- }
1227
- else {
1228
- /* example is another ASN1Constructive */
1229
- if (!rb_obj_is_kind_of(example, cASN1Constructive)){
1230
- ossl_raise(eASN1Error, "invalid constructed encoding");
1231
- return Qnil; /* dummy */
1232
- }
1233
- ary = ossl_asn1_get_value(example);
1234
- }
1235
- }
1236
- tag = ossl_asn1_default_tag(example);
1237
- }
1238
- }
1239
- else {
1240
- if (rb_obj_class(self) == cASN1Constructive)
1241
- ossl_raise(eASN1Error, "Constructive shall only be used with infinite length");
1242
- tag = ossl_asn1_default_tag(self);
1243
- }
1244
- explicit = ossl_asn1_is_explicit(self);
1245
- value = join_der(ossl_asn1_get_value(self));
1246
-
1247
- seq_len = ASN1_object_size(constructed, RSTRING_LENINT(value), tag);
1248
- length = ASN1_object_size(constructed, seq_len, tn);
1249
- str = rb_str_new(0, length);
1250
- p = (unsigned char *)RSTRING_PTR(str);
1251
- if(tc == V_ASN1_UNIVERSAL)
1252
- ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tn, tc);
1253
- else{
1254
- if(explicit){
1255
- ASN1_put_object(&p, constructed, seq_len, tn, tc);
1256
- ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tag, V_ASN1_UNIVERSAL);
1257
- }
1258
- else{
1259
- ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tn, tc);
1174
+ VALUE ary, str;
1175
+ long i;
1176
+ int indef_len;
1177
+
1178
+ indef_len = RTEST(ossl_asn1_get_indefinite_length(self));
1179
+ ary = rb_convert_type(ossl_asn1_get_value(self), T_ARRAY, "Array", "to_a");
1180
+ str = rb_str_new(NULL, 0);
1181
+ for (i = 0; i < RARRAY_LEN(ary); i++) {
1182
+ VALUE item = RARRAY_AREF(ary, i);
1183
+
1184
+ if (indef_len && rb_obj_is_kind_of(item, cASN1EndOfContent)) {
1185
+ if (i != RARRAY_LEN(ary) - 1)
1186
+ ossl_raise(eASN1Error, "illegal EOC octets in value");
1187
+
1188
+ /*
1189
+ * EOC is not really part of the content, but we required to add one
1190
+ * at the end in the past.
1191
+ */
1192
+ break;
1260
1193
  }
1261
- }
1262
- memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value));
1263
- p += RSTRING_LEN(value);
1264
1194
 
1265
- /* In this case we need an additional EOC (one for the explicit part and
1266
- * one for the Constructive itself. The EOC for the Constructive is
1267
- * supplied by the user, but that for the "explicit wrapper" must be
1268
- * added here.
1269
- */
1270
- if (explicit && inf_length == Qtrue) {
1271
- ASN1_put_eoc(&p);
1195
+ item = ossl_to_der_if_possible(item);
1196
+ StringValue(item);
1197
+ rb_str_append(str, item);
1272
1198
  }
1273
- ossl_str_adjust(str, p);
1274
1199
 
1275
- return str;
1200
+ return to_der_internal(self, 1, indef_len, str);
1276
1201
  }
1277
1202
 
1278
1203
  /*
1279
1204
  * call-seq:
1280
1205
  * asn1_ary.each { |asn1| block } => asn1_ary
1281
1206
  *
1282
- * Calls <i>block</i> once for each element in +self+, passing that element
1283
- * as parameter +asn1+. If no block is given, an enumerator is returned
1207
+ * Calls the given block once for each element in self, passing that element
1208
+ * as parameter _asn1_. If no block is given, an enumerator is returned
1284
1209
  * instead.
1285
1210
  *
1286
1211
  * == Example
@@ -1300,8 +1225,8 @@ ossl_asn1cons_each(VALUE self)
1300
1225
  * call-seq:
1301
1226
  * OpenSSL::ASN1::ObjectId.register(object_id, short_name, long_name)
1302
1227
  *
1303
- * This adds a new ObjectId to the internal tables. Where +object_id+ is the
1304
- * numerical form, +short_name+ is the short name, and +long_name+ is the long
1228
+ * This adds a new ObjectId to the internal tables. Where _object_id_ is the
1229
+ * numerical form, _short_name_ is the short name, and _long_name_ is the long
1305
1230
  * name.
1306
1231
  *
1307
1232
  * Returns +true+ if successful. Raises an OpenSSL::ASN1::ASN1Error if it fails.
@@ -1320,14 +1245,13 @@ ossl_asn1obj_s_register(VALUE self, VALUE oid, VALUE sn, VALUE ln)
1320
1245
  return Qtrue;
1321
1246
  }
1322
1247
 
1323
- /* Document-method: OpenSSL::ASN1::ObjectId#sn
1248
+ /*
1249
+ * call-seq:
1250
+ * oid.sn -> string
1251
+ * oid.short_name -> string
1324
1252
  *
1325
1253
  * The short name of the ObjectId, as defined in <openssl/objects.h>.
1326
1254
  */
1327
- /* Document-method: OpenSSL::ASN1::ObjectId#short_name
1328
- *
1329
- * +short_name+ is an alias to +sn+
1330
- */
1331
1255
  static VALUE
1332
1256
  ossl_asn1obj_get_sn(VALUE self)
1333
1257
  {
@@ -1341,14 +1265,13 @@ ossl_asn1obj_get_sn(VALUE self)
1341
1265
  return ret;
1342
1266
  }
1343
1267
 
1344
- /* Document-method: OpenSSL::ASN1::ObjectId#ln
1268
+ /*
1269
+ * call-seq:
1270
+ * oid.ln -> string
1271
+ * oid.long_name -> string
1345
1272
  *
1346
1273
  * The long name of the ObjectId, as defined in <openssl/objects.h>.
1347
1274
  */
1348
- /* Document-method: OpenSSL::ASN1::ObjectId#long_name
1349
- *
1350
- * +long_name+ is an alias to +ln+
1351
- */
1352
1275
  static VALUE
1353
1276
  ossl_asn1obj_get_ln(VALUE self)
1354
1277
  {
@@ -1362,23 +1285,48 @@ ossl_asn1obj_get_ln(VALUE self)
1362
1285
  return ret;
1363
1286
  }
1364
1287
 
1365
- /* Document-method: OpenSSL::ASN1::ObjectId#oid
1288
+ static VALUE
1289
+ asn1obj_get_oid_i(VALUE vobj)
1290
+ {
1291
+ ASN1_OBJECT *a1obj = (void *)vobj;
1292
+ VALUE str;
1293
+ int len;
1294
+
1295
+ str = rb_usascii_str_new(NULL, 127);
1296
+ len = OBJ_obj2txt(RSTRING_PTR(str), RSTRING_LENINT(str), a1obj, 1);
1297
+ if (len <= 0 || len == INT_MAX)
1298
+ ossl_raise(eASN1Error, "OBJ_obj2txt");
1299
+ if (len > RSTRING_LEN(str)) {
1300
+ /* +1 is for the \0 terminator added by OBJ_obj2txt() */
1301
+ rb_str_resize(str, len + 1);
1302
+ len = OBJ_obj2txt(RSTRING_PTR(str), len + 1, a1obj, 1);
1303
+ if (len <= 0)
1304
+ ossl_raise(eASN1Error, "OBJ_obj2txt");
1305
+ }
1306
+ rb_str_set_len(str, len);
1307
+ return str;
1308
+ }
1309
+
1310
+ /*
1311
+ * call-seq:
1312
+ * oid.oid -> string
1366
1313
  *
1367
- * The object identifier as a +String+, e.g. "1.2.3.4.5"
1314
+ * Returns a String representing the Object Identifier in the dot notation,
1315
+ * e.g. "1.2.3.4.5"
1368
1316
  */
1369
1317
  static VALUE
1370
1318
  ossl_asn1obj_get_oid(VALUE self)
1371
1319
  {
1372
- VALUE val;
1320
+ VALUE str;
1373
1321
  ASN1_OBJECT *a1obj;
1374
- char buf[128];
1322
+ int state;
1375
1323
 
1376
- val = ossl_asn1_get_value(self);
1377
- a1obj = obj_to_asn1obj(val);
1378
- OBJ_obj2txt(buf, sizeof(buf), a1obj, 1);
1324
+ a1obj = obj_to_asn1obj(ossl_asn1_get_value(self));
1325
+ str = rb_protect(asn1obj_get_oid_i, (VALUE)a1obj, &state);
1379
1326
  ASN1_OBJECT_free(a1obj);
1380
-
1381
- return rb_str_new2(buf);
1327
+ if (state)
1328
+ rb_jump_tag(state);
1329
+ return str;
1382
1330
  }
1383
1331
 
1384
1332
  #define OSSL_ASN1_IMPL_FACTORY_METHOD(klass) \
@@ -1412,7 +1360,6 @@ OSSL_ASN1_IMPL_FACTORY_METHOD(EndOfContent)
1412
1360
  void
1413
1361
  Init_ossl_asn1(void)
1414
1362
  {
1415
- #undef rb_intern
1416
1363
  VALUE ary;
1417
1364
  int i;
1418
1365
 
@@ -1432,7 +1379,7 @@ Init_ossl_asn1(void)
1432
1379
  sivTAG = rb_intern("@tag");
1433
1380
  sivTAGGING = rb_intern("@tagging");
1434
1381
  sivTAG_CLASS = rb_intern("@tag_class");
1435
- sivINFINITE_LENGTH = rb_intern("@infinite_length");
1382
+ sivINDEFINITE_LENGTH = rb_intern("@indefinite_length");
1436
1383
  sivUNUSED_BITS = rb_intern("@unused_bits");
1437
1384
 
1438
1385
  /*
@@ -1458,24 +1405,21 @@ Init_ossl_asn1(void)
1458
1405
  * == ASN.1 class hierarchy
1459
1406
  *
1460
1407
  * The base class representing ASN.1 structures is ASN1Data. ASN1Data offers
1461
- * attributes to read and set the +tag+, the +tag_class+ and finally the
1462
- * +value+ of a particular ASN.1 item. Upon parsing, any tagged values
1408
+ * attributes to read and set the _tag_, the _tag_class_ and finally the
1409
+ * _value_ of a particular ASN.1 item. Upon parsing, any tagged values
1463
1410
  * (implicit or explicit) will be represented by ASN1Data instances because
1464
1411
  * their "real type" can only be determined using out-of-band information
1465
1412
  * from the ASN.1 type declaration. Since this information is normally
1466
1413
  * known when encoding a type, all sub-classes of ASN1Data offer an
1467
- * additional attribute +tagging+ that allows to encode a value implicitly
1414
+ * additional attribute _tagging_ that allows to encode a value implicitly
1468
1415
  * (+:IMPLICIT+) or explicitly (+:EXPLICIT+).
1469
1416
  *
1470
1417
  * === Constructive
1471
1418
  *
1472
1419
  * Constructive is, as its name implies, the base class for all
1473
1420
  * constructed encodings, i.e. those that consist of several values,
1474
- * opposed to "primitive" encodings with just one single value.
1475
- * Primitive values that are encoded with "infinite length" are typically
1476
- * constructed (their values come in multiple chunks) and are therefore
1477
- * represented by instances of Constructive. The value of an Constructive
1478
- * is always an Array.
1421
+ * opposed to "primitive" encodings with just one single value. The value of
1422
+ * an Constructive is always an Array.
1479
1423
  *
1480
1424
  * ==== ASN1::Set and ASN1::Sequence
1481
1425
  *
@@ -1492,18 +1436,18 @@ Init_ossl_asn1(void)
1492
1436
  * Please cf. Primitive documentation for details on sub-classes and
1493
1437
  * their respective mappings of ASN.1 data types to Ruby objects.
1494
1438
  *
1495
- * == Possible values for +tagging+
1439
+ * == Possible values for _tagging_
1496
1440
  *
1497
1441
  * When constructing an ASN1Data object the ASN.1 type definition may
1498
1442
  * require certain elements to be either implicitly or explicitly tagged.
1499
- * This can be achieved by setting the +tagging+ attribute manually for
1443
+ * This can be achieved by setting the _tagging_ attribute manually for
1500
1444
  * sub-classes of ASN1Data. Use the symbol +:IMPLICIT+ for implicit
1501
1445
  * tagging and +:EXPLICIT+ if the element requires explicit tagging.
1502
1446
  *
1503
- * == Possible values for +tag_class+
1447
+ * == Possible values for _tag_class_
1504
1448
  *
1505
1449
  * It is possible to create arbitrary ASN1Data objects that also support
1506
- * a PRIVATE or APPLICATION tag class. Possible values for the +tag_class+
1450
+ * a PRIVATE or APPLICATION tag class. Possible values for the _tag_class_
1507
1451
  * attribute are:
1508
1452
  * * +:UNIVERSAL+ (the default for untagged values)
1509
1453
  * * +:CONTEXT_SPECIFIC+ (the default for tagged values)
@@ -1605,9 +1549,9 @@ Init_ossl_asn1(void)
1605
1549
  *
1606
1550
  * An implicitly 1-tagged INTEGER value will be parsed as an
1607
1551
  * ASN1Data with
1608
- * * +tag+ equal to 1
1609
- * * +tag_class+ equal to +:CONTEXT_SPECIFIC+
1610
- * * +value+ equal to a +String+ that carries the raw encoding
1552
+ * * _tag_ equal to 1
1553
+ * * _tag_class_ equal to +:CONTEXT_SPECIFIC+
1554
+ * * _value_ equal to a String that carries the raw encoding
1611
1555
  * of the INTEGER.
1612
1556
  * This implies that a subsequent decoding step is required to
1613
1557
  * completely decode implicitly tagged values.
@@ -1616,9 +1560,9 @@ Init_ossl_asn1(void)
1616
1560
  *
1617
1561
  * An explicitly 1-tagged INTEGER value will be parsed as an
1618
1562
  * ASN1Data with
1619
- * * +tag+ equal to 1
1620
- * * +tag_class+ equal to +:CONTEXT_SPECIFIC+
1621
- * * +value+ equal to an +Array+ with one single element, an
1563
+ * * _tag_ equal to 1
1564
+ * * _tag_class_ equal to +:CONTEXT_SPECIFIC+
1565
+ * * _value_ equal to an Array with one single element, an
1622
1566
  * instance of OpenSSL::ASN1::Integer, i.e. the inner element
1623
1567
  * is the non-tagged primitive value, and the tagging is represented
1624
1568
  * in the outer ASN1Data
@@ -1629,13 +1573,13 @@ Init_ossl_asn1(void)
1629
1573
  * der = seq.to_der
1630
1574
  * asn1 = OpenSSL::ASN1.decode(der)
1631
1575
  * # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
1632
- * # @infinite_length=false,
1576
+ * # @indefinite_length=false,
1633
1577
  * # @tag=16,
1634
1578
  * # @tag_class=:UNIVERSAL,
1635
1579
  * # @tagging=nil,
1636
1580
  * # @value=
1637
1581
  * # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
1638
- * # @infinite_length=false,
1582
+ * # @indefinite_length=false,
1639
1583
  * # @tag=0,
1640
1584
  * # @tag_class=:CONTEXT_SPECIFIC,
1641
1585
  * # @value="\x01">]>
@@ -1652,18 +1596,18 @@ Init_ossl_asn1(void)
1652
1596
  * der = seq.to_der
1653
1597
  * asn1 = OpenSSL::ASN1.decode(der)
1654
1598
  * # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
1655
- * # @infinite_length=false,
1599
+ * # @indefinite_length=false,
1656
1600
  * # @tag=16,
1657
1601
  * # @tag_class=:UNIVERSAL,
1658
1602
  * # @tagging=nil,
1659
1603
  * # @value=
1660
1604
  * # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
1661
- * # @infinite_length=false,
1605
+ * # @indefinite_length=false,
1662
1606
  * # @tag=0,
1663
1607
  * # @tag_class=:CONTEXT_SPECIFIC,
1664
1608
  * # @value=
1665
1609
  * # [#<OpenSSL::ASN1::Integer:0x85bf308
1666
- * # @infinite_length=false,
1610
+ * # @indefinite_length=false,
1667
1611
  * # @tag=2,
1668
1612
  * # @tag_class=:UNIVERSAL
1669
1613
  * # @tagging=nil,
@@ -1679,73 +1623,75 @@ Init_ossl_asn1(void)
1679
1623
  */
1680
1624
  rb_attr(cASN1Data, rb_intern("value"), 1, 1, 0);
1681
1625
  /*
1682
- * A +Number+ representing the tag number of this ASN1Data. Never +nil+.
1626
+ * An Integer representing the tag number of this ASN1Data. Never +nil+.
1683
1627
  */
1684
1628
  rb_attr(cASN1Data, rb_intern("tag"), 1, 1, 0);
1685
1629
  /*
1686
- * A +Symbol+ representing the tag class of this ASN1Data. Never +nil+.
1630
+ * A Symbol representing the tag class of this ASN1Data. Never +nil+.
1687
1631
  * See ASN1Data for possible values.
1688
1632
  */
1689
1633
  rb_attr(cASN1Data, rb_intern("tag_class"), 1, 1, 0);
1690
1634
  /*
1691
- * Never +nil+. A +Boolean+ indicating whether the encoding was infinite
1692
- * length (in the case of parsing) or whether an infinite length encoding
1693
- * shall be used (in the encoding case).
1694
- * In DER, every value has a finite length associated with it. But in
1695
- * scenarios where large amounts of data need to be transferred it
1696
- * might be desirable to have some kind of streaming support available.
1635
+ * Never +nil+. A boolean value indicating whether the encoding uses
1636
+ * indefinite length (in the case of parsing) or whether an indefinite
1637
+ * length form shall be used (in the encoding case).
1638
+ * In DER, every value uses definite length form. But in scenarios where
1639
+ * large amounts of data need to be transferred it might be desirable to
1640
+ * have some kind of streaming support available.
1697
1641
  * For example, huge OCTET STRINGs are preferably sent in smaller-sized
1698
1642
  * chunks, each at a time.
1699
1643
  * This is possible in BER by setting the length bytes of an encoding
1700
1644
  * to zero and by this indicating that the following value will be
1701
- * sent in chunks. Infinite length encodings are always constructed.
1645
+ * sent in chunks. Indefinite length encodings are always constructed.
1702
1646
  * The end of such a stream of chunks is indicated by sending a EOC
1703
- * (End of Content) tag. SETs and SEQUENCEs may use an infinite length
1647
+ * (End of Content) tag. SETs and SEQUENCEs may use an indefinite length
1704
1648
  * encoding, but also primitive types such as e.g. OCTET STRINGS or
1705
1649
  * BIT STRINGS may leverage this functionality (cf. ITU-T X.690).
1706
1650
  */
1707
- rb_attr(cASN1Data, rb_intern("infinite_length"), 1, 1, 0);
1651
+ rb_attr(cASN1Data, rb_intern("indefinite_length"), 1, 1, 0);
1652
+ rb_define_alias(cASN1Data, "infinite_length", "indefinite_length");
1653
+ rb_define_alias(cASN1Data, "infinite_length=", "indefinite_length=");
1708
1654
  rb_define_method(cASN1Data, "initialize", ossl_asn1data_initialize, 3);
1709
1655
  rb_define_method(cASN1Data, "to_der", ossl_asn1data_to_der, 0);
1710
1656
 
1711
1657
  /* Document-class: OpenSSL::ASN1::Primitive
1712
1658
  *
1713
1659
  * The parent class for all primitive encodings. Attributes are the same as
1714
- * for ASN1Data, with the addition of +tagging+.
1715
- * Primitive values can never be infinite length encodings, thus it is not
1716
- * possible to set the +infinite_length+ attribute for Primitive and its
1717
- * sub-classes.
1660
+ * for ASN1Data, with the addition of _tagging_.
1661
+ * Primitive values can never be encoded with indefinite length form, thus
1662
+ * it is not possible to set the _indefinite_length_ attribute for Primitive
1663
+ * and its sub-classes.
1718
1664
  *
1719
1665
  * == Primitive sub-classes and their mapping to Ruby classes
1720
- * * OpenSSL::ASN1::EndOfContent <=> +value+ is always +nil+
1721
- * * OpenSSL::ASN1::Boolean <=> +value+ is a +Boolean+
1722
- * * OpenSSL::ASN1::Integer <=> +value+ is an OpenSSL::BN
1723
- * * OpenSSL::ASN1::BitString <=> +value+ is a +String+
1724
- * * OpenSSL::ASN1::OctetString <=> +value+ is a +String+
1725
- * * OpenSSL::ASN1::Null <=> +value+ is always +nil+
1726
- * * OpenSSL::ASN1::Object <=> +value+ is a +String+
1727
- * * OpenSSL::ASN1::Enumerated <=> +value+ is an OpenSSL::BN
1728
- * * OpenSSL::ASN1::UTF8String <=> +value+ is a +String+
1729
- * * OpenSSL::ASN1::NumericString <=> +value+ is a +String+
1730
- * * OpenSSL::ASN1::PrintableString <=> +value+ is a +String+
1731
- * * OpenSSL::ASN1::T61String <=> +value+ is a +String+
1732
- * * OpenSSL::ASN1::VideotexString <=> +value+ is a +String+
1733
- * * OpenSSL::ASN1::IA5String <=> +value+ is a +String+
1734
- * * OpenSSL::ASN1::UTCTime <=> +value+ is a +Time+
1735
- * * OpenSSL::ASN1::GeneralizedTime <=> +value+ is a +Time+
1736
- * * OpenSSL::ASN1::GraphicString <=> +value+ is a +String+
1737
- * * OpenSSL::ASN1::ISO64String <=> +value+ is a +String+
1738
- * * OpenSSL::ASN1::GeneralString <=> +value+ is a +String+
1739
- * * OpenSSL::ASN1::UniversalString <=> +value+ is a +String+
1740
- * * OpenSSL::ASN1::BMPString <=> +value+ is a +String+
1666
+ * * OpenSSL::ASN1::EndOfContent <=> _value_ is always +nil+
1667
+ * * OpenSSL::ASN1::Boolean <=> _value_ is +true+ or +false+
1668
+ * * OpenSSL::ASN1::Integer <=> _value_ is an Integer
1669
+ * * OpenSSL::ASN1::BitString <=> _value_ is a String
1670
+ * * OpenSSL::ASN1::OctetString <=> _value_ is a String
1671
+ * * OpenSSL::ASN1::Null <=> _value_ is always +nil+
1672
+ * * OpenSSL::ASN1::Object <=> _value_ is a String
1673
+ * * OpenSSL::ASN1::Enumerated <=> _value_ is an Integer
1674
+ * * OpenSSL::ASN1::UTF8String <=> _value_ is a String
1675
+ * * OpenSSL::ASN1::NumericString <=> _value_ is a String
1676
+ * * OpenSSL::ASN1::PrintableString <=> _value_ is a String
1677
+ * * OpenSSL::ASN1::T61String <=> _value_ is a String
1678
+ * * OpenSSL::ASN1::VideotexString <=> _value_ is a String
1679
+ * * OpenSSL::ASN1::IA5String <=> _value_ is a String
1680
+ * * OpenSSL::ASN1::UTCTime <=> _value_ is a Time
1681
+ * * OpenSSL::ASN1::GeneralizedTime <=> _value_ is a Time
1682
+ * * OpenSSL::ASN1::GraphicString <=> _value_ is a String
1683
+ * * OpenSSL::ASN1::ISO64String <=> _value_ is a String
1684
+ * * OpenSSL::ASN1::GeneralString <=> _value_ is a String
1685
+ * * OpenSSL::ASN1::UniversalString <=> _value_ is a String
1686
+ * * OpenSSL::ASN1::BMPString <=> _value_ is a String
1741
1687
  *
1742
1688
  * == OpenSSL::ASN1::BitString
1743
1689
  *
1744
1690
  * === Additional attributes
1745
- * +unused_bits+: if the underlying BIT STRING's
1746
- * length is a multiple of 8 then +unused_bits+ is 0. Otherwise
1747
- * +unused_bits+ indicates the number of bits that are to be ignored in
1748
- * the final octet of the +BitString+'s +value+.
1691
+ * _unused_bits_: if the underlying BIT STRING's
1692
+ * length is a multiple of 8 then _unused_bits_ is 0. Otherwise
1693
+ * _unused_bits_ indicates the number of bits that are to be ignored in
1694
+ * the final octet of the BitString's _value_.
1749
1695
  *
1750
1696
  * == OpenSSL::ASN1::ObjectId
1751
1697
  *
@@ -1754,15 +1700,15 @@ Init_ossl_asn1(void)
1754
1700
  * parsed ASN1 encodings.
1755
1701
  *
1756
1702
  * === Additional attributes
1757
- * * +sn+: the short name as defined in <openssl/objects.h>.
1758
- * * +ln+: the long name as defined in <openssl/objects.h>.
1759
- * * +oid+: the object identifier as a +String+, e.g. "1.2.3.4.5"
1760
- * * +short_name+: alias for +sn+.
1761
- * * +long_name+: alias for +ln+.
1703
+ * * _sn_: the short name as defined in <openssl/objects.h>.
1704
+ * * _ln_: the long name as defined in <openssl/objects.h>.
1705
+ * * _oid_: the object identifier as a String, e.g. "1.2.3.4.5"
1706
+ * * _short_name_: alias for _sn_.
1707
+ * * _long_name_: alias for _ln_.
1762
1708
  *
1763
1709
  * == Examples
1764
1710
  * With the Exception of OpenSSL::ASN1::EndOfContent, each Primitive class
1765
- * constructor takes at least one parameter, the +value+.
1711
+ * constructor takes at least one parameter, the _value_.
1766
1712
  *
1767
1713
  * === Creating EndOfContent
1768
1714
  * eoc = OpenSSL::ASN1::EndOfContent.new
@@ -1776,19 +1722,20 @@ Init_ossl_asn1(void)
1776
1722
  /*
1777
1723
  * May be used as a hint for encoding a value either implicitly or
1778
1724
  * explicitly by setting it either to +:IMPLICIT+ or to +:EXPLICIT+.
1779
- * +tagging+ is not set when a ASN.1 structure is parsed using
1725
+ * _tagging_ is not set when a ASN.1 structure is parsed using
1780
1726
  * OpenSSL::ASN1.decode.
1781
1727
  */
1782
1728
  rb_attr(cASN1Primitive, rb_intern("tagging"), 1, 1, Qtrue);
1729
+ rb_undef_method(cASN1Primitive, "indefinite_length=");
1783
1730
  rb_undef_method(cASN1Primitive, "infinite_length=");
1784
1731
  rb_define_method(cASN1Primitive, "initialize", ossl_asn1_initialize, -1);
1785
1732
  rb_define_method(cASN1Primitive, "to_der", ossl_asn1prim_to_der, 0);
1786
1733
 
1787
1734
  /* Document-class: OpenSSL::ASN1::Constructive
1788
1735
  *
1789
- * The parent class for all constructed encodings. The +value+ attribute
1790
- * of a Constructive is always an +Array+. Attributes are the same as
1791
- * for ASN1Data, with the addition of +tagging+.
1736
+ * The parent class for all constructed encodings. The _value_ attribute
1737
+ * of a Constructive is always an Array. Attributes are the same as
1738
+ * for ASN1Data, with the addition of _tagging_.
1792
1739
  *
1793
1740
  * == SET and SEQUENCE
1794
1741
  *
@@ -1810,48 +1757,13 @@ Init_ossl_asn1(void)
1810
1757
  * int = OpenSSL::ASN1::Integer.new(1)
1811
1758
  * str = OpenSSL::ASN1::PrintableString.new('abc')
1812
1759
  * set = OpenSSL::ASN1::Set.new( [ int, str ] )
1813
- *
1814
- * == Infinite length primitive values
1815
- *
1816
- * The only case where Constructive is used directly is for infinite
1817
- * length encodings of primitive values. These encodings are always
1818
- * constructed, with the contents of the +value+ +Array+ being either
1819
- * UNIVERSAL non-infinite length partial encodings of the actual value
1820
- * or again constructive encodings with infinite length (i.e. infinite
1821
- * length primitive encodings may be constructed recursively with another
1822
- * infinite length value within an already infinite length value). Each
1823
- * partial encoding must be of the same UNIVERSAL type as the overall
1824
- * encoding. The value of the overall encoding consists of the
1825
- * concatenation of each partial encoding taken in sequence. The +value+
1826
- * array of the outer infinite length value must end with a
1827
- * OpenSSL::ASN1::EndOfContent instance.
1828
- *
1829
- * Please note that it is not possible to encode Constructive without
1830
- * the +infinite_length+ attribute being set to +true+, use
1831
- * OpenSSL::ASN1::Sequence or OpenSSL::ASN1::Set in these cases instead.
1832
- *
1833
- * === Example - Infinite length OCTET STRING
1834
- * partial1 = OpenSSL::ASN1::OctetString.new("\x01")
1835
- * partial2 = OpenSSL::ASN1::OctetString.new("\x02")
1836
- * inf_octets = OpenSSL::ASN1::Constructive.new( [ partial1,
1837
- * partial2,
1838
- * OpenSSL::ASN1::EndOfContent.new ],
1839
- * OpenSSL::ASN1::OCTET_STRING,
1840
- * nil,
1841
- * :UNIVERSAL )
1842
- * # The real value of inf_octets is "\x01\x02", i.e. the concatenation
1843
- * # of partial1 and partial2
1844
- * inf_octets.infinite_length = true
1845
- * der = inf_octets.to_der
1846
- * asn1 = OpenSSL::ASN1.decode(der)
1847
- * puts asn1.infinite_length # => true
1848
1760
  */
1849
1761
  cASN1Constructive = rb_define_class_under(mASN1,"Constructive", cASN1Data);
1850
1762
  rb_include_module(cASN1Constructive, rb_mEnumerable);
1851
1763
  /*
1852
1764
  * May be used as a hint for encoding a value either implicitly or
1853
1765
  * explicitly by setting it either to +:IMPLICIT+ or to +:EXPLICIT+.
1854
- * +tagging+ is not set when a ASN.1 structure is parsed using
1766
+ * _tagging_ is not set when a ASN.1 structure is parsed using
1855
1767
  * OpenSSL::ASN1.decode.
1856
1768
  */
1857
1769
  rb_attr(cASN1Constructive, rb_intern("tagging"), 1, 1, Qtrue);
@@ -1908,6 +1820,7 @@ do{\
1908
1820
  rb_attr(cASN1BitString, rb_intern("unused_bits"), 1, 1, 0);
1909
1821
 
1910
1822
  rb_define_method(cASN1EndOfContent, "initialize", ossl_asn1eoc_initialize, 0);
1823
+ rb_define_method(cASN1EndOfContent, "to_der", ossl_asn1eoc_to_der, 0);
1911
1824
 
1912
1825
  class_tag_map = rb_hash_new();
1913
1826
  rb_hash_aset(class_tag_map, cASN1EndOfContent, INT2NUM(V_ASN1_EOC));