binenc-java 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a3ff5a49611f9e67b97f742acd87debdc32b2694c9790a2ebeb4bc4f9f3f9166
4
+ data.tar.gz: ce244062aeb2762574838c619c3feb8aaa6331dd20a962be0819e203dc31be50
5
+ SHA512:
6
+ metadata.gz: 7c0d5f3ee644f9b16aa193866c74a2ca5b6088e2f9e5d52e88ba648b0cd270181d074eec2d14d2cc42dc59e2fa68f311aa31d3a7d7cd2e11f6d50919343419f0
7
+ data.tar.gz: cd24cbe77105ecbb70ca8c089b443c97ab73a2bb0ad674dc0b911dff0cf602aa9205d01570425e36e3a623756589b8d4df180d30f3745ca050c62fd7a978a1a8
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in binenc-java.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ #gem 'binenc', git: 'binenc', branch: "master"
13
+
data/Gemfile.lock ADDED
@@ -0,0 +1,78 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ binenc-java (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
+ universal-java-11
70
+
71
+ DEPENDENCIES
72
+ binenc-java!
73
+ devops_assist
74
+ rake (~> 13.0)
75
+ rspec (~> 3.0)
76
+
77
+ BUNDLED WITH
78
+ 2.3.6
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Binenc::Java
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/java`. 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-java'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install binenc-java
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-java.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ require 'devops_assist'
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task default: :spec
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/binenc/java/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "binenc-java"
7
+ spec.version = Binenc::Java::VERSION
8
+ spec.authors = ["Ian"]
9
+ spec.email = ["cameronian0@protonmail.com"]
10
+
11
+ spec.summary = ""
12
+ spec.description = ""
13
+ spec.homepage = ""
14
+ spec.required_ruby_version = ">= 2.6.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(?:(?:bin|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, check out our
44
+ # guide at: https://bundler.io/guides/creating_gem.html
45
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,33 @@
1
+
2
+ require_relative 'asn1_object'
3
+
4
+ module Binenc
5
+ module Java
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,78 @@
1
+
2
+ require 'binenc'
3
+
4
+ require_relative '../data_conversion'
5
+
6
+ module Binenc
7
+ module Java
8
+ module ASN1Object
9
+ include Binenc::BinaryObject
10
+ include TR::CondUtils
11
+ include DataConversion
12
+
13
+ attr_accessor :value
14
+
15
+ def initialize(*args, &block)
16
+ @value = args.first
17
+ end
18
+
19
+ def is_equal?(val)
20
+ @value == val
21
+ end
22
+
23
+ def self.decode(bin)
24
+ if not_empty?(bin)
25
+
26
+ begin
27
+ ais = ::Java::OrgBouncycastleAsn1::ASN1InputStream.new(to_java_bytes(bin))
28
+ obj = ais.readObject
29
+
30
+ case obj
31
+ when ::Java::OrgBouncycastleAsn1::DERBitString
32
+ ASN1Binary.new(obj.bytes)
33
+ when ::Java::OrgBouncycastleAsn1::DERUTF8String
34
+ ASN1String.new(obj.to_s)
35
+ when ::Java::OrgBouncycastleAsn1::ASN1Integer
36
+ ASN1Integer.new(obj.value)
37
+ when ::Java::OrgBouncycastleAsn1::DERSequence, ::Java::OrgBouncycastleAsn1::DLSequence
38
+ ASN1Sequence.new(obj.to_a)
39
+ when ::Java::OrgBouncycastleAsn1::ASN1GeneralizedTime
40
+ ASN1DateTime.new(obj.date)
41
+ when ::Java::OrgBouncycastleAsn1::ASN1ObjectIdentifier
42
+ ASN1OID.new(obj.id)
43
+ else
44
+ raise BinencEngineException, "Unhandled ASN1 object '#{obj.class}'"
45
+ end
46
+ rescue ::Java::JavaIo::IOException => ex
47
+ raise BinencDecodingError, ex
48
+ end
49
+
50
+ else
51
+ raise BinencEngineException, "Cannot decode empty binary #{bin}"
52
+ end
53
+ end
54
+
55
+ private
56
+ def self.logger
57
+ if @logger.nil?
58
+ @logger = TeLogger::Tlogger.new
59
+ @logger.tag = :java_asn1Obj
60
+ end
61
+ @logger
62
+ end
63
+
64
+ def logger
65
+ self.class.logger
66
+ end
67
+
68
+
69
+ end
70
+ end
71
+ end
72
+
73
+
74
+ Dir.glob(File.join(File.dirname(__FILE__),"object","*.rb")) do |f|
75
+ require_relative f
76
+ end
77
+
78
+
@@ -0,0 +1,25 @@
1
+
2
+
3
+ module Binenc
4
+ module Java
5
+
6
+ class ASN1Binary
7
+ include ASN1Object
8
+
9
+ def encoded(binary = true)
10
+ obj = org.bouncycastle.asn1.DERBitString.new(to_java_bytes(@value))
11
+
12
+ if binary
13
+ obj.encoded
14
+ else
15
+ obj
16
+ end
17
+ end
18
+
19
+ def is_equal?(val)
20
+ @value == to_java_bytes(val)
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+
2
+
3
+ module Binenc
4
+ module Java
5
+
6
+ class ASN1DateTime
7
+ include ASN1Object
8
+
9
+ def encoded(binary = true)
10
+ raise BinencEngineException, "Given value is not a Time object. #{@value.class}" if not (@value.is_a?(Time) or @value.is_a?(java.util.Date))
11
+ obj = org.bouncycastle.asn1.DERGeneralizedTime.new(@value)
12
+ if binary
13
+ obj.encoded
14
+ else
15
+ obj
16
+ end
17
+ end
18
+
19
+ def is_equal?(val)
20
+
21
+ case val
22
+ when Integer
23
+ @value.time.to_i == val
24
+ when Time
25
+ @value.time.to_i == val.to_i*1000
26
+ else
27
+ @value == val
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+
2
+
3
+ module Binenc
4
+ module Java
5
+
6
+ class ASN1Integer
7
+ include ASN1Object
8
+
9
+ def encoded(binary = true)
10
+ obj = org.bouncycastle.asn1.DERInteger.new(@value)
11
+ if binary
12
+ obj.encoded
13
+ else
14
+ obj
15
+ end
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+
2
+
3
+ module Binenc
4
+ module Java
5
+
6
+ class ASN1OID
7
+ include ASN1Object
8
+
9
+ def encoded
10
+ org.bouncycastle.asn1.ASN1ObjectIdentifier.new(@value).encoded
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,154 @@
1
+
2
+
3
+ module Binenc
4
+ module Java
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(binary = true)
15
+ @value = [@value] if not @value.is_a?(Array)
16
+ value = to_encoded(@value)
17
+ obj = org.bouncycastle.asn1.DERSequence.new(value)
18
+
19
+ if binary
20
+ obj.encoded
21
+ else
22
+ obj
23
+ end
24
+ end
25
+
26
+ def is_equal?(val)
27
+
28
+ if @value.length == val.length
29
+ res = true
30
+ @value.each do |e|
31
+ if not val.include?(e)
32
+ res = false
33
+ break
34
+ end
35
+ end
36
+ res
37
+ else
38
+ false
39
+ end
40
+ end
41
+
42
+ private
43
+ def is_binary_string?(str)
44
+ not str.ascii_only?
45
+ end
46
+
47
+ def to_encoded(val)
48
+
49
+ if not val.nil?
50
+ val = [val] if not val.is_a?(Array)
51
+
52
+ v = val.map do |e|
53
+
54
+ case e
55
+ when Integer
56
+ ASN1Integer.new(e).encoded(false)
57
+ when String
58
+ if is_binary_string?(e)
59
+ ASN1Binary.new(e).encoded(false)
60
+ else
61
+ ASN1String.new(e).encoded(false)
62
+ end
63
+ when ::Java::byte[]
64
+ ASN1Binary.new(e).encoded(false)
65
+ when Array
66
+ ASN1Sequence.new(e).encoded(false)
67
+ when Time, java.util.Date
68
+ ASN1DateTime.new(e).encoded(false)
69
+
70
+ when ::Java::OrgBouncycastleAsn1::DERBitString
71
+ ASN1Binary.new(e.bytes).encoded(false)
72
+ when ::Java::OrgBouncycastleAsn1::DERUTF8String
73
+ ASN1String.new(e.to_s).encoded(false)
74
+ when ::Java::OrgBouncycastleAsn1::ASN1Integer
75
+ ASN1Integer.new(e.value).encoded(false)
76
+ when ::Java::OrgBouncycastleAsn1::DERSequence, ::Java::OrgBouncycastleAsn1::DLSequence
77
+ ASN1Sequence.new(e.to_a).encoded(false)
78
+ when ::Java::OrgBouncycastleAsn1::ASN1GeneralizedTime
79
+ ASN1DateTime.new(e.date).encoded(false)
80
+ when ::Java::OrgBouncycastleAsn1::ASN1ObjectIdentifier
81
+ ASN1OID.new(e.id).encoded(false)
82
+ else
83
+ logger.debug "Oopss #{e} / #{e.class}"
84
+ nil
85
+ end
86
+
87
+ end # map
88
+
89
+ v.delete_if { |e| e.nil? }
90
+
91
+ vec = org.bouncycastle.asn1.ASN1EncodableVector.new
92
+ v.each do |ve|
93
+ vec.add(ve)
94
+ end
95
+ vec
96
+
97
+ else
98
+ org.bouncycastle.asn1.ASN1EncodableVector.new
99
+ end
100
+
101
+
102
+ end
103
+
104
+ def to_value(arr)
105
+
106
+ if not arr.nil?
107
+ arr = [arr] if not arr.is_a?(Array)
108
+
109
+ arr.map do |e|
110
+ case e
111
+ when ::Java::byte[]
112
+ ASN1Object.decode(e).value
113
+ when ::Java::OrgBouncycastleAsn1::ASN1Object
114
+ case e
115
+ when ::Java::OrgBouncycastleAsn1::DERBitString
116
+ String.from_java_bytes(e.bytes)
117
+ when ::Java::OrgBouncycastleAsn1::DERUTF8String
118
+ e.to_s
119
+ when ::Java::OrgBouncycastleAsn1::ASN1Integer
120
+ e.value
121
+ when ::Java::OrgBouncycastleAsn1::DERSequence, ::Java::OrgBouncycastleAsn1::DLSequence
122
+ to_value(e.to_a)
123
+ when ::Java::OrgBouncycastleAsn1::ASN1GeneralizedTime
124
+ d = e.date
125
+ Time.at(d.time/1000)
126
+ when ::Java::OrgBouncycastleAsn1::ASN1ObjectIdentifier
127
+ e.id
128
+ else
129
+ raise BinencEngineException, "Unhandled ASN1 object '#{obj.class}'"
130
+ end
131
+ else
132
+ e
133
+ end
134
+
135
+ end # map
136
+
137
+ else
138
+ []
139
+ end
140
+
141
+ end
142
+
143
+ def logger
144
+ if @logger.nil?
145
+ @logger = TeLogger::Tlogger.new
146
+ @logger.tag = :asn1_seq
147
+ end
148
+ @logger
149
+ end
150
+
151
+ end
152
+
153
+ end
154
+ end
@@ -0,0 +1,25 @@
1
+
2
+
3
+ module Binenc
4
+ module Java
5
+
6
+ class ASN1String
7
+ include ASN1Object
8
+
9
+ def encoded(binary = true)
10
+ obj = org.bouncycastle.asn1.DERUTF8String.new(@value.to_s)
11
+ if binary
12
+ obj.encoded
13
+ else
14
+ obj
15
+ end
16
+ end
17
+
18
+ def is_equal?(val)
19
+ @value.bytes == val.bytes
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,73 @@
1
+
2
+ module Binenc
3
+ module Java
4
+ module DataConversion
5
+
6
+ def to_hex(bin)
7
+ String.from_java_bytes(org.bouncycastle.util.encoders.Hex.encode(bin))
8
+ end
9
+ # end to_hex
10
+
11
+ def from_hex(str)
12
+ org.bouncycastle.util.encoders.Hex.decode(str)
13
+ end
14
+ # end from_hex
15
+
16
+ def to_b64(bin)
17
+ String.from_java_bytes(java.util.Base64.encoder.encode(bin))
18
+ end
19
+ # end to_b64
20
+ #
21
+
22
+ def to_b64_mime(bin)
23
+ String.from_java_bytes(java.util.Base64.mime_encoder.encode(bin))
24
+ end
25
+
26
+ def from_b64(str)
27
+ java.util.Base64.decoder.decode(str)
28
+ end
29
+ # end from_b64
30
+
31
+ def to_str(bin)
32
+ if bin.is_a?(::Java::byte[])
33
+ String.from_java_bytes(bin)
34
+ else
35
+ bin
36
+ end
37
+ end
38
+
39
+ def to_bin(str)
40
+ if str.nil?
41
+ ::Java::byte[0].new
42
+ else
43
+ str.to_java.getBytes
44
+ end
45
+ end
46
+
47
+ def to_java_bytes(val, encoding = nil)
48
+ case val
49
+ when String
50
+ val.to_java_bytes
51
+ when java.lang.String
52
+ if not_empty?(encoding)
53
+ val.getBytes(encoding)
54
+ else
55
+ val.getBytes
56
+ end
57
+ else
58
+ val
59
+ end
60
+ end
61
+
62
+ #
63
+ # Add the methods to class level
64
+ #
65
+ def self.included(klass)
66
+ klass.class_eval <<-END
67
+ extend Binenc::Java::DataConversion
68
+ END
69
+ end
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,14 @@
1
+
2
+ require_relative 'sbl_factory/sbl_factory'
3
+
4
+ module Binenc
5
+ module Java
6
+ module BinStructFactory
7
+
8
+ def self.instance(*args, &block)
9
+ SBLFactory.new
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,169 @@
1
+
2
+ require_relative '../../asn1_engine/asn1_engine'
3
+
4
+ module Binenc
5
+ module Java
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
+
83
+ def seq(*args)
84
+ name = args.first
85
+ val = args[1]
86
+
87
+ raise BinencEngineException, "Name '#{name}' already defined" if keyRegister.keys.include?(name)
88
+
89
+ # register name with field type
90
+ keyRegister[name] = :seq
91
+ # create instance method dynamically with given name
92
+ create_instance_method(name, val)
93
+ # keep the name in sequence as in the layout
94
+ structure << name
95
+ end
96
+
97
+ private
98
+ def keyRegister
99
+ if @keyReg.nil?
100
+ @keyReg = { }
101
+ end
102
+ @keyReg
103
+ end
104
+
105
+ def structure
106
+ if @struct.nil?
107
+ @struct = []
108
+ end
109
+ @struct
110
+ end
111
+
112
+ def create_instance_method(name, value = nil)
113
+
114
+ raise BinencEngineException, "Name #{name} is not defined" if not keyRegister.keys.include?(name)
115
+
116
+ instance_eval <<-END
117
+
118
+ def #{name}=(val)
119
+ #{name}_inst.value = val
120
+ end
121
+ def #{name}
122
+ #{name}_inst.value
123
+ end
124
+
125
+
126
+ def #{to_instance_method_name(name)}
127
+ if @#{to_instance_method_name(name)}.nil?
128
+ @#{to_instance_method_name(name)} = #{type_to_asn1_object(keyRegister[name])}
129
+ end
130
+
131
+ @#{to_instance_method_name(name)}
132
+ end
133
+
134
+ END
135
+
136
+ if not_empty?(value)
137
+ self.send("#{name}=", value)
138
+ end
139
+
140
+ end
141
+
142
+ def to_instance_method_name(name)
143
+ "#{name}_inst"
144
+ end
145
+
146
+ def type_to_asn1_object(type)
147
+ case type
148
+ when :bin
149
+ "ASN1Binary.new"
150
+ when :oid
151
+ "ASN1OID.new"
152
+ when :int
153
+ "ASN1Integer.new"
154
+ when :str
155
+ "ASN1String.new"
156
+ when :seq
157
+ "ASN1Sequence.new"
158
+ when :time
159
+ "ASN1DateTime.new"
160
+ else
161
+ raise BinencEngineException, "Unknown type to ASN1 object #{type}"
162
+ end
163
+ end
164
+
165
+
166
+ end
167
+
168
+ end
169
+ end
@@ -0,0 +1,103 @@
1
+
2
+ require_relative 'sbl_dsl'
3
+
4
+ module Binenc
5
+ module Java
6
+
7
+ # simple binary layout factory
8
+ class SBLFactory
9
+ include SBLDSL
10
+ include DataConversion
11
+
12
+ def define(&block)
13
+ instance_eval(&block)
14
+ self
15
+ end
16
+
17
+ def encoded
18
+ res = []
19
+ structure.each do |st|
20
+ res << self.send("#{to_instance_method_name(st)}").encoded(false)
21
+ end
22
+
23
+ ASN1Sequence.new(res).encoded
24
+ end
25
+
26
+ def from_bin(bin)
27
+
28
+ seq = ASN1Object.decode(bin).value
29
+
30
+ if seq.length > structure.length
31
+ logger.warn "Given binary has more field (#{seq.length}) than the defined specification (#{structure.length}). Different version of structure?"
32
+ elsif structure.length > seq.length
33
+ logger.warn "Defined specification has more field (#{structure.length}) than given binary (#{seq.length}). Different version of structure?"
34
+ end
35
+
36
+ structure.each_index do |i|
37
+ name = structure[i]
38
+ val = seq[i]
39
+ create_instance_method(name, val)
40
+ end
41
+
42
+ self
43
+
44
+ end
45
+
46
+ def value_from_bin_struct(bin, *fieldNo)
47
+
48
+ seq = ASN1Object.decode(bin).value
49
+
50
+ ret = []
51
+ fieldNo.each do |fn|
52
+ raise BinencEngineException, "Given field no '#{fn}' to extract is larger than found fields (#{seq.length})" if fn > seq.length
53
+
54
+ v = seq[fn]
55
+ if v.is_a?(::Java::OrgBouncycastleAsn1::ASN1Object)
56
+ case v
57
+ when ::Java::OrgBouncycastleAsn1::DERBitString
58
+ #vv = String.from_java_bytes(v.bytes)
59
+ vv = to_java_bytes(v.bytes)
60
+ when ::Java::OrgBouncycastleAsn1::DERUTF8String
61
+ vv = v.to_s
62
+ when ::Java::OrgBouncycastleAsn1::ASN1Integer
63
+ vv = v.value
64
+ #when ::Java::OrgBouncycastleAsn1::DERSequence, ::Java::OrgBouncycastleAsn1::DLSequence
65
+ # to_value(v.to_a)
66
+ when ::Java::OrgBouncycastleAsn1::ASN1GeneralizedTime
67
+ d = v.date
68
+ vv = Time.at(d.time/1000)
69
+ when ::Java::OrgBouncycastleAsn1::ASN1ObjectIdentifier
70
+ vv = v.id
71
+ else
72
+ raise BinencEngineException, "Unhandled ASN1 object '#{obj.class}'"
73
+ end
74
+
75
+ #begin
76
+ # vv = ASN1Object.decode(v).value
77
+ #rescue OpenSSL::ASN1::ASN1Error
78
+ # vv = v
79
+ #end
80
+ else
81
+ vv = v
82
+ end
83
+
84
+ ret << vv
85
+
86
+ end
87
+
88
+ ret
89
+ end
90
+
91
+ private
92
+ def logger
93
+ if @logger.nil?
94
+ @logger = TeLogger::Tlogger.new
95
+ @logger.tag = :sbl_fact
96
+ end
97
+ @logger
98
+ end
99
+
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Binenc
4
+ module Java
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'teLogger'
4
+ require 'toolrack'
5
+
6
+ Dir.glob(File.join(File.dirname(__FILE__),"..","..","jars","*.jar")).each do |f|
7
+ require f
8
+ #puts "Loaded #{f}"
9
+ end
10
+
11
+ require_relative "java/version"
12
+
13
+ require_relative 'provider'
14
+
15
+ module Binenc
16
+ module Java
17
+ class Error < StandardError; end
18
+ # Your code goes here...
19
+ end
20
+ end
21
+
22
+
23
+ # register the provider
24
+ require 'binenc'
25
+ Binenc::Provider.instance.register(Binenc::Java::Provider)
26
+
27
+
@@ -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 Java
10
+ class Provider
11
+ include TR::CondUtils
12
+
13
+ def self.provider_name
14
+ "Java"
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
+
@@ -0,0 +1,6 @@
1
+ module Binenc
2
+ module Java
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: binenc-java
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
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ name: teLogger
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ name: toolrack
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ name: binenc
48
+ prerelease: false
49
+ type: :runtime
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: devops_assist
62
+ prerelease: false
63
+ type: :development
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
+ - binenc-java.gemspec
82
+ - jars/bcmail-jdk15on-165.jar
83
+ - jars/bcpg-jdk15on-165.jar
84
+ - jars/bcpkix-jdk15on-165.jar
85
+ - jars/bcprov-ext-jdk15on-165.jar
86
+ - jars/bcprov-jdk15on-165.jar
87
+ - jars/bctls-jdk15on-165.jar
88
+ - lib/binenc/asn1_engine/asn1_engine.rb
89
+ - lib/binenc/asn1_engine/asn1_object.rb
90
+ - lib/binenc/asn1_engine/object/binary.rb
91
+ - lib/binenc/asn1_engine/object/datetime.rb
92
+ - lib/binenc/asn1_engine/object/integer.rb
93
+ - lib/binenc/asn1_engine/object/oid.rb
94
+ - lib/binenc/asn1_engine/object/sequence.rb
95
+ - lib/binenc/asn1_engine/object/string.rb
96
+ - lib/binenc/data_conversion.rb
97
+ - lib/binenc/factory/bin_struct_factory.rb
98
+ - lib/binenc/factory/sbl_factory/sbl_dsl.rb
99
+ - lib/binenc/factory/sbl_factory/sbl_factory.rb
100
+ - lib/binenc/java.rb
101
+ - lib/binenc/java/version.rb
102
+ - lib/binenc/provider.rb
103
+ - sig/binenc/java.rbs
104
+ homepage: ''
105
+ licenses: []
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: 2.6.0
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubygems_version: 3.2.29
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: ''
126
+ test_files: []