ccrypto-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 +12 -0
- data/Gemfile.lock +64 -0
- data/README.md +149 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/ccrypto-ruby.gemspec +45 -0
- data/lib/ccrypto/provider.rb +175 -0
- data/lib/ccrypto/ruby/data_conversion.rb +68 -0
- data/lib/ccrypto/ruby/engines/asn1_engine.rb +110 -0
- data/lib/ccrypto/ruby/engines/asn1_object.rb +19 -0
- data/lib/ccrypto/ruby/engines/cipher_engine.rb +170 -0
- data/lib/ccrypto/ruby/engines/compression_engine.rb +61 -0
- data/lib/ccrypto/ruby/engines/data_conversion_engine.rb +9 -0
- data/lib/ccrypto/ruby/engines/decompression_engine.rb +70 -0
- data/lib/ccrypto/ruby/engines/digest_engine.rb +127 -0
- data/lib/ccrypto/ruby/engines/ecc_engine.rb +218 -0
- data/lib/ccrypto/ruby/engines/hkdf_engine.rb +54 -0
- data/lib/ccrypto/ruby/engines/hmac_engine.rb +53 -0
- data/lib/ccrypto/ruby/engines/pbkdf2_engine.rb +69 -0
- data/lib/ccrypto/ruby/engines/pkcs7_engine.rb +179 -0
- data/lib/ccrypto/ruby/engines/rsa_engine.rb +300 -0
- data/lib/ccrypto/ruby/engines/scrypt_engine.rb +34 -0
- data/lib/ccrypto/ruby/engines/secret_key_engine.rb +18 -0
- data/lib/ccrypto/ruby/engines/secret_sharing_engine.rb +331 -0
- data/lib/ccrypto/ruby/engines/secure_random_engine.rb +34 -0
- data/lib/ccrypto/ruby/engines/x509_engine.rb +213 -0
- data/lib/ccrypto/ruby/ext/secret_key.rb +24 -0
- data/lib/ccrypto/ruby/ext/x509_cert.rb +24 -0
- data/lib/ccrypto/ruby/keybundle_store/pem_store.rb +73 -0
- data/lib/ccrypto/ruby/keybundle_store/pkcs12.rb +111 -0
- data/lib/ccrypto/ruby/utils/comparator.rb +15 -0
- data/lib/ccrypto/ruby/utils/memory_buffer.rb +63 -0
- data/lib/ccrypto/ruby/utils/native_helper.rb +17 -0
- data/lib/ccrypto/ruby/version.rb +7 -0
- data/lib/ccrypto/ruby.rb +25 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 19d85394d769795021b692546a1b3373f207245170fdb3643fe0cae3f61de767
|
4
|
+
data.tar.gz: 8801b56e4104f29d75c149b4cde284e9a472f84457a71adf362fa2d8cefe3549
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4c638760828f593ae2821d2c7d2043c0edd7e3774a1e1ef65c5d8f9b050deb451e4a73548d89549568ddc41c160d3de1256e0bb4ff22ca8d6989999c50d986e2
|
7
|
+
data.tar.gz: 91ac3b9c37c7f4fc39ba7eb1e727017e30eec7a2f809b74390a22634f35c5f4f813fe0d072a0099ae4073721586abbde9e01fac6fbf54965c077c6722aa97af8
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
GIT
|
2
|
+
remote: ccrypto
|
3
|
+
revision: 0d1c446d77fb781857f5ac087de9326bc3dafca9
|
4
|
+
branch: main
|
5
|
+
specs:
|
6
|
+
ccrypto (0.1.0)
|
7
|
+
activesupport
|
8
|
+
teLogger
|
9
|
+
toolrack
|
10
|
+
|
11
|
+
PATH
|
12
|
+
remote: .
|
13
|
+
specs:
|
14
|
+
ccrypto-ruby (0.1.0)
|
15
|
+
teLogger
|
16
|
+
toolrack
|
17
|
+
|
18
|
+
GEM
|
19
|
+
remote: https://rubygems.org/
|
20
|
+
specs:
|
21
|
+
activesupport (7.0.3.1)
|
22
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
23
|
+
i18n (>= 1.6, < 2)
|
24
|
+
minitest (>= 5.1)
|
25
|
+
tzinfo (~> 2.0)
|
26
|
+
base58 (0.2.3)
|
27
|
+
concurrent-ruby (1.1.10)
|
28
|
+
diff-lcs (1.5.0)
|
29
|
+
i18n (1.12.0)
|
30
|
+
concurrent-ruby (~> 1.0)
|
31
|
+
minitest (5.16.2)
|
32
|
+
rake (13.0.6)
|
33
|
+
rspec (3.11.0)
|
34
|
+
rspec-core (~> 3.11.0)
|
35
|
+
rspec-expectations (~> 3.11.0)
|
36
|
+
rspec-mocks (~> 3.11.0)
|
37
|
+
rspec-core (3.11.0)
|
38
|
+
rspec-support (~> 3.11.0)
|
39
|
+
rspec-expectations (3.11.0)
|
40
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
+
rspec-support (~> 3.11.0)
|
42
|
+
rspec-mocks (3.11.1)
|
43
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
44
|
+
rspec-support (~> 3.11.0)
|
45
|
+
rspec-support (3.11.0)
|
46
|
+
teLogger (0.1.0)
|
47
|
+
tlogger (0.26.3)
|
48
|
+
toolrack (0.18.5)
|
49
|
+
base58
|
50
|
+
tlogger
|
51
|
+
tzinfo (2.0.5)
|
52
|
+
concurrent-ruby (~> 1.0)
|
53
|
+
|
54
|
+
PLATFORMS
|
55
|
+
x86_64-linux
|
56
|
+
|
57
|
+
DEPENDENCIES
|
58
|
+
ccrypto!
|
59
|
+
ccrypto-ruby!
|
60
|
+
rake (~> 13.0)
|
61
|
+
rspec (~> 3.0)
|
62
|
+
|
63
|
+
BUNDLED WITH
|
64
|
+
2.2.28
|
data/README.md
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
# Ccrypto::Ruby
|
2
|
+
|
3
|
+
This project is the Ruby 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 [Java](https://github.com/cameronian/ccrypto-java).
|
8
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'ccrypto'
|
16
|
+
gem 'ccrypto-ruby'
|
17
|
+
```
|
18
|
+
|
19
|
+
Note that gem 'ccrypto' need to precede 'ccrypto-ruby'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle install
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install ccrypto
|
28
|
+
$ gem install ccrypto-ruby
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
All operations is driven by the config object in [Ccrypto](https://github.com/cameronian/ccrypto).
|
33
|
+
|
34
|
+
The following are some common operations that can easily be initiated via the respective config object.
|
35
|
+
|
36
|
+
The value of [Ccrypto](https://github.com/cameronian/ccrypto) is the following codes should have same result when run with Java runtime using [ccrypto-java](https://github.com/cameronian/ccrypto-java) gem.
|
37
|
+
|
38
|
+
|
39
|
+
### Generate Secret Key
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# Ccrypto::KeyConfig is the object for secret key generation
|
43
|
+
keyConfig = Ccrypto::KeyConfig.new
|
44
|
+
keyConfig.algo = :aes
|
45
|
+
keyConfig.keysize = 256
|
46
|
+
|
47
|
+
# instantiate the key generator by giving the class of Ccrypto::KeyConfig
|
48
|
+
# There are two ways to instantiate: with class and with instance.
|
49
|
+
# There are some engine which provides class methods which can be call when passing in the class.
|
50
|
+
# Engine that provides instance method requires to pass in the instance to instantiate.
|
51
|
+
|
52
|
+
keyGen = Ccrypto::AlgoFactory.engine(Ccrypto::KeyConfig)
|
53
|
+
key = keyGen.generate(keyConfig)
|
54
|
+
# key is now the AES key in 256 bits
|
55
|
+
|
56
|
+
```
|
57
|
+
|
58
|
+
### Encrypt & Decrypt with Secret Key
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
|
62
|
+
# generate key
|
63
|
+
keyConfig = Ccrypto::KeyConfig.new
|
64
|
+
keyConfig.algo = :aes
|
65
|
+
keyConfig.keysize = 256
|
66
|
+
|
67
|
+
keyGen = Ccrypto::AlgoFactory.engine(Ccrypto::KeyConfig)
|
68
|
+
key = keyGen.generate(keyConfig)
|
69
|
+
|
70
|
+
cipherConfig = Ccrypto::DirectCipherConfig.new({ algo: :aes, keysize: 256, mode: :gcm, padding: :pkcs5, key: key })
|
71
|
+
|
72
|
+
cipherConfig.cipherOps = :encrypt
|
73
|
+
|
74
|
+
# library shall generate missing component such as IV if required
|
75
|
+
# and store it back into the passed in config object
|
76
|
+
cipher = Ccrypto::AlgoFactory.engine(cipherConfig)
|
77
|
+
|
78
|
+
output = []
|
79
|
+
output << cipher.update(data)
|
80
|
+
output << cipher.update(data)
|
81
|
+
|
82
|
+
output << cipher.final
|
83
|
+
|
84
|
+
res = output.join
|
85
|
+
|
86
|
+
# Encryption done!
|
87
|
+
# res now is the encrypted data
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
### Decrypt with Secret Key
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
|
96
|
+
decConfig = Ccrypto::DirectCipherConfig.new({ algo: :aes, keysize: 256, mode: :gcm, padding: :pkcs5, key: key, iv: cipherConfig.iv })
|
97
|
+
|
98
|
+
# GCM mode has this additional tag
|
99
|
+
decConfig.auth_tag = cipherConfig.auth_tag
|
100
|
+
|
101
|
+
deCipher = Ccrypto::AlgoFactory.engine(decConfig)
|
102
|
+
|
103
|
+
dres = []
|
104
|
+
dres << deCipher.update(res)
|
105
|
+
dres << deCipher.final
|
106
|
+
|
107
|
+
# decryption done!
|
108
|
+
# dres is the decrypted output
|
109
|
+
|
110
|
+
```
|
111
|
+
|
112
|
+
|
113
|
+
### Digest / Hashing
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
|
117
|
+
digest = Ccrypto::AlgoFactory.engine(Ccrypto::DigestConfig)
|
118
|
+
|
119
|
+
digest.digest_update("data to be digested")
|
120
|
+
res = digest.digest_final
|
121
|
+
|
122
|
+
# res is the digest output in String
|
123
|
+
|
124
|
+
hres = digest.digest_final(:hex)
|
125
|
+
# hres is the digest output converted to hex
|
126
|
+
|
127
|
+
b64res = digest.digest_final(:b64)
|
128
|
+
# b64res is the digest output converted to Base64
|
129
|
+
|
130
|
+
```
|
131
|
+
|
132
|
+
|
133
|
+
### ECC key generation
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
# set the required curve name
|
137
|
+
eccConfig = CCrypto::ECCConfig.new("secp256k1")
|
138
|
+
ecc = Ccrypto::AlgoFactory.engine(Ccrypto::ECCConfig)
|
139
|
+
eccKey = ecc.generate_keypair
|
140
|
+
|
141
|
+
# eccKey shall be a ECC keypair
|
142
|
+
|
143
|
+
```
|
144
|
+
|
145
|
+
|
146
|
+
Refers to spec inside spec/ccrypto/xxx\_spec.rb
|
147
|
+
|
148
|
+
|
149
|
+
|
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/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
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/ccrypto/ruby/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ccrypto-ruby"
|
7
|
+
spec.version = Ccrypto::Ruby::VERSION
|
8
|
+
spec.authors = ["Ian"]
|
9
|
+
spec.email = ["cameronian0@protonmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Ccrypto API provider for Ruby runtime"
|
12
|
+
spec.description = "Refers Ccrypto library for further info. This is the Ruby implementation of the core cryptographic API"
|
13
|
+
spec.homepage = "https://github.com/cameronian/ccrypto-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 'ccrypto'
|
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,175 @@
|
|
1
|
+
|
2
|
+
require_relative 'ruby/engines/ecc_engine'
|
3
|
+
require_relative 'ruby/engines/digest_engine'
|
4
|
+
require_relative 'ruby/engines/x509_engine'
|
5
|
+
|
6
|
+
require_relative 'ruby/engines/scrypt_engine'
|
7
|
+
require_relative 'ruby/engines/hkdf_engine'
|
8
|
+
require_relative 'ruby/engines/pbkdf2_engine'
|
9
|
+
|
10
|
+
require_relative 'ruby/engines/secure_random_engine'
|
11
|
+
require_relative 'ruby/engines/cipher_engine'
|
12
|
+
|
13
|
+
require_relative 'ruby/utils/comparator'
|
14
|
+
require_relative 'ruby/utils/memory_buffer'
|
15
|
+
require_relative 'ruby/utils/native_helper'
|
16
|
+
|
17
|
+
require_relative 'ruby/engines/asn1_engine'
|
18
|
+
require_relative 'ruby/engines/compression_engine'
|
19
|
+
require_relative 'ruby/engines/decompression_engine'
|
20
|
+
|
21
|
+
require_relative 'ruby/engines/secret_key_engine'
|
22
|
+
require_relative 'ruby/engines/hmac_engine'
|
23
|
+
|
24
|
+
require_relative 'ruby/engines/data_conversion_engine'
|
25
|
+
|
26
|
+
require_relative 'ruby/engines/secret_sharing_engine'
|
27
|
+
|
28
|
+
require_relative 'ruby/engines/pkcs7_engine'
|
29
|
+
|
30
|
+
require_relative 'ruby/engines/rsa_engine'
|
31
|
+
|
32
|
+
module Ccrypto
|
33
|
+
module Ruby
|
34
|
+
class Provider
|
35
|
+
|
36
|
+
def self.provider_name
|
37
|
+
"ruby"
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.algo_instance(*args, &block)
|
41
|
+
config = args.first
|
42
|
+
|
43
|
+
if config.is_a?(Class) or config.is_a?(Module)
|
44
|
+
if config == Ccrypto::ECCConfig
|
45
|
+
ECCEngine
|
46
|
+
elsif config == Ccrypto::RSAConfig
|
47
|
+
RSAEngine
|
48
|
+
elsif config == Ccrypto::ECCKeyBundle
|
49
|
+
ECCKeyBundle
|
50
|
+
elsif config == Ccrypto::RSAKeyBundle
|
51
|
+
RSAKeyBundle
|
52
|
+
elsif config == Ccrypto::DigestConfig
|
53
|
+
DigestEngine
|
54
|
+
elsif config == Ccrypto::SecureRandomConfig
|
55
|
+
SecureRandomEngine
|
56
|
+
elsif config == Ccrypto::CipherConfig
|
57
|
+
CipherEngine
|
58
|
+
elsif config == Ccrypto::ECCPublicKey
|
59
|
+
Ccrypto::Ruby::ECCPublicKey
|
60
|
+
elsif config == Ccrypto::KeyConfig
|
61
|
+
Ccrypto::Ruby::SecretKeyEngine
|
62
|
+
elsif config == Ccrypto::SecretSharingConfig
|
63
|
+
SecretSharingEngine
|
64
|
+
else
|
65
|
+
raise CcryptoProviderException, "Config class '#{config}' is not supported for provider '#{self.provider_name}'"
|
66
|
+
end
|
67
|
+
else
|
68
|
+
case config
|
69
|
+
when Ccrypto::ECCConfig
|
70
|
+
ECCEngine.new(*args, &block)
|
71
|
+
when Ccrypto::RSAConfig
|
72
|
+
RSAEngine.new(*args, &block)
|
73
|
+
when Ccrypto::DigestConfig
|
74
|
+
DigestEngine.instance(*args, &block)
|
75
|
+
when Ccrypto::X509::CertProfile
|
76
|
+
X509Engine.new(*args,&block)
|
77
|
+
when Ccrypto::ScryptConfig
|
78
|
+
ScryptEngine.new(*args,&block)
|
79
|
+
when Ccrypto::HKDFConfig
|
80
|
+
HKDFEngine.new(*args, &block)
|
81
|
+
when Ccrypto::PBKDF2Config
|
82
|
+
PBKDF2Engine.new(*args, &block)
|
83
|
+
when Ccrypto::CipherConfig
|
84
|
+
CipherEngine.new(*args, &block)
|
85
|
+
when Ccrypto::HMACConfig
|
86
|
+
HMACEngine.new(*args, &block)
|
87
|
+
when Ccrypto::SecretSharingConfig
|
88
|
+
SecretSharingEngine.new(*args,&block)
|
89
|
+
when Ccrypto::PKCS7Config
|
90
|
+
PKCS7Engine.new(*args, &block)
|
91
|
+
else
|
92
|
+
raise CcryptoProviderException, "Config instance '#{config}' is not supported for provider '#{self.provider_name}'"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
#case config
|
97
|
+
#when Ccrypto::ECCConfig.class
|
98
|
+
# puts "ecc config class"
|
99
|
+
# ECCEngine
|
100
|
+
#when Ccrypto::ECCConfig
|
101
|
+
# puts "ecc config"
|
102
|
+
# ECCEngine.new(*args, &block)
|
103
|
+
#when Ccrypto::DigestConfig.class
|
104
|
+
# puts "digest config class"
|
105
|
+
# DigestEngine
|
106
|
+
#when Ccrypto::DigestConfig
|
107
|
+
# puts "digest config"
|
108
|
+
# DigestEngine.instance(*args,&block)
|
109
|
+
#else
|
110
|
+
# raise CcryptoProviderException, "Config '#{config}' is not supported for provider '#{self.provider_name}'"
|
111
|
+
#end
|
112
|
+
|
113
|
+
#case algo
|
114
|
+
#when :ecc
|
115
|
+
# ECCEngine
|
116
|
+
#when :x509
|
117
|
+
# if args.length > 1
|
118
|
+
# X509Engine.new(*args[1..-1])
|
119
|
+
# else
|
120
|
+
# X509Engine
|
121
|
+
# end
|
122
|
+
#when :scrypt
|
123
|
+
# ScryptEngine.new
|
124
|
+
#when :secure_random
|
125
|
+
# SecureRandomEngine
|
126
|
+
#else
|
127
|
+
# if DigestEngine.is_supported?(algo)
|
128
|
+
# DigestEngine.instance(algo)
|
129
|
+
# elsif CipherEngine.is_supported_cipher?(algo.to_s)
|
130
|
+
# if args.length > 1 or args[0].is_a?(String)
|
131
|
+
# CipherEngine.new(*args)
|
132
|
+
# else
|
133
|
+
# CipherEngine
|
134
|
+
# end
|
135
|
+
# else
|
136
|
+
# raise CcryptoProviderException, "Algo '#{algo}' is not supported for provider '#{self.provider_name}'"
|
137
|
+
# end
|
138
|
+
#end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
def self.asn1_engine(*args, &block)
|
143
|
+
ASN1Engine
|
144
|
+
end
|
145
|
+
|
146
|
+
def self.util_instance(*args, &block)
|
147
|
+
type = args.first
|
148
|
+
case type
|
149
|
+
when :comparator, :compare
|
150
|
+
ComparatorUtil
|
151
|
+
when :data_conversion, :converter, :data_converter
|
152
|
+
DataConversionEngine
|
153
|
+
|
154
|
+
when :memory_buffer, :membuf, :buffer, :mem
|
155
|
+
MemoryBuffer
|
156
|
+
|
157
|
+
when :compression, :compressor
|
158
|
+
Compression.new(*(args[1..-1]), &block)
|
159
|
+
|
160
|
+
when :decompression
|
161
|
+
Decompression.new(*(args[1..-1]), &block)
|
162
|
+
|
163
|
+
when :native_helper
|
164
|
+
NativeHelper
|
165
|
+
|
166
|
+
else
|
167
|
+
raise CcryptoProviderException, "Util type #{type} is not supported by provider #{self.provider_name}"
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
module Ccrypto
|
4
|
+
module Ruby
|
5
|
+
module DataConversion
|
6
|
+
|
7
|
+
def to_hex(bin, opts = { })
|
8
|
+
if not bin.nil?
|
9
|
+
bin.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
|
10
|
+
else
|
11
|
+
bin
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def from_hex(str, opts = { })
|
16
|
+
if not str.nil?
|
17
|
+
str.scan(/../).map { |x| x.hex.chr }.join
|
18
|
+
else
|
19
|
+
str
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_b64(bin, opts = { })
|
24
|
+
if not bin.nil?
|
25
|
+
if not (opts[:strict].nil? and opts[:strict] == true)
|
26
|
+
Base64.encode64(bin)
|
27
|
+
else
|
28
|
+
Base64.strict_encode64(bin)
|
29
|
+
end
|
30
|
+
else
|
31
|
+
bin
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def from_b64(str, opts = { })
|
36
|
+
if not str.nil?
|
37
|
+
if not (opts[:strict].nil? and opts[:strict] == true)
|
38
|
+
Base64.decode64(str)
|
39
|
+
else
|
40
|
+
Base64.strict_decode64(str)
|
41
|
+
end
|
42
|
+
else
|
43
|
+
str
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_int_array(str, opts = { })
|
48
|
+
if not str.nil?
|
49
|
+
str.each_char.map { |c| c.ord }
|
50
|
+
else
|
51
|
+
str
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
#
|
56
|
+
# Add the methods to class level
|
57
|
+
#
|
58
|
+
def self.included(klass)
|
59
|
+
klass.class_eval <<-END
|
60
|
+
extend Ccrypto::Ruby::DataConversion
|
61
|
+
END
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
# end module Converter
|
66
|
+
#
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
|
2
|
+
require_relative 'asn1_object'
|
3
|
+
|
4
|
+
module Ccrypto
|
5
|
+
module Ruby
|
6
|
+
|
7
|
+
class ASN1Engine
|
8
|
+
include TR::CondUtils
|
9
|
+
|
10
|
+
def self.build(*args, &block)
|
11
|
+
type = args.first
|
12
|
+
val = args[1]
|
13
|
+
case type
|
14
|
+
when :oid
|
15
|
+
ASN1Object.new(type, OpenSSL::ASN1::ObjectId.new(val))
|
16
|
+
when :seq
|
17
|
+
ASN1Object.new(type, OpenSSL::ASN1::Sequence.new(val))
|
18
|
+
when :str, :utf8_str
|
19
|
+
ASN1Object.new(type, OpenSSL::ASN1::UTF8String.new(val))
|
20
|
+
when :octet_str
|
21
|
+
ASN1Object.new(type, OpenSSL::ASN1::OctetString.new(val))
|
22
|
+
when :int
|
23
|
+
ASN1Object.new(type, OpenSSL::ASN1::Integer.new(val))
|
24
|
+
when :bin
|
25
|
+
ASN1Object.new(type, OpenSSL::ASN1::BitString.new(val))
|
26
|
+
when :date, :time, :generalize_time
|
27
|
+
ASN1Object.new(type, OpenSSL::ASN1::GeneralizedTime.new(val))
|
28
|
+
else
|
29
|
+
raise ASN1EngineException, "Unknown ASN1 object type '#{type.class}'"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.to_value(*args, &block)
|
34
|
+
val = args.first
|
35
|
+
expectedType = args[1]
|
36
|
+
v = OpenSSL::ASN1.decode(val).value
|
37
|
+
if not_empty?(expectedType)
|
38
|
+
case expectedType
|
39
|
+
when :int
|
40
|
+
if v.is_a?(OpenSSL::BN)
|
41
|
+
v.to_i
|
42
|
+
else
|
43
|
+
v
|
44
|
+
end
|
45
|
+
else
|
46
|
+
v
|
47
|
+
end
|
48
|
+
else
|
49
|
+
if v.is_a?(OpenSSL::BN)
|
50
|
+
v.to_i
|
51
|
+
else
|
52
|
+
v
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.asn1_length(*args, &block)
|
58
|
+
|
59
|
+
val = args.first
|
60
|
+
if not_empty?(val)
|
61
|
+
|
62
|
+
v = val
|
63
|
+
if v.is_a?(ASN1Object)
|
64
|
+
v = v.native_asn1
|
65
|
+
end
|
66
|
+
|
67
|
+
totalLen = 0
|
68
|
+
begin
|
69
|
+
OpenSSL::ASN1.traverse(v) do |depth,offset,headerLen,length,constructed,tagClass,tag|
|
70
|
+
totalLen = headerLen+length
|
71
|
+
break
|
72
|
+
end
|
73
|
+
rescue StandardError => ex
|
74
|
+
rescue OpenSSL::ASN1::ASN1Error => ex
|
75
|
+
raise ASN1EngineException, ex
|
76
|
+
end
|
77
|
+
|
78
|
+
totalLen
|
79
|
+
|
80
|
+
else
|
81
|
+
0
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
def self.openssl_to_asn1object(oasn1)
|
88
|
+
case oasn1
|
89
|
+
when OpenSSL::ASN1::ObjectId
|
90
|
+
type = :oid
|
91
|
+
when OpenSSL::ASN1::Sequence
|
92
|
+
type = :seq
|
93
|
+
when OpenSSL::ASN1::UTF8String
|
94
|
+
type = :str
|
95
|
+
when OpenSSL::ASN1::OctetString
|
96
|
+
type = :octet_str
|
97
|
+
when OpenSSL::ASN1::Integer
|
98
|
+
type = :int
|
99
|
+
when OpenSSL::ASN1::BitString
|
100
|
+
type = :bin
|
101
|
+
when OpenSSL::ASN1::GeneralizedTime
|
102
|
+
type = :time
|
103
|
+
end
|
104
|
+
|
105
|
+
ASN1Object.new(:oid, oasn1)
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module Ccrypto
|
4
|
+
module Ruby
|
5
|
+
class ASN1Object < Ccrypto::ASN1Object
|
6
|
+
|
7
|
+
def to_bin
|
8
|
+
case @asn1
|
9
|
+
when OpenSSL::ASN1::Sequence
|
10
|
+
seq = OpenSSL::ASN1::Sequence.new(@asn1.map { |e| e.to_der })
|
11
|
+
seq.to_der
|
12
|
+
else
|
13
|
+
@asn1.to_der
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|