Sha256: 5628c17541fd77f3003ae168c58a224731f38c9903d7f69a64619bb3dff14caa

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'rasn1/version'
require 'rasn1/errors'
require 'rasn1/types'
require 'rasn1/model'

# Rasn1 is a pure ruby library to parse, decode and encode ASN.1 data.
# @author Sylvain Daubert
module RASN1
  # Parse a DER/BER string without checking a model
  # @note If you want to check ASN.1 grammary, you should define a {Model}
  #       and use {Model#parse}.
  # @note This method will never decode SEQUENCE OF or SET OF objects, as these
  #       ones use the same encoding as SEQUENCE and SET, respectively.
  # @note Real type of tagged value cannot be guessed. So, such tag will
  #       generate {Types::Base} objects.
  # @param [String] der binary string to parse
  # @param [Boolean] ber if +true+, decode a BER string, else a DER one
  # @return [Types::Base]
  def self.parse(der, ber: false)
    root = nil
    until der.empty?
      type = Types.id2type(der)
      type.parse!(der, ber: ber)
      root ||= type

      if [Types::Sequence, Types::Set].include? type.class
        subder = type.value
        ary = []
        until subder.empty?
          ary << self.parse(subder)
          subder = subder[ary.last.to_der.size..-1]
        end
        type.value = ary
      end
      der = der[type.to_der.size..-1]
    end
    root
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rasn1-0.11.0 lib/rasn1.rb