Skip to content

Commit c88a258

Browse files
moved documentation of ASN1 modules/classes to ruby
1 parent 0e44f0d commit c88a258

File tree

2 files changed

+306
-308
lines changed

2 files changed

+306
-308
lines changed

ext/openssl/ossl_asn1.c

Lines changed: 1 addition & 307 deletions
Original file line numberDiff line numberDiff line change
@@ -297,129 +297,6 @@ Init_ossl_asn1(void)
297297
sivINDEFINITE_LENGTH = rb_intern("@indefinite_length");
298298
sivUNUSED_BITS = rb_intern("@unused_bits");
299299

300-
/*
301-
* Document-module: OpenSSL::ASN1
302-
*
303-
* Abstract Syntax Notation One (or ASN.1) is a notation syntax to
304-
* describe data structures and is defined in ITU-T X.680. ASN.1 itself
305-
* does not mandate any encoding or parsing rules, but usually ASN.1 data
306-
* structures are encoded using the Distinguished Encoding Rules (DER) or
307-
* less often the Basic Encoding Rules (BER) described in ITU-T X.690. DER
308-
* and BER encodings are binary Tag-Length-Value (TLV) encodings that are
309-
* quite concise compared to other popular data description formats such
310-
* as XML, JSON etc.
311-
* ASN.1 data structures are very common in cryptographic applications,
312-
* e.g. X.509 public key certificates or certificate revocation lists
313-
* (CRLs) are all defined in ASN.1 and DER-encoded. ASN.1, DER and BER are
314-
* the building blocks of applied cryptography.
315-
* The ASN1 module provides the necessary classes that allow generation
316-
* of ASN.1 data structures and the methods to encode them using a DER
317-
* encoding. The decode method allows parsing arbitrary BER-/DER-encoded
318-
* data to a Ruby object that can then be modified and re-encoded at will.
319-
*
320-
* == ASN.1 class hierarchy
321-
*
322-
* The base class representing ASN.1 structures is ASN1Data. ASN1Data offers
323-
* attributes to read and set the _tag_, the _tag_class_ and finally the
324-
* _value_ of a particular ASN.1 item. Upon parsing, any tagged values
325-
* (implicit or explicit) will be represented by ASN1Data instances because
326-
* their "real type" can only be determined using out-of-band information
327-
* from the ASN.1 type declaration. Since this information is normally
328-
* known when encoding a type, all sub-classes of ASN1Data offer an
329-
* additional attribute _tagging_ that allows to encode a value implicitly
330-
* (+:IMPLICIT+) or explicitly (+:EXPLICIT+).
331-
*
332-
* === Constructive
333-
*
334-
* Constructive is, as its name implies, the base class for all
335-
* constructed encodings, i.e. those that consist of several values,
336-
* opposed to "primitive" encodings with just one single value. The value of
337-
* an Constructive is always an Array.
338-
*
339-
* ==== ASN1::Set and ASN1::Sequence
340-
*
341-
* The most common constructive encodings are SETs and SEQUENCEs, which is
342-
* why there are two sub-classes of Constructive representing each of
343-
* them.
344-
*
345-
* === Primitive
346-
*
347-
* This is the super class of all primitive values. Primitive
348-
* itself is not used when parsing ASN.1 data, all values are either
349-
* instances of a corresponding sub-class of Primitive or they are
350-
* instances of ASN1Data if the value was tagged implicitly or explicitly.
351-
* Please cf. Primitive documentation for details on sub-classes and
352-
* their respective mappings of ASN.1 data types to Ruby objects.
353-
*
354-
* == Possible values for _tagging_
355-
*
356-
* When constructing an ASN1Data object the ASN.1 type definition may
357-
* require certain elements to be either implicitly or explicitly tagged.
358-
* This can be achieved by setting the _tagging_ attribute manually for
359-
* sub-classes of ASN1Data. Use the symbol +:IMPLICIT+ for implicit
360-
* tagging and +:EXPLICIT+ if the element requires explicit tagging.
361-
*
362-
* == Possible values for _tag_class_
363-
*
364-
* It is possible to create arbitrary ASN1Data objects that also support
365-
* a PRIVATE or APPLICATION tag class. Possible values for the _tag_class_
366-
* attribute are:
367-
* * +:UNIVERSAL+ (the default for untagged values)
368-
* * +:CONTEXT_SPECIFIC+ (the default for tagged values)
369-
* * +:APPLICATION+
370-
* * +:PRIVATE+
371-
*
372-
* == Tag constants
373-
*
374-
* There is a constant defined for each universal tag:
375-
* * OpenSSL::ASN1::EOC (0)
376-
* * OpenSSL::ASN1::BOOLEAN (1)
377-
* * OpenSSL::ASN1::INTEGER (2)
378-
* * OpenSSL::ASN1::BIT_STRING (3)
379-
* * OpenSSL::ASN1::OCTET_STRING (4)
380-
* * OpenSSL::ASN1::NULL (5)
381-
* * OpenSSL::ASN1::OBJECT (6)
382-
* * OpenSSL::ASN1::ENUMERATED (10)
383-
* * OpenSSL::ASN1::UTF8STRING (12)
384-
* * OpenSSL::ASN1::SEQUENCE (16)
385-
* * OpenSSL::ASN1::SET (17)
386-
* * OpenSSL::ASN1::NUMERICSTRING (18)
387-
* * OpenSSL::ASN1::PRINTABLESTRING (19)
388-
* * OpenSSL::ASN1::T61STRING (20)
389-
* * OpenSSL::ASN1::VIDEOTEXSTRING (21)
390-
* * OpenSSL::ASN1::IA5STRING (22)
391-
* * OpenSSL::ASN1::UTCTIME (23)
392-
* * OpenSSL::ASN1::GENERALIZEDTIME (24)
393-
* * OpenSSL::ASN1::GRAPHICSTRING (25)
394-
* * OpenSSL::ASN1::ISO64STRING (26)
395-
* * OpenSSL::ASN1::GENERALSTRING (27)
396-
* * OpenSSL::ASN1::UNIVERSALSTRING (28)
397-
* * OpenSSL::ASN1::BMPSTRING (30)
398-
*
399-
* == UNIVERSAL_TAG_NAME constant
400-
*
401-
* An Array that stores the name of a given tag number. These names are
402-
* the same as the name of the tag constant that is additionally defined,
403-
* e.g. <tt>UNIVERSAL_TAG_NAME[2] = "INTEGER"</tt> and <tt>OpenSSL::ASN1::INTEGER = 2</tt>.
404-
*
405-
* == Example usage
406-
*
407-
* === Decoding and viewing a DER-encoded file
408-
* require 'openssl'
409-
* require 'pp'
410-
* der = File.binread('data.der')
411-
* asn1 = OpenSSL::ASN1.decode(der)
412-
* pp der
413-
*
414-
* === Creating an ASN.1 structure and DER-encoding it
415-
* require 'openssl'
416-
* version = OpenSSL::ASN1::Integer.new(1)
417-
* # Explicitly 0-tagged implies context-specific tag class
418-
* serial = OpenSSL::ASN1::Integer.new(12345, 0, :EXPLICIT, :CONTEXT_SPECIFIC)
419-
* name = OpenSSL::ASN1::PrintableString.new('Data 1')
420-
* sequence = OpenSSL::ASN1::Sequence.new( [ version, serial, name ] )
421-
* der = sequence.to_der
422-
*/
423300
mASN1 = rb_define_module_under(mOSSL, "ASN1");
424301

425302
/* Document-class: OpenSSL::ASN1::ASN1Error
@@ -440,190 +317,11 @@ Init_ossl_asn1(void)
440317
rb_ary_store(ary, i, rb_str_new2(ossl_asn1_info[i].name));
441318
}
442319

443-
/* Document-class: OpenSSL::ASN1::ASN1Data
444-
*
445-
* The top-level class representing any ASN.1 object. When parsed by
446-
* ASN1.decode, tagged values are always represented by an instance
447-
* of ASN1Data.
448-
*
449-
* == The role of ASN1Data for parsing tagged values
450-
*
451-
* When encoding an ASN.1 type it is inherently clear what original
452-
* type (e.g. INTEGER, OCTET STRING etc.) this value has, regardless
453-
* of its tagging.
454-
* But opposed to the time an ASN.1 type is to be encoded, when parsing
455-
* them it is not possible to deduce the "real type" of tagged
456-
* values. This is why tagged values are generally parsed into ASN1Data
457-
* instances, but with a different outcome for implicit and explicit
458-
* tagging.
459-
*
460-
* === Example of a parsed implicitly tagged value
461-
*
462-
* An implicitly 1-tagged INTEGER value will be parsed as an
463-
* ASN1Data with
464-
* * _tag_ equal to 1
465-
* * _tag_class_ equal to +:CONTEXT_SPECIFIC+
466-
* * _value_ equal to a String that carries the raw encoding
467-
* of the INTEGER.
468-
* This implies that a subsequent decoding step is required to
469-
* completely decode implicitly tagged values.
470-
*
471-
* === Example of a parsed explicitly tagged value
472-
*
473-
* An explicitly 1-tagged INTEGER value will be parsed as an
474-
* ASN1Data with
475-
* * _tag_ equal to 1
476-
* * _tag_class_ equal to +:CONTEXT_SPECIFIC+
477-
* * _value_ equal to an Array with one single element, an
478-
* instance of OpenSSL::ASN1::Integer, i.e. the inner element
479-
* is the non-tagged primitive value, and the tagging is represented
480-
* in the outer ASN1Data
481-
*
482-
* == Example - Decoding an implicitly tagged INTEGER
483-
* int = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT) # implicit 0-tagged
484-
* seq = OpenSSL::ASN1::Sequence.new( [int] )
485-
* der = seq.to_der
486-
* asn1 = OpenSSL::ASN1.decode(der)
487-
* # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
488-
* # @indefinite_length=false,
489-
* # @tag=16,
490-
* # @tag_class=:UNIVERSAL,
491-
* # @tagging=nil,
492-
* # @value=
493-
* # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
494-
* # @indefinite_length=false,
495-
* # @tag=0,
496-
* # @tag_class=:CONTEXT_SPECIFIC,
497-
* # @value="\x01">]>
498-
* raw_int = asn1.value[0]
499-
* # manually rewrite tag and tag class to make it an UNIVERSAL value
500-
* raw_int.tag = OpenSSL::ASN1::INTEGER
501-
* raw_int.tag_class = :UNIVERSAL
502-
* int2 = OpenSSL::ASN1.decode(raw_int)
503-
* puts int2.value # => 1
504-
*
505-
* == Example - Decoding an explicitly tagged INTEGER
506-
* int = OpenSSL::ASN1::Integer.new(1, 0, :EXPLICIT) # explicit 0-tagged
507-
* seq = OpenSSL::ASN1::Sequence.new( [int] )
508-
* der = seq.to_der
509-
* asn1 = OpenSSL::ASN1.decode(der)
510-
* # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
511-
* # @indefinite_length=false,
512-
* # @tag=16,
513-
* # @tag_class=:UNIVERSAL,
514-
* # @tagging=nil,
515-
* # @value=
516-
* # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
517-
* # @indefinite_length=false,
518-
* # @tag=0,
519-
* # @tag_class=:CONTEXT_SPECIFIC,
520-
* # @value=
521-
* # [#<OpenSSL::ASN1::Integer:0x85bf308
522-
* # @indefinite_length=false,
523-
* # @tag=2,
524-
* # @tag_class=:UNIVERSAL
525-
* # @tagging=nil,
526-
* # @value=1>]>]>
527-
* int2 = asn1.value[0].value[0]
528-
* puts int2.value # => 1
529-
*/
530-
cASN1Data = rb_define_class_under(mASN1, "ASN1Data", rb_cObject);
531320

532-
/* Document-class: OpenSSL::ASN1::Primitive
533-
*
534-
* The parent class for all primitive encodings. Attributes are the same as
535-
* for ASN1Data, with the addition of _tagging_.
536-
* Primitive values can never be encoded with indefinite length form, thus
537-
* it is not possible to set the _indefinite_length_ attribute for Primitive
538-
* and its sub-classes.
539-
*
540-
* == Primitive sub-classes and their mapping to Ruby classes
541-
* * OpenSSL::ASN1::EndOfContent <=> _value_ is always +nil+
542-
* * OpenSSL::ASN1::Boolean <=> _value_ is +true+ or +false+
543-
* * OpenSSL::ASN1::Integer <=> _value_ is an OpenSSL::BN
544-
* * OpenSSL::ASN1::BitString <=> _value_ is a String
545-
* * OpenSSL::ASN1::OctetString <=> _value_ is a String
546-
* * OpenSSL::ASN1::Null <=> _value_ is always +nil+
547-
* * OpenSSL::ASN1::Object <=> _value_ is a String
548-
* * OpenSSL::ASN1::Enumerated <=> _value_ is an OpenSSL::BN
549-
* * OpenSSL::ASN1::UTF8String <=> _value_ is a String
550-
* * OpenSSL::ASN1::NumericString <=> _value_ is a String
551-
* * OpenSSL::ASN1::PrintableString <=> _value_ is a String
552-
* * OpenSSL::ASN1::T61String <=> _value_ is a String
553-
* * OpenSSL::ASN1::VideotexString <=> _value_ is a String
554-
* * OpenSSL::ASN1::IA5String <=> _value_ is a String
555-
* * OpenSSL::ASN1::UTCTime <=> _value_ is a Time
556-
* * OpenSSL::ASN1::GeneralizedTime <=> _value_ is a Time
557-
* * OpenSSL::ASN1::GraphicString <=> _value_ is a String
558-
* * OpenSSL::ASN1::ISO64String <=> _value_ is a String
559-
* * OpenSSL::ASN1::GeneralString <=> _value_ is a String
560-
* * OpenSSL::ASN1::UniversalString <=> _value_ is a String
561-
* * OpenSSL::ASN1::BMPString <=> _value_ is a String
562-
*
563-
* == OpenSSL::ASN1::BitString
564-
*
565-
* === Additional attributes
566-
* _unused_bits_: if the underlying BIT STRING's
567-
* length is a multiple of 8 then _unused_bits_ is 0. Otherwise
568-
* _unused_bits_ indicates the number of bits that are to be ignored in
569-
* the final octet of the BitString's _value_.
570-
*
571-
* == OpenSSL::ASN1::ObjectId
572-
*
573-
* NOTE: While OpenSSL::ASN1::ObjectId.new will allocate a new ObjectId,
574-
* it is not typically allocated this way, but rather that are received from
575-
* parsed ASN1 encodings.
576-
*
577-
* === Additional attributes
578-
* * _sn_: the short name as defined in <openssl/objects.h>.
579-
* * _ln_: the long name as defined in <openssl/objects.h>.
580-
* * _oid_: the object identifier as a String, e.g. "1.2.3.4.5"
581-
* * _short_name_: alias for _sn_.
582-
* * _long_name_: alias for _ln_.
583-
*
584-
* == Examples
585-
* With the Exception of OpenSSL::ASN1::EndOfContent, each Primitive class
586-
* constructor takes at least one parameter, the _value_.
587-
*
588-
* === Creating EndOfContent
589-
* eoc = OpenSSL::ASN1::EndOfContent.new
590-
*
591-
* === Creating any other Primitive
592-
* prim = <class>.new(value) # <class> being one of the sub-classes except EndOfContent
593-
* prim_zero_tagged_implicit = <class>.new(value, 0, :IMPLICIT)
594-
* prim_zero_tagged_explicit = <class>.new(value, 0, :EXPLICIT)
595-
*/
321+
cASN1Data = rb_define_class_under(mASN1, "ASN1Data", rb_cObject);
596322
cASN1Primitive = rb_define_class_under(mASN1, "Primitive", cASN1Data);
597323

598-
/* Document-class: OpenSSL::ASN1::Constructive
599-
*
600-
* The parent class for all constructed encodings. The _value_ attribute
601-
* of a Constructive is always an Array. Attributes are the same as
602-
* for ASN1Data, with the addition of _tagging_.
603-
*
604-
* == SET and SEQUENCE
605-
*
606-
* Most constructed encodings come in the form of a SET or a SEQUENCE.
607-
* These encodings are represented by one of the two sub-classes of
608-
* Constructive:
609-
* * OpenSSL::ASN1::Set
610-
* * OpenSSL::ASN1::Sequence
611-
* Please note that tagged sequences and sets are still parsed as
612-
* instances of ASN1Data. Find further details on tagged values
613-
* there.
614-
*
615-
* === Example - constructing a SEQUENCE
616-
* int = OpenSSL::ASN1::Integer.new(1)
617-
* str = OpenSSL::ASN1::PrintableString.new('abc')
618-
* sequence = OpenSSL::ASN1::Sequence.new( [ int, str ] )
619-
*
620-
* === Example - constructing a SET
621-
* int = OpenSSL::ASN1::Integer.new(1)
622-
* str = OpenSSL::ASN1::PrintableString.new('abc')
623-
* set = OpenSSL::ASN1::Set.new( [ int, str ] )
624-
*/
625324
cASN1Constructive = rb_define_class_under(mASN1,"Constructive", cASN1Data);
626-
627325
#define OSSL_ASN1_DEFINE_CLASS(name, super) \
628326
do{\
629327
cASN1##name = rb_define_class_under(mASN1, #name, cASN1##super);\
@@ -657,10 +355,6 @@ do{\
657355
OSSL_ASN1_DEFINE_CLASS(EndOfContent, Data);
658356

659357

660-
/* Document-class: OpenSSL::ASN1::ObjectId
661-
*
662-
* Represents the primitive object id for OpenSSL::ASN1
663-
*/
664358
#if 0
665359
cASN1ObjectId = rb_define_class_under(mASN1, "ObjectId", cASN1Primitive); /* let rdoc know */
666360
#endif

0 commit comments

Comments
 (0)