binenc-ruby 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +78 -0
- data/README.md +35 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/binenc-ruby.gemspec +45 -0
- data/lib/binenc/asn1_engine/asn1_engine.rb +33 -0
- data/lib/binenc/asn1_engine/asn1_object.rb +69 -0
- data/lib/binenc/asn1_engine/object/binary.rb +15 -0
- data/lib/binenc/asn1_engine/object/datetime.rb +30 -0
- data/lib/binenc/asn1_engine/object/integer.rb +15 -0
- data/lib/binenc/asn1_engine/object/oid.rb +16 -0
- data/lib/binenc/asn1_engine/object/sequence.rb +95 -0
- data/lib/binenc/asn1_engine/object/string.rb +20 -0
- data/lib/binenc/factory/bin_struct_factory.rb +14 -0
- data/lib/binenc/factory/sbl_factory/sbl_dsl.rb +172 -0
- data/lib/binenc/factory/sbl_factory/sbl_factory.rb +110 -0
- data/lib/binenc/provider.rb +41 -0
- data/lib/binenc/ruby/version.rb +7 -0
- data/lib/binenc/ruby.rb +31 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 144717c466afede72dbc3e05acd170dd379d092b220974799d9f110e3360b2b2
|
4
|
+
data.tar.gz: 9d14ed325232f998cc087eb159c1ebf1be02d11b1819578f8add29e2fc2a2693
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6a6aa1ccd811f540e1f75b08315500bab77e88ae327bb7165791372d31d5e8e3991620409c6c0815d511cbc446d490417892f23de14af1d5c04388bdef65edd8
|
7
|
+
data.tar.gz: b6a3a02b569a6401ae674cf634e628f0f661dde0e35ee3512514b4873490fb489b59c9240b2ac7b2f734866f0cc1886b03cc4fe002140a087625335e3d355f10
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
binenc-ruby (0.1.0)
|
5
|
+
binenc
|
6
|
+
teLogger
|
7
|
+
toolrack
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
base58 (0.2.3)
|
13
|
+
binenc (0.1.0)
|
14
|
+
teLogger
|
15
|
+
toolrack
|
16
|
+
devops_assist (0.2.0)
|
17
|
+
git_cli
|
18
|
+
git_cli_prompt
|
19
|
+
gvcs
|
20
|
+
teLogger
|
21
|
+
toolrack
|
22
|
+
tty-prompt
|
23
|
+
diff-lcs (1.5.0)
|
24
|
+
git_cli (0.9.0)
|
25
|
+
gvcs
|
26
|
+
ptools (~> 1.4.0)
|
27
|
+
tlogger
|
28
|
+
toolrack
|
29
|
+
git_cli_prompt (0.2.2)
|
30
|
+
tlogger
|
31
|
+
toolrack
|
32
|
+
tty-prompt
|
33
|
+
gvcs (0.1.0)
|
34
|
+
pastel (0.8.0)
|
35
|
+
tty-color (~> 0.5)
|
36
|
+
ptools (1.4.2)
|
37
|
+
rake (13.0.6)
|
38
|
+
rspec (3.11.0)
|
39
|
+
rspec-core (~> 3.11.0)
|
40
|
+
rspec-expectations (~> 3.11.0)
|
41
|
+
rspec-mocks (~> 3.11.0)
|
42
|
+
rspec-core (3.11.0)
|
43
|
+
rspec-support (~> 3.11.0)
|
44
|
+
rspec-expectations (3.11.0)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.11.0)
|
47
|
+
rspec-mocks (3.11.1)
|
48
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
49
|
+
rspec-support (~> 3.11.0)
|
50
|
+
rspec-support (3.11.0)
|
51
|
+
teLogger (0.1.0)
|
52
|
+
tlogger (0.26.3)
|
53
|
+
toolrack (0.18.6)
|
54
|
+
base58
|
55
|
+
tlogger
|
56
|
+
tty-color (0.6.0)
|
57
|
+
tty-cursor (0.7.1)
|
58
|
+
tty-prompt (0.23.1)
|
59
|
+
pastel (~> 0.8)
|
60
|
+
tty-reader (~> 0.8)
|
61
|
+
tty-reader (0.9.0)
|
62
|
+
tty-cursor (~> 0.7)
|
63
|
+
tty-screen (~> 0.8)
|
64
|
+
wisper (~> 2.0)
|
65
|
+
tty-screen (0.8.1)
|
66
|
+
wisper (2.0.1)
|
67
|
+
|
68
|
+
PLATFORMS
|
69
|
+
x86_64-linux
|
70
|
+
|
71
|
+
DEPENDENCIES
|
72
|
+
binenc-ruby!
|
73
|
+
devops_assist
|
74
|
+
rake (~> 13.0)
|
75
|
+
rspec (~> 3.0)
|
76
|
+
|
77
|
+
BUNDLED WITH
|
78
|
+
2.2.28
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Binenc::Ruby
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/binenc/ruby`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'binenc-ruby'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install binenc-ruby
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/binenc-ruby.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "binenc/ruby"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/binenc-ruby.gemspec
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/binenc/ruby/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "binenc-ruby"
|
7
|
+
spec.version = Binenc::Ruby::VERSION
|
8
|
+
spec.authors = ["Ian"]
|
9
|
+
spec.email = ["cameronian0@protonmail.com"]
|
10
|
+
|
11
|
+
spec.summary = ""
|
12
|
+
spec.description = ""
|
13
|
+
spec.homepage = "https://github.com/cameronian/binenc-ruby"
|
14
|
+
spec.required_ruby_version = ">= 2.4.0"
|
15
|
+
|
16
|
+
#spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
17
|
+
|
18
|
+
#spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
#spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
20
|
+
#spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_dependency "teLogger"
|
34
|
+
spec.add_dependency "toolrack"
|
35
|
+
|
36
|
+
spec.add_dependency 'binenc'
|
37
|
+
|
38
|
+
spec.add_development_dependency 'devops_assist'
|
39
|
+
|
40
|
+
# Uncomment to register a new dependency of your gem
|
41
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
42
|
+
|
43
|
+
# For more information and examples about making a new gem, checkout our
|
44
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
45
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
require_relative 'asn1_object'
|
3
|
+
|
4
|
+
module Binenc
|
5
|
+
module Ruby
|
6
|
+
class ASN1Engine
|
7
|
+
|
8
|
+
def object(type, *args)
|
9
|
+
case type
|
10
|
+
when :bin, :binary
|
11
|
+
ASN1Binary.new(*args)
|
12
|
+
when :str, :string
|
13
|
+
ASN1String.new(*args)
|
14
|
+
when :int, :integer, :number, :num
|
15
|
+
ASN1Integer.new(*args)
|
16
|
+
when :seq, :sequence
|
17
|
+
ASN1Sequence.new(*args)
|
18
|
+
when :date, :datetime, :time
|
19
|
+
ASN1DateTime.new(*args)
|
20
|
+
when :oid
|
21
|
+
ASN1OID.new(*args)
|
22
|
+
else
|
23
|
+
raise BinencEngineException, "Unknown ASN1 object '#{type}'"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def from_bin(bin)
|
28
|
+
ASN1Object.decode(bin)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
|
2
|
+
require 'binenc'
|
3
|
+
|
4
|
+
module Binenc
|
5
|
+
module Ruby
|
6
|
+
module ASN1Object
|
7
|
+
include Binenc::BinaryObject
|
8
|
+
include TR::CondUtils
|
9
|
+
|
10
|
+
attr_accessor :value
|
11
|
+
|
12
|
+
def initialize(*args, &block)
|
13
|
+
@value = args.first
|
14
|
+
end
|
15
|
+
|
16
|
+
def is_equal?(val)
|
17
|
+
@value == val
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.decode(bin)
|
21
|
+
if not_empty?(bin)
|
22
|
+
obj = OpenSSL::ASN1.decode(bin)
|
23
|
+
case obj
|
24
|
+
when OpenSSL::ASN1::BitString
|
25
|
+
#logger.debug "Found bitstring value : #{obj.value}"
|
26
|
+
ASN1Binary.new(obj.value)
|
27
|
+
when OpenSSL::ASN1::UTF8String
|
28
|
+
#logger.debug "Found UTF8String value : #{obj.value}"
|
29
|
+
ASN1String.new(obj.value)
|
30
|
+
when OpenSSL::ASN1::Integer
|
31
|
+
#logger.debug "Found Integer value : #{obj.value}"
|
32
|
+
ASN1Integer.new(obj.value)
|
33
|
+
when OpenSSL::ASN1::Sequence
|
34
|
+
#logger.debug "Found sequence value : #{obj.value}"
|
35
|
+
ASN1Sequence.new(obj.value)
|
36
|
+
when OpenSSL::ASN1::GeneralizedTime
|
37
|
+
#logger.debug "Found GeneralizedTime value : #{obj.value}"
|
38
|
+
ASN1DateTime.new(obj.value)
|
39
|
+
when OpenSSL::ASN1::ObjectId
|
40
|
+
#logger.debug "Found ObjectId value : #{obj.value}"
|
41
|
+
ASN1OID.new(obj.value)
|
42
|
+
else
|
43
|
+
raise BinencEngineException, "Unhandled ASN1 object '#{obj.class}'"
|
44
|
+
end
|
45
|
+
else
|
46
|
+
raise BinencEngineException, "Cannot decode empty binary #{bin}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
def self.logger
|
52
|
+
if @logger.nil?
|
53
|
+
@logger = TeLogger::Tlogger.new
|
54
|
+
@logger.tag = :ruby_asn1Obj
|
55
|
+
end
|
56
|
+
@logger
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
Dir.glob(File.join(File.dirname(__FILE__),"object","*.rb")) do |f|
|
66
|
+
require_relative f
|
67
|
+
end
|
68
|
+
|
69
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module Binenc
|
4
|
+
module Ruby
|
5
|
+
|
6
|
+
class ASN1DateTime
|
7
|
+
include ASN1Object
|
8
|
+
|
9
|
+
def encoded
|
10
|
+
raise BinencEngineException, "Given value is not a Time object. #{@value.class}" if not @value.is_a?(Time)
|
11
|
+
OpenSSL::ASN1::GeneralizedTime.new(@value).to_der
|
12
|
+
end
|
13
|
+
|
14
|
+
def is_equal?(val)
|
15
|
+
|
16
|
+
case val
|
17
|
+
when Integer
|
18
|
+
@value.to_i == val
|
19
|
+
when Time
|
20
|
+
@value.to_i == val.to_i
|
21
|
+
else
|
22
|
+
@value == val
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module Binenc
|
4
|
+
module Ruby
|
5
|
+
|
6
|
+
class ASN1Sequence
|
7
|
+
include ASN1Object
|
8
|
+
|
9
|
+
def initialize(*args, &block)
|
10
|
+
super
|
11
|
+
@value = to_value(@value)
|
12
|
+
end
|
13
|
+
|
14
|
+
def encoded
|
15
|
+
@value = [@value] if not @value.is_a?(Array)
|
16
|
+
value = to_encoded(@value)
|
17
|
+
OpenSSL::ASN1::Sequence.new(value).to_der
|
18
|
+
end
|
19
|
+
|
20
|
+
def is_equal?(val)
|
21
|
+
not @value.difference(val).any?
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
#def is_binary_string?(str)
|
26
|
+
# # https://stackoverflow.com/a/32536221/3625825
|
27
|
+
# str.count('01') == str.size
|
28
|
+
#end
|
29
|
+
|
30
|
+
def to_encoded(val)
|
31
|
+
|
32
|
+
if not val.nil?
|
33
|
+
val = [val] if not val.is_a?(Array)
|
34
|
+
|
35
|
+
v = val.map do |e|
|
36
|
+
|
37
|
+
case e
|
38
|
+
when Integer
|
39
|
+
ASN1Integer.new(e).encoded
|
40
|
+
when String
|
41
|
+
if Binenc::Ruby.is_binary_string?(e)
|
42
|
+
ASN1Binary.new(e).encoded
|
43
|
+
else
|
44
|
+
ASN1String.new(e).encoded
|
45
|
+
end
|
46
|
+
when Time
|
47
|
+
ASN1DateTime.new(e).encoded
|
48
|
+
else
|
49
|
+
logger.debug "Missed #{e} / #{e.class}"
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
|
53
|
+
end # map
|
54
|
+
|
55
|
+
v.delete_if { |e| e.nil? }
|
56
|
+
|
57
|
+
else
|
58
|
+
[]
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_value(arr)
|
65
|
+
|
66
|
+
if not arr.nil?
|
67
|
+
arr = [arr] if not arr.is_a?(Array)
|
68
|
+
|
69
|
+
arr.map do |e|
|
70
|
+
|
71
|
+
case e
|
72
|
+
when OpenSSL::ASN1::Primitive
|
73
|
+
v = e.value
|
74
|
+
|
75
|
+
if v.is_a?(OpenSSL::BN)
|
76
|
+
v.to_i
|
77
|
+
else
|
78
|
+
v
|
79
|
+
end
|
80
|
+
else
|
81
|
+
e
|
82
|
+
end
|
83
|
+
|
84
|
+
end # map
|
85
|
+
|
86
|
+
else
|
87
|
+
[]
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,172 @@
|
|
1
|
+
|
2
|
+
require_relative '../../asn1_engine/asn1_engine'
|
3
|
+
|
4
|
+
module Binenc
|
5
|
+
module Ruby
|
6
|
+
|
7
|
+
# Simple Binary Layout Domain Specific Language (DSL)
|
8
|
+
module SBLDSL
|
9
|
+
include TR::CondUtils
|
10
|
+
|
11
|
+
def bin(*args)
|
12
|
+
name = args.first
|
13
|
+
val = args[1]
|
14
|
+
|
15
|
+
raise BinencEngineException, "Name '#{name}' already defined" if keyRegister.keys.include?(name)
|
16
|
+
|
17
|
+
# register name with field type
|
18
|
+
keyRegister[name] = :bin
|
19
|
+
# create instance method dynamically with given name
|
20
|
+
create_instance_method(name, val)
|
21
|
+
# keep the name in sequence as in the layout
|
22
|
+
structure << name
|
23
|
+
end
|
24
|
+
|
25
|
+
def int(*args)
|
26
|
+
name = args.first
|
27
|
+
val = args[1]
|
28
|
+
|
29
|
+
raise BinencEngineException, "Name '#{name}' already defined" if keyRegister.keys.include?(name)
|
30
|
+
|
31
|
+
# register name with field type
|
32
|
+
keyRegister[name] = :int
|
33
|
+
# create instance method dynamically with given name
|
34
|
+
create_instance_method(name, val)
|
35
|
+
# keep the name in sequence as in the layout
|
36
|
+
structure << name
|
37
|
+
end
|
38
|
+
|
39
|
+
def oid(*args)
|
40
|
+
name = args.first
|
41
|
+
val = args[1]
|
42
|
+
|
43
|
+
raise BinencEngineException, "Name '#{name}' already defined" if keyRegister.keys.include?(name)
|
44
|
+
|
45
|
+
# register name with field type
|
46
|
+
keyRegister[name] = :bin
|
47
|
+
# create instance method dynamically with given name
|
48
|
+
create_instance_method(name, val)
|
49
|
+
# keep the name in sequence as in the layout
|
50
|
+
structure << name
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
def str(*args)
|
55
|
+
name = args.first
|
56
|
+
val = args[1]
|
57
|
+
|
58
|
+
raise BinencEngineException, "Name '#{name}' already defined" if keyRegister.keys.include?(name)
|
59
|
+
|
60
|
+
# register name with field type
|
61
|
+
keyRegister[name] = :str
|
62
|
+
# create instance method dynamically with given name
|
63
|
+
create_instance_method(name, val)
|
64
|
+
# keep the name in sequence as in the layout
|
65
|
+
structure << name
|
66
|
+
end
|
67
|
+
|
68
|
+
def date(*args)
|
69
|
+
name = args.first
|
70
|
+
val = args[1]
|
71
|
+
|
72
|
+
raise BinencEngineException, "Name '#{name}' already defined" if keyRegister.keys.include?(name)
|
73
|
+
|
74
|
+
# register name with field type
|
75
|
+
keyRegister[name] = :time
|
76
|
+
# create instance method dynamically with given name
|
77
|
+
create_instance_method(name, val)
|
78
|
+
# keep the name in sequence as in the layout
|
79
|
+
structure << name
|
80
|
+
end
|
81
|
+
alias_method :time, :date
|
82
|
+
alias_method :datetime, :date
|
83
|
+
|
84
|
+
|
85
|
+
def seq(*args)
|
86
|
+
name = args.first
|
87
|
+
val = args[1]
|
88
|
+
|
89
|
+
raise BinencEngineException, "Name '#{name}' already defined" if keyRegister.keys.include?(name)
|
90
|
+
|
91
|
+
# register name with field type
|
92
|
+
keyRegister[name] = :seq
|
93
|
+
# create instance method dynamically with given name
|
94
|
+
create_instance_method(name, val)
|
95
|
+
# keep the name in sequence as in the layout
|
96
|
+
structure << name
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
def keyRegister
|
101
|
+
if @keyReg.nil?
|
102
|
+
@keyReg = { }
|
103
|
+
end
|
104
|
+
@keyReg
|
105
|
+
end
|
106
|
+
|
107
|
+
def structure
|
108
|
+
if @struct.nil?
|
109
|
+
@struct = []
|
110
|
+
end
|
111
|
+
@struct
|
112
|
+
end
|
113
|
+
|
114
|
+
def create_instance_method(name, value = nil)
|
115
|
+
|
116
|
+
raise BinencEngineException, "Name #{name} is not defined" if not keyRegister.keys.include?(name)
|
117
|
+
|
118
|
+
instance_eval <<-END
|
119
|
+
|
120
|
+
def #{name}=(val)
|
121
|
+
\#puts "setting value for #{name} with \#\{val\}"
|
122
|
+
#{to_instance_method_name(name)}.value = val
|
123
|
+
end
|
124
|
+
def #{name}
|
125
|
+
#{to_instance_method_name(name)}.value
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
def #{to_instance_method_name(name)}
|
130
|
+
if @#{to_instance_method_name(name)}.nil?
|
131
|
+
@#{to_instance_method_name(name)} = #{type_to_asn1_object(keyRegister[name])}
|
132
|
+
end
|
133
|
+
|
134
|
+
@#{to_instance_method_name(name)}
|
135
|
+
end
|
136
|
+
|
137
|
+
END
|
138
|
+
|
139
|
+
if not_empty?(value)
|
140
|
+
self.send("#{name}=", value)
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
def to_instance_method_name(name)
|
146
|
+
"#{name}_inst"
|
147
|
+
end
|
148
|
+
|
149
|
+
def type_to_asn1_object(type)
|
150
|
+
case type
|
151
|
+
when :bin
|
152
|
+
"ASN1Binary.new"
|
153
|
+
when :oid
|
154
|
+
"ASN1OID.new"
|
155
|
+
when :int
|
156
|
+
"ASN1Integer.new"
|
157
|
+
when :str
|
158
|
+
"ASN1String.new"
|
159
|
+
when :seq
|
160
|
+
"ASN1Sequence.new"
|
161
|
+
when :time
|
162
|
+
"ASN1DateTime.new"
|
163
|
+
else
|
164
|
+
raise BinencEngineException, "Unknown type to ASN1 object #{type}"
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
|
2
|
+
require_relative 'sbl_dsl'
|
3
|
+
|
4
|
+
module Binenc
|
5
|
+
module Ruby
|
6
|
+
|
7
|
+
# simple binary layout factory
|
8
|
+
class SBLFactory
|
9
|
+
include SBLDSL
|
10
|
+
|
11
|
+
def define(&block)
|
12
|
+
instance_eval(&block)
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def encoded
|
17
|
+
res = []
|
18
|
+
structure.each do |st|
|
19
|
+
res << self.send("#{to_instance_method_name(st)}").encoded
|
20
|
+
end
|
21
|
+
|
22
|
+
ASN1Sequence.new(res).encoded
|
23
|
+
end
|
24
|
+
|
25
|
+
def from_bin(bin)
|
26
|
+
|
27
|
+
seq = ASN1Object.decode(bin).value
|
28
|
+
|
29
|
+
if seq.length > structure.length
|
30
|
+
logger.warn "Given binary has more field (#{seq.length}) than the defined specification (#{structure.length}). Different version of structure?"
|
31
|
+
elsif structure.length > seq.length
|
32
|
+
logger.warn "Defined specification has more field (#{structure.length}) than given binary (#{seq.length}). Different version of structure?"
|
33
|
+
end
|
34
|
+
|
35
|
+
structure.each_index do |i|
|
36
|
+
name = structure[i]
|
37
|
+
|
38
|
+
if seq[i].is_a?(String)
|
39
|
+
# begin block is to cater sequence but tagged as binary field
|
40
|
+
# Therefore during 1st decode, it will become an array with actual value,
|
41
|
+
# no longer a ASN1 encoded field.
|
42
|
+
# Different if the field is tagged as sequence, then this will not happened
|
43
|
+
begin
|
44
|
+
val = ASN1Object.decode(seq[i]).value
|
45
|
+
rescue OpenSSL::ASN1::ASN1Error
|
46
|
+
val = seq[i]
|
47
|
+
end
|
48
|
+
else
|
49
|
+
val = seq[i]
|
50
|
+
end
|
51
|
+
|
52
|
+
case val
|
53
|
+
when OpenSSL::BN
|
54
|
+
val = val.to_i
|
55
|
+
end
|
56
|
+
create_instance_method(name, val)
|
57
|
+
end
|
58
|
+
|
59
|
+
self
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
def value_from_bin_struct(bin, *fieldNo)
|
64
|
+
|
65
|
+
begin
|
66
|
+
seq = ASN1Object.decode(bin).value
|
67
|
+
|
68
|
+
ret = []
|
69
|
+
fieldNo.each do |fn|
|
70
|
+
raise BinencEngineException, "Given field no '#{fn}' to extract is larger than found fields (#{seq.length})" if fn > seq.length
|
71
|
+
|
72
|
+
v = seq[fn]
|
73
|
+
if v.is_a?(String)
|
74
|
+
begin
|
75
|
+
vv = ASN1Object.decode(v).value
|
76
|
+
rescue OpenSSL::ASN1::ASN1Error
|
77
|
+
vv = v
|
78
|
+
end
|
79
|
+
else
|
80
|
+
vv = v
|
81
|
+
end
|
82
|
+
|
83
|
+
case vv
|
84
|
+
when OpenSSL::BN
|
85
|
+
ret << vv.to_i
|
86
|
+
else
|
87
|
+
ret << vv
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
ret
|
92
|
+
rescue OpenSSL::ASN1::ASN1Error => ex
|
93
|
+
raise BinencDecodingError, ex
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
def logger
|
100
|
+
if @logger.nil?
|
101
|
+
@logger = TeLogger::Tlogger.new
|
102
|
+
@logger.tag = :sbl_fact
|
103
|
+
end
|
104
|
+
@logger
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
|
2
|
+
require_relative 'asn1_engine/asn1_engine'
|
3
|
+
|
4
|
+
require_relative 'asn1_engine/asn1_object'
|
5
|
+
|
6
|
+
require_relative 'factory/bin_struct_factory'
|
7
|
+
|
8
|
+
module Binenc
|
9
|
+
module Ruby
|
10
|
+
class Provider
|
11
|
+
include TR::CondUtils
|
12
|
+
|
13
|
+
def self.provider_name
|
14
|
+
"ruby"
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.engine_instance(*args, &block)
|
18
|
+
eng = args.first
|
19
|
+
|
20
|
+
|
21
|
+
if is_empty?(eng)
|
22
|
+
ASN1Engine.new(*args[1..-1], &block)
|
23
|
+
else
|
24
|
+
|
25
|
+
case eng.to_s.downcase.to_sym
|
26
|
+
when :asn1, :asn1_eng
|
27
|
+
ASN1Engine.new(*args[1..-1], &block)
|
28
|
+
when :bin_struct, :binstruct
|
29
|
+
BinStructFactory.instance(*args, &block)
|
30
|
+
else
|
31
|
+
raise BinencEngineException, "Engine #{eng} is not supported"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
data/lib/binenc/ruby.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'teLogger'
|
4
|
+
require 'toolrack'
|
5
|
+
|
6
|
+
|
7
|
+
require_relative "ruby/version"
|
8
|
+
|
9
|
+
require_relative 'provider'
|
10
|
+
|
11
|
+
module Binenc
|
12
|
+
module Ruby
|
13
|
+
class Error < StandardError; end
|
14
|
+
# Your code goes here...
|
15
|
+
|
16
|
+
# detect if the given string is binary or not
|
17
|
+
def self.is_binary_string?(str)
|
18
|
+
# https://stackoverflow.com/a/32536221/3625825
|
19
|
+
str.count('01') == str.size
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# register the provider
|
28
|
+
require 'binenc'
|
29
|
+
Binenc::Provider.instance.register(Binenc::Ruby::Provider)
|
30
|
+
|
31
|
+
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: binenc-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ian
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-08-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: teLogger
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: toolrack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: binenc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: devops_assist
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- cameronian0@protonmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".rspec"
|
77
|
+
- Gemfile
|
78
|
+
- Gemfile.lock
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- bin/console
|
82
|
+
- bin/setup
|
83
|
+
- binenc-ruby.gemspec
|
84
|
+
- lib/binenc/asn1_engine/asn1_engine.rb
|
85
|
+
- lib/binenc/asn1_engine/asn1_object.rb
|
86
|
+
- lib/binenc/asn1_engine/object/binary.rb
|
87
|
+
- lib/binenc/asn1_engine/object/datetime.rb
|
88
|
+
- lib/binenc/asn1_engine/object/integer.rb
|
89
|
+
- lib/binenc/asn1_engine/object/oid.rb
|
90
|
+
- lib/binenc/asn1_engine/object/sequence.rb
|
91
|
+
- lib/binenc/asn1_engine/object/string.rb
|
92
|
+
- lib/binenc/factory/bin_struct_factory.rb
|
93
|
+
- lib/binenc/factory/sbl_factory/sbl_dsl.rb
|
94
|
+
- lib/binenc/factory/sbl_factory/sbl_factory.rb
|
95
|
+
- lib/binenc/provider.rb
|
96
|
+
- lib/binenc/ruby.rb
|
97
|
+
- lib/binenc/ruby/version.rb
|
98
|
+
homepage: https://github.com/cameronian/binenc-ruby
|
99
|
+
licenses: []
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 2.4.0
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubygems_version: 3.2.22
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: ''
|
120
|
+
test_files: []
|