在线文档教程
Ruby 2.4
OpenSSL

OpenSSL::ASN1::Constructive

类 OpenSSL :: ASN1 :: Constructive

父类:OpenSSL :: ASN1 :: ASN1DataIncluded 模块:可枚举

所有构造的编码的父类。value建设性的属性总是一个Array。属性与 ASN1Data 相同,但增加了tagging

SET 和 SEQUENCE

大多数构造的编码都以 SET 或 SEQUENCE 的形式出现。这些编码由 Construtive 的两个子类之一表示:

  • OpenSSL的:: ASN1 ::设为

  • OpenSSL的:: ASN1 ::序列

请注意,标记的序列和集合仍然被解析为 ASN1Data 的实例。查找更多有关标记值的详细信息。

示例 - 构造一个 SEQUENCE

int = OpenSSL::ASN1::Integer.new(1) str = OpenSSL::ASN1::PrintableString.new('abc') sequence = OpenSSL::ASN1::Sequence.new( [ int, str ] )

示例 - 构建一个 SET

int = OpenSSL::ASN1::Integer.new(1) str = OpenSSL::ASN1::PrintableString.new('abc') set = OpenSSL::ASN1::Set.new( [ int, str ] )

无限长度的原始值

直接使用构造函数的唯一情况是对原始值进行无限长编码。这些编码总是被构造的,其中的内容是对value Array实际值的通用非无限长度部分编码或再次无限长度的构造编码(即,无限长的基本编码可以递归地用另一无限长度值在已经无限的长度值)。每个部分编码必须与整体编码具有相同的 UNIVERSAL 类型。整体编码的值由按顺序拍摄的每个部分编码的连接组成。value外部无限长度值的数组必须以 OpenSSL :: ASN1 :: EndOfContent 实例结束。

Please note that it is not possible to encode Constructive without the infinite_length attribute being set to true, use OpenSSL::ASN1::Sequence or OpenSSL::ASN1::Set in these cases instead.

示例 - 无限长度的 OCTET STRING

partial1 = OpenSSL::ASN1::OctetString.new("\x01") partial2 = OpenSSL::ASN1::OctetString.new("\x02") inf_octets = OpenSSL::ASN1::Constructive.new( [ partial1, partial2, OpenSSL::ASN1::EndOfContent.new ], OpenSSL::ASN1::OCTET_STRING, nil, :UNIVERSAL ) # The real value of inf_octets is "\x01\x02", i.e. the concatenation # of partial1 and partial2 inf_octets.infinite_length = true der = inf_octets.to_der asn1 = OpenSSL::ASN1.decode(der) puts asn1.infinite_length # => true

属性

taggingRW

可以通过将它设置为:IMPLICIT或设为隐式或显式对值进行编码,作为提示使用:EXPLICITtagging在使用OpenSSL :: ASN1.decode 解析 ASN.1 结构时未设置。

公共类方法

OpenSSL :: ASN1 :: Primitive.new(value,tag,tagging,tag_class)→Primitive Show source

value: 是强制性的。

tag:可选,可以为标记值指定。如果 tag不是指定,默认使用与 Primitive 子类对应的 UNIVERSAL 标记。

tagging:可以用作编码提示来显式或隐式地对值进行编码,请参阅 ASN1 以了解可能的值。

tag_class:如果tagtaggingnil那么这个被设置为:UNIVERSAL默认。如果其中之一tag或者tagging被设置,:CONTEXT_SPECIFIC则被用作默认值。有关可能的值,请参阅 ASN1。

示例

int = OpenSSL::ASN1::Integer.new(42) zero_tagged_int = OpenSSL::ASN1::Integer.new(42, 0, :IMPLICIT) private_explicit_zero_tagged_int = OpenSSL::ASN1::Integer.new(42, 0, :EXPLICIT, :PRIVATE)

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) && !SYMBOL_P(tagging)) ossl_raise(eASN1Error, "invalid tagging method" if(NIL_P(tag_class)) { if (NIL_P(tagging)) tag_class = sym_UNIVERSAL; else tag_class = sym_CONTEXT_SPECIFIC; } if(!SYMBOL_P(tag_class)) ossl_raise(eASN1Error, "invalid tag class" if (tagging == sym_IMPLICIT && NUM2INT(tag) > 31) ossl_raise(eASN1Error, "tag number for Universal too large" } else{ tag = INT2NUM(ossl_asn1_default_tag(self) tagging = Qnil; tag_class = sym_UNIVERSAL; } 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 ossl_asn1_set_infinite_length(self, Qfalse return self; }

公共实例方法

每个 {| asn1 | 块}→asn1_ary 显示源

为每个元素调用一次self,将该元素作为参数传递asn1。如果没有给出,则返回一个枚举器。

示例

asn1_ary.each do |asn1| puts asn1 end

static VALUE ossl_asn1cons_each(VALUE self) { rb_block_call(ossl_asn1_get_value(self), id_each, 0, 0, 0, 0 return self; }

to_der→DER 编码的字符串显示源

有关详细信息,请参阅 OpenSSL :: ASN1 :: ASN1Data#to_der。

static VALUE ossl_asn1cons_to_der(VALUE self) { int tag, tn, tc, explicit, constructed = 1; int found_prim = 0, seq_len; long length; unsigned char *p; VALUE value, str, inf_length; tn = NUM2INT(ossl_asn1_get_tag(self) tc = ossl_asn1_tag_class(self inf_length = ossl_asn1_get_infinite_length(self if (inf_length == Qtrue) { VALUE ary, example; constructed = 2; if (rb_obj_class(self) == cASN1Sequence || rb_obj_class(self) == cASN1Set) { tag = ossl_asn1_default_tag(self } else { /* must be a constructive encoding of a primitive value */ ary = ossl_asn1_get_value(self if (!rb_obj_is_kind_of(ary, rb_cArray)) ossl_raise(eASN1Error, "Constructive value must be an Array" /* Recursively descend until a primitive value is found. The overall value of the entire constructed encoding is of the type of the first primitive encoding to be found. */ while (!found_prim){ example = rb_ary_entry(ary, 0 if (rb_obj_is_kind_of(example, cASN1Primitive)){ found_prim = 1; } else { /* example is another ASN1Constructive */ if (!rb_obj_is_kind_of(example, cASN1Constructive)){ ossl_raise(eASN1Error, "invalid constructed encoding" return Qnil; /* dummy */ } ary = ossl_asn1_get_value(example } } tag = ossl_asn1_default_tag(example } } else { if (rb_obj_class(self) == cASN1Constructive) ossl_raise(eASN1Error, "Constructive shall only be used with infinite length" tag = ossl_asn1_default_tag(self } explicit = ossl_asn1_is_explicit(self value = join_der(ossl_asn1_get_value(self) seq_len = ASN1_object_size(constructed, RSTRING_LENINT(value), tag length = ASN1_object_size(constructed, seq_len, tn str = rb_str_new(0, length p = (unsigned char *)RSTRING_PTR(str if(tc == V_ASN1_UNIVERSAL) ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tn, tc else{ if(explicit){ ASN1_put_object(&p, constructed, seq_len, tn, tc ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tag, V_ASN1_UNIVERSAL } else{ ASN1_put_object(&p, constructed, RSTRING_LENINT(value), tn, tc } } memcpy(p, RSTRING_PTR(value), RSTRING_LEN(value) p += RSTRING_LEN(value /* In this case we need an additional EOC (one for the explicit part and * one for the Constructive itself. The EOC for the Constructive is * supplied by the user, but that for the "explicit wrapper" must be * added here. */ if (explicit && inf_length == Qtrue) { ASN1_put_eoc(&p } ossl_str_adjust(str, p return str; }