ccrypto-java 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/.java-version +1 -0
- data/.rspec +3 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +94 -0
- data/README.md +150 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/ccrypto-java.gemspec +44 -0
- data/jars/bcmail-jdk15on-165.jar +0 -0
- data/jars/bcpg-jdk15on-165.jar +0 -0
- data/jars/bcpkix-jdk15on-165.jar +0 -0
- data/jars/bcprov-ext-jdk15on-165.jar +0 -0
- data/jars/bcprov-jdk15on-165.jar +0 -0
- data/jars/bctls-jdk15on-165.jar +0 -0
- data/jars/shamir-0.6.1-p.jar +0 -0
- data/lib/ccrypto/java/data_conversion.rb +80 -0
- data/lib/ccrypto/java/engines/asn1_engine.rb +161 -0
- data/lib/ccrypto/java/engines/asn1_object.rb +12 -0
- data/lib/ccrypto/java/engines/cipher_engine.rb +255 -0
- data/lib/ccrypto/java/engines/compression_engine.rb +92 -0
- data/lib/ccrypto/java/engines/data_conversion_engine.rb +9 -0
- data/lib/ccrypto/java/engines/decompression_engine.rb +48 -0
- data/lib/ccrypto/java/engines/digest_engine.rb +208 -0
- data/lib/ccrypto/java/engines/ecc_engine.rb +263 -0
- data/lib/ccrypto/java/engines/hkdf_engine.rb +72 -0
- data/lib/ccrypto/java/engines/hmac_engine.rb +75 -0
- data/lib/ccrypto/java/engines/pbkdf2_engine.rb +87 -0
- data/lib/ccrypto/java/engines/pkcs7_engine.rb +558 -0
- data/lib/ccrypto/java/engines/rsa_engine.rb +572 -0
- data/lib/ccrypto/java/engines/scrypt_engine.rb +35 -0
- data/lib/ccrypto/java/engines/secret_key_engine.rb +44 -0
- data/lib/ccrypto/java/engines/secret_sharing_engine.rb +59 -0
- data/lib/ccrypto/java/engines/secure_random_engine.rb +76 -0
- data/lib/ccrypto/java/engines/x509_engine.rb +311 -0
- data/lib/ccrypto/java/ext/secret_key.rb +75 -0
- data/lib/ccrypto/java/ext/x509_cert.rb +48 -0
- data/lib/ccrypto/java/jce_provider.rb +52 -0
- data/lib/ccrypto/java/keybundle_store/pkcs12.rb +125 -0
- data/lib/ccrypto/java/utils/comparator.rb +20 -0
- data/lib/ccrypto/java/utils/memory_buffer.rb +77 -0
- data/lib/ccrypto/java/utils/native_helper.rb +19 -0
- data/lib/ccrypto/java/version.rb +7 -0
- data/lib/ccrypto/java.rb +30 -0
- data/lib/ccrypto/provider.rb +132 -0
- metadata +144 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6de516075322f46df8dc000fb1160c5719cc993b9ac3619e643eef54dfa4cf7c
|
4
|
+
data.tar.gz: eca79e4bf92f9cefb090e5aaa06401774a19d4d194cff264cf939f2200b2a772
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1e2d6bcefb3e2288526ccad521bcdaa01e959c3d4f336f8ce67c239f0f4fe6d958e95a6b601f8abc76a6ab3bf259d3ef2e94557d7d85195c68987609be83c298
|
7
|
+
data.tar.gz: b74f70994b7828b6db115c242cc5e4daed1452b67d1d801620ee4f91e8566a593fdd10c5616eaf0e9911f4482fc65b692cb501473ae5e940710cda125700cb7c
|
data/.java-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
11
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ccrypto-java (0.1.0)
|
5
|
+
ccrypto
|
6
|
+
teLogger
|
7
|
+
toolrack
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activesupport (6.1.6)
|
13
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
14
|
+
i18n (>= 1.6, < 2)
|
15
|
+
minitest (>= 5.1)
|
16
|
+
tzinfo (~> 2.0)
|
17
|
+
zeitwerk (~> 2.3)
|
18
|
+
base58 (0.2.3)
|
19
|
+
ccrypto (0.1.0)
|
20
|
+
activesupport
|
21
|
+
teLogger
|
22
|
+
toolrack
|
23
|
+
concurrent-ruby (1.1.10)
|
24
|
+
devops_assist (0.2.0)
|
25
|
+
git_cli
|
26
|
+
git_cli_prompt
|
27
|
+
gvcs
|
28
|
+
teLogger
|
29
|
+
toolrack
|
30
|
+
tty-prompt
|
31
|
+
diff-lcs (1.5.0)
|
32
|
+
git_cli (0.9.0)
|
33
|
+
gvcs
|
34
|
+
ptools (~> 1.4.0)
|
35
|
+
tlogger
|
36
|
+
toolrack
|
37
|
+
git_cli_prompt (0.2.2)
|
38
|
+
tlogger
|
39
|
+
toolrack
|
40
|
+
tty-prompt
|
41
|
+
gvcs (0.1.0)
|
42
|
+
i18n (1.10.0)
|
43
|
+
concurrent-ruby (~> 1.0)
|
44
|
+
minitest (5.16.2)
|
45
|
+
pastel (0.8.0)
|
46
|
+
tty-color (~> 0.5)
|
47
|
+
ptools (1.4.2)
|
48
|
+
rake (13.0.6)
|
49
|
+
rspec (3.11.0)
|
50
|
+
rspec-core (~> 3.11.0)
|
51
|
+
rspec-expectations (~> 3.11.0)
|
52
|
+
rspec-mocks (~> 3.11.0)
|
53
|
+
rspec-core (3.11.0)
|
54
|
+
rspec-support (~> 3.11.0)
|
55
|
+
rspec-expectations (3.11.0)
|
56
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
57
|
+
rspec-support (~> 3.11.0)
|
58
|
+
rspec-mocks (3.11.0)
|
59
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
60
|
+
rspec-support (~> 3.11.0)
|
61
|
+
rspec-support (3.11.0)
|
62
|
+
teLogger (0.1.0)
|
63
|
+
tlogger (0.26.3)
|
64
|
+
toolrack (0.18.6)
|
65
|
+
base58
|
66
|
+
tlogger
|
67
|
+
tty-color (0.6.0)
|
68
|
+
tty-cursor (0.7.1)
|
69
|
+
tty-prompt (0.23.1)
|
70
|
+
pastel (~> 0.8)
|
71
|
+
tty-reader (~> 0.8)
|
72
|
+
tty-reader (0.9.0)
|
73
|
+
tty-cursor (~> 0.7)
|
74
|
+
tty-screen (~> 0.8)
|
75
|
+
wisper (~> 2.0)
|
76
|
+
tty-screen (0.8.1)
|
77
|
+
tzinfo (2.0.4)
|
78
|
+
concurrent-ruby (~> 1.0)
|
79
|
+
wisper (2.0.1)
|
80
|
+
zeitwerk (2.6.0)
|
81
|
+
|
82
|
+
PLATFORMS
|
83
|
+
universal-java-1.8
|
84
|
+
universal-java-11
|
85
|
+
x86_64-linux
|
86
|
+
|
87
|
+
DEPENDENCIES
|
88
|
+
ccrypto-java!
|
89
|
+
devops_assist
|
90
|
+
rake (~> 13.0)
|
91
|
+
rspec (~> 3.0)
|
92
|
+
|
93
|
+
BUNDLED WITH
|
94
|
+
2.3.6
|
data/README.md
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
# Ccrypto::Java
|
2
|
+
|
3
|
+
This project is the Java implementation for the [Ccrypto](https://github.com/cameronian/ccrypto) Common Crypto API.
|
4
|
+
|
5
|
+
This collection of API is meant to normalize the differences between runtimes.
|
6
|
+
|
7
|
+
Another notable implementation is [Ruby](https://github.com/cameronian/ccrypto-ruby).
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'ccrypto'
|
17
|
+
gem 'ccrypto-java'
|
18
|
+
```
|
19
|
+
|
20
|
+
Note that gem 'ccrypto' need to precede 'ccrypto-java'
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle install
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install ccrypto
|
29
|
+
$ gem install ccrypto-java
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
All operations is driven by the config object in [Ccrypto](https://github.com/cameronian/ccrypto).
|
34
|
+
|
35
|
+
The following are some common operations that can easily be initiated via the respective config object.
|
36
|
+
|
37
|
+
The value of [Ccrypto](https://github.com/cameronian/ccrypto) is the following codes should have same result when run with Ruby runtime using [ccrypto-ruby](https://github.com/cameronian/ccrypto-ruby) gem.
|
38
|
+
|
39
|
+
|
40
|
+
### Generate Secret Key
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
# Ccrypto::KeyConfig is the object for secret key generation
|
44
|
+
keyConfig = Ccrypto::KeyConfig.new
|
45
|
+
keyConfig.algo = :aes
|
46
|
+
keyConfig.keysize = 256
|
47
|
+
|
48
|
+
# instantiate the key generator by giving the class of Ccrypto::KeyConfig
|
49
|
+
# There are two ways to instantiate: with class and with instance.
|
50
|
+
# There are some engine which provides class methods which can be call when passing in the class.
|
51
|
+
# Engine that provides instance method requires to pass in the instance to instantiate.
|
52
|
+
|
53
|
+
keyGen = Ccrypto::AlgoFactory.engine(Ccrypto::KeyConfig)
|
54
|
+
key = keyGen.generate(keyConfig)
|
55
|
+
# key is now the AES key in 256 bits
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
### Encrypt & Decrypt with Secret Key
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
|
63
|
+
# generate key
|
64
|
+
keyConfig = Ccrypto::KeyConfig.new
|
65
|
+
keyConfig.algo = :aes
|
66
|
+
keyConfig.keysize = 256
|
67
|
+
|
68
|
+
keyGen = Ccrypto::AlgoFactory.engine(Ccrypto::KeyConfig)
|
69
|
+
key = keyGen.generate(keyConfig)
|
70
|
+
|
71
|
+
cipherConfig = Ccrypto::DirectCipherConfig.new({ algo: :aes, keysize: 256, mode: :gcm, padding: :pkcs5, key: key })
|
72
|
+
|
73
|
+
cipherConfig.cipherOps = :encrypt
|
74
|
+
|
75
|
+
# library shall generate missing component such as IV if required
|
76
|
+
# and store it back into the passed in config object
|
77
|
+
cipher = Ccrypto::AlgoFactory.engine(cipherConfig)
|
78
|
+
|
79
|
+
output = []
|
80
|
+
output << cipher.update(data)
|
81
|
+
output << cipher.update(data)
|
82
|
+
|
83
|
+
output << cipher.final
|
84
|
+
|
85
|
+
res = output.join
|
86
|
+
|
87
|
+
# Encryption done!
|
88
|
+
# res now is the encrypted data
|
89
|
+
|
90
|
+
```
|
91
|
+
|
92
|
+
|
93
|
+
### Decrypt with Secret Key
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
|
97
|
+
decConfig = Ccrypto::DirectCipherConfig.new({ algo: :aes, keysize: 256, mode: :gcm, padding: :pkcs5, key: key, iv: cipherConfig.iv })
|
98
|
+
|
99
|
+
# GCM mode has this additional tag
|
100
|
+
decConfig.auth_tag = cipherConfig.auth_tag
|
101
|
+
|
102
|
+
deCipher = Ccrypto::AlgoFactory.engine(decConfig)
|
103
|
+
|
104
|
+
dres = []
|
105
|
+
dres << deCipher.update(res)
|
106
|
+
dres << deCipher.final
|
107
|
+
|
108
|
+
# decryption done!
|
109
|
+
# dres is the decrypted output
|
110
|
+
|
111
|
+
```
|
112
|
+
|
113
|
+
|
114
|
+
### Digest / Hashing
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
|
118
|
+
digest = Ccrypto::AlgoFactory.engine(Ccrypto::DigestConfig)
|
119
|
+
|
120
|
+
digest.digest_update("data to be digested")
|
121
|
+
res = digest.digest_final
|
122
|
+
|
123
|
+
# res is the digest output in String
|
124
|
+
|
125
|
+
hres = digest.digest_final(:hex)
|
126
|
+
# hres is the digest output converted to hex
|
127
|
+
|
128
|
+
b64res = digest.digest_final(:b64)
|
129
|
+
# b64res is the digest output converted to Base64
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
|
134
|
+
### ECC key generation
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
# set the required curve name
|
138
|
+
eccConfig = CCrypto::ECCConfig.new("secp256k1")
|
139
|
+
ecc = Ccrypto::AlgoFactory.engine(Ccrypto::ECCConfig)
|
140
|
+
eccKey = ecc.generate_keypair
|
141
|
+
|
142
|
+
# eccKey shall be a ECC keypair
|
143
|
+
|
144
|
+
```
|
145
|
+
|
146
|
+
|
147
|
+
Refers to spec inside spec/ccrypto/xxx\_spec.rb
|
148
|
+
|
149
|
+
|
150
|
+
|
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 "ccrypto/java"
|
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
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/ccrypto/java/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ccrypto-java"
|
7
|
+
spec.version = Ccrypto::Java::VERSION
|
8
|
+
spec.authors = ["Ian"]
|
9
|
+
spec.email = ["cameronian0@protonmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Cryptographic API for Java runtime based on Bouncycastle library. Provider for Ccrypto library"
|
12
|
+
spec.description = "Refers to Ccrypto library for more info"
|
13
|
+
spec.homepage = "https://github.com/cameronian/ccrypto-java"
|
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 'ccrypto'
|
37
|
+
spec.add_development_dependency 'devops_assist'
|
38
|
+
|
39
|
+
# Uncomment to register a new dependency of your gem
|
40
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
41
|
+
|
42
|
+
# For more information and examples about making a new gem, checkout our
|
43
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
44
|
+
end
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,80 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module Ccrypto
|
4
|
+
module Java
|
5
|
+
module DataConversion
|
6
|
+
|
7
|
+
def to_hex(bin)
|
8
|
+
String.from_java_bytes(org.bouncycastle.util.encoders.Hex.encode(to_java_bytes(bin)))
|
9
|
+
end
|
10
|
+
# end to_hex
|
11
|
+
|
12
|
+
def from_hex(str)
|
13
|
+
org.bouncycastle.util.encoders.Hex.decode(str)
|
14
|
+
end
|
15
|
+
# end from_hex
|
16
|
+
|
17
|
+
def to_b64(bin)
|
18
|
+
String.from_java_bytes(java.util.Base64.encoder.encode(bin))
|
19
|
+
end
|
20
|
+
# end to_b64
|
21
|
+
#
|
22
|
+
|
23
|
+
def to_b64_mime(bin)
|
24
|
+
String.from_java_bytes(java.util.Base64.mime_encoder.encode(bin))
|
25
|
+
end
|
26
|
+
|
27
|
+
def from_b64(str)
|
28
|
+
java.util.Base64.decoder.decode(str)
|
29
|
+
end
|
30
|
+
# end from_b64
|
31
|
+
|
32
|
+
def to_str(bin)
|
33
|
+
if bin.is_a?(::Java::byte[])
|
34
|
+
String.from_java_bytes(bin)
|
35
|
+
else
|
36
|
+
bin
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_bin(str)
|
41
|
+
if str.nil?
|
42
|
+
::Java::byte[0].new
|
43
|
+
else
|
44
|
+
str.to_java.getBytes
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_java_bytes(val, encoding = nil)
|
49
|
+
case val
|
50
|
+
when String
|
51
|
+
val.to_java_bytes
|
52
|
+
when java.lang.String
|
53
|
+
if not_empty?(encoding)
|
54
|
+
val.getBytes(encoding)
|
55
|
+
else
|
56
|
+
val.getBytes
|
57
|
+
end
|
58
|
+
when Ccrypto::Java::ManagedMemoryBuffer
|
59
|
+
val.bytes
|
60
|
+
else
|
61
|
+
if val.respond_to?(:encoded)
|
62
|
+
val.encoded
|
63
|
+
else
|
64
|
+
val
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
#
|
70
|
+
# Add the methods to class level
|
71
|
+
#
|
72
|
+
def self.included(klass)
|
73
|
+
klass.class_eval <<-END
|
74
|
+
extend Ccrypto::Java::DataConversion
|
75
|
+
END
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,161 @@
|
|
1
|
+
|
2
|
+
require 'java'
|
3
|
+
|
4
|
+
require_relative 'asn1_object'
|
5
|
+
|
6
|
+
require_relative '../data_conversion'
|
7
|
+
|
8
|
+
module Ccrypto
|
9
|
+
module Java
|
10
|
+
|
11
|
+
class ASN1Engine
|
12
|
+
include TR::CondUtils
|
13
|
+
extend DataConversion
|
14
|
+
|
15
|
+
include TeLogger::TeLogHelper
|
16
|
+
|
17
|
+
teLogger_tag :j_asn1
|
18
|
+
|
19
|
+
def self.build(*args,&block)
|
20
|
+
type = args.first
|
21
|
+
val = args[1]
|
22
|
+
case type
|
23
|
+
when :oid
|
24
|
+
ASN1Object.new(type,org.bouncycastle.asn1.ASN1ObjectIdentifier.new(val))
|
25
|
+
|
26
|
+
when :seq
|
27
|
+
v = org.bouncycastle.asn1.ASN1EncodableVector.new
|
28
|
+
val.each do |vv|
|
29
|
+
if vv.is_a?(::Java::byte[])
|
30
|
+
v.add(org.bouncycastle.asn1.DERBitString.new(vv))
|
31
|
+
else
|
32
|
+
v.add(vv)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
ASN1Object.new(type,org.bouncycastle.asn1.DERSequence.new(v))
|
36
|
+
|
37
|
+
when :str, :utf8_str
|
38
|
+
ASN1Object.new(type,org.bouncycastle.asn1.DERUTF8String.new(val.to_s))
|
39
|
+
|
40
|
+
when :octet_str
|
41
|
+
if val.is_a?(Array)
|
42
|
+
val = val.pack("c*").to_java_bytes
|
43
|
+
elsif val.respond_to?(:to_bin)
|
44
|
+
val = val.to_bin
|
45
|
+
end
|
46
|
+
|
47
|
+
ASN1Object.new(type, val)
|
48
|
+
|
49
|
+
when :int
|
50
|
+
ASN1Object.new(type,org.bouncycastle.asn1.DERInteger.new(val))
|
51
|
+
#Java::OrgBouncycastleAsn1::DERInteger.new(val)
|
52
|
+
|
53
|
+
when :bin
|
54
|
+
ASN1Object.new(type,org.bouncycastle.asn1.DERBitString.new(to_java_bytes(val)))
|
55
|
+
#baos = java.io.ByteArrayOutputStream.new
|
56
|
+
#aos = org.bouncycastle.asn1.ASN1OutputStream.new(baos)
|
57
|
+
#aos.writeObject(org.bouncycastle.asn1.DERBitString.new(to_java_bytes(val)))
|
58
|
+
#aos.flush
|
59
|
+
#aos.close
|
60
|
+
|
61
|
+
#baos
|
62
|
+
|
63
|
+
when :date, :time, :generalize_time
|
64
|
+
if val.is_a?(Time)
|
65
|
+
val = val.to_java
|
66
|
+
elsif not val.is_a?(java.util.Date)
|
67
|
+
raise ASN1EngineException, "Unknown datetime objec to convert ['#{val.class}']"
|
68
|
+
end
|
69
|
+
|
70
|
+
#ASN1Object.new(type,org.bouncycastle.asn1.DERUTCTime.new(val))
|
71
|
+
ASN1Object.new(type,org.bouncycastle.asn1.DERGeneralizedTime.new(val))
|
72
|
+
|
73
|
+
else
|
74
|
+
raise ASN1EngineException, "Unknown ASN1 Object type '#{type.class}'"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
def self.to_value(*args, &block)
|
80
|
+
|
81
|
+
val = args.first
|
82
|
+
#teLogger.debug "Received #{val}"
|
83
|
+
|
84
|
+
expectedType = args[1]
|
85
|
+
|
86
|
+
if val.is_a?(::Java::byte[])
|
87
|
+
ais = org.bouncycastle.asn1.ASN1InputStream.new(val)
|
88
|
+
tag = ais.readObject
|
89
|
+
elsif val.is_a?(String)
|
90
|
+
ais = org.bouncycastle.asn1.ASN1InputStream.new(val.to_java_bytes)
|
91
|
+
tag = ais.readObject
|
92
|
+
else
|
93
|
+
tag = val
|
94
|
+
end
|
95
|
+
#raise ASN1EngineException, "Byte array is expected" if not val.is_a?(::Java::byte[])
|
96
|
+
|
97
|
+
#teLogger.debug "Tag : #{tag} / #{tag.class}"
|
98
|
+
|
99
|
+
case tag
|
100
|
+
when org.bouncycastle.asn1.ASN1ObjectIdentifier
|
101
|
+
tag.id
|
102
|
+
|
103
|
+
when org.bouncycastle.asn1.DEROctetString
|
104
|
+
tag.octets
|
105
|
+
|
106
|
+
when org.bouncycastle.asn1.ASN1Integer
|
107
|
+
#tag.int_value_exact
|
108
|
+
tag.value
|
109
|
+
|
110
|
+
when org.bouncycastle.asn1.DLSequence, org.bouncycastle.asn1.DERSequence
|
111
|
+
tag.to_a
|
112
|
+
|
113
|
+
when org.bouncycastle.asn1.DERUTF8String, org.bouncycastle.asn1.DERPrintableString, org.bouncycastle.asn1.DERVisibleString
|
114
|
+
tag.to_s
|
115
|
+
|
116
|
+
when org.bouncycastle.asn1.DERNumericString
|
117
|
+
"#{tag.to_i}"
|
118
|
+
|
119
|
+
when org.bouncycastle.asn1.DERUTCTime, org.bouncycastle.asn1.ASN1UTCTime, org.bouncycastle.asn1.ASN1GeneralizedTime
|
120
|
+
tag.date
|
121
|
+
|
122
|
+
when org.bouncycastle.asn1.DERBitString
|
123
|
+
tag.bytes
|
124
|
+
|
125
|
+
else
|
126
|
+
raise ASN1EngineException, "Unknown type '#{tag}'"
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
def self.asn1_length(*args, &block)
|
134
|
+
val = args.first
|
135
|
+
if not_empty?(val)
|
136
|
+
if val.is_a?(ASN1Object)
|
137
|
+
v = val.native_asn1
|
138
|
+
elsif val.is_a?(String)
|
139
|
+
v = val.to_java_bytes
|
140
|
+
else
|
141
|
+
v = val
|
142
|
+
end
|
143
|
+
|
144
|
+
totalLen = 0
|
145
|
+
|
146
|
+
ais = org.bouncycastle.asn1.ASN1InputStream.new(v)
|
147
|
+
res = ais.readObject
|
148
|
+
totalLen = res.encoded.length
|
149
|
+
|
150
|
+
totalLen
|
151
|
+
|
152
|
+
else
|
153
|
+
0
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
161
|
+
end
|