Class: OpenSSL::ASN1::Primitive

Inherits:
ASN1Data
  • Object
show all
Defined in:
ossl_asn1.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
# File 'ossl_asn1.c', line 866

static VALUE
ossl_asn1_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE value, tag, tagging, tag_class;

    rb_scan_args(argc, argv, "13", &value, &tag, &tagging, &tag_class);
    if(argc > 1){
	if(NIL_P(tag))
	    ossl_raise(eASN1Error, "must specify tag number");
        if(NIL_P(tagging))
	    tagging = ID2SYM(sEXPLICIT);
	if(!SYMBOL_P(tagging))
	    ossl_raise(eASN1Error, "invalid tag default");
	if(NIL_P(tag_class))
	    tag_class = ID2SYM(sCONTEXT_SPECIFIC);
	if(!SYMBOL_P(tag_class))
	    ossl_raise(eASN1Error, "invalid tag class");
	if(SYM2ID(tagging) == sIMPLICIT && NUM2INT(tag) > 31)
	    ossl_raise(eASN1Error, "tag number for Universal too large");
    }
    else{
	tag = INT2NUM(ossl_asn1_default_tag(self));
        tagging = Qnil;
	tag_class = ID2SYM(sUNIVERSAL);
    }
    ossl_asn1_set_tag(self, tag);
    ossl_asn1_set_value(self, value);
    ossl_asn1_set_tagging(self, tagging);
    ossl_asn1_set_tag_class(self, tag_class);

    return self;
}

Instance Method Details

#to_derObject



923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# File 'ossl_asn1.c', line 923

static VALUE
ossl_asn1prim_to_der(VALUE self)
{
    ASN1_TYPE *asn1;
    int tn, tc, explicit;
    long length, reallen;
    unsigned char *buf, *p;
    VALUE str;

    tn = NUM2INT(ossl_asn1_get_tag(self));
    tc = ossl_asn1_tag_class(self);
    explicit = ossl_asn1_is_explicit(self);
    asn1 = ossl_asn1_get_asn1type(self);

    length = ASN1_object_size(1, ossl_i2d_ASN1_TYPE(asn1, NULL), tn);
    if(!(buf = OPENSSL_malloc(length))){
	ossl_ASN1_TYPE_free(asn1);
	ossl_raise(eASN1Error, "cannot alloc buffer");
    }
    p = buf;
    if(tc == V_ASN1_UNIVERSAL) ossl_i2d_ASN1_TYPE(asn1, &p);
    else{
	if(explicit){
	    ASN1_put_object(&p, 1, ossl_i2d_ASN1_TYPE(asn1, NULL), tn, tc);
	    ossl_i2d_ASN1_TYPE(asn1, &p);
	}
	else{
	    ossl_i2d_ASN1_TYPE(asn1, &p);
	    *buf = tc | tn | (*buf & V_ASN1_CONSTRUCTED);
	}
    }
    ossl_ASN1_TYPE_free(asn1);
    reallen = p - buf;
    assert(reallen <= length);
    str = ossl_buf2str(buf, reallen); /* buf will be free in ossl_buf2str */

    return str;
}