-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewriting most of the asn1 init code in ruby
to have as much of the lib in ruby as possible
- Loading branch information
1 parent
a8caa63
commit a492c19
Showing
3 changed files
with
170 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# frozen_string_literal: true | ||
#-- | ||
# | ||
# = Ruby-space definitions that completes C-space funcs for BN | ||
# | ||
# = Licence | ||
# This program is licensed under the same licence as Ruby. | ||
# (See the file 'LICENCE'.) | ||
#++ | ||
|
||
module OpenSSL | ||
module ASN1 | ||
class ASN1Data | ||
attr_accessor :value, :tag, :tag_class | ||
|
||
attr_reader :indefinite_length | ||
|
||
def initialize(value, tag, tag_class) | ||
raise ASN1Error, "invalid tag class" unless tag_class.is_a?(Symbol) | ||
|
||
@tag = tag | ||
@value = value | ||
@tag_class = tag_class | ||
@indefinite_length = false | ||
end | ||
|
||
def indefinite_length=(val) | ||
@indefinite_length = val | ||
end | ||
|
||
alias infinite_length indefinite_length | ||
alias infinite_length= indefinite_length= | ||
end | ||
|
||
class Primitive < ASN1Data | ||
attr_accessor :tagging | ||
|
||
undef_method :indefinite_length= | ||
undef_method :infinite_length= | ||
|
||
def initialize(value, tag = nil, tagging = nil, tag_class = nil) | ||
tag ||= ASN1.take_default_tag(self.class) | ||
|
||
raise ASN1Error, "must specify tag number" unless tag | ||
|
||
if tagging | ||
raise ASN1Error, "invalid tagging method" unless tagging.is_a?(Symbol) | ||
end | ||
|
||
tag_class ||= tagging ? :CONTEXT_SPECIFIC : :UNIVERSAL | ||
|
||
raise ASN1Error, "invalid tag class" unless tag_class.is_a?(Symbol) | ||
|
||
@tagging = tagging | ||
super(value ,tag, tag_class) | ||
end | ||
end | ||
|
||
class Constructive < ASN1Data | ||
include Enumerable | ||
|
||
attr_accessor :tagging | ||
|
||
def initialize(value, tag = nil, tagging = nil, tag_class = nil) | ||
tag ||= ASN1.take_default_tag(self.class) | ||
|
||
raise ASN1Error, "must specify tag number" unless tag | ||
|
||
if tagging | ||
raise ASN1Error, "invalid tagging method" unless tagging.is_a?(Symbol) | ||
end | ||
|
||
tag_class ||= tagging ? :CONTEXT_SPECIFIC : :UNIVERSAL | ||
|
||
raise ASN1Error, "invalid tag class" unless tag_class.is_a?(Symbol) | ||
|
||
@tagging = tagging | ||
super(value ,tag, tag_class) | ||
end | ||
|
||
def each(&blk) | ||
@value.each(&blk) | ||
end | ||
end | ||
|
||
class Boolean < Primitive ; end | ||
class Integer < Primitive ; end | ||
class Enumerated < Primitive ; end | ||
|
||
class BitString < Primitive | ||
attr_accessor :unused_bits | ||
|
||
def initialize(*) | ||
super | ||
|
||
@unused_bits = 0 | ||
end | ||
end | ||
|
||
class EndOfContent < ASN1Data | ||
def initialize | ||
super("", 0, :UNIVERSAL) | ||
end | ||
end | ||
|
||
# private | ||
def self.take_default_tag(klass) | ||
tag = CLASS_TAG_MAP[klass] | ||
|
||
return tag if tag | ||
|
||
sklass = klass.superclass | ||
|
||
return unless sklass | ||
|
||
take_default_tag(sklass) | ||
end | ||
end | ||
end |