Skip to content

Commit 1e35012

Browse files
ASN1: #to_der in pure ruby
1 parent c959729 commit 1e35012

File tree

2 files changed

+333
-75
lines changed

2 files changed

+333
-75
lines changed

ext/openssl/ossl_asn1.c

+4-71
Original file line numberDiff line numberDiff line change
@@ -680,30 +680,6 @@ to_der_internal(VALUE self, int constructed, int indef_len, VALUE body)
680680
}
681681

682682
static VALUE ossl_asn1prim_to_der(VALUE);
683-
static VALUE ossl_asn1cons_to_der(VALUE);
684-
/*
685-
* call-seq:
686-
* asn1.to_der => DER-encoded String
687-
*
688-
* Encodes this ASN1Data into a DER-encoded String value. The result is
689-
* DER-encoded except for the possibility of indefinite length forms.
690-
* Indefinite length forms are not allowed in strict DER, so strictly speaking
691-
* the result of such an encoding would be a BER-encoding.
692-
*/
693-
static VALUE
694-
ossl_asn1data_to_der(VALUE self)
695-
{
696-
VALUE value = ossl_asn1_get_value(self);
697-
698-
if (rb_obj_is_kind_of(value, rb_cArray))
699-
return ossl_asn1cons_to_der(self);
700-
else {
701-
if (RTEST(ossl_asn1_get_indefinite_length(self)))
702-
ossl_raise(eASN1Error, "indefinite length form cannot be used " \
703-
"with primitive encoding");
704-
return ossl_asn1prim_to_der(self);
705-
}
706-
}
707683

708684
static VALUE
709685
int_ossl_asn1_decode0_prim(unsigned char **pp, long length, long hlen, int tag,
@@ -1012,11 +988,6 @@ ossl_asn1_decode_all(VALUE self, VALUE obj)
1012988
return ary;
1013989
}
1014990

1015-
static VALUE
1016-
ossl_asn1eoc_to_der(VALUE self)
1017-
{
1018-
return rb_str_new("\0\0", 2);
1019-
}
1020991

1021992
/*
1022993
* call-seq:
@@ -1065,44 +1036,6 @@ ossl_asn1prim_to_der(VALUE self)
10651036
return to_der_internal(self, 0, 0, rb_str_drop_bytes(str, alllen - bodylen));
10661037
}
10671038

1068-
/*
1069-
* call-seq:
1070-
* asn1.to_der => DER-encoded String
1071-
*
1072-
* See ASN1Data#to_der for details.
1073-
*/
1074-
static VALUE
1075-
ossl_asn1cons_to_der(VALUE self)
1076-
{
1077-
VALUE ary, str;
1078-
long i;
1079-
int indef_len;
1080-
1081-
indef_len = RTEST(ossl_asn1_get_indefinite_length(self));
1082-
ary = rb_convert_type(ossl_asn1_get_value(self), T_ARRAY, "Array", "to_a");
1083-
str = rb_str_new(NULL, 0);
1084-
for (i = 0; i < RARRAY_LEN(ary); i++) {
1085-
VALUE item = RARRAY_AREF(ary, i);
1086-
1087-
if (indef_len && rb_obj_is_kind_of(item, cASN1EndOfContent)) {
1088-
if (i != RARRAY_LEN(ary) - 1)
1089-
ossl_raise(eASN1Error, "illegal EOC octets in value");
1090-
1091-
/*
1092-
* EOC is not really part of the content, but we required to add one
1093-
* at the end in the past.
1094-
*/
1095-
break;
1096-
}
1097-
1098-
item = ossl_to_der_if_possible(item);
1099-
StringValue(item);
1100-
rb_str_append(str, item);
1101-
}
1102-
1103-
return to_der_internal(self, 1, indef_len, str);
1104-
}
1105-
11061039
/*
11071040
* call-seq:
11081041
* OpenSSL::ASN1::ObjectId.register(object_id, short_name, long_name)
@@ -1523,7 +1456,6 @@ Init_ossl_asn1(void)
15231456
* puts int2.value # => 1
15241457
*/
15251458
cASN1Data = rb_define_class_under(mASN1, "ASN1Data", rb_cObject);
1526-
rb_define_method(cASN1Data, "to_der", ossl_asn1data_to_der, 0);
15271459

15281460
/* Document-class: OpenSSL::ASN1::Primitive
15291461
*
@@ -1590,7 +1522,7 @@ Init_ossl_asn1(void)
15901522
* prim_zero_tagged_explicit = <class>.new(value, 0, :EXPLICIT)
15911523
*/
15921524
cASN1Primitive = rb_define_class_under(mASN1, "Primitive", cASN1Data);
1593-
rb_define_method(cASN1Primitive, "to_der", ossl_asn1prim_to_der, 0);
1525+
// rb_define_method(cASN1Primitive, "to_der", ossl_asn1prim_to_der, 0);
15941526

15951527
/* Document-class: OpenSSL::ASN1::Constructive
15961528
*
@@ -1620,7 +1552,6 @@ Init_ossl_asn1(void)
16201552
* set = OpenSSL::ASN1::Set.new( [ int, str ] )
16211553
*/
16221554
cASN1Constructive = rb_define_class_under(mASN1,"Constructive", cASN1Data);
1623-
rb_define_method(cASN1Constructive, "to_der", ossl_asn1cons_to_der, 0);
16241555

16251556
#define OSSL_ASN1_DEFINE_CLASS(name, super) \
16261557
do{\
@@ -1670,7 +1601,9 @@ do{\
16701601
rb_define_alias(cASN1ObjectId, "long_name", "ln");
16711602
rb_define_method(cASN1ObjectId, "==", ossl_asn1obj_eq, 1);
16721603

1673-
rb_define_method(cASN1EndOfContent, "to_der", ossl_asn1eoc_to_der, 0);
1604+
// rb_define_method(cASN1ObjectId, "to_der", ossl_asn1prim_to_der, 0);
1605+
// rb_define_method(cASN1UTCTime, "to_der", ossl_asn1prim_to_der, 0);
1606+
// rb_define_method(cASN1GeneralizedTime, "to_der", ossl_asn1prim_to_der, 0);
16741607

16751608
class_tag_map = rb_hash_new();
16761609
rb_hash_aset(class_tag_map, cASN1EndOfContent, INT2NUM(V_ASN1_EOC));

0 commit comments

Comments
 (0)