Skip to content

Commit

Permalink
ASN1: #to_der in pure ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
HoneyryderChuck committed Jul 23, 2024
1 parent c959729 commit 95e2d8d
Show file tree
Hide file tree
Showing 2 changed files with 328 additions and 75 deletions.
75 changes: 4 additions & 71 deletions ext/openssl/ossl_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,30 +680,6 @@ to_der_internal(VALUE self, int constructed, int indef_len, VALUE body)
}

static VALUE ossl_asn1prim_to_der(VALUE);
static VALUE ossl_asn1cons_to_der(VALUE);
/*
* call-seq:
* asn1.to_der => DER-encoded String
*
* Encodes this ASN1Data into a DER-encoded String value. The result is
* DER-encoded except for the possibility of indefinite length forms.
* Indefinite length forms are not allowed in strict DER, so strictly speaking
* the result of such an encoding would be a BER-encoding.
*/
static VALUE
ossl_asn1data_to_der(VALUE self)
{
VALUE value = ossl_asn1_get_value(self);

if (rb_obj_is_kind_of(value, rb_cArray))
return ossl_asn1cons_to_der(self);
else {
if (RTEST(ossl_asn1_get_indefinite_length(self)))
ossl_raise(eASN1Error, "indefinite length form cannot be used " \
"with primitive encoding");
return ossl_asn1prim_to_der(self);
}
}

static VALUE
int_ossl_asn1_decode0_prim(unsigned char **pp, long length, long hlen, int tag,
Expand Down Expand Up @@ -1012,11 +988,6 @@ ossl_asn1_decode_all(VALUE self, VALUE obj)
return ary;
}

static VALUE
ossl_asn1eoc_to_der(VALUE self)
{
return rb_str_new("\0\0", 2);
}

/*
* call-seq:
Expand Down Expand Up @@ -1065,44 +1036,6 @@ ossl_asn1prim_to_der(VALUE self)
return to_der_internal(self, 0, 0, rb_str_drop_bytes(str, alllen - bodylen));
}

/*
* call-seq:
* asn1.to_der => DER-encoded String
*
* See ASN1Data#to_der for details.
*/
static VALUE
ossl_asn1cons_to_der(VALUE self)
{
VALUE ary, str;
long i;
int indef_len;

indef_len = RTEST(ossl_asn1_get_indefinite_length(self));
ary = rb_convert_type(ossl_asn1_get_value(self), T_ARRAY, "Array", "to_a");
str = rb_str_new(NULL, 0);
for (i = 0; i < RARRAY_LEN(ary); i++) {
VALUE item = RARRAY_AREF(ary, i);

if (indef_len && rb_obj_is_kind_of(item, cASN1EndOfContent)) {
if (i != RARRAY_LEN(ary) - 1)
ossl_raise(eASN1Error, "illegal EOC octets in value");

/*
* EOC is not really part of the content, but we required to add one
* at the end in the past.
*/
break;
}

item = ossl_to_der_if_possible(item);
StringValue(item);
rb_str_append(str, item);
}

return to_der_internal(self, 1, indef_len, str);
}

/*
* call-seq:
* OpenSSL::ASN1::ObjectId.register(object_id, short_name, long_name)
Expand Down Expand Up @@ -1523,7 +1456,6 @@ Init_ossl_asn1(void)
* puts int2.value # => 1
*/
cASN1Data = rb_define_class_under(mASN1, "ASN1Data", rb_cObject);
rb_define_method(cASN1Data, "to_der", ossl_asn1data_to_der, 0);

/* Document-class: OpenSSL::ASN1::Primitive
*
Expand Down Expand Up @@ -1590,7 +1522,7 @@ Init_ossl_asn1(void)
* prim_zero_tagged_explicit = <class>.new(value, 0, :EXPLICIT)
*/
cASN1Primitive = rb_define_class_under(mASN1, "Primitive", cASN1Data);
rb_define_method(cASN1Primitive, "to_der", ossl_asn1prim_to_der, 0);
// rb_define_method(cASN1Primitive, "to_der", ossl_asn1prim_to_der, 0);

/* Document-class: OpenSSL::ASN1::Constructive
*
Expand Down Expand Up @@ -1620,7 +1552,6 @@ Init_ossl_asn1(void)
* set = OpenSSL::ASN1::Set.new( [ int, str ] )
*/
cASN1Constructive = rb_define_class_under(mASN1,"Constructive", cASN1Data);
rb_define_method(cASN1Constructive, "to_der", ossl_asn1cons_to_der, 0);

#define OSSL_ASN1_DEFINE_CLASS(name, super) \
do{\
Expand Down Expand Up @@ -1670,7 +1601,9 @@ do{\
rb_define_alias(cASN1ObjectId, "long_name", "ln");
rb_define_method(cASN1ObjectId, "==", ossl_asn1obj_eq, 1);

rb_define_method(cASN1EndOfContent, "to_der", ossl_asn1eoc_to_der, 0);
// rb_define_method(cASN1ObjectId, "to_der", ossl_asn1prim_to_der, 0);
rb_define_method(cASN1UTCTime, "to_der", ossl_asn1prim_to_der, 0);
rb_define_method(cASN1GeneralizedTime, "to_der", ossl_asn1prim_to_der, 0);

class_tag_map = rb_hash_new();
rb_hash_aset(class_tag_map, cASN1EndOfContent, INT2NUM(V_ASN1_EOC));
Expand Down
Loading

0 comments on commit 95e2d8d

Please sign in to comment.