robust_client_socket 0.5.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openssl'
4
+ require 'base64'
5
+
6
+ module RobustClientSocket
7
+ # A named asymmetric token scheme. Built-ins are X25519 and X448.
8
+ # Register your own with CipherRegistry.register(:name, scheme).
9
+ # A scheme answers seal(plaintext, public_key_pem) and read_public_key(pem).
10
+ module CipherRegistry
11
+ UnknownCipher = Class.new(SecurityError)
12
+
13
+ @schemes = {}
14
+
15
+ class << self
16
+ def register(name, scheme)
17
+ raise ArgumentError, 'cipher name must be a Symbol' unless name.is_a?(Symbol)
18
+ raise ArgumentError, 'cipher must answer seal and read_public_key' unless scheme.respond_to?(:seal) && scheme.respond_to?(:read_public_key)
19
+
20
+ @schemes[name] = scheme
21
+ name
22
+ end
23
+
24
+ def fetch(name)
25
+ @schemes.fetch(name.to_sym) { raise UnknownCipher, "Unknown cipher: #{name}" }
26
+ end
27
+
28
+ def registered?(name)
29
+ @schemes.key?(name.to_sym)
30
+ end
31
+ end
32
+ end
33
+
34
+ module EcdhCipher
35
+ IV_LENGTH = 12
36
+ TAG_LENGTH = 16
37
+ KEY_LENGTH = 32
38
+
39
+ def self.build(algorithm, ephemeral_der_length)
40
+ Module.new do
41
+ extend self
42
+
43
+ const_set(:ALGORITHM, algorithm)
44
+ const_set(:EPHEMERAL_DER_LENGTH, ephemeral_der_length)
45
+ const_set(:INFO, "robust-socket-token-v1-#{algorithm.downcase}")
46
+
47
+ def seal(plaintext, public_key_pem)
48
+ recipient = read_public_key(public_key_pem)
49
+ ephemeral = OpenSSL::PKey.generate_key(self::ALGORITHM)
50
+ blob = ephemeral.public_to_der.b + gcm_seal(derive_key(ephemeral.derive(recipient)), plaintext)
51
+
52
+ ::Base64.strict_encode64(blob)
53
+ rescue OpenSSL::PKey::PKeyError, OpenSSL::Cipher::CipherError => e
54
+ raise SecurityError, "Encryption failed: #{e.message}"
55
+ end
56
+
57
+ def read_public_key(pem)
58
+ key = OpenSSL::PKey.read(pem)
59
+ raise SecurityError, 'Invalid public key' unless public_key?(key)
60
+
61
+ key
62
+ rescue OpenSSL::PKey::PKeyError => e
63
+ raise SecurityError, "Invalid public key: #{e.message}"
64
+ end
65
+
66
+ def public_key?(key)
67
+ key.oid == self::ALGORITHM && !holds_private_key?(key)
68
+ end
69
+
70
+ def holds_private_key?(key)
71
+ !key.raw_private_key.nil?
72
+ rescue OpenSSL::PKey::PKeyError
73
+ false
74
+ end
75
+
76
+ def derive_key(shared_secret)
77
+ OpenSSL::KDF.hkdf(shared_secret, salt: '', info: self::INFO, length: EcdhCipher::KEY_LENGTH, hash: 'SHA256')
78
+ end
79
+
80
+ def gcm_seal(key, plaintext)
81
+ cipher = OpenSSL::Cipher.new('aes-256-gcm')
82
+ cipher.encrypt
83
+ cipher.key = key
84
+ iv = cipher.random_iv
85
+ cipher.auth_data = self::INFO
86
+ ciphertext = cipher.update(plaintext) + cipher.final
87
+
88
+ iv + cipher.auth_tag + ciphertext
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ X25519 = EcdhCipher.build('X25519', 44)
95
+ X448 = EcdhCipher.build('X448', 68)
96
+
97
+ CipherRegistry.register(:x25519, X25519)
98
+ CipherRegistry.register(:x448, X448)
99
+
100
+ # Default cipher. TokenCipher.seal keeps the previous call shape.
101
+ module TokenCipher
102
+ module_function
103
+
104
+ def seal(plaintext, public_key_pem, cipher: :x25519)
105
+ CipherRegistry.fetch(cipher).seal(plaintext, public_key_pem)
106
+ end
107
+
108
+ def read_public_key(pem, cipher: :x25519)
109
+ CipherRegistry.fetch(cipher).read_public_key(pem)
110
+ end
111
+ end
112
+ end
@@ -16,6 +16,7 @@ module RobustClientSocket
16
16
  require 'base64'
17
17
  require 'oj'
18
18
 
19
+ require_relative 'robust_client_socket/token_cipher'
19
20
  require_relative 'robust_client_socket/http/httparty_overrides'
20
21
  require_relative 'robust_client_socket/http/helpers'
21
22
  require_relative 'robust_client_socket/http/client'
data/lib/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RobustClientSocket
4
- VERSION = '0.5.2'
4
+ VERSION = '0.7.0'
5
5
  end
@@ -19,7 +19,8 @@ Gem::Specification.new do |spec|
19
19
  f.start_with?(*%w[features/ .git .circleci Gemfile])
20
20
  end
21
21
  end
22
- spec.require_paths = %w[lib config]
22
+ spec.require_paths = %w[lib]
23
+ spec.add_dependency 'base64'
23
24
  spec.add_dependency 'oj'
24
25
  spec.add_dependency 'httparty'
25
26
  spec.add_dependency 'rspec'
@@ -9,7 +9,7 @@ RSpec.describe RobustClientSocket::HTTP::Client do
9
9
  let(:credentials) do
10
10
  {
11
11
  base_uri: 'https://example.com',
12
- public_key: OpenSSL::PKey::RSA.new(2048).public_key.to_s
12
+ public_key: OpenSSL::PKey.generate_key('X25519').public_to_pem
13
13
  }
14
14
  end
15
15
  let(:client_name) { 'test_client' }
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
2
  require './lib/robust_client_socket/configuration.rb'
3
+ require './lib/robust_client_socket/token_cipher.rb'
3
4
 
4
5
  RSpec.describe RobustClientSocket::Configuration do
5
6
  let(:dummy_class) { Class.new { extend RobustClientSocket::Configuration } }
@@ -7,7 +8,7 @@ RSpec.describe RobustClientSocket::Configuration do
7
8
  before { allow(dummy_class).to receive(:correct_configuration?).and_return(true) }
8
9
 
9
10
  describe '#configure' do
10
- let(:public_key) { OpenSSL::PKey::RSA.generate(2048).public_key.to_pem }
11
+ let(:public_key) { OpenSSL::PKey.generate_key('X25519').public_to_pem }
11
12
 
12
13
  it 'yields the configuration object to the block' do
13
14
  dummy_class.configure do |config|
@@ -25,8 +26,8 @@ RSpec.describe RobustClientSocket::Configuration do
25
26
  expect(dummy_class.configured?).to eq(true)
26
27
  end
27
28
 
28
- context 'with invalid key size' do
29
- let(:small_key) { OpenSSL::PKey::RSA.generate(1024).public_key.to_pem }
29
+ context 'with a non-X25519 key' do
30
+ let(:small_key) { OpenSSL::PKey::RSA.generate(2048).public_key.to_pem }
30
31
 
31
32
  it 'raises SecurityError' do
32
33
  expect {
@@ -34,13 +35,13 @@ RSpec.describe RobustClientSocket::Configuration do
34
35
  config.client_name = 'test'
35
36
  config.service = { base_uri: 'https://example.com', public_key: small_key }
36
37
  end
37
- }.to raise_error(SecurityError, /below minimum/)
38
+ }.to raise_error(SecurityError, /Invalid public key/)
38
39
  end
39
40
  end
40
41
  end
41
42
 
42
43
  describe '#configured?' do
43
- let(:public_key) { OpenSSL::PKey::RSA.generate(2048).public_key.to_pem }
44
+ let(:public_key) { OpenSSL::PKey.generate_key('X25519').public_to_pem }
44
45
 
45
46
  it 'returns false if not configured' do
46
47
  expect(dummy_class.configured?).to eq(false)
@@ -56,7 +57,7 @@ RSpec.describe RobustClientSocket::Configuration do
56
57
  end
57
58
 
58
59
  describe '#correct_configuration?' do
59
- let(:public_key) { OpenSSL::PKey::RSA.generate(2048).public_key.to_pem }
60
+ let(:public_key) { OpenSSL::PKey.generate_key('X25519').public_to_pem }
60
61
 
61
62
  before { allow(dummy_class).to receive(:correct_configuration?).and_call_original }
62
63
 
@@ -9,7 +9,7 @@ RSpec.describe RobustClientSocket::HTTP::Client do
9
9
  let(:credentials) do
10
10
  {
11
11
  base_uri: 'https://example.com',
12
- public_key: OpenSSL::PKey::RSA.new(2048).public_key.to_s
12
+ public_key: OpenSSL::PKey.generate_key('X25519').public_to_pem
13
13
  }
14
14
  end
15
15
  let(:client_name) { 'test_client' }
@@ -3,6 +3,7 @@ require 'openssl'
3
3
  require 'base64'
4
4
  require './lib/version.rb'
5
5
  require './lib/robust_client_socket/http/helpers.rb'
6
+ require './lib/robust_client_socket/token_cipher.rb'
6
7
 
7
8
  RSpec.describe RobustClientSocket::HTTP::Helpers do
8
9
  let(:dummy_class) do
@@ -34,8 +35,8 @@ RSpec.describe RobustClientSocket::HTTP::Helpers do
34
35
 
35
36
  describe '.secure_token' do
36
37
  it 'returns the encrypted token' do
37
- rsa_key = OpenSSL::PKey::RSA.new(2048)
38
- allow(dummy_class).to receive(:public_key).and_return(rsa_key.public_key.to_s)
38
+ public_key = OpenSSL::PKey.generate_key('X25519').public_to_pem
39
+ allow(dummy_class).to receive(:public_key).and_return(public_key)
39
40
  allow(dummy_class).to receive(:app_token).and_return('app_token')
40
41
 
41
42
  result = dummy_class.send(:secure_token)
@@ -60,16 +61,16 @@ RSpec.describe RobustClientSocket::HTTP::Helpers do
60
61
  end
61
62
 
62
63
  describe '.time_now_in_utc' do
63
- it 'returns current UTC timestamp' do
64
- allow(Time).to receive_message_chain(:now, :utc, :to_i).and_return(1234567890)
65
- expect(dummy_class.send(:time_now_in_utc)).to eq(1234567890)
64
+ it 'returns current UTC timestamp in milliseconds' do
65
+ allow(Process).to receive(:clock_gettime).with(Process::CLOCK_REALTIME, :millisecond).and_return(1234567890123)
66
+ expect(dummy_class.send(:time_now_in_utc)).to eq(1234567890123)
66
67
  end
67
68
  end
68
69
 
69
70
  describe '.encrypted_data' do
70
71
  it 'encrypts and base64 encodes data' do
71
- rsa_key = OpenSSL::PKey::RSA.new(2048)
72
- allow(dummy_class).to receive(:public_key).and_return(rsa_key.public_key.to_s)
72
+ public_key = OpenSSL::PKey.generate_key('X25519').public_to_pem
73
+ allow(dummy_class).to receive(:public_key).and_return(public_key)
73
74
 
74
75
  result = dummy_class.send(:encrypted_data, 'test_data')
75
76
  expect(result).to be_a(String)
@@ -19,7 +19,7 @@ RSpec.describe RobustClientSocket::HTTP::HTTPartyOverrides do
19
19
 
20
20
  describe '.perform_request' do
21
21
  before do
22
- test_class.credentials = { public_key: OpenSSL::PKey::RSA.new(2048).public_key.to_s }
22
+ test_class.credentials = { public_key: OpenSSL::PKey.generate_key('X25519').public_to_pem }
23
23
  test_class.client_name = 'test_client'
24
24
  allow(Time).to receive_message_chain(:now, :utc, :to_i).and_return(12345)
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: robust_client_socket
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taras Zhuk
@@ -9,6 +9,20 @@ bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: base64
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
12
26
  - !ruby/object:Gem::Dependency
13
27
  name: oj
14
28
  requirement: !ruby/object:Gem::Requirement
@@ -73,15 +87,20 @@ extensions: []
73
87
  extra_rdoc_files: []
74
88
  files:
75
89
  - ".rspec"
90
+ - BENCHMARK_ANALYSIS.md
76
91
  - CODE_OF_CONDUCT.md
77
92
  - LICENSE.txt
93
+ - README.en.md
78
94
  - README.md
79
95
  - Rakefile
96
+ - img.png
97
+ - img_1.png
80
98
  - lib/robust_client_socket.rb
81
99
  - lib/robust_client_socket/configuration.rb
82
100
  - lib/robust_client_socket/http/client.rb
83
101
  - lib/robust_client_socket/http/helpers.rb
84
102
  - lib/robust_client_socket/http/httparty_overrides.rb
103
+ - lib/robust_client_socket/token_cipher.rb
85
104
  - lib/version.rb
86
105
  - robust_client_socket.gemspec
87
106
  - spec/lib/payrent_client_socket.rb
@@ -96,7 +115,6 @@ metadata: {}
96
115
  rdoc_options: []
97
116
  require_paths:
98
117
  - lib
99
- - config
100
118
  required_ruby_version: !ruby/object:Gem::Requirement
101
119
  requirements:
102
120
  - - ">="
@@ -108,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
108
126
  - !ruby/object:Gem::Version
109
127
  version: '0'
110
128
  requirements: []
111
- rubygems_version: 3.6.9
129
+ rubygems_version: 4.0.6
112
130
  specification_version: 4
113
131
  summary: Robust Client Socket
114
132
  test_files: []