Ruby 2.4
OpenSSL

OpenSSL::ASN1::Primitive

类 OpenSSL :: ASN1 :: Primitive

家长:OpenSSL的:: ASN1 :: ASN1Data

所有原始编码的父类。属性与 ASN1Data 相同,但增加了tagging。原始值永远不能是无限长度编码,因此不可能infinite_length为 Primitive 及其子类设置属性。

原始子类及其对 Ruby 类的映射

  • OpenSSL :: ASN1 :: EndOfContent <=> value总是nil

  • OpenSSL :: ASN1 :: Boolean <=> value是一个Boolean

  • OpenSSL :: ASN1 :: Integer <=> value是一个Number

  • OpenSSL :: ASN1 :: BitString <=> value是一个String

  • OpenSSL :: ASN1 :: OctetString <=> value是一个String

  • OpenSSL :: ASN1 :: Null <=> value总是nil

  • OpenSSL :: ASN1 :: Object <=> value是一个String

  • OpenSSL :: ASN1 :: Enumerated <=> value是一个Number

  • OpenSSL :: ASN1 :: UTF8String <=> value是一个String

  • OpenSSL :: ASN1 :: NumericString <=> value是一个String

  • OpenSSL :: ASN1 :: PrintableString <=> value是一个String

  • OpenSSL :: ASN1 :: T61String <=> value是一个String

  • OpenSSL :: ASN1 :: VideotexString <=> value是一个String

  • OpenSSL :: ASN1 :: IA5String <=> value是一个String

  • OpenSSL :: ASN1 :: UTCTime <=> value是一个Time

  • OpenSSL :: ASN1 :: GeneralizedTime <=> value是一个Time

  • OpenSSL :: ASN1 :: GraphicString <=> value是一个String

  • OpenSSL :: ASN1 :: ISO64String <=> value是一个String

  • OpenSSL :: ASN1 :: GeneralString <=> value是一个String

  • OpenSSL :: ASN1 :: UniversalString <=> value是一个String

  • OpenSSL :: ASN1 :: BMPString <=> value是一个StringOpenSSL :: ASN1 :: BitStringAdditional属性unused_bits:如果底层 BIT STRING 的长度是8的倍数,那么它unused_bits是0.否则unused_bits表示最终要忽略的比特数的八位字节BitString的value.OpenSSL :: ASN1 :: ObjectIdNOTE:尽管::新会分配一个新的ObjectId,通常不会被分配这种方式,而是从解析的 ASN1 encodings.Additional 属性接受

  • sn:在<openssl / objects.h>中定义的短名称。

  • ln:在<openssl / objects.h>中定义的长名称。

  • oid:对象标识符为一个String,例如“1.2.3.4.5”

  • short_name:别名sn

  • long_name:别名ln

示例

OpenSSL :: ASN1 :: EndOfContent 的例外,每个 Primitive类构造函数至少需要一个参数value

创建 EndOfContent

eoc = OpenSSL::ASN1::EndOfContent.new

创建任何其他基元

prim = <class>.new(value) # <class> being one of the sub-classes except EndOfContent prim_zero_tagged_implicit = <class>.new(value, 0, :IMPLICIT) prim_zero_tagged_explicit = <class>.new(value, 0, :EXPLICIT)

属性

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; }

公共实例方法

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

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

static VALUE ossl_asn1prim_to_der(VALUE self) { ASN1_TYPE *asn1; int tn, tc, explicit; long len, 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 len = ASN1_object_size(1, i2d_ASN1_TYPE(asn1, NULL), tn if(!(buf = OPENSSL_malloc(len))){ ASN1_TYPE_free(asn1 ossl_raise(eASN1Error, "cannot alloc buffer" } p = buf; if (tc == V_ASN1_UNIVERSAL) { i2d_ASN1_TYPE(asn1, &p } else if (explicit) { ASN1_put_object(&p, 1, i2d_ASN1_TYPE(asn1, NULL), tn, tc i2d_ASN1_TYPE(asn1, &p } else { i2d_ASN1_TYPE(asn1, &p *buf = tc | tn | (*buf & V_ASN1_CONSTRUCTED } ASN1_TYPE_free(asn1 reallen = p - buf; assert(reallen <= len str = ossl_buf2str((char *)buf, rb_long2int(reallen) /* buf will be free in ossl_buf2str */ return str; }