Class: Krypt::ASN1::Primitive

Inherits:
ASN1Data
  • Object
show all
Defined in:
ext/krypt/core/krypt_asn1.c,
ext/krypt/core/krypt_asn1.c

Overview

The parent class for all primitive encodings. Attributes are the same as for ASN1Data. Primitive values can never be infinite length encodings, thus it is not possible to set the infinite_length attribute for Primitive and its sub-classes.

Primitive sub-classes and their mapping to Ruby classes

  • Krypt::ASN1::EndOfContents <=> value is always nil

  • Krypt::ASN1::Boolean <=> value is a Boolean

  • Krypt::ASN1::Integer <=> value is a Number

  • Krypt::ASN1::BitString <=> value is a String

  • Krypt::ASN1::OctetString <=> value is a String

  • Krypt::ASN1::Null <=> value is always nil

  • Krypt::ASN1::Object <=> value is a String

  • Krypt::ASN1::Enumerated <=> value is a Number

  • Krypt::ASN1::UTF8String <=> value is a String

  • Krypt::ASN1::NumericString <=> value is a String

  • Krypt::ASN1::PrintableString <=> value is a String

  • Krypt::ASN1::T61String <=> value is a String

  • Krypt::ASN1::VideotexString <=> value is a String

  • Krypt::ASN1::IA5String <=> value is a String

  • Krypt::ASN1::UTCTime <=> value is a Time (or a Number when creating them)

  • Krypt::ASN1::GeneralizedTime <=> value is a Time (or a Number when creating them)

  • Krypt::ASN1::GraphicString <=> value is a String

  • Krypt::ASN1::ISO64String <=> value is a String

  • Krypt::ASN1::GeneralString <=> value is a String

  • Krypt::ASN1::UniversalString <=> value is a String

  • Krypt::ASN1::BMPString <=> value is a String

Krypt::ASN1::BitString

Additional attribute

unused_bits: if the underlying BIT STRING’s length is a multiple of 8 then unused_bits is 0. Otherwise unused_bits indicates the number of bits that are to be ignored in the final octet of the BitString‘s value.

Examples

With the Exception of Krypt::ASN1::EndOfContents and Krypt::ASN1::Null, each Primitive class constructor takes at least one parameter, the value. Since the value of the former two is always nil, they also support a no-arg constructor.

Creating EndOfContents and Null

eoc = Krypt::ASN1::EndOfContents.new
null = Krypt::ASN1::Null.new

Creating any other Primitive

prim = <class>.new(value) # <class> being one of the sub-classes except EndOfContents of Null
prim_zero_context = <class>.new(value, 0, :CONTEXT_SPECIFIC)
prim_zero_private = <class>.new(value, 0, :PRIVATE)

Instance Method Summary collapse

Methods inherited from ASN1Data

#<=>, #encode_to, #infinite_length, #infinite_length=, #tag, #tag=, #tag_class, #tag_class=, #to_der, #value, #value=

Constructor Details

#new(value, tag, tag_class) ⇒ ASN1Data

  • value: the value to be associated. See Primitive for the mappings

between ASN.1 types and Ruby types.

  • tag: a Number representing this value’s tag.

  • tag_class: a Symbol representing one of the four valid tag classes

:UNIVERSAL, :CONTEXT_SPECIFIC, :APPLICATION or :PRIVATE.

Creates an ASN1Data from scratch.



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'ext/krypt/core/krypt_asn1.c', line 359

static VALUE
krypt_asn1_data_initialize(VALUE self, VALUE value, VALUE vtag, VALUE vtag_class)
{
    ID stag_class;
    int tag, tag_class, is_constructed;
    krypt_asn1_data *data;

    int_validate_tag_and_class(vtag, vtag_class);
    tag = NUM2INT(vtag);
    stag_class = SYM2ID(vtag_class);
    if (stag_class == sKrypt_TC_EXPLICIT)
	rb_raise(eKryptASN1Error, "Explicit tagging is only supported for explicit UNIVERSAL sub classes of ASN1Data");
    if (stag_class == sKrypt_TC_UNIVERSAL && tag > 30)
	rb_raise(eKryptASN1Error, "Tag too large for UNIVERSAL tag class");
    if ((tag_class = krypt_asn1_tag_class_for_id(stag_class)) == KRYPT_ERR)
        rb_raise(eKryptASN1Error, "Unknown tag class");
    is_constructed = rb_respond_to(value, sKrypt_ID_EACH);
    
    int_asn1_data_initialize(self, tag, tag_class, is_constructed, 0);

    int_asn1_data_get(self, data);
    data->update_cb = int_asn1_data_update_cb;

    int_asn1_data_set_tag(self, vtag);
    int_asn1_data_set_tag_class(self, vtag_class);
    int_asn1_data_set_infinite_length(self, Qfalse);
    int_asn1_data_set_value(self, value);

    int_asn1_data_set_modified(data, 1); /* newly created is modified by default */

    return self;
}